]> The Tcpdump Group git mirrors - libpcap/blob - gencode.c
Make indentation consistent in gencode.c.
[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_SUNATM:
5484 /*
5485 * This is LLC-multiplexed traffic; if it were
5486 * LANE, cstate->linktype would have been set to
5487 * DLT_EN10MB.
5488 */
5489 bpf_error(cstate,
5490 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5491 case DLT_IP_OVER_FC:
5492 b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5493 break;
5494 default:
5495 bpf_error(cstate,
5496 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5497 }
5498 b1 = NULL;
5499 for (ai = alist; ai != NULL; ai = ai->ai_next) {
5500 /*
5501 * Does it have an address?
5502 */
5503 if (ai->ai_addr != NULL) {
5504 /*
5505 * Yes. Is it an IPv4 address?
5506 */
5507 if (ai->ai_addr->sa_family == AF_INET) {
5508 /*
5509 * Generate an entry for it.
5510 */
5511 sin = (struct sockaddr_in *)ai->ai_addr;
5512 tmp = gen_host(cstate,
5513 ntohl(sin->sin_addr.s_addr),
5514 0xffffffff, proto, Q_OR, Q_HOST);
5515 /*
5516 * Is it the *first* IPv4 address?
5517 */
5518 if (b1 == NULL) {
5519 /*
5520 * Yes, so start with it.
5521 */
5522 b1 = tmp;
5523 } else {
5524 /*
5525 * No, so OR it into the
5526 * existing set of
5527 * addresses.
5528 */
5529 gen_or(b1, tmp);
5530 b1 = tmp;
5531 }
5532 }
5533 }
5534 }
5535 if (b1 == NULL) {
5536 /*
5537 * No IPv4 addresses found.
5538 */
5539 return (NULL);
5540 }
5541 gen_not(b1);
5542 gen_and(b0, b1);
5543 return b1;
5544 }
5545 bpf_error(cstate, "illegal modifier of 'gateway'");
5546 /*NOTREACHED*/
5547 }
5548 #endif
5549
5550 static struct block *
5551 gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
5552 {
5553 struct block *b0;
5554 struct block *b1;
5555
5556 switch (proto) {
5557
5558 case Q_SCTP:
5559 b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT);
5560 break;
5561
5562 case Q_TCP:
5563 b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT);
5564 break;
5565
5566 case Q_UDP:
5567 b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT);
5568 break;
5569
5570 case Q_ICMP:
5571 b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5572 break;
5573
5574 #ifndef IPPROTO_IGMP
5575 #define IPPROTO_IGMP 2
5576 #endif
5577
5578 case Q_IGMP:
5579 b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5580 break;
5581
5582 #ifndef IPPROTO_IGRP
5583 #define IPPROTO_IGRP 9
5584 #endif
5585 case Q_IGRP:
5586 b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5587 break;
5588
5589 #ifndef IPPROTO_PIM
5590 #define IPPROTO_PIM 103
5591 #endif
5592
5593 case Q_PIM:
5594 b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT);
5595 break;
5596
5597 #ifndef IPPROTO_VRRP
5598 #define IPPROTO_VRRP 112
5599 #endif
5600
5601 case Q_VRRP:
5602 b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5603 break;
5604
5605 #ifndef IPPROTO_CARP
5606 #define IPPROTO_CARP 112
5607 #endif
5608
5609 case Q_CARP:
5610 b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5611 break;
5612
5613 case Q_IP:
5614 b1 = gen_linktype(cstate, ETHERTYPE_IP);
5615 break;
5616
5617 case Q_ARP:
5618 b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5619 break;
5620
5621 case Q_RARP:
5622 b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5623 break;
5624
5625 case Q_LINK:
5626 bpf_error(cstate, "link layer applied in wrong context");
5627
5628 case Q_ATALK:
5629 b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5630 break;
5631
5632 case Q_AARP:
5633 b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5634 break;
5635
5636 case Q_DECNET:
5637 b1 = gen_linktype(cstate, ETHERTYPE_DN);
5638 break;
5639
5640 case Q_SCA:
5641 b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5642 break;
5643
5644 case Q_LAT:
5645 b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5646 break;
5647
5648 case Q_MOPDL:
5649 b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5650 break;
5651
5652 case Q_MOPRC:
5653 b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5654 break;
5655
5656 case Q_IPV6:
5657 b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5658 break;
5659
5660 #ifndef IPPROTO_ICMPV6
5661 #define IPPROTO_ICMPV6 58
5662 #endif
5663 case Q_ICMPV6:
5664 b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5665 break;
5666
5667 #ifndef IPPROTO_AH
5668 #define IPPROTO_AH 51
5669 #endif
5670 case Q_AH:
5671 b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT);
5672 break;
5673
5674 #ifndef IPPROTO_ESP
5675 #define IPPROTO_ESP 50
5676 #endif
5677 case Q_ESP:
5678 b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT);
5679 break;
5680
5681 case Q_ISO:
5682 b1 = gen_linktype(cstate, LLCSAP_ISONS);
5683 break;
5684
5685 case Q_ESIS:
5686 b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5687 break;
5688
5689 case Q_ISIS:
5690 b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5691 break;
5692
5693 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
5694 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5695 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5696 gen_or(b0, b1);
5697 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5698 gen_or(b0, b1);
5699 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5700 gen_or(b0, b1);
5701 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5702 gen_or(b0, b1);
5703 break;
5704
5705 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
5706 b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5707 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5708 gen_or(b0, b1);
5709 b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5710 gen_or(b0, b1);
5711 b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5712 gen_or(b0, b1);
5713 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5714 gen_or(b0, b1);
5715 break;
5716
5717 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
5718 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5719 b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5720 gen_or(b0, b1);
5721 b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5722 gen_or(b0, b1);
5723 break;
5724
5725 case Q_ISIS_LSP:
5726 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5727 b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5728 gen_or(b0, b1);
5729 break;
5730
5731 case Q_ISIS_SNP:
5732 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5733 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5734 gen_or(b0, b1);
5735 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5736 gen_or(b0, b1);
5737 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5738 gen_or(b0, b1);
5739 break;
5740
5741 case Q_ISIS_CSNP:
5742 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5743 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5744 gen_or(b0, b1);
5745 break;
5746
5747 case Q_ISIS_PSNP:
5748 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5749 b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5750 gen_or(b0, b1);
5751 break;
5752
5753 case Q_CLNP:
5754 b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5755 break;
5756
5757 case Q_STP:
5758 b1 = gen_linktype(cstate, LLCSAP_8021D);
5759 break;
5760
5761 case Q_IPX:
5762 b1 = gen_linktype(cstate, LLCSAP_IPX);
5763 break;
5764
5765 case Q_NETBEUI:
5766 b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5767 break;
5768
5769 case Q_RADIO:
5770 bpf_error(cstate, "'radio' is not a valid protocol type");
5771
5772 default:
5773 abort();
5774 }
5775 return b1;
5776 }
5777
5778 struct block *
5779 gen_proto_abbrev(compiler_state_t *cstate, int proto)
5780 {
5781 /*
5782 * Catch errors reported by us and routines below us, and return NULL
5783 * on an error.
5784 */
5785 if (setjmp(cstate->top_ctx))
5786 return (NULL);
5787
5788 return gen_proto_abbrev_internal(cstate, proto);
5789 }
5790
5791 static struct block *
5792 gen_ipfrag(compiler_state_t *cstate)
5793 {
5794 struct slist *s;
5795 struct block *b;
5796
5797 /* not IPv4 frag other than the first frag */
5798 s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5799 b = new_block(cstate, JMP(BPF_JSET));
5800 b->s.k = 0x1fff;
5801 b->stmts = s;
5802 gen_not(b);
5803
5804 return b;
5805 }
5806
5807 /*
5808 * Generate a comparison to a port value in the transport-layer header
5809 * at the specified offset from the beginning of that header.
5810 *
5811 * XXX - this handles a variable-length prefix preceding the link-layer
5812 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5813 * variable-length link-layer headers (such as Token Ring or 802.11
5814 * headers).
5815 */
5816 static struct block *
5817 gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
5818 {
5819 return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5820 }
5821
5822 static struct block *
5823 gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
5824 {
5825 return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5826 }
5827
5828 static struct block *
5829 gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5830 {
5831 struct block *b0, *b1, *tmp;
5832
5833 /* ip proto 'proto' and not a fragment other than the first fragment */
5834 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5835 b0 = gen_ipfrag(cstate);
5836 gen_and(tmp, b0);
5837
5838 switch (dir) {
5839 case Q_SRC:
5840 b1 = gen_portatom(cstate, 0, port);
5841 break;
5842
5843 case Q_DST:
5844 b1 = gen_portatom(cstate, 2, port);
5845 break;
5846
5847 case Q_AND:
5848 tmp = gen_portatom(cstate, 0, port);
5849 b1 = gen_portatom(cstate, 2, port);
5850 gen_and(tmp, b1);
5851 break;
5852
5853 case Q_DEFAULT:
5854 case Q_OR:
5855 tmp = gen_portatom(cstate, 0, port);
5856 b1 = gen_portatom(cstate, 2, port);
5857 gen_or(tmp, b1);
5858 break;
5859
5860 case Q_ADDR1:
5861 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
5862 /*NOTREACHED*/
5863
5864 case Q_ADDR2:
5865 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
5866 /*NOTREACHED*/
5867
5868 case Q_ADDR3:
5869 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
5870 /*NOTREACHED*/
5871
5872 case Q_ADDR4:
5873 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
5874 /*NOTREACHED*/
5875
5876 case Q_RA:
5877 bpf_error(cstate, "'ra' is not a valid qualifier for ports");
5878 /*NOTREACHED*/
5879
5880 case Q_TA:
5881 bpf_error(cstate, "'ta' is not a valid qualifier for ports");
5882 /*NOTREACHED*/
5883
5884 default:
5885 abort();
5886 /*NOTREACHED*/
5887 }
5888 gen_and(b0, b1);
5889
5890 return b1;
5891 }
5892
5893 static struct block *
5894 gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5895 {
5896 struct block *b0, *b1, *tmp;
5897
5898 /*
5899 * ether proto ip
5900 *
5901 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5902 * not LLC encapsulation with LLCSAP_IP.
5903 *
5904 * For IEEE 802 networks - which includes 802.5 token ring
5905 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5906 * says that SNAP encapsulation is used, not LLC encapsulation
5907 * with LLCSAP_IP.
5908 *
5909 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5910 * RFC 2225 say that SNAP encapsulation is used, not LLC
5911 * encapsulation with LLCSAP_IP.
5912 *
5913 * So we always check for ETHERTYPE_IP.
5914 */
5915 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5916
5917 switch (ip_proto) {
5918 case IPPROTO_UDP:
5919 case IPPROTO_TCP:
5920 case IPPROTO_SCTP:
5921 b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
5922 break;
5923
5924 case PROTO_UNDEF:
5925 tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5926 b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5927 gen_or(tmp, b1);
5928 tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5929 gen_or(tmp, b1);
5930 break;
5931
5932 default:
5933 abort();
5934 }
5935 gen_and(b0, b1);
5936 return b1;
5937 }
5938
5939 struct block *
5940 gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5941 {
5942 struct block *b0, *b1, *tmp;
5943
5944 /* ip6 proto 'proto' */
5945 /* XXX - catch the first fragment of a fragmented packet? */
5946 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5947
5948 switch (dir) {
5949 case Q_SRC:
5950 b1 = gen_portatom6(cstate, 0, port);
5951 break;
5952
5953 case Q_DST:
5954 b1 = gen_portatom6(cstate, 2, port);
5955 break;
5956
5957 case Q_AND:
5958 tmp = gen_portatom6(cstate, 0, port);
5959 b1 = gen_portatom6(cstate, 2, port);
5960 gen_and(tmp, b1);
5961 break;
5962
5963 case Q_DEFAULT:
5964 case Q_OR:
5965 tmp = gen_portatom6(cstate, 0, port);
5966 b1 = gen_portatom6(cstate, 2, port);
5967 gen_or(tmp, b1);
5968 break;
5969
5970 default:
5971 abort();
5972 }
5973 gen_and(b0, b1);
5974
5975 return b1;
5976 }
5977
5978 static struct block *
5979 gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5980 {
5981 struct block *b0, *b1, *tmp;
5982
5983 /* link proto ip6 */
5984 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5985
5986 switch (ip_proto) {
5987 case IPPROTO_UDP:
5988 case IPPROTO_TCP:
5989 case IPPROTO_SCTP:
5990 b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
5991 break;
5992
5993 case PROTO_UNDEF:
5994 tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5995 b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5996 gen_or(tmp, b1);
5997 tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5998 gen_or(tmp, b1);
5999 break;
6000
6001 default:
6002 abort();
6003 }
6004 gen_and(b0, b1);
6005 return b1;
6006 }
6007
6008 /* gen_portrange code */
6009 static struct block *
6010 gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
6011 bpf_u_int32 v2)
6012 {
6013 struct block *b1, *b2;
6014
6015 if (v1 > v2) {
6016 /*
6017 * Reverse the order of the ports, so v1 is the lower one.
6018 */
6019 bpf_u_int32 vtemp;
6020
6021 vtemp = v1;
6022 v1 = v2;
6023 v2 = vtemp;
6024 }
6025
6026 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
6027 b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
6028
6029 gen_and(b1, b2);
6030
6031 return b2;
6032 }
6033
6034 static struct block *
6035 gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
6036 bpf_u_int32 proto, int dir)
6037 {
6038 struct block *b0, *b1, *tmp;
6039
6040 /* ip proto 'proto' and not a fragment other than the first fragment */
6041 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
6042 b0 = gen_ipfrag(cstate);
6043 gen_and(tmp, b0);
6044
6045 switch (dir) {
6046 case Q_SRC:
6047 b1 = gen_portrangeatom(cstate, 0, port1, port2);
6048 break;
6049
6050 case Q_DST:
6051 b1 = gen_portrangeatom(cstate, 2, port1, port2);
6052 break;
6053
6054 case Q_AND:
6055 tmp = gen_portrangeatom(cstate, 0, port1, port2);
6056 b1 = gen_portrangeatom(cstate, 2, port1, port2);
6057 gen_and(tmp, b1);
6058 break;
6059
6060 case Q_DEFAULT:
6061 case Q_OR:
6062 tmp = gen_portrangeatom(cstate, 0, port1, port2);
6063 b1 = gen_portrangeatom(cstate, 2, port1, port2);
6064 gen_or(tmp, b1);
6065 break;
6066
6067 case Q_ADDR1:
6068 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
6069 /*NOTREACHED*/
6070
6071 case Q_ADDR2:
6072 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
6073 /*NOTREACHED*/
6074
6075 case Q_ADDR3:
6076 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
6077 /*NOTREACHED*/
6078
6079 case Q_ADDR4:
6080 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
6081 /*NOTREACHED*/
6082
6083 case Q_RA:
6084 bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
6085 /*NOTREACHED*/
6086
6087 case Q_TA:
6088 bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
6089 /*NOTREACHED*/
6090
6091 default:
6092 abort();
6093 /*NOTREACHED*/
6094 }
6095 gen_and(b0, b1);
6096
6097 return b1;
6098 }
6099
6100 static struct block *
6101 gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
6102 int dir)
6103 {
6104 struct block *b0, *b1, *tmp;
6105
6106 /* link proto ip */
6107 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6108
6109 switch (ip_proto) {
6110 case IPPROTO_UDP:
6111 case IPPROTO_TCP:
6112 case IPPROTO_SCTP:
6113 b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
6114 dir);
6115 break;
6116
6117 case PROTO_UNDEF:
6118 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
6119 b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
6120 gen_or(tmp, b1);
6121 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
6122 gen_or(tmp, b1);
6123 break;
6124
6125 default:
6126 abort();
6127 }
6128 gen_and(b0, b1);
6129 return b1;
6130 }
6131
6132 static struct block *
6133 gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
6134 bpf_u_int32 v2)
6135 {
6136 struct block *b1, *b2;
6137
6138 if (v1 > v2) {
6139 /*
6140 * Reverse the order of the ports, so v1 is the lower one.
6141 */
6142 bpf_u_int32 vtemp;
6143
6144 vtemp = v1;
6145 v1 = v2;
6146 v2 = vtemp;
6147 }
6148
6149 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
6150 b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
6151
6152 gen_and(b1, b2);
6153
6154 return b2;
6155 }
6156
6157 static struct block *
6158 gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
6159 bpf_u_int32 proto, int dir)
6160 {
6161 struct block *b0, *b1, *tmp;
6162
6163 /* ip6 proto 'proto' */
6164 /* XXX - catch the first fragment of a fragmented packet? */
6165 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
6166
6167 switch (dir) {
6168 case Q_SRC:
6169 b1 = gen_portrangeatom6(cstate, 0, port1, port2);
6170 break;
6171
6172 case Q_DST:
6173 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
6174 break;
6175
6176 case Q_AND:
6177 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
6178 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
6179 gen_and(tmp, b1);
6180 break;
6181
6182 case Q_DEFAULT:
6183 case Q_OR:
6184 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
6185 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
6186 gen_or(tmp, b1);
6187 break;
6188
6189 default:
6190 abort();
6191 }
6192 gen_and(b0, b1);
6193
6194 return b1;
6195 }
6196
6197 static struct block *
6198 gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
6199 int dir)
6200 {
6201 struct block *b0, *b1, *tmp;
6202
6203 /* link proto ip6 */
6204 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6205
6206 switch (ip_proto) {
6207 case IPPROTO_UDP:
6208 case IPPROTO_TCP:
6209 case IPPROTO_SCTP:
6210 b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
6211 dir);
6212 break;
6213
6214 case PROTO_UNDEF:
6215 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
6216 b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
6217 gen_or(tmp, b1);
6218 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
6219 gen_or(tmp, b1);
6220 break;
6221
6222 default:
6223 abort();
6224 }
6225 gen_and(b0, b1);
6226 return b1;
6227 }
6228
6229 static int
6230 lookup_proto(compiler_state_t *cstate, const char *name, int proto)
6231 {
6232 register int v;
6233
6234 switch (proto) {
6235
6236 case Q_DEFAULT:
6237 case Q_IP:
6238 case Q_IPV6:
6239 v = pcap_nametoproto(name);
6240 if (v == PROTO_UNDEF)
6241 bpf_error(cstate, "unknown ip proto '%s'", name);
6242 break;
6243
6244 case Q_LINK:
6245 /* XXX should look up h/w protocol type based on cstate->linktype */
6246 v = pcap_nametoeproto(name);
6247 if (v == PROTO_UNDEF) {
6248 v = pcap_nametollc(name);
6249 if (v == PROTO_UNDEF)
6250 bpf_error(cstate, "unknown ether proto '%s'", name);
6251 }
6252 break;
6253
6254 case Q_ISO:
6255 if (strcmp(name, "esis") == 0)
6256 v = ISO9542_ESIS;
6257 else if (strcmp(name, "isis") == 0)
6258 v = ISO10589_ISIS;
6259 else if (strcmp(name, "clnp") == 0)
6260 v = ISO8473_CLNP;
6261 else
6262 bpf_error(cstate, "unknown osi proto '%s'", name);
6263 break;
6264
6265 default:
6266 v = PROTO_UNDEF;
6267 break;
6268 }
6269 return v;
6270 }
6271
6272 #if !defined(NO_PROTOCHAIN)
6273 static struct block *
6274 gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
6275 {
6276 struct block *b0, *b;
6277 struct slist *s[100];
6278 int fix2, fix3, fix4, fix5;
6279 int ahcheck, again, end;
6280 int i, max;
6281 int reg2 = alloc_reg(cstate);
6282
6283 memset(s, 0, sizeof(s));
6284 fix3 = fix4 = fix5 = 0;
6285
6286 switch (proto) {
6287 case Q_IP:
6288 case Q_IPV6:
6289 break;
6290 case Q_DEFAULT:
6291 b0 = gen_protochain(cstate, v, Q_IP);
6292 b = gen_protochain(cstate, v, Q_IPV6);
6293 gen_or(b0, b);
6294 return b;
6295 default:
6296 bpf_error(cstate, "bad protocol applied for 'protochain'");
6297 /*NOTREACHED*/
6298 }
6299
6300 /*
6301 * We don't handle variable-length prefixes before the link-layer
6302 * header, or variable-length link-layer headers, here yet.
6303 * We might want to add BPF instructions to do the protochain
6304 * work, to simplify that and, on platforms that have a BPF
6305 * interpreter with the new instructions, let the filtering
6306 * be done in the kernel. (We already require a modified BPF
6307 * engine to do the protochain stuff, to support backward
6308 * branches, and backward branch support is unlikely to appear
6309 * in kernel BPF engines.)
6310 */
6311 if (cstate->off_linkpl.is_variable)
6312 bpf_error(cstate, "'protochain' not supported with variable length headers");
6313
6314 /*
6315 * To quote a comment in optimize.c:
6316 *
6317 * "These data structures are used in a Cocke and Schwartz style
6318 * value numbering scheme. Since the flowgraph is acyclic,
6319 * exit values can be propagated from a node's predecessors
6320 * provided it is uniquely defined."
6321 *
6322 * "Acyclic" means "no backward branches", which means "no
6323 * loops", so we have to turn the optimizer off.
6324 */
6325 cstate->no_optimize = 1;
6326
6327 /*
6328 * s[0] is a dummy entry to protect other BPF insn from damage
6329 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6330 * hard to find interdependency made by jump table fixup.
6331 */
6332 i = 0;
6333 s[i] = new_stmt(cstate, 0); /*dummy*/
6334 i++;
6335
6336 switch (proto) {
6337 case Q_IP:
6338 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6339
6340 /* A = ip->ip_p */
6341 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6342 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
6343 i++;
6344 /* X = ip->ip_hl << 2 */
6345 s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
6346 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6347 i++;
6348 break;
6349
6350 case Q_IPV6:
6351 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6352
6353 /* A = ip6->ip_nxt */
6354 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6355 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
6356 i++;
6357 /* X = sizeof(struct ip6_hdr) */
6358 s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
6359 s[i]->s.k = 40;
6360 i++;
6361 break;
6362
6363 default:
6364 bpf_error(cstate, "unsupported proto to gen_protochain");
6365 /*NOTREACHED*/
6366 }
6367
6368 /* again: if (A == v) goto end; else fall through; */
6369 again = i;
6370 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6371 s[i]->s.k = v;
6372 s[i]->s.jt = NULL; /*later*/
6373 s[i]->s.jf = NULL; /*update in next stmt*/
6374 fix5 = i;
6375 i++;
6376
6377 #ifndef IPPROTO_NONE
6378 #define IPPROTO_NONE 59
6379 #endif
6380 /* if (A == IPPROTO_NONE) goto end */
6381 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6382 s[i]->s.jt = NULL; /*later*/
6383 s[i]->s.jf = NULL; /*update in next stmt*/
6384 s[i]->s.k = IPPROTO_NONE;
6385 s[fix5]->s.jf = s[i];
6386 fix2 = i;
6387 i++;
6388
6389 if (proto == Q_IPV6) {
6390 int v6start, v6end, v6advance, j;
6391
6392 v6start = i;
6393 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6394 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6395 s[i]->s.jt = NULL; /*later*/
6396 s[i]->s.jf = NULL; /*update in next stmt*/
6397 s[i]->s.k = IPPROTO_HOPOPTS;
6398 s[fix2]->s.jf = s[i];
6399 i++;
6400 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6401 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6402 s[i]->s.jt = NULL; /*later*/
6403 s[i]->s.jf = NULL; /*update in next stmt*/
6404 s[i]->s.k = IPPROTO_DSTOPTS;
6405 i++;
6406 /* if (A == IPPROTO_ROUTING) goto v6advance */
6407 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6408 s[i]->s.jt = NULL; /*later*/
6409 s[i]->s.jf = NULL; /*update in next stmt*/
6410 s[i]->s.k = IPPROTO_ROUTING;
6411 i++;
6412 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6413 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6414 s[i]->s.jt = NULL; /*later*/
6415 s[i]->s.jf = NULL; /*later*/
6416 s[i]->s.k = IPPROTO_FRAGMENT;
6417 fix3 = i;
6418 v6end = i;
6419 i++;
6420
6421 /* v6advance: */
6422 v6advance = i;
6423
6424 /*
6425 * in short,
6426 * A = P[X + packet head];
6427 * X = X + (P[X + packet head + 1] + 1) * 8;
6428 */
6429 /* A = P[X + packet head] */
6430 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6431 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6432 i++;
6433 /* MEM[reg2] = A */
6434 s[i] = new_stmt(cstate, BPF_ST);
6435 s[i]->s.k = reg2;
6436 i++;
6437 /* A = P[X + packet head + 1]; */
6438 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6439 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
6440 i++;
6441 /* A += 1 */
6442 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6443 s[i]->s.k = 1;
6444 i++;
6445 /* A *= 8 */
6446 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6447 s[i]->s.k = 8;
6448 i++;
6449 /* A += X */
6450 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
6451 s[i]->s.k = 0;
6452 i++;
6453 /* X = A; */
6454 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6455 i++;
6456 /* A = MEM[reg2] */
6457 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6458 s[i]->s.k = reg2;
6459 i++;
6460
6461 /* goto again; (must use BPF_JA for backward jump) */
6462 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6463 s[i]->s.k = again - i - 1;
6464 s[i - 1]->s.jf = s[i];
6465 i++;
6466
6467 /* fixup */
6468 for (j = v6start; j <= v6end; j++)
6469 s[j]->s.jt = s[v6advance];
6470 } else {
6471 /* nop */
6472 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6473 s[i]->s.k = 0;
6474 s[fix2]->s.jf = s[i];
6475 i++;
6476 }
6477
6478 /* ahcheck: */
6479 ahcheck = i;
6480 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6481 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6482 s[i]->s.jt = NULL; /*later*/
6483 s[i]->s.jf = NULL; /*later*/
6484 s[i]->s.k = IPPROTO_AH;
6485 if (fix3)
6486 s[fix3]->s.jf = s[ahcheck];
6487 fix4 = i;
6488 i++;
6489
6490 /*
6491 * in short,
6492 * A = P[X];
6493 * X = X + (P[X + 1] + 2) * 4;
6494 */
6495 /* A = X */
6496 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6497 i++;
6498 /* A = P[X + packet head]; */
6499 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6500 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6501 i++;
6502 /* MEM[reg2] = A */
6503 s[i] = new_stmt(cstate, BPF_ST);
6504 s[i]->s.k = reg2;
6505 i++;
6506 /* A = X */
6507 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6508 i++;
6509 /* A += 1 */
6510 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6511 s[i]->s.k = 1;
6512 i++;
6513 /* X = A */
6514 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6515 i++;
6516 /* A = P[X + packet head] */
6517 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6518 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6519 i++;
6520 /* A += 2 */
6521 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6522 s[i]->s.k = 2;
6523 i++;
6524 /* A *= 4 */
6525 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6526 s[i]->s.k = 4;
6527 i++;
6528 /* X = A; */
6529 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6530 i++;
6531 /* A = MEM[reg2] */
6532 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6533 s[i]->s.k = reg2;
6534 i++;
6535
6536 /* goto again; (must use BPF_JA for backward jump) */
6537 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6538 s[i]->s.k = again - i - 1;
6539 i++;
6540
6541 /* end: nop */
6542 end = i;
6543 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6544 s[i]->s.k = 0;
6545 s[fix2]->s.jt = s[end];
6546 s[fix4]->s.jf = s[end];
6547 s[fix5]->s.jt = s[end];
6548 i++;
6549
6550 /*
6551 * make slist chain
6552 */
6553 max = i;
6554 for (i = 0; i < max - 1; i++)
6555 s[i]->next = s[i + 1];
6556 s[max - 1]->next = NULL;
6557
6558 /*
6559 * emit final check
6560 */
6561 b = new_block(cstate, JMP(BPF_JEQ));
6562 b->stmts = s[1]; /*remember, s[0] is dummy*/
6563 b->s.k = v;
6564
6565 free_reg(cstate, reg2);
6566
6567 gen_and(b0, b);
6568 return b;
6569 }
6570 #endif /* !defined(NO_PROTOCHAIN) */
6571
6572 static struct block *
6573 gen_check_802_11_data_frame(compiler_state_t *cstate)
6574 {
6575 struct slist *s;
6576 struct block *b0, *b1;
6577
6578 /*
6579 * A data frame has the 0x08 bit (b3) in the frame control field set
6580 * and the 0x04 bit (b2) clear.
6581 */
6582 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6583 b0 = new_block(cstate, JMP(BPF_JSET));
6584 b0->s.k = 0x08;
6585 b0->stmts = s;
6586
6587 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6588 b1 = new_block(cstate, JMP(BPF_JSET));
6589 b1->s.k = 0x04;
6590 b1->stmts = s;
6591 gen_not(b1);
6592
6593 gen_and(b1, b0);
6594
6595 return b0;
6596 }
6597
6598 /*
6599 * Generate code that checks whether the packet is a packet for protocol
6600 * <proto> and whether the type field in that protocol's header has
6601 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6602 * IP packet and checks the protocol number in the IP header against <v>.
6603 *
6604 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6605 * against Q_IP and Q_IPV6.
6606 */
6607 static struct block *
6608 gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
6609 {
6610 struct block *b0, *b1;
6611 struct block *b2;
6612
6613 if (dir != Q_DEFAULT)
6614 bpf_error(cstate, "direction applied to 'proto'");
6615
6616 switch (proto) {
6617 case Q_DEFAULT:
6618 b0 = gen_proto(cstate, v, Q_IP, dir);
6619 b1 = gen_proto(cstate, v, Q_IPV6, dir);
6620 gen_or(b0, b1);
6621 return b1;
6622
6623 case Q_LINK:
6624 return gen_linktype(cstate, v);
6625
6626 case Q_IP:
6627 /*
6628 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6629 * not LLC encapsulation with LLCSAP_IP.
6630 *
6631 * For IEEE 802 networks - which includes 802.5 token ring
6632 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6633 * says that SNAP encapsulation is used, not LLC encapsulation
6634 * with LLCSAP_IP.
6635 *
6636 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6637 * RFC 2225 say that SNAP encapsulation is used, not LLC
6638 * encapsulation with LLCSAP_IP.
6639 *
6640 * So we always check for ETHERTYPE_IP.
6641 */
6642 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6643 b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
6644 gen_and(b0, b1);
6645 return b1;
6646
6647 case Q_ARP:
6648 bpf_error(cstate, "arp does not encapsulate another protocol");
6649 /*NOTREACHED*/
6650
6651 case Q_RARP:
6652 bpf_error(cstate, "rarp does not encapsulate another protocol");
6653 /*NOTREACHED*/
6654
6655 case Q_SCTP:
6656 bpf_error(cstate, "'sctp proto' is bogus");
6657 /*NOTREACHED*/
6658
6659 case Q_TCP:
6660 bpf_error(cstate, "'tcp proto' is bogus");
6661 /*NOTREACHED*/
6662
6663 case Q_UDP:
6664 bpf_error(cstate, "'udp proto' is bogus");
6665 /*NOTREACHED*/
6666
6667 case Q_ICMP:
6668 bpf_error(cstate, "'icmp proto' is bogus");
6669 /*NOTREACHED*/
6670
6671 case Q_IGMP:
6672 bpf_error(cstate, "'igmp proto' is bogus");
6673 /*NOTREACHED*/
6674
6675 case Q_IGRP:
6676 bpf_error(cstate, "'igrp proto' is bogus");
6677 /*NOTREACHED*/
6678
6679 case Q_ATALK:
6680 bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
6681 /*NOTREACHED*/
6682
6683 case Q_DECNET:
6684 bpf_error(cstate, "DECNET encapsulation is not specifiable");
6685 /*NOTREACHED*/
6686
6687 case Q_LAT:
6688 bpf_error(cstate, "LAT does not encapsulate another protocol");
6689 /*NOTREACHED*/
6690
6691 case Q_SCA:
6692 bpf_error(cstate, "SCA does not encapsulate another protocol");
6693 /*NOTREACHED*/
6694
6695 case Q_MOPRC:
6696 bpf_error(cstate, "MOPRC does not encapsulate another protocol");
6697 /*NOTREACHED*/
6698
6699 case Q_MOPDL:
6700 bpf_error(cstate, "MOPDL does not encapsulate another protocol");
6701 /*NOTREACHED*/
6702
6703 case Q_IPV6:
6704 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6705 /*
6706 * Also check for a fragment header before the final
6707 * header.
6708 */
6709 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6710 b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
6711 gen_and(b2, b1);
6712 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
6713 gen_or(b2, b1);
6714 gen_and(b0, b1);
6715 return b1;
6716
6717 case Q_ICMPV6:
6718 bpf_error(cstate, "'icmp6 proto' is bogus");
6719 /*NOTREACHED*/
6720
6721 case Q_AH:
6722 bpf_error(cstate, "'ah proto' is bogus");
6723 /*NOTREACHED*/
6724
6725 case Q_ESP:
6726 bpf_error(cstate, "'esp proto' is bogus");
6727 /*NOTREACHED*/
6728
6729 case Q_PIM:
6730 bpf_error(cstate, "'pim proto' is bogus");
6731 /*NOTREACHED*/
6732
6733 case Q_VRRP:
6734 bpf_error(cstate, "'vrrp proto' is bogus");
6735 /*NOTREACHED*/
6736
6737 case Q_AARP:
6738 bpf_error(cstate, "'aarp proto' is bogus");
6739 /*NOTREACHED*/
6740
6741 case Q_ISO:
6742 switch (cstate->linktype) {
6743
6744 case DLT_FRELAY:
6745 /*
6746 * Frame Relay packets typically have an OSI
6747 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6748 * generates code to check for all the OSI
6749 * NLPIDs, so calling it and then adding a check
6750 * for the particular NLPID for which we're
6751 * looking is bogus, as we can just check for
6752 * the NLPID.
6753 *
6754 * What we check for is the NLPID and a frame
6755 * control field value of UI, i.e. 0x03 followed
6756 * by the NLPID.
6757 *
6758 * XXX - assumes a 2-byte Frame Relay header with
6759 * DLCI and flags. What if the address is longer?
6760 *
6761 * XXX - what about SNAP-encapsulated frames?
6762 */
6763 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6764 /*NOTREACHED*/
6765
6766 case DLT_C_HDLC:
6767 case DLT_HDLC:
6768 /*
6769 * Cisco uses an Ethertype lookalike - for OSI,
6770 * it's 0xfefe.
6771 */
6772 b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6773 /* OSI in C-HDLC is stuffed with a fudge byte */
6774 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
6775 gen_and(b0, b1);
6776 return b1;
6777
6778 default:
6779 b0 = gen_linktype(cstate, LLCSAP_ISONS);
6780 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
6781 gen_and(b0, b1);
6782 return b1;
6783 }
6784
6785 case Q_ESIS:
6786 bpf_error(cstate, "'esis proto' is bogus");
6787 /*NOTREACHED*/
6788
6789 case Q_ISIS:
6790 b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6791 /*
6792 * 4 is the offset of the PDU type relative to the IS-IS
6793 * header.
6794 */
6795 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
6796 gen_and(b0, b1);
6797 return b1;
6798
6799 case Q_CLNP:
6800 bpf_error(cstate, "'clnp proto' is not supported");
6801 /*NOTREACHED*/
6802
6803 case Q_STP:
6804 bpf_error(cstate, "'stp proto' is bogus");
6805 /*NOTREACHED*/
6806
6807 case Q_IPX:
6808 bpf_error(cstate, "'ipx proto' is bogus");
6809 /*NOTREACHED*/
6810
6811 case Q_NETBEUI:
6812 bpf_error(cstate, "'netbeui proto' is bogus");
6813 /*NOTREACHED*/
6814
6815 case Q_ISIS_L1:
6816 bpf_error(cstate, "'l1 proto' is bogus");
6817 /*NOTREACHED*/
6818
6819 case Q_ISIS_L2:
6820 bpf_error(cstate, "'l2 proto' is bogus");
6821 /*NOTREACHED*/
6822
6823 case Q_ISIS_IIH:
6824 bpf_error(cstate, "'iih proto' is bogus");
6825 /*NOTREACHED*/
6826
6827 case Q_ISIS_SNP:
6828 bpf_error(cstate, "'snp proto' is bogus");
6829 /*NOTREACHED*/
6830
6831 case Q_ISIS_CSNP:
6832 bpf_error(cstate, "'csnp proto' is bogus");
6833 /*NOTREACHED*/
6834
6835 case Q_ISIS_PSNP:
6836 bpf_error(cstate, "'psnp proto' is bogus");
6837 /*NOTREACHED*/
6838
6839 case Q_ISIS_LSP:
6840 bpf_error(cstate, "'lsp proto' is bogus");
6841 /*NOTREACHED*/
6842
6843 case Q_RADIO:
6844 bpf_error(cstate, "'radio proto' is bogus");
6845 /*NOTREACHED*/
6846
6847 case Q_CARP:
6848 bpf_error(cstate, "'carp proto' is bogus");
6849 /*NOTREACHED*/
6850
6851 default:
6852 abort();
6853 /*NOTREACHED*/
6854 }
6855 /*NOTREACHED*/
6856 }
6857
6858 /*
6859 * Convert a non-numeric name to a port number.
6860 */
6861 static int
6862 nametoport(compiler_state_t *cstate, const char *name, int ipproto)
6863 {
6864 struct addrinfo hints, *res, *ai;
6865 int error;
6866 struct sockaddr_in *in4;
6867 #ifdef INET6
6868 struct sockaddr_in6 *in6;
6869 #endif
6870 int port = -1;
6871
6872 /*
6873 * We check for both TCP and UDP in case there are
6874 * ambiguous entries.
6875 */
6876 memset(&hints, 0, sizeof(hints));
6877 hints.ai_family = PF_UNSPEC;
6878 hints.ai_socktype = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM;
6879 hints.ai_protocol = ipproto;
6880 error = getaddrinfo(NULL, name, &hints, &res);
6881 if (error != 0) {
6882 switch (error) {
6883
6884 case EAI_NONAME:
6885 case EAI_SERVICE:
6886 /*
6887 * No such port. Just return -1.
6888 */
6889 break;
6890
6891 #ifdef EAI_SYSTEM
6892 case EAI_SYSTEM:
6893 /*
6894 * We don't use strerror() because it's not
6895 * guaranteed to be thread-safe on all platforms
6896 * (probably because it might use a non-thread-local
6897 * buffer into which to format an error message
6898 * if the error code isn't one for which it has
6899 * a canned string; three cheers for C string
6900 * handling).
6901 */
6902 bpf_set_error(cstate, "getaddrinfo(\"%s\" fails with system error: %d",
6903 name, errno);
6904 port = -2; /* a real error */
6905 break;
6906 #endif
6907
6908 default:
6909 /*
6910 * This is a real error, not just "there's
6911 * no such service name".
6912 *
6913 * We don't use gai_strerror() because it's not
6914 * guaranteed to be thread-safe on all platforms
6915 * (probably because it might use a non-thread-local
6916 * buffer into which to format an error message
6917 * if the error code isn't one for which it has
6918 * a canned string; three cheers for C string
6919 * handling).
6920 */
6921 bpf_set_error(cstate, "getaddrinfo(\"%s\") fails with error: %d",
6922 name, error);
6923 port = -2; /* a real error */
6924 break;
6925 }
6926 } else {
6927 /*
6928 * OK, we found it. Did it find anything?
6929 */
6930 for (ai = res; ai != NULL; ai = ai->ai_next) {
6931 /*
6932 * Does it have an address?
6933 */
6934 if (ai->ai_addr != NULL) {
6935 /*
6936 * Yes. Get a port number; we're done.
6937 */
6938 if (ai->ai_addr->sa_family == AF_INET) {
6939 in4 = (struct sockaddr_in *)ai->ai_addr;
6940 port = ntohs(in4->sin_port);
6941 break;
6942 }
6943 #ifdef INET6
6944 if (ai->ai_addr->sa_family == AF_INET6) {
6945 in6 = (struct sockaddr_in6 *)ai->ai_addr;
6946 port = ntohs(in6->sin6_port);
6947 break;
6948 }
6949 #endif
6950 }
6951 }
6952 freeaddrinfo(res);
6953 }
6954 return port;
6955 }
6956
6957 /*
6958 * Convert a string to a port number.
6959 */
6960 static bpf_u_int32
6961 stringtoport(compiler_state_t *cstate, const char *string, size_t string_size,
6962 int *proto)
6963 {
6964 stoulen_ret ret;
6965 char *cpy;
6966 bpf_u_int32 val;
6967 int tcp_port = -1;
6968 int udp_port = -1;
6969
6970 /*
6971 * See if it's a number.
6972 */
6973 ret = stoulen(string, string_size, &val, cstate);
6974 switch (ret) {
6975
6976 case STOULEN_OK:
6977 /* Unknown port type - it's just a number. */
6978 *proto = PROTO_UNDEF;
6979 break;
6980
6981 case STOULEN_NOT_OCTAL_NUMBER:
6982 case STOULEN_NOT_HEX_NUMBER:
6983 case STOULEN_NOT_DECIMAL_NUMBER:
6984 /*
6985 * Not a valid number; try looking it up as a port.
6986 */
6987 cpy = malloc(string_size + 1); /* +1 for terminating '\0' */
6988 memcpy(cpy, string, string_size);
6989 cpy[string_size] = '\0';
6990 tcp_port = nametoport(cstate, cpy, IPPROTO_TCP);
6991 if (tcp_port == -2) {
6992 /*
6993 * We got a hard error; the error string has
6994 * already been set.
6995 */
6996 free(cpy);
6997 longjmp(cstate->top_ctx, 1);
6998 /*NOTREACHED*/
6999 }
7000 udp_port = nametoport(cstate, cpy, IPPROTO_UDP);
7001 if (udp_port == -2) {
7002 /*
7003 * We got a hard error; the error string has
7004 * already been set.
7005 */
7006 free(cpy);
7007 longjmp(cstate->top_ctx, 1);
7008 /*NOTREACHED*/
7009 }
7010
7011 /*
7012 * We need to check /etc/services for ambiguous entries.
7013 * If we find an ambiguous entry, and it has the
7014 * same port number, change the proto to PROTO_UNDEF
7015 * so both TCP and UDP will be checked.
7016 */
7017 if (tcp_port >= 0) {
7018 val = (bpf_u_int32)tcp_port;
7019 *proto = IPPROTO_TCP;
7020 if (udp_port >= 0) {
7021 if (udp_port == tcp_port)
7022 *proto = PROTO_UNDEF;
7023 #ifdef notdef
7024 else
7025 /* Can't handle ambiguous names that refer
7026 to different port numbers. */
7027 warning("ambiguous port %s in /etc/services",
7028 cpy);
7029 #endif
7030 }
7031 free(cpy);
7032 break;
7033 }
7034 if (udp_port >= 0) {
7035 val = (bpf_u_int32)udp_port;
7036 *proto = IPPROTO_UDP;
7037 free(cpy);
7038 break;
7039 }
7040 bpf_set_error(cstate, "'%s' is not a valid port", cpy);
7041 free(cpy);
7042 longjmp(cstate->top_ctx, 1);
7043 /*NOTREACHED*/
7044 #ifdef _AIX
7045 PCAP_UNREACHABLE
7046 #endif /* _AIX */
7047
7048 case STOULEN_ERROR:
7049 /* Error already set. */
7050 longjmp(cstate->top_ctx, 1);
7051 /*NOTREACHED*/
7052 #ifdef _AIX
7053 PCAP_UNREACHABLE
7054 #endif /* _AIX */
7055
7056 default:
7057 /* Should not happen */
7058 bpf_set_error(cstate, "stoulen returned %d - this should not happen", ret);
7059 longjmp(cstate->top_ctx, 1);
7060 /*NOTREACHED*/
7061 }
7062 return (val);
7063 }
7064
7065 /*
7066 * Convert a string in the form PPP-PPP, which correspond to ports, to
7067 * a starting and ending port in a port range.
7068 */
7069 static void
7070 stringtoportrange(compiler_state_t *cstate, const char *string,
7071 bpf_u_int32 *port1, bpf_u_int32 *port2, int *proto)
7072 {
7073 char *hyphen_off;
7074 const char *first, *second;
7075 size_t first_size, second_size;
7076 int save_proto;
7077
7078 if ((hyphen_off = strchr(string, '-')) == NULL)
7079 bpf_error(cstate, "port range '%s' contains no hyphen", string);
7080
7081 /*
7082 * Make sure there are no other hyphens.
7083 *
7084 * XXX - we support named ports, but there are some port names
7085 * in /etc/services that include hyphens, so this would rule
7086 * that out.
7087 */
7088 if (strchr(hyphen_off + 1, '-') != NULL)
7089 bpf_error(cstate, "port range '%s' contains more than one hyphen",
7090 string);
7091
7092 /*
7093 * Get the length of the first port.
7094 */
7095 first = string;
7096 first_size = hyphen_off - string;
7097 if (first_size == 0) {
7098 /* Range of "-port", which we don't support. */
7099 bpf_error(cstate, "port range '%s' has no starting port", string);
7100 }
7101
7102 /*
7103 * Try to convert it to a port.
7104 */
7105 *port1 = stringtoport(cstate, first, first_size, proto);
7106 save_proto = *proto;
7107
7108 /*
7109 * Get the length of the second port.
7110 */
7111 second = hyphen_off + 1;
7112 second_size = strlen(second);
7113 if (second_size == 0) {
7114 /* Range of "port-", which we don't support. */
7115 bpf_error(cstate, "port range '%s' has no ending port", string);
7116 }
7117
7118 /*
7119 * Try to convert it to a port.
7120 */
7121 *port2 = stringtoport(cstate, second, second_size, proto);
7122 if (*proto != save_proto)
7123 *proto = PROTO_UNDEF;
7124 }
7125
7126 struct block *
7127 gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
7128 {
7129 int proto = q.proto;
7130 int dir = q.dir;
7131 int tproto;
7132 u_char *eaddr;
7133 bpf_u_int32 mask, addr;
7134 struct addrinfo *res, *res0;
7135 struct sockaddr_in *sin4;
7136 #ifdef INET6
7137 int tproto6;
7138 struct sockaddr_in6 *sin6;
7139 struct in6_addr mask128;
7140 #endif /*INET6*/
7141 struct block *b, *tmp;
7142 int port, real_proto;
7143 bpf_u_int32 port1, port2;
7144
7145 /*
7146 * Catch errors reported by us and routines below us, and return NULL
7147 * on an error.
7148 */
7149 if (setjmp(cstate->top_ctx))
7150 return (NULL);
7151
7152 switch (q.addr) {
7153
7154 case Q_NET:
7155 addr = pcap_nametonetaddr(name);
7156 if (addr == 0)
7157 bpf_error(cstate, "unknown network '%s'", name);
7158 /* Left justify network addr and calculate its network mask */
7159 mask = 0xffffffff;
7160 while (addr && (addr & 0xff000000) == 0) {
7161 addr <<= 8;
7162 mask <<= 8;
7163 }
7164 return gen_host(cstate, addr, mask, proto, dir, q.addr);
7165
7166 case Q_DEFAULT:
7167 case Q_HOST:
7168 if (proto == Q_LINK) {
7169 switch (cstate->linktype) {
7170
7171 case DLT_EN10MB:
7172 case DLT_NETANALYZER:
7173 case DLT_NETANALYZER_TRANSPARENT:
7174 eaddr = pcap_ether_hostton(name);
7175 if (eaddr == NULL)
7176 bpf_error(cstate,
7177 "unknown ether host '%s'", name);
7178 tmp = gen_prevlinkhdr_check(cstate);
7179 b = gen_ehostop(cstate, eaddr, dir);
7180 if (tmp != NULL)
7181 gen_and(tmp, b);
7182 free(eaddr);
7183 return b;
7184
7185 case DLT_FDDI:
7186 eaddr = pcap_ether_hostton(name);
7187 if (eaddr == NULL)
7188 bpf_error(cstate,
7189 "unknown FDDI host '%s'", name);
7190 b = gen_fhostop(cstate, eaddr, dir);
7191 free(eaddr);
7192 return b;
7193
7194 case DLT_IEEE802:
7195 eaddr = pcap_ether_hostton(name);
7196 if (eaddr == NULL)
7197 bpf_error(cstate,
7198 "unknown token ring host '%s'", name);
7199 b = gen_thostop(cstate, eaddr, dir);
7200 free(eaddr);
7201 return b;
7202
7203 case DLT_IEEE802_11:
7204 case DLT_PRISM_HEADER:
7205 case DLT_IEEE802_11_RADIO_AVS:
7206 case DLT_IEEE802_11_RADIO:
7207 case DLT_PPI:
7208 eaddr = pcap_ether_hostton(name);
7209 if (eaddr == NULL)
7210 bpf_error(cstate,
7211 "unknown 802.11 host '%s'", name);
7212 b = gen_wlanhostop(cstate, eaddr, dir);
7213 free(eaddr);
7214 return b;
7215
7216 case DLT_IP_OVER_FC:
7217 eaddr = pcap_ether_hostton(name);
7218 if (eaddr == NULL)
7219 bpf_error(cstate,
7220 "unknown Fibre Channel host '%s'", name);
7221 b = gen_ipfchostop(cstate, eaddr, dir);
7222 free(eaddr);
7223 return b;
7224 }
7225
7226 bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
7227 } else if (proto == Q_DECNET) {
7228 /*
7229 * A long time ago on Ultrix libpcap supported
7230 * translation of DECnet host names into DECnet
7231 * addresses, but this feature is history now.
7232 */
7233 bpf_error(cstate, "invalid DECnet address '%s'", name);
7234 } else {
7235 #ifdef INET6
7236 memset(&mask128, 0xff, sizeof(mask128));
7237 #endif
7238 res0 = res = pcap_nametoaddrinfo(name);
7239 if (res == NULL)
7240 bpf_error(cstate, "unknown host '%s'", name);
7241 cstate->ai = res;
7242 b = tmp = NULL;
7243 tproto = proto;
7244 #ifdef INET6
7245 tproto6 = proto;
7246 #endif
7247 if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
7248 tproto == Q_DEFAULT) {
7249 tproto = Q_IP;
7250 #ifdef INET6
7251 tproto6 = Q_IPV6;
7252 #endif
7253 }
7254 for (res = res0; res; res = res->ai_next) {
7255 switch (res->ai_family) {
7256 case AF_INET:
7257 #ifdef INET6
7258 if (tproto == Q_IPV6)
7259 continue;
7260 #endif
7261
7262 sin4 = (struct sockaddr_in *)
7263 res->ai_addr;
7264 tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
7265 0xffffffff, tproto, dir, q.addr);
7266 break;
7267 #ifdef INET6
7268 case AF_INET6:
7269 if (tproto6 == Q_IP)
7270 continue;
7271
7272 sin6 = (struct sockaddr_in6 *)
7273 res->ai_addr;
7274 tmp = gen_host6(cstate, &sin6->sin6_addr,
7275 &mask128, tproto6, dir, q.addr);
7276 break;
7277 #endif
7278 default:
7279 continue;
7280 }
7281 if (b)
7282 gen_or(b, tmp);
7283 b = tmp;
7284 }
7285 cstate->ai = NULL;
7286 freeaddrinfo(res0);
7287 if (b == NULL) {
7288 bpf_error(cstate, "unknown host '%s'%s", name,
7289 (proto == Q_DEFAULT)
7290 ? ""
7291 : " for specified address family");
7292 }
7293 return b;
7294 }
7295
7296 case Q_PORT:
7297 if (proto != Q_DEFAULT &&
7298 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
7299 bpf_error(cstate, "illegal qualifier of 'port'");
7300 if (pcap_nametoport(name, &port, &real_proto) == 0)
7301 bpf_error(cstate, "unknown port '%s'", name);
7302 if (proto == Q_UDP) {
7303 if (real_proto == IPPROTO_TCP)
7304 bpf_error(cstate, "port '%s' is tcp", name);
7305 else if (real_proto == IPPROTO_SCTP)
7306 bpf_error(cstate, "port '%s' is sctp", name);
7307 else
7308 /* override PROTO_UNDEF */
7309 real_proto = IPPROTO_UDP;
7310 }
7311 if (proto == Q_TCP) {
7312 if (real_proto == IPPROTO_UDP)
7313 bpf_error(cstate, "port '%s' is udp", name);
7314
7315 else if (real_proto == IPPROTO_SCTP)
7316 bpf_error(cstate, "port '%s' is sctp", name);
7317 else
7318 /* override PROTO_UNDEF */
7319 real_proto = IPPROTO_TCP;
7320 }
7321 if (proto == Q_SCTP) {
7322 if (real_proto == IPPROTO_UDP)
7323 bpf_error(cstate, "port '%s' is udp", name);
7324
7325 else if (real_proto == IPPROTO_TCP)
7326 bpf_error(cstate, "port '%s' is tcp", name);
7327 else
7328 /* override PROTO_UNDEF */
7329 real_proto = IPPROTO_SCTP;
7330 }
7331 if (port < 0)
7332 bpf_error(cstate, "illegal port number %d < 0", port);
7333 if (port > 65535)
7334 bpf_error(cstate, "illegal port number %d > 65535", port);
7335 b = gen_port(cstate, port, real_proto, dir);
7336 gen_or(gen_port6(cstate, port, real_proto, dir), b);
7337 return b;
7338
7339 case Q_PORTRANGE:
7340 if (proto != Q_DEFAULT &&
7341 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
7342 bpf_error(cstate, "illegal qualifier of 'portrange'");
7343 stringtoportrange(cstate, name, &port1, &port2, &real_proto);
7344 if (proto == Q_UDP) {
7345 if (real_proto == IPPROTO_TCP)
7346 bpf_error(cstate, "port in range '%s' is tcp", name);
7347 else if (real_proto == IPPROTO_SCTP)
7348 bpf_error(cstate, "port in range '%s' is sctp", name);
7349 else
7350 /* override PROTO_UNDEF */
7351 real_proto = IPPROTO_UDP;
7352 }
7353 if (proto == Q_TCP) {
7354 if (real_proto == IPPROTO_UDP)
7355 bpf_error(cstate, "port in range '%s' is udp", name);
7356 else if (real_proto == IPPROTO_SCTP)
7357 bpf_error(cstate, "port in range '%s' is sctp", name);
7358 else
7359 /* override PROTO_UNDEF */
7360 real_proto = IPPROTO_TCP;
7361 }
7362 if (proto == Q_SCTP) {
7363 if (real_proto == IPPROTO_UDP)
7364 bpf_error(cstate, "port in range '%s' is udp", name);
7365 else if (real_proto == IPPROTO_TCP)
7366 bpf_error(cstate, "port in range '%s' is tcp", name);
7367 else
7368 /* override PROTO_UNDEF */
7369 real_proto = IPPROTO_SCTP;
7370 }
7371 if (port1 > 65535)
7372 bpf_error(cstate, "illegal port number %d > 65535", port1);
7373 if (port2 > 65535)
7374 bpf_error(cstate, "illegal port number %d > 65535", port2);
7375
7376 b = gen_portrange(cstate, port1, port2, real_proto, dir);
7377 gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
7378 return b;
7379
7380 case Q_GATEWAY:
7381 #ifndef INET6
7382 eaddr = pcap_ether_hostton(name);
7383 if (eaddr == NULL)
7384 bpf_error(cstate, "unknown ether host: %s", name);
7385
7386 res = pcap_nametoaddrinfo(name);
7387 cstate->ai = res;
7388 if (res == NULL)
7389 bpf_error(cstate, "unknown host '%s'", name);
7390 b = gen_gateway(cstate, eaddr, res, proto, dir);
7391 cstate->ai = NULL;
7392 freeaddrinfo(res);
7393 free(eaddr);
7394 if (b == NULL)
7395 bpf_error(cstate, "unknown host '%s'", name);
7396 return b;
7397 #else
7398 bpf_error(cstate, "'gateway' not supported in this configuration");
7399 #endif /*INET6*/
7400
7401 case Q_PROTO:
7402 real_proto = lookup_proto(cstate, name, proto);
7403 if (real_proto >= 0)
7404 return gen_proto(cstate, real_proto, proto, dir);
7405 else
7406 bpf_error(cstate, "unknown protocol: %s", name);
7407
7408 #if !defined(NO_PROTOCHAIN)
7409 case Q_PROTOCHAIN:
7410 real_proto = lookup_proto(cstate, name, proto);
7411 if (real_proto >= 0)
7412 return gen_protochain(cstate, real_proto, proto);
7413 else
7414 bpf_error(cstate, "unknown protocol: %s", name);
7415 #endif /* !defined(NO_PROTOCHAIN) */
7416
7417 case Q_UNDEF:
7418 syntax(cstate);
7419 /*NOTREACHED*/
7420 }
7421 abort();
7422 /*NOTREACHED*/
7423 }
7424
7425 struct block *
7426 gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
7427 bpf_u_int32 masklen, struct qual q)
7428 {
7429 register int nlen, mlen;
7430 bpf_u_int32 n, m;
7431 uint64_t m64;
7432
7433 /*
7434 * Catch errors reported by us and routines below us, and return NULL
7435 * on an error.
7436 */
7437 if (setjmp(cstate->top_ctx))
7438 return (NULL);
7439
7440 nlen = pcapint_atoin(s1, &n);
7441 if (nlen < 0)
7442 bpf_error(cstate, "invalid IPv4 address '%s'", s1);
7443 /* Promote short ipaddr */
7444 n <<= 32 - nlen;
7445
7446 if (s2 != NULL) {
7447 mlen = pcapint_atoin(s2, &m);
7448 if (mlen < 0)
7449 bpf_error(cstate, "invalid IPv4 address '%s'", s2);
7450 /* Promote short ipaddr */
7451 m <<= 32 - mlen;
7452 if ((n & ~m) != 0)
7453 bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
7454 s1, s2);
7455 } else {
7456 /* Convert mask len to mask */
7457 if (masklen > 32)
7458 bpf_error(cstate, "mask length must be <= 32");
7459 m64 = UINT64_C(0xffffffff) << (32 - masklen);
7460 m = (bpf_u_int32)m64;
7461 if ((n & ~m) != 0)
7462 bpf_error(cstate, "non-network bits set in \"%s/%d\"",
7463 s1, masklen);
7464 }
7465
7466 switch (q.addr) {
7467
7468 case Q_NET:
7469 return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
7470
7471 default:
7472 bpf_error(cstate, "Mask syntax for networks only");
7473 /*NOTREACHED*/
7474 }
7475 /*NOTREACHED*/
7476 }
7477
7478 struct block *
7479 gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
7480 {
7481 bpf_u_int32 mask;
7482 int proto;
7483 int dir;
7484 register int vlen;
7485
7486 /*
7487 * Catch errors reported by us and routines below us, and return NULL
7488 * on an error.
7489 */
7490 if (setjmp(cstate->top_ctx))
7491 return (NULL);
7492
7493 proto = q.proto;
7494 dir = q.dir;
7495 if (s == NULL) {
7496 /*
7497 * v contains a 32-bit unsigned parsed from a string of the
7498 * form {N}, which could be decimal, hexadecimal or octal.
7499 * Although it would be possible to use the value as a raw
7500 * 16-bit DECnet address when the value fits into 16 bits, this
7501 * would be a questionable feature: DECnet address wire
7502 * encoding is little-endian, so this would not work as
7503 * intuitively as the same works for [big-endian] IPv4
7504 * addresses (0x01020304 means 1.2.3.4).
7505 */
7506 if (proto == Q_DECNET)
7507 bpf_error(cstate, "invalid DECnet address '%u'", v);
7508 vlen = 32;
7509 } else if (proto == Q_DECNET) {
7510 /*
7511 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7512 * {N}.{N}.{N}.{N}, of which only the first potentially stands
7513 * for a valid DECnet address.
7514 */
7515 vlen = pcapint_atodn(s, &v);
7516 if (vlen == 0)
7517 bpf_error(cstate, "invalid DECnet address '%s'", s);
7518 } else {
7519 /*
7520 * s points to a string of the form {N}.{N}, {N}.{N}.{N} or
7521 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
7522 * IPv4 address.
7523 */
7524 vlen = pcapint_atoin(s, &v);
7525 if (vlen < 0)
7526 bpf_error(cstate, "invalid IPv4 address '%s'", s);
7527 }
7528
7529 switch (q.addr) {
7530
7531 case Q_DEFAULT:
7532 case Q_HOST:
7533 case Q_NET:
7534 if (proto == Q_DECNET)
7535 return gen_host(cstate, v, 0, proto, dir, q.addr);
7536 else if (proto == Q_LINK) {
7537 bpf_error(cstate, "illegal link layer address");
7538 } else {
7539 mask = 0xffffffff;
7540 if (s == NULL && q.addr == Q_NET) {
7541 /* Promote short net number */
7542 while (v && (v & 0xff000000) == 0) {
7543 v <<= 8;
7544 mask <<= 8;
7545 }
7546 } else {
7547 /* Promote short ipaddr */
7548 v <<= 32 - vlen;
7549 mask <<= 32 - vlen ;
7550 }
7551 return gen_host(cstate, v, mask, proto, dir, q.addr);
7552 }
7553
7554 case Q_PORT:
7555 if (proto == Q_UDP)
7556 proto = IPPROTO_UDP;
7557 else if (proto == Q_TCP)
7558 proto = IPPROTO_TCP;
7559 else if (proto == Q_SCTP)
7560 proto = IPPROTO_SCTP;
7561 else if (proto == Q_DEFAULT)
7562 proto = PROTO_UNDEF;
7563 else
7564 bpf_error(cstate, "illegal qualifier of 'port'");
7565
7566 if (v > 65535)
7567 bpf_error(cstate, "illegal port number %u > 65535", v);
7568
7569 {
7570 struct block *b;
7571 b = gen_port(cstate, v, proto, dir);
7572 gen_or(gen_port6(cstate, v, proto, dir), b);
7573 return b;
7574 }
7575
7576 case Q_PORTRANGE:
7577 if (proto == Q_UDP)
7578 proto = IPPROTO_UDP;
7579 else if (proto == Q_TCP)
7580 proto = IPPROTO_TCP;
7581 else if (proto == Q_SCTP)
7582 proto = IPPROTO_SCTP;
7583 else if (proto == Q_DEFAULT)
7584 proto = PROTO_UNDEF;
7585 else
7586 bpf_error(cstate, "illegal qualifier of 'portrange'");
7587
7588 if (v > 65535)
7589 bpf_error(cstate, "illegal port number %u > 65535", v);
7590
7591 {
7592 struct block *b;
7593 b = gen_portrange(cstate, v, v, proto, dir);
7594 gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
7595 return b;
7596 }
7597
7598 case Q_GATEWAY:
7599 bpf_error(cstate, "'gateway' requires a name");
7600 /*NOTREACHED*/
7601
7602 case Q_PROTO:
7603 return gen_proto(cstate, v, proto, dir);
7604
7605 #if !defined(NO_PROTOCHAIN)
7606 case Q_PROTOCHAIN:
7607 return gen_protochain(cstate, v, proto);
7608 #endif
7609
7610 case Q_UNDEF:
7611 syntax(cstate);
7612 /*NOTREACHED*/
7613
7614 default:
7615 abort();
7616 /*NOTREACHED*/
7617 }
7618 /*NOTREACHED*/
7619 }
7620
7621 #ifdef INET6
7622 struct block *
7623 gen_mcode6(compiler_state_t *cstate, const char *s, bpf_u_int32 masklen,
7624 struct qual q)
7625 {
7626 struct addrinfo *res;
7627 struct in6_addr *addr;
7628 struct in6_addr mask;
7629 struct block *b;
7630 bpf_u_int32 a[4], m[4]; /* Same as in gen_hostop6(). */
7631
7632 /*
7633 * Catch errors reported by us and routines below us, and return NULL
7634 * on an error.
7635 */
7636 if (setjmp(cstate->top_ctx))
7637 return (NULL);
7638
7639 res = pcap_nametoaddrinfo(s);
7640 if (!res)
7641 bpf_error(cstate, "invalid ip6 address %s", s);
7642 cstate->ai = res;
7643 if (res->ai_next)
7644 bpf_error(cstate, "%s resolved to multiple address", s);
7645 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
7646
7647 if (masklen > sizeof(mask.s6_addr) * 8)
7648 bpf_error(cstate, "mask length must be <= %zu", sizeof(mask.s6_addr) * 8);
7649 memset(&mask, 0, sizeof(mask));
7650 memset(&mask.s6_addr, 0xff, masklen / 8);
7651 if (masklen % 8) {
7652 mask.s6_addr[masklen / 8] =
7653 (0xff << (8 - masklen % 8)) & 0xff;
7654 }
7655
7656 memcpy(a, addr, sizeof(a));
7657 memcpy(m, &mask, sizeof(m));
7658 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
7659 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
7660 bpf_error(cstate, "non-network bits set in \"%s/%d\"", s, masklen);
7661 }
7662
7663 switch (q.addr) {
7664
7665 case Q_DEFAULT:
7666 case Q_HOST:
7667 if (masklen != 128)
7668 bpf_error(cstate, "Mask syntax for networks only");
7669 /* FALLTHROUGH */
7670
7671 case Q_NET:
7672 b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
7673 cstate->ai = NULL;
7674 freeaddrinfo(res);
7675 return b;
7676
7677 default:
7678 bpf_error(cstate, "invalid qualifier against IPv6 address");
7679 /*NOTREACHED*/
7680 }
7681 }
7682 #endif /*INET6*/
7683
7684 struct block *
7685 gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
7686 {
7687 struct block *b, *tmp;
7688
7689 /*
7690 * Catch errors reported by us and routines below us, and return NULL
7691 * on an error.
7692 */
7693 if (setjmp(cstate->top_ctx))
7694 return (NULL);
7695
7696 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
7697 cstate->e = pcap_ether_aton(s);
7698 if (cstate->e == NULL)
7699 bpf_error(cstate, "malloc");
7700 switch (cstate->linktype) {
7701 case DLT_EN10MB:
7702 case DLT_NETANALYZER:
7703 case DLT_NETANALYZER_TRANSPARENT:
7704 tmp = gen_prevlinkhdr_check(cstate);
7705 b = gen_ehostop(cstate, cstate->e, (int)q.dir);
7706 if (tmp != NULL)
7707 gen_and(tmp, b);
7708 break;
7709 case DLT_FDDI:
7710 b = gen_fhostop(cstate, cstate->e, (int)q.dir);
7711 break;
7712 case DLT_IEEE802:
7713 b = gen_thostop(cstate, cstate->e, (int)q.dir);
7714 break;
7715 case DLT_IEEE802_11:
7716 case DLT_PRISM_HEADER:
7717 case DLT_IEEE802_11_RADIO_AVS:
7718 case DLT_IEEE802_11_RADIO:
7719 case DLT_PPI:
7720 b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
7721 break;
7722 case DLT_IP_OVER_FC:
7723 b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
7724 break;
7725 default:
7726 free(cstate->e);
7727 cstate->e = NULL;
7728 bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7729 /*NOTREACHED*/
7730 }
7731 free(cstate->e);
7732 cstate->e = NULL;
7733 return (b);
7734 }
7735 bpf_error(cstate, "ethernet address used in non-ether expression");
7736 /*NOTREACHED*/
7737 }
7738
7739 void
7740 sappend(struct slist *s0, struct slist *s1)
7741 {
7742 /*
7743 * This is definitely not the best way to do this, but the
7744 * lists will rarely get long.
7745 */
7746 while (s0->next)
7747 s0 = s0->next;
7748 s0->next = s1;
7749 }
7750
7751 static struct slist *
7752 xfer_to_x(compiler_state_t *cstate, struct arth *a)
7753 {
7754 struct slist *s;
7755
7756 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
7757 s->s.k = a->regno;
7758 return s;
7759 }
7760
7761 static struct slist *
7762 xfer_to_a(compiler_state_t *cstate, struct arth *a)
7763 {
7764 struct slist *s;
7765
7766 s = new_stmt(cstate, BPF_LD|BPF_MEM);
7767 s->s.k = a->regno;
7768 return s;
7769 }
7770
7771 /*
7772 * Modify "index" to use the value stored into its register as an
7773 * offset relative to the beginning of the header for the protocol
7774 * "proto", and allocate a register and put an item "size" bytes long
7775 * (1, 2, or 4) at that offset into that register, making it the register
7776 * for "index".
7777 */
7778 static struct arth *
7779 gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
7780 bpf_u_int32 size)
7781 {
7782 int size_code;
7783 struct slist *s, *tmp;
7784 struct block *b;
7785 int regno = alloc_reg(cstate);
7786
7787 free_reg(cstate, inst->regno);
7788 switch (size) {
7789
7790 default:
7791 bpf_error(cstate, "data size must be 1, 2, or 4");
7792 /*NOTREACHED*/
7793
7794 case 1:
7795 size_code = BPF_B;
7796 break;
7797
7798 case 2:
7799 size_code = BPF_H;
7800 break;
7801
7802 case 4:
7803 size_code = BPF_W;
7804 break;
7805 }
7806 switch (proto) {
7807 default:
7808 bpf_error(cstate, "unsupported index operation");
7809
7810 case Q_RADIO:
7811 /*
7812 * The offset is relative to the beginning of the packet
7813 * data, if we have a radio header. (If we don't, this
7814 * is an error.)
7815 */
7816 if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
7817 cstate->linktype != DLT_IEEE802_11_RADIO &&
7818 cstate->linktype != DLT_PRISM_HEADER)
7819 bpf_error(cstate, "radio information not present in capture");
7820
7821 /*
7822 * Load into the X register the offset computed into the
7823 * register specified by "index".
7824 */
7825 s = xfer_to_x(cstate, inst);
7826
7827 /*
7828 * Load the item at that offset.
7829 */
7830 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7831 sappend(s, tmp);
7832 sappend(inst->s, s);
7833 break;
7834
7835 case Q_LINK:
7836 /*
7837 * The offset is relative to the beginning of
7838 * the link-layer header.
7839 *
7840 * XXX - what about ATM LANE? Should the index be
7841 * relative to the beginning of the AAL5 frame, so
7842 * that 0 refers to the beginning of the LE Control
7843 * field, or relative to the beginning of the LAN
7844 * frame, so that 0 refers, for Ethernet LANE, to
7845 * the beginning of the destination address?
7846 */
7847 s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
7848
7849 /*
7850 * If "s" is non-null, it has code to arrange that the
7851 * X register contains the length of the prefix preceding
7852 * the link-layer header. Add to it the offset computed
7853 * into the register specified by "index", and move that
7854 * into the X register. Otherwise, just load into the X
7855 * register the offset computed into the register specified
7856 * by "index".
7857 */
7858 if (s != NULL) {
7859 sappend(s, xfer_to_a(cstate, inst));
7860 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7861 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7862 } else
7863 s = xfer_to_x(cstate, inst);
7864
7865 /*
7866 * Load the item at the sum of the offset we've put in the
7867 * X register and the offset of the start of the link
7868 * layer header (which is 0 if the radio header is
7869 * variable-length; that header length is what we put
7870 * into the X register and then added to the index).
7871 */
7872 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7873 tmp->s.k = cstate->off_linkhdr.constant_part;
7874 sappend(s, tmp);
7875 sappend(inst->s, s);
7876 break;
7877
7878 case Q_IP:
7879 case Q_ARP:
7880 case Q_RARP:
7881 case Q_ATALK:
7882 case Q_DECNET:
7883 case Q_SCA:
7884 case Q_LAT:
7885 case Q_MOPRC:
7886 case Q_MOPDL:
7887 case Q_IPV6:
7888 /*
7889 * The offset is relative to the beginning of
7890 * the network-layer header.
7891 * XXX - are there any cases where we want
7892 * cstate->off_nl_nosnap?
7893 */
7894 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7895
7896 /*
7897 * If "s" is non-null, it has code to arrange that the
7898 * X register contains the variable part of the offset
7899 * of the link-layer payload. Add to it the offset
7900 * computed into the register specified by "index",
7901 * and move that into the X register. Otherwise, just
7902 * load into the X register the offset computed into
7903 * the register specified by "index".
7904 */
7905 if (s != NULL) {
7906 sappend(s, xfer_to_a(cstate, inst));
7907 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7908 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7909 } else
7910 s = xfer_to_x(cstate, inst);
7911
7912 /*
7913 * Load the item at the sum of the offset we've put in the
7914 * X register, the offset of the start of the network
7915 * layer header from the beginning of the link-layer
7916 * payload, and the constant part of the offset of the
7917 * start of the link-layer payload.
7918 */
7919 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7920 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7921 sappend(s, tmp);
7922 sappend(inst->s, s);
7923
7924 /*
7925 * Do the computation only if the packet contains
7926 * the protocol in question.
7927 */
7928 b = gen_proto_abbrev_internal(cstate, proto);
7929 if (inst->b)
7930 gen_and(inst->b, b);
7931 inst->b = b;
7932 break;
7933
7934 case Q_SCTP:
7935 case Q_TCP:
7936 case Q_UDP:
7937 case Q_ICMP:
7938 case Q_IGMP:
7939 case Q_IGRP:
7940 case Q_PIM:
7941 case Q_VRRP:
7942 case Q_CARP:
7943 /*
7944 * The offset is relative to the beginning of
7945 * the transport-layer header.
7946 *
7947 * Load the X register with the length of the IPv4 header
7948 * (plus the offset of the link-layer header, if it's
7949 * a variable-length header), in bytes.
7950 *
7951 * XXX - are there any cases where we want
7952 * cstate->off_nl_nosnap?
7953 * XXX - we should, if we're built with
7954 * IPv6 support, generate code to load either
7955 * IPv4, IPv6, or both, as appropriate.
7956 */
7957 s = gen_loadx_iphdrlen(cstate);
7958
7959 /*
7960 * The X register now contains the sum of the variable
7961 * part of the offset of the link-layer payload and the
7962 * length of the network-layer header.
7963 *
7964 * Load into the A register the offset relative to
7965 * the beginning of the transport layer header,
7966 * add the X register to that, move that to the
7967 * X register, and load with an offset from the
7968 * X register equal to the sum of the constant part of
7969 * the offset of the link-layer payload and the offset,
7970 * relative to the beginning of the link-layer payload,
7971 * of the network-layer header.
7972 */
7973 sappend(s, xfer_to_a(cstate, inst));
7974 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7975 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7976 sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
7977 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7978 sappend(inst->s, s);
7979
7980 /*
7981 * Do the computation only if the packet contains
7982 * the protocol in question - which is true only
7983 * if this is an IP datagram and is the first or
7984 * only fragment of that datagram.
7985 */
7986 gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
7987 if (inst->b)
7988 gen_and(inst->b, b);
7989 gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
7990 inst->b = b;
7991 break;
7992 case Q_ICMPV6:
7993 /*
7994 * Do the computation only if the packet contains
7995 * the protocol in question.
7996 */
7997 b = gen_proto_abbrev_internal(cstate, Q_IPV6);
7998 if (inst->b)
7999 gen_and(inst->b, b);
8000 inst->b = b;
8001
8002 /*
8003 * Check if we have an icmp6 next header
8004 */
8005 b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
8006 if (inst->b)
8007 gen_and(inst->b, b);
8008 inst->b = b;
8009
8010 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
8011 /*
8012 * If "s" is non-null, it has code to arrange that the
8013 * X register contains the variable part of the offset
8014 * of the link-layer payload. Add to it the offset
8015 * computed into the register specified by "index",
8016 * and move that into the X register. Otherwise, just
8017 * load into the X register the offset computed into
8018 * the register specified by "index".
8019 */
8020 if (s != NULL) {
8021 sappend(s, xfer_to_a(cstate, inst));
8022 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
8023 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
8024 } else
8025 s = xfer_to_x(cstate, inst);
8026
8027 /*
8028 * Load the item at the sum of the offset we've put in the
8029 * X register, the offset of the start of the network
8030 * layer header from the beginning of the link-layer
8031 * payload, and the constant part of the offset of the
8032 * start of the link-layer payload.
8033 */
8034 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
8035 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
8036
8037 sappend(s, tmp);
8038 sappend(inst->s, s);
8039
8040 break;
8041 }
8042 inst->regno = regno;
8043 s = new_stmt(cstate, BPF_ST);
8044 s->s.k = regno;
8045 sappend(inst->s, s);
8046
8047 return inst;
8048 }
8049
8050 struct arth *
8051 gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
8052 bpf_u_int32 size)
8053 {
8054 /*
8055 * Catch errors reported by us and routines below us, and return NULL
8056 * on an error.
8057 */
8058 if (setjmp(cstate->top_ctx))
8059 return (NULL);
8060
8061 return gen_load_internal(cstate, proto, inst, size);
8062 }
8063
8064 static struct block *
8065 gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
8066 struct arth *a1, int reversed)
8067 {
8068 struct slist *s0, *s1, *s2;
8069 struct block *b, *tmp;
8070
8071 s0 = xfer_to_x(cstate, a1);
8072 s1 = xfer_to_a(cstate, a0);
8073 if (code == BPF_JEQ) {
8074 s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
8075 b = new_block(cstate, JMP(code));
8076 sappend(s1, s2);
8077 }
8078 else
8079 b = new_block(cstate, BPF_JMP|code|BPF_X);
8080 if (reversed)
8081 gen_not(b);
8082
8083 sappend(s0, s1);
8084 sappend(a1->s, s0);
8085 sappend(a0->s, a1->s);
8086
8087 b->stmts = a0->s;
8088
8089 free_reg(cstate, a0->regno);
8090 free_reg(cstate, a1->regno);
8091
8092 /* 'and' together protocol checks */
8093 if (a0->b) {
8094 if (a1->b) {
8095 gen_and(a0->b, tmp = a1->b);
8096 }
8097 else
8098 tmp = a0->b;
8099 } else
8100 tmp = a1->b;
8101
8102 if (tmp)
8103 gen_and(tmp, b);
8104
8105 return b;
8106 }
8107
8108 struct block *
8109 gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
8110 struct arth *a1, int reversed)
8111 {
8112 /*
8113 * Catch errors reported by us and routines below us, and return NULL
8114 * on an error.
8115 */
8116 if (setjmp(cstate->top_ctx))
8117 return (NULL);
8118
8119 return gen_relation_internal(cstate, code, a0, a1, reversed);
8120 }
8121
8122 struct arth *
8123 gen_loadlen(compiler_state_t *cstate)
8124 {
8125 int regno;
8126 struct arth *a;
8127 struct slist *s;
8128
8129 /*
8130 * Catch errors reported by us and routines below us, and return NULL
8131 * on an error.
8132 */
8133 if (setjmp(cstate->top_ctx))
8134 return (NULL);
8135
8136 regno = alloc_reg(cstate);
8137 a = (struct arth *)newchunk(cstate, sizeof(*a));
8138 s = new_stmt(cstate, BPF_LD|BPF_LEN);
8139 s->next = new_stmt(cstate, BPF_ST);
8140 s->next->s.k = regno;
8141 a->s = s;
8142 a->regno = regno;
8143
8144 return a;
8145 }
8146
8147 static struct arth *
8148 gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
8149 {
8150 struct arth *a;
8151 struct slist *s;
8152 int reg;
8153
8154 a = (struct arth *)newchunk(cstate, sizeof(*a));
8155
8156 reg = alloc_reg(cstate);
8157
8158 s = new_stmt(cstate, BPF_LD|BPF_IMM);
8159 s->s.k = val;
8160 s->next = new_stmt(cstate, BPF_ST);
8161 s->next->s.k = reg;
8162 a->s = s;
8163 a->regno = reg;
8164
8165 return a;
8166 }
8167
8168 struct arth *
8169 gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
8170 {
8171 /*
8172 * Catch errors reported by us and routines below us, and return NULL
8173 * on an error.
8174 */
8175 if (setjmp(cstate->top_ctx))
8176 return (NULL);
8177
8178 return gen_loadi_internal(cstate, val);
8179 }
8180
8181 /*
8182 * The a_arg dance is to avoid annoying whining by compilers that
8183 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8184 * It's not *used* after setjmp returns.
8185 */
8186 struct arth *
8187 gen_neg(compiler_state_t *cstate, struct arth *a_arg)
8188 {
8189 struct arth *a = a_arg;
8190 struct slist *s;
8191
8192 /*
8193 * Catch errors reported by us and routines below us, and return NULL
8194 * on an error.
8195 */
8196 if (setjmp(cstate->top_ctx))
8197 return (NULL);
8198
8199 s = xfer_to_a(cstate, a);
8200 sappend(a->s, s);
8201 s = new_stmt(cstate, BPF_ALU|BPF_NEG);
8202 s->s.k = 0;
8203 sappend(a->s, s);
8204 s = new_stmt(cstate, BPF_ST);
8205 s->s.k = a->regno;
8206 sappend(a->s, s);
8207
8208 return a;
8209 }
8210
8211 /*
8212 * The a0_arg dance is to avoid annoying whining by compilers that
8213 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8214 * It's not *used* after setjmp returns.
8215 */
8216 struct arth *
8217 gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
8218 struct arth *a1)
8219 {
8220 struct arth *a0 = a0_arg;
8221 struct slist *s0, *s1, *s2;
8222
8223 /*
8224 * Catch errors reported by us and routines below us, and return NULL
8225 * on an error.
8226 */
8227 if (setjmp(cstate->top_ctx))
8228 return (NULL);
8229
8230 /*
8231 * Disallow division by, or modulus by, zero; we do this here
8232 * so that it gets done even if the optimizer is disabled.
8233 *
8234 * Also disallow shifts by a value greater than 31; we do this
8235 * here, for the same reason.
8236 */
8237 if (code == BPF_DIV) {
8238 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
8239 bpf_error(cstate, "division by zero");
8240 } else if (code == BPF_MOD) {
8241 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
8242 bpf_error(cstate, "modulus by zero");
8243 } else if (code == BPF_LSH || code == BPF_RSH) {
8244 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)
8245 bpf_error(cstate, "shift by more than 31 bits");
8246 }
8247 s0 = xfer_to_x(cstate, a1);
8248 s1 = xfer_to_a(cstate, a0);
8249 s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
8250
8251 sappend(s1, s2);
8252 sappend(s0, s1);
8253 sappend(a1->s, s0);
8254 sappend(a0->s, a1->s);
8255
8256 free_reg(cstate, a0->regno);
8257 free_reg(cstate, a1->regno);
8258
8259 s0 = new_stmt(cstate, BPF_ST);
8260 a0->regno = s0->s.k = alloc_reg(cstate);
8261 sappend(a0->s, s0);
8262
8263 return a0;
8264 }
8265
8266 /*
8267 * Initialize the table of used registers and the current register.
8268 */
8269 static void
8270 init_regs(compiler_state_t *cstate)
8271 {
8272 cstate->curreg = 0;
8273 memset(cstate->regused, 0, sizeof cstate->regused);
8274 }
8275
8276 /*
8277 * Return the next free register.
8278 */
8279 static int
8280 alloc_reg(compiler_state_t *cstate)
8281 {
8282 int n = BPF_MEMWORDS;
8283
8284 while (--n >= 0) {
8285 if (cstate->regused[cstate->curreg])
8286 cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
8287 else {
8288 cstate->regused[cstate->curreg] = 1;
8289 return cstate->curreg;
8290 }
8291 }
8292 bpf_error(cstate, "too many registers needed to evaluate expression");
8293 /*NOTREACHED*/
8294 }
8295
8296 /*
8297 * Return a register to the table so it can
8298 * be used later.
8299 */
8300 static void
8301 free_reg(compiler_state_t *cstate, int n)
8302 {
8303 cstate->regused[n] = 0;
8304 }
8305
8306 static struct block *
8307 gen_len(compiler_state_t *cstate, int jmp, int n)
8308 {
8309 struct slist *s;
8310 struct block *b;
8311
8312 s = new_stmt(cstate, BPF_LD|BPF_LEN);
8313 b = new_block(cstate, JMP(jmp));
8314 b->stmts = s;
8315 b->s.k = n;
8316
8317 return b;
8318 }
8319
8320 struct block *
8321 gen_greater(compiler_state_t *cstate, int n)
8322 {
8323 /*
8324 * Catch errors reported by us and routines below us, and return NULL
8325 * on an error.
8326 */
8327 if (setjmp(cstate->top_ctx))
8328 return (NULL);
8329
8330 return gen_len(cstate, BPF_JGE, n);
8331 }
8332
8333 /*
8334 * Actually, this is less than or equal.
8335 */
8336 struct block *
8337 gen_less(compiler_state_t *cstate, int n)
8338 {
8339 struct block *b;
8340
8341 /*
8342 * Catch errors reported by us and routines below us, and return NULL
8343 * on an error.
8344 */
8345 if (setjmp(cstate->top_ctx))
8346 return (NULL);
8347
8348 b = gen_len(cstate, BPF_JGT, n);
8349 gen_not(b);
8350
8351 return b;
8352 }
8353
8354 /*
8355 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
8356 * the beginning of the link-layer header.
8357 * XXX - that means you can't test values in the radiotap header, but
8358 * as that header is difficult if not impossible to parse generally
8359 * without a loop, that might not be a severe problem. A new keyword
8360 * "radio" could be added for that, although what you'd really want
8361 * would be a way of testing particular radio header values, which
8362 * would generate code appropriate to the radio header in question.
8363 */
8364 struct block *
8365 gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
8366 {
8367 struct block *b;
8368 struct slist *s;
8369
8370 /*
8371 * Catch errors reported by us and routines below us, and return NULL
8372 * on an error.
8373 */
8374 if (setjmp(cstate->top_ctx))
8375 return (NULL);
8376
8377 switch (op) {
8378 default:
8379 abort();
8380
8381 case '=':
8382 return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
8383
8384 case '<':
8385 b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
8386 return b;
8387
8388 case '>':
8389 b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
8390 return b;
8391
8392 case '|':
8393 s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
8394 break;
8395
8396 case '&':
8397 s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
8398 break;
8399 }
8400 s->s.k = val;
8401 b = new_block(cstate, JMP(BPF_JEQ));
8402 b->stmts = s;
8403 gen_not(b);
8404
8405 return b;
8406 }
8407
8408 struct block *
8409 gen_broadcast(compiler_state_t *cstate, int proto)
8410 {
8411 bpf_u_int32 hostmask;
8412 struct block *b0, *b1, *b2;
8413 static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
8414
8415 /*
8416 * Catch errors reported by us and routines below us, and return NULL
8417 * on an error.
8418 */
8419 if (setjmp(cstate->top_ctx))
8420 return (NULL);
8421
8422 switch (proto) {
8423
8424 case Q_DEFAULT:
8425 case Q_LINK:
8426 switch (cstate->linktype) {
8427 case DLT_ARCNET:
8428 case DLT_ARCNET_LINUX:
8429 // ARCnet broadcast is [8-bit] destination address 0.
8430 return gen_ahostop(cstate, 0, Q_DST);
8431 case DLT_EN10MB:
8432 case DLT_NETANALYZER:
8433 case DLT_NETANALYZER_TRANSPARENT:
8434 b1 = gen_prevlinkhdr_check(cstate);
8435 b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
8436 if (b1 != NULL)
8437 gen_and(b1, b0);
8438 return b0;
8439 case DLT_FDDI:
8440 return gen_fhostop(cstate, ebroadcast, Q_DST);
8441 case DLT_IEEE802:
8442 return gen_thostop(cstate, ebroadcast, Q_DST);
8443 case DLT_IEEE802_11:
8444 case DLT_PRISM_HEADER:
8445 case DLT_IEEE802_11_RADIO_AVS:
8446 case DLT_IEEE802_11_RADIO:
8447 case DLT_PPI:
8448 return gen_wlanhostop(cstate, ebroadcast, Q_DST);
8449 case DLT_IP_OVER_FC:
8450 return gen_ipfchostop(cstate, ebroadcast, Q_DST);
8451 default:
8452 bpf_error(cstate, "not a broadcast link");
8453 }
8454 /*NOTREACHED*/
8455
8456 case Q_IP:
8457 /*
8458 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8459 * as an indication that we don't know the netmask, and fail
8460 * in that case.
8461 */
8462 if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
8463 bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
8464 b0 = gen_linktype(cstate, ETHERTYPE_IP);
8465 hostmask = ~cstate->netmask;
8466 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
8467 b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
8468 ~0 & hostmask, hostmask);
8469 gen_or(b1, b2);
8470 gen_and(b0, b2);
8471 return b2;
8472 }
8473 bpf_error(cstate, "only link-layer/IP broadcast filters supported");
8474 /*NOTREACHED*/
8475 }
8476
8477 /*
8478 * Generate code to test the low-order bit of a MAC address (that's
8479 * the bottom bit of the *first* byte).
8480 */
8481 static struct block *
8482 gen_mac_multicast(compiler_state_t *cstate, int offset)
8483 {
8484 register struct block *b0;
8485 register struct slist *s;
8486
8487 /* link[offset] & 1 != 0 */
8488 s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
8489 b0 = new_block(cstate, JMP(BPF_JSET));
8490 b0->s.k = 1;
8491 b0->stmts = s;
8492 return b0;
8493 }
8494
8495 struct block *
8496 gen_multicast(compiler_state_t *cstate, int proto)
8497 {
8498 register struct block *b0, *b1, *b2;
8499 register struct slist *s;
8500
8501 /*
8502 * Catch errors reported by us and routines below us, and return NULL
8503 * on an error.
8504 */
8505 if (setjmp(cstate->top_ctx))
8506 return (NULL);
8507
8508 switch (proto) {
8509
8510 case Q_DEFAULT:
8511 case Q_LINK:
8512 switch (cstate->linktype) {
8513 case DLT_ARCNET:
8514 case DLT_ARCNET_LINUX:
8515 // ARCnet multicast is the same as broadcast.
8516 return gen_ahostop(cstate, 0, Q_DST);
8517 case DLT_EN10MB:
8518 case DLT_NETANALYZER:
8519 case DLT_NETANALYZER_TRANSPARENT:
8520 b1 = gen_prevlinkhdr_check(cstate);
8521 /* ether[0] & 1 != 0 */
8522 b0 = gen_mac_multicast(cstate, 0);
8523 if (b1 != NULL)
8524 gen_and(b1, b0);
8525 return b0;
8526 case DLT_FDDI:
8527 /*
8528 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8529 *
8530 * XXX - was that referring to bit-order issues?
8531 */
8532 /* fddi[1] & 1 != 0 */
8533 return gen_mac_multicast(cstate, 1);
8534 case DLT_IEEE802:
8535 /* tr[2] & 1 != 0 */
8536 return gen_mac_multicast(cstate, 2);
8537 case DLT_IEEE802_11:
8538 case DLT_PRISM_HEADER:
8539 case DLT_IEEE802_11_RADIO_AVS:
8540 case DLT_IEEE802_11_RADIO:
8541 case DLT_PPI:
8542 /*
8543 * Oh, yuk.
8544 *
8545 * For control frames, there is no DA.
8546 *
8547 * For management frames, DA is at an
8548 * offset of 4 from the beginning of
8549 * the packet.
8550 *
8551 * For data frames, DA is at an offset
8552 * of 4 from the beginning of the packet
8553 * if To DS is clear and at an offset of
8554 * 16 from the beginning of the packet
8555 * if To DS is set.
8556 */
8557
8558 /*
8559 * Generate the tests to be done for data frames.
8560 *
8561 * First, check for To DS set, i.e. "link[1] & 0x01".
8562 */
8563 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8564 b1 = new_block(cstate, JMP(BPF_JSET));
8565 b1->s.k = 0x01; /* To DS */
8566 b1->stmts = s;
8567
8568 /*
8569 * If To DS is set, the DA is at 16.
8570 */
8571 b0 = gen_mac_multicast(cstate, 16);
8572 gen_and(b1, b0);
8573
8574 /*
8575 * Now, check for To DS not set, i.e. check
8576 * "!(link[1] & 0x01)".
8577 */
8578 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8579 b2 = new_block(cstate, JMP(BPF_JSET));
8580 b2->s.k = 0x01; /* To DS */
8581 b2->stmts = s;
8582 gen_not(b2);
8583
8584 /*
8585 * If To DS is not set, the DA is at 4.
8586 */
8587 b1 = gen_mac_multicast(cstate, 4);
8588 gen_and(b2, b1);
8589
8590 /*
8591 * Now OR together the last two checks. That gives
8592 * the complete set of checks for data frames.
8593 */
8594 gen_or(b1, b0);
8595
8596 /*
8597 * Now check for a data frame.
8598 * I.e, check "link[0] & 0x08".
8599 */
8600 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8601 b1 = new_block(cstate, JMP(BPF_JSET));
8602 b1->s.k = 0x08;
8603 b1->stmts = s;
8604
8605 /*
8606 * AND that with the checks done for data frames.
8607 */
8608 gen_and(b1, b0);
8609
8610 /*
8611 * If the high-order bit of the type value is 0, this
8612 * is a management frame.
8613 * I.e, check "!(link[0] & 0x08)".
8614 */
8615 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8616 b2 = new_block(cstate, JMP(BPF_JSET));
8617 b2->s.k = 0x08;
8618 b2->stmts = s;
8619 gen_not(b2);
8620
8621 /*
8622 * For management frames, the DA is at 4.
8623 */
8624 b1 = gen_mac_multicast(cstate, 4);
8625 gen_and(b2, b1);
8626
8627 /*
8628 * OR that with the checks done for data frames.
8629 * That gives the checks done for management and
8630 * data frames.
8631 */
8632 gen_or(b1, b0);
8633
8634 /*
8635 * If the low-order bit of the type value is 1,
8636 * this is either a control frame or a frame
8637 * with a reserved type, and thus not a
8638 * frame with an SA.
8639 *
8640 * I.e., check "!(link[0] & 0x04)".
8641 */
8642 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8643 b1 = new_block(cstate, JMP(BPF_JSET));
8644 b1->s.k = 0x04;
8645 b1->stmts = s;
8646 gen_not(b1);
8647
8648 /*
8649 * AND that with the checks for data and management
8650 * frames.
8651 */
8652 gen_and(b1, b0);
8653 return b0;
8654 case DLT_IP_OVER_FC:
8655 b0 = gen_mac_multicast(cstate, 2);
8656 return b0;
8657 default:
8658 break;
8659 }
8660 /* Link not known to support multicasts */
8661 break;
8662
8663 case Q_IP:
8664 b0 = gen_linktype(cstate, ETHERTYPE_IP);
8665 b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
8666 gen_and(b0, b1);
8667 return b1;
8668
8669 case Q_IPV6:
8670 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
8671 b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
8672 gen_and(b0, b1);
8673 return b1;
8674 }
8675 bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8676 /*NOTREACHED*/
8677 }
8678
8679 #ifdef __linux__
8680 /*
8681 * This is Linux; we require PF_PACKET support. If this is a *live* capture,
8682 * we can look at special meta-data in the filter expression; otherwise we
8683 * can't because it is either a savefile (rfile != NULL) or a pcap_t created
8684 * using pcap_open_dead() (rfile == NULL). Thus check for a flag that
8685 * pcap_activate() conditionally sets.
8686 */
8687 static void
8688 require_basic_bpf_extensions(compiler_state_t *cstate, const char *keyword)
8689 {
8690 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_BASIC_HANDLING)
8691 return;
8692 bpf_error(cstate, "%s not supported on %s (not a live capture)",
8693 keyword,
8694 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8695 }
8696 #endif // __linux__
8697
8698 struct block *
8699 gen_ifindex(compiler_state_t *cstate, int ifindex)
8700 {
8701 register struct block *b0;
8702
8703 /*
8704 * Catch errors reported by us and routines below us, and return NULL
8705 * on an error.
8706 */
8707 if (setjmp(cstate->top_ctx))
8708 return (NULL);
8709
8710 /*
8711 * Only some data link types support ifindex qualifiers.
8712 */
8713 switch (cstate->linktype) {
8714 case DLT_LINUX_SLL2:
8715 /* match packets on this interface */
8716 b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);
8717 break;
8718 default:
8719 #if defined(__linux__)
8720 require_basic_bpf_extensions(cstate, "ifindex");
8721 /* match ifindex */
8722 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,
8723 ifindex);
8724 #else /* defined(__linux__) */
8725 bpf_error(cstate, "ifindex not supported on %s",
8726 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8727 /*NOTREACHED*/
8728 #endif /* defined(__linux__) */
8729 }
8730 return (b0);
8731 }
8732
8733 /*
8734 * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic.
8735 * Outbound traffic is sent by this machine, while inbound traffic is
8736 * sent by a remote machine (and may include packets destined for a
8737 * unicast or multicast link-layer address we are not subscribing to).
8738 * These are the same definitions implemented by pcap_setdirection().
8739 * Capturing only unicast traffic destined for this host is probably
8740 * better accomplished using a higher-layer filter.
8741 */
8742 struct block *
8743 gen_inbound_outbound(compiler_state_t *cstate, const int outbound)
8744 {
8745 register struct block *b0;
8746
8747 /*
8748 * Catch errors reported by us and routines below us, and return NULL
8749 * on an error.
8750 */
8751 if (setjmp(cstate->top_ctx))
8752 return (NULL);
8753
8754 /*
8755 * Only some data link types support inbound/outbound qualifiers.
8756 */
8757 switch (cstate->linktype) {
8758 case DLT_SLIP:
8759 b0 = gen_relation_internal(cstate, BPF_JEQ,
8760 gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
8761 gen_loadi_internal(cstate, 0),
8762 outbound ? SLIPDIR_OUT : SLIPDIR_IN);
8763 break;
8764
8765 case DLT_IPNET:
8766 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H,
8767 outbound ? IPNET_OUTBOUND : IPNET_INBOUND);
8768 break;
8769
8770 case DLT_LINUX_SLL:
8771 /* match outgoing packets */
8772 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
8773 if (! outbound) {
8774 /* to filter on inbound traffic, invert the match */
8775 gen_not(b0);
8776 }
8777 break;
8778
8779 case DLT_LINUX_SLL2:
8780 /* match outgoing packets */
8781 b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
8782 if (! outbound) {
8783 /* to filter on inbound traffic, invert the match */
8784 gen_not(b0);
8785 }
8786 break;
8787
8788 case DLT_PFLOG:
8789 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
8790 outbound ? PF_OUT : PF_IN);
8791 break;
8792
8793 case DLT_PPP_PPPD:
8794 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, outbound ? PPP_PPPD_OUT : PPP_PPPD_IN);
8795 break;
8796
8797 case DLT_JUNIPER_MFR:
8798 case DLT_JUNIPER_MLFR:
8799 case DLT_JUNIPER_MLPPP:
8800 case DLT_JUNIPER_ATM1:
8801 case DLT_JUNIPER_ATM2:
8802 case DLT_JUNIPER_PPPOE:
8803 case DLT_JUNIPER_PPPOE_ATM:
8804 case DLT_JUNIPER_GGSN:
8805 case DLT_JUNIPER_ES:
8806 case DLT_JUNIPER_MONITOR:
8807 case DLT_JUNIPER_SERVICES:
8808 case DLT_JUNIPER_ETHER:
8809 case DLT_JUNIPER_PPP:
8810 case DLT_JUNIPER_FRELAY:
8811 case DLT_JUNIPER_CHDLC:
8812 case DLT_JUNIPER_VP:
8813 case DLT_JUNIPER_ST:
8814 case DLT_JUNIPER_ISM:
8815 case DLT_JUNIPER_VS:
8816 case DLT_JUNIPER_SRX_E2E:
8817 case DLT_JUNIPER_FIBRECHANNEL:
8818 case DLT_JUNIPER_ATM_CEMIC:
8819 /* juniper flags (including direction) are stored
8820 * the byte after the 3-byte magic number */
8821 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, outbound ? 0 : 1, 0x01);
8822 break;
8823
8824 default:
8825 /*
8826 * If we have packet meta-data indicating a direction,
8827 * and that metadata can be checked by BPF code, check
8828 * it. Otherwise, give up, as this link-layer type has
8829 * nothing in the packet data.
8830 *
8831 * Currently, the only platform where a BPF filter can
8832 * check that metadata is Linux with the in-kernel
8833 * BPF interpreter. If other packet capture mechanisms
8834 * and BPF filters also supported this, it would be
8835 * nice. It would be even better if they made that
8836 * metadata available so that we could provide it
8837 * with newer capture APIs, allowing it to be saved
8838 * in pcapng files.
8839 */
8840 #if defined(__linux__)
8841 require_basic_bpf_extensions(cstate, outbound ? "outbound" : "inbound");
8842 /* match outgoing packets */
8843 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
8844 PACKET_OUTGOING);
8845 if (! outbound) {
8846 /* to filter on inbound traffic, invert the match */
8847 gen_not(b0);
8848 }
8849 #else /* defined(__linux__) */
8850 bpf_error(cstate, "inbound/outbound not supported on %s",
8851 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
8852 /*NOTREACHED*/
8853 #endif /* defined(__linux__) */
8854 }
8855 return (b0);
8856 }
8857
8858 /* PF firewall log matched interface */
8859 struct block *
8860 gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
8861 {
8862 struct block *b0;
8863 u_int len, off;
8864
8865 /*
8866 * Catch errors reported by us and routines below us, and return NULL
8867 * on an error.
8868 */
8869 if (setjmp(cstate->top_ctx))
8870 return (NULL);
8871
8872 if (cstate->linktype != DLT_PFLOG) {
8873 bpf_error(cstate, "ifname supported only on PF linktype");
8874 /*NOTREACHED*/
8875 }
8876 len = sizeof(((struct pfloghdr *)0)->ifname);
8877 off = offsetof(struct pfloghdr, ifname);
8878 if (strlen(ifname) >= len) {
8879 bpf_error(cstate, "ifname interface names can only be %d characters",
8880 len-1);
8881 /*NOTREACHED*/
8882 }
8883 b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
8884 (const u_char *)ifname);
8885 return (b0);
8886 }
8887
8888 /* PF firewall log ruleset name */
8889 struct block *
8890 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
8891 {
8892 struct block *b0;
8893
8894 /*
8895 * Catch errors reported by us and routines below us, and return NULL
8896 * on an error.
8897 */
8898 if (setjmp(cstate->top_ctx))
8899 return (NULL);
8900
8901 if (cstate->linktype != DLT_PFLOG) {
8902 bpf_error(cstate, "ruleset supported only on PF linktype");
8903 /*NOTREACHED*/
8904 }
8905
8906 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
8907 bpf_error(cstate, "ruleset names can only be %ld characters",
8908 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
8909 /*NOTREACHED*/
8910 }
8911
8912 b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
8913 (u_int)strlen(ruleset), (const u_char *)ruleset);
8914 return (b0);
8915 }
8916
8917 /* PF firewall log rule number */
8918 struct block *
8919 gen_pf_rnr(compiler_state_t *cstate, int rnr)
8920 {
8921 struct block *b0;
8922
8923 /*
8924 * Catch errors reported by us and routines below us, and return NULL
8925 * on an error.
8926 */
8927 if (setjmp(cstate->top_ctx))
8928 return (NULL);
8929
8930 if (cstate->linktype != DLT_PFLOG) {
8931 bpf_error(cstate, "rnr supported only on PF linktype");
8932 /*NOTREACHED*/
8933 }
8934
8935 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
8936 (bpf_u_int32)rnr);
8937 return (b0);
8938 }
8939
8940 /* PF firewall log sub-rule number */
8941 struct block *
8942 gen_pf_srnr(compiler_state_t *cstate, int srnr)
8943 {
8944 struct block *b0;
8945
8946 /*
8947 * Catch errors reported by us and routines below us, and return NULL
8948 * on an error.
8949 */
8950 if (setjmp(cstate->top_ctx))
8951 return (NULL);
8952
8953 if (cstate->linktype != DLT_PFLOG) {
8954 bpf_error(cstate, "srnr supported only on PF linktype");
8955 /*NOTREACHED*/
8956 }
8957
8958 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
8959 (bpf_u_int32)srnr);
8960 return (b0);
8961 }
8962
8963 /* PF firewall log reason code */
8964 struct block *
8965 gen_pf_reason(compiler_state_t *cstate, int reason)
8966 {
8967 struct block *b0;
8968
8969 /*
8970 * Catch errors reported by us and routines below us, and return NULL
8971 * on an error.
8972 */
8973 if (setjmp(cstate->top_ctx))
8974 return (NULL);
8975
8976 if (cstate->linktype != DLT_PFLOG) {
8977 bpf_error(cstate, "reason supported only on PF linktype");
8978 /*NOTREACHED*/
8979 }
8980
8981 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
8982 (bpf_u_int32)reason);
8983 return (b0);
8984 }
8985
8986 /* PF firewall log action */
8987 struct block *
8988 gen_pf_action(compiler_state_t *cstate, int action)
8989 {
8990 struct block *b0;
8991
8992 /*
8993 * Catch errors reported by us and routines below us, and return NULL
8994 * on an error.
8995 */
8996 if (setjmp(cstate->top_ctx))
8997 return (NULL);
8998
8999 if (cstate->linktype != DLT_PFLOG) {
9000 bpf_error(cstate, "action supported only on PF linktype");
9001 /*NOTREACHED*/
9002 }
9003
9004 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
9005 (bpf_u_int32)action);
9006 return (b0);
9007 }
9008
9009 /* IEEE 802.11 wireless header */
9010 struct block *
9011 gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
9012 {
9013 struct block *b0;
9014
9015 /*
9016 * Catch errors reported by us and routines below us, and return NULL
9017 * on an error.
9018 */
9019 if (setjmp(cstate->top_ctx))
9020 return (NULL);
9021
9022 switch (cstate->linktype) {
9023
9024 case DLT_IEEE802_11:
9025 case DLT_PRISM_HEADER:
9026 case DLT_IEEE802_11_RADIO_AVS:
9027 case DLT_IEEE802_11_RADIO:
9028 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
9029 break;
9030
9031 default:
9032 bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
9033 /*NOTREACHED*/
9034 }
9035
9036 return (b0);
9037 }
9038
9039 struct block *
9040 gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
9041 {
9042 struct block *b0;
9043
9044 /*
9045 * Catch errors reported by us and routines below us, and return NULL
9046 * on an error.
9047 */
9048 if (setjmp(cstate->top_ctx))
9049 return (NULL);
9050
9051 switch (cstate->linktype) {
9052
9053 case DLT_IEEE802_11:
9054 case DLT_PRISM_HEADER:
9055 case DLT_IEEE802_11_RADIO_AVS:
9056 case DLT_IEEE802_11_RADIO:
9057 break;
9058
9059 default:
9060 bpf_error(cstate, "frame direction supported only with 802.11 headers");
9061 /*NOTREACHED*/
9062 }
9063
9064 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
9065 IEEE80211_FC1_DIR_MASK);
9066
9067 return (b0);
9068 }
9069
9070 // Process an ARCnet host address string.
9071 struct block *
9072 gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
9073 {
9074 /*
9075 * Catch errors reported by us and routines below us, and return NULL
9076 * on an error.
9077 */
9078 if (setjmp(cstate->top_ctx))
9079 return (NULL);
9080
9081 switch (cstate->linktype) {
9082
9083 case DLT_ARCNET:
9084 case DLT_ARCNET_LINUX:
9085 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
9086 q.proto == Q_LINK) {
9087 uint8_t addr;
9088 /*
9089 * The lexer currently defines the address format in a
9090 * way that makes this error condition never true.
9091 * Let's check it anyway in case this part of the lexer
9092 * changes in future.
9093 */
9094 if (! pcapint_atoan(s, &addr))
9095 bpf_error(cstate, "invalid ARCnet address '%s'", s);
9096 return gen_ahostop(cstate, addr, (int)q.dir);
9097 } else
9098 bpf_error(cstate, "ARCnet address used in non-arc expression");
9099 /*NOTREACHED*/
9100
9101 default:
9102 bpf_error(cstate, "aid supported only on ARCnet");
9103 /*NOTREACHED*/
9104 }
9105 }
9106
9107 // Compare an ARCnet host address with the given value.
9108 static struct block *
9109 gen_ahostop(compiler_state_t *cstate, const uint8_t eaddr, int dir)
9110 {
9111 register struct block *b0, *b1;
9112
9113 switch (dir) {
9114 /*
9115 * ARCnet is different from Ethernet: the source address comes before
9116 * the destination address, each is one byte long. This holds for all
9117 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10
9118 * in the 1983 edition of the "ARCNET Designer's Handbook" published
9119 * by Datapoint (document number 61610-01).
9120 */
9121 case Q_SRC:
9122 return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, eaddr);
9123
9124 case Q_DST:
9125 return gen_cmp(cstate, OR_LINKHDR, 1, BPF_B, eaddr);
9126
9127 case Q_AND:
9128 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
9129 b1 = gen_ahostop(cstate, eaddr, Q_DST);
9130 gen_and(b0, b1);
9131 return b1;
9132
9133 case Q_DEFAULT:
9134 case Q_OR:
9135 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
9136 b1 = gen_ahostop(cstate, eaddr, Q_DST);
9137 gen_or(b0, b1);
9138 return b1;
9139
9140 case Q_ADDR1:
9141 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
9142 /*NOTREACHED*/
9143
9144 case Q_ADDR2:
9145 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
9146 /*NOTREACHED*/
9147
9148 case Q_ADDR3:
9149 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
9150 /*NOTREACHED*/
9151
9152 case Q_ADDR4:
9153 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
9154 /*NOTREACHED*/
9155
9156 case Q_RA:
9157 bpf_error(cstate, "'ra' is only supported on 802.11");
9158 /*NOTREACHED*/
9159
9160 case Q_TA:
9161 bpf_error(cstate, "'ta' is only supported on 802.11");
9162 /*NOTREACHED*/
9163 }
9164 abort();
9165 /*NOTREACHED*/
9166 }
9167
9168 static struct block *
9169 gen_vlan_tpid_test(compiler_state_t *cstate)
9170 {
9171 struct block *b0, *b1;
9172
9173 /* check for VLAN, including 802.1ad and QinQ */
9174 b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
9175 b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
9176 gen_or(b0,b1);
9177 b0 = b1;
9178 b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
9179 gen_or(b0,b1);
9180
9181 return b1;
9182 }
9183
9184 static struct block *
9185 gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
9186 {
9187 if (vlan_num > 0x0fff) {
9188 bpf_error(cstate, "VLAN tag %u greater than maximum %u",
9189 vlan_num, 0x0fff);
9190 }
9191 return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
9192 }
9193
9194 static struct block *
9195 gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
9196 int has_vlan_tag)
9197 {
9198 struct block *b0, *b1;
9199
9200 b0 = gen_vlan_tpid_test(cstate);
9201
9202 if (has_vlan_tag) {
9203 b1 = gen_vlan_vid_test(cstate, vlan_num);
9204 gen_and(b0, b1);
9205 b0 = b1;
9206 }
9207
9208 /*
9209 * Both payload and link header type follow the VLAN tags so that
9210 * both need to be updated.
9211 */
9212 cstate->off_linkpl.constant_part += 4;
9213 cstate->off_linktype.constant_part += 4;
9214
9215 return b0;
9216 }
9217
9218 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9219 /* add v to variable part of off */
9220 static void
9221 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
9222 bpf_u_int32 v, struct slist *s)
9223 {
9224 struct slist *s2;
9225
9226 if (!off->is_variable)
9227 off->is_variable = 1;
9228 if (off->reg == -1)
9229 off->reg = alloc_reg(cstate);
9230
9231 s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
9232 s2->s.k = off->reg;
9233 sappend(s, s2);
9234 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
9235 s2->s.k = v;
9236 sappend(s, s2);
9237 s2 = new_stmt(cstate, BPF_ST);
9238 s2->s.k = off->reg;
9239 sappend(s, s2);
9240 }
9241
9242 /*
9243 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
9244 * and link type offsets first
9245 */
9246 static void
9247 gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
9248 {
9249 struct slist s;
9250
9251 /* offset determined at run time, shift variable part */
9252 s.next = NULL;
9253 cstate->is_vlan_vloffset = 1;
9254 gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
9255 gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
9256
9257 /* we get a pointer to a chain of or-ed blocks, patch first of them */
9258 sappend(s.next, b_tpid->head->stmts);
9259 b_tpid->head->stmts = s.next;
9260 }
9261
9262 /*
9263 * patch block b_vid (VLAN id test) to load VID value either from packet
9264 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
9265 */
9266 static void
9267 gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
9268 {
9269 struct slist *s, *s2, *sjeq;
9270 unsigned cnt;
9271
9272 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
9273 s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
9274
9275 /* true -> next instructions, false -> beginning of b_vid */
9276 sjeq = new_stmt(cstate, JMP(BPF_JEQ));
9277 sjeq->s.k = 1;
9278 sjeq->s.jf = b_vid->stmts;
9279 sappend(s, sjeq);
9280
9281 s2 = new_stmt(cstate, BPF_LD|BPF_H|BPF_ABS);
9282 s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
9283 sappend(s, s2);
9284 sjeq->s.jt = s2;
9285
9286 /* Jump to the test in b_vid. We need to jump one instruction before
9287 * the end of the b_vid block so that we only skip loading the TCI
9288 * from packet data and not the 'and' instruction extracting VID.
9289 */
9290 cnt = 0;
9291 for (s2 = b_vid->stmts; s2; s2 = s2->next)
9292 cnt++;
9293 s2 = new_stmt(cstate, JMP(BPF_JA));
9294 s2->s.k = cnt - 1;
9295 sappend(s, s2);
9296
9297 /* insert our statements at the beginning of b_vid */
9298 sappend(s, b_vid->stmts);
9299 b_vid->stmts = s;
9300 }
9301
9302 /*
9303 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
9304 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
9305 * tag can be either in metadata or in packet data; therefore if the
9306 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
9307 * header for VLAN tag. As the decision is done at run time, we need
9308 * update variable part of the offsets
9309 */
9310 static struct block *
9311 gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
9312 int has_vlan_tag)
9313 {
9314 struct block *b0, *b_tpid, *b_vid = NULL;
9315 struct slist *s;
9316
9317 /* generate new filter code based on extracting packet
9318 * metadata */
9319 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
9320 s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
9321
9322 b0 = new_block(cstate, JMP(BPF_JEQ));
9323 b0->stmts = s;
9324 b0->s.k = 1;
9325
9326 /*
9327 * This is tricky. We need to insert the statements updating variable
9328 * parts of offsets before the traditional TPID and VID tests so
9329 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
9330 * we do not want this update to affect those checks. That's why we
9331 * generate both test blocks first and insert the statements updating
9332 * variable parts of both offsets after that. This wouldn't work if
9333 * there already were variable length link header when entering this
9334 * function but gen_vlan_bpf_extensions() isn't called in that case.
9335 */
9336 b_tpid = gen_vlan_tpid_test(cstate);
9337 if (has_vlan_tag)
9338 b_vid = gen_vlan_vid_test(cstate, vlan_num);
9339
9340 gen_vlan_patch_tpid_test(cstate, b_tpid);
9341 gen_or(b0, b_tpid);
9342 b0 = b_tpid;
9343
9344 if (has_vlan_tag) {
9345 gen_vlan_patch_vid_test(cstate, b_vid);
9346 gen_and(b0, b_vid);
9347 b0 = b_vid;
9348 }
9349
9350 return b0;
9351 }
9352 #endif
9353
9354 /*
9355 * support IEEE 802.1Q VLAN trunk over ethernet
9356 */
9357 struct block *
9358 gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
9359 {
9360 struct block *b0;
9361
9362 /*
9363 * Catch errors reported by us and routines below us, and return NULL
9364 * on an error.
9365 */
9366 if (setjmp(cstate->top_ctx))
9367 return (NULL);
9368
9369 /* can't check for VLAN-encapsulated packets inside MPLS */
9370 if (cstate->label_stack_depth > 0)
9371 bpf_error(cstate, "no VLAN match after MPLS");
9372
9373 /*
9374 * Check for a VLAN packet, and then change the offsets to point
9375 * to the type and data fields within the VLAN packet. Just
9376 * increment the offsets, so that we can support a hierarchy, e.g.
9377 * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within
9378 * VLAN 100.
9379 *
9380 * XXX - this is a bit of a kludge. If we were to split the
9381 * compiler into a parser that parses an expression and
9382 * generates an expression tree, and a code generator that
9383 * takes an expression tree (which could come from our
9384 * parser or from some other parser) and generates BPF code,
9385 * we could perhaps make the offsets parameters of routines
9386 * and, in the handler for an "AND" node, pass to subnodes
9387 * other than the VLAN node the adjusted offsets.
9388 *
9389 * This would mean that "vlan" would, instead of changing the
9390 * behavior of *all* tests after it, change only the behavior
9391 * of tests ANDed with it. That would change the documented
9392 * semantics of "vlan", which might break some expressions.
9393 * However, it would mean that "(vlan and ip) or ip" would check
9394 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9395 * checking only for VLAN-encapsulated IP, so that could still
9396 * be considered worth doing; it wouldn't break expressions
9397 * that are of the form "vlan and ..." or "vlan N and ...",
9398 * which I suspect are the most common expressions involving
9399 * "vlan". "vlan or ..." doesn't necessarily do what the user
9400 * would really want, now, as all the "or ..." tests would
9401 * be done assuming a VLAN, even though the "or" could be viewed
9402 * as meaning "or, if this isn't a VLAN packet...".
9403 */
9404 switch (cstate->linktype) {
9405
9406 case DLT_EN10MB:
9407 /*
9408 * Newer version of the Linux kernel pass around
9409 * packets in which the VLAN tag has been removed
9410 * from the packet data and put into metadata.
9411 *
9412 * This requires special treatment.
9413 */
9414 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9415 /* Verify that this is the outer part of the packet and
9416 * not encapsulated somehow. */
9417 if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
9418 cstate->off_linkhdr.constant_part ==
9419 cstate->off_outermostlinkhdr.constant_part) {
9420 /*
9421 * Do we need special VLAN handling?
9422 */
9423 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
9424 b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
9425 has_vlan_tag);
9426 else
9427 b0 = gen_vlan_no_bpf_extensions(cstate,
9428 vlan_num, has_vlan_tag);
9429 } else
9430 #endif
9431 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
9432 has_vlan_tag);
9433 break;
9434
9435 case DLT_NETANALYZER:
9436 case DLT_NETANALYZER_TRANSPARENT:
9437 case DLT_IEEE802_11:
9438 case DLT_PRISM_HEADER:
9439 case DLT_IEEE802_11_RADIO_AVS:
9440 case DLT_IEEE802_11_RADIO:
9441 /*
9442 * These are either Ethernet packets with an additional
9443 * metadata header (the NetAnalyzer types), or 802.11
9444 * packets, possibly with an additional metadata header.
9445 *
9446 * For the first of those, the VLAN tag is in the normal
9447 * place, so the special-case handling above isn't
9448 * necessary.
9449 *
9450 * For the second of those, we don't do the special-case
9451 * handling for now.
9452 */
9453 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
9454 break;
9455
9456 default:
9457 bpf_error(cstate, "no VLAN support for %s",
9458 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
9459 /*NOTREACHED*/
9460 }
9461
9462 cstate->vlan_stack_depth++;
9463
9464 return (b0);
9465 }
9466
9467 /*
9468 * support for MPLS
9469 *
9470 * The label_num_arg dance is to avoid annoying whining by compilers that
9471 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9472 * It's not *used* after setjmp returns.
9473 */
9474 static struct block *
9475 gen_mpls_internal(compiler_state_t *cstate, bpf_u_int32 label_num,
9476 int has_label_num)
9477 {
9478 struct block *b0, *b1;
9479
9480 if (cstate->label_stack_depth > 0) {
9481 /* just match the bottom-of-stack bit clear */
9482 b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
9483 } else {
9484 /*
9485 * We're not in an MPLS stack yet, so check the link-layer
9486 * type against MPLS.
9487 */
9488 switch (cstate->linktype) {
9489
9490 case DLT_C_HDLC: /* fall through */
9491 case DLT_HDLC:
9492 case DLT_EN10MB:
9493 case DLT_NETANALYZER:
9494 case DLT_NETANALYZER_TRANSPARENT:
9495 b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
9496 break;
9497
9498 case DLT_PPP:
9499 b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
9500 break;
9501
9502 /* FIXME add other DLT_s ...
9503 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9504 * leave it for now */
9505
9506 default:
9507 bpf_error(cstate, "no MPLS support for %s",
9508 pcap_datalink_val_to_description_or_dlt(cstate->linktype));
9509 /*NOTREACHED*/
9510 }
9511 }
9512
9513 /* If a specific MPLS label is requested, check it */
9514 if (has_label_num) {
9515 if (label_num > 0xFFFFF) {
9516 bpf_error(cstate, "MPLS label %u greater than maximum %u",
9517 label_num, 0xFFFFF);
9518 }
9519 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
9520 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
9521 0xfffff000); /* only compare the first 20 bits */
9522 gen_and(b0, b1);
9523 b0 = b1;
9524 }
9525
9526 /*
9527 * Change the offsets to point to the type and data fields within
9528 * the MPLS packet. Just increment the offsets, so that we
9529 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9530 * capture packets with an outer label of 100000 and an inner
9531 * label of 1024.
9532 *
9533 * Increment the MPLS stack depth as well; this indicates that
9534 * we're checking MPLS-encapsulated headers, to make sure higher
9535 * level code generators don't try to match against IP-related
9536 * protocols such as Q_ARP, Q_RARP etc.
9537 *
9538 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9539 */
9540 cstate->off_nl_nosnap += 4;
9541 cstate->off_nl += 4;
9542 cstate->label_stack_depth++;
9543 return (b0);
9544 }
9545
9546 struct block *
9547 gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num, int has_label_num)
9548 {
9549 /*
9550 * Catch errors reported by us and routines below us, and return NULL
9551 * on an error.
9552 */
9553 if (setjmp(cstate->top_ctx))
9554 return (NULL);
9555
9556 return gen_mpls_internal(cstate, label_num, has_label_num);
9557 }
9558
9559 /*
9560 * Support PPPOE discovery and session.
9561 */
9562 struct block *
9563 gen_pppoed(compiler_state_t *cstate)
9564 {
9565 /*
9566 * Catch errors reported by us and routines below us, and return NULL
9567 * on an error.
9568 */
9569 if (setjmp(cstate->top_ctx))
9570 return (NULL);
9571
9572 /* check for PPPoE discovery */
9573 return gen_linktype(cstate, ETHERTYPE_PPPOED);
9574 }
9575
9576 /*
9577 * RFC 2516 Section 4:
9578 *
9579 * The Ethernet payload for PPPoE is as follows:
9580 *
9581 * 1 2 3
9582 * 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
9583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9584 * | VER | TYPE | CODE | SESSION_ID |
9585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9586 * | LENGTH | payload ~
9587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9588 */
9589 struct block *
9590 gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
9591 {
9592 struct block *b0, *b1;
9593
9594 /*
9595 * Catch errors reported by us and routines below us, and return NULL
9596 * on an error.
9597 */
9598 if (setjmp(cstate->top_ctx))
9599 return (NULL);
9600
9601 /*
9602 * Test against the PPPoE session link-layer type.
9603 */
9604 b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
9605
9606 /* If a specific session is requested, check PPPoE session id */
9607 if (has_sess_num) {
9608 if (sess_num > UINT16_MAX) {
9609 bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
9610 sess_num, UINT16_MAX);
9611 }
9612 b1 = gen_cmp(cstate, OR_LINKPL, 2, BPF_H, sess_num);
9613 gen_and(b0, b1);
9614 b0 = b1;
9615 }
9616
9617 /*
9618 * Change the offsets to point to the type and data fields within
9619 * the PPP packet, and note that this is PPPoE rather than
9620 * raw PPP.
9621 *
9622 * XXX - this is a bit of a kludge. See the comments in
9623 * gen_vlan().
9624 *
9625 * The "network-layer" protocol is PPPoE, which has a 6-byte
9626 * PPPoE header, followed by a PPP packet.
9627 *
9628 * There is no HDLC encapsulation for the PPP packet (it's
9629 * encapsulated in PPPoES instead), so the link-layer type
9630 * starts at the first byte of the PPP packet. For PPPoE,
9631 * that offset is relative to the beginning of the total
9632 * link-layer payload, including any 802.2 LLC header, so
9633 * it's 6 bytes past cstate->off_nl.
9634 */
9635 PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
9636 cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
9637 cstate->off_linkpl.reg);
9638
9639 cstate->off_linktype = cstate->off_linkhdr;
9640 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
9641
9642 cstate->off_nl = 0;
9643 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
9644
9645 return b0;
9646 }
9647
9648 /* Check that this is Geneve and the VNI is correct if
9649 * specified. Parameterized to handle both IPv4 and IPv6. */
9650 static struct block *
9651 gen_geneve_check(compiler_state_t *cstate,
9652 struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
9653 enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
9654 {
9655 struct block *b0, *b1;
9656
9657 b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
9658
9659 /* Check that we are operating on version 0. Otherwise, we
9660 * can't decode the rest of the fields. The version is 2 bits
9661 * in the first byte of the Geneve header. */
9662 b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
9663 gen_and(b0, b1);
9664 b0 = b1;
9665
9666 if (has_vni) {
9667 if (vni > 0xffffff) {
9668 bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
9669 vni, 0xffffff);
9670 }
9671 vni <<= 8; /* VNI is in the upper 3 bytes */
9672 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9673 gen_and(b0, b1);
9674 b0 = b1;
9675 }
9676
9677 return b0;
9678 }
9679
9680 /* The IPv4 and IPv6 Geneve checks need to do two things:
9681 * - Verify that this actually is Geneve with the right VNI.
9682 * - Place the IP header length (plus variable link prefix if
9683 * needed) into register A to be used later to compute
9684 * the inner packet offsets. */
9685 static struct block *
9686 gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9687 {
9688 struct block *b0, *b1;
9689 struct slist *s, *s1;
9690
9691 b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9692
9693 /* Load the IP header length into A. */
9694 s = gen_loadx_iphdrlen(cstate);
9695
9696 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9697 sappend(s, s1);
9698
9699 /* Forcibly append these statements to the true condition
9700 * of the protocol check by creating a new block that is
9701 * always true and ANDing them. */
9702 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9703 b1->stmts = s;
9704 b1->s.k = 0;
9705
9706 gen_and(b0, b1);
9707
9708 return b1;
9709 }
9710
9711 static struct block *
9712 gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9713 {
9714 struct block *b0, *b1;
9715 struct slist *s, *s1;
9716
9717 b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9718
9719 /* Load the IP header length. We need to account for a
9720 * variable length link prefix if there is one. */
9721 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9722 if (s) {
9723 s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9724 s1->s.k = 40;
9725 sappend(s, s1);
9726
9727 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9728 s1->s.k = 0;
9729 sappend(s, s1);
9730 } else {
9731 s = new_stmt(cstate, BPF_LD|BPF_IMM);
9732 s->s.k = 40;
9733 }
9734
9735 /* Forcibly append these statements to the true condition
9736 * of the protocol check by creating a new block that is
9737 * always true and ANDing them. */
9738 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9739 sappend(s, s1);
9740
9741 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9742 b1->stmts = s;
9743 b1->s.k = 0;
9744
9745 gen_and(b0, b1);
9746
9747 return b1;
9748 }
9749
9750 /* We need to store three values based on the Geneve header::
9751 * - The offset of the linktype.
9752 * - The offset of the end of the Geneve header.
9753 * - The offset of the end of the encapsulated MAC header. */
9754 static struct slist *
9755 gen_geneve_offsets(compiler_state_t *cstate)
9756 {
9757 struct slist *s, *s1, *s_proto;
9758
9759 /* First we need to calculate the offset of the Geneve header
9760 * itself. This is composed of the IP header previously calculated
9761 * (include any variable link prefix) and stored in A plus the
9762 * fixed sized headers (fixed link prefix, MAC length, and UDP
9763 * header). */
9764 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9765 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
9766
9767 /* Stash this in X since we'll need it later. */
9768 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9769 sappend(s, s1);
9770
9771 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9772 * store it. */
9773 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9774 s1->s.k = 2;
9775 sappend(s, s1);
9776
9777 cstate->off_linktype.reg = alloc_reg(cstate);
9778 cstate->off_linktype.is_variable = 1;
9779 cstate->off_linktype.constant_part = 0;
9780
9781 s1 = new_stmt(cstate, BPF_ST);
9782 s1->s.k = cstate->off_linktype.reg;
9783 sappend(s, s1);
9784
9785 /* Load the Geneve option length and mask and shift to get the
9786 * number of bytes. It is stored in the first byte of the Geneve
9787 * header. */
9788 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
9789 s1->s.k = 0;
9790 sappend(s, s1);
9791
9792 s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
9793 s1->s.k = 0x3f;
9794 sappend(s, s1);
9795
9796 s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
9797 s1->s.k = 4;
9798 sappend(s, s1);
9799
9800 /* Add in the rest of the Geneve base header. */
9801 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9802 s1->s.k = 8;
9803 sappend(s, s1);
9804
9805 /* Add the Geneve header length to its offset and store. */
9806 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9807 s1->s.k = 0;
9808 sappend(s, s1);
9809
9810 /* Set the encapsulated type as Ethernet. Even though we may
9811 * not actually have Ethernet inside there are two reasons this
9812 * is useful:
9813 * - The linktype field is always in EtherType format regardless
9814 * of whether it is in Geneve or an inner Ethernet frame.
9815 * - The only link layer that we have specific support for is
9816 * Ethernet. We will confirm that the packet actually is
9817 * Ethernet at runtime before executing these checks. */
9818 PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
9819
9820 s1 = new_stmt(cstate, BPF_ST);
9821 s1->s.k = cstate->off_linkhdr.reg;
9822 sappend(s, s1);
9823
9824 /* Calculate whether we have an Ethernet header or just raw IP/
9825 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9826 * and linktype by 14 bytes so that the network header can be found
9827 * seamlessly. Otherwise, keep what we've calculated already. */
9828
9829 /* We have a bare jmp so we can't use the optimizer. */
9830 cstate->no_optimize = 1;
9831
9832 /* Load the EtherType in the Geneve header, 2 bytes in. */
9833 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
9834 s1->s.k = 2;
9835 sappend(s, s1);
9836
9837 /* Load X with the end of the Geneve header. */
9838 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9839 s1->s.k = cstate->off_linkhdr.reg;
9840 sappend(s, s1);
9841
9842 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9843 * end of this check, we should have the total length in X. In
9844 * the non-Ethernet case, it's already there. */
9845 s_proto = new_stmt(cstate, JMP(BPF_JEQ));
9846 s_proto->s.k = ETHERTYPE_TEB;
9847 sappend(s, s_proto);
9848
9849 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9850 sappend(s, s1);
9851 s_proto->s.jt = s1;
9852
9853 /* Since this is Ethernet, use the EtherType of the payload
9854 * directly as the linktype. Overwrite what we already have. */
9855 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9856 s1->s.k = 12;
9857 sappend(s, s1);
9858
9859 s1 = new_stmt(cstate, BPF_ST);
9860 s1->s.k = cstate->off_linktype.reg;
9861 sappend(s, s1);
9862
9863 /* Advance two bytes further to get the end of the Ethernet
9864 * header. */
9865 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9866 s1->s.k = 2;
9867 sappend(s, s1);
9868
9869 /* Move the result to X. */
9870 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9871 sappend(s, s1);
9872
9873 /* Store the final result of our linkpl calculation. */
9874 cstate->off_linkpl.reg = alloc_reg(cstate);
9875 cstate->off_linkpl.is_variable = 1;
9876 cstate->off_linkpl.constant_part = 0;
9877
9878 s1 = new_stmt(cstate, BPF_STX);
9879 s1->s.k = cstate->off_linkpl.reg;
9880 sappend(s, s1);
9881 s_proto->s.jf = s1;
9882
9883 cstate->off_nl = 0;
9884
9885 return s;
9886 }
9887
9888 /* Check to see if this is a Geneve packet. */
9889 struct block *
9890 gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9891 {
9892 struct block *b0, *b1;
9893 struct slist *s;
9894
9895 /*
9896 * Catch errors reported by us and routines below us, and return NULL
9897 * on an error.
9898 */
9899 if (setjmp(cstate->top_ctx))
9900 return (NULL);
9901
9902 b0 = gen_geneve4(cstate, vni, has_vni);
9903 b1 = gen_geneve6(cstate, vni, has_vni);
9904
9905 gen_or(b0, b1);
9906 b0 = b1;
9907
9908 /* Later filters should act on the payload of the Geneve frame,
9909 * update all of the header pointers. Attach this code so that
9910 * it gets executed in the event that the Geneve filter matches. */
9911 s = gen_geneve_offsets(cstate);
9912
9913 b1 = gen_true(cstate);
9914 sappend(s, b1->stmts);
9915 b1->stmts = s;
9916
9917 gen_and(b0, b1);
9918
9919 cstate->is_encap = 1;
9920
9921 return b1;
9922 }
9923
9924 /* Check that this is VXLAN and the VNI is correct if
9925 * specified. Parameterized to handle both IPv4 and IPv6. */
9926 static struct block *
9927 gen_vxlan_check(compiler_state_t *cstate,
9928 struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
9929 enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
9930 {
9931 struct block *b0, *b1;
9932
9933 b0 = gen_portfn(cstate, VXLAN_PORT, IPPROTO_UDP, Q_DST);
9934
9935 /* Check that the VXLAN header has the flag bits set
9936 * correctly. */
9937 b1 = gen_cmp(cstate, offrel, 8, BPF_B, 0x08);
9938 gen_and(b0, b1);
9939 b0 = b1;
9940
9941 if (has_vni) {
9942 if (vni > 0xffffff) {
9943 bpf_error(cstate, "VXLAN VNI %u greater than maximum %u",
9944 vni, 0xffffff);
9945 }
9946 vni <<= 8; /* VNI is in the upper 3 bytes */
9947 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9948 gen_and(b0, b1);
9949 b0 = b1;
9950 }
9951
9952 return b0;
9953 }
9954
9955 /* The IPv4 and IPv6 VXLAN checks need to do two things:
9956 * - Verify that this actually is VXLAN with the right VNI.
9957 * - Place the IP header length (plus variable link prefix if
9958 * needed) into register A to be used later to compute
9959 * the inner packet offsets. */
9960 static struct block *
9961 gen_vxlan4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9962 {
9963 struct block *b0, *b1;
9964 struct slist *s, *s1;
9965
9966 b0 = gen_vxlan_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9967
9968 /* Load the IP header length into A. */
9969 s = gen_loadx_iphdrlen(cstate);
9970
9971 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9972 sappend(s, s1);
9973
9974 /* Forcibly append these statements to the true condition
9975 * of the protocol check by creating a new block that is
9976 * always true and ANDing them. */
9977 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9978 b1->stmts = s;
9979 b1->s.k = 0;
9980
9981 gen_and(b0, b1);
9982
9983 return b1;
9984 }
9985
9986 static struct block *
9987 gen_vxlan6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9988 {
9989 struct block *b0, *b1;
9990 struct slist *s, *s1;
9991
9992 b0 = gen_vxlan_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9993
9994 /* Load the IP header length. We need to account for a
9995 * variable length link prefix if there is one. */
9996 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9997 if (s) {
9998 s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9999 s1->s.k = 40;
10000 sappend(s, s1);
10001
10002 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
10003 s1->s.k = 0;
10004 sappend(s, s1);
10005 } else {
10006 s = new_stmt(cstate, BPF_LD|BPF_IMM);
10007 s->s.k = 40;
10008 }
10009
10010 /* Forcibly append these statements to the true condition
10011 * of the protocol check by creating a new block that is
10012 * always true and ANDing them. */
10013 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
10014 sappend(s, s1);
10015
10016 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
10017 b1->stmts = s;
10018 b1->s.k = 0;
10019
10020 gen_and(b0, b1);
10021
10022 return b1;
10023 }
10024
10025 /* We need to store three values based on the VXLAN header:
10026 * - The offset of the linktype.
10027 * - The offset of the end of the VXLAN header.
10028 * - The offset of the end of the encapsulated MAC header. */
10029 static struct slist *
10030 gen_vxlan_offsets(compiler_state_t *cstate)
10031 {
10032 struct slist *s, *s1;
10033
10034 /* Calculate the offset of the VXLAN header itself. This
10035 * includes the IP header computed previously (including any
10036 * variable link prefix) and stored in A plus the fixed size
10037 * headers (fixed link prefix, MAC length, UDP header). */
10038 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
10039 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
10040
10041 /* Add the VXLAN header length to its offset and store */
10042 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
10043 s1->s.k = 8;
10044 sappend(s, s1);
10045
10046 /* Push the link header. VXLAN packets always contain Ethernet
10047 * frames. */
10048 PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
10049
10050 s1 = new_stmt(cstate, BPF_ST);
10051 s1->s.k = cstate->off_linkhdr.reg;
10052 sappend(s, s1);
10053
10054 /* As the payload is an Ethernet packet, we can use the
10055 * EtherType of the payload directly as the linktype. */
10056 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
10057 s1->s.k = 12;
10058 sappend(s, s1);
10059
10060 cstate->off_linktype.reg = alloc_reg(cstate);
10061 cstate->off_linktype.is_variable = 1;
10062 cstate->off_linktype.constant_part = 0;
10063
10064 s1 = new_stmt(cstate, BPF_ST);
10065 s1->s.k = cstate->off_linktype.reg;
10066 sappend(s, s1);
10067
10068 /* Two bytes further is the end of the Ethernet header and the
10069 * start of the payload. */
10070 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
10071 s1->s.k = 2;
10072 sappend(s, s1);
10073
10074 /* Move the result to X. */
10075 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
10076 sappend(s, s1);
10077
10078 /* Store the final result of our linkpl calculation. */
10079 cstate->off_linkpl.reg = alloc_reg(cstate);
10080 cstate->off_linkpl.is_variable = 1;
10081 cstate->off_linkpl.constant_part = 0;
10082
10083 s1 = new_stmt(cstate, BPF_STX);
10084 s1->s.k = cstate->off_linkpl.reg;
10085 sappend(s, s1);
10086
10087 cstate->off_nl = 0;
10088
10089 return s;
10090 }
10091
10092 /* Check to see if this is a VXLAN packet. */
10093 struct block *
10094 gen_vxlan(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
10095 {
10096 struct block *b0, *b1;
10097 struct slist *s;
10098
10099 /*
10100 * Catch errors reported by us and routines below us, and return NULL
10101 * on an error.
10102 */
10103 if (setjmp(cstate->top_ctx))
10104 return (NULL);
10105
10106 b0 = gen_vxlan4(cstate, vni, has_vni);
10107 b1 = gen_vxlan6(cstate, vni, has_vni);
10108
10109 gen_or(b0, b1);
10110 b0 = b1;
10111
10112 /* Later filters should act on the payload of the VXLAN frame,
10113 * update all of the header pointers. Attach this code so that
10114 * it gets executed in the event that the VXLAN filter matches. */
10115 s = gen_vxlan_offsets(cstate);
10116
10117 b1 = gen_true(cstate);
10118 sappend(s, b1->stmts);
10119 b1->stmts = s;
10120
10121 gen_and(b0, b1);
10122
10123 cstate->is_encap = 1;
10124
10125 return b1;
10126 }
10127
10128 /* Check that the encapsulated frame has a link layer header
10129 * for Ethernet filters. */
10130 static struct block *
10131 gen_encap_ll_check(compiler_state_t *cstate)
10132 {
10133 struct block *b0;
10134 struct slist *s, *s1;
10135
10136 /* The easiest way to see if there is a link layer present
10137 * is to check if the link layer header and payload are not
10138 * the same. */
10139
10140 /* Geneve always generates pure variable offsets so we can
10141 * compare only the registers. */
10142 s = new_stmt(cstate, BPF_LD|BPF_MEM);
10143 s->s.k = cstate->off_linkhdr.reg;
10144
10145 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
10146 s1->s.k = cstate->off_linkpl.reg;
10147 sappend(s, s1);
10148
10149 b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
10150 b0->stmts = s;
10151 b0->s.k = 0;
10152 gen_not(b0);
10153
10154 return b0;
10155 }
10156
10157 static struct block *
10158 gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
10159 bpf_u_int32 jvalue, int jtype, int reverse)
10160 {
10161 struct block *b0;
10162
10163 switch (atmfield) {
10164
10165 case A_VPI:
10166 if (!cstate->is_atm)
10167 bpf_error(cstate, "'vpi' supported only on raw ATM");
10168 if (cstate->off_vpi == OFFSET_NOT_SET)
10169 abort();
10170 if (jvalue > UINT8_MAX)
10171 bpf_error(cstate, "VPI value %u > %u", jvalue, UINT8_MAX);
10172 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
10173 0xffffffffU, jtype, reverse, jvalue);
10174 break;
10175
10176 case A_VCI:
10177 if (!cstate->is_atm)
10178 bpf_error(cstate, "'vci' supported only on raw ATM");
10179 if (cstate->off_vci == OFFSET_NOT_SET)
10180 abort();
10181 if (jvalue > UINT16_MAX)
10182 bpf_error(cstate, "VCI value %u > %u", jvalue, UINT16_MAX);
10183 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
10184 0xffffffffU, jtype, reverse, jvalue);
10185 break;
10186
10187 case A_PROTOTYPE:
10188 if (cstate->off_proto == OFFSET_NOT_SET)
10189 abort(); /* XXX - this isn't on FreeBSD */
10190 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
10191 0x0fU, jtype, reverse, jvalue);
10192 break;
10193
10194 case A_MSGTYPE:
10195 if (cstate->off_payload == OFFSET_NOT_SET)
10196 abort();
10197 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
10198 0xffffffffU, jtype, reverse, jvalue);
10199 break;
10200
10201 case A_CALLREFTYPE:
10202 if (!cstate->is_atm)
10203 bpf_error(cstate, "'callref' supported only on raw ATM");
10204 if (cstate->off_proto == OFFSET_NOT_SET)
10205 abort();
10206 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
10207 0xffffffffU, jtype, reverse, jvalue);
10208 break;
10209
10210 default:
10211 abort();
10212 }
10213 return b0;
10214 }
10215
10216 static struct block *
10217 gen_atmtype_metac(compiler_state_t *cstate)
10218 {
10219 struct block *b0, *b1;
10220
10221 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10222 b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
10223 gen_and(b0, b1);
10224 return b1;
10225 }
10226
10227 static struct block *
10228 gen_atmtype_sc(compiler_state_t *cstate)
10229 {
10230 struct block *b0, *b1;
10231
10232 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10233 b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
10234 gen_and(b0, b1);
10235 return b1;
10236 }
10237
10238 static struct block *
10239 gen_atmtype_llc(compiler_state_t *cstate)
10240 {
10241 struct block *b0;
10242
10243 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
10244 cstate->linktype = cstate->prevlinktype;
10245 return b0;
10246 }
10247
10248 struct block *
10249 gen_atmfield_code(compiler_state_t *cstate, int atmfield,
10250 bpf_u_int32 jvalue, int jtype, int reverse)
10251 {
10252 /*
10253 * Catch errors reported by us and routines below us, and return NULL
10254 * on an error.
10255 */
10256 if (setjmp(cstate->top_ctx))
10257 return (NULL);
10258
10259 return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
10260 reverse);
10261 }
10262
10263 struct block *
10264 gen_atmtype_abbrev(compiler_state_t *cstate, int type)
10265 {
10266 struct block *b0, *b1;
10267
10268 /*
10269 * Catch errors reported by us and routines below us, and return NULL
10270 * on an error.
10271 */
10272 if (setjmp(cstate->top_ctx))
10273 return (NULL);
10274
10275 switch (type) {
10276
10277 case A_METAC:
10278 /* Get all packets in Meta signalling Circuit */
10279 if (!cstate->is_atm)
10280 bpf_error(cstate, "'metac' supported only on raw ATM");
10281 b1 = gen_atmtype_metac(cstate);
10282 break;
10283
10284 case A_BCC:
10285 /* Get all packets in Broadcast Circuit*/
10286 if (!cstate->is_atm)
10287 bpf_error(cstate, "'bcc' supported only on raw ATM");
10288 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10289 b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
10290 gen_and(b0, b1);
10291 break;
10292
10293 case A_OAMF4SC:
10294 /* Get all cells in Segment OAM F4 circuit*/
10295 if (!cstate->is_atm)
10296 bpf_error(cstate, "'oam4sc' supported only on raw ATM");
10297 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10298 b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
10299 gen_and(b0, b1);
10300 break;
10301
10302 case A_OAMF4EC:
10303 /* Get all cells in End-to-End OAM F4 Circuit*/
10304 if (!cstate->is_atm)
10305 bpf_error(cstate, "'oam4ec' supported only on raw ATM");
10306 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10307 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
10308 gen_and(b0, b1);
10309 break;
10310
10311 case A_SC:
10312 /* Get all packets in connection Signalling Circuit */
10313 if (!cstate->is_atm)
10314 bpf_error(cstate, "'sc' supported only on raw ATM");
10315 b1 = gen_atmtype_sc(cstate);
10316 break;
10317
10318 case A_ILMIC:
10319 /* Get all packets in ILMI Circuit */
10320 if (!cstate->is_atm)
10321 bpf_error(cstate, "'ilmic' supported only on raw ATM");
10322 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10323 b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
10324 gen_and(b0, b1);
10325 break;
10326
10327 case A_LANE:
10328 /* Get all LANE packets */
10329 if (!cstate->is_atm)
10330 bpf_error(cstate, "'lane' supported only on raw ATM");
10331 b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
10332
10333 /*
10334 * Arrange that all subsequent tests assume LANE
10335 * rather than LLC-encapsulated packets, and set
10336 * the offsets appropriately for LANE-encapsulated
10337 * Ethernet.
10338 *
10339 * We assume LANE means Ethernet, not Token Ring.
10340 */
10341 PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
10342 cstate->off_payload + 2, /* Ethernet header */
10343 -1);
10344 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
10345 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
10346 cstate->off_nl = 0; /* Ethernet II */
10347 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
10348 break;
10349
10350 case A_LLC:
10351 /* Get all LLC-encapsulated packets */
10352 if (!cstate->is_atm)
10353 bpf_error(cstate, "'llc' supported only on raw ATM");
10354 b1 = gen_atmtype_llc(cstate);
10355 break;
10356
10357 default:
10358 abort();
10359 }
10360 return b1;
10361 }
10362
10363 /*
10364 * Filtering for MTP2 messages based on li value
10365 * FISU, length is null
10366 * LSSU, length is 1 or 2
10367 * MSU, length is 3 or more
10368 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
10369 */
10370 struct block *
10371 gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
10372 {
10373 struct block *b0, *b1;
10374
10375 /*
10376 * Catch errors reported by us and routines below us, and return NULL
10377 * on an error.
10378 */
10379 if (setjmp(cstate->top_ctx))
10380 return (NULL);
10381
10382 switch (type) {
10383
10384 case M_FISU:
10385 if ( (cstate->linktype != DLT_MTP2) &&
10386 (cstate->linktype != DLT_ERF) &&
10387 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10388 bpf_error(cstate, "'fisu' supported only on MTP2");
10389 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10390 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
10391 0x3fU, BPF_JEQ, 0, 0U);
10392 break;
10393
10394 case M_LSSU:
10395 if ( (cstate->linktype != DLT_MTP2) &&
10396 (cstate->linktype != DLT_ERF) &&
10397 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10398 bpf_error(cstate, "'lssu' supported only on MTP2");
10399 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
10400 0x3fU, BPF_JGT, 1, 2U);
10401 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
10402 0x3fU, BPF_JGT, 0, 0U);
10403 gen_and(b1, b0);
10404 break;
10405
10406 case M_MSU:
10407 if ( (cstate->linktype != DLT_MTP2) &&
10408 (cstate->linktype != DLT_ERF) &&
10409 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10410 bpf_error(cstate, "'msu' supported only on MTP2");
10411 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
10412 0x3fU, BPF_JGT, 0, 2U);
10413 break;
10414
10415 case MH_FISU:
10416 if ( (cstate->linktype != DLT_MTP2) &&
10417 (cstate->linktype != DLT_ERF) &&
10418 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10419 bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
10420 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
10421 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
10422 0xff80U, BPF_JEQ, 0, 0U);
10423 break;
10424
10425 case MH_LSSU:
10426 if ( (cstate->linktype != DLT_MTP2) &&
10427 (cstate->linktype != DLT_ERF) &&
10428 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10429 bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
10430 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
10431 0xff80U, BPF_JGT, 1, 0x0100U);
10432 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
10433 0xff80U, BPF_JGT, 0, 0U);
10434 gen_and(b1, b0);
10435 break;
10436
10437 case MH_MSU:
10438 if ( (cstate->linktype != DLT_MTP2) &&
10439 (cstate->linktype != DLT_ERF) &&
10440 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
10441 bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
10442 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
10443 0xff80U, BPF_JGT, 0, 0x0100U);
10444 break;
10445
10446 default:
10447 abort();
10448 }
10449 return b0;
10450 }
10451
10452 /*
10453 * These maximum valid values are all-ones, so they double as the bitmasks
10454 * before any bitwise shifting.
10455 */
10456 #define MTP2_SIO_MAXVAL UINT8_MAX
10457 #define MTP3_PC_MAXVAL 0x3fffU
10458 #define MTP3_SLS_MAXVAL 0xfU
10459
10460 static struct block *
10461 gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field,
10462 bpf_u_int32 jvalue, int jtype, int reverse)
10463 {
10464 struct block *b0;
10465 u_int newoff_sio;
10466 u_int newoff_opc;
10467 u_int newoff_dpc;
10468 u_int newoff_sls;
10469
10470 newoff_sio = cstate->off_sio;
10471 newoff_opc = cstate->off_opc;
10472 newoff_dpc = cstate->off_dpc;
10473 newoff_sls = cstate->off_sls;
10474 switch (mtp3field) {
10475
10476 /*
10477 * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703.
10478 *
10479 * SIO is the simplest field: the size is one byte and the offset is a
10480 * multiple of bytes, so the only detail to get right is the value of
10481 * the [right-to-left] field offset.
10482 */
10483 case MH_SIO:
10484 newoff_sio += 3; /* offset for MTP2_HSL */
10485 /* FALLTHROUGH */
10486
10487 case M_SIO:
10488 if (cstate->off_sio == OFFSET_NOT_SET)
10489 bpf_error(cstate, "'sio' supported only on SS7");
10490 if(jvalue > MTP2_SIO_MAXVAL)
10491 bpf_error(cstate, "sio value %u too big; max value = %u",
10492 jvalue, MTP2_SIO_MAXVAL);
10493 // Here the bitmask means "do not apply a bitmask".
10494 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, UINT32_MAX,
10495 jtype, reverse, jvalue);
10496 break;
10497
10498 /*
10499 * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704.
10500 *
10501 * SLS, OPC and DPC are more complicated: none of these is sized in a
10502 * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet
10503 * diagrams are meant to be read right-to-left. This means in the
10504 * diagrams within individual fields and concatenations thereof
10505 * bitwise shifts and masks can be noted in the common left-to-right
10506 * manner until each final value is ready to be byte-swapped and
10507 * handed to gen_ncmp(). See also gen_dnhostop(), which solves a
10508 * similar problem in a similar way.
10509 *
10510 * Offsets of fields within the packet header always have the
10511 * right-to-left meaning. Note that in DLT_MTP2 and possibly other
10512 * DLTs the offset does not include the F (Flag) field at the
10513 * beginning of each message.
10514 *
10515 * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the
10516 * 32-bit standard routing header has a 4 byte [RTL] offset and could
10517 * be tested entirely using a single BPF_W comparison. In this case
10518 * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC
10519 * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field
10520 * [LTR] bitmask would be (0xF << 28), all of which conveniently
10521 * correlates with the [RTL] packet diagram until the byte-swapping is
10522 * done before use.
10523 *
10524 * The code below uses this approach for OPC, which spans 3 bytes.
10525 * DPC and SLS use shorter loads, SLS also uses a different offset.
10526 */
10527 case MH_OPC:
10528 newoff_opc += 3;
10529
10530 /* FALLTHROUGH */
10531 case M_OPC:
10532 if (cstate->off_opc == OFFSET_NOT_SET)
10533 bpf_error(cstate, "'opc' supported only on SS7");
10534 if (jvalue > MTP3_PC_MAXVAL)
10535 bpf_error(cstate, "opc value %u too big; max value = %u",
10536 jvalue, MTP3_PC_MAXVAL);
10537 b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W,
10538 SWAPLONG(MTP3_PC_MAXVAL << 14), jtype, reverse,
10539 SWAPLONG(jvalue << 14));
10540 break;
10541
10542 case MH_DPC:
10543 newoff_dpc += 3;
10544 /* FALLTHROUGH */
10545
10546 case M_DPC:
10547 if (cstate->off_dpc == OFFSET_NOT_SET)
10548 bpf_error(cstate, "'dpc' supported only on SS7");
10549 if (jvalue > MTP3_PC_MAXVAL)
10550 bpf_error(cstate, "dpc value %u too big; max value = %u",
10551 jvalue, MTP3_PC_MAXVAL);
10552 b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_H,
10553 SWAPSHORT(MTP3_PC_MAXVAL), jtype, reverse,
10554 SWAPSHORT(jvalue));
10555 break;
10556
10557 case MH_SLS:
10558 newoff_sls += 3;
10559 /* FALLTHROUGH */
10560
10561 case M_SLS:
10562 if (cstate->off_sls == OFFSET_NOT_SET)
10563 bpf_error(cstate, "'sls' supported only on SS7");
10564 if (jvalue > MTP3_SLS_MAXVAL)
10565 bpf_error(cstate, "sls value %u too big; max value = %u",
10566 jvalue, MTP3_SLS_MAXVAL);
10567 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B,
10568 MTP3_SLS_MAXVAL << 4, jtype, reverse,
10569 jvalue << 4);
10570 break;
10571
10572 default:
10573 abort();
10574 }
10575 return b0;
10576 }
10577
10578 struct block *
10579 gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
10580 bpf_u_int32 jvalue, int jtype, int reverse)
10581 {
10582 /*
10583 * Catch errors reported by us and routines below us, and return NULL
10584 * on an error.
10585 */
10586 if (setjmp(cstate->top_ctx))
10587 return (NULL);
10588
10589 return gen_mtp3field_code_internal(cstate, mtp3field, jvalue, jtype,
10590 reverse);
10591 }
10592
10593 static struct block *
10594 gen_msg_abbrev(compiler_state_t *cstate, int type)
10595 {
10596 struct block *b1;
10597
10598 /*
10599 * Q.2931 signalling protocol messages for handling virtual circuits
10600 * establishment and teardown
10601 */
10602 switch (type) {
10603
10604 case A_SETUP:
10605 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
10606 break;
10607
10608 case A_CALLPROCEED:
10609 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
10610 break;
10611
10612 case A_CONNECT:
10613 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
10614 break;
10615
10616 case A_CONNECTACK:
10617 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
10618 break;
10619
10620 case A_RELEASE:
10621 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
10622 break;
10623
10624 case A_RELEASE_DONE:
10625 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
10626 break;
10627
10628 default:
10629 abort();
10630 }
10631 return b1;
10632 }
10633
10634 struct block *
10635 gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
10636 {
10637 struct block *b0, *b1;
10638
10639 /*
10640 * Catch errors reported by us and routines below us, and return NULL
10641 * on an error.
10642 */
10643 if (setjmp(cstate->top_ctx))
10644 return (NULL);
10645
10646 switch (type) {
10647
10648 case A_OAM:
10649 if (!cstate->is_atm)
10650 bpf_error(cstate, "'oam' supported only on raw ATM");
10651 /* OAM F4 type */
10652 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
10653 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
10654 gen_or(b0, b1);
10655 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10656 gen_and(b0, b1);
10657 break;
10658
10659 case A_OAMF4:
10660 if (!cstate->is_atm)
10661 bpf_error(cstate, "'oamf4' supported only on raw ATM");
10662 /* OAM F4 type */
10663 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
10664 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
10665 gen_or(b0, b1);
10666 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
10667 gen_and(b0, b1);
10668 break;
10669
10670 case A_CONNECTMSG:
10671 /*
10672 * Get Q.2931 signalling messages for switched
10673 * virtual connection
10674 */
10675 if (!cstate->is_atm)
10676 bpf_error(cstate, "'connectmsg' supported only on raw ATM");
10677 b0 = gen_msg_abbrev(cstate, A_SETUP);
10678 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
10679 gen_or(b0, b1);
10680 b0 = gen_msg_abbrev(cstate, A_CONNECT);
10681 gen_or(b0, b1);
10682 b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
10683 gen_or(b0, b1);
10684 b0 = gen_msg_abbrev(cstate, A_RELEASE);
10685 gen_or(b0, b1);
10686 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
10687 gen_or(b0, b1);
10688 b0 = gen_atmtype_sc(cstate);
10689 gen_and(b0, b1);
10690 break;
10691
10692 case A_METACONNECT:
10693 if (!cstate->is_atm)
10694 bpf_error(cstate, "'metaconnect' supported only on raw ATM");
10695 b0 = gen_msg_abbrev(cstate, A_SETUP);
10696 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
10697 gen_or(b0, b1);
10698 b0 = gen_msg_abbrev(cstate, A_CONNECT);
10699 gen_or(b0, b1);
10700 b0 = gen_msg_abbrev(cstate, A_RELEASE);
10701 gen_or(b0, b1);
10702 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
10703 gen_or(b0, b1);
10704 b0 = gen_atmtype_metac(cstate);
10705 gen_and(b0, b1);
10706 break;
10707
10708 default:
10709 abort();
10710 }
10711 return b1;
10712 }