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