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