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