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