Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / libpcap / gencode.c
blob9cdc479c847ca34ace5e578b9724a3dd145f645d
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: /pub/NetBSD/misc/repositories/cvsroot/src/dist/libpcap/gencode.c,v 1.4 2006/10/15 19:27:21 christos 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 #include <sys/types.h>
35 #include <sys/socket.h>
36 #endif /* WIN32 */
39 * XXX - why was this included even on UNIX?
41 #ifdef __MINGW32__
42 #include "IP6_misc.h"
43 #endif
45 #ifndef WIN32
47 #ifdef __NetBSD__
48 #include <sys/param.h>
49 #endif
51 #include <netinet/in.h>
53 #endif /* WIN32 */
55 #include <stdlib.h>
56 #include <string.h>
57 #include <memory.h>
58 #include <setjmp.h>
59 #include <stdarg.h>
61 #ifdef MSDOS
62 #include "pcap-dos.h"
63 #endif
65 #include "pcap-int.h"
67 #include "ethertype.h"
68 #include "nlpid.h"
69 #include "llc.h"
70 #include "gencode.h"
71 #include "atmuni31.h"
72 #include "sunatmpos.h"
73 #include "ppp.h"
74 #include "sll.h"
75 #include "arcnet.h"
76 #include "pf.h"
77 #ifndef offsetof
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
79 #endif
80 #ifdef INET6
81 #ifndef WIN32
82 #include <netdb.h> /* for "struct addrinfo" */
83 #endif /* WIN32 */
84 #endif /*INET6*/
85 #include <pcap-namedb.h>
87 #define ETHERMTU 1500
89 #ifndef IPPROTO_SCTP
90 #define IPPROTO_SCTP 132
91 #endif
93 #ifdef HAVE_OS_PROTO_H
94 #include "os-proto.h"
95 #endif
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
99 /* Locals */
100 static jmp_buf top_ctx;
101 static pcap_t *bpf_pcap;
103 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
104 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
106 /* XXX */
107 #ifdef PCAP_FDDIPAD
108 static int pcap_fddipad;
109 #endif
111 /* VARARGS */
112 void
113 bpf_error(const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 if (bpf_pcap != NULL)
119 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
120 fmt, ap);
121 va_end(ap);
122 longjmp(top_ctx, 1);
123 /* NOTREACHED */
126 static void init_linktype(pcap_t *);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block *root;
134 * Value passed to gen_load_a() to indicate what the offset argument
135 * is relative to.
137 enum e_offrel {
138 OR_PACKET, /* relative to the beginning of the packet */
139 OR_LINK, /* relative to the link-layer header */
140 OR_NET, /* relative to the network-layer header */
141 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
154 #define NCHUNKS 16
155 #define CHUNK0SIZE 1024
156 struct chunk {
157 u_int n_left;
158 void *m;
161 static struct chunk chunks[NCHUNKS];
162 static int cur_chunk;
164 static void *newchunk(u_int);
165 static void freechunks(void);
166 static inline struct block *new_block(int);
167 static inline struct slist *new_stmt(int);
168 static struct block *gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block *, struct block *);
172 static void merge(struct block *, struct block *);
173 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
174 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
175 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
176 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
177 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
178 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
179 bpf_u_int32);
180 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
181 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
182 bpf_u_int32, bpf_u_int32, int, bpf_int32);
183 static struct slist *gen_load_llrel(u_int, u_int);
184 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
185 static struct slist *gen_loadx_iphdrlen(void);
186 static struct block *gen_uncond(int);
187 static inline struct block *gen_true(void);
188 static inline struct block *gen_false(void);
189 static struct block *gen_ether_linktype(int);
190 static struct block *gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block *);
192 static void insert_load_llprefixlen(struct block *);
193 static struct slist *gen_llprefixlen(void);
194 static struct block *gen_linktype(int);
195 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
196 static struct block *gen_llc_linktype(int);
197 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
198 #ifdef INET6
199 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
200 #endif
201 static struct block *gen_ahostop(const u_char *, int);
202 static struct block *gen_ehostop(const u_char *, int);
203 static struct block *gen_fhostop(const u_char *, int);
204 static struct block *gen_thostop(const u_char *, int);
205 static struct block *gen_wlanhostop(const u_char *, int);
206 static struct block *gen_ipfchostop(const u_char *, int);
207 static struct block *gen_dnhostop(bpf_u_int32, int);
208 static struct block *gen_mpls_linktype(int);
209 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
210 #ifdef INET6
211 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
212 #endif
213 #ifndef INET6
214 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
215 #endif
216 static struct block *gen_ipfrag(void);
217 static struct block *gen_portatom(int, bpf_int32);
218 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
219 #ifdef INET6
220 static struct block *gen_portatom6(int, bpf_int32);
221 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
222 #endif
223 struct block *gen_portop(int, int, int);
224 static struct block *gen_port(int, int, int);
225 struct block *gen_portrangeop(int, int, int, int);
226 static struct block *gen_portrange(int, int, int, int);
227 #ifdef INET6
228 struct block *gen_portop6(int, int, int);
229 static struct block *gen_port6(int, int, int);
230 struct block *gen_portrangeop6(int, int, int, int);
231 static struct block *gen_portrange6(int, int, int, int);
232 #endif
233 static int lookup_proto(const char *, int);
234 static struct block *gen_protochain(int, int, int);
235 static struct block *gen_proto(int, int, int);
236 static struct slist *xfer_to_x(struct arth *);
237 static struct slist *xfer_to_a(struct arth *);
238 static struct block *gen_mac_multicast(int);
239 static struct block *gen_len(int, int);
241 static struct block *gen_msg_abbrev(int type);
243 static void *
244 newchunk(n)
245 u_int n;
247 struct chunk *cp;
248 int k;
249 size_t size;
251 #ifndef __NetBSD__
252 /* XXX Round up to nearest long. */
253 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
254 #else
255 /* XXX Round up to structure boundary. */
256 n = ALIGN(n);
257 #endif
259 cp = &chunks[cur_chunk];
260 if (n > cp->n_left) {
261 ++cp, k = ++cur_chunk;
262 if (k >= NCHUNKS)
263 bpf_error("out of memory");
264 size = CHUNK0SIZE << k;
265 cp->m = (void *)malloc(size);
266 if (cp->m == NULL)
267 bpf_error("out of memory");
268 memset((char *)cp->m, 0, size);
269 cp->n_left = size;
270 if (n > size)
271 bpf_error("out of memory");
273 cp->n_left -= n;
274 return (void *)((char *)cp->m + cp->n_left);
277 static void
278 freechunks()
280 int i;
282 cur_chunk = 0;
283 for (i = 0; i < NCHUNKS; ++i)
284 if (chunks[i].m != NULL) {
285 free(chunks[i].m);
286 chunks[i].m = NULL;
291 * A strdup whose allocations are freed after code generation is over.
293 char *
294 sdup(s)
295 register const char *s;
297 int n = strlen(s) + 1;
298 char *cp = newchunk(n);
300 strlcpy(cp, s, n);
301 return (cp);
304 static inline struct block *
305 new_block(code)
306 int code;
308 struct block *p;
310 p = (struct block *)newchunk(sizeof(*p));
311 p->s.code = code;
312 p->head = p;
314 return p;
317 static inline struct slist *
318 new_stmt(code)
319 int code;
321 struct slist *p;
323 p = (struct slist *)newchunk(sizeof(*p));
324 p->s.code = code;
326 return p;
329 static struct block *
330 gen_retblk(v)
331 int v;
333 struct block *b = new_block(BPF_RET|BPF_K);
335 b->s.k = v;
336 return b;
339 static inline void
340 syntax()
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask;
346 static int snaplen;
347 int no_optimize;
350 pcap_compile(pcap_t *p, struct bpf_program *program,
351 const char *buf, int optimize, bpf_u_int32 mask)
353 extern int n_errors;
354 const char * volatile xbuf = buf;
355 int len;
357 no_optimize = 0;
358 n_errors = 0;
359 root = NULL;
360 bpf_pcap = p;
361 if (setjmp(top_ctx)) {
362 lex_cleanup();
363 freechunks();
364 return (-1);
367 netmask = mask;
369 snaplen = pcap_snapshot(p);
370 if (snaplen == 0) {
371 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
372 "snaplen of 0 rejects all packets");
373 return -1;
376 lex_init(xbuf ? xbuf : "");
377 init_linktype(p);
378 (void)pcap_parse();
380 if (n_errors)
381 syntax();
383 if (root == NULL)
384 root = gen_retblk(snaplen);
386 if (optimize && !no_optimize) {
387 bpf_optimize(&root);
388 if (root == NULL ||
389 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
390 bpf_error("expression rejects all packets");
392 program->bf_insns = icode_to_fcode(root, &len);
393 program->bf_len = len;
395 lex_cleanup();
396 freechunks();
397 return (0);
401 * entry point for using the compiler with no pcap open
402 * pass in all the stuff that is needed explicitly instead.
405 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
406 struct bpf_program *program,
407 const char *buf, int optimize, bpf_u_int32 mask)
409 pcap_t *p;
410 int ret;
412 p = pcap_open_dead(linktype_arg, snaplen_arg);
413 if (p == NULL)
414 return (-1);
415 ret = pcap_compile(p, program, buf, optimize, mask);
416 pcap_close(p);
417 return (ret);
421 * Clean up a "struct bpf_program" by freeing all the memory allocated
422 * in it.
424 void
425 pcap_freecode(struct bpf_program *program)
427 program->bf_len = 0;
428 if (program->bf_insns != NULL) {
429 free((char *)program->bf_insns);
430 program->bf_insns = NULL;
435 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
436 * which of the jt and jf fields has been resolved and which is a pointer
437 * back to another unresolved block (or nil). At least one of the fields
438 * in each block is already resolved.
440 static void
441 backpatch(list, target)
442 struct block *list, *target;
444 struct block *next;
446 while (list) {
447 if (!list->sense) {
448 next = JT(list);
449 JT(list) = target;
450 } else {
451 next = JF(list);
452 JF(list) = target;
454 list = next;
459 * Merge the lists in b0 and b1, using the 'sense' field to indicate
460 * which of jt and jf is the link.
462 static void
463 merge(b0, b1)
464 struct block *b0, *b1;
466 register struct block **p = &b0;
468 /* Find end of list. */
469 while (*p)
470 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
472 /* Concatenate the lists. */
473 *p = b1;
476 void
477 finish_parse(p)
478 struct block *p;
480 backpatch(p, gen_retblk(snaplen));
481 p->sense = !p->sense;
482 backpatch(p, gen_retblk(0));
483 root = p->head;
486 * Insert before the statements of the first (root) block any
487 * statements needed to load the lengths of any variable-length
488 * headers into registers.
490 * XXX - a fancier strategy would be to insert those before the
491 * statements of all blocks that use those lengths and that
492 * have no predecessors that use them, so that we only compute
493 * the lengths if we need them. There might be even better
494 * approaches than that. However, as we're currently only
495 * handling variable-length radiotap headers, and as all
496 * filtering expressions other than raw link[M:N] tests
497 * require the length of that header, doing more for that
498 * header length isn't really worth the effort.
500 insert_load_llprefixlen(root);
503 void
504 gen_and(b0, b1)
505 struct block *b0, *b1;
507 backpatch(b0, b1->head);
508 b0->sense = !b0->sense;
509 b1->sense = !b1->sense;
510 merge(b1, b0);
511 b1->sense = !b1->sense;
512 b1->head = b0->head;
515 void
516 gen_or(b0, b1)
517 struct block *b0, *b1;
519 b0->sense = !b0->sense;
520 backpatch(b0, b1->head);
521 b0->sense = !b0->sense;
522 merge(b1, b0);
523 b1->head = b0->head;
526 void
527 gen_not(b)
528 struct block *b;
530 b->sense = !b->sense;
533 static struct block *
534 gen_cmp(offrel, offset, size, v)
535 enum e_offrel offrel;
536 u_int offset, size;
537 bpf_int32 v;
539 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
542 static struct block *
543 gen_cmp_gt(offrel, offset, size, v)
544 enum e_offrel offrel;
545 u_int offset, size;
546 bpf_int32 v;
548 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
551 static struct block *
552 gen_cmp_ge(offrel, offset, size, v)
553 enum e_offrel offrel;
554 u_int offset, size;
555 bpf_int32 v;
557 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
560 static struct block *
561 gen_cmp_lt(offrel, offset, size, v)
562 enum e_offrel offrel;
563 u_int offset, size;
564 bpf_int32 v;
566 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
569 static struct block *
570 gen_cmp_le(offrel, offset, size, v)
571 enum e_offrel offrel;
572 u_int offset, size;
573 bpf_int32 v;
575 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
578 static struct block *
579 gen_mcmp(offrel, offset, size, v, mask)
580 enum e_offrel offrel;
581 u_int offset, size;
582 bpf_int32 v;
583 bpf_u_int32 mask;
585 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
588 static struct block *
589 gen_bcmp(offrel, offset, size, v)
590 enum e_offrel offrel;
591 register u_int offset, size;
592 register const u_char *v;
594 register struct block *b, *tmp;
596 b = NULL;
597 while (size >= 4) {
598 register const u_char *p = &v[size - 4];
599 bpf_int32 w = ((bpf_int32)p[0] << 24) |
600 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
602 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
603 if (b != NULL)
604 gen_and(b, tmp);
605 b = tmp;
606 size -= 4;
608 while (size >= 2) {
609 register const u_char *p = &v[size - 2];
610 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
612 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
613 if (b != NULL)
614 gen_and(b, tmp);
615 b = tmp;
616 size -= 2;
618 if (size > 0) {
619 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
620 if (b != NULL)
621 gen_and(b, tmp);
622 b = tmp;
624 return b;
628 * AND the field of size "size" at offset "offset" relative to the header
629 * specified by "offrel" with "mask", and compare it with the value "v"
630 * with the test specified by "jtype"; if "reverse" is true, the test
631 * should test the opposite of "jtype".
633 static struct block *
634 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
635 enum e_offrel offrel;
636 bpf_int32 v;
637 bpf_u_int32 offset, size, mask, jtype;
638 int reverse;
640 struct slist *s, *s2;
641 struct block *b;
643 s = gen_load_a(offrel, offset, size);
645 if (mask != 0xffffffff) {
646 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
647 s2->s.k = mask;
648 sappend(s, s2);
651 b = new_block(JMP(jtype));
652 b->stmts = s;
653 b->s.k = v;
654 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
655 gen_not(b);
656 return b;
660 * Various code constructs need to know the layout of the data link
661 * layer. These variables give the necessary offsets from the beginning
662 * of the packet data.
664 * If the link layer has variable_length headers, the offsets are offsets
665 * from the end of the link-link-layer header, and "reg_ll_size" is
666 * the register number for a register containing the length of the
667 * link-layer header. Otherwise, "reg_ll_size" is -1.
669 static int reg_ll_size;
672 * This is the offset of the beginning of the link-layer header.
673 * It's usually 0, except for 802.11 with a fixed-length radio header.
675 static u_int off_ll;
678 * This is the offset of the beginning of the MAC-layer header.
679 * It's usually 0, except for ATM LANE.
681 static u_int off_mac;
684 * "off_linktype" is the offset to information in the link-layer header
685 * giving the packet type.
687 * For Ethernet, it's the offset of the Ethernet type field.
689 * For link-layer types that always use 802.2 headers, it's the
690 * offset of the LLC header.
692 * For PPP, it's the offset of the PPP type field.
694 * For Cisco HDLC, it's the offset of the CHDLC type field.
696 * For BSD loopback, it's the offset of the AF_ value.
698 * For Linux cooked sockets, it's the offset of the type field.
700 * It's set to -1 for no encapsulation, in which case, IP is assumed.
702 static u_int off_linktype;
705 * TRUE if the link layer includes an ATM pseudo-header.
707 static int is_atm = 0;
710 * TRUE if "lane" appeared in the filter; it causes us to generate
711 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
713 static int is_lane = 0;
716 * These are offsets for the ATM pseudo-header.
718 static u_int off_vpi;
719 static u_int off_vci;
720 static u_int off_proto;
723 * These are offsets for the MTP3 fields.
725 static u_int off_sio;
726 static u_int off_opc;
727 static u_int off_dpc;
728 static u_int off_sls;
731 * This is the offset of the first byte after the ATM pseudo_header,
732 * or -1 if there is no ATM pseudo-header.
734 static u_int off_payload;
737 * These are offsets to the beginning of the network-layer header.
739 * If the link layer never uses 802.2 LLC:
741 * "off_nl" and "off_nl_nosnap" are the same.
743 * If the link layer always uses 802.2 LLC:
745 * "off_nl" is the offset if there's a SNAP header following
746 * the 802.2 header;
748 * "off_nl_nosnap" is the offset if there's no SNAP header.
750 * If the link layer is Ethernet:
752 * "off_nl" is the offset if the packet is an Ethernet II packet
753 * (we assume no 802.3+802.2+SNAP);
755 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
756 * with an 802.2 header following it.
758 static u_int off_nl;
759 static u_int off_nl_nosnap;
761 static int linktype;
763 static void
764 init_linktype(p)
765 pcap_t *p;
767 linktype = pcap_datalink(p);
768 #ifdef PCAP_FDDIPAD
769 pcap_fddipad = p->fddipad;
770 #endif
773 * Assume it's not raw ATM with a pseudo-header, for now.
775 off_mac = 0;
776 is_atm = 0;
777 is_lane = 0;
778 off_vpi = -1;
779 off_vci = -1;
780 off_proto = -1;
781 off_payload = -1;
784 * And assume we're not doing SS7.
786 off_sio = -1;
787 off_opc = -1;
788 off_dpc = -1;
789 off_sls = -1;
792 * Also assume it's not 802.11 with a fixed-length radio header.
794 off_ll = 0;
796 orig_linktype = -1;
797 orig_nl = -1;
798 label_stack_depth = 0;
800 reg_ll_size = -1;
802 switch (linktype) {
804 case DLT_ARCNET:
805 off_linktype = 2;
806 off_nl = 6; /* XXX in reality, variable! */
807 off_nl_nosnap = 6; /* no 802.2 LLC */
808 return;
810 case DLT_ARCNET_LINUX:
811 off_linktype = 4;
812 off_nl = 8; /* XXX in reality, variable! */
813 off_nl_nosnap = 8; /* no 802.2 LLC */
814 return;
816 case DLT_EN10MB:
817 off_linktype = 12;
818 off_nl = 14; /* Ethernet II */
819 off_nl_nosnap = 17; /* 802.3+802.2 */
820 return;
822 case DLT_SLIP:
824 * SLIP doesn't have a link level type. The 16 byte
825 * header is hacked into our SLIP driver.
827 off_linktype = -1;
828 off_nl = 16;
829 off_nl_nosnap = 16; /* no 802.2 LLC */
830 return;
832 case DLT_SLIP_BSDOS:
833 /* XXX this may be the same as the DLT_PPP_BSDOS case */
834 off_linktype = -1;
835 /* XXX end */
836 off_nl = 24;
837 off_nl_nosnap = 24; /* no 802.2 LLC */
838 return;
840 case DLT_NULL:
841 case DLT_LOOP:
842 off_linktype = 0;
843 off_nl = 4;
844 off_nl_nosnap = 4; /* no 802.2 LLC */
845 return;
847 case DLT_ENC:
848 off_linktype = 0;
849 off_nl = 12;
850 off_nl_nosnap = 12; /* no 802.2 LLC */
851 return;
853 case DLT_PPP:
854 case DLT_PPP_PPPD:
855 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
856 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
857 off_linktype = 2;
858 off_nl = 4;
859 off_nl_nosnap = 4; /* no 802.2 LLC */
860 return;
862 case DLT_PPP_ETHER:
864 * This does no include the Ethernet header, and
865 * only covers session state.
867 off_linktype = 6;
868 off_nl = 8;
869 off_nl_nosnap = 8; /* no 802.2 LLC */
870 return;
872 case DLT_PPP_BSDOS:
873 off_linktype = 5;
874 off_nl = 24;
875 off_nl_nosnap = 24; /* no 802.2 LLC */
876 return;
878 case DLT_FDDI:
880 * FDDI doesn't really have a link-level type field.
881 * We set "off_linktype" to the offset of the LLC header.
883 * To check for Ethernet types, we assume that SSAP = SNAP
884 * is being used and pick out the encapsulated Ethernet type.
885 * XXX - should we generate code to check for SNAP?
887 off_linktype = 13;
888 #ifdef PCAP_FDDIPAD
889 off_linktype += pcap_fddipad;
890 #endif
891 off_nl = 21; /* FDDI+802.2+SNAP */
892 off_nl_nosnap = 16; /* FDDI+802.2 */
893 #ifdef PCAP_FDDIPAD
894 off_nl += pcap_fddipad;
895 off_nl_nosnap += pcap_fddipad;
896 #endif
897 return;
899 case DLT_IEEE802:
901 * Token Ring doesn't really have a link-level type field.
902 * We set "off_linktype" to the offset of the LLC header.
904 * To check for Ethernet types, we assume that SSAP = SNAP
905 * is being used and pick out the encapsulated Ethernet type.
906 * XXX - should we generate code to check for SNAP?
908 * XXX - the header is actually variable-length.
909 * Some various Linux patched versions gave 38
910 * as "off_linktype" and 40 as "off_nl"; however,
911 * if a token ring packet has *no* routing
912 * information, i.e. is not source-routed, the correct
913 * values are 20 and 22, as they are in the vanilla code.
915 * A packet is source-routed iff the uppermost bit
916 * of the first byte of the source address, at an
917 * offset of 8, has the uppermost bit set. If the
918 * packet is source-routed, the total number of bytes
919 * of routing information is 2 plus bits 0x1F00 of
920 * the 16-bit value at an offset of 14 (shifted right
921 * 8 - figure out which byte that is).
923 off_linktype = 14;
924 off_nl = 22; /* Token Ring+802.2+SNAP */
925 off_nl_nosnap = 17; /* Token Ring+802.2 */
926 return;
928 case DLT_IEEE802_11:
930 * 802.11 doesn't really have a link-level type field.
931 * We set "off_linktype" to the offset of the LLC header.
933 * To check for Ethernet types, we assume that SSAP = SNAP
934 * is being used and pick out the encapsulated Ethernet type.
935 * XXX - should we generate code to check for SNAP?
937 * XXX - the header is actually variable-length. We
938 * assume a 24-byte link-layer header, as appears in
939 * data frames in networks with no bridges. If the
940 * fromds and tods 802.11 header bits are both set,
941 * it's actually supposed to be 30 bytes.
943 off_linktype = 24;
944 off_nl = 32; /* 802.11+802.2+SNAP */
945 off_nl_nosnap = 27; /* 802.11+802.2 */
946 return;
948 case DLT_PRISM_HEADER:
950 * Same as 802.11, but with an additional header before
951 * the 802.11 header, containing a bunch of additional
952 * information including radio-level information.
954 * The header is 144 bytes long.
956 * XXX - same variable-length header problem; at least
957 * the Prism header is fixed-length.
959 off_ll = 144;
960 off_linktype = 144+24;
961 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
962 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
963 return;
965 case DLT_IEEE802_11_RADIO_AVS:
967 * Same as 802.11, but with an additional header before
968 * the 802.11 header, containing a bunch of additional
969 * information including radio-level information.
971 * The header is 64 bytes long, at least in its
972 * current incarnation.
974 * XXX - same variable-length header problem, only
975 * more so; this header is also variable-length,
976 * with the length being the 32-bit big-endian
977 * number at an offset of 4 from the beginning
978 * of the radio header.
980 off_ll = 64;
981 off_linktype = 64+24;
982 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
983 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
984 return;
986 case DLT_IEEE802_11_RADIO:
988 * Same as 802.11, but with an additional header before
989 * the 802.11 header, containing a bunch of additional
990 * information including radio-level information.
992 * The radiotap header is variable length, and we
993 * generate code to compute its length and store it
994 * in a register. These offsets are relative to the
995 * beginning of the 802.11 header.
997 off_linktype = 24;
998 off_nl = 32; /* 802.11+802.2+SNAP */
999 off_nl_nosnap = 27; /* 802.11+802.2 */
1000 return;
1002 case DLT_ATM_RFC1483:
1003 case DLT_ATM_CLIP: /* Linux ATM defines this */
1005 * assume routed, non-ISO PDUs
1006 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1008 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1009 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1010 * latter would presumably be treated the way PPPoE
1011 * should be, so you can do "pppoe and udp port 2049"
1012 * or "pppoa and tcp port 80" and have it check for
1013 * PPPo{A,E} and a PPP protocol of IP and....
1015 off_linktype = 0;
1016 off_nl = 8; /* 802.2+SNAP */
1017 off_nl_nosnap = 3; /* 802.2 */
1018 return;
1020 case DLT_SUNATM:
1022 * Full Frontal ATM; you get AALn PDUs with an ATM
1023 * pseudo-header.
1025 is_atm = 1;
1026 off_vpi = SUNATM_VPI_POS;
1027 off_vci = SUNATM_VCI_POS;
1028 off_proto = PROTO_POS;
1029 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1030 off_payload = SUNATM_PKT_BEGIN_POS;
1031 off_linktype = off_payload;
1032 off_nl = off_payload+8; /* 802.2+SNAP */
1033 off_nl_nosnap = off_payload+3; /* 802.2 */
1034 return;
1036 case DLT_RAW:
1037 off_linktype = -1;
1038 off_nl = 0;
1039 off_nl_nosnap = 0; /* no 802.2 LLC */
1040 return;
1042 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1043 off_linktype = 14;
1044 off_nl = 16;
1045 off_nl_nosnap = 16; /* no 802.2 LLC */
1046 return;
1048 case DLT_LTALK:
1050 * LocalTalk does have a 1-byte type field in the LLAP header,
1051 * but really it just indicates whether there is a "short" or
1052 * "long" DDP packet following.
1054 off_linktype = -1;
1055 off_nl = 0;
1056 off_nl_nosnap = 0; /* no 802.2 LLC */
1057 return;
1059 case DLT_IP_OVER_FC:
1061 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1062 * link-level type field. We set "off_linktype" to the
1063 * offset of the LLC header.
1065 * To check for Ethernet types, we assume that SSAP = SNAP
1066 * is being used and pick out the encapsulated Ethernet type.
1067 * XXX - should we generate code to check for SNAP? RFC
1068 * 2625 says SNAP should be used.
1070 off_linktype = 16;
1071 off_nl = 24; /* IPFC+802.2+SNAP */
1072 off_nl_nosnap = 19; /* IPFC+802.2 */
1073 return;
1075 case DLT_FRELAY:
1077 * XXX - we should set this to handle SNAP-encapsulated
1078 * frames (NLPID of 0x80).
1080 off_linktype = -1;
1081 off_nl = 0;
1082 off_nl_nosnap = 0; /* no 802.2 LLC */
1083 return;
1085 case DLT_APPLE_IP_OVER_IEEE1394:
1086 off_linktype = 16;
1087 off_nl = 18;
1088 off_nl_nosnap = 18; /* no 802.2 LLC */
1089 return;
1091 case DLT_LINUX_IRDA:
1093 * Currently, only raw "link[N:M]" filtering is supported.
1095 off_linktype = -1;
1096 off_nl = -1;
1097 off_nl_nosnap = -1;
1098 return;
1100 case DLT_DOCSIS:
1102 * Currently, only raw "link[N:M]" filtering is supported.
1104 off_linktype = -1;
1105 off_nl = -1;
1106 off_nl_nosnap = -1;
1107 return;
1109 case DLT_SYMANTEC_FIREWALL:
1110 off_linktype = 6;
1111 off_nl = 44; /* Ethernet II */
1112 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1113 return;
1115 case DLT_PFLOG:
1116 off_linktype = 0;
1117 /* XXX read this from pf.h? */
1118 off_nl = PFLOG_HDRLEN;
1119 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1120 return;
1122 case DLT_JUNIPER_MFR:
1123 case DLT_JUNIPER_MLFR:
1124 case DLT_JUNIPER_MLPPP:
1125 case DLT_JUNIPER_PPP:
1126 case DLT_JUNIPER_CHDLC:
1127 case DLT_JUNIPER_FRELAY:
1128 off_linktype = 4;
1129 off_nl = 4;
1130 off_nl_nosnap = -1; /* no 802.2 LLC */
1131 return;
1133 case DLT_JUNIPER_ATM1:
1134 off_linktype = 4; /* in reality variable between 4-8 */
1135 off_nl = 4;
1136 off_nl_nosnap = 14;
1137 return;
1139 case DLT_JUNIPER_ATM2:
1140 off_linktype = 8; /* in reality variable between 8-12 */
1141 off_nl = 8;
1142 off_nl_nosnap = 18;
1143 return;
1145 /* frames captured on a Juniper PPPoE service PIC
1146 * contain raw ethernet frames */
1147 case DLT_JUNIPER_PPPOE:
1148 case DLT_JUNIPER_ETHER:
1149 off_linktype = 16;
1150 off_nl = 18; /* Ethernet II */
1151 off_nl_nosnap = 21; /* 802.3+802.2 */
1152 return;
1154 case DLT_JUNIPER_PPPOE_ATM:
1155 off_linktype = 4;
1156 off_nl = 6;
1157 off_nl_nosnap = -1; /* no 802.2 LLC */
1158 return;
1160 case DLT_JUNIPER_GGSN:
1161 off_linktype = 6;
1162 off_nl = 12;
1163 off_nl_nosnap = -1; /* no 802.2 LLC */
1164 return;
1166 case DLT_JUNIPER_ES:
1167 off_linktype = 6;
1168 off_nl = -1; /* not really a network layer but raw IP adresses */
1169 off_nl_nosnap = -1; /* no 802.2 LLC */
1170 return;
1172 case DLT_JUNIPER_MONITOR:
1173 off_linktype = 12;
1174 off_nl = 12; /* raw IP/IP6 header */
1175 off_nl_nosnap = -1; /* no 802.2 LLC */
1176 return;
1178 case DLT_JUNIPER_SERVICES:
1179 off_linktype = 12;
1180 off_nl = -1; /* L3 proto location dep. on cookie type */
1181 off_nl_nosnap = -1; /* no 802.2 LLC */
1182 return;
1184 case DLT_MTP2:
1185 off_sio = 3;
1186 off_opc = 4;
1187 off_dpc = 4;
1188 off_sls = 7;
1189 off_linktype = -1;
1190 off_nl = -1;
1191 off_nl_nosnap = -1;
1192 return;
1194 #ifdef DLT_PFSYNC
1195 case DLT_PFSYNC:
1196 off_linktype = -1;
1197 off_nl = 4;
1198 off_nl_nosnap = 4;
1199 return;
1200 #endif
1202 case DLT_LINUX_LAPD:
1204 * Currently, only raw "link[N:M]" filtering is supported.
1206 off_linktype = -1;
1207 off_nl = -1;
1208 off_nl_nosnap = -1;
1209 return;
1211 bpf_error("unknown data link type %d", linktype);
1212 /* NOTREACHED */
1216 * Load a value relative to the beginning of the link-layer header.
1217 * The link-layer header doesn't necessarily begin at the beginning
1218 * of the packet data; there might be a variable-length prefix containing
1219 * radio information.
1221 static struct slist *
1222 gen_load_llrel(offset, size)
1223 u_int offset, size;
1225 struct slist *s, *s2;
1227 s = gen_llprefixlen();
1230 * If "s" is non-null, it has code to arrange that the X register
1231 * contains the length of the prefix preceding the link-layer
1232 * header.
1234 if (s != NULL) {
1235 s2 = new_stmt(BPF_LD|BPF_IND|size);
1236 s2->s.k = offset;
1237 sappend(s, s2);
1238 } else {
1239 s = new_stmt(BPF_LD|BPF_ABS|size);
1240 s->s.k = offset;
1242 return s;
1246 * Load a value relative to the beginning of the specified header.
1248 static struct slist *
1249 gen_load_a(offrel, offset, size)
1250 enum e_offrel offrel;
1251 u_int offset, size;
1253 struct slist *s, *s2;
1255 switch (offrel) {
1257 case OR_PACKET:
1258 s = gen_load_llrel(offset, size);
1259 break;
1261 case OR_LINK:
1262 s = gen_load_llrel(off_ll + offset, size);
1263 break;
1265 case OR_NET:
1266 s = gen_load_llrel(off_nl + offset, size);
1267 break;
1269 case OR_NET_NOSNAP:
1270 s = gen_load_llrel(off_nl_nosnap + offset, size);
1271 break;
1273 case OR_TRAN_IPV4:
1275 * Load the X register with the length of the IPv4 header,
1276 * in bytes.
1278 s = gen_loadx_iphdrlen();
1281 * Load the item at {length of the link-layer header} +
1282 * {length of the IPv4 header} + {specified offset}.
1284 s2 = new_stmt(BPF_LD|BPF_IND|size);
1285 s2->s.k = off_nl + offset;
1286 sappend(s, s2);
1287 break;
1289 case OR_TRAN_IPV6:
1290 s = gen_load_llrel(off_nl + 40 + offset, size);
1291 break;
1293 default:
1294 abort();
1295 return NULL;
1297 return s;
1301 * Generate code to load into the X register the sum of the length of
1302 * the IPv4 header and any variable-length header preceding the link-layer
1303 * header.
1305 static struct slist *
1306 gen_loadx_iphdrlen()
1308 struct slist *s, *s2;
1310 s = gen_llprefixlen();
1311 if (s != NULL) {
1313 * There's a variable-length prefix preceding the
1314 * link-layer header. "s" points to a list of statements
1315 * that put the length of that prefix into the X register.
1316 * The 4*([k]&0xf) addressing mode can't be used, as we
1317 * don't have a constant offset, so we have to load the
1318 * value in question into the A register and add to it
1319 * the value from the X register.
1321 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1322 s2->s.k = off_nl;
1323 sappend(s, s2);
1324 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1325 s2->s.k = 0xf;
1326 sappend(s, s2);
1327 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1328 s2->s.k = 2;
1329 sappend(s, s2);
1332 * The A register now contains the length of the
1333 * IP header. We need to add to it the length
1334 * of the prefix preceding the link-layer
1335 * header, which is still in the X register, and
1336 * move the result into the X register.
1338 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1339 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1340 } else {
1342 * There is no variable-length header preceding the
1343 * link-layer header; if there's a fixed-length
1344 * header preceding it, its length is included in
1345 * the off_ variables, so it doesn't need to be added.
1347 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1348 s->s.k = off_nl;
1350 return s;
1353 static struct block *
1354 gen_uncond(rsense)
1355 int rsense;
1357 struct block *b;
1358 struct slist *s;
1360 s = new_stmt(BPF_LD|BPF_IMM);
1361 s->s.k = !rsense;
1362 b = new_block(JMP(BPF_JEQ));
1363 b->stmts = s;
1365 return b;
1368 static inline struct block *
1369 gen_true()
1371 return gen_uncond(1);
1374 static inline struct block *
1375 gen_false()
1377 return gen_uncond(0);
1381 * Byte-swap a 32-bit number.
1382 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1383 * big-endian platforms.)
1385 #define SWAPLONG(y) \
1386 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1389 * Generate code to match a particular packet type.
1391 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1392 * value, if <= ETHERMTU. We use that to determine whether to
1393 * match the type/length field or to check the type/length field for
1394 * a value <= ETHERMTU to see whether it's a type field and then do
1395 * the appropriate test.
1397 static struct block *
1398 gen_ether_linktype(proto)
1399 register int proto;
1401 struct block *b0, *b1;
1403 switch (proto) {
1405 case LLCSAP_ISONS:
1406 case LLCSAP_IP:
1407 case LLCSAP_NETBEUI:
1409 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1410 * so we check the DSAP and SSAP.
1412 * LLCSAP_IP checks for IP-over-802.2, rather
1413 * than IP-over-Ethernet or IP-over-SNAP.
1415 * XXX - should we check both the DSAP and the
1416 * SSAP, like this, or should we check just the
1417 * DSAP, as we do for other types <= ETHERMTU
1418 * (i.e., other SAP values)?
1420 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1421 gen_not(b0);
1422 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1423 ((proto << 8) | proto));
1424 gen_and(b0, b1);
1425 return b1;
1427 case LLCSAP_IPX:
1429 * Check for;
1431 * Ethernet_II frames, which are Ethernet
1432 * frames with a frame type of ETHERTYPE_IPX;
1434 * Ethernet_802.3 frames, which are 802.3
1435 * frames (i.e., the type/length field is
1436 * a length field, <= ETHERMTU, rather than
1437 * a type field) with the first two bytes
1438 * after the Ethernet/802.3 header being
1439 * 0xFFFF;
1441 * Ethernet_802.2 frames, which are 802.3
1442 * frames with an 802.2 LLC header and
1443 * with the IPX LSAP as the DSAP in the LLC
1444 * header;
1446 * Ethernet_SNAP frames, which are 802.3
1447 * frames with an LLC header and a SNAP
1448 * header and with an OUI of 0x000000
1449 * (encapsulated Ethernet) and a protocol
1450 * ID of ETHERTYPE_IPX in the SNAP header.
1452 * XXX - should we generate the same code both
1453 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1457 * This generates code to check both for the
1458 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1460 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1461 (bpf_int32)LLCSAP_IPX);
1462 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1463 (bpf_int32)0xFFFF);
1464 gen_or(b0, b1);
1467 * Now we add code to check for SNAP frames with
1468 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1470 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1471 gen_or(b0, b1);
1474 * Now we generate code to check for 802.3
1475 * frames in general.
1477 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1478 gen_not(b0);
1481 * Now add the check for 802.3 frames before the
1482 * check for Ethernet_802.2 and Ethernet_802.3,
1483 * as those checks should only be done on 802.3
1484 * frames, not on Ethernet frames.
1486 gen_and(b0, b1);
1489 * Now add the check for Ethernet_II frames, and
1490 * do that before checking for the other frame
1491 * types.
1493 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1494 (bpf_int32)ETHERTYPE_IPX);
1495 gen_or(b0, b1);
1496 return b1;
1498 case ETHERTYPE_ATALK:
1499 case ETHERTYPE_AARP:
1501 * EtherTalk (AppleTalk protocols on Ethernet link
1502 * layer) may use 802.2 encapsulation.
1506 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1507 * we check for an Ethernet type field less than
1508 * 1500, which means it's an 802.3 length field.
1510 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1511 gen_not(b0);
1514 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1515 * SNAP packets with an organization code of
1516 * 0x080007 (Apple, for Appletalk) and a protocol
1517 * type of ETHERTYPE_ATALK (Appletalk).
1519 * 802.2-encapsulated ETHERTYPE_AARP packets are
1520 * SNAP packets with an organization code of
1521 * 0x000000 (encapsulated Ethernet) and a protocol
1522 * type of ETHERTYPE_AARP (Appletalk ARP).
1524 if (proto == ETHERTYPE_ATALK)
1525 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1526 else /* proto == ETHERTYPE_AARP */
1527 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1528 gen_and(b0, b1);
1531 * Check for Ethernet encapsulation (Ethertalk
1532 * phase 1?); we just check for the Ethernet
1533 * protocol type.
1535 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1537 gen_or(b0, b1);
1538 return b1;
1540 default:
1541 if (proto <= ETHERMTU) {
1543 * This is an LLC SAP value, so the frames
1544 * that match would be 802.2 frames.
1545 * Check that the frame is an 802.2 frame
1546 * (i.e., that the length/type field is
1547 * a length field, <= ETHERMTU) and
1548 * then check the DSAP.
1550 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1551 gen_not(b0);
1552 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1553 (bpf_int32)proto);
1554 gen_and(b0, b1);
1555 return b1;
1556 } else {
1558 * This is an Ethernet type, so compare
1559 * the length/type field with it (if
1560 * the frame is an 802.2 frame, the length
1561 * field will be <= ETHERMTU, and, as
1562 * "proto" is > ETHERMTU, this test
1563 * will fail and the frame won't match,
1564 * which is what we want).
1566 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1567 (bpf_int32)proto);
1573 * Generate code to match a particular packet type.
1575 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1576 * value, if <= ETHERMTU. We use that to determine whether to
1577 * match the type field or to check the type field for the special
1578 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1580 static struct block *
1581 gen_linux_sll_linktype(proto)
1582 register int proto;
1584 struct block *b0, *b1;
1586 switch (proto) {
1588 case LLCSAP_ISONS:
1589 case LLCSAP_IP:
1590 case LLCSAP_NETBEUI:
1592 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1593 * so we check the DSAP and SSAP.
1595 * LLCSAP_IP checks for IP-over-802.2, rather
1596 * than IP-over-Ethernet or IP-over-SNAP.
1598 * XXX - should we check both the DSAP and the
1599 * SSAP, like this, or should we check just the
1600 * DSAP, as we do for other types <= ETHERMTU
1601 * (i.e., other SAP values)?
1603 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1604 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1605 ((proto << 8) | proto));
1606 gen_and(b0, b1);
1607 return b1;
1609 case LLCSAP_IPX:
1611 * Ethernet_II frames, which are Ethernet
1612 * frames with a frame type of ETHERTYPE_IPX;
1614 * Ethernet_802.3 frames, which have a frame
1615 * type of LINUX_SLL_P_802_3;
1617 * Ethernet_802.2 frames, which are 802.3
1618 * frames with an 802.2 LLC header (i.e, have
1619 * a frame type of LINUX_SLL_P_802_2) and
1620 * with the IPX LSAP as the DSAP in the LLC
1621 * header;
1623 * Ethernet_SNAP frames, which are 802.3
1624 * frames with an LLC header and a SNAP
1625 * header and with an OUI of 0x000000
1626 * (encapsulated Ethernet) and a protocol
1627 * ID of ETHERTYPE_IPX in the SNAP header.
1629 * First, do the checks on LINUX_SLL_P_802_2
1630 * frames; generate the check for either
1631 * Ethernet_802.2 or Ethernet_SNAP frames, and
1632 * then put a check for LINUX_SLL_P_802_2 frames
1633 * before it.
1635 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1636 (bpf_int32)LLCSAP_IPX);
1637 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1638 off_linktype + 2);
1639 gen_or(b0, b1);
1640 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1641 gen_and(b0, b1);
1644 * Now check for 802.3 frames and OR that with
1645 * the previous test.
1647 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1648 gen_or(b0, b1);
1651 * Now add the check for Ethernet_II frames, and
1652 * do that before checking for the other frame
1653 * types.
1655 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1656 (bpf_int32)ETHERTYPE_IPX);
1657 gen_or(b0, b1);
1658 return b1;
1660 case ETHERTYPE_ATALK:
1661 case ETHERTYPE_AARP:
1663 * EtherTalk (AppleTalk protocols on Ethernet link
1664 * layer) may use 802.2 encapsulation.
1668 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1669 * we check for the 802.2 protocol type in the
1670 * "Ethernet type" field.
1672 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1675 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1676 * SNAP packets with an organization code of
1677 * 0x080007 (Apple, for Appletalk) and a protocol
1678 * type of ETHERTYPE_ATALK (Appletalk).
1680 * 802.2-encapsulated ETHERTYPE_AARP packets are
1681 * SNAP packets with an organization code of
1682 * 0x000000 (encapsulated Ethernet) and a protocol
1683 * type of ETHERTYPE_AARP (Appletalk ARP).
1685 if (proto == ETHERTYPE_ATALK)
1686 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1687 off_linktype + 2);
1688 else /* proto == ETHERTYPE_AARP */
1689 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1690 off_linktype + 2);
1691 gen_and(b0, b1);
1694 * Check for Ethernet encapsulation (Ethertalk
1695 * phase 1?); we just check for the Ethernet
1696 * protocol type.
1698 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1700 gen_or(b0, b1);
1701 return b1;
1703 default:
1704 if (proto <= ETHERMTU) {
1706 * This is an LLC SAP value, so the frames
1707 * that match would be 802.2 frames.
1708 * Check for the 802.2 protocol type
1709 * in the "Ethernet type" field, and
1710 * then check the DSAP.
1712 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1713 LINUX_SLL_P_802_2);
1714 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1715 (bpf_int32)proto);
1716 gen_and(b0, b1);
1717 return b1;
1718 } else {
1720 * This is an Ethernet type, so compare
1721 * the length/type field with it (if
1722 * the frame is an 802.2 frame, the length
1723 * field will be <= ETHERMTU, and, as
1724 * "proto" is > ETHERMTU, this test
1725 * will fail and the frame won't match,
1726 * which is what we want).
1728 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1729 (bpf_int32)proto);
1734 static void
1735 insert_radiotap_load_llprefixlen(b)
1736 struct block *b;
1738 struct slist *s1, *s2;
1741 * Prepend to the statements in this block code to load the
1742 * length of the radiotap header into the register assigned
1743 * to hold that length, if one has been assigned.
1745 if (reg_ll_size != -1) {
1747 * The 2 bytes at offsets of 2 and 3 from the beginning
1748 * of the radiotap header are the length of the radiotap
1749 * header; unfortunately, it's little-endian, so we have
1750 * to load it a byte at a time and construct the value.
1754 * Load the high-order byte, at an offset of 3, shift it
1755 * left a byte, and put the result in the X register.
1757 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1758 s1->s.k = 3;
1759 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1760 sappend(s1, s2);
1761 s2->s.k = 8;
1762 s2 = new_stmt(BPF_MISC|BPF_TAX);
1763 sappend(s1, s2);
1766 * Load the next byte, at an offset of 2, and OR the
1767 * value from the X register into it.
1769 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1770 sappend(s1, s2);
1771 s2->s.k = 2;
1772 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1773 sappend(s1, s2);
1776 * Now allocate a register to hold that value and store
1777 * it.
1779 s2 = new_stmt(BPF_ST);
1780 s2->s.k = reg_ll_size;
1781 sappend(s1, s2);
1784 * Now move it into the X register.
1786 s2 = new_stmt(BPF_MISC|BPF_TAX);
1787 sappend(s1, s2);
1790 * Now append all the existing statements in this
1791 * block to these statements.
1793 sappend(s1, b->stmts);
1794 b->stmts = s1;
1799 static void
1800 insert_load_llprefixlen(b)
1801 struct block *b;
1803 switch (linktype) {
1805 case DLT_IEEE802_11_RADIO:
1806 insert_radiotap_load_llprefixlen(b);
1811 static struct slist *
1812 gen_radiotap_llprefixlen(void)
1814 struct slist *s;
1816 if (reg_ll_size == -1) {
1818 * We haven't yet assigned a register for the length
1819 * of the radiotap header; allocate one.
1821 reg_ll_size = alloc_reg();
1825 * Load the register containing the radiotap length
1826 * into the X register.
1828 s = new_stmt(BPF_LDX|BPF_MEM);
1829 s->s.k = reg_ll_size;
1830 return s;
1834 * Generate code to compute the link-layer header length, if necessary,
1835 * putting it into the X register, and to return either a pointer to a
1836 * "struct slist" for the list of statements in that code, or NULL if
1837 * no code is necessary.
1839 static struct slist *
1840 gen_llprefixlen(void)
1842 switch (linktype) {
1844 case DLT_IEEE802_11_RADIO:
1845 return gen_radiotap_llprefixlen();
1847 default:
1848 return NULL;
1853 * Generate code to match a particular packet type by matching the
1854 * link-layer type field or fields in the 802.2 LLC header.
1856 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1857 * value, if <= ETHERMTU.
1859 static struct block *
1860 gen_linktype(proto)
1861 register int proto;
1863 struct block *b0, *b1, *b2;
1865 /* are we checking MPLS-encapsulated packets? */
1866 if (label_stack_depth > 0) {
1867 switch (proto) {
1868 case ETHERTYPE_IP:
1869 case PPP_IP:
1870 /* FIXME add other L3 proto IDs */
1871 return gen_mpls_linktype(Q_IP);
1873 case ETHERTYPE_IPV6:
1874 case PPP_IPV6:
1875 /* FIXME add other L3 proto IDs */
1876 return gen_mpls_linktype(Q_IPV6);
1878 default:
1879 bpf_error("unsupported protocol over mpls");
1880 /* NOTREACHED */
1884 switch (linktype) {
1886 case DLT_EN10MB:
1887 return gen_ether_linktype(proto);
1888 /*NOTREACHED*/
1889 break;
1891 case DLT_C_HDLC:
1892 switch (proto) {
1894 case LLCSAP_ISONS:
1895 proto = (proto << 8 | LLCSAP_ISONS);
1896 /* fall through */
1898 default:
1899 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1900 (bpf_int32)proto);
1901 /*NOTREACHED*/
1902 break;
1904 break;
1906 case DLT_FDDI:
1907 case DLT_IEEE802:
1908 case DLT_IEEE802_11:
1909 case DLT_IEEE802_11_RADIO_AVS:
1910 case DLT_IEEE802_11_RADIO:
1911 case DLT_PRISM_HEADER:
1912 case DLT_ATM_RFC1483:
1913 case DLT_ATM_CLIP:
1914 case DLT_IP_OVER_FC:
1915 return gen_llc_linktype(proto);
1916 /*NOTREACHED*/
1917 break;
1919 case DLT_SUNATM:
1921 * If "is_lane" is set, check for a LANE-encapsulated
1922 * version of this protocol, otherwise check for an
1923 * LLC-encapsulated version of this protocol.
1925 * We assume LANE means Ethernet, not Token Ring.
1927 if (is_lane) {
1929 * Check that the packet doesn't begin with an
1930 * LE Control marker. (We've already generated
1931 * a test for LANE.)
1933 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
1934 0xFF00);
1935 gen_not(b0);
1938 * Now generate an Ethernet test.
1940 b1 = gen_ether_linktype(proto);
1941 gen_and(b0, b1);
1942 return b1;
1943 } else {
1945 * Check for LLC encapsulation and then check the
1946 * protocol.
1948 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1949 b1 = gen_llc_linktype(proto);
1950 gen_and(b0, b1);
1951 return b1;
1953 /*NOTREACHED*/
1954 break;
1956 case DLT_LINUX_SLL:
1957 return gen_linux_sll_linktype(proto);
1958 /*NOTREACHED*/
1959 break;
1961 case DLT_SLIP:
1962 case DLT_SLIP_BSDOS:
1963 case DLT_RAW:
1965 * These types don't provide any type field; packets
1966 * are always IP.
1968 * XXX - for IPv4, check for a version number of 4, and,
1969 * for IPv6, check for a version number of 6?
1971 switch (proto) {
1973 case ETHERTYPE_IP:
1974 #ifdef INET6
1975 case ETHERTYPE_IPV6:
1976 #endif
1977 return gen_true(); /* always true */
1979 default:
1980 return gen_false(); /* always false */
1982 /*NOTREACHED*/
1983 break;
1985 case DLT_PPP:
1986 case DLT_PPP_PPPD:
1987 case DLT_PPP_SERIAL:
1988 case DLT_PPP_ETHER:
1990 * We use Ethernet protocol types inside libpcap;
1991 * map them to the corresponding PPP protocol types.
1993 switch (proto) {
1995 case ETHERTYPE_IP:
1996 proto = PPP_IP;
1997 break;
1999 #ifdef INET6
2000 case ETHERTYPE_IPV6:
2001 proto = PPP_IPV6;
2002 break;
2003 #endif
2005 case ETHERTYPE_DN:
2006 proto = PPP_DECNET;
2007 break;
2009 case ETHERTYPE_ATALK:
2010 proto = PPP_APPLE;
2011 break;
2013 case ETHERTYPE_NS:
2014 proto = PPP_NS;
2015 break;
2017 case LLCSAP_ISONS:
2018 proto = PPP_OSI;
2019 break;
2021 case LLCSAP_8021D:
2023 * I'm assuming the "Bridging PDU"s that go
2024 * over PPP are Spanning Tree Protocol
2025 * Bridging PDUs.
2027 proto = PPP_BRPDU;
2028 break;
2030 case LLCSAP_IPX:
2031 proto = PPP_IPX;
2032 break;
2034 break;
2036 case DLT_PPP_BSDOS:
2038 * We use Ethernet protocol types inside libpcap;
2039 * map them to the corresponding PPP protocol types.
2041 switch (proto) {
2043 case ETHERTYPE_IP:
2044 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2045 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2046 gen_or(b0, b1);
2047 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2048 gen_or(b1, b0);
2049 return b0;
2051 #ifdef INET6
2052 case ETHERTYPE_IPV6:
2053 proto = PPP_IPV6;
2054 /* more to go? */
2055 break;
2056 #endif
2058 case ETHERTYPE_DN:
2059 proto = PPP_DECNET;
2060 break;
2062 case ETHERTYPE_ATALK:
2063 proto = PPP_APPLE;
2064 break;
2066 case ETHERTYPE_NS:
2067 proto = PPP_NS;
2068 break;
2070 case LLCSAP_ISONS:
2071 proto = PPP_OSI;
2072 break;
2074 case LLCSAP_8021D:
2076 * I'm assuming the "Bridging PDU"s that go
2077 * over PPP are Spanning Tree Protocol
2078 * Bridging PDUs.
2080 proto = PPP_BRPDU;
2081 break;
2083 case LLCSAP_IPX:
2084 proto = PPP_IPX;
2085 break;
2087 break;
2089 case DLT_NULL:
2090 case DLT_LOOP:
2091 case DLT_ENC:
2093 * For DLT_NULL, the link-layer header is a 32-bit
2094 * word containing an AF_ value in *host* byte order,
2095 * and for DLT_ENC, the link-layer header begins
2096 * with a 32-bit work containing an AF_ value in
2097 * host byte order.
2099 * In addition, if we're reading a saved capture file,
2100 * the host byte order in the capture may not be the
2101 * same as the host byte order on this machine.
2103 * For DLT_LOOP, the link-layer header is a 32-bit
2104 * word containing an AF_ value in *network* byte order.
2106 * XXX - AF_ values may, unfortunately, be platform-
2107 * dependent; for example, FreeBSD's AF_INET6 is 24
2108 * whilst NetBSD's and OpenBSD's is 26.
2110 * This means that, when reading a capture file, just
2111 * checking for our AF_INET6 value won't work if the
2112 * capture file came from another OS.
2114 switch (proto) {
2116 case ETHERTYPE_IP:
2117 proto = AF_INET;
2118 break;
2120 #ifdef INET6
2121 case ETHERTYPE_IPV6:
2122 proto = AF_INET6;
2123 break;
2124 #endif
2126 default:
2128 * Not a type on which we support filtering.
2129 * XXX - support those that have AF_ values
2130 * #defined on this platform, at least?
2132 return gen_false();
2135 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2137 * The AF_ value is in host byte order, but
2138 * the BPF interpreter will convert it to
2139 * network byte order.
2141 * If this is a save file, and it's from a
2142 * machine with the opposite byte order to
2143 * ours, we byte-swap the AF_ value.
2145 * Then we run it through "htonl()", and
2146 * generate code to compare against the result.
2148 if (bpf_pcap->sf.rfile != NULL &&
2149 bpf_pcap->sf.swapped)
2150 proto = SWAPLONG(proto);
2151 proto = htonl(proto);
2153 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2155 case DLT_PFLOG:
2157 * af field is host byte order in contrast to the rest of
2158 * the packet.
2160 if (proto == ETHERTYPE_IP)
2161 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2162 BPF_B, (bpf_int32)AF_INET));
2163 #ifdef INET6
2164 else if (proto == ETHERTYPE_IPV6)
2165 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2166 BPF_B, (bpf_int32)AF_INET6));
2167 #endif /* INET6 */
2168 else
2169 return gen_false();
2170 /*NOTREACHED*/
2171 break;
2173 case DLT_ARCNET:
2174 case DLT_ARCNET_LINUX:
2176 * XXX should we check for first fragment if the protocol
2177 * uses PHDS?
2179 switch (proto) {
2181 default:
2182 return gen_false();
2184 #ifdef INET6
2185 case ETHERTYPE_IPV6:
2186 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2187 (bpf_int32)ARCTYPE_INET6));
2188 #endif /* INET6 */
2190 case ETHERTYPE_IP:
2191 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2192 (bpf_int32)ARCTYPE_IP);
2193 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2194 (bpf_int32)ARCTYPE_IP_OLD);
2195 gen_or(b0, b1);
2196 return (b1);
2198 case ETHERTYPE_ARP:
2199 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2200 (bpf_int32)ARCTYPE_ARP);
2201 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2202 (bpf_int32)ARCTYPE_ARP_OLD);
2203 gen_or(b0, b1);
2204 return (b1);
2206 case ETHERTYPE_REVARP:
2207 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2208 (bpf_int32)ARCTYPE_REVARP));
2210 case ETHERTYPE_ATALK:
2211 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2212 (bpf_int32)ARCTYPE_ATALK));
2214 /*NOTREACHED*/
2215 break;
2217 case DLT_LTALK:
2218 switch (proto) {
2219 case ETHERTYPE_ATALK:
2220 return gen_true();
2221 default:
2222 return gen_false();
2224 /*NOTREACHED*/
2225 break;
2227 case DLT_FRELAY:
2229 * XXX - assumes a 2-byte Frame Relay header with
2230 * DLCI and flags. What if the address is longer?
2232 switch (proto) {
2234 case ETHERTYPE_IP:
2236 * Check for the special NLPID for IP.
2238 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2240 #ifdef INET6
2241 case ETHERTYPE_IPV6:
2243 * Check for the special NLPID for IPv6.
2245 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2246 #endif
2248 case LLCSAP_ISONS:
2250 * Check for several OSI protocols.
2252 * Frame Relay packets typically have an OSI
2253 * NLPID at the beginning; we check for each
2254 * of them.
2256 * What we check for is the NLPID and a frame
2257 * control field of UI, i.e. 0x03 followed
2258 * by the NLPID.
2260 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2261 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2262 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2263 gen_or(b1, b2);
2264 gen_or(b0, b2);
2265 return b2;
2267 default:
2268 return gen_false();
2270 /*NOTREACHED*/
2271 break;
2273 case DLT_JUNIPER_MFR:
2274 case DLT_JUNIPER_MLFR:
2275 case DLT_JUNIPER_MLPPP:
2276 case DLT_JUNIPER_ATM1:
2277 case DLT_JUNIPER_ATM2:
2278 case DLT_JUNIPER_PPPOE:
2279 case DLT_JUNIPER_PPPOE_ATM:
2280 case DLT_JUNIPER_GGSN:
2281 case DLT_JUNIPER_ES:
2282 case DLT_JUNIPER_MONITOR:
2283 case DLT_JUNIPER_SERVICES:
2284 case DLT_JUNIPER_ETHER:
2285 case DLT_JUNIPER_PPP:
2286 case DLT_JUNIPER_FRELAY:
2287 case DLT_JUNIPER_CHDLC:
2288 /* just lets verify the magic number for now -
2289 * on ATM we may have up to 6 different encapsulations on the wire
2290 * and need a lot of heuristics to figure out that the payload
2291 * might be;
2293 * FIXME encapsulation specific BPF_ filters
2295 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2297 case DLT_LINUX_IRDA:
2298 bpf_error("IrDA link-layer type filtering not implemented");
2300 case DLT_DOCSIS:
2301 bpf_error("DOCSIS link-layer type filtering not implemented");
2303 case DLT_LINUX_LAPD:
2304 bpf_error("LAPD link-layer type filtering not implemented");
2308 * All the types that have no encapsulation should either be
2309 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2310 * all packets are IP packets, or should be handled in some
2311 * special case, if none of them are (if some are and some
2312 * aren't, the lack of encapsulation is a problem, as we'd
2313 * have to find some other way of determining the packet type).
2315 * Therefore, if "off_linktype" is -1, there's an error.
2317 if (off_linktype == (u_int)-1)
2318 abort();
2321 * Any type not handled above should always have an Ethernet
2322 * type at an offset of "off_linktype". (PPP is partially
2323 * handled above - the protocol type is mapped from the
2324 * Ethernet and LLC types we use internally to the corresponding
2325 * PPP type - but the PPP type is always specified by a value
2326 * at "off_linktype", so we don't have to do the code generation
2327 * above.)
2329 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2333 * Check for an LLC SNAP packet with a given organization code and
2334 * protocol type; we check the entire contents of the 802.2 LLC and
2335 * snap headers, checking for DSAP and SSAP of SNAP and a control
2336 * field of 0x03 in the LLC header, and for the specified organization
2337 * code and protocol type in the SNAP header.
2339 static struct block *
2340 gen_snap(orgcode, ptype, offset)
2341 bpf_u_int32 orgcode;
2342 bpf_u_int32 ptype;
2343 u_int offset;
2345 u_char snapblock[8];
2347 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2348 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2349 snapblock[2] = 0x03; /* control = UI */
2350 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2351 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2352 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2353 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2354 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2355 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2359 * Generate code to match a particular packet type, for link-layer types
2360 * using 802.2 LLC headers.
2362 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2363 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2365 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2366 * value, if <= ETHERMTU. We use that to determine whether to
2367 * match the DSAP or both DSAP and LSAP or to check the OUI and
2368 * protocol ID in a SNAP header.
2370 static struct block *
2371 gen_llc_linktype(proto)
2372 int proto;
2375 * XXX - handle token-ring variable-length header.
2377 switch (proto) {
2379 case LLCSAP_IP:
2380 case LLCSAP_ISONS:
2381 case LLCSAP_NETBEUI:
2383 * XXX - should we check both the DSAP and the
2384 * SSAP, like this, or should we check just the
2385 * DSAP, as we do for other types <= ETHERMTU
2386 * (i.e., other SAP values)?
2388 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2389 ((proto << 8) | proto));
2391 case LLCSAP_IPX:
2393 * XXX - are there ever SNAP frames for IPX on
2394 * non-Ethernet 802.x networks?
2396 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2397 (bpf_int32)LLCSAP_IPX);
2399 case ETHERTYPE_ATALK:
2401 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2402 * SNAP packets with an organization code of
2403 * 0x080007 (Apple, for Appletalk) and a protocol
2404 * type of ETHERTYPE_ATALK (Appletalk).
2406 * XXX - check for an organization code of
2407 * encapsulated Ethernet as well?
2409 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2411 default:
2413 * XXX - we don't have to check for IPX 802.3
2414 * here, but should we check for the IPX Ethertype?
2416 if (proto <= ETHERMTU) {
2418 * This is an LLC SAP value, so check
2419 * the DSAP.
2421 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2422 (bpf_int32)proto);
2423 } else {
2425 * This is an Ethernet type; we assume that it's
2426 * unlikely that it'll appear in the right place
2427 * at random, and therefore check only the
2428 * location that would hold the Ethernet type
2429 * in a SNAP frame with an organization code of
2430 * 0x000000 (encapsulated Ethernet).
2432 * XXX - if we were to check for the SNAP DSAP and
2433 * LSAP, as per XXX, and were also to check for an
2434 * organization code of 0x000000 (encapsulated
2435 * Ethernet), we'd do
2437 * return gen_snap(0x000000, proto,
2438 * off_linktype);
2440 * here; for now, we don't, as per the above.
2441 * I don't know whether it's worth the extra CPU
2442 * time to do the right check or not.
2444 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2445 (bpf_int32)proto);
2450 static struct block *
2451 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2452 bpf_u_int32 addr;
2453 bpf_u_int32 mask;
2454 int dir, proto;
2455 u_int src_off, dst_off;
2457 struct block *b0, *b1;
2458 u_int offset;
2460 switch (dir) {
2462 case Q_SRC:
2463 offset = src_off;
2464 break;
2466 case Q_DST:
2467 offset = dst_off;
2468 break;
2470 case Q_AND:
2471 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2472 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2473 gen_and(b0, b1);
2474 return b1;
2476 case Q_OR:
2477 case Q_DEFAULT:
2478 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2479 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2480 gen_or(b0, b1);
2481 return b1;
2483 default:
2484 abort();
2486 b0 = gen_linktype(proto);
2487 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2488 gen_and(b0, b1);
2489 return b1;
2492 #ifdef INET6
2493 static struct block *
2494 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2495 struct in6_addr *addr;
2496 struct in6_addr *mask;
2497 int dir, proto;
2498 u_int src_off, dst_off;
2500 struct block *b0, *b1;
2501 u_int offset;
2502 u_int32_t *a, *m;
2504 switch (dir) {
2506 case Q_SRC:
2507 offset = src_off;
2508 break;
2510 case Q_DST:
2511 offset = dst_off;
2512 break;
2514 case Q_AND:
2515 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2516 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2517 gen_and(b0, b1);
2518 return b1;
2520 case Q_OR:
2521 case Q_DEFAULT:
2522 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2523 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2524 gen_or(b0, b1);
2525 return b1;
2527 default:
2528 abort();
2530 /* this order is important */
2531 a = (u_int32_t *)addr;
2532 m = (u_int32_t *)mask;
2533 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2534 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2535 gen_and(b0, b1);
2536 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2537 gen_and(b0, b1);
2538 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2539 gen_and(b0, b1);
2540 b0 = gen_linktype(proto);
2541 gen_and(b0, b1);
2542 return b1;
2544 #endif /*INET6*/
2546 static struct block *
2547 gen_ehostop(eaddr, dir)
2548 register const u_char *eaddr;
2549 register int dir;
2551 register struct block *b0, *b1;
2553 switch (dir) {
2554 case Q_SRC:
2555 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2557 case Q_DST:
2558 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2560 case Q_AND:
2561 b0 = gen_ehostop(eaddr, Q_SRC);
2562 b1 = gen_ehostop(eaddr, Q_DST);
2563 gen_and(b0, b1);
2564 return b1;
2566 case Q_DEFAULT:
2567 case Q_OR:
2568 b0 = gen_ehostop(eaddr, Q_SRC);
2569 b1 = gen_ehostop(eaddr, Q_DST);
2570 gen_or(b0, b1);
2571 return b1;
2573 abort();
2574 /* NOTREACHED */
2578 * Like gen_ehostop, but for DLT_FDDI
2580 static struct block *
2581 gen_fhostop(eaddr, dir)
2582 register const u_char *eaddr;
2583 register int dir;
2585 struct block *b0, *b1;
2587 switch (dir) {
2588 case Q_SRC:
2589 #ifdef PCAP_FDDIPAD
2590 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2591 #else
2592 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2593 #endif
2595 case Q_DST:
2596 #ifdef PCAP_FDDIPAD
2597 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2598 #else
2599 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2600 #endif
2602 case Q_AND:
2603 b0 = gen_fhostop(eaddr, Q_SRC);
2604 b1 = gen_fhostop(eaddr, Q_DST);
2605 gen_and(b0, b1);
2606 return b1;
2608 case Q_DEFAULT:
2609 case Q_OR:
2610 b0 = gen_fhostop(eaddr, Q_SRC);
2611 b1 = gen_fhostop(eaddr, Q_DST);
2612 gen_or(b0, b1);
2613 return b1;
2615 abort();
2616 /* NOTREACHED */
2620 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2622 static struct block *
2623 gen_thostop(eaddr, dir)
2624 register const u_char *eaddr;
2625 register int dir;
2627 register struct block *b0, *b1;
2629 switch (dir) {
2630 case Q_SRC:
2631 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2633 case Q_DST:
2634 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2636 case Q_AND:
2637 b0 = gen_thostop(eaddr, Q_SRC);
2638 b1 = gen_thostop(eaddr, Q_DST);
2639 gen_and(b0, b1);
2640 return b1;
2642 case Q_DEFAULT:
2643 case Q_OR:
2644 b0 = gen_thostop(eaddr, Q_SRC);
2645 b1 = gen_thostop(eaddr, Q_DST);
2646 gen_or(b0, b1);
2647 return b1;
2649 abort();
2650 /* NOTREACHED */
2654 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2656 static struct block *
2657 gen_wlanhostop(eaddr, dir)
2658 register const u_char *eaddr;
2659 register int dir;
2661 register struct block *b0, *b1, *b2;
2662 register struct slist *s;
2664 switch (dir) {
2665 case Q_SRC:
2667 * Oh, yuk.
2669 * For control frames, there is no SA.
2671 * For management frames, SA is at an
2672 * offset of 10 from the beginning of
2673 * the packet.
2675 * For data frames, SA is at an offset
2676 * of 10 from the beginning of the packet
2677 * if From DS is clear, at an offset of
2678 * 16 from the beginning of the packet
2679 * if From DS is set and To DS is clear,
2680 * and an offset of 24 from the beginning
2681 * of the packet if From DS is set and To DS
2682 * is set.
2686 * Generate the tests to be done for data frames
2687 * with From DS set.
2689 * First, check for To DS set, i.e. check "link[1] & 0x01".
2691 s = gen_load_a(OR_LINK, 1, BPF_B);
2692 b1 = new_block(JMP(BPF_JSET));
2693 b1->s.k = 0x01; /* To DS */
2694 b1->stmts = s;
2697 * If To DS is set, the SA is at 24.
2699 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2700 gen_and(b1, b0);
2703 * Now, check for To DS not set, i.e. check
2704 * "!(link[1] & 0x01)".
2706 s = gen_load_a(OR_LINK, 1, BPF_B);
2707 b2 = new_block(JMP(BPF_JSET));
2708 b2->s.k = 0x01; /* To DS */
2709 b2->stmts = s;
2710 gen_not(b2);
2713 * If To DS is not set, the SA is at 16.
2715 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2716 gen_and(b2, b1);
2719 * Now OR together the last two checks. That gives
2720 * the complete set of checks for data frames with
2721 * From DS set.
2723 gen_or(b1, b0);
2726 * Now check for From DS being set, and AND that with
2727 * the ORed-together checks.
2729 s = gen_load_a(OR_LINK, 1, BPF_B);
2730 b1 = new_block(JMP(BPF_JSET));
2731 b1->s.k = 0x02; /* From DS */
2732 b1->stmts = s;
2733 gen_and(b1, b0);
2736 * Now check for data frames with From DS not set.
2738 s = gen_load_a(OR_LINK, 1, BPF_B);
2739 b2 = new_block(JMP(BPF_JSET));
2740 b2->s.k = 0x02; /* From DS */
2741 b2->stmts = s;
2742 gen_not(b2);
2745 * If From DS isn't set, the SA is at 10.
2747 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2748 gen_and(b2, b1);
2751 * Now OR together the checks for data frames with
2752 * From DS not set and for data frames with From DS
2753 * set; that gives the checks done for data frames.
2755 gen_or(b1, b0);
2758 * Now check for a data frame.
2759 * I.e, check "link[0] & 0x08".
2761 gen_load_a(OR_LINK, 0, BPF_B);
2762 b1 = new_block(JMP(BPF_JSET));
2763 b1->s.k = 0x08;
2764 b1->stmts = s;
2767 * AND that with the checks done for data frames.
2769 gen_and(b1, b0);
2772 * If the high-order bit of the type value is 0, this
2773 * is a management frame.
2774 * I.e, check "!(link[0] & 0x08)".
2776 s = gen_load_a(OR_LINK, 0, BPF_B);
2777 b2 = new_block(JMP(BPF_JSET));
2778 b2->s.k = 0x08;
2779 b2->stmts = s;
2780 gen_not(b2);
2783 * For management frames, the SA is at 10.
2785 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2786 gen_and(b2, b1);
2789 * OR that with the checks done for data frames.
2790 * That gives the checks done for management and
2791 * data frames.
2793 gen_or(b1, b0);
2796 * If the low-order bit of the type value is 1,
2797 * this is either a control frame or a frame
2798 * with a reserved type, and thus not a
2799 * frame with an SA.
2801 * I.e., check "!(link[0] & 0x04)".
2803 s = gen_load_a(OR_LINK, 0, BPF_B);
2804 b1 = new_block(JMP(BPF_JSET));
2805 b1->s.k = 0x04;
2806 b1->stmts = s;
2807 gen_not(b1);
2810 * AND that with the checks for data and management
2811 * frames.
2813 gen_and(b1, b0);
2814 return b0;
2816 case Q_DST:
2818 * Oh, yuk.
2820 * For control frames, there is no DA.
2822 * For management frames, DA is at an
2823 * offset of 4 from the beginning of
2824 * the packet.
2826 * For data frames, DA is at an offset
2827 * of 4 from the beginning of the packet
2828 * if To DS is clear and at an offset of
2829 * 16 from the beginning of the packet
2830 * if To DS is set.
2834 * Generate the tests to be done for data frames.
2836 * First, check for To DS set, i.e. "link[1] & 0x01".
2838 s = gen_load_a(OR_LINK, 1, BPF_B);
2839 b1 = new_block(JMP(BPF_JSET));
2840 b1->s.k = 0x01; /* To DS */
2841 b1->stmts = s;
2844 * If To DS is set, the DA is at 16.
2846 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2847 gen_and(b1, b0);
2850 * Now, check for To DS not set, i.e. check
2851 * "!(link[1] & 0x01)".
2853 s = gen_load_a(OR_LINK, 1, BPF_B);
2854 b2 = new_block(JMP(BPF_JSET));
2855 b2->s.k = 0x01; /* To DS */
2856 b2->stmts = s;
2857 gen_not(b2);
2860 * If To DS is not set, the DA is at 4.
2862 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2863 gen_and(b2, b1);
2866 * Now OR together the last two checks. That gives
2867 * the complete set of checks for data frames.
2869 gen_or(b1, b0);
2872 * Now check for a data frame.
2873 * I.e, check "link[0] & 0x08".
2875 s = gen_load_a(OR_LINK, 0, BPF_B);
2876 b1 = new_block(JMP(BPF_JSET));
2877 b1->s.k = 0x08;
2878 b1->stmts = s;
2881 * AND that with the checks done for data frames.
2883 gen_and(b1, b0);
2886 * If the high-order bit of the type value is 0, this
2887 * is a management frame.
2888 * I.e, check "!(link[0] & 0x08)".
2890 s = gen_load_a(OR_LINK, 0, BPF_B);
2891 b2 = new_block(JMP(BPF_JSET));
2892 b2->s.k = 0x08;
2893 b2->stmts = s;
2894 gen_not(b2);
2897 * For management frames, the DA is at 4.
2899 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2900 gen_and(b2, b1);
2903 * OR that with the checks done for data frames.
2904 * That gives the checks done for management and
2905 * data frames.
2907 gen_or(b1, b0);
2910 * If the low-order bit of the type value is 1,
2911 * this is either a control frame or a frame
2912 * with a reserved type, and thus not a
2913 * frame with an SA.
2915 * I.e., check "!(link[0] & 0x04)".
2917 s = gen_load_a(OR_LINK, 0, BPF_B);
2918 b1 = new_block(JMP(BPF_JSET));
2919 b1->s.k = 0x04;
2920 b1->stmts = s;
2921 gen_not(b1);
2924 * AND that with the checks for data and management
2925 * frames.
2927 gen_and(b1, b0);
2928 return b0;
2930 case Q_AND:
2931 b0 = gen_wlanhostop(eaddr, Q_SRC);
2932 b1 = gen_wlanhostop(eaddr, Q_DST);
2933 gen_and(b0, b1);
2934 return b1;
2936 case Q_DEFAULT:
2937 case Q_OR:
2938 b0 = gen_wlanhostop(eaddr, Q_SRC);
2939 b1 = gen_wlanhostop(eaddr, Q_DST);
2940 gen_or(b0, b1);
2941 return b1;
2943 abort();
2944 /* NOTREACHED */
2948 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2949 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2950 * as the RFC states.)
2952 static struct block *
2953 gen_ipfchostop(eaddr, dir)
2954 register const u_char *eaddr;
2955 register int dir;
2957 register struct block *b0, *b1;
2959 switch (dir) {
2960 case Q_SRC:
2961 return gen_bcmp(OR_LINK, 10, 6, eaddr);
2963 case Q_DST:
2964 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2966 case Q_AND:
2967 b0 = gen_ipfchostop(eaddr, Q_SRC);
2968 b1 = gen_ipfchostop(eaddr, Q_DST);
2969 gen_and(b0, b1);
2970 return b1;
2972 case Q_DEFAULT:
2973 case Q_OR:
2974 b0 = gen_ipfchostop(eaddr, Q_SRC);
2975 b1 = gen_ipfchostop(eaddr, Q_DST);
2976 gen_or(b0, b1);
2977 return b1;
2979 abort();
2980 /* NOTREACHED */
2984 * This is quite tricky because there may be pad bytes in front of the
2985 * DECNET header, and then there are two possible data packet formats that
2986 * carry both src and dst addresses, plus 5 packet types in a format that
2987 * carries only the src node, plus 2 types that use a different format and
2988 * also carry just the src node.
2990 * Yuck.
2992 * Instead of doing those all right, we just look for data packets with
2993 * 0 or 1 bytes of padding. If you want to look at other packets, that
2994 * will require a lot more hacking.
2996 * To add support for filtering on DECNET "areas" (network numbers)
2997 * one would want to add a "mask" argument to this routine. That would
2998 * make the filter even more inefficient, although one could be clever
2999 * and not generate masking instructions if the mask is 0xFFFF.
3001 static struct block *
3002 gen_dnhostop(addr, dir)
3003 bpf_u_int32 addr;
3004 int dir;
3006 struct block *b0, *b1, *b2, *tmp;
3007 u_int offset_lh; /* offset if long header is received */
3008 u_int offset_sh; /* offset if short header is received */
3010 switch (dir) {
3012 case Q_DST:
3013 offset_sh = 1; /* follows flags */
3014 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3015 break;
3017 case Q_SRC:
3018 offset_sh = 3; /* follows flags, dstnode */
3019 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3020 break;
3022 case Q_AND:
3023 /* Inefficient because we do our Calvinball dance twice */
3024 b0 = gen_dnhostop(addr, Q_SRC);
3025 b1 = gen_dnhostop(addr, Q_DST);
3026 gen_and(b0, b1);
3027 return b1;
3029 case Q_OR:
3030 case Q_DEFAULT:
3031 /* Inefficient because we do our Calvinball dance twice */
3032 b0 = gen_dnhostop(addr, Q_SRC);
3033 b1 = gen_dnhostop(addr, Q_DST);
3034 gen_or(b0, b1);
3035 return b1;
3037 case Q_ISO:
3038 bpf_error("ISO host filtering not implemented");
3040 default:
3041 abort();
3043 b0 = gen_linktype(ETHERTYPE_DN);
3044 /* Check for pad = 1, long header case */
3045 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3046 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3047 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3048 BPF_H, (bpf_int32)ntohs(addr));
3049 gen_and(tmp, b1);
3050 /* Check for pad = 0, long header case */
3051 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3052 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
3053 gen_and(tmp, b2);
3054 gen_or(b2, b1);
3055 /* Check for pad = 1, short header case */
3056 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3057 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3058 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3059 gen_and(tmp, b2);
3060 gen_or(b2, b1);
3061 /* Check for pad = 0, short header case */
3062 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3063 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3064 gen_and(tmp, b2);
3065 gen_or(b2, b1);
3067 /* Combine with test for linktype */
3068 gen_and(b0, b1);
3069 return b1;
3073 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3074 * test the bottom-of-stack bit, and then check the version number
3075 * field in the IP header.
3077 static struct block *
3078 gen_mpls_linktype(proto)
3079 int proto;
3081 struct block *b0, *b1;
3083 switch (proto) {
3085 case Q_IP:
3086 /* match the bottom-of-stack bit */
3087 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3088 /* match the IPv4 version number */
3089 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3090 gen_and(b0, b1);
3091 return b1;
3093 case Q_IPV6:
3094 /* match the bottom-of-stack bit */
3095 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3096 /* match the IPv4 version number */
3097 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3098 gen_and(b0, b1);
3099 return b1;
3101 default:
3102 abort();
3106 static struct block *
3107 gen_host(addr, mask, proto, dir)
3108 bpf_u_int32 addr;
3109 bpf_u_int32 mask;
3110 int proto;
3111 int dir;
3113 struct block *b0, *b1;
3115 switch (proto) {
3117 case Q_DEFAULT:
3118 b0 = gen_host(addr, mask, Q_IP, dir);
3120 * Only check for non-IPv4 addresses if we're not
3121 * checking MPLS-encapsulated packets.
3123 if (label_stack_depth == 0) {
3124 b1 = gen_host(addr, mask, Q_ARP, dir);
3125 gen_or(b0, b1);
3126 b0 = gen_host(addr, mask, Q_RARP, dir);
3127 gen_or(b1, b0);
3129 return b0;
3131 case Q_IP:
3132 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3134 case Q_RARP:
3135 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3137 case Q_ARP:
3138 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3140 case Q_TCP:
3141 bpf_error("'tcp' modifier applied to host");
3143 case Q_SCTP:
3144 bpf_error("'sctp' modifier applied to host");
3146 case Q_UDP:
3147 bpf_error("'udp' modifier applied to host");
3149 case Q_ICMP:
3150 bpf_error("'icmp' modifier applied to host");
3152 case Q_IGMP:
3153 bpf_error("'igmp' modifier applied to host");
3155 case Q_IGRP:
3156 bpf_error("'igrp' modifier applied to host");
3158 case Q_PIM:
3159 bpf_error("'pim' modifier applied to host");
3161 case Q_VRRP:
3162 bpf_error("'vrrp' modifier applied to host");
3164 case Q_ATALK:
3165 bpf_error("ATALK host filtering not implemented");
3167 case Q_AARP:
3168 bpf_error("AARP host filtering not implemented");
3170 case Q_DECNET:
3171 return gen_dnhostop(addr, dir);
3173 case Q_SCA:
3174 bpf_error("SCA host filtering not implemented");
3176 case Q_LAT:
3177 bpf_error("LAT host filtering not implemented");
3179 case Q_MOPDL:
3180 bpf_error("MOPDL host filtering not implemented");
3182 case Q_MOPRC:
3183 bpf_error("MOPRC host filtering not implemented");
3185 #ifdef INET6
3186 case Q_IPV6:
3187 bpf_error("'ip6' modifier applied to ip host");
3189 case Q_ICMPV6:
3190 bpf_error("'icmp6' modifier applied to host");
3191 #endif /* INET6 */
3193 case Q_AH:
3194 bpf_error("'ah' modifier applied to host");
3196 case Q_ESP:
3197 bpf_error("'esp' modifier applied to host");
3199 case Q_ISO:
3200 bpf_error("ISO host filtering not implemented");
3202 case Q_ESIS:
3203 bpf_error("'esis' modifier applied to host");
3205 case Q_ISIS:
3206 bpf_error("'isis' modifier applied to host");
3208 case Q_CLNP:
3209 bpf_error("'clnp' modifier applied to host");
3211 case Q_STP:
3212 bpf_error("'stp' modifier applied to host");
3214 case Q_IPX:
3215 bpf_error("IPX host filtering not implemented");
3217 case Q_NETBEUI:
3218 bpf_error("'netbeui' modifier applied to host");
3220 case Q_RADIO:
3221 bpf_error("'radio' modifier applied to host");
3223 default:
3224 abort();
3226 /* NOTREACHED */
3229 #ifdef INET6
3230 static struct block *
3231 gen_host6(addr, mask, proto, dir)
3232 struct in6_addr *addr;
3233 struct in6_addr *mask;
3234 int proto;
3235 int dir;
3237 switch (proto) {
3239 case Q_DEFAULT:
3240 return gen_host6(addr, mask, Q_IPV6, dir);
3242 case Q_IP:
3243 bpf_error("'ip' modifier applied to ip6 host");
3245 case Q_RARP:
3246 bpf_error("'rarp' modifier applied to ip6 host");
3248 case Q_ARP:
3249 bpf_error("'arp' modifier applied to ip6 host");
3251 case Q_SCTP:
3252 bpf_error("'sctp' modifier applied to host");
3254 case Q_TCP:
3255 bpf_error("'tcp' modifier applied to host");
3257 case Q_UDP:
3258 bpf_error("'udp' modifier applied to host");
3260 case Q_ICMP:
3261 bpf_error("'icmp' modifier applied to host");
3263 case Q_IGMP:
3264 bpf_error("'igmp' modifier applied to host");
3266 case Q_IGRP:
3267 bpf_error("'igrp' modifier applied to host");
3269 case Q_PIM:
3270 bpf_error("'pim' modifier applied to host");
3272 case Q_VRRP:
3273 bpf_error("'vrrp' modifier applied to host");
3275 case Q_ATALK:
3276 bpf_error("ATALK host filtering not implemented");
3278 case Q_AARP:
3279 bpf_error("AARP host filtering not implemented");
3281 case Q_DECNET:
3282 bpf_error("'decnet' modifier applied to ip6 host");
3284 case Q_SCA:
3285 bpf_error("SCA host filtering not implemented");
3287 case Q_LAT:
3288 bpf_error("LAT host filtering not implemented");
3290 case Q_MOPDL:
3291 bpf_error("MOPDL host filtering not implemented");
3293 case Q_MOPRC:
3294 bpf_error("MOPRC host filtering not implemented");
3296 case Q_IPV6:
3297 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3299 case Q_ICMPV6:
3300 bpf_error("'icmp6' modifier applied to host");
3302 case Q_AH:
3303 bpf_error("'ah' modifier applied to host");
3305 case Q_ESP:
3306 bpf_error("'esp' modifier applied to host");
3308 case Q_ISO:
3309 bpf_error("ISO host filtering not implemented");
3311 case Q_ESIS:
3312 bpf_error("'esis' modifier applied to host");
3314 case Q_ISIS:
3315 bpf_error("'isis' modifier applied to host");
3317 case Q_CLNP:
3318 bpf_error("'clnp' modifier applied to host");
3320 case Q_STP:
3321 bpf_error("'stp' modifier applied to host");
3323 case Q_IPX:
3324 bpf_error("IPX host filtering not implemented");
3326 case Q_NETBEUI:
3327 bpf_error("'netbeui' modifier applied to host");
3329 case Q_RADIO:
3330 bpf_error("'radio' modifier applied to host");
3332 default:
3333 abort();
3335 /* NOTREACHED */
3337 #endif /*INET6*/
3339 #ifndef INET6
3340 static struct block *
3341 gen_gateway(eaddr, alist, proto, dir)
3342 const u_char *eaddr;
3343 bpf_u_int32 **alist;
3344 int proto;
3345 int dir;
3347 struct block *b0, *b1, *tmp;
3349 if (dir != 0)
3350 bpf_error("direction applied to 'gateway'");
3352 switch (proto) {
3353 case Q_DEFAULT:
3354 case Q_IP:
3355 case Q_ARP:
3356 case Q_RARP:
3357 if (linktype == DLT_EN10MB)
3358 b0 = gen_ehostop(eaddr, Q_OR);
3359 else if (linktype == DLT_FDDI)
3360 b0 = gen_fhostop(eaddr, Q_OR);
3361 else if (linktype == DLT_IEEE802)
3362 b0 = gen_thostop(eaddr, Q_OR);
3363 else if (linktype == DLT_IEEE802_11 ||
3364 linktype == DLT_IEEE802_11_RADIO_AVS ||
3365 linktype == DLT_IEEE802_11_RADIO ||
3366 linktype == DLT_PRISM_HEADER)
3367 b0 = gen_wlanhostop(eaddr, Q_OR);
3368 else if (linktype == DLT_SUNATM && is_lane) {
3370 * Check that the packet doesn't begin with an
3371 * LE Control marker. (We've already generated
3372 * a test for LANE.)
3374 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3375 0xFF00);
3376 gen_not(b1);
3379 * Now check the MAC address.
3381 b0 = gen_ehostop(eaddr, Q_OR);
3382 gen_and(b1, b0);
3383 } else if (linktype == DLT_IP_OVER_FC)
3384 b0 = gen_ipfchostop(eaddr, Q_OR);
3385 else
3386 bpf_error(
3387 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3389 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3390 while (*alist) {
3391 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3392 gen_or(b1, tmp);
3393 b1 = tmp;
3395 gen_not(b1);
3396 gen_and(b0, b1);
3397 return b1;
3399 bpf_error("illegal modifier of 'gateway'");
3400 /* NOTREACHED */
3402 #endif
3404 struct block *
3405 gen_proto_abbrev(proto)
3406 int proto;
3408 struct block *b0;
3409 struct block *b1;
3411 switch (proto) {
3413 case Q_SCTP:
3414 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3415 #ifdef INET6
3416 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3417 gen_or(b0, b1);
3418 #endif
3419 break;
3421 case Q_TCP:
3422 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3423 #ifdef INET6
3424 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3425 gen_or(b0, b1);
3426 #endif
3427 break;
3429 case Q_UDP:
3430 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3431 #ifdef INET6
3432 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3433 gen_or(b0, b1);
3434 #endif
3435 break;
3437 case Q_ICMP:
3438 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3439 break;
3441 #ifndef IPPROTO_IGMP
3442 #define IPPROTO_IGMP 2
3443 #endif
3445 case Q_IGMP:
3446 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3447 break;
3449 #ifndef IPPROTO_IGRP
3450 #define IPPROTO_IGRP 9
3451 #endif
3452 case Q_IGRP:
3453 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3454 break;
3456 #ifndef IPPROTO_PIM
3457 #define IPPROTO_PIM 103
3458 #endif
3460 case Q_PIM:
3461 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3462 #ifdef INET6
3463 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3464 gen_or(b0, b1);
3465 #endif
3466 break;
3468 #ifndef IPPROTO_VRRP
3469 #define IPPROTO_VRRP 112
3470 #endif
3472 case Q_VRRP:
3473 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3474 break;
3476 case Q_IP:
3477 b1 = gen_linktype(ETHERTYPE_IP);
3478 break;
3480 case Q_ARP:
3481 b1 = gen_linktype(ETHERTYPE_ARP);
3482 break;
3484 case Q_RARP:
3485 b1 = gen_linktype(ETHERTYPE_REVARP);
3486 break;
3488 case Q_LINK:
3489 bpf_error("link layer applied in wrong context");
3491 case Q_ATALK:
3492 b1 = gen_linktype(ETHERTYPE_ATALK);
3493 break;
3495 case Q_AARP:
3496 b1 = gen_linktype(ETHERTYPE_AARP);
3497 break;
3499 case Q_DECNET:
3500 b1 = gen_linktype(ETHERTYPE_DN);
3501 break;
3503 case Q_SCA:
3504 b1 = gen_linktype(ETHERTYPE_SCA);
3505 break;
3507 case Q_LAT:
3508 b1 = gen_linktype(ETHERTYPE_LAT);
3509 break;
3511 case Q_MOPDL:
3512 b1 = gen_linktype(ETHERTYPE_MOPDL);
3513 break;
3515 case Q_MOPRC:
3516 b1 = gen_linktype(ETHERTYPE_MOPRC);
3517 break;
3519 #ifdef INET6
3520 case Q_IPV6:
3521 b1 = gen_linktype(ETHERTYPE_IPV6);
3522 break;
3524 #ifndef IPPROTO_ICMPV6
3525 #define IPPROTO_ICMPV6 58
3526 #endif
3527 case Q_ICMPV6:
3528 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3529 break;
3530 #endif /* INET6 */
3532 #ifndef IPPROTO_AH
3533 #define IPPROTO_AH 51
3534 #endif
3535 case Q_AH:
3536 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3537 #ifdef INET6
3538 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3539 gen_or(b0, b1);
3540 #endif
3541 break;
3543 #ifndef IPPROTO_ESP
3544 #define IPPROTO_ESP 50
3545 #endif
3546 case Q_ESP:
3547 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3548 #ifdef INET6
3549 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3550 gen_or(b0, b1);
3551 #endif
3552 break;
3554 case Q_ISO:
3555 b1 = gen_linktype(LLCSAP_ISONS);
3556 break;
3558 case Q_ESIS:
3559 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3560 break;
3562 case Q_ISIS:
3563 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3564 break;
3566 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3567 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3568 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3569 gen_or(b0, b1);
3570 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3571 gen_or(b0, b1);
3572 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3573 gen_or(b0, b1);
3574 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3575 gen_or(b0, b1);
3576 break;
3578 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3579 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3580 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3581 gen_or(b0, b1);
3582 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3583 gen_or(b0, b1);
3584 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3585 gen_or(b0, b1);
3586 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3587 gen_or(b0, b1);
3588 break;
3590 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3591 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3592 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3593 gen_or(b0, b1);
3594 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3595 gen_or(b0, b1);
3596 break;
3598 case Q_ISIS_LSP:
3599 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3600 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3601 gen_or(b0, b1);
3602 break;
3604 case Q_ISIS_SNP:
3605 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3606 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3607 gen_or(b0, b1);
3608 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3609 gen_or(b0, b1);
3610 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3611 gen_or(b0, b1);
3612 break;
3614 case Q_ISIS_CSNP:
3615 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3616 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3617 gen_or(b0, b1);
3618 break;
3620 case Q_ISIS_PSNP:
3621 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3622 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3623 gen_or(b0, b1);
3624 break;
3626 case Q_CLNP:
3627 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3628 break;
3630 case Q_STP:
3631 b1 = gen_linktype(LLCSAP_8021D);
3632 break;
3634 case Q_IPX:
3635 b1 = gen_linktype(LLCSAP_IPX);
3636 break;
3638 case Q_NETBEUI:
3639 b1 = gen_linktype(LLCSAP_NETBEUI);
3640 break;
3642 case Q_RADIO:
3643 bpf_error("'radio' is not a valid protocol type");
3645 default:
3646 abort();
3648 return b1;
3651 static struct block *
3652 gen_ipfrag()
3654 struct slist *s;
3655 struct block *b;
3657 /* not ip frag */
3658 s = gen_load_a(OR_NET, 6, BPF_H);
3659 b = new_block(JMP(BPF_JSET));
3660 b->s.k = 0x1fff;
3661 b->stmts = s;
3662 gen_not(b);
3664 return b;
3668 * Generate a comparison to a port value in the transport-layer header
3669 * at the specified offset from the beginning of that header.
3671 * XXX - this handles a variable-length prefix preceding the link-layer
3672 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3673 * variable-length link-layer headers (such as Token Ring or 802.11
3674 * headers).
3676 static struct block *
3677 gen_portatom(off, v)
3678 int off;
3679 bpf_int32 v;
3681 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3684 #ifdef INET6
3685 static struct block *
3686 gen_portatom6(off, v)
3687 int off;
3688 bpf_int32 v;
3690 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3692 #endif/*INET6*/
3694 struct block *
3695 gen_portop(port, proto, dir)
3696 int port, proto, dir;
3698 struct block *b0, *b1, *tmp;
3700 /* ip proto 'proto' */
3701 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3702 b0 = gen_ipfrag();
3703 gen_and(tmp, b0);
3705 switch (dir) {
3706 case Q_SRC:
3707 b1 = gen_portatom(0, (bpf_int32)port);
3708 break;
3710 case Q_DST:
3711 b1 = gen_portatom(2, (bpf_int32)port);
3712 break;
3714 case Q_OR:
3715 case Q_DEFAULT:
3716 tmp = gen_portatom(0, (bpf_int32)port);
3717 b1 = gen_portatom(2, (bpf_int32)port);
3718 gen_or(tmp, b1);
3719 break;
3721 case Q_AND:
3722 tmp = gen_portatom(0, (bpf_int32)port);
3723 b1 = gen_portatom(2, (bpf_int32)port);
3724 gen_and(tmp, b1);
3725 break;
3727 default:
3728 abort();
3730 gen_and(b0, b1);
3732 return b1;
3735 static struct block *
3736 gen_port(port, ip_proto, dir)
3737 int port;
3738 int ip_proto;
3739 int dir;
3741 struct block *b0, *b1, *tmp;
3744 * ether proto ip
3746 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3747 * not LLC encapsulation with LLCSAP_IP.
3749 * For IEEE 802 networks - which includes 802.5 token ring
3750 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3751 * says that SNAP encapsulation is used, not LLC encapsulation
3752 * with LLCSAP_IP.
3754 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3755 * RFC 2225 say that SNAP encapsulation is used, not LLC
3756 * encapsulation with LLCSAP_IP.
3758 * So we always check for ETHERTYPE_IP.
3760 b0 = gen_linktype(ETHERTYPE_IP);
3762 switch (ip_proto) {
3763 case IPPROTO_UDP:
3764 case IPPROTO_TCP:
3765 case IPPROTO_SCTP:
3766 b1 = gen_portop(port, ip_proto, dir);
3767 break;
3769 case PROTO_UNDEF:
3770 tmp = gen_portop(port, IPPROTO_TCP, dir);
3771 b1 = gen_portop(port, IPPROTO_UDP, dir);
3772 gen_or(tmp, b1);
3773 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3774 gen_or(tmp, b1);
3775 break;
3777 default:
3778 abort();
3780 gen_and(b0, b1);
3781 return b1;
3784 #ifdef INET6
3785 struct block *
3786 gen_portop6(port, proto, dir)
3787 int port, proto, dir;
3789 struct block *b0, *b1, *tmp;
3791 /* ip6 proto 'proto' */
3792 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3794 switch (dir) {
3795 case Q_SRC:
3796 b1 = gen_portatom6(0, (bpf_int32)port);
3797 break;
3799 case Q_DST:
3800 b1 = gen_portatom6(2, (bpf_int32)port);
3801 break;
3803 case Q_OR:
3804 case Q_DEFAULT:
3805 tmp = gen_portatom6(0, (bpf_int32)port);
3806 b1 = gen_portatom6(2, (bpf_int32)port);
3807 gen_or(tmp, b1);
3808 break;
3810 case Q_AND:
3811 tmp = gen_portatom6(0, (bpf_int32)port);
3812 b1 = gen_portatom6(2, (bpf_int32)port);
3813 gen_and(tmp, b1);
3814 break;
3816 default:
3817 abort();
3819 gen_and(b0, b1);
3821 return b1;
3824 static struct block *
3825 gen_port6(port, ip_proto, dir)
3826 int port;
3827 int ip_proto;
3828 int dir;
3830 struct block *b0, *b1, *tmp;
3832 /* link proto ip6 */
3833 b0 = gen_linktype(ETHERTYPE_IPV6);
3835 switch (ip_proto) {
3836 case IPPROTO_UDP:
3837 case IPPROTO_TCP:
3838 case IPPROTO_SCTP:
3839 b1 = gen_portop6(port, ip_proto, dir);
3840 break;
3842 case PROTO_UNDEF:
3843 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3844 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3845 gen_or(tmp, b1);
3846 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3847 gen_or(tmp, b1);
3848 break;
3850 default:
3851 abort();
3853 gen_and(b0, b1);
3854 return b1;
3856 #endif /* INET6 */
3858 /* gen_portrange code */
3859 static struct block *
3860 gen_portrangeatom(off, v1, v2)
3861 int off;
3862 bpf_int32 v1, v2;
3864 struct block *b1, *b2;
3866 if (v1 > v2) {
3868 * Reverse the order of the ports, so v1 is the lower one.
3870 bpf_int32 vtemp;
3872 vtemp = v1;
3873 v1 = v2;
3874 v2 = vtemp;
3877 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
3878 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
3880 gen_and(b1, b2);
3882 return b2;
3885 struct block *
3886 gen_portrangeop(port1, port2, proto, dir)
3887 int port1, port2;
3888 int proto;
3889 int dir;
3891 struct block *b0, *b1, *tmp;
3893 /* ip proto 'proto' */
3894 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3895 b0 = gen_ipfrag();
3896 gen_and(tmp, b0);
3898 switch (dir) {
3899 case Q_SRC:
3900 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3901 break;
3903 case Q_DST:
3904 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3905 break;
3907 case Q_OR:
3908 case Q_DEFAULT:
3909 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3910 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3911 gen_or(tmp, b1);
3912 break;
3914 case Q_AND:
3915 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3916 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3917 gen_and(tmp, b1);
3918 break;
3920 default:
3921 abort();
3923 gen_and(b0, b1);
3925 return b1;
3928 static struct block *
3929 gen_portrange(port1, port2, ip_proto, dir)
3930 int port1, port2;
3931 int ip_proto;
3932 int dir;
3934 struct block *b0, *b1, *tmp;
3936 /* link proto ip */
3937 b0 = gen_linktype(ETHERTYPE_IP);
3939 switch (ip_proto) {
3940 case IPPROTO_UDP:
3941 case IPPROTO_TCP:
3942 case IPPROTO_SCTP:
3943 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
3944 break;
3946 case PROTO_UNDEF:
3947 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
3948 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
3949 gen_or(tmp, b1);
3950 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
3951 gen_or(tmp, b1);
3952 break;
3954 default:
3955 abort();
3957 gen_and(b0, b1);
3958 return b1;
3961 #ifdef INET6
3962 static struct block *
3963 gen_portrangeatom6(off, v1, v2)
3964 int off;
3965 bpf_int32 v1, v2;
3967 struct block *b1, *b2;
3969 if (v1 > v2) {
3971 * Reverse the order of the ports, so v1 is the lower one.
3973 bpf_int32 vtemp;
3975 vtemp = v1;
3976 v1 = v2;
3977 v2 = vtemp;
3980 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
3981 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
3983 gen_and(b1, b2);
3985 return b2;
3988 struct block *
3989 gen_portrangeop6(port1, port2, proto, dir)
3990 int port1, port2;
3991 int proto;
3992 int dir;
3994 struct block *b0, *b1, *tmp;
3996 /* ip6 proto 'proto' */
3997 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3999 switch (dir) {
4000 case Q_SRC:
4001 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4002 break;
4004 case Q_DST:
4005 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4006 break;
4008 case Q_OR:
4009 case Q_DEFAULT:
4010 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4011 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4012 gen_or(tmp, b1);
4013 break;
4015 case Q_AND:
4016 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4017 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4018 gen_and(tmp, b1);
4019 break;
4021 default:
4022 abort();
4024 gen_and(b0, b1);
4026 return b1;
4029 static struct block *
4030 gen_portrange6(port1, port2, ip_proto, dir)
4031 int port1, port2;
4032 int ip_proto;
4033 int dir;
4035 struct block *b0, *b1, *tmp;
4037 /* link proto ip6 */
4038 b0 = gen_linktype(ETHERTYPE_IPV6);
4040 switch (ip_proto) {
4041 case IPPROTO_UDP:
4042 case IPPROTO_TCP:
4043 case IPPROTO_SCTP:
4044 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4045 break;
4047 case PROTO_UNDEF:
4048 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4049 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4050 gen_or(tmp, b1);
4051 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4052 gen_or(tmp, b1);
4053 break;
4055 default:
4056 abort();
4058 gen_and(b0, b1);
4059 return b1;
4061 #endif /* INET6 */
4063 static int
4064 lookup_proto(name, proto)
4065 register const char *name;
4066 register int proto;
4068 register int v;
4070 switch (proto) {
4072 case Q_DEFAULT:
4073 case Q_IP:
4074 case Q_IPV6:
4075 v = pcap_nametoproto(name);
4076 if (v == PROTO_UNDEF)
4077 bpf_error("unknown ip proto '%s'", name);
4078 break;
4080 case Q_LINK:
4081 /* XXX should look up h/w protocol type based on linktype */
4082 v = pcap_nametoeproto(name);
4083 if (v == PROTO_UNDEF) {
4084 v = pcap_nametollc(name);
4085 if (v == PROTO_UNDEF)
4086 bpf_error("unknown ether proto '%s'", name);
4088 break;
4090 case Q_ISO:
4091 if (strcmp(name, "esis") == 0)
4092 v = ISO9542_ESIS;
4093 else if (strcmp(name, "isis") == 0)
4094 v = ISO10589_ISIS;
4095 else if (strcmp(name, "clnp") == 0)
4096 v = ISO8473_CLNP;
4097 else
4098 bpf_error("unknown osi proto '%s'", name);
4099 break;
4101 default:
4102 v = PROTO_UNDEF;
4103 break;
4105 return v;
4108 #if 0
4109 struct stmt *
4110 gen_joinsp(s, n)
4111 struct stmt **s;
4112 int n;
4114 return NULL;
4116 #endif
4118 static struct block *
4119 gen_protochain(v, proto, dir)
4120 int v;
4121 int proto;
4122 int dir;
4124 #ifdef NO_PROTOCHAIN
4125 return gen_proto(v, proto, dir);
4126 #else
4127 struct block *b0, *b;
4128 struct slist *s[100];
4129 int fix2, fix3, fix4, fix5;
4130 int ahcheck, again, end;
4131 int i, max;
4132 int reg2 = alloc_reg();
4134 memset(s, 0, sizeof(s));
4135 fix2 = fix3 = fix4 = fix5 = 0;
4137 switch (proto) {
4138 case Q_IP:
4139 case Q_IPV6:
4140 break;
4141 case Q_DEFAULT:
4142 b0 = gen_protochain(v, Q_IP, dir);
4143 b = gen_protochain(v, Q_IPV6, dir);
4144 gen_or(b0, b);
4145 return b;
4146 default:
4147 bpf_error("bad protocol applied for 'protochain'");
4148 /*NOTREACHED*/
4152 * We don't handle variable-length radiotap here headers yet.
4153 * We might want to add BPF instructions to do the protochain
4154 * work, to simplify that and, on platforms that have a BPF
4155 * interpreter with the new instructions, let the filtering
4156 * be done in the kernel. (We already require a modified BPF
4157 * engine to do the protochain stuff, to support backward
4158 * branches, and backward branch support is unlikely to appear
4159 * in kernel BPF engines.)
4161 if (linktype == DLT_IEEE802_11_RADIO)
4162 bpf_error("'protochain' not supported with radiotap headers");
4164 no_optimize = 1; /*this code is not compatible with optimzer yet */
4167 * s[0] is a dummy entry to protect other BPF insn from damage
4168 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4169 * hard to find interdependency made by jump table fixup.
4171 i = 0;
4172 s[i] = new_stmt(0); /*dummy*/
4173 i++;
4175 switch (proto) {
4176 case Q_IP:
4177 b0 = gen_linktype(ETHERTYPE_IP);
4179 /* A = ip->ip_p */
4180 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4181 s[i]->s.k = off_nl + 9;
4182 i++;
4183 /* X = ip->ip_hl << 2 */
4184 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4185 s[i]->s.k = off_nl;
4186 i++;
4187 break;
4188 #ifdef INET6
4189 case Q_IPV6:
4190 b0 = gen_linktype(ETHERTYPE_IPV6);
4192 /* A = ip6->ip_nxt */
4193 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4194 s[i]->s.k = off_nl + 6;
4195 i++;
4196 /* X = sizeof(struct ip6_hdr) */
4197 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4198 s[i]->s.k = 40;
4199 i++;
4200 break;
4201 #endif
4202 default:
4203 bpf_error("unsupported proto to gen_protochain");
4204 /*NOTREACHED*/
4207 /* again: if (A == v) goto end; else fall through; */
4208 again = i;
4209 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4210 s[i]->s.k = v;
4211 s[i]->s.jt = NULL; /*later*/
4212 s[i]->s.jf = NULL; /*update in next stmt*/
4213 fix5 = i;
4214 i++;
4216 #ifndef IPPROTO_NONE
4217 #define IPPROTO_NONE 59
4218 #endif
4219 /* if (A == IPPROTO_NONE) goto end */
4220 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4221 s[i]->s.jt = NULL; /*later*/
4222 s[i]->s.jf = NULL; /*update in next stmt*/
4223 s[i]->s.k = IPPROTO_NONE;
4224 s[fix5]->s.jf = s[i];
4225 fix2 = i;
4226 i++;
4228 #ifdef INET6
4229 if (proto == Q_IPV6) {
4230 int v6start, v6end, v6advance, j;
4232 v6start = i;
4233 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4234 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4235 s[i]->s.jt = NULL; /*later*/
4236 s[i]->s.jf = NULL; /*update in next stmt*/
4237 s[i]->s.k = IPPROTO_HOPOPTS;
4238 s[fix2]->s.jf = s[i];
4239 i++;
4240 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4241 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4242 s[i]->s.jt = NULL; /*later*/
4243 s[i]->s.jf = NULL; /*update in next stmt*/
4244 s[i]->s.k = IPPROTO_DSTOPTS;
4245 i++;
4246 /* if (A == IPPROTO_ROUTING) goto v6advance */
4247 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4248 s[i]->s.jt = NULL; /*later*/
4249 s[i]->s.jf = NULL; /*update in next stmt*/
4250 s[i]->s.k = IPPROTO_ROUTING;
4251 i++;
4252 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4253 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4254 s[i]->s.jt = NULL; /*later*/
4255 s[i]->s.jf = NULL; /*later*/
4256 s[i]->s.k = IPPROTO_FRAGMENT;
4257 fix3 = i;
4258 v6end = i;
4259 i++;
4261 /* v6advance: */
4262 v6advance = i;
4265 * in short,
4266 * A = P[X];
4267 * X = X + (P[X + 1] + 1) * 8;
4269 /* A = X */
4270 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4271 i++;
4272 /* A = P[X + packet head] */
4273 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4274 s[i]->s.k = off_nl;
4275 i++;
4276 /* MEM[reg2] = A */
4277 s[i] = new_stmt(BPF_ST);
4278 s[i]->s.k = reg2;
4279 i++;
4280 /* A = X */
4281 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4282 i++;
4283 /* A += 1 */
4284 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4285 s[i]->s.k = 1;
4286 i++;
4287 /* X = A */
4288 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4289 i++;
4290 /* A = P[X + packet head]; */
4291 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4292 s[i]->s.k = off_nl;
4293 i++;
4294 /* A += 1 */
4295 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4296 s[i]->s.k = 1;
4297 i++;
4298 /* A *= 8 */
4299 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4300 s[i]->s.k = 8;
4301 i++;
4302 /* X = A; */
4303 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4304 i++;
4305 /* A = MEM[reg2] */
4306 s[i] = new_stmt(BPF_LD|BPF_MEM);
4307 s[i]->s.k = reg2;
4308 i++;
4310 /* goto again; (must use BPF_JA for backward jump) */
4311 s[i] = new_stmt(BPF_JMP|BPF_JA);
4312 s[i]->s.k = again - i - 1;
4313 s[i - 1]->s.jf = s[i];
4314 i++;
4316 /* fixup */
4317 for (j = v6start; j <= v6end; j++)
4318 s[j]->s.jt = s[v6advance];
4319 } else
4320 #endif
4322 /* nop */
4323 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4324 s[i]->s.k = 0;
4325 s[fix2]->s.jf = s[i];
4326 i++;
4329 /* ahcheck: */
4330 ahcheck = i;
4331 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4332 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4333 s[i]->s.jt = NULL; /*later*/
4334 s[i]->s.jf = NULL; /*later*/
4335 s[i]->s.k = IPPROTO_AH;
4336 if (fix3)
4337 s[fix3]->s.jf = s[ahcheck];
4338 fix4 = i;
4339 i++;
4342 * in short,
4343 * A = P[X];
4344 * X = X + (P[X + 1] + 2) * 4;
4346 /* A = X */
4347 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4348 i++;
4349 /* A = P[X + packet head]; */
4350 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4351 s[i]->s.k = off_nl;
4352 i++;
4353 /* MEM[reg2] = A */
4354 s[i] = new_stmt(BPF_ST);
4355 s[i]->s.k = reg2;
4356 i++;
4357 /* A = X */
4358 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4359 i++;
4360 /* A += 1 */
4361 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4362 s[i]->s.k = 1;
4363 i++;
4364 /* X = A */
4365 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4366 i++;
4367 /* A = P[X + packet head] */
4368 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4369 s[i]->s.k = off_nl;
4370 i++;
4371 /* A += 2 */
4372 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4373 s[i]->s.k = 2;
4374 i++;
4375 /* A *= 4 */
4376 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4377 s[i]->s.k = 4;
4378 i++;
4379 /* X = A; */
4380 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4381 i++;
4382 /* A = MEM[reg2] */
4383 s[i] = new_stmt(BPF_LD|BPF_MEM);
4384 s[i]->s.k = reg2;
4385 i++;
4387 /* goto again; (must use BPF_JA for backward jump) */
4388 s[i] = new_stmt(BPF_JMP|BPF_JA);
4389 s[i]->s.k = again - i - 1;
4390 i++;
4392 /* end: nop */
4393 end = i;
4394 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4395 s[i]->s.k = 0;
4396 s[fix2]->s.jt = s[end];
4397 s[fix4]->s.jf = s[end];
4398 s[fix5]->s.jt = s[end];
4399 i++;
4402 * make slist chain
4404 max = i;
4405 for (i = 0; i < max - 1; i++)
4406 s[i]->next = s[i + 1];
4407 s[max - 1]->next = NULL;
4410 * emit final check
4412 b = new_block(JMP(BPF_JEQ));
4413 b->stmts = s[1]; /*remember, s[0] is dummy*/
4414 b->s.k = v;
4416 free_reg(reg2);
4418 gen_and(b0, b);
4419 return b;
4420 #endif
4424 * Generate code that checks whether the packet is a packet for protocol
4425 * <proto> and whether the type field in that protocol's header has
4426 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4427 * IP packet and checks the protocol number in the IP header against <v>.
4429 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4430 * against Q_IP and Q_IPV6.
4432 static struct block *
4433 gen_proto(v, proto, dir)
4434 int v;
4435 int proto;
4436 int dir;
4438 struct block *b0, *b1;
4440 if (dir != Q_DEFAULT)
4441 bpf_error("direction applied to 'proto'");
4443 switch (proto) {
4444 case Q_DEFAULT:
4445 #ifdef INET6
4446 b0 = gen_proto(v, Q_IP, dir);
4447 b1 = gen_proto(v, Q_IPV6, dir);
4448 gen_or(b0, b1);
4449 return b1;
4450 #else
4451 /*FALLTHROUGH*/
4452 #endif
4453 case Q_IP:
4455 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4456 * not LLC encapsulation with LLCSAP_IP.
4458 * For IEEE 802 networks - which includes 802.5 token ring
4459 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4460 * says that SNAP encapsulation is used, not LLC encapsulation
4461 * with LLCSAP_IP.
4463 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4464 * RFC 2225 say that SNAP encapsulation is used, not LLC
4465 * encapsulation with LLCSAP_IP.
4467 * So we always check for ETHERTYPE_IP.
4470 b0 = gen_linktype(ETHERTYPE_IP);
4471 #ifndef CHASE_CHAIN
4472 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4473 #else
4474 b1 = gen_protochain(v, Q_IP);
4475 #endif
4476 gen_and(b0, b1);
4477 return b1;
4479 case Q_ISO:
4480 switch (linktype) {
4482 case DLT_FRELAY:
4484 * Frame Relay packets typically have an OSI
4485 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4486 * generates code to check for all the OSI
4487 * NLPIDs, so calling it and then adding a check
4488 * for the particular NLPID for which we're
4489 * looking is bogus, as we can just check for
4490 * the NLPID.
4492 * What we check for is the NLPID and a frame
4493 * control field value of UI, i.e. 0x03 followed
4494 * by the NLPID.
4496 * XXX - assumes a 2-byte Frame Relay header with
4497 * DLCI and flags. What if the address is longer?
4499 * XXX - what about SNAP-encapsulated frames?
4501 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4502 /*NOTREACHED*/
4503 break;
4505 case DLT_C_HDLC:
4507 * Cisco uses an Ethertype lookalike - for OSI,
4508 * it's 0xfefe.
4510 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4511 /* OSI in C-HDLC is stuffed with a fudge byte */
4512 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4513 gen_and(b0, b1);
4514 return b1;
4516 default:
4517 b0 = gen_linktype(LLCSAP_ISONS);
4518 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4519 gen_and(b0, b1);
4520 return b1;
4523 case Q_ISIS:
4524 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4526 * 4 is the offset of the PDU type relative to the IS-IS
4527 * header.
4529 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4530 gen_and(b0, b1);
4531 return b1;
4533 case Q_ARP:
4534 bpf_error("arp does not encapsulate another protocol");
4535 /* NOTREACHED */
4537 case Q_RARP:
4538 bpf_error("rarp does not encapsulate another protocol");
4539 /* NOTREACHED */
4541 case Q_ATALK:
4542 bpf_error("atalk encapsulation is not specifiable");
4543 /* NOTREACHED */
4545 case Q_DECNET:
4546 bpf_error("decnet encapsulation is not specifiable");
4547 /* NOTREACHED */
4549 case Q_SCA:
4550 bpf_error("sca does not encapsulate another protocol");
4551 /* NOTREACHED */
4553 case Q_LAT:
4554 bpf_error("lat does not encapsulate another protocol");
4555 /* NOTREACHED */
4557 case Q_MOPRC:
4558 bpf_error("moprc does not encapsulate another protocol");
4559 /* NOTREACHED */
4561 case Q_MOPDL:
4562 bpf_error("mopdl does not encapsulate another protocol");
4563 /* NOTREACHED */
4565 case Q_LINK:
4566 return gen_linktype(v);
4568 case Q_UDP:
4569 bpf_error("'udp proto' is bogus");
4570 /* NOTREACHED */
4572 case Q_TCP:
4573 bpf_error("'tcp proto' is bogus");
4574 /* NOTREACHED */
4576 case Q_SCTP:
4577 bpf_error("'sctp proto' is bogus");
4578 /* NOTREACHED */
4580 case Q_ICMP:
4581 bpf_error("'icmp proto' is bogus");
4582 /* NOTREACHED */
4584 case Q_IGMP:
4585 bpf_error("'igmp proto' is bogus");
4586 /* NOTREACHED */
4588 case Q_IGRP:
4589 bpf_error("'igrp proto' is bogus");
4590 /* NOTREACHED */
4592 case Q_PIM:
4593 bpf_error("'pim proto' is bogus");
4594 /* NOTREACHED */
4596 case Q_VRRP:
4597 bpf_error("'vrrp proto' is bogus");
4598 /* NOTREACHED */
4600 #ifdef INET6
4601 case Q_IPV6:
4602 b0 = gen_linktype(ETHERTYPE_IPV6);
4603 #ifndef CHASE_CHAIN
4604 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4605 #else
4606 b1 = gen_protochain(v, Q_IPV6);
4607 #endif
4608 gen_and(b0, b1);
4609 return b1;
4611 case Q_ICMPV6:
4612 bpf_error("'icmp6 proto' is bogus");
4613 #endif /* INET6 */
4615 case Q_AH:
4616 bpf_error("'ah proto' is bogus");
4618 case Q_ESP:
4619 bpf_error("'ah proto' is bogus");
4621 case Q_STP:
4622 bpf_error("'stp proto' is bogus");
4624 case Q_IPX:
4625 bpf_error("'ipx proto' is bogus");
4627 case Q_NETBEUI:
4628 bpf_error("'netbeui proto' is bogus");
4630 case Q_RADIO:
4631 bpf_error("'radio proto' is bogus");
4633 default:
4634 abort();
4635 /* NOTREACHED */
4637 /* NOTREACHED */
4640 struct block *
4641 gen_scode(name, q)
4642 register const char *name;
4643 struct qual q;
4645 int proto = q.proto;
4646 int dir = q.dir;
4647 int tproto;
4648 u_char *eaddr;
4649 bpf_u_int32 mask, addr;
4650 #ifndef INET6
4651 bpf_u_int32 **alist;
4652 #else
4653 int tproto6;
4654 struct sockaddr_in *sin4;
4655 struct sockaddr_in6 *sin6;
4656 struct addrinfo *res, *res0;
4657 struct in6_addr mask128;
4658 #endif /*INET6*/
4659 struct block *b, *tmp;
4660 int port, real_proto;
4661 int port1, port2;
4663 switch (q.addr) {
4665 case Q_NET:
4666 addr = pcap_nametonetaddr(name);
4667 if (addr == 0)
4668 bpf_error("unknown network '%s'", name);
4669 /* Left justify network addr and calculate its network mask */
4670 mask = 0xffffffff;
4671 while (addr && (addr & 0xff000000) == 0) {
4672 addr <<= 8;
4673 mask <<= 8;
4675 return gen_host(addr, mask, proto, dir);
4677 case Q_DEFAULT:
4678 case Q_HOST:
4679 if (proto == Q_LINK) {
4680 switch (linktype) {
4682 case DLT_EN10MB:
4683 eaddr = pcap_ether_hostton(name);
4684 if (eaddr == NULL)
4685 bpf_error(
4686 "unknown ether host '%s'", name);
4687 b = gen_ehostop(eaddr, dir);
4688 free(eaddr);
4689 return b;
4691 case DLT_FDDI:
4692 eaddr = pcap_ether_hostton(name);
4693 if (eaddr == NULL)
4694 bpf_error(
4695 "unknown FDDI host '%s'", name);
4696 b = gen_fhostop(eaddr, dir);
4697 free(eaddr);
4698 return b;
4700 case DLT_IEEE802:
4701 eaddr = pcap_ether_hostton(name);
4702 if (eaddr == NULL)
4703 bpf_error(
4704 "unknown token ring host '%s'", name);
4705 b = gen_thostop(eaddr, dir);
4706 free(eaddr);
4707 return b;
4709 case DLT_IEEE802_11:
4710 case DLT_IEEE802_11_RADIO_AVS:
4711 case DLT_IEEE802_11_RADIO:
4712 case DLT_PRISM_HEADER:
4713 eaddr = pcap_ether_hostton(name);
4714 if (eaddr == NULL)
4715 bpf_error(
4716 "unknown 802.11 host '%s'", name);
4717 b = gen_wlanhostop(eaddr, dir);
4718 free(eaddr);
4719 return b;
4721 case DLT_IP_OVER_FC:
4722 eaddr = pcap_ether_hostton(name);
4723 if (eaddr == NULL)
4724 bpf_error(
4725 "unknown Fibre Channel host '%s'", name);
4726 b = gen_ipfchostop(eaddr, dir);
4727 free(eaddr);
4728 return b;
4730 case DLT_SUNATM:
4731 if (!is_lane)
4732 break;
4735 * Check that the packet doesn't begin
4736 * with an LE Control marker. (We've
4737 * already generated a test for LANE.)
4739 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4740 BPF_H, 0xFF00);
4741 gen_not(tmp);
4743 eaddr = pcap_ether_hostton(name);
4744 if (eaddr == NULL)
4745 bpf_error(
4746 "unknown ether host '%s'", name);
4747 b = gen_ehostop(eaddr, dir);
4748 gen_and(tmp, b);
4749 free(eaddr);
4750 return b;
4753 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4754 } else if (proto == Q_DECNET) {
4755 unsigned short dn_addr = __pcap_nametodnaddr(name);
4757 * I don't think DECNET hosts can be multihomed, so
4758 * there is no need to build up a list of addresses
4760 return (gen_host(dn_addr, 0, proto, dir));
4761 } else {
4762 #ifndef INET6
4763 alist = pcap_nametoaddr(name);
4764 if (alist == NULL || *alist == NULL)
4765 bpf_error("unknown host '%s'", name);
4766 tproto = proto;
4767 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4768 tproto = Q_IP;
4769 b = gen_host(**alist++, 0xffffffff, tproto, dir);
4770 while (*alist) {
4771 tmp = gen_host(**alist++, 0xffffffff,
4772 tproto, dir);
4773 gen_or(b, tmp);
4774 b = tmp;
4776 return b;
4777 #else
4778 memset(&mask128, 0xff, sizeof(mask128));
4779 res0 = res = pcap_nametoaddrinfo(name);
4780 if (res == NULL)
4781 bpf_error("unknown host '%s'", name);
4782 b = tmp = NULL;
4783 tproto = tproto6 = proto;
4784 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4785 tproto = Q_IP;
4786 tproto6 = Q_IPV6;
4788 for (res = res0; res; res = res->ai_next) {
4789 switch (res->ai_family) {
4790 case AF_INET:
4791 if (tproto == Q_IPV6)
4792 continue;
4794 sin4 = (struct sockaddr_in *)
4795 res->ai_addr;
4796 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
4797 0xffffffff, tproto, dir);
4798 break;
4799 case AF_INET6:
4800 if (tproto6 == Q_IP)
4801 continue;
4803 sin6 = (struct sockaddr_in6 *)
4804 res->ai_addr;
4805 tmp = gen_host6(&sin6->sin6_addr,
4806 &mask128, tproto6, dir);
4807 break;
4808 default:
4809 continue;
4811 if (b)
4812 gen_or(b, tmp);
4813 b = tmp;
4815 freeaddrinfo(res0);
4816 if (b == NULL) {
4817 bpf_error("unknown host '%s'%s", name,
4818 (proto == Q_DEFAULT)
4819 ? ""
4820 : " for specified address family");
4822 return b;
4823 #endif /*INET6*/
4826 case Q_PORT:
4827 if (proto != Q_DEFAULT &&
4828 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4829 bpf_error("illegal qualifier of 'port'");
4830 if (pcap_nametoport(name, &port, &real_proto) == 0)
4831 bpf_error("unknown port '%s'", name);
4832 if (proto == Q_UDP) {
4833 if (real_proto == IPPROTO_TCP)
4834 bpf_error("port '%s' is tcp", name);
4835 else if (real_proto == IPPROTO_SCTP)
4836 bpf_error("port '%s' is sctp", name);
4837 else
4838 /* override PROTO_UNDEF */
4839 real_proto = IPPROTO_UDP;
4841 if (proto == Q_TCP) {
4842 if (real_proto == IPPROTO_UDP)
4843 bpf_error("port '%s' is udp", name);
4845 else if (real_proto == IPPROTO_SCTP)
4846 bpf_error("port '%s' is sctp", name);
4847 else
4848 /* override PROTO_UNDEF */
4849 real_proto = IPPROTO_TCP;
4851 if (proto == Q_SCTP) {
4852 if (real_proto == IPPROTO_UDP)
4853 bpf_error("port '%s' is udp", name);
4855 else if (real_proto == IPPROTO_TCP)
4856 bpf_error("port '%s' is tcp", name);
4857 else
4858 /* override PROTO_UNDEF */
4859 real_proto = IPPROTO_SCTP;
4861 #ifndef INET6
4862 return gen_port(port, real_proto, dir);
4863 #else
4865 b = gen_port(port, real_proto, dir);
4866 gen_or(gen_port6(port, real_proto, dir), b);
4867 return b;
4869 #endif /* INET6 */
4871 case Q_PORTRANGE:
4872 if (proto != Q_DEFAULT &&
4873 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4874 bpf_error("illegal qualifier of 'portrange'");
4875 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
4876 bpf_error("unknown port in range '%s'", name);
4877 if (proto == Q_UDP) {
4878 if (real_proto == IPPROTO_TCP)
4879 bpf_error("port in range '%s' is tcp", name);
4880 else if (real_proto == IPPROTO_SCTP)
4881 bpf_error("port in range '%s' is sctp", name);
4882 else
4883 /* override PROTO_UNDEF */
4884 real_proto = IPPROTO_UDP;
4886 if (proto == Q_TCP) {
4887 if (real_proto == IPPROTO_UDP)
4888 bpf_error("port in range '%s' is udp", name);
4889 else if (real_proto == IPPROTO_SCTP)
4890 bpf_error("port in range '%s' is sctp", name);
4891 else
4892 /* override PROTO_UNDEF */
4893 real_proto = IPPROTO_TCP;
4895 if (proto == Q_SCTP) {
4896 if (real_proto == IPPROTO_UDP)
4897 bpf_error("port in range '%s' is udp", name);
4898 else if (real_proto == IPPROTO_TCP)
4899 bpf_error("port in range '%s' is tcp", name);
4900 else
4901 /* override PROTO_UNDEF */
4902 real_proto = IPPROTO_SCTP;
4904 #ifndef INET6
4905 return gen_portrange(port1, port2, real_proto, dir);
4906 #else
4908 b = gen_portrange(port1, port2, real_proto, dir);
4909 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
4910 return b;
4912 #endif /* INET6 */
4914 case Q_GATEWAY:
4915 #ifndef INET6
4916 eaddr = pcap_ether_hostton(name);
4917 if (eaddr == NULL)
4918 bpf_error("unknown ether host: %s", name);
4920 alist = pcap_nametoaddr(name);
4921 if (alist == NULL || *alist == NULL)
4922 bpf_error("unknown host '%s'", name);
4923 b = gen_gateway(eaddr, alist, proto, dir);
4924 free(eaddr);
4925 return b;
4926 #else
4927 bpf_error("'gateway' not supported in this configuration");
4928 #endif /*INET6*/
4930 case Q_PROTO:
4931 real_proto = lookup_proto(name, proto);
4932 if (real_proto >= 0)
4933 return gen_proto(real_proto, proto, dir);
4934 else
4935 bpf_error("unknown protocol: %s", name);
4937 case Q_PROTOCHAIN:
4938 real_proto = lookup_proto(name, proto);
4939 if (real_proto >= 0)
4940 return gen_protochain(real_proto, proto, dir);
4941 else
4942 bpf_error("unknown protocol: %s", name);
4945 case Q_UNDEF:
4946 syntax();
4947 /* NOTREACHED */
4949 abort();
4950 /* NOTREACHED */
4953 struct block *
4954 gen_mcode(s1, s2, masklen, q)
4955 register const char *s1, *s2;
4956 register int masklen;
4957 struct qual q;
4959 register int nlen, mlen;
4960 bpf_u_int32 n, m;
4962 nlen = __pcap_atoin(s1, &n);
4963 /* Promote short ipaddr */
4964 n <<= 32 - nlen;
4966 if (s2 != NULL) {
4967 mlen = __pcap_atoin(s2, &m);
4968 /* Promote short ipaddr */
4969 m <<= 32 - mlen;
4970 if ((n & ~m) != 0)
4971 bpf_error("non-network bits set in \"%s mask %s\"",
4972 s1, s2);
4973 } else {
4974 /* Convert mask len to mask */
4975 if (masklen > 32)
4976 bpf_error("mask length must be <= 32");
4977 m = 0xffffffff << (32 - masklen);
4978 if ((n & ~m) != 0)
4979 bpf_error("non-network bits set in \"%s/%d\"",
4980 s1, masklen);
4983 switch (q.addr) {
4985 case Q_NET:
4986 return gen_host(n, m, q.proto, q.dir);
4988 default:
4989 bpf_error("Mask syntax for networks only");
4990 /* NOTREACHED */
4992 /* NOTREACHED */
4995 struct block *
4996 gen_ncode(s, v, q)
4997 register const char *s;
4998 bpf_u_int32 v;
4999 struct qual q;
5001 bpf_u_int32 mask;
5002 int proto = q.proto;
5003 int dir = q.dir;
5004 register int vlen;
5006 if (s == NULL)
5007 vlen = 32;
5008 else if (q.proto == Q_DECNET)
5009 vlen = __pcap_atodn(s, &v);
5010 else
5011 vlen = __pcap_atoin(s, &v);
5013 switch (q.addr) {
5015 case Q_DEFAULT:
5016 case Q_HOST:
5017 case Q_NET:
5018 if (proto == Q_DECNET)
5019 return gen_host(v, 0, proto, dir);
5020 else if (proto == Q_LINK) {
5021 bpf_error("illegal link layer address");
5022 } else {
5023 mask = 0xffffffff;
5024 if (s == NULL && q.addr == Q_NET) {
5025 /* Promote short net number */
5026 while (v && (v & 0xff000000) == 0) {
5027 v <<= 8;
5028 mask <<= 8;
5030 } else {
5031 /* Promote short ipaddr */
5032 v <<= 32 - vlen;
5033 mask <<= 32 - vlen;
5035 return gen_host(v, mask, proto, dir);
5038 case Q_PORT:
5039 if (proto == Q_UDP)
5040 proto = IPPROTO_UDP;
5041 else if (proto == Q_TCP)
5042 proto = IPPROTO_TCP;
5043 else if (proto == Q_SCTP)
5044 proto = IPPROTO_SCTP;
5045 else if (proto == Q_DEFAULT)
5046 proto = PROTO_UNDEF;
5047 else
5048 bpf_error("illegal qualifier of 'port'");
5050 #ifndef INET6
5051 return gen_port((int)v, proto, dir);
5052 #else
5054 struct block *b;
5055 b = gen_port((int)v, proto, dir);
5056 gen_or(gen_port6((int)v, proto, dir), b);
5057 return b;
5059 #endif /* INET6 */
5061 case Q_PORTRANGE:
5062 if (proto == Q_UDP)
5063 proto = IPPROTO_UDP;
5064 else if (proto == Q_TCP)
5065 proto = IPPROTO_TCP;
5066 else if (proto == Q_SCTP)
5067 proto = IPPROTO_SCTP;
5068 else if (proto == Q_DEFAULT)
5069 proto = PROTO_UNDEF;
5070 else
5071 bpf_error("illegal qualifier of 'portrange'");
5073 #ifndef INET6
5074 return gen_portrange((int)v, (int)v, proto, dir);
5075 #else
5077 struct block *b;
5078 b = gen_portrange((int)v, (int)v, proto, dir);
5079 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5080 return b;
5082 #endif /* INET6 */
5084 case Q_GATEWAY:
5085 bpf_error("'gateway' requires a name");
5086 /* NOTREACHED */
5088 case Q_PROTO:
5089 return gen_proto((int)v, proto, dir);
5091 case Q_PROTOCHAIN:
5092 return gen_protochain((int)v, proto, dir);
5094 case Q_UNDEF:
5095 syntax();
5096 /* NOTREACHED */
5098 default:
5099 abort();
5100 /* NOTREACHED */
5102 /* NOTREACHED */
5105 #ifdef INET6
5106 struct block *
5107 gen_mcode6(s1, s2, masklen, q)
5108 register const char *s1, *s2;
5109 register int masklen;
5110 struct qual q;
5112 struct addrinfo *res;
5113 struct in6_addr *addr;
5114 struct in6_addr mask;
5115 struct block *b;
5116 u_int32_t *a, *m;
5118 if (s2)
5119 bpf_error("no mask %s supported", s2);
5121 res = pcap_nametoaddrinfo(s1);
5122 if (!res)
5123 bpf_error("invalid ip6 address %s", s1);
5124 if (res->ai_next)
5125 bpf_error("%s resolved to multiple address", s1);
5126 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5128 if (sizeof(mask) * 8 < masklen)
5129 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5130 memset(&mask, 0, sizeof(mask));
5131 memset(&mask, 0xff, masklen / 8);
5132 if (masklen % 8) {
5133 mask.s6_addr[masklen / 8] =
5134 (0xff << (8 - masklen % 8)) & 0xff;
5137 a = (u_int32_t *)addr;
5138 m = (u_int32_t *)&mask;
5139 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5140 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5141 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5144 switch (q.addr) {
5146 case Q_DEFAULT:
5147 case Q_HOST:
5148 if (masklen != 128)
5149 bpf_error("Mask syntax for networks only");
5150 /* FALLTHROUGH */
5152 case Q_NET:
5153 b = gen_host6(addr, &mask, q.proto, q.dir);
5154 freeaddrinfo(res);
5155 return b;
5157 default:
5158 bpf_error("invalid qualifier against IPv6 address");
5159 /* NOTREACHED */
5162 #endif /*INET6*/
5164 struct block *
5165 gen_ecode(eaddr, q)
5166 register const u_char *eaddr;
5167 struct qual q;
5169 struct block *b, *tmp;
5171 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5172 if (linktype == DLT_EN10MB)
5173 return gen_ehostop(eaddr, (int)q.dir);
5174 if (linktype == DLT_FDDI)
5175 return gen_fhostop(eaddr, (int)q.dir);
5176 if (linktype == DLT_IEEE802)
5177 return gen_thostop(eaddr, (int)q.dir);
5178 if (linktype == DLT_IEEE802_11 ||
5179 linktype == DLT_IEEE802_11_RADIO_AVS ||
5180 linktype == DLT_IEEE802_11_RADIO ||
5181 linktype == DLT_PRISM_HEADER)
5182 return gen_wlanhostop(eaddr, (int)q.dir);
5183 if (linktype == DLT_SUNATM && is_lane) {
5185 * Check that the packet doesn't begin with an
5186 * LE Control marker. (We've already generated
5187 * a test for LANE.)
5189 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5190 0xFF00);
5191 gen_not(tmp);
5194 * Now check the MAC address.
5196 b = gen_ehostop(eaddr, (int)q.dir);
5197 gen_and(tmp, b);
5198 return b;
5200 if (linktype == DLT_IP_OVER_FC)
5201 return gen_ipfchostop(eaddr, (int)q.dir);
5202 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5204 bpf_error("ethernet address used in non-ether expression");
5205 /* NOTREACHED */
5208 void
5209 sappend(s0, s1)
5210 struct slist *s0, *s1;
5213 * This is definitely not the best way to do this, but the
5214 * lists will rarely get long.
5216 while (s0->next)
5217 s0 = s0->next;
5218 s0->next = s1;
5221 static struct slist *
5222 xfer_to_x(a)
5223 struct arth *a;
5225 struct slist *s;
5227 s = new_stmt(BPF_LDX|BPF_MEM);
5228 s->s.k = a->regno;
5229 return s;
5232 static struct slist *
5233 xfer_to_a(a)
5234 struct arth *a;
5236 struct slist *s;
5238 s = new_stmt(BPF_LD|BPF_MEM);
5239 s->s.k = a->regno;
5240 return s;
5244 * Modify "index" to use the value stored into its register as an
5245 * offset relative to the beginning of the header for the protocol
5246 * "proto", and allocate a register and put an item "size" bytes long
5247 * (1, 2, or 4) at that offset into that register, making it the register
5248 * for "index".
5250 struct arth *
5251 gen_load(proto, inst, size)
5252 int proto;
5253 struct arth *inst;
5254 int size;
5256 struct slist *s, *tmp;
5257 struct block *b;
5258 int regno = alloc_reg();
5260 free_reg(inst->regno);
5261 switch (size) {
5263 default:
5264 bpf_error("data size must be 1, 2, or 4");
5266 case 1:
5267 size = BPF_B;
5268 break;
5270 case 2:
5271 size = BPF_H;
5272 break;
5274 case 4:
5275 size = BPF_W;
5276 break;
5278 switch (proto) {
5279 default:
5280 bpf_error("unsupported index operation");
5282 case Q_RADIO:
5284 * The offset is relative to the beginning of the packet
5285 * data, if we have a radio header. (If we don't, this
5286 * is an error.)
5288 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5289 linktype != DLT_IEEE802_11_RADIO &&
5290 linktype != DLT_PRISM_HEADER)
5291 bpf_error("radio information not present in capture");
5294 * Load into the X register the offset computed into the
5295 * register specifed by "index".
5297 s = xfer_to_x(inst);
5300 * Load the item at that offset.
5302 tmp = new_stmt(BPF_LD|BPF_IND|size);
5303 sappend(s, tmp);
5304 sappend(inst->s, s);
5305 break;
5307 case Q_LINK:
5309 * The offset is relative to the beginning of
5310 * the link-layer header.
5312 * XXX - what about ATM LANE? Should the index be
5313 * relative to the beginning of the AAL5 frame, so
5314 * that 0 refers to the beginning of the LE Control
5315 * field, or relative to the beginning of the LAN
5316 * frame, so that 0 refers, for Ethernet LANE, to
5317 * the beginning of the destination address?
5319 s = gen_llprefixlen();
5322 * If "s" is non-null, it has code to arrange that the
5323 * X register contains the length of the prefix preceding
5324 * the link-layer header. Add to it the offset computed
5325 * into the register specified by "index", and move that
5326 * into the X register. Otherwise, just load into the X
5327 * register the offset computed into the register specifed
5328 * by "index".
5330 if (s != NULL) {
5331 sappend(s, xfer_to_a(inst));
5332 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5333 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5334 } else
5335 s = xfer_to_x(inst);
5338 * Load the item at the sum of the offset we've put in the
5339 * X register and the offset of the start of the link
5340 * layer header (which is 0 if the radio header is
5341 * variable-length; that header length is what we put
5342 * into the X register and then added to the index).
5344 tmp = new_stmt(BPF_LD|BPF_IND|size);
5345 tmp->s.k = off_ll;
5346 sappend(s, tmp);
5347 sappend(inst->s, s);
5348 break;
5350 case Q_IP:
5351 case Q_ARP:
5352 case Q_RARP:
5353 case Q_ATALK:
5354 case Q_DECNET:
5355 case Q_SCA:
5356 case Q_LAT:
5357 case Q_MOPRC:
5358 case Q_MOPDL:
5359 #ifdef INET6
5360 case Q_IPV6:
5361 #endif
5363 * The offset is relative to the beginning of
5364 * the network-layer header.
5365 * XXX - are there any cases where we want
5366 * off_nl_nosnap?
5368 s = gen_llprefixlen();
5371 * If "s" is non-null, it has code to arrange that the
5372 * X register contains the length of the prefix preceding
5373 * the link-layer header. Add to it the offset computed
5374 * into the register specified by "index", and move that
5375 * into the X register. Otherwise, just load into the X
5376 * register the offset computed into the register specifed
5377 * by "index".
5379 if (s != NULL) {
5380 sappend(s, xfer_to_a(inst));
5381 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5382 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5383 } else
5384 s = xfer_to_x(inst);
5387 * Load the item at the sum of the offset we've put in the
5388 * X register and the offset of the start of the network
5389 * layer header.
5391 tmp = new_stmt(BPF_LD|BPF_IND|size);
5392 tmp->s.k = off_nl;
5393 sappend(s, tmp);
5394 sappend(inst->s, s);
5397 * Do the computation only if the packet contains
5398 * the protocol in question.
5400 b = gen_proto_abbrev(proto);
5401 if (inst->b)
5402 gen_and(inst->b, b);
5403 inst->b = b;
5404 break;
5406 case Q_SCTP:
5407 case Q_TCP:
5408 case Q_UDP:
5409 case Q_ICMP:
5410 case Q_IGMP:
5411 case Q_IGRP:
5412 case Q_PIM:
5413 case Q_VRRP:
5415 * The offset is relative to the beginning of
5416 * the transport-layer header.
5417 * XXX - are there any cases where we want
5418 * off_nl_nosnap?
5419 * XXX - we should, if we're built with
5420 * IPv6 support, generate code to load either
5421 * IPv4, IPv6, or both, as appropriate.
5423 s = gen_loadx_iphdrlen();
5426 * The X register now contains the sum of the offset
5427 * of the beginning of the link-layer header and
5428 * the length of the network-layer header. Load
5429 * into the A register the offset relative to
5430 * the beginning of the transport layer header,
5431 * add the X register to that, move that to the
5432 * X register, and load with an offset from the
5433 * X register equal to the offset of the network
5434 * layer header relative to the beginning of
5435 * the link-layer header.
5437 sappend(s, xfer_to_a(inst));
5438 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5439 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5440 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5441 tmp->s.k = off_nl;
5442 sappend(inst->s, s);
5445 * Do the computation only if the packet contains
5446 * the protocol in question - which is true only
5447 * if this is an IP datagram and is the first or
5448 * only fragment of that datagram.
5450 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5451 if (inst->b)
5452 gen_and(inst->b, b);
5453 #ifdef INET6
5454 gen_and(gen_proto_abbrev(Q_IP), b);
5455 #endif
5456 inst->b = b;
5457 break;
5458 #ifdef INET6
5459 case Q_ICMPV6:
5460 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5461 /*NOTREACHED*/
5462 #endif
5464 inst->regno = regno;
5465 s = new_stmt(BPF_ST);
5466 s->s.k = regno;
5467 sappend(inst->s, s);
5469 return inst;
5472 struct block *
5473 gen_relation(code, a0, a1, reversed)
5474 int code;
5475 struct arth *a0, *a1;
5476 int reversed;
5478 struct slist *s0, *s1, *s2;
5479 struct block *b, *tmp;
5481 s0 = xfer_to_x(a1);
5482 s1 = xfer_to_a(a0);
5483 if (code == BPF_JEQ) {
5484 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5485 b = new_block(JMP(code));
5486 sappend(s1, s2);
5488 else
5489 b = new_block(BPF_JMP|code|BPF_X);
5490 if (reversed)
5491 gen_not(b);
5493 sappend(s0, s1);
5494 sappend(a1->s, s0);
5495 sappend(a0->s, a1->s);
5497 b->stmts = a0->s;
5499 free_reg(a0->regno);
5500 free_reg(a1->regno);
5502 /* 'and' together protocol checks */
5503 if (a0->b) {
5504 if (a1->b) {
5505 gen_and(a0->b, tmp = a1->b);
5507 else
5508 tmp = a0->b;
5509 } else
5510 tmp = a1->b;
5512 if (tmp)
5513 gen_and(tmp, b);
5515 return b;
5518 struct arth *
5519 gen_loadlen()
5521 int regno = alloc_reg();
5522 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5523 struct slist *s;
5525 s = new_stmt(BPF_LD|BPF_LEN);
5526 s->next = new_stmt(BPF_ST);
5527 s->next->s.k = regno;
5528 a->s = s;
5529 a->regno = regno;
5531 return a;
5534 struct arth *
5535 gen_loadi(val)
5536 int val;
5538 struct arth *a;
5539 struct slist *s;
5540 int reg;
5542 a = (struct arth *)newchunk(sizeof(*a));
5544 reg = alloc_reg();
5546 s = new_stmt(BPF_LD|BPF_IMM);
5547 s->s.k = val;
5548 s->next = new_stmt(BPF_ST);
5549 s->next->s.k = reg;
5550 a->s = s;
5551 a->regno = reg;
5553 return a;
5556 struct arth *
5557 gen_neg(a)
5558 struct arth *a;
5560 struct slist *s;
5562 s = xfer_to_a(a);
5563 sappend(a->s, s);
5564 s = new_stmt(BPF_ALU|BPF_NEG);
5565 s->s.k = 0;
5566 sappend(a->s, s);
5567 s = new_stmt(BPF_ST);
5568 s->s.k = a->regno;
5569 sappend(a->s, s);
5571 return a;
5574 struct arth *
5575 gen_arth(code, a0, a1)
5576 int code;
5577 struct arth *a0, *a1;
5579 struct slist *s0, *s1, *s2;
5581 s0 = xfer_to_x(a1);
5582 s1 = xfer_to_a(a0);
5583 s2 = new_stmt(BPF_ALU|BPF_X|code);
5585 sappend(s1, s2);
5586 sappend(s0, s1);
5587 sappend(a1->s, s0);
5588 sappend(a0->s, a1->s);
5590 free_reg(a0->regno);
5591 free_reg(a1->regno);
5593 s0 = new_stmt(BPF_ST);
5594 a0->regno = s0->s.k = alloc_reg();
5595 sappend(a0->s, s0);
5597 return a0;
5601 * Here we handle simple allocation of the scratch registers.
5602 * If too many registers are alloc'd, the allocator punts.
5604 static int regused[BPF_MEMWORDS];
5605 static int curreg;
5608 * Return the next free register.
5610 static int
5611 alloc_reg()
5613 int n = BPF_MEMWORDS;
5615 while (--n >= 0) {
5616 if (regused[curreg])
5617 curreg = (curreg + 1) % BPF_MEMWORDS;
5618 else {
5619 regused[curreg] = 1;
5620 return curreg;
5623 bpf_error("too many registers needed to evaluate expression");
5624 /* NOTREACHED */
5628 * Return a register to the table so it can
5629 * be used later.
5631 static void
5632 free_reg(n)
5633 int n;
5635 regused[n] = 0;
5638 static struct block *
5639 gen_len(jmp, n)
5640 int jmp, n;
5642 struct slist *s;
5643 struct block *b;
5645 s = new_stmt(BPF_LD|BPF_LEN);
5646 b = new_block(JMP(jmp));
5647 b->stmts = s;
5648 b->s.k = n;
5650 return b;
5653 struct block *
5654 gen_greater(n)
5655 int n;
5657 return gen_len(BPF_JGE, n);
5661 * Actually, this is less than or equal.
5663 struct block *
5664 gen_less(n)
5665 int n;
5667 struct block *b;
5669 b = gen_len(BPF_JGT, n);
5670 gen_not(b);
5672 return b;
5676 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5677 * the beginning of the link-layer header.
5678 * XXX - that means you can't test values in the radiotap header, but
5679 * as that header is difficult if not impossible to parse generally
5680 * without a loop, that might not be a severe problem. A new keyword
5681 * "radio" could be added for that, although what you'd really want
5682 * would be a way of testing particular radio header values, which
5683 * would generate code appropriate to the radio header in question.
5685 struct block *
5686 gen_byteop(op, idx, val)
5687 int op, idx, val;
5689 struct block *b;
5690 struct slist *s;
5692 switch (op) {
5693 default:
5694 abort();
5696 case '=':
5697 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5699 case '<':
5700 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5701 return b;
5703 case '>':
5704 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5705 return b;
5707 case '|':
5708 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
5709 break;
5711 case '&':
5712 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
5713 break;
5715 s->s.k = val;
5716 b = new_block(JMP(BPF_JEQ));
5717 b->stmts = s;
5718 gen_not(b);
5720 return b;
5723 static u_char abroadcast[] = { 0x0 };
5725 struct block *
5726 gen_broadcast(proto)
5727 int proto;
5729 bpf_u_int32 hostmask;
5730 struct block *b0, *b1, *b2;
5731 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5733 switch (proto) {
5735 case Q_DEFAULT:
5736 case Q_LINK:
5737 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5738 return gen_ahostop(abroadcast, Q_DST);
5739 if (linktype == DLT_EN10MB)
5740 return gen_ehostop(ebroadcast, Q_DST);
5741 if (linktype == DLT_FDDI)
5742 return gen_fhostop(ebroadcast, Q_DST);
5743 if (linktype == DLT_IEEE802)
5744 return gen_thostop(ebroadcast, Q_DST);
5745 if (linktype == DLT_IEEE802_11 ||
5746 linktype == DLT_IEEE802_11_RADIO_AVS ||
5747 linktype == DLT_IEEE802_11_RADIO ||
5748 linktype == DLT_PRISM_HEADER)
5749 return gen_wlanhostop(ebroadcast, Q_DST);
5750 if (linktype == DLT_IP_OVER_FC)
5751 return gen_ipfchostop(ebroadcast, Q_DST);
5752 if (linktype == DLT_SUNATM && is_lane) {
5754 * Check that the packet doesn't begin with an
5755 * LE Control marker. (We've already generated
5756 * a test for LANE.)
5758 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5759 0xFF00);
5760 gen_not(b1);
5763 * Now check the MAC address.
5765 b0 = gen_ehostop(ebroadcast, Q_DST);
5766 gen_and(b1, b0);
5767 return b0;
5769 bpf_error("not a broadcast link");
5770 break;
5772 case Q_IP:
5773 b0 = gen_linktype(ETHERTYPE_IP);
5774 hostmask = ~netmask;
5775 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
5776 b2 = gen_mcmp(OR_NET, 16, BPF_W,
5777 (bpf_int32)(~0 & hostmask), hostmask);
5778 gen_or(b1, b2);
5779 gen_and(b0, b2);
5780 return b2;
5782 bpf_error("only link-layer/IP broadcast filters supported");
5783 /* NOTREACHED */
5787 * Generate code to test the low-order bit of a MAC address (that's
5788 * the bottom bit of the *first* byte).
5790 static struct block *
5791 gen_mac_multicast(offset)
5792 int offset;
5794 register struct block *b0;
5795 register struct slist *s;
5797 /* link[offset] & 1 != 0 */
5798 s = gen_load_a(OR_LINK, offset, BPF_B);
5799 b0 = new_block(JMP(BPF_JSET));
5800 b0->s.k = 1;
5801 b0->stmts = s;
5802 return b0;
5805 struct block *
5806 gen_multicast(proto)
5807 int proto;
5809 register struct block *b0, *b1, *b2;
5810 register struct slist *s;
5812 switch (proto) {
5814 case Q_DEFAULT:
5815 case Q_LINK:
5816 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5817 /* all ARCnet multicasts use the same address */
5818 return gen_ahostop(abroadcast, Q_DST);
5820 if (linktype == DLT_EN10MB) {
5821 /* ether[0] & 1 != 0 */
5822 return gen_mac_multicast(0);
5825 if (linktype == DLT_FDDI) {
5827 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5829 * XXX - was that referring to bit-order issues?
5831 /* fddi[1] & 1 != 0 */
5832 return gen_mac_multicast(1);
5835 if (linktype == DLT_IEEE802) {
5836 /* tr[2] & 1 != 0 */
5837 return gen_mac_multicast(2);
5840 if (linktype == DLT_IEEE802_11 ||
5841 linktype == DLT_IEEE802_11_RADIO_AVS ||
5842 linktype == DLT_IEEE802_11_RADIO ||
5843 linktype == DLT_PRISM_HEADER) {
5845 * Oh, yuk.
5847 * For control frames, there is no DA.
5849 * For management frames, DA is at an
5850 * offset of 4 from the beginning of
5851 * the packet.
5853 * For data frames, DA is at an offset
5854 * of 4 from the beginning of the packet
5855 * if To DS is clear and at an offset of
5856 * 16 from the beginning of the packet
5857 * if To DS is set.
5861 * Generate the tests to be done for data frames.
5863 * First, check for To DS set, i.e. "link[1] & 0x01".
5865 s = gen_load_a(OR_LINK, 1, BPF_B);
5866 b1 = new_block(JMP(BPF_JSET));
5867 b1->s.k = 0x01; /* To DS */
5868 b1->stmts = s;
5871 * If To DS is set, the DA is at 16.
5873 b0 = gen_mac_multicast(16);
5874 gen_and(b1, b0);
5877 * Now, check for To DS not set, i.e. check
5878 * "!(link[1] & 0x01)".
5880 s = gen_load_a(OR_LINK, 1, BPF_B);
5881 b2 = new_block(JMP(BPF_JSET));
5882 b2->s.k = 0x01; /* To DS */
5883 b2->stmts = s;
5884 gen_not(b2);
5887 * If To DS is not set, the DA is at 4.
5889 b1 = gen_mac_multicast(4);
5890 gen_and(b2, b1);
5893 * Now OR together the last two checks. That gives
5894 * the complete set of checks for data frames.
5896 gen_or(b1, b0);
5899 * Now check for a data frame.
5900 * I.e, check "link[0] & 0x08".
5902 s = gen_load_a(OR_LINK, 0, BPF_B);
5903 b1 = new_block(JMP(BPF_JSET));
5904 b1->s.k = 0x08;
5905 b1->stmts = s;
5908 * AND that with the checks done for data frames.
5910 gen_and(b1, b0);
5913 * If the high-order bit of the type value is 0, this
5914 * is a management frame.
5915 * I.e, check "!(link[0] & 0x08)".
5917 s = gen_load_a(OR_LINK, 0, BPF_B);
5918 b2 = new_block(JMP(BPF_JSET));
5919 b2->s.k = 0x08;
5920 b2->stmts = s;
5921 gen_not(b2);
5924 * For management frames, the DA is at 4.
5926 b1 = gen_mac_multicast(4);
5927 gen_and(b2, b1);
5930 * OR that with the checks done for data frames.
5931 * That gives the checks done for management and
5932 * data frames.
5934 gen_or(b1, b0);
5937 * If the low-order bit of the type value is 1,
5938 * this is either a control frame or a frame
5939 * with a reserved type, and thus not a
5940 * frame with an SA.
5942 * I.e., check "!(link[0] & 0x04)".
5944 s = gen_load_a(OR_LINK, 0, BPF_B);
5945 b1 = new_block(JMP(BPF_JSET));
5946 b1->s.k = 0x04;
5947 b1->stmts = s;
5948 gen_not(b1);
5951 * AND that with the checks for data and management
5952 * frames.
5954 gen_and(b1, b0);
5955 return b0;
5958 if (linktype == DLT_IP_OVER_FC) {
5959 b0 = gen_mac_multicast(2);
5960 return b0;
5963 if (linktype == DLT_SUNATM && is_lane) {
5965 * Check that the packet doesn't begin with an
5966 * LE Control marker. (We've already generated
5967 * a test for LANE.)
5969 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5970 0xFF00);
5971 gen_not(b1);
5973 /* ether[off_mac] & 1 != 0 */
5974 b0 = gen_mac_multicast(off_mac);
5975 gen_and(b1, b0);
5976 return b0;
5979 /* Link not known to support multicasts */
5980 break;
5982 case Q_IP:
5983 b0 = gen_linktype(ETHERTYPE_IP);
5984 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
5985 gen_and(b0, b1);
5986 return b1;
5988 #ifdef INET6
5989 case Q_IPV6:
5990 b0 = gen_linktype(ETHERTYPE_IPV6);
5991 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
5992 gen_and(b0, b1);
5993 return b1;
5994 #endif /* INET6 */
5996 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5997 /* NOTREACHED */
6001 * generate command for inbound/outbound. It's here so we can
6002 * make it link-type specific. 'dir' = 0 implies "inbound",
6003 * = 1 implies "outbound".
6005 struct block *
6006 gen_inbound(dir)
6007 int dir;
6009 register struct block *b0;
6012 * Only some data link types support inbound/outbound qualifiers.
6014 switch (linktype) {
6015 case DLT_SLIP:
6016 b0 = gen_relation(BPF_JEQ,
6017 gen_load(Q_LINK, gen_loadi(0), 1),
6018 gen_loadi(0),
6019 dir);
6020 break;
6022 case DLT_LINUX_SLL:
6023 if (dir) {
6025 * Match packets sent by this machine.
6027 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6028 } else {
6030 * Match packets sent to this machine.
6031 * (No broadcast or multicast packets, or
6032 * packets sent to some other machine and
6033 * received promiscuously.)
6035 * XXX - packets sent to other machines probably
6036 * shouldn't be matched, but what about broadcast
6037 * or multicast packets we received?
6039 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6041 break;
6043 case DLT_PFLOG:
6044 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6045 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6046 break;
6048 case DLT_PPP_PPPD:
6049 if (dir) {
6050 /* match outgoing packets */
6051 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6052 } else {
6053 /* match incoming packets */
6054 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6056 break;
6058 case DLT_JUNIPER_MFR:
6059 case DLT_JUNIPER_MLFR:
6060 case DLT_JUNIPER_MLPPP:
6061 case DLT_JUNIPER_ATM1:
6062 case DLT_JUNIPER_ATM2:
6063 case DLT_JUNIPER_PPPOE:
6064 case DLT_JUNIPER_PPPOE_ATM:
6065 case DLT_JUNIPER_GGSN:
6066 case DLT_JUNIPER_ES:
6067 case DLT_JUNIPER_MONITOR:
6068 case DLT_JUNIPER_SERVICES:
6069 case DLT_JUNIPER_ETHER:
6070 case DLT_JUNIPER_PPP:
6071 case DLT_JUNIPER_FRELAY:
6072 case DLT_JUNIPER_CHDLC:
6073 /* juniper flags (including direction) are stored
6074 * the byte after the 3-byte magic number */
6075 if (dir) {
6076 /* match outgoing packets */
6077 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6078 } else {
6079 /* match incoming packets */
6080 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6082 break;
6084 default:
6085 bpf_error("inbound/outbound not supported on linktype %d",
6086 linktype);
6087 b0 = NULL;
6088 /* NOTREACHED */
6090 return (b0);
6093 /* PF firewall log matched interface */
6094 struct block *
6095 gen_pf_ifname(const char *ifname)
6097 struct block *b0;
6098 u_int len, off;
6100 if (linktype == DLT_PFLOG) {
6101 len = sizeof(((struct pfloghdr *)0)->ifname);
6102 off = offsetof(struct pfloghdr, ifname);
6103 } else {
6104 bpf_error("ifname not supported on linktype 0x%x", linktype);
6105 /* NOTREACHED */
6107 if (strlen(ifname) >= len) {
6108 bpf_error("ifname interface names can only be %d characters",
6109 len-1);
6110 /* NOTREACHED */
6112 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6113 return (b0);
6116 /* PF firewall log ruleset name */
6117 struct block *
6118 gen_pf_ruleset(char *ruleset)
6120 struct block *b0;
6122 if (linktype != DLT_PFLOG) {
6123 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6124 /* NOTREACHED */
6126 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6127 bpf_error("ruleset names can only be %ld characters",
6128 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6129 /* NOTREACHED */
6131 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6132 strlen(ruleset), (const u_char *)ruleset);
6133 return (b0);
6136 /* PF firewall log rule number */
6137 struct block *
6138 gen_pf_rnr(int rnr)
6140 struct block *b0;
6142 if (linktype == DLT_PFLOG) {
6143 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6144 (bpf_int32)rnr);
6145 } else {
6146 bpf_error("rnr not supported on linktype 0x%x", linktype);
6147 /* NOTREACHED */
6150 return (b0);
6153 /* PF firewall log sub-rule number */
6154 struct block *
6155 gen_pf_srnr(int srnr)
6157 struct block *b0;
6159 if (linktype != DLT_PFLOG) {
6160 bpf_error("srnr not supported on linktype 0x%x", linktype);
6161 /* NOTREACHED */
6164 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6165 (bpf_int32)srnr);
6166 return (b0);
6169 /* PF firewall log reason code */
6170 struct block *
6171 gen_pf_reason(int reason)
6173 struct block *b0;
6175 if (linktype == DLT_PFLOG) {
6176 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6177 (bpf_int32)reason);
6178 } else {
6179 bpf_error("reason not supported on linktype 0x%x", linktype);
6180 /* NOTREACHED */
6183 return (b0);
6186 /* PF firewall log action */
6187 struct block *
6188 gen_pf_action(int action)
6190 struct block *b0;
6192 if (linktype == DLT_PFLOG) {
6193 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6194 (bpf_int32)action);
6195 } else {
6196 bpf_error("action not supported on linktype 0x%x", linktype);
6197 /* NOTREACHED */
6200 return (b0);
6203 struct block *
6204 gen_acode(eaddr, q)
6205 register const u_char *eaddr;
6206 struct qual q;
6208 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6209 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6210 return gen_ahostop(eaddr, (int)q.dir);
6212 bpf_error("ARCnet address used in non-arc expression");
6213 /* NOTREACHED */
6216 static struct block *
6217 gen_ahostop(eaddr, dir)
6218 register const u_char *eaddr;
6219 register int dir;
6221 register struct block *b0, *b1;
6223 switch (dir) {
6224 /* src comes first, different from Ethernet */
6225 case Q_SRC:
6226 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6228 case Q_DST:
6229 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6231 case Q_AND:
6232 b0 = gen_ahostop(eaddr, Q_SRC);
6233 b1 = gen_ahostop(eaddr, Q_DST);
6234 gen_and(b0, b1);
6235 return b1;
6237 case Q_DEFAULT:
6238 case Q_OR:
6239 b0 = gen_ahostop(eaddr, Q_SRC);
6240 b1 = gen_ahostop(eaddr, Q_DST);
6241 gen_or(b0, b1);
6242 return b1;
6244 abort();
6245 /* NOTREACHED */
6249 * support IEEE 802.1Q VLAN trunk over ethernet
6251 struct block *
6252 gen_vlan(vlan_num)
6253 int vlan_num;
6255 struct block *b0, *b1;
6257 /* can't check for VLAN-encapsulated packets inside MPLS */
6258 if (label_stack_depth > 0)
6259 bpf_error("no VLAN match after MPLS");
6262 * Change the offsets to point to the type and data fields within
6263 * the VLAN packet. Just increment the offsets, so that we
6264 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6265 * capture VLAN 200 encapsulated within VLAN 100.
6267 * XXX - this is a bit of a kludge. If we were to split the
6268 * compiler into a parser that parses an expression and
6269 * generates an expression tree, and a code generator that
6270 * takes an expression tree (which could come from our
6271 * parser or from some other parser) and generates BPF code,
6272 * we could perhaps make the offsets parameters of routines
6273 * and, in the handler for an "AND" node, pass to subnodes
6274 * other than the VLAN node the adjusted offsets.
6276 * This would mean that "vlan" would, instead of changing the
6277 * behavior of *all* tests after it, change only the behavior
6278 * of tests ANDed with it. That would change the documented
6279 * semantics of "vlan", which might break some expressions.
6280 * However, it would mean that "(vlan and ip) or ip" would check
6281 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6282 * checking only for VLAN-encapsulated IP, so that could still
6283 * be considered worth doing; it wouldn't break expressions
6284 * that are of the form "vlan and ..." or "vlan N and ...",
6285 * which I suspect are the most common expressions involving
6286 * "vlan". "vlan or ..." doesn't necessarily do what the user
6287 * would really want, now, as all the "or ..." tests would
6288 * be done assuming a VLAN, even though the "or" could be viewed
6289 * as meaning "or, if this isn't a VLAN packet...".
6291 orig_linktype = off_linktype; /* save original values */
6292 orig_nl = off_nl;
6294 switch (linktype) {
6296 case DLT_EN10MB:
6297 off_linktype += 4;
6298 off_nl_nosnap += 4;
6299 off_nl += 4;
6300 break;
6302 default:
6303 bpf_error("no VLAN support for data link type %d",
6304 linktype);
6305 /*NOTREACHED*/
6308 /* check for VLAN */
6309 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6311 /* If a specific VLAN is requested, check VLAN id */
6312 if (vlan_num >= 0) {
6313 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6314 0x0fff);
6315 gen_and(b0, b1);
6316 b0 = b1;
6319 return (b0);
6323 * support for MPLS
6325 struct block *
6326 gen_mpls(label_num)
6327 int label_num;
6329 struct block *b0,*b1;
6332 * Change the offsets to point to the type and data fields within
6333 * the MPLS packet. Just increment the offsets, so that we
6334 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6335 * capture packets with an outer label of 100000 and an inner
6336 * label of 1024.
6338 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6340 orig_nl = off_nl;
6342 if (label_stack_depth > 0) {
6343 /* just match the bottom-of-stack bit clear */
6344 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6345 } else {
6347 * Indicate that we're checking MPLS-encapsulated headers,
6348 * to make sure higher level code generators don't try to
6349 * match against IP-related protocols such as Q_ARP, Q_RARP
6350 * etc.
6352 switch (linktype) {
6354 case DLT_C_HDLC: /* fall through */
6355 case DLT_EN10MB:
6356 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6357 (bpf_int32)ETHERTYPE_MPLS);
6358 break;
6360 case DLT_PPP:
6361 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6362 (bpf_int32)PPP_MPLS_UCAST);
6363 break;
6365 /* FIXME add other DLT_s ...
6366 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6367 * leave it for now */
6369 default:
6370 bpf_error("no MPLS support for data link type %d",
6371 linktype);
6372 b0 = NULL;
6373 /*NOTREACHED*/
6374 break;
6378 /* If a specific MPLS label is requested, check it */
6379 if (label_num >= 0) {
6380 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6381 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6382 0xfffff000); /* only compare the first 20 bits */
6383 gen_and(b0, b1);
6384 b0 = b1;
6387 off_nl_nosnap += 4;
6388 off_nl += 4;
6389 label_stack_depth++;
6390 return (b0);
6394 * Support PPPOE discovery and session.
6396 struct block *
6397 gen_pppoed()
6399 /* check for PPPoE discovery */
6400 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6403 struct block *
6404 gen_pppoes()
6406 struct block *b0;
6409 * Test against the PPPoE session link-layer type.
6411 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6414 * Change the offsets to point to the type and data fields within
6415 * the PPP packet.
6417 * XXX - this is a bit of a kludge. If we were to split the
6418 * compiler into a parser that parses an expression and
6419 * generates an expression tree, and a code generator that
6420 * takes an expression tree (which could come from our
6421 * parser or from some other parser) and generates BPF code,
6422 * we could perhaps make the offsets parameters of routines
6423 * and, in the handler for an "AND" node, pass to subnodes
6424 * other than the PPPoE node the adjusted offsets.
6426 * This would mean that "pppoes" would, instead of changing the
6427 * behavior of *all* tests after it, change only the behavior
6428 * of tests ANDed with it. That would change the documented
6429 * semantics of "pppoes", which might break some expressions.
6430 * However, it would mean that "(pppoes and ip) or ip" would check
6431 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6432 * checking only for VLAN-encapsulated IP, so that could still
6433 * be considered worth doing; it wouldn't break expressions
6434 * that are of the form "pppoes and ..." which I suspect are the
6435 * most common expressions involving "pppoes". "pppoes or ..."
6436 * doesn't necessarily do what the user would really want, now,
6437 * as all the "or ..." tests would be done assuming PPPoE, even
6438 * though the "or" could be viewed as meaning "or, if this isn't
6439 * a PPPoE packet...".
6441 orig_linktype = off_linktype; /* save original values */
6442 orig_nl = off_nl;
6445 * The "network-layer" protocol is PPPoE, which has a 6-byte
6446 * PPPoE header, followed by PPP payload, so we set the
6447 * offsets to the network layer offset plus 6 bytes for
6448 * the PPPoE header plus the values appropriate for PPP when
6449 * encapsulated in Ethernet (which means there's no HDLC
6450 * encapsulation).
6452 off_linktype = orig_nl + 6;
6453 off_nl = orig_nl + 6 + 2;
6454 off_nl_nosnap = orig_nl + 6 + 2;
6457 * Set the link-layer type to PPP, as all subsequent tests will
6458 * be on the encapsulated PPP header.
6460 linktype = DLT_PPP;
6462 return b0;
6465 struct block *
6466 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6467 int atmfield;
6468 bpf_int32 jvalue;
6469 bpf_u_int32 jtype;
6470 int reverse;
6472 struct block *b0;
6474 switch (atmfield) {
6476 case A_VPI:
6477 if (!is_atm)
6478 bpf_error("'vpi' supported only on raw ATM");
6479 if (off_vpi == (u_int)-1)
6480 abort();
6481 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6482 reverse, jvalue);
6483 break;
6485 case A_VCI:
6486 if (!is_atm)
6487 bpf_error("'vci' supported only on raw ATM");
6488 if (off_vci == (u_int)-1)
6489 abort();
6490 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6491 reverse, jvalue);
6492 break;
6494 case A_PROTOTYPE:
6495 if (off_proto == (u_int)-1)
6496 abort(); /* XXX - this isn't on FreeBSD */
6497 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6498 reverse, jvalue);
6499 break;
6501 case A_MSGTYPE:
6502 if (off_payload == (u_int)-1)
6503 abort();
6504 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6505 0xffffffff, jtype, reverse, jvalue);
6506 break;
6508 case A_CALLREFTYPE:
6509 if (!is_atm)
6510 bpf_error("'callref' supported only on raw ATM");
6511 if (off_proto == (u_int)-1)
6512 abort();
6513 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6514 jtype, reverse, jvalue);
6515 break;
6517 default:
6518 abort();
6520 return b0;
6523 struct block *
6524 gen_atmtype_abbrev(type)
6525 int type;
6527 struct block *b0, *b1;
6529 switch (type) {
6531 case A_METAC:
6532 /* Get all packets in Meta signalling Circuit */
6533 if (!is_atm)
6534 bpf_error("'metac' supported only on raw ATM");
6535 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6536 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6537 gen_and(b0, b1);
6538 break;
6540 case A_BCC:
6541 /* Get all packets in Broadcast Circuit*/
6542 if (!is_atm)
6543 bpf_error("'bcc' supported only on raw ATM");
6544 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6545 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6546 gen_and(b0, b1);
6547 break;
6549 case A_OAMF4SC:
6550 /* Get all cells in Segment OAM F4 circuit*/
6551 if (!is_atm)
6552 bpf_error("'oam4sc' supported only on raw ATM");
6553 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6554 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6555 gen_and(b0, b1);
6556 break;
6558 case A_OAMF4EC:
6559 /* Get all cells in End-to-End OAM F4 Circuit*/
6560 if (!is_atm)
6561 bpf_error("'oam4ec' supported only on raw ATM");
6562 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6563 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6564 gen_and(b0, b1);
6565 break;
6567 case A_SC:
6568 /* Get all packets in connection Signalling Circuit */
6569 if (!is_atm)
6570 bpf_error("'sc' supported only on raw ATM");
6571 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6572 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6573 gen_and(b0, b1);
6574 break;
6576 case A_ILMIC:
6577 /* Get all packets in ILMI Circuit */
6578 if (!is_atm)
6579 bpf_error("'ilmic' supported only on raw ATM");
6580 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6581 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6582 gen_and(b0, b1);
6583 break;
6585 case A_LANE:
6586 /* Get all LANE packets */
6587 if (!is_atm)
6588 bpf_error("'lane' supported only on raw ATM");
6589 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6592 * Arrange that all subsequent tests assume LANE
6593 * rather than LLC-encapsulated packets, and set
6594 * the offsets appropriately for LANE-encapsulated
6595 * Ethernet.
6597 * "off_mac" is the offset of the Ethernet header,
6598 * which is 2 bytes past the ATM pseudo-header
6599 * (skipping the pseudo-header and 2-byte LE Client
6600 * field). The other offsets are Ethernet offsets
6601 * relative to "off_mac".
6603 is_lane = 1;
6604 off_mac = off_payload + 2; /* MAC header */
6605 off_linktype = off_mac + 12;
6606 off_nl = off_mac + 14; /* Ethernet II */
6607 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6608 break;
6610 case A_LLC:
6611 /* Get all LLC-encapsulated packets */
6612 if (!is_atm)
6613 bpf_error("'llc' supported only on raw ATM");
6614 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6615 is_lane = 0;
6616 break;
6618 default:
6619 abort();
6621 return b1;
6624 struct block *
6625 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
6626 int mtp3field;
6627 bpf_u_int32 jvalue;
6628 bpf_u_int32 jtype;
6629 int reverse;
6631 struct block *b0;
6632 bpf_u_int32 val1 , val2 , val3;
6634 switch (mtp3field) {
6636 case M_SIO:
6637 if (off_sio == (u_int)-1)
6638 bpf_error("'sio' supported only on SS7");
6639 /* sio coded on 1 byte so max value 255 */
6640 if(jvalue > 255)
6641 bpf_error("sio value %u too big; max value = 255",
6642 jvalue);
6643 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
6644 (u_int)jtype, reverse, (u_int)jvalue);
6645 break;
6647 case M_OPC:
6648 if (off_opc == (u_int)-1)
6649 bpf_error("'opc' supported only on SS7");
6650 /* opc coded on 14 bits so max value 16383 */
6651 if (jvalue > 16383)
6652 bpf_error("opc value %u too big; max value = 16383",
6653 jvalue);
6654 /* the following instructions are made to convert jvalue
6655 * to the form used to write opc in an ss7 message*/
6656 val1 = jvalue & 0x00003c00;
6657 val1 = val1 >>10;
6658 val2 = jvalue & 0x000003fc;
6659 val2 = val2 <<6;
6660 val3 = jvalue & 0x00000003;
6661 val3 = val3 <<22;
6662 jvalue = val1 + val2 + val3;
6663 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
6664 (u_int)jtype, reverse, (u_int)jvalue);
6665 break;
6667 case M_DPC:
6668 if (off_dpc == (u_int)-1)
6669 bpf_error("'dpc' supported only on SS7");
6670 /* dpc coded on 14 bits so max value 16383 */
6671 if (jvalue > 16383)
6672 bpf_error("dpc value %u too big; max value = 16383",
6673 jvalue);
6674 /* the following instructions are made to convert jvalue
6675 * to the forme used to write dpc in an ss7 message*/
6676 val1 = jvalue & 0x000000ff;
6677 val1 = val1 << 24;
6678 val2 = jvalue & 0x00003f00;
6679 val2 = val2 << 8;
6680 jvalue = val1 + val2;
6681 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
6682 (u_int)jtype, reverse, (u_int)jvalue);
6683 break;
6685 case M_SLS:
6686 if (off_sls == (u_int)-1)
6687 bpf_error("'sls' supported only on SS7");
6688 /* sls coded on 4 bits so max value 15 */
6689 if (jvalue > 15)
6690 bpf_error("sls value %u too big; max value = 15",
6691 jvalue);
6692 /* the following instruction is made to convert jvalue
6693 * to the forme used to write sls in an ss7 message*/
6694 jvalue = jvalue << 4;
6695 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
6696 (u_int)jtype,reverse, (u_int)jvalue);
6697 break;
6699 default:
6700 abort();
6702 return b0;
6705 static struct block *
6706 gen_msg_abbrev(type)
6707 int type;
6709 struct block *b1;
6712 * Q.2931 signalling protocol messages for handling virtual circuits
6713 * establishment and teardown
6715 switch (type) {
6717 case A_SETUP:
6718 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
6719 break;
6721 case A_CALLPROCEED:
6722 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
6723 break;
6725 case A_CONNECT:
6726 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
6727 break;
6729 case A_CONNECTACK:
6730 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
6731 break;
6733 case A_RELEASE:
6734 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
6735 break;
6737 case A_RELEASE_DONE:
6738 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
6739 break;
6741 default:
6742 abort();
6744 return b1;
6747 struct block *
6748 gen_atmmulti_abbrev(type)
6749 int type;
6751 struct block *b0, *b1;
6753 switch (type) {
6755 case A_OAM:
6756 if (!is_atm)
6757 bpf_error("'oam' supported only on raw ATM");
6758 b1 = gen_atmmulti_abbrev(A_OAMF4);
6759 break;
6761 case A_OAMF4:
6762 if (!is_atm)
6763 bpf_error("'oamf4' supported only on raw ATM");
6764 /* OAM F4 type */
6765 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6766 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6767 gen_or(b0, b1);
6768 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6769 gen_and(b0, b1);
6770 break;
6772 case A_CONNECTMSG:
6774 * Get Q.2931 signalling messages for switched
6775 * virtual connection
6777 if (!is_atm)
6778 bpf_error("'connectmsg' supported only on raw ATM");
6779 b0 = gen_msg_abbrev(A_SETUP);
6780 b1 = gen_msg_abbrev(A_CALLPROCEED);
6781 gen_or(b0, b1);
6782 b0 = gen_msg_abbrev(A_CONNECT);
6783 gen_or(b0, b1);
6784 b0 = gen_msg_abbrev(A_CONNECTACK);
6785 gen_or(b0, b1);
6786 b0 = gen_msg_abbrev(A_RELEASE);
6787 gen_or(b0, b1);
6788 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6789 gen_or(b0, b1);
6790 b0 = gen_atmtype_abbrev(A_SC);
6791 gen_and(b0, b1);
6792 break;
6794 case A_METACONNECT:
6795 if (!is_atm)
6796 bpf_error("'metaconnect' supported only on raw ATM");
6797 b0 = gen_msg_abbrev(A_SETUP);
6798 b1 = gen_msg_abbrev(A_CALLPROCEED);
6799 gen_or(b0, b1);
6800 b0 = gen_msg_abbrev(A_CONNECT);
6801 gen_or(b0, b1);
6802 b0 = gen_msg_abbrev(A_RELEASE);
6803 gen_or(b0, b1);
6804 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6805 gen_or(b0, b1);
6806 b0 = gen_atmtype_abbrev(A_METAC);
6807 gen_and(b0, b1);
6808 break;
6810 default:
6811 abort();
6813 return b1;