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