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