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