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