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