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