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