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