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