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