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