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