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