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