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