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