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