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