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