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