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