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