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