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