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