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