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