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