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