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