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