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