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