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