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