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