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