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