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