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