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