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