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