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