]> The Tcpdump Group git mirrors - libpcap/blob - gencode.c
Add DLT_/LINKTYPE_ values for IEEE802_15_4_TAP
[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 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:
3596 bpf_error(cstate, "IPMB link-layer type filtering not implemented");
3597
3598 case DLT_AX25_KISS:
3599 bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
3600
3601 case DLT_NFLOG:
3602 /* Using the fixed-size NFLOG header it is possible to tell only
3603 * the address family of the packet, other meaningful data is
3604 * either missing or behind TLVs.
3605 */
3606 bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
3607
3608 default:
3609 /*
3610 * Does this link-layer header type have a field
3611 * indicating the type of the next protocol? If
3612 * so, off_linktype.constant_part will be the offset of that
3613 * field in the packet; if not, it will be OFFSET_NOT_SET.
3614 */
3615 if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
3616 /*
3617 * Yes; assume it's an Ethernet type. (If
3618 * it's not, it needs to be handled specially
3619 * above.)
3620 */
3621 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
3622 /*NOTREACHED */
3623 } else {
3624 /*
3625 * No; report an error.
3626 */
3627 description = pcap_datalink_val_to_description(cstate->linktype);
3628 if (description != NULL) {
3629 bpf_error(cstate, "%s link-layer type filtering not implemented",
3630 description);
3631 } else {
3632 bpf_error(cstate, "DLT %u link-layer type filtering not implemented",
3633 cstate->linktype);
3634 }
3635 /*NOTREACHED */
3636 }
3637 }
3638 }
3639
3640 /*
3641 * Check for an LLC SNAP packet with a given organization code and
3642 * protocol type; we check the entire contents of the 802.2 LLC and
3643 * snap headers, checking for DSAP and SSAP of SNAP and a control
3644 * field of 0x03 in the LLC header, and for the specified organization
3645 * code and protocol type in the SNAP header.
3646 */
3647 static struct block *
3648 gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
3649 {
3650 u_char snapblock[8];
3651
3652 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3653 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3654 snapblock[2] = 0x03; /* control = UI */
3655 snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */
3656 snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */
3657 snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */
3658 snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */
3659 snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */
3660 return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
3661 }
3662
3663 /*
3664 * Generate code to match frames with an LLC header.
3665 */
3666 static struct block *
3667 gen_llc_internal(compiler_state_t *cstate)
3668 {
3669 struct block *b0, *b1;
3670
3671 switch (cstate->linktype) {
3672
3673 case DLT_EN10MB:
3674 /*
3675 * We check for an Ethernet type field less than
3676 * 1500, which means it's an 802.3 length field.
3677 */
3678 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
3679 gen_not(b0);
3680
3681 /*
3682 * Now check for the purported DSAP and SSAP not being
3683 * 0xFF, to rule out NetWare-over-802.3.
3684 */
3685 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
3686 gen_not(b1);
3687 gen_and(b0, b1);
3688 return b1;
3689
3690 case DLT_SUNATM:
3691 /*
3692 * We check for LLC traffic.
3693 */
3694 b0 = gen_atmtype_llc(cstate);
3695 return b0;
3696
3697 case DLT_IEEE802: /* Token Ring */
3698 /*
3699 * XXX - check for LLC frames.
3700 */
3701 return gen_true(cstate);
3702
3703 case DLT_FDDI:
3704 /*
3705 * XXX - check for LLC frames.
3706 */
3707 return gen_true(cstate);
3708
3709 case DLT_ATM_RFC1483:
3710 /*
3711 * For LLC encapsulation, these are defined to have an
3712 * 802.2 LLC header.
3713 *
3714 * For VC encapsulation, they don't, but there's no
3715 * way to check for that; the protocol used on the VC
3716 * is negotiated out of band.
3717 */
3718 return gen_true(cstate);
3719
3720 case DLT_IEEE802_11:
3721 case DLT_PRISM_HEADER:
3722 case DLT_IEEE802_11_RADIO:
3723 case DLT_IEEE802_11_RADIO_AVS:
3724 case DLT_PPI:
3725 /*
3726 * Check that we have a data frame.
3727 */
3728 b0 = gen_check_802_11_data_frame(cstate);
3729 return b0;
3730
3731 default:
3732 bpf_error(cstate, "'llc' not supported for linktype %d", cstate->linktype);
3733 /*NOTREACHED*/
3734 }
3735 }
3736
3737 struct block *
3738 gen_llc(compiler_state_t *cstate)
3739 {
3740 /*
3741 * Catch errors reported by us and routines below us, and return NULL
3742 * on an error.
3743 */
3744 if (setjmp(cstate->top_ctx))
3745 return (NULL);
3746
3747 return gen_llc_internal(cstate);
3748 }
3749
3750 struct block *
3751 gen_llc_i(compiler_state_t *cstate)
3752 {
3753 struct block *b0, *b1;
3754 struct slist *s;
3755
3756 /*
3757 * Catch errors reported by us and routines below us, and return NULL
3758 * on an error.
3759 */
3760 if (setjmp(cstate->top_ctx))
3761 return (NULL);
3762
3763 /*
3764 * Check whether this is an LLC frame.
3765 */
3766 b0 = gen_llc_internal(cstate);
3767
3768 /*
3769 * Load the control byte and test the low-order bit; it must
3770 * be clear for I frames.
3771 */
3772 s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
3773 b1 = new_block(cstate, JMP(BPF_JSET));
3774 b1->s.k = 0x01;
3775 b1->stmts = s;
3776 gen_not(b1);
3777 gen_and(b0, b1);
3778 return b1;
3779 }
3780
3781 struct block *
3782 gen_llc_s(compiler_state_t *cstate)
3783 {
3784 struct block *b0, *b1;
3785
3786 /*
3787 * Catch errors reported by us and routines below us, and return NULL
3788 * on an error.
3789 */
3790 if (setjmp(cstate->top_ctx))
3791 return (NULL);
3792
3793 /*
3794 * Check whether this is an LLC frame.
3795 */
3796 b0 = gen_llc_internal(cstate);
3797
3798 /*
3799 * Now compare the low-order 2 bit of the control byte against
3800 * the appropriate value for S frames.
3801 */
3802 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
3803 gen_and(b0, b1);
3804 return b1;
3805 }
3806
3807 struct block *
3808 gen_llc_u(compiler_state_t *cstate)
3809 {
3810 struct block *b0, *b1;
3811
3812 /*
3813 * Catch errors reported by us and routines below us, and return NULL
3814 * on an error.
3815 */
3816 if (setjmp(cstate->top_ctx))
3817 return (NULL);
3818
3819 /*
3820 * Check whether this is an LLC frame.
3821 */
3822 b0 = gen_llc_internal(cstate);
3823
3824 /*
3825 * Now compare the low-order 2 bit of the control byte against
3826 * the appropriate value for U frames.
3827 */
3828 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
3829 gen_and(b0, b1);
3830 return b1;
3831 }
3832
3833 struct block *
3834 gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3835 {
3836 struct block *b0, *b1;
3837
3838 /*
3839 * Catch errors reported by us and routines below us, and return NULL
3840 * on an error.
3841 */
3842 if (setjmp(cstate->top_ctx))
3843 return (NULL);
3844
3845 /*
3846 * Check whether this is an LLC frame.
3847 */
3848 b0 = gen_llc_internal(cstate);
3849
3850 /*
3851 * Now check for an S frame with the appropriate type.
3852 */
3853 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
3854 gen_and(b0, b1);
3855 return b1;
3856 }
3857
3858 struct block *
3859 gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
3860 {
3861 struct block *b0, *b1;
3862
3863 /*
3864 * Catch errors reported by us and routines below us, and return NULL
3865 * on an error.
3866 */
3867 if (setjmp(cstate->top_ctx))
3868 return (NULL);
3869
3870 /*
3871 * Check whether this is an LLC frame.
3872 */
3873 b0 = gen_llc_internal(cstate);
3874
3875 /*
3876 * Now check for a U frame with the appropriate type.
3877 */
3878 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
3879 gen_and(b0, b1);
3880 return b1;
3881 }
3882
3883 /*
3884 * Generate code to match a particular packet type, for link-layer types
3885 * using 802.2 LLC headers.
3886 *
3887 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3888 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3889 *
3890 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3891 * value, if <= ETHERMTU. We use that to determine whether to
3892 * match the DSAP or both DSAP and LSAP or to check the OUI and
3893 * protocol ID in a SNAP header.
3894 */
3895 static struct block *
3896 gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
3897 {
3898 /*
3899 * XXX - handle token-ring variable-length header.
3900 */
3901 switch (ll_proto) {
3902
3903 case LLCSAP_IP:
3904 case LLCSAP_ISONS:
3905 case LLCSAP_NETBEUI:
3906 /*
3907 * XXX - should we check both the DSAP and the
3908 * SSAP, like this, or should we check just the
3909 * DSAP, as we do for other SAP values?
3910 */
3911 return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
3912 ((ll_proto << 8) | ll_proto));
3913
3914 case LLCSAP_IPX:
3915 /*
3916 * XXX - are there ever SNAP frames for IPX on
3917 * non-Ethernet 802.x networks?
3918 */
3919 return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
3920
3921 case ETHERTYPE_ATALK:
3922 /*
3923 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3924 * SNAP packets with an organization code of
3925 * 0x080007 (Apple, for Appletalk) and a protocol
3926 * type of ETHERTYPE_ATALK (Appletalk).
3927 *
3928 * XXX - check for an organization code of
3929 * encapsulated Ethernet as well?
3930 */
3931 return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
3932
3933 default:
3934 /*
3935 * XXX - we don't have to check for IPX 802.3
3936 * here, but should we check for the IPX Ethertype?
3937 */
3938 if (ll_proto <= ETHERMTU) {
3939 /*
3940 * This is an LLC SAP value, so check
3941 * the DSAP.
3942 */
3943 return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);
3944 } else {
3945 /*
3946 * This is an Ethernet type; we assume that it's
3947 * unlikely that it'll appear in the right place
3948 * at random, and therefore check only the
3949 * location that would hold the Ethernet type
3950 * in a SNAP frame with an organization code of
3951 * 0x000000 (encapsulated Ethernet).
3952 *
3953 * XXX - if we were to check for the SNAP DSAP and
3954 * LSAP, as per XXX, and were also to check for an
3955 * organization code of 0x000000 (encapsulated
3956 * Ethernet), we'd do
3957 *
3958 * return gen_snap(cstate, 0x000000, ll_proto);
3959 *
3960 * here; for now, we don't, as per the above.
3961 * I don't know whether it's worth the extra CPU
3962 * time to do the right check or not.
3963 */
3964 return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);
3965 }
3966 }
3967 }
3968
3969 static struct block *
3970 gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
3971 int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)
3972 {
3973 struct block *b0, *b1;
3974 u_int offset;
3975
3976 switch (dir) {
3977
3978 case Q_SRC:
3979 offset = src_off;
3980 break;
3981
3982 case Q_DST:
3983 offset = dst_off;
3984 break;
3985
3986 case Q_AND:
3987 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
3988 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
3989 gen_and(b0, b1);
3990 return b1;
3991
3992 case Q_DEFAULT:
3993 case Q_OR:
3994 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
3995 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
3996 gen_or(b0, b1);
3997 return b1;
3998
3999 case Q_ADDR1:
4000 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4001 /*NOTREACHED*/
4002
4003 case Q_ADDR2:
4004 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4005 /*NOTREACHED*/
4006
4007 case Q_ADDR3:
4008 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4009 /*NOTREACHED*/
4010
4011 case Q_ADDR4:
4012 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4013 /*NOTREACHED*/
4014
4015 case Q_RA:
4016 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4017 /*NOTREACHED*/
4018
4019 case Q_TA:
4020 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4021 /*NOTREACHED*/
4022
4023 default:
4024 abort();
4025 /*NOTREACHED*/
4026 }
4027 b0 = gen_linktype(cstate, ll_proto);
4028 b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
4029 gen_and(b0, b1);
4030 return b1;
4031 }
4032
4033 #ifdef INET6
4034 static struct block *
4035 gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
4036 struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,
4037 u_int dst_off)
4038 {
4039 struct block *b0, *b1;
4040 u_int offset;
4041 uint32_t *a, *m;
4042
4043 switch (dir) {
4044
4045 case Q_SRC:
4046 offset = src_off;
4047 break;
4048
4049 case Q_DST:
4050 offset = dst_off;
4051 break;
4052
4053 case Q_AND:
4054 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4055 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4056 gen_and(b0, b1);
4057 return b1;
4058
4059 case Q_DEFAULT:
4060 case Q_OR:
4061 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4062 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4063 gen_or(b0, b1);
4064 return b1;
4065
4066 case Q_ADDR1:
4067 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4068 /*NOTREACHED*/
4069
4070 case Q_ADDR2:
4071 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4072 /*NOTREACHED*/
4073
4074 case Q_ADDR3:
4075 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4076 /*NOTREACHED*/
4077
4078 case Q_ADDR4:
4079 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4080 /*NOTREACHED*/
4081
4082 case Q_RA:
4083 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4084 /*NOTREACHED*/
4085
4086 case Q_TA:
4087 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4088 /*NOTREACHED*/
4089
4090 default:
4091 abort();
4092 /*NOTREACHED*/
4093 }
4094 /* this order is important */
4095 a = (uint32_t *)addr;
4096 m = (uint32_t *)mask;
4097 b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
4098 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
4099 gen_and(b0, b1);
4100 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
4101 gen_and(b0, b1);
4102 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
4103 gen_and(b0, b1);
4104 b0 = gen_linktype(cstate, ll_proto);
4105 gen_and(b0, b1);
4106 return b1;
4107 }
4108 #endif
4109
4110 static struct block *
4111 gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4112 {
4113 register struct block *b0, *b1;
4114
4115 switch (dir) {
4116 case Q_SRC:
4117 return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
4118
4119 case Q_DST:
4120 return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
4121
4122 case Q_AND:
4123 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4124 b1 = gen_ehostop(cstate, eaddr, Q_DST);
4125 gen_and(b0, b1);
4126 return b1;
4127
4128 case Q_DEFAULT:
4129 case Q_OR:
4130 b0 = gen_ehostop(cstate, eaddr, Q_SRC);
4131 b1 = gen_ehostop(cstate, eaddr, Q_DST);
4132 gen_or(b0, b1);
4133 return b1;
4134
4135 case Q_ADDR1:
4136 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4137 /*NOTREACHED*/
4138
4139 case Q_ADDR2:
4140 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4141 /*NOTREACHED*/
4142
4143 case Q_ADDR3:
4144 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4145 /*NOTREACHED*/
4146
4147 case Q_ADDR4:
4148 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4149 /*NOTREACHED*/
4150
4151 case Q_RA:
4152 bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
4153 /*NOTREACHED*/
4154
4155 case Q_TA:
4156 bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
4157 /*NOTREACHED*/
4158 }
4159 abort();
4160 /*NOTREACHED*/
4161 }
4162
4163 /*
4164 * Like gen_ehostop, but for DLT_FDDI
4165 */
4166 static struct block *
4167 gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4168 {
4169 struct block *b0, *b1;
4170
4171 switch (dir) {
4172 case Q_SRC:
4173 return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
4174
4175 case Q_DST:
4176 return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
4177
4178 case Q_AND:
4179 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4180 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4181 gen_and(b0, b1);
4182 return b1;
4183
4184 case Q_DEFAULT:
4185 case Q_OR:
4186 b0 = gen_fhostop(cstate, eaddr, Q_SRC);
4187 b1 = gen_fhostop(cstate, eaddr, Q_DST);
4188 gen_or(b0, b1);
4189 return b1;
4190
4191 case Q_ADDR1:
4192 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4193 /*NOTREACHED*/
4194
4195 case Q_ADDR2:
4196 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4197 /*NOTREACHED*/
4198
4199 case Q_ADDR3:
4200 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4201 /*NOTREACHED*/
4202
4203 case Q_ADDR4:
4204 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4205 /*NOTREACHED*/
4206
4207 case Q_RA:
4208 bpf_error(cstate, "'ra' is only supported on 802.11");
4209 /*NOTREACHED*/
4210
4211 case Q_TA:
4212 bpf_error(cstate, "'ta' is only supported on 802.11");
4213 /*NOTREACHED*/
4214 }
4215 abort();
4216 /*NOTREACHED*/
4217 }
4218
4219 /*
4220 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4221 */
4222 static struct block *
4223 gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4224 {
4225 register struct block *b0, *b1;
4226
4227 switch (dir) {
4228 case Q_SRC:
4229 return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
4230
4231 case Q_DST:
4232 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4233
4234 case Q_AND:
4235 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4236 b1 = gen_thostop(cstate, eaddr, Q_DST);
4237 gen_and(b0, b1);
4238 return b1;
4239
4240 case Q_DEFAULT:
4241 case Q_OR:
4242 b0 = gen_thostop(cstate, eaddr, Q_SRC);
4243 b1 = gen_thostop(cstate, eaddr, Q_DST);
4244 gen_or(b0, b1);
4245 return b1;
4246
4247 case Q_ADDR1:
4248 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4249 /*NOTREACHED*/
4250
4251 case Q_ADDR2:
4252 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4253 /*NOTREACHED*/
4254
4255 case Q_ADDR3:
4256 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4257 /*NOTREACHED*/
4258
4259 case Q_ADDR4:
4260 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4261 /*NOTREACHED*/
4262
4263 case Q_RA:
4264 bpf_error(cstate, "'ra' is only supported on 802.11");
4265 /*NOTREACHED*/
4266
4267 case Q_TA:
4268 bpf_error(cstate, "'ta' is only supported on 802.11");
4269 /*NOTREACHED*/
4270 }
4271 abort();
4272 /*NOTREACHED*/
4273 }
4274
4275 /*
4276 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4277 * various 802.11 + radio headers.
4278 */
4279 static struct block *
4280 gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4281 {
4282 register struct block *b0, *b1, *b2;
4283 register struct slist *s;
4284
4285 #ifdef ENABLE_WLAN_FILTERING_PATCH
4286 /*
4287 * TODO GV 20070613
4288 * We need to disable the optimizer because the optimizer is buggy
4289 * and wipes out some LD instructions generated by the below
4290 * code to validate the Frame Control bits
4291 */
4292 cstate->no_optimize = 1;
4293 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4294
4295 switch (dir) {
4296 case Q_SRC:
4297 /*
4298 * Oh, yuk.
4299 *
4300 * For control frames, there is no SA.
4301 *
4302 * For management frames, SA is at an
4303 * offset of 10 from the beginning of
4304 * the packet.
4305 *
4306 * For data frames, SA is at an offset
4307 * of 10 from the beginning of the packet
4308 * if From DS is clear, at an offset of
4309 * 16 from the beginning of the packet
4310 * if From DS is set and To DS is clear,
4311 * and an offset of 24 from the beginning
4312 * of the packet if From DS is set and To DS
4313 * is set.
4314 */
4315
4316 /*
4317 * Generate the tests to be done for data frames
4318 * with From DS set.
4319 *
4320 * First, check for To DS set, i.e. check "link[1] & 0x01".
4321 */
4322 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4323 b1 = new_block(cstate, JMP(BPF_JSET));
4324 b1->s.k = 0x01; /* To DS */
4325 b1->stmts = s;
4326
4327 /*
4328 * If To DS is set, the SA is at 24.
4329 */
4330 b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4331 gen_and(b1, b0);
4332
4333 /*
4334 * Now, check for To DS not set, i.e. check
4335 * "!(link[1] & 0x01)".
4336 */
4337 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4338 b2 = new_block(cstate, JMP(BPF_JSET));
4339 b2->s.k = 0x01; /* To DS */
4340 b2->stmts = s;
4341 gen_not(b2);
4342
4343 /*
4344 * If To DS is not set, the SA is at 16.
4345 */
4346 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4347 gen_and(b2, b1);
4348
4349 /*
4350 * Now OR together the last two checks. That gives
4351 * the complete set of checks for data frames with
4352 * From DS set.
4353 */
4354 gen_or(b1, b0);
4355
4356 /*
4357 * Now check for From DS being set, and AND that with
4358 * the ORed-together checks.
4359 */
4360 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4361 b1 = new_block(cstate, JMP(BPF_JSET));
4362 b1->s.k = 0x02; /* From DS */
4363 b1->stmts = s;
4364 gen_and(b1, b0);
4365
4366 /*
4367 * Now check for data frames with From DS not set.
4368 */
4369 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4370 b2 = new_block(cstate, JMP(BPF_JSET));
4371 b2->s.k = 0x02; /* From DS */
4372 b2->stmts = s;
4373 gen_not(b2);
4374
4375 /*
4376 * If From DS isn't set, the SA is at 10.
4377 */
4378 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4379 gen_and(b2, b1);
4380
4381 /*
4382 * Now OR together the checks for data frames with
4383 * From DS not set and for data frames with From DS
4384 * set; that gives the checks done for data frames.
4385 */
4386 gen_or(b1, b0);
4387
4388 /*
4389 * Now check for a data frame.
4390 * I.e, check "link[0] & 0x08".
4391 */
4392 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4393 b1 = new_block(cstate, JMP(BPF_JSET));
4394 b1->s.k = 0x08;
4395 b1->stmts = s;
4396
4397 /*
4398 * AND that with the checks done for data frames.
4399 */
4400 gen_and(b1, b0);
4401
4402 /*
4403 * If the high-order bit of the type value is 0, this
4404 * is a management frame.
4405 * I.e, check "!(link[0] & 0x08)".
4406 */
4407 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4408 b2 = new_block(cstate, JMP(BPF_JSET));
4409 b2->s.k = 0x08;
4410 b2->stmts = s;
4411 gen_not(b2);
4412
4413 /*
4414 * For management frames, the SA is at 10.
4415 */
4416 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4417 gen_and(b2, b1);
4418
4419 /*
4420 * OR that with the checks done for data frames.
4421 * That gives the checks done for management and
4422 * data frames.
4423 */
4424 gen_or(b1, b0);
4425
4426 /*
4427 * If the low-order bit of the type value is 1,
4428 * this is either a control frame or a frame
4429 * with a reserved type, and thus not a
4430 * frame with an SA.
4431 *
4432 * I.e., check "!(link[0] & 0x04)".
4433 */
4434 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4435 b1 = new_block(cstate, JMP(BPF_JSET));
4436 b1->s.k = 0x04;
4437 b1->stmts = s;
4438 gen_not(b1);
4439
4440 /*
4441 * AND that with the checks for data and management
4442 * frames.
4443 */
4444 gen_and(b1, b0);
4445 return b0;
4446
4447 case Q_DST:
4448 /*
4449 * Oh, yuk.
4450 *
4451 * For control frames, there is no DA.
4452 *
4453 * For management frames, DA is at an
4454 * offset of 4 from the beginning of
4455 * the packet.
4456 *
4457 * For data frames, DA is at an offset
4458 * of 4 from the beginning of the packet
4459 * if To DS is clear and at an offset of
4460 * 16 from the beginning of the packet
4461 * if To DS is set.
4462 */
4463
4464 /*
4465 * Generate the tests to be done for data frames.
4466 *
4467 * First, check for To DS set, i.e. "link[1] & 0x01".
4468 */
4469 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4470 b1 = new_block(cstate, JMP(BPF_JSET));
4471 b1->s.k = 0x01; /* To DS */
4472 b1->stmts = s;
4473
4474 /*
4475 * If To DS is set, the DA is at 16.
4476 */
4477 b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4478 gen_and(b1, b0);
4479
4480 /*
4481 * Now, check for To DS not set, i.e. check
4482 * "!(link[1] & 0x01)".
4483 */
4484 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
4485 b2 = new_block(cstate, JMP(BPF_JSET));
4486 b2->s.k = 0x01; /* To DS */
4487 b2->stmts = s;
4488 gen_not(b2);
4489
4490 /*
4491 * If To DS is not set, the DA is at 4.
4492 */
4493 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4494 gen_and(b2, b1);
4495
4496 /*
4497 * Now OR together the last two checks. That gives
4498 * the complete set of checks for data frames.
4499 */
4500 gen_or(b1, b0);
4501
4502 /*
4503 * Now check for a data frame.
4504 * I.e, check "link[0] & 0x08".
4505 */
4506 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4507 b1 = new_block(cstate, JMP(BPF_JSET));
4508 b1->s.k = 0x08;
4509 b1->stmts = s;
4510
4511 /*
4512 * AND that with the checks done for data frames.
4513 */
4514 gen_and(b1, b0);
4515
4516 /*
4517 * If the high-order bit of the type value is 0, this
4518 * is a management frame.
4519 * I.e, check "!(link[0] & 0x08)".
4520 */
4521 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4522 b2 = new_block(cstate, JMP(BPF_JSET));
4523 b2->s.k = 0x08;
4524 b2->stmts = s;
4525 gen_not(b2);
4526
4527 /*
4528 * For management frames, the DA is at 4.
4529 */
4530 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4531 gen_and(b2, b1);
4532
4533 /*
4534 * OR that with the checks done for data frames.
4535 * That gives the checks done for management and
4536 * data frames.
4537 */
4538 gen_or(b1, b0);
4539
4540 /*
4541 * If the low-order bit of the type value is 1,
4542 * this is either a control frame or a frame
4543 * with a reserved type, and thus not a
4544 * frame with an SA.
4545 *
4546 * I.e., check "!(link[0] & 0x04)".
4547 */
4548 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4549 b1 = new_block(cstate, JMP(BPF_JSET));
4550 b1->s.k = 0x04;
4551 b1->stmts = s;
4552 gen_not(b1);
4553
4554 /*
4555 * AND that with the checks for data and management
4556 * frames.
4557 */
4558 gen_and(b1, b0);
4559 return b0;
4560
4561 case Q_AND:
4562 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4563 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4564 gen_and(b0, b1);
4565 return b1;
4566
4567 case Q_DEFAULT:
4568 case Q_OR:
4569 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
4570 b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
4571 gen_or(b0, b1);
4572 return b1;
4573
4574 /*
4575 * XXX - add BSSID keyword?
4576 */
4577 case Q_ADDR1:
4578 return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
4579
4580 case Q_ADDR2:
4581 /*
4582 * Not present in CTS or ACK control frames.
4583 */
4584 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4585 IEEE80211_FC0_TYPE_MASK);
4586 gen_not(b0);
4587 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4588 IEEE80211_FC0_SUBTYPE_MASK);
4589 gen_not(b1);
4590 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4591 IEEE80211_FC0_SUBTYPE_MASK);
4592 gen_not(b2);
4593 gen_and(b1, b2);
4594 gen_or(b0, b2);
4595 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4596 gen_and(b2, b1);
4597 return b1;
4598
4599 case Q_ADDR3:
4600 /*
4601 * Not present in control frames.
4602 */
4603 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4604 IEEE80211_FC0_TYPE_MASK);
4605 gen_not(b0);
4606 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
4607 gen_and(b0, b1);
4608 return b1;
4609
4610 case Q_ADDR4:
4611 /*
4612 * Present only if the direction mask has both "From DS"
4613 * and "To DS" set. Neither control frames nor management
4614 * frames should have both of those set, so we don't
4615 * check the frame type.
4616 */
4617 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
4618 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4619 b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
4620 gen_and(b0, b1);
4621 return b1;
4622
4623 case Q_RA:
4624 /*
4625 * Not present in management frames; addr1 in other
4626 * frames.
4627 */
4628
4629 /*
4630 * If the high-order bit of the type value is 0, this
4631 * is a management frame.
4632 * I.e, check "(link[0] & 0x08)".
4633 */
4634 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4635 b1 = new_block(cstate, JMP(BPF_JSET));
4636 b1->s.k = 0x08;
4637 b1->stmts = s;
4638
4639 /*
4640 * Check addr1.
4641 */
4642 b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
4643
4644 /*
4645 * AND that with the check of addr1.
4646 */
4647 gen_and(b1, b0);
4648 return (b0);
4649
4650 case Q_TA:
4651 /*
4652 * Not present in management frames; addr2, if present,
4653 * in other frames.
4654 */
4655
4656 /*
4657 * Not present in CTS or ACK control frames.
4658 */
4659 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4660 IEEE80211_FC0_TYPE_MASK);
4661 gen_not(b0);
4662 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4663 IEEE80211_FC0_SUBTYPE_MASK);
4664 gen_not(b1);
4665 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4666 IEEE80211_FC0_SUBTYPE_MASK);
4667 gen_not(b2);
4668 gen_and(b1, b2);
4669 gen_or(b0, b2);
4670
4671 /*
4672 * If the high-order bit of the type value is 0, this
4673 * is a management frame.
4674 * I.e, check "(link[0] & 0x08)".
4675 */
4676 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
4677 b1 = new_block(cstate, JMP(BPF_JSET));
4678 b1->s.k = 0x08;
4679 b1->stmts = s;
4680
4681 /*
4682 * AND that with the check for frames other than
4683 * CTS and ACK frames.
4684 */
4685 gen_and(b1, b2);
4686
4687 /*
4688 * Check addr2.
4689 */
4690 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4691 gen_and(b2, b1);
4692 return b1;
4693 }
4694 abort();
4695 /*NOTREACHED*/
4696 }
4697
4698 /*
4699 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4700 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4701 * as the RFC states.)
4702 */
4703 static struct block *
4704 gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
4705 {
4706 register struct block *b0, *b1;
4707
4708 switch (dir) {
4709 case Q_SRC:
4710 return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
4711
4712 case Q_DST:
4713 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
4714
4715 case Q_AND:
4716 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4717 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4718 gen_and(b0, b1);
4719 return b1;
4720
4721 case Q_DEFAULT:
4722 case Q_OR:
4723 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
4724 b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
4725 gen_or(b0, b1);
4726 return b1;
4727
4728 case Q_ADDR1:
4729 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
4730 /*NOTREACHED*/
4731
4732 case Q_ADDR2:
4733 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
4734 /*NOTREACHED*/
4735
4736 case Q_ADDR3:
4737 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
4738 /*NOTREACHED*/
4739
4740 case Q_ADDR4:
4741 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
4742 /*NOTREACHED*/
4743
4744 case Q_RA:
4745 bpf_error(cstate, "'ra' is only supported on 802.11");
4746 /*NOTREACHED*/
4747
4748 case Q_TA:
4749 bpf_error(cstate, "'ta' is only supported on 802.11");
4750 /*NOTREACHED*/
4751 }
4752 abort();
4753 /*NOTREACHED*/
4754 }
4755
4756 /*
4757 * This is quite tricky because there may be pad bytes in front of the
4758 * DECNET header, and then there are two possible data packet formats that
4759 * carry both src and dst addresses, plus 5 packet types in a format that
4760 * carries only the src node, plus 2 types that use a different format and
4761 * also carry just the src node.
4762 *
4763 * Yuck.
4764 *
4765 * Instead of doing those all right, we just look for data packets with
4766 * 0 or 1 bytes of padding. If you want to look at other packets, that
4767 * will require a lot more hacking.
4768 *
4769 * To add support for filtering on DECNET "areas" (network numbers)
4770 * one would want to add a "mask" argument to this routine. That would
4771 * make the filter even more inefficient, although one could be clever
4772 * and not generate masking instructions if the mask is 0xFFFF.
4773 */
4774 static struct block *
4775 gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
4776 {
4777 struct block *b0, *b1, *b2, *tmp;
4778 u_int offset_lh; /* offset if long header is received */
4779 u_int offset_sh; /* offset if short header is received */
4780
4781 switch (dir) {
4782
4783 case Q_DST:
4784 offset_sh = 1; /* follows flags */
4785 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4786 break;
4787
4788 case Q_SRC:
4789 offset_sh = 3; /* follows flags, dstnode */
4790 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4791 break;
4792
4793 case Q_AND:
4794 /* Inefficient because we do our Calvinball dance twice */
4795 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4796 b1 = gen_dnhostop(cstate, addr, Q_DST);
4797 gen_and(b0, b1);
4798 return b1;
4799
4800 case Q_DEFAULT:
4801 case Q_OR:
4802 /* Inefficient because we do our Calvinball dance twice */
4803 b0 = gen_dnhostop(cstate, addr, Q_SRC);
4804 b1 = gen_dnhostop(cstate, addr, Q_DST);
4805 gen_or(b0, b1);
4806 return b1;
4807
4808 case Q_ADDR1:
4809 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4810 /*NOTREACHED*/
4811
4812 case Q_ADDR2:
4813 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4814 /*NOTREACHED*/
4815
4816 case Q_ADDR3:
4817 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4818 /*NOTREACHED*/
4819
4820 case Q_ADDR4:
4821 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4822 /*NOTREACHED*/
4823
4824 case Q_RA:
4825 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4826 /*NOTREACHED*/
4827
4828 case Q_TA:
4829 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4830 /*NOTREACHED*/
4831
4832 default:
4833 abort();
4834 /*NOTREACHED*/
4835 }
4836 b0 = gen_linktype(cstate, ETHERTYPE_DN);
4837 /* Check for pad = 1, long header case */
4838 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4839 (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF));
4840 b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
4841 BPF_H, (bpf_u_int32)ntohs((u_short)addr));
4842 gen_and(tmp, b1);
4843 /* Check for pad = 0, long header case */
4844 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,
4845 (bpf_u_int32)0x7);
4846 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,
4847 (bpf_u_int32)ntohs((u_short)addr));
4848 gen_and(tmp, b2);
4849 gen_or(b2, b1);
4850 /* Check for pad = 1, short header case */
4851 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
4852 (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF));
4853 b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,
4854 (bpf_u_int32)ntohs((u_short)addr));
4855 gen_and(tmp, b2);
4856 gen_or(b2, b1);
4857 /* Check for pad = 0, short header case */
4858 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,
4859 (bpf_u_int32)0x7);
4860 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,
4861 (bpf_u_int32)ntohs((u_short)addr));
4862 gen_and(tmp, b2);
4863 gen_or(b2, b1);
4864
4865 /* Combine with test for cstate->linktype */
4866 gen_and(b0, b1);
4867 return b1;
4868 }
4869
4870 /*
4871 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4872 * test the bottom-of-stack bit, and then check the version number
4873 * field in the IP header.
4874 */
4875 static struct block *
4876 gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
4877 {
4878 struct block *b0, *b1;
4879
4880 switch (ll_proto) {
4881
4882 case ETHERTYPE_IP:
4883 /* match the bottom-of-stack bit */
4884 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4885 /* match the IPv4 version number */
4886 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
4887 gen_and(b0, b1);
4888 return b1;
4889
4890 case ETHERTYPE_IPV6:
4891 /* match the bottom-of-stack bit */
4892 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
4893 /* match the IPv4 version number */
4894 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
4895 gen_and(b0, b1);
4896 return b1;
4897
4898 default:
4899 /* FIXME add other L3 proto IDs */
4900 bpf_error(cstate, "unsupported protocol over mpls");
4901 /*NOTREACHED*/
4902 }
4903 }
4904
4905 static struct block *
4906 gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4907 int proto, int dir, int type)
4908 {
4909 struct block *b0, *b1;
4910 const char *typestr;
4911
4912 if (type == Q_NET)
4913 typestr = "net";
4914 else
4915 typestr = "host";
4916
4917 switch (proto) {
4918
4919 case Q_DEFAULT:
4920 b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
4921 /*
4922 * Only check for non-IPv4 addresses if we're not
4923 * checking MPLS-encapsulated packets.
4924 */
4925 if (cstate->label_stack_depth == 0) {
4926 b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
4927 gen_or(b0, b1);
4928 b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
4929 gen_or(b1, b0);
4930 }
4931 return b0;
4932
4933 case Q_LINK:
4934 bpf_error(cstate, "link-layer modifier applied to %s", typestr);
4935
4936 case Q_IP:
4937 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
4938
4939 case Q_RARP:
4940 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4941
4942 case Q_ARP:
4943 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4944
4945 case Q_SCTP:
4946 bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
4947
4948 case Q_TCP:
4949 bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
4950
4951 case Q_UDP:
4952 bpf_error(cstate, "'udp' modifier applied to %s", typestr);
4953
4954 case Q_ICMP:
4955 bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
4956
4957 case Q_IGMP:
4958 bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
4959
4960 case Q_IGRP:
4961 bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
4962
4963 case Q_ATALK:
4964 bpf_error(cstate, "AppleTalk host filtering not implemented");
4965
4966 case Q_DECNET:
4967 return gen_dnhostop(cstate, addr, dir);
4968
4969 case Q_LAT:
4970 bpf_error(cstate, "LAT host filtering not implemented");
4971
4972 case Q_SCA:
4973 bpf_error(cstate, "SCA host filtering not implemented");
4974
4975 case Q_MOPRC:
4976 bpf_error(cstate, "MOPRC host filtering not implemented");
4977
4978 case Q_MOPDL:
4979 bpf_error(cstate, "MOPDL host filtering not implemented");
4980
4981 case Q_IPV6:
4982 bpf_error(cstate, "'ip6' modifier applied to ip host");
4983
4984 case Q_ICMPV6:
4985 bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
4986
4987 case Q_AH:
4988 bpf_error(cstate, "'ah' modifier applied to %s", typestr);
4989
4990 case Q_ESP:
4991 bpf_error(cstate, "'esp' modifier applied to %s", typestr);
4992
4993 case Q_PIM:
4994 bpf_error(cstate, "'pim' modifier applied to %s", typestr);
4995
4996 case Q_VRRP:
4997 bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
4998
4999 case Q_AARP:
5000 bpf_error(cstate, "AARP host filtering not implemented");
5001
5002 case Q_ISO:
5003 bpf_error(cstate, "ISO host filtering not implemented");
5004
5005 case Q_ESIS:
5006 bpf_error(cstate, "'esis' modifier applied to %s", typestr);
5007
5008 case Q_ISIS:
5009 bpf_error(cstate, "'isis' modifier applied to %s", typestr);
5010
5011 case Q_CLNP:
5012 bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
5013
5014 case Q_STP:
5015 bpf_error(cstate, "'stp' modifier applied to %s", typestr);
5016
5017 case Q_IPX:
5018 bpf_error(cstate, "IPX host filtering not implemented");
5019
5020 case Q_NETBEUI:
5021 bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
5022
5023 case Q_ISIS_L1:
5024 bpf_error(cstate, "'l1' modifier applied to %s", typestr);
5025
5026 case Q_ISIS_L2:
5027 bpf_error(cstate, "'l2' modifier applied to %s", typestr);
5028
5029 case Q_ISIS_IIH:
5030 bpf_error(cstate, "'iih' modifier applied to %s", typestr);
5031
5032 case Q_ISIS_SNP:
5033 bpf_error(cstate, "'snp' modifier applied to %s", typestr);
5034
5035 case Q_ISIS_CSNP:
5036 bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
5037
5038 case Q_ISIS_PSNP:
5039 bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
5040
5041 case Q_ISIS_LSP:
5042 bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
5043
5044 case Q_RADIO:
5045 bpf_error(cstate, "'radio' modifier applied to %s", typestr);
5046
5047 case Q_CARP:
5048 bpf_error(cstate, "'carp' modifier applied to %s", typestr);
5049
5050 default:
5051 abort();
5052 }
5053 /*NOTREACHED*/
5054 }
5055
5056 #ifdef INET6
5057 static struct block *
5058 gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
5059 struct in6_addr *mask, int proto, int dir, int type)
5060 {
5061 const char *typestr;
5062
5063 if (type == Q_NET)
5064 typestr = "net";
5065 else
5066 typestr = "host";
5067
5068 switch (proto) {
5069
5070 case Q_DEFAULT:
5071 return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
5072
5073 case Q_LINK:
5074 bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
5075
5076 case Q_IP:
5077 bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
5078
5079 case Q_RARP:
5080 bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
5081
5082 case Q_ARP:
5083 bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
5084
5085 case Q_SCTP:
5086 bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
5087
5088 case Q_TCP:
5089 bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
5090
5091 case Q_UDP:
5092 bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
5093
5094 case Q_ICMP:
5095 bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
5096
5097 case Q_IGMP:
5098 bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
5099
5100 case Q_IGRP:
5101 bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
5102
5103 case Q_ATALK:
5104 bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
5105
5106 case Q_DECNET:
5107 bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
5108
5109 case Q_LAT:
5110 bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
5111
5112 case Q_SCA:
5113 bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
5114
5115 case Q_MOPRC:
5116 bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
5117
5118 case Q_MOPDL:
5119 bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
5120
5121 case Q_IPV6:
5122 return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
5123
5124 case Q_ICMPV6:
5125 bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
5126
5127 case Q_AH:
5128 bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
5129
5130 case Q_ESP:
5131 bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
5132
5133 case Q_PIM:
5134 bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
5135
5136 case Q_VRRP:
5137 bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
5138
5139 case Q_AARP:
5140 bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
5141
5142 case Q_ISO:
5143 bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
5144
5145 case Q_ESIS:
5146 bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
5147
5148 case Q_ISIS:
5149 bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
5150
5151 case Q_CLNP:
5152 bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
5153
5154 case Q_STP:
5155 bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
5156
5157 case Q_IPX:
5158 bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
5159
5160 case Q_NETBEUI:
5161 bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
5162
5163 case Q_ISIS_L1:
5164 bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
5165
5166 case Q_ISIS_L2:
5167 bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
5168
5169 case Q_ISIS_IIH:
5170 bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
5171
5172 case Q_ISIS_SNP:
5173 bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
5174
5175 case Q_ISIS_CSNP:
5176 bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
5177
5178 case Q_ISIS_PSNP:
5179 bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
5180
5181 case Q_ISIS_LSP:
5182 bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
5183
5184 case Q_RADIO:
5185 bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
5186
5187 case Q_CARP:
5188 bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
5189
5190 default:
5191 abort();
5192 }
5193 /*NOTREACHED*/
5194 }
5195 #endif
5196
5197 #ifndef INET6
5198 static struct block *
5199 gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
5200 struct addrinfo *alist, int proto, int dir)
5201 {
5202 struct block *b0, *b1, *tmp;
5203 struct addrinfo *ai;
5204 struct sockaddr_in *sin;
5205
5206 if (dir != 0)
5207 bpf_error(cstate, "direction applied to 'gateway'");
5208
5209 switch (proto) {
5210 case Q_DEFAULT:
5211 case Q_IP:
5212 case Q_ARP:
5213 case Q_RARP:
5214 switch (cstate->linktype) {
5215 case DLT_EN10MB:
5216 case DLT_NETANALYZER:
5217 case DLT_NETANALYZER_TRANSPARENT:
5218 b1 = gen_prevlinkhdr_check(cstate);
5219 b0 = gen_ehostop(cstate, eaddr, Q_OR);
5220 if (b1 != NULL)
5221 gen_and(b1, b0);
5222 break;
5223 case DLT_FDDI:
5224 b0 = gen_fhostop(cstate, eaddr, Q_OR);
5225 break;
5226 case DLT_IEEE802:
5227 b0 = gen_thostop(cstate, eaddr, Q_OR);
5228 break;
5229 case DLT_IEEE802_11:
5230 case DLT_PRISM_HEADER:
5231 case DLT_IEEE802_11_RADIO_AVS:
5232 case DLT_IEEE802_11_RADIO:
5233 case DLT_PPI:
5234 b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
5235 break;
5236 case DLT_SUNATM:
5237 /*
5238 * This is LLC-multiplexed traffic; if it were
5239 * LANE, cstate->linktype would have been set to
5240 * DLT_EN10MB.
5241 */
5242 bpf_error(cstate,
5243 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5244 break;
5245 case DLT_IP_OVER_FC:
5246 b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
5247 break;
5248 default:
5249 bpf_error(cstate,
5250 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5251 }
5252 b1 = NULL;
5253 for (ai = alist; ai != NULL; ai = ai->ai_next) {
5254 /*
5255 * Does it have an address?
5256 */
5257 if (ai->ai_addr != NULL) {
5258 /*
5259 * Yes. Is it an IPv4 address?
5260 */
5261 if (ai->ai_addr->sa_family == AF_INET) {
5262 /*
5263 * Generate an entry for it.
5264 */
5265 sin = (struct sockaddr_in *)ai->ai_addr;
5266 tmp = gen_host(cstate,
5267 ntohl(sin->sin_addr.s_addr),
5268 0xffffffff, proto, Q_OR, Q_HOST);
5269 /*
5270 * Is it the *first* IPv4 address?
5271 */
5272 if (b1 == NULL) {
5273 /*
5274 * Yes, so start with it.
5275 */
5276 b1 = tmp;
5277 } else {
5278 /*
5279 * No, so OR it into the
5280 * existing set of
5281 * addresses.
5282 */
5283 gen_or(b1, tmp);
5284 b1 = tmp;
5285 }
5286 }
5287 }
5288 }
5289 if (b1 == NULL) {
5290 /*
5291 * No IPv4 addresses found.
5292 */
5293 return (NULL);
5294 }
5295 gen_not(b1);
5296 gen_and(b0, b1);
5297 return b1;
5298 }
5299 bpf_error(cstate, "illegal modifier of 'gateway'");
5300 /*NOTREACHED*/
5301 }
5302 #endif
5303
5304 static struct block *
5305 gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
5306 {
5307 struct block *b0;
5308 struct block *b1;
5309
5310 switch (proto) {
5311
5312 case Q_SCTP:
5313 b1 = gen_proto(cstate, IPPROTO_SCTP, Q_IP, Q_DEFAULT);
5314 b0 = gen_proto(cstate, IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
5315 gen_or(b0, b1);
5316 break;
5317
5318 case Q_TCP:
5319 b1 = gen_proto(cstate, IPPROTO_TCP, Q_IP, Q_DEFAULT);
5320 b0 = gen_proto(cstate, IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
5321 gen_or(b0, b1);
5322 break;
5323
5324 case Q_UDP:
5325 b1 = gen_proto(cstate, IPPROTO_UDP, Q_IP, Q_DEFAULT);
5326 b0 = gen_proto(cstate, IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
5327 gen_or(b0, b1);
5328 break;
5329
5330 case Q_ICMP:
5331 b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
5332 break;
5333
5334 #ifndef IPPROTO_IGMP
5335 #define IPPROTO_IGMP 2
5336 #endif
5337
5338 case Q_IGMP:
5339 b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
5340 break;
5341
5342 #ifndef IPPROTO_IGRP
5343 #define IPPROTO_IGRP 9
5344 #endif
5345 case Q_IGRP:
5346 b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
5347 break;
5348
5349 #ifndef IPPROTO_PIM
5350 #define IPPROTO_PIM 103
5351 #endif
5352
5353 case Q_PIM:
5354 b1 = gen_proto(cstate, IPPROTO_PIM, Q_IP, Q_DEFAULT);
5355 b0 = gen_proto(cstate, IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
5356 gen_or(b0, b1);
5357 break;
5358
5359 #ifndef IPPROTO_VRRP
5360 #define IPPROTO_VRRP 112
5361 #endif
5362
5363 case Q_VRRP:
5364 b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
5365 break;
5366
5367 #ifndef IPPROTO_CARP
5368 #define IPPROTO_CARP 112
5369 #endif
5370
5371 case Q_CARP:
5372 b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
5373 break;
5374
5375 case Q_IP:
5376 b1 = gen_linktype(cstate, ETHERTYPE_IP);
5377 break;
5378
5379 case Q_ARP:
5380 b1 = gen_linktype(cstate, ETHERTYPE_ARP);
5381 break;
5382
5383 case Q_RARP:
5384 b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
5385 break;
5386
5387 case Q_LINK:
5388 bpf_error(cstate, "link layer applied in wrong context");
5389
5390 case Q_ATALK:
5391 b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
5392 break;
5393
5394 case Q_AARP:
5395 b1 = gen_linktype(cstate, ETHERTYPE_AARP);
5396 break;
5397
5398 case Q_DECNET:
5399 b1 = gen_linktype(cstate, ETHERTYPE_DN);
5400 break;
5401
5402 case Q_SCA:
5403 b1 = gen_linktype(cstate, ETHERTYPE_SCA);
5404 break;
5405
5406 case Q_LAT:
5407 b1 = gen_linktype(cstate, ETHERTYPE_LAT);
5408 break;
5409
5410 case Q_MOPDL:
5411 b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
5412 break;
5413
5414 case Q_MOPRC:
5415 b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
5416 break;
5417
5418 case Q_IPV6:
5419 b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
5420 break;
5421
5422 #ifndef IPPROTO_ICMPV6
5423 #define IPPROTO_ICMPV6 58
5424 #endif
5425 case Q_ICMPV6:
5426 b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
5427 break;
5428
5429 #ifndef IPPROTO_AH
5430 #define IPPROTO_AH 51
5431 #endif
5432 case Q_AH:
5433 b1 = gen_proto(cstate, IPPROTO_AH, Q_IP, Q_DEFAULT);
5434 b0 = gen_proto(cstate, IPPROTO_AH, Q_IPV6, Q_DEFAULT);
5435 gen_or(b0, b1);
5436 break;
5437
5438 #ifndef IPPROTO_ESP
5439 #define IPPROTO_ESP 50
5440 #endif
5441 case Q_ESP:
5442 b1 = gen_proto(cstate, IPPROTO_ESP, Q_IP, Q_DEFAULT);
5443 b0 = gen_proto(cstate, IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
5444 gen_or(b0, b1);
5445 break;
5446
5447 case Q_ISO:
5448 b1 = gen_linktype(cstate, LLCSAP_ISONS);
5449 break;
5450
5451 case Q_ESIS:
5452 b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
5453 break;
5454
5455 case Q_ISIS:
5456 b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5457 break;
5458
5459 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
5460 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5461 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5462 gen_or(b0, b1);
5463 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5464 gen_or(b0, b1);
5465 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5466 gen_or(b0, b1);
5467 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5468 gen_or(b0, b1);
5469 break;
5470
5471 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
5472 b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5473 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
5474 gen_or(b0, b1);
5475 b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5476 gen_or(b0, b1);
5477 b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5478 gen_or(b0, b1);
5479 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5480 gen_or(b0, b1);
5481 break;
5482
5483 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
5484 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
5485 b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
5486 gen_or(b0, b1);
5487 b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
5488 gen_or(b0, b1);
5489 break;
5490
5491 case Q_ISIS_LSP:
5492 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
5493 b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
5494 gen_or(b0, b1);
5495 break;
5496
5497 case Q_ISIS_SNP:
5498 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5499 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5500 gen_or(b0, b1);
5501 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5502 gen_or(b0, b1);
5503 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5504 gen_or(b0, b1);
5505 break;
5506
5507 case Q_ISIS_CSNP:
5508 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5509 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5510 gen_or(b0, b1);
5511 break;
5512
5513 case Q_ISIS_PSNP:
5514 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5515 b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5516 gen_or(b0, b1);
5517 break;
5518
5519 case Q_CLNP:
5520 b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5521 break;
5522
5523 case Q_STP:
5524 b1 = gen_linktype(cstate, LLCSAP_8021D);
5525 break;
5526
5527 case Q_IPX:
5528 b1 = gen_linktype(cstate, LLCSAP_IPX);
5529 break;
5530
5531 case Q_NETBEUI:
5532 b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
5533 break;
5534
5535 case Q_RADIO:
5536 bpf_error(cstate, "'radio' is not a valid protocol type");
5537
5538 default:
5539 abort();
5540 }
5541 return b1;
5542 }
5543
5544 struct block *
5545 gen_proto_abbrev(compiler_state_t *cstate, int proto)
5546 {
5547 /*
5548 * Catch errors reported by us and routines below us, and return NULL
5549 * on an error.
5550 */
5551 if (setjmp(cstate->top_ctx))
5552 return (NULL);
5553
5554 return gen_proto_abbrev_internal(cstate, proto);
5555 }
5556
5557 static struct block *
5558 gen_ipfrag(compiler_state_t *cstate)
5559 {
5560 struct slist *s;
5561 struct block *b;
5562
5563 /* not IPv4 frag other than the first frag */
5564 s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
5565 b = new_block(cstate, JMP(BPF_JSET));
5566 b->s.k = 0x1fff;
5567 b->stmts = s;
5568 gen_not(b);
5569
5570 return b;
5571 }
5572
5573 /*
5574 * Generate a comparison to a port value in the transport-layer header
5575 * at the specified offset from the beginning of that header.
5576 *
5577 * XXX - this handles a variable-length prefix preceding the link-layer
5578 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5579 * variable-length link-layer headers (such as Token Ring or 802.11
5580 * headers).
5581 */
5582 static struct block *
5583 gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
5584 {
5585 return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
5586 }
5587
5588 static struct block *
5589 gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
5590 {
5591 return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
5592 }
5593
5594 static struct block *
5595 gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5596 {
5597 struct block *b0, *b1, *tmp;
5598
5599 /* ip proto 'proto' and not a fragment other than the first fragment */
5600 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5601 b0 = gen_ipfrag(cstate);
5602 gen_and(tmp, b0);
5603
5604 switch (dir) {
5605 case Q_SRC:
5606 b1 = gen_portatom(cstate, 0, port);
5607 break;
5608
5609 case Q_DST:
5610 b1 = gen_portatom(cstate, 2, port);
5611 break;
5612
5613 case Q_AND:
5614 tmp = gen_portatom(cstate, 0, port);
5615 b1 = gen_portatom(cstate, 2, port);
5616 gen_and(tmp, b1);
5617 break;
5618
5619 case Q_DEFAULT:
5620 case Q_OR:
5621 tmp = gen_portatom(cstate, 0, port);
5622 b1 = gen_portatom(cstate, 2, port);
5623 gen_or(tmp, b1);
5624 break;
5625
5626 case Q_ADDR1:
5627 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
5628 /*NOTREACHED*/
5629
5630 case Q_ADDR2:
5631 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
5632 /*NOTREACHED*/
5633
5634 case Q_ADDR3:
5635 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
5636 /*NOTREACHED*/
5637
5638 case Q_ADDR4:
5639 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
5640 /*NOTREACHED*/
5641
5642 case Q_RA:
5643 bpf_error(cstate, "'ra' is not a valid qualifier for ports");
5644 /*NOTREACHED*/
5645
5646 case Q_TA:
5647 bpf_error(cstate, "'ta' is not a valid qualifier for ports");
5648 /*NOTREACHED*/
5649
5650 default:
5651 abort();
5652 /*NOTREACHED*/
5653 }
5654 gen_and(b0, b1);
5655
5656 return b1;
5657 }
5658
5659 static struct block *
5660 gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5661 {
5662 struct block *b0, *b1, *tmp;
5663
5664 /*
5665 * ether proto ip
5666 *
5667 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5668 * not LLC encapsulation with LLCSAP_IP.
5669 *
5670 * For IEEE 802 networks - which includes 802.5 token ring
5671 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5672 * says that SNAP encapsulation is used, not LLC encapsulation
5673 * with LLCSAP_IP.
5674 *
5675 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5676 * RFC 2225 say that SNAP encapsulation is used, not LLC
5677 * encapsulation with LLCSAP_IP.
5678 *
5679 * So we always check for ETHERTYPE_IP.
5680 */
5681 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5682
5683 switch (ip_proto) {
5684 case IPPROTO_UDP:
5685 case IPPROTO_TCP:
5686 case IPPROTO_SCTP:
5687 b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
5688 break;
5689
5690 case PROTO_UNDEF:
5691 tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
5692 b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
5693 gen_or(tmp, b1);
5694 tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
5695 gen_or(tmp, b1);
5696 break;
5697
5698 default:
5699 abort();
5700 }
5701 gen_and(b0, b1);
5702 return b1;
5703 }
5704
5705 struct block *
5706 gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
5707 {
5708 struct block *b0, *b1, *tmp;
5709
5710 /* ip6 proto 'proto' */
5711 /* XXX - catch the first fragment of a fragmented packet? */
5712 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5713
5714 switch (dir) {
5715 case Q_SRC:
5716 b1 = gen_portatom6(cstate, 0, port);
5717 break;
5718
5719 case Q_DST:
5720 b1 = gen_portatom6(cstate, 2, port);
5721 break;
5722
5723 case Q_AND:
5724 tmp = gen_portatom6(cstate, 0, port);
5725 b1 = gen_portatom6(cstate, 2, port);
5726 gen_and(tmp, b1);
5727 break;
5728
5729 case Q_DEFAULT:
5730 case Q_OR:
5731 tmp = gen_portatom6(cstate, 0, port);
5732 b1 = gen_portatom6(cstate, 2, port);
5733 gen_or(tmp, b1);
5734 break;
5735
5736 default:
5737 abort();
5738 }
5739 gen_and(b0, b1);
5740
5741 return b1;
5742 }
5743
5744 static struct block *
5745 gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
5746 {
5747 struct block *b0, *b1, *tmp;
5748
5749 /* link proto ip6 */
5750 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5751
5752 switch (ip_proto) {
5753 case IPPROTO_UDP:
5754 case IPPROTO_TCP:
5755 case IPPROTO_SCTP:
5756 b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
5757 break;
5758
5759 case PROTO_UNDEF:
5760 tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
5761 b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
5762 gen_or(tmp, b1);
5763 tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
5764 gen_or(tmp, b1);
5765 break;
5766
5767 default:
5768 abort();
5769 }
5770 gen_and(b0, b1);
5771 return b1;
5772 }
5773
5774 /* gen_portrange code */
5775 static struct block *
5776 gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5777 bpf_u_int32 v2)
5778 {
5779 struct block *b1, *b2;
5780
5781 if (v1 > v2) {
5782 /*
5783 * Reverse the order of the ports, so v1 is the lower one.
5784 */
5785 bpf_u_int32 vtemp;
5786
5787 vtemp = v1;
5788 v1 = v2;
5789 v2 = vtemp;
5790 }
5791
5792 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
5793 b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
5794
5795 gen_and(b1, b2);
5796
5797 return b2;
5798 }
5799
5800 static struct block *
5801 gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
5802 bpf_u_int32 proto, int dir)
5803 {
5804 struct block *b0, *b1, *tmp;
5805
5806 /* ip proto 'proto' and not a fragment other than the first fragment */
5807 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
5808 b0 = gen_ipfrag(cstate);
5809 gen_and(tmp, b0);
5810
5811 switch (dir) {
5812 case Q_SRC:
5813 b1 = gen_portrangeatom(cstate, 0, port1, port2);
5814 break;
5815
5816 case Q_DST:
5817 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5818 break;
5819
5820 case Q_AND:
5821 tmp = gen_portrangeatom(cstate, 0, port1, port2);
5822 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5823 gen_and(tmp, b1);
5824 break;
5825
5826 case Q_DEFAULT:
5827 case Q_OR:
5828 tmp = gen_portrangeatom(cstate, 0, port1, port2);
5829 b1 = gen_portrangeatom(cstate, 2, port1, port2);
5830 gen_or(tmp, b1);
5831 break;
5832
5833 case Q_ADDR1:
5834 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5835 /*NOTREACHED*/
5836
5837 case Q_ADDR2:
5838 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5839 /*NOTREACHED*/
5840
5841 case Q_ADDR3:
5842 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5843 /*NOTREACHED*/
5844
5845 case Q_ADDR4:
5846 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5847 /*NOTREACHED*/
5848
5849 case Q_RA:
5850 bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
5851 /*NOTREACHED*/
5852
5853 case Q_TA:
5854 bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
5855 /*NOTREACHED*/
5856
5857 default:
5858 abort();
5859 /*NOTREACHED*/
5860 }
5861 gen_and(b0, b1);
5862
5863 return b1;
5864 }
5865
5866 static struct block *
5867 gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
5868 int dir)
5869 {
5870 struct block *b0, *b1, *tmp;
5871
5872 /* link proto ip */
5873 b0 = gen_linktype(cstate, ETHERTYPE_IP);
5874
5875 switch (ip_proto) {
5876 case IPPROTO_UDP:
5877 case IPPROTO_TCP:
5878 case IPPROTO_SCTP:
5879 b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
5880 dir);
5881 break;
5882
5883 case PROTO_UNDEF:
5884 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
5885 b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
5886 gen_or(tmp, b1);
5887 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
5888 gen_or(tmp, b1);
5889 break;
5890
5891 default:
5892 abort();
5893 }
5894 gen_and(b0, b1);
5895 return b1;
5896 }
5897
5898 static struct block *
5899 gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
5900 bpf_u_int32 v2)
5901 {
5902 struct block *b1, *b2;
5903
5904 if (v1 > v2) {
5905 /*
5906 * Reverse the order of the ports, so v1 is the lower one.
5907 */
5908 bpf_u_int32 vtemp;
5909
5910 vtemp = v1;
5911 v1 = v2;
5912 v2 = vtemp;
5913 }
5914
5915 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
5916 b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
5917
5918 gen_and(b1, b2);
5919
5920 return b2;
5921 }
5922
5923 static struct block *
5924 gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
5925 bpf_u_int32 proto, int dir)
5926 {
5927 struct block *b0, *b1, *tmp;
5928
5929 /* ip6 proto 'proto' */
5930 /* XXX - catch the first fragment of a fragmented packet? */
5931 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
5932
5933 switch (dir) {
5934 case Q_SRC:
5935 b1 = gen_portrangeatom6(cstate, 0, port1, port2);
5936 break;
5937
5938 case Q_DST:
5939 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5940 break;
5941
5942 case Q_AND:
5943 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5944 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5945 gen_and(tmp, b1);
5946 break;
5947
5948 case Q_DEFAULT:
5949 case Q_OR:
5950 tmp = gen_portrangeatom6(cstate, 0, port1, port2);
5951 b1 = gen_portrangeatom6(cstate, 2, port1, port2);
5952 gen_or(tmp, b1);
5953 break;
5954
5955 default:
5956 abort();
5957 }
5958 gen_and(b0, b1);
5959
5960 return b1;
5961 }
5962
5963 static struct block *
5964 gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
5965 int dir)
5966 {
5967 struct block *b0, *b1, *tmp;
5968
5969 /* link proto ip6 */
5970 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5971
5972 switch (ip_proto) {
5973 case IPPROTO_UDP:
5974 case IPPROTO_TCP:
5975 case IPPROTO_SCTP:
5976 b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
5977 dir);
5978 break;
5979
5980 case PROTO_UNDEF:
5981 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
5982 b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
5983 gen_or(tmp, b1);
5984 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
5985 gen_or(tmp, b1);
5986 break;
5987
5988 default:
5989 abort();
5990 }
5991 gen_and(b0, b1);
5992 return b1;
5993 }
5994
5995 static int
5996 lookup_proto(compiler_state_t *cstate, const char *name, int proto)
5997 {
5998 register int v;
5999
6000 switch (proto) {
6001
6002 case Q_DEFAULT:
6003 case Q_IP:
6004 case Q_IPV6:
6005 v = pcap_nametoproto(name);
6006 if (v == PROTO_UNDEF)
6007 bpf_error(cstate, "unknown ip proto '%s'", name);
6008 break;
6009
6010 case Q_LINK:
6011 /* XXX should look up h/w protocol type based on cstate->linktype */
6012 v = pcap_nametoeproto(name);
6013 if (v == PROTO_UNDEF) {
6014 v = pcap_nametollc(name);
6015 if (v == PROTO_UNDEF)
6016 bpf_error(cstate, "unknown ether proto '%s'", name);
6017 }
6018 break;
6019
6020 case Q_ISO:
6021 if (strcmp(name, "esis") == 0)
6022 v = ISO9542_ESIS;
6023 else if (strcmp(name, "isis") == 0)
6024 v = ISO10589_ISIS;
6025 else if (strcmp(name, "clnp") == 0)
6026 v = ISO8473_CLNP;
6027 else
6028 bpf_error(cstate, "unknown osi proto '%s'", name);
6029 break;
6030
6031 default:
6032 v = PROTO_UNDEF;
6033 break;
6034 }
6035 return v;
6036 }
6037
6038 #if 0
6039 struct stmt *
6040 gen_joinsp(struct stmt **s, int n)
6041 {
6042 return NULL;
6043 }
6044 #endif
6045
6046 static struct block *
6047 gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
6048 {
6049 #ifdef NO_PROTOCHAIN
6050 return gen_proto(cstate, v, proto);
6051 #else
6052 struct block *b0, *b;
6053 struct slist *s[100];
6054 int fix2, fix3, fix4, fix5;
6055 int ahcheck, again, end;
6056 int i, max;
6057 int reg2 = alloc_reg(cstate);
6058
6059 memset(s, 0, sizeof(s));
6060 fix3 = fix4 = fix5 = 0;
6061
6062 switch (proto) {
6063 case Q_IP:
6064 case Q_IPV6:
6065 break;
6066 case Q_DEFAULT:
6067 b0 = gen_protochain(cstate, v, Q_IP);
6068 b = gen_protochain(cstate, v, Q_IPV6);
6069 gen_or(b0, b);
6070 return b;
6071 default:
6072 bpf_error(cstate, "bad protocol applied for 'protochain'");
6073 /*NOTREACHED*/
6074 }
6075
6076 /*
6077 * We don't handle variable-length prefixes before the link-layer
6078 * header, or variable-length link-layer headers, here yet.
6079 * We might want to add BPF instructions to do the protochain
6080 * work, to simplify that and, on platforms that have a BPF
6081 * interpreter with the new instructions, let the filtering
6082 * be done in the kernel. (We already require a modified BPF
6083 * engine to do the protochain stuff, to support backward
6084 * branches, and backward branch support is unlikely to appear
6085 * in kernel BPF engines.)
6086 */
6087 if (cstate->off_linkpl.is_variable)
6088 bpf_error(cstate, "'protochain' not supported with variable length headers");
6089
6090 cstate->no_optimize = 1; /* this code is not compatible with optimizer yet */
6091
6092 /*
6093 * s[0] is a dummy entry to protect other BPF insn from damage
6094 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6095 * hard to find interdependency made by jump table fixup.
6096 */
6097 i = 0;
6098 s[i] = new_stmt(cstate, 0); /*dummy*/
6099 i++;
6100
6101 switch (proto) {
6102 case Q_IP:
6103 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6104
6105 /* A = ip->ip_p */
6106 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6107 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
6108 i++;
6109 /* X = ip->ip_hl << 2 */
6110 s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
6111 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6112 i++;
6113 break;
6114
6115 case Q_IPV6:
6116 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6117
6118 /* A = ip6->ip_nxt */
6119 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
6120 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
6121 i++;
6122 /* X = sizeof(struct ip6_hdr) */
6123 s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
6124 s[i]->s.k = 40;
6125 i++;
6126 break;
6127
6128 default:
6129 bpf_error(cstate, "unsupported proto to gen_protochain");
6130 /*NOTREACHED*/
6131 }
6132
6133 /* again: if (A == v) goto end; else fall through; */
6134 again = i;
6135 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6136 s[i]->s.k = v;
6137 s[i]->s.jt = NULL; /*later*/
6138 s[i]->s.jf = NULL; /*update in next stmt*/
6139 fix5 = i;
6140 i++;
6141
6142 #ifndef IPPROTO_NONE
6143 #define IPPROTO_NONE 59
6144 #endif
6145 /* if (A == IPPROTO_NONE) goto end */
6146 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6147 s[i]->s.jt = NULL; /*later*/
6148 s[i]->s.jf = NULL; /*update in next stmt*/
6149 s[i]->s.k = IPPROTO_NONE;
6150 s[fix5]->s.jf = s[i];
6151 fix2 = i;
6152 i++;
6153
6154 if (proto == Q_IPV6) {
6155 int v6start, v6end, v6advance, j;
6156
6157 v6start = i;
6158 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6159 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6160 s[i]->s.jt = NULL; /*later*/
6161 s[i]->s.jf = NULL; /*update in next stmt*/
6162 s[i]->s.k = IPPROTO_HOPOPTS;
6163 s[fix2]->s.jf = s[i];
6164 i++;
6165 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6166 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6167 s[i]->s.jt = NULL; /*later*/
6168 s[i]->s.jf = NULL; /*update in next stmt*/
6169 s[i]->s.k = IPPROTO_DSTOPTS;
6170 i++;
6171 /* if (A == IPPROTO_ROUTING) goto v6advance */
6172 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6173 s[i]->s.jt = NULL; /*later*/
6174 s[i]->s.jf = NULL; /*update in next stmt*/
6175 s[i]->s.k = IPPROTO_ROUTING;
6176 i++;
6177 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6178 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6179 s[i]->s.jt = NULL; /*later*/
6180 s[i]->s.jf = NULL; /*later*/
6181 s[i]->s.k = IPPROTO_FRAGMENT;
6182 fix3 = i;
6183 v6end = i;
6184 i++;
6185
6186 /* v6advance: */
6187 v6advance = i;
6188
6189 /*
6190 * in short,
6191 * A = P[X + packet head];
6192 * X = X + (P[X + packet head + 1] + 1) * 8;
6193 */
6194 /* A = P[X + packet head] */
6195 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6196 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6197 i++;
6198 /* MEM[reg2] = A */
6199 s[i] = new_stmt(cstate, BPF_ST);
6200 s[i]->s.k = reg2;
6201 i++;
6202 /* A = P[X + packet head + 1]; */
6203 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6204 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
6205 i++;
6206 /* A += 1 */
6207 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6208 s[i]->s.k = 1;
6209 i++;
6210 /* A *= 8 */
6211 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6212 s[i]->s.k = 8;
6213 i++;
6214 /* A += X */
6215 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
6216 s[i]->s.k = 0;
6217 i++;
6218 /* X = A; */
6219 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6220 i++;
6221 /* A = MEM[reg2] */
6222 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6223 s[i]->s.k = reg2;
6224 i++;
6225
6226 /* goto again; (must use BPF_JA for backward jump) */
6227 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6228 s[i]->s.k = again - i - 1;
6229 s[i - 1]->s.jf = s[i];
6230 i++;
6231
6232 /* fixup */
6233 for (j = v6start; j <= v6end; j++)
6234 s[j]->s.jt = s[v6advance];
6235 } else {
6236 /* nop */
6237 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6238 s[i]->s.k = 0;
6239 s[fix2]->s.jf = s[i];
6240 i++;
6241 }
6242
6243 /* ahcheck: */
6244 ahcheck = i;
6245 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6246 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
6247 s[i]->s.jt = NULL; /*later*/
6248 s[i]->s.jf = NULL; /*later*/
6249 s[i]->s.k = IPPROTO_AH;
6250 if (fix3)
6251 s[fix3]->s.jf = s[ahcheck];
6252 fix4 = i;
6253 i++;
6254
6255 /*
6256 * in short,
6257 * A = P[X];
6258 * X = X + (P[X + 1] + 2) * 4;
6259 */
6260 /* A = X */
6261 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6262 i++;
6263 /* A = P[X + packet head]; */
6264 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6265 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6266 i++;
6267 /* MEM[reg2] = A */
6268 s[i] = new_stmt(cstate, BPF_ST);
6269 s[i]->s.k = reg2;
6270 i++;
6271 /* A = X */
6272 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
6273 i++;
6274 /* A += 1 */
6275 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6276 s[i]->s.k = 1;
6277 i++;
6278 /* X = A */
6279 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6280 i++;
6281 /* A = P[X + packet head] */
6282 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
6283 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
6284 i++;
6285 /* A += 2 */
6286 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6287 s[i]->s.k = 2;
6288 i++;
6289 /* A *= 4 */
6290 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
6291 s[i]->s.k = 4;
6292 i++;
6293 /* X = A; */
6294 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
6295 i++;
6296 /* A = MEM[reg2] */
6297 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
6298 s[i]->s.k = reg2;
6299 i++;
6300
6301 /* goto again; (must use BPF_JA for backward jump) */
6302 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
6303 s[i]->s.k = again - i - 1;
6304 i++;
6305
6306 /* end: nop */
6307 end = i;
6308 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
6309 s[i]->s.k = 0;
6310 s[fix2]->s.jt = s[end];
6311 s[fix4]->s.jf = s[end];
6312 s[fix5]->s.jt = s[end];
6313 i++;
6314
6315 /*
6316 * make slist chain
6317 */
6318 max = i;
6319 for (i = 0; i < max - 1; i++)
6320 s[i]->next = s[i + 1];
6321 s[max - 1]->next = NULL;
6322
6323 /*
6324 * emit final check
6325 */
6326 b = new_block(cstate, JMP(BPF_JEQ));
6327 b->stmts = s[1]; /*remember, s[0] is dummy*/
6328 b->s.k = v;
6329
6330 free_reg(cstate, reg2);
6331
6332 gen_and(b0, b);
6333 return b;
6334 #endif
6335 }
6336
6337 static struct block *
6338 gen_check_802_11_data_frame(compiler_state_t *cstate)
6339 {
6340 struct slist *s;
6341 struct block *b0, *b1;
6342
6343 /*
6344 * A data frame has the 0x08 bit (b3) in the frame control field set
6345 * and the 0x04 bit (b2) clear.
6346 */
6347 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6348 b0 = new_block(cstate, JMP(BPF_JSET));
6349 b0->s.k = 0x08;
6350 b0->stmts = s;
6351
6352 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
6353 b1 = new_block(cstate, JMP(BPF_JSET));
6354 b1->s.k = 0x04;
6355 b1->stmts = s;
6356 gen_not(b1);
6357
6358 gen_and(b1, b0);
6359
6360 return b0;
6361 }
6362
6363 /*
6364 * Generate code that checks whether the packet is a packet for protocol
6365 * <proto> and whether the type field in that protocol's header has
6366 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6367 * IP packet and checks the protocol number in the IP header against <v>.
6368 *
6369 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6370 * against Q_IP and Q_IPV6.
6371 */
6372 static struct block *
6373 gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
6374 {
6375 struct block *b0, *b1;
6376 #ifndef CHASE_CHAIN
6377 struct block *b2;
6378 #endif
6379
6380 if (dir != Q_DEFAULT)
6381 bpf_error(cstate, "direction applied to 'proto'");
6382
6383 switch (proto) {
6384 case Q_DEFAULT:
6385 b0 = gen_proto(cstate, v, Q_IP, dir);
6386 b1 = gen_proto(cstate, v, Q_IPV6, dir);
6387 gen_or(b0, b1);
6388 return b1;
6389
6390 case Q_LINK:
6391 return gen_linktype(cstate, v);
6392
6393 case Q_IP:
6394 /*
6395 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6396 * not LLC encapsulation with LLCSAP_IP.
6397 *
6398 * For IEEE 802 networks - which includes 802.5 token ring
6399 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6400 * says that SNAP encapsulation is used, not LLC encapsulation
6401 * with LLCSAP_IP.
6402 *
6403 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6404 * RFC 2225 say that SNAP encapsulation is used, not LLC
6405 * encapsulation with LLCSAP_IP.
6406 *
6407 * So we always check for ETHERTYPE_IP.
6408 */
6409 b0 = gen_linktype(cstate, ETHERTYPE_IP);
6410 #ifndef CHASE_CHAIN
6411 b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
6412 #else
6413 b1 = gen_protochain(cstate, v, Q_IP);
6414 #endif
6415 gen_and(b0, b1);
6416 return b1;
6417
6418 case Q_ARP:
6419 bpf_error(cstate, "arp does not encapsulate another protocol");
6420 /*NOTREACHED*/
6421
6422 case Q_RARP:
6423 bpf_error(cstate, "rarp does not encapsulate another protocol");
6424 /*NOTREACHED*/
6425
6426 case Q_SCTP:
6427 bpf_error(cstate, "'sctp proto' is bogus");
6428 /*NOTREACHED*/
6429
6430 case Q_TCP:
6431 bpf_error(cstate, "'tcp proto' is bogus");
6432 /*NOTREACHED*/
6433
6434 case Q_UDP:
6435 bpf_error(cstate, "'udp proto' is bogus");
6436 /*NOTREACHED*/
6437
6438 case Q_ICMP:
6439 bpf_error(cstate, "'icmp proto' is bogus");
6440 /*NOTREACHED*/
6441
6442 case Q_IGMP:
6443 bpf_error(cstate, "'igmp proto' is bogus");
6444 /*NOTREACHED*/
6445
6446 case Q_IGRP:
6447 bpf_error(cstate, "'igrp proto' is bogus");
6448 /*NOTREACHED*/
6449
6450 case Q_ATALK:
6451 bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
6452 /*NOTREACHED*/
6453
6454 case Q_DECNET:
6455 bpf_error(cstate, "DECNET encapsulation is not specifiable");
6456 /*NOTREACHED*/
6457
6458 case Q_LAT:
6459 bpf_error(cstate, "LAT does not encapsulate another protocol");
6460 /*NOTREACHED*/
6461
6462 case Q_SCA:
6463 bpf_error(cstate, "SCA does not encapsulate another protocol");
6464 /*NOTREACHED*/
6465
6466 case Q_MOPRC:
6467 bpf_error(cstate, "MOPRC does not encapsulate another protocol");
6468 /*NOTREACHED*/
6469
6470 case Q_MOPDL:
6471 bpf_error(cstate, "MOPDL does not encapsulate another protocol");
6472 /*NOTREACHED*/
6473
6474 case Q_IPV6:
6475 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
6476 #ifndef CHASE_CHAIN
6477 /*
6478 * Also check for a fragment header before the final
6479 * header.
6480 */
6481 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
6482 b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
6483 gen_and(b2, b1);
6484 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
6485 gen_or(b2, b1);
6486 #else
6487 b1 = gen_protochain(cstate, v, Q_IPV6);
6488 #endif
6489 gen_and(b0, b1);
6490 return b1;
6491
6492 case Q_ICMPV6:
6493 bpf_error(cstate, "'icmp6 proto' is bogus");
6494 /*NOTREACHED*/
6495
6496 case Q_AH:
6497 bpf_error(cstate, "'ah proto' is bogus");
6498 /*NOTREACHED*/
6499
6500 case Q_ESP:
6501 bpf_error(cstate, "'ah proto' is bogus");
6502 /*NOTREACHED*/
6503
6504 case Q_PIM:
6505 bpf_error(cstate, "'pim proto' is bogus");
6506 /*NOTREACHED*/
6507
6508 case Q_VRRP:
6509 bpf_error(cstate, "'vrrp proto' is bogus");
6510 /*NOTREACHED*/
6511
6512 case Q_AARP:
6513 bpf_error(cstate, "'aarp proto' is bogus");
6514 /*NOTREACHED*/
6515
6516 case Q_ISO:
6517 switch (cstate->linktype) {
6518
6519 case DLT_FRELAY:
6520 /*
6521 * Frame Relay packets typically have an OSI
6522 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6523 * generates code to check for all the OSI
6524 * NLPIDs, so calling it and then adding a check
6525 * for the particular NLPID for which we're
6526 * looking is bogus, as we can just check for
6527 * the NLPID.
6528 *
6529 * What we check for is the NLPID and a frame
6530 * control field value of UI, i.e. 0x03 followed
6531 * by the NLPID.
6532 *
6533 * XXX - assumes a 2-byte Frame Relay header with
6534 * DLCI and flags. What if the address is longer?
6535 *
6536 * XXX - what about SNAP-encapsulated frames?
6537 */
6538 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
6539 /*NOTREACHED*/
6540
6541 case DLT_C_HDLC:
6542 /*
6543 * Cisco uses an Ethertype lookalike - for OSI,
6544 * it's 0xfefe.
6545 */
6546 b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
6547 /* OSI in C-HDLC is stuffed with a fudge byte */
6548 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
6549 gen_and(b0, b1);
6550 return b1;
6551
6552 default:
6553 b0 = gen_linktype(cstate, LLCSAP_ISONS);
6554 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
6555 gen_and(b0, b1);
6556 return b1;
6557 }
6558
6559 case Q_ESIS:
6560 bpf_error(cstate, "'esis proto' is bogus");
6561 /*NOTREACHED*/
6562
6563 case Q_ISIS:
6564 b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
6565 /*
6566 * 4 is the offset of the PDU type relative to the IS-IS
6567 * header.
6568 */
6569 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
6570 gen_and(b0, b1);
6571 return b1;
6572
6573 case Q_CLNP:
6574 bpf_error(cstate, "'clnp proto' is not supported");
6575 /*NOTREACHED*/
6576
6577 case Q_STP:
6578 bpf_error(cstate, "'stp proto' is bogus");
6579 /*NOTREACHED*/
6580
6581 case Q_IPX:
6582 bpf_error(cstate, "'ipx proto' is bogus");
6583 /*NOTREACHED*/
6584
6585 case Q_NETBEUI:
6586 bpf_error(cstate, "'netbeui proto' is bogus");
6587 /*NOTREACHED*/
6588
6589 case Q_ISIS_L1:
6590 bpf_error(cstate, "'l1 proto' is bogus");
6591 /*NOTREACHED*/
6592
6593 case Q_ISIS_L2:
6594 bpf_error(cstate, "'l2 proto' is bogus");
6595 /*NOTREACHED*/
6596
6597 case Q_ISIS_IIH:
6598 bpf_error(cstate, "'iih proto' is bogus");
6599 /*NOTREACHED*/
6600
6601 case Q_ISIS_SNP:
6602 bpf_error(cstate, "'snp proto' is bogus");
6603 /*NOTREACHED*/
6604
6605 case Q_ISIS_CSNP:
6606 bpf_error(cstate, "'csnp proto' is bogus");
6607 /*NOTREACHED*/
6608
6609 case Q_ISIS_PSNP:
6610 bpf_error(cstate, "'psnp proto' is bogus");
6611 /*NOTREACHED*/
6612
6613 case Q_ISIS_LSP:
6614 bpf_error(cstate, "'lsp proto' is bogus");
6615 /*NOTREACHED*/
6616
6617 case Q_RADIO:
6618 bpf_error(cstate, "'radio proto' is bogus");
6619 /*NOTREACHED*/
6620
6621 case Q_CARP:
6622 bpf_error(cstate, "'carp proto' is bogus");
6623 /*NOTREACHED*/
6624
6625 default:
6626 abort();
6627 /*NOTREACHED*/
6628 }
6629 /*NOTREACHED*/
6630 }
6631
6632 struct block *
6633 gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
6634 {
6635 int proto = q.proto;
6636 int dir = q.dir;
6637 int tproto;
6638 u_char *eaddr;
6639 bpf_u_int32 mask, addr;
6640 struct addrinfo *res, *res0;
6641 struct sockaddr_in *sin4;
6642 #ifdef INET6
6643 int tproto6;
6644 struct sockaddr_in6 *sin6;
6645 struct in6_addr mask128;
6646 #endif /*INET6*/
6647 struct block *b, *tmp;
6648 int port, real_proto;
6649 int port1, port2;
6650
6651 /*
6652 * Catch errors reported by us and routines below us, and return NULL
6653 * on an error.
6654 */
6655 if (setjmp(cstate->top_ctx))
6656 return (NULL);
6657
6658 switch (q.addr) {
6659
6660 case Q_NET:
6661 addr = pcap_nametonetaddr(name);
6662 if (addr == 0)
6663 bpf_error(cstate, "unknown network '%s'", name);
6664 /* Left justify network addr and calculate its network mask */
6665 mask = 0xffffffff;
6666 while (addr && (addr & 0xff000000) == 0) {
6667 addr <<= 8;
6668 mask <<= 8;
6669 }
6670 return gen_host(cstate, addr, mask, proto, dir, q.addr);
6671
6672 case Q_DEFAULT:
6673 case Q_HOST:
6674 if (proto == Q_LINK) {
6675 switch (cstate->linktype) {
6676
6677 case DLT_EN10MB:
6678 case DLT_NETANALYZER:
6679 case DLT_NETANALYZER_TRANSPARENT:
6680 eaddr = pcap_ether_hostton(name);
6681 if (eaddr == NULL)
6682 bpf_error(cstate,
6683 "unknown ether host '%s'", name);
6684 tmp = gen_prevlinkhdr_check(cstate);
6685 b = gen_ehostop(cstate, eaddr, dir);
6686 if (tmp != NULL)
6687 gen_and(tmp, b);
6688 free(eaddr);
6689 return b;
6690
6691 case DLT_FDDI:
6692 eaddr = pcap_ether_hostton(name);
6693 if (eaddr == NULL)
6694 bpf_error(cstate,
6695 "unknown FDDI host '%s'", name);
6696 b = gen_fhostop(cstate, eaddr, dir);
6697 free(eaddr);
6698 return b;
6699
6700 case DLT_IEEE802:
6701 eaddr = pcap_ether_hostton(name);
6702 if (eaddr == NULL)
6703 bpf_error(cstate,
6704 "unknown token ring host '%s'", name);
6705 b = gen_thostop(cstate, eaddr, dir);
6706 free(eaddr);
6707 return b;
6708
6709 case DLT_IEEE802_11:
6710 case DLT_PRISM_HEADER:
6711 case DLT_IEEE802_11_RADIO_AVS:
6712 case DLT_IEEE802_11_RADIO:
6713 case DLT_PPI:
6714 eaddr = pcap_ether_hostton(name);
6715 if (eaddr == NULL)
6716 bpf_error(cstate,
6717 "unknown 802.11 host '%s'", name);
6718 b = gen_wlanhostop(cstate, eaddr, dir);
6719 free(eaddr);
6720 return b;
6721
6722 case DLT_IP_OVER_FC:
6723 eaddr = pcap_ether_hostton(name);
6724 if (eaddr == NULL)
6725 bpf_error(cstate,
6726 "unknown Fibre Channel host '%s'", name);
6727 b = gen_ipfchostop(cstate, eaddr, dir);
6728 free(eaddr);
6729 return b;
6730 }
6731
6732 bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6733 } else if (proto == Q_DECNET) {
6734 unsigned short dn_addr;
6735
6736 if (!__pcap_nametodnaddr(name, &dn_addr)) {
6737 #ifdef DECNETLIB
6738 bpf_error(cstate, "unknown decnet host name '%s'\n", name);
6739 #else
6740 bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
6741 name);
6742 #endif
6743 }
6744 /*
6745 * I don't think DECNET hosts can be multihomed, so
6746 * there is no need to build up a list of addresses
6747 */
6748 return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
6749 } else {
6750 #ifdef INET6
6751 memset(&mask128, 0xff, sizeof(mask128));
6752 #endif
6753 res0 = res = pcap_nametoaddrinfo(name);
6754 if (res == NULL)
6755 bpf_error(cstate, "unknown host '%s'", name);
6756 cstate->ai = res;
6757 b = tmp = NULL;
6758 tproto = proto;
6759 #ifdef INET6
6760 tproto6 = proto;
6761 #endif
6762 if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
6763 tproto == Q_DEFAULT) {
6764 tproto = Q_IP;
6765 #ifdef INET6
6766 tproto6 = Q_IPV6;
6767 #endif
6768 }
6769 for (res = res0; res; res = res->ai_next) {
6770 switch (res->ai_family) {
6771 case AF_INET:
6772 #ifdef INET6
6773 if (tproto == Q_IPV6)
6774 continue;
6775 #endif
6776
6777 sin4 = (struct sockaddr_in *)
6778 res->ai_addr;
6779 tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
6780 0xffffffff, tproto, dir, q.addr);
6781 break;
6782 #ifdef INET6
6783 case AF_INET6:
6784 if (tproto6 == Q_IP)
6785 continue;
6786
6787 sin6 = (struct sockaddr_in6 *)
6788 res->ai_addr;
6789 tmp = gen_host6(cstate, &sin6->sin6_addr,
6790 &mask128, tproto6, dir, q.addr);
6791 break;
6792 #endif
6793 default:
6794 continue;
6795 }
6796 if (b)
6797 gen_or(b, tmp);
6798 b = tmp;
6799 }
6800 cstate->ai = NULL;
6801 freeaddrinfo(res0);
6802 if (b == NULL) {
6803 bpf_error(cstate, "unknown host '%s'%s", name,
6804 (proto == Q_DEFAULT)
6805 ? ""
6806 : " for specified address family");
6807 }
6808 return b;
6809 }
6810
6811 case Q_PORT:
6812 if (proto != Q_DEFAULT &&
6813 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6814 bpf_error(cstate, "illegal qualifier of 'port'");
6815 if (pcap_nametoport(name, &port, &real_proto) == 0)
6816 bpf_error(cstate, "unknown port '%s'", name);
6817 if (proto == Q_UDP) {
6818 if (real_proto == IPPROTO_TCP)
6819 bpf_error(cstate, "port '%s' is tcp", name);
6820 else if (real_proto == IPPROTO_SCTP)
6821 bpf_error(cstate, "port '%s' is sctp", name);
6822 else
6823 /* override PROTO_UNDEF */
6824 real_proto = IPPROTO_UDP;
6825 }
6826 if (proto == Q_TCP) {
6827 if (real_proto == IPPROTO_UDP)
6828 bpf_error(cstate, "port '%s' is udp", name);
6829
6830 else if (real_proto == IPPROTO_SCTP)
6831 bpf_error(cstate, "port '%s' is sctp", name);
6832 else
6833 /* override PROTO_UNDEF */
6834 real_proto = IPPROTO_TCP;
6835 }
6836 if (proto == Q_SCTP) {
6837 if (real_proto == IPPROTO_UDP)
6838 bpf_error(cstate, "port '%s' is udp", name);
6839
6840 else if (real_proto == IPPROTO_TCP)
6841 bpf_error(cstate, "port '%s' is tcp", name);
6842 else
6843 /* override PROTO_UNDEF */
6844 real_proto = IPPROTO_SCTP;
6845 }
6846 if (port < 0)
6847 bpf_error(cstate, "illegal port number %d < 0", port);
6848 if (port > 65535)
6849 bpf_error(cstate, "illegal port number %d > 65535", port);
6850 b = gen_port(cstate, port, real_proto, dir);
6851 gen_or(gen_port6(cstate, port, real_proto, dir), b);
6852 return b;
6853
6854 case Q_PORTRANGE:
6855 if (proto != Q_DEFAULT &&
6856 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6857 bpf_error(cstate, "illegal qualifier of 'portrange'");
6858 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6859 bpf_error(cstate, "unknown port in range '%s'", name);
6860 if (proto == Q_UDP) {
6861 if (real_proto == IPPROTO_TCP)
6862 bpf_error(cstate, "port in range '%s' is tcp", name);
6863 else if (real_proto == IPPROTO_SCTP)
6864 bpf_error(cstate, "port in range '%s' is sctp", name);
6865 else
6866 /* override PROTO_UNDEF */
6867 real_proto = IPPROTO_UDP;
6868 }
6869 if (proto == Q_TCP) {
6870 if (real_proto == IPPROTO_UDP)
6871 bpf_error(cstate, "port in range '%s' is udp", name);
6872 else if (real_proto == IPPROTO_SCTP)
6873 bpf_error(cstate, "port in range '%s' is sctp", name);
6874 else
6875 /* override PROTO_UNDEF */
6876 real_proto = IPPROTO_TCP;
6877 }
6878 if (proto == Q_SCTP) {
6879 if (real_proto == IPPROTO_UDP)
6880 bpf_error(cstate, "port in range '%s' is udp", name);
6881 else if (real_proto == IPPROTO_TCP)
6882 bpf_error(cstate, "port in range '%s' is tcp", name);
6883 else
6884 /* override PROTO_UNDEF */
6885 real_proto = IPPROTO_SCTP;
6886 }
6887 if (port1 < 0)
6888 bpf_error(cstate, "illegal port number %d < 0", port1);
6889 if (port1 > 65535)
6890 bpf_error(cstate, "illegal port number %d > 65535", port1);
6891 if (port2 < 0)
6892 bpf_error(cstate, "illegal port number %d < 0", port2);
6893 if (port2 > 65535)
6894 bpf_error(cstate, "illegal port number %d > 65535", port2);
6895
6896 b = gen_portrange(cstate, port1, port2, real_proto, dir);
6897 gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
6898 return b;
6899
6900 case Q_GATEWAY:
6901 #ifndef INET6
6902 eaddr = pcap_ether_hostton(name);
6903 if (eaddr == NULL)
6904 bpf_error(cstate, "unknown ether host: %s", name);
6905
6906 res = pcap_nametoaddrinfo(name);
6907 cstate->ai = res;
6908 if (res == NULL)
6909 bpf_error(cstate, "unknown host '%s'", name);
6910 b = gen_gateway(cstate, eaddr, res, proto, dir);
6911 cstate->ai = NULL;
6912 freeaddrinfo(res);
6913 if (b == NULL)
6914 bpf_error(cstate, "unknown host '%s'", name);
6915 return b;
6916 #else
6917 bpf_error(cstate, "'gateway' not supported in this configuration");
6918 #endif /*INET6*/
6919
6920 case Q_PROTO:
6921 real_proto = lookup_proto(cstate, name, proto);
6922 if (real_proto >= 0)
6923 return gen_proto(cstate, real_proto, proto, dir);
6924 else
6925 bpf_error(cstate, "unknown protocol: %s", name);
6926
6927 case Q_PROTOCHAIN:
6928 real_proto = lookup_proto(cstate, name, proto);
6929 if (real_proto >= 0)
6930 return gen_protochain(cstate, real_proto, proto);
6931 else
6932 bpf_error(cstate, "unknown protocol: %s", name);
6933
6934 case Q_UNDEF:
6935 syntax(cstate);
6936 /*NOTREACHED*/
6937 }
6938 abort();
6939 /*NOTREACHED*/
6940 }
6941
6942 struct block *
6943 gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
6944 bpf_u_int32 masklen, struct qual q)
6945 {
6946 register int nlen, mlen;
6947 bpf_u_int32 n, m;
6948
6949 /*
6950 * Catch errors reported by us and routines below us, and return NULL
6951 * on an error.
6952 */
6953 if (setjmp(cstate->top_ctx))
6954 return (NULL);
6955
6956 nlen = __pcap_atoin(s1, &n);
6957 /* Promote short ipaddr */
6958 n <<= 32 - nlen;
6959
6960 if (s2 != NULL) {
6961 mlen = __pcap_atoin(s2, &m);
6962 /* Promote short ipaddr */
6963 m <<= 32 - mlen;
6964 if ((n & ~m) != 0)
6965 bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
6966 s1, s2);
6967 } else {
6968 /* Convert mask len to mask */
6969 if (masklen > 32)
6970 bpf_error(cstate, "mask length must be <= 32");
6971 if (masklen == 0) {
6972 /*
6973 * X << 32 is not guaranteed by C to be 0; it's
6974 * undefined.
6975 */
6976 m = 0;
6977 } else
6978 m = 0xffffffff << (32 - masklen);
6979 if ((n & ~m) != 0)
6980 bpf_error(cstate, "non-network bits set in \"%s/%d\"",
6981 s1, masklen);
6982 }
6983
6984 switch (q.addr) {
6985
6986 case Q_NET:
6987 return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
6988
6989 default:
6990 bpf_error(cstate, "Mask syntax for networks only");
6991 /*NOTREACHED*/
6992 }
6993 /*NOTREACHED*/
6994 }
6995
6996 struct block *
6997 gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
6998 {
6999 bpf_u_int32 mask;
7000 int proto;
7001 int dir;
7002 register int vlen;
7003
7004 /*
7005 * Catch errors reported by us and routines below us, and return NULL
7006 * on an error.
7007 */
7008 if (setjmp(cstate->top_ctx))
7009 return (NULL);
7010
7011 proto = q.proto;
7012 dir = q.dir;
7013 if (s == NULL)
7014 vlen = 32;
7015 else if (q.proto == Q_DECNET) {
7016 vlen = __pcap_atodn(s, &v);
7017 if (vlen == 0)
7018 bpf_error(cstate, "malformed decnet address '%s'", s);
7019 } else
7020 vlen = __pcap_atoin(s, &v);
7021
7022 switch (q.addr) {
7023
7024 case Q_DEFAULT:
7025 case Q_HOST:
7026 case Q_NET:
7027 if (proto == Q_DECNET)
7028 return gen_host(cstate, v, 0, proto, dir, q.addr);
7029 else if (proto == Q_LINK) {
7030 bpf_error(cstate, "illegal link layer address");
7031 } else {
7032 mask = 0xffffffff;
7033 if (s == NULL && q.addr == Q_NET) {
7034 /* Promote short net number */
7035 while (v && (v & 0xff000000) == 0) {
7036 v <<= 8;
7037 mask <<= 8;
7038 }
7039 } else {
7040 /* Promote short ipaddr */
7041 v <<= 32 - vlen;
7042 mask <<= 32 - vlen ;
7043 }
7044 return gen_host(cstate, v, mask, proto, dir, q.addr);
7045 }
7046
7047 case Q_PORT:
7048 if (proto == Q_UDP)
7049 proto = IPPROTO_UDP;
7050 else if (proto == Q_TCP)
7051 proto = IPPROTO_TCP;
7052 else if (proto == Q_SCTP)
7053 proto = IPPROTO_SCTP;
7054 else if (proto == Q_DEFAULT)
7055 proto = PROTO_UNDEF;
7056 else
7057 bpf_error(cstate, "illegal qualifier of 'port'");
7058
7059 if (v > 65535)
7060 bpf_error(cstate, "illegal port number %u > 65535", v);
7061
7062 {
7063 struct block *b;
7064 b = gen_port(cstate, v, proto, dir);
7065 gen_or(gen_port6(cstate, v, proto, dir), b);
7066 return b;
7067 }
7068
7069 case Q_PORTRANGE:
7070 if (proto == Q_UDP)
7071 proto = IPPROTO_UDP;
7072 else if (proto == Q_TCP)
7073 proto = IPPROTO_TCP;
7074 else if (proto == Q_SCTP)
7075 proto = IPPROTO_SCTP;
7076 else if (proto == Q_DEFAULT)
7077 proto = PROTO_UNDEF;
7078 else
7079 bpf_error(cstate, "illegal qualifier of 'portrange'");
7080
7081 if (v > 65535)
7082 bpf_error(cstate, "illegal port number %u > 65535", v);
7083
7084 {
7085 struct block *b;
7086 b = gen_portrange(cstate, v, v, proto, dir);
7087 gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
7088 return b;
7089 }
7090
7091 case Q_GATEWAY:
7092 bpf_error(cstate, "'gateway' requires a name");
7093 /*NOTREACHED*/
7094
7095 case Q_PROTO:
7096 return gen_proto(cstate, v, proto, dir);
7097
7098 case Q_PROTOCHAIN:
7099 return gen_protochain(cstate, v, proto);
7100
7101 case Q_UNDEF:
7102 syntax(cstate);
7103 /*NOTREACHED*/
7104
7105 default:
7106 abort();
7107 /*NOTREACHED*/
7108 }
7109 /*NOTREACHED*/
7110 }
7111
7112 #ifdef INET6
7113 struct block *
7114 gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
7115 bpf_u_int32 masklen, struct qual q)
7116 {
7117 struct addrinfo *res;
7118 struct in6_addr *addr;
7119 struct in6_addr mask;
7120 struct block *b;
7121 uint32_t *a, *m;
7122
7123 /*
7124 * Catch errors reported by us and routines below us, and return NULL
7125 * on an error.
7126 */
7127 if (setjmp(cstate->top_ctx))
7128 return (NULL);
7129
7130 if (s2)
7131 bpf_error(cstate, "no mask %s supported", s2);
7132
7133 res = pcap_nametoaddrinfo(s1);
7134 if (!res)
7135 bpf_error(cstate, "invalid ip6 address %s", s1);
7136 cstate->ai = res;
7137 if (res->ai_next)
7138 bpf_error(cstate, "%s resolved to multiple address", s1);
7139 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
7140
7141 if (sizeof(mask) * 8 < masklen)
7142 bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
7143 memset(&mask, 0, sizeof(mask));
7144 memset(&mask, 0xff, masklen / 8);
7145 if (masklen % 8) {
7146 mask.s6_addr[masklen / 8] =
7147 (0xff << (8 - masklen % 8)) & 0xff;
7148 }
7149
7150 a = (uint32_t *)addr;
7151 m = (uint32_t *)&mask;
7152 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
7153 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
7154 bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
7155 }
7156
7157 switch (q.addr) {
7158
7159 case Q_DEFAULT:
7160 case Q_HOST:
7161 if (masklen != 128)
7162 bpf_error(cstate, "Mask syntax for networks only");
7163 /* FALLTHROUGH */
7164
7165 case Q_NET:
7166 b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
7167 cstate->ai = NULL;
7168 freeaddrinfo(res);
7169 return b;
7170
7171 default:
7172 bpf_error(cstate, "invalid qualifier against IPv6 address");
7173 /*NOTREACHED*/
7174 }
7175 }
7176 #endif /*INET6*/
7177
7178 struct block *
7179 gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
7180 {
7181 struct block *b, *tmp;
7182
7183 /*
7184 * Catch errors reported by us and routines below us, and return NULL
7185 * on an error.
7186 */
7187 if (setjmp(cstate->top_ctx))
7188 return (NULL);
7189
7190 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
7191 cstate->e = pcap_ether_aton(s);
7192 if (cstate->e == NULL)
7193 bpf_error(cstate, "malloc");
7194 switch (cstate->linktype) {
7195 case DLT_EN10MB:
7196 case DLT_NETANALYZER:
7197 case DLT_NETANALYZER_TRANSPARENT:
7198 tmp = gen_prevlinkhdr_check(cstate);
7199 b = gen_ehostop(cstate, cstate->e, (int)q.dir);
7200 if (tmp != NULL)
7201 gen_and(tmp, b);
7202 break;
7203 case DLT_FDDI:
7204 b = gen_fhostop(cstate, cstate->e, (int)q.dir);
7205 break;
7206 case DLT_IEEE802:
7207 b = gen_thostop(cstate, cstate->e, (int)q.dir);
7208 break;
7209 case DLT_IEEE802_11:
7210 case DLT_PRISM_HEADER:
7211 case DLT_IEEE802_11_RADIO_AVS:
7212 case DLT_IEEE802_11_RADIO:
7213 case DLT_PPI:
7214 b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
7215 break;
7216 case DLT_IP_OVER_FC:
7217 b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
7218 break;
7219 default:
7220 free(cstate->e);
7221 cstate->e = NULL;
7222 bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7223 /*NOTREACHED*/
7224 }
7225 free(cstate->e);
7226 cstate->e = NULL;
7227 return (b);
7228 }
7229 bpf_error(cstate, "ethernet address used in non-ether expression");
7230 /*NOTREACHED*/
7231 }
7232
7233 void
7234 sappend(struct slist *s0, struct slist *s1)
7235 {
7236 /*
7237 * This is definitely not the best way to do this, but the
7238 * lists will rarely get long.
7239 */
7240 while (s0->next)
7241 s0 = s0->next;
7242 s0->next = s1;
7243 }
7244
7245 static struct slist *
7246 xfer_to_x(compiler_state_t *cstate, struct arth *a)
7247 {
7248 struct slist *s;
7249
7250 s = new_stmt(cstate, BPF_LDX|BPF_MEM);
7251 s->s.k = a->regno;
7252 return s;
7253 }
7254
7255 static struct slist *
7256 xfer_to_a(compiler_state_t *cstate, struct arth *a)
7257 {
7258 struct slist *s;
7259
7260 s = new_stmt(cstate, BPF_LD|BPF_MEM);
7261 s->s.k = a->regno;
7262 return s;
7263 }
7264
7265 /*
7266 * Modify "index" to use the value stored into its register as an
7267 * offset relative to the beginning of the header for the protocol
7268 * "proto", and allocate a register and put an item "size" bytes long
7269 * (1, 2, or 4) at that offset into that register, making it the register
7270 * for "index".
7271 */
7272 static struct arth *
7273 gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
7274 bpf_u_int32 size)
7275 {
7276 int size_code;
7277 struct slist *s, *tmp;
7278 struct block *b;
7279 int regno = alloc_reg(cstate);
7280
7281 free_reg(cstate, inst->regno);
7282 switch (size) {
7283
7284 default:
7285 bpf_error(cstate, "data size must be 1, 2, or 4");
7286 /*NOTREACHED*/
7287
7288 case 1:
7289 size_code = BPF_B;
7290 break;
7291
7292 case 2:
7293 size_code = BPF_H;
7294 break;
7295
7296 case 4:
7297 size_code = BPF_W;
7298 break;
7299 }
7300 switch (proto) {
7301 default:
7302 bpf_error(cstate, "unsupported index operation");
7303
7304 case Q_RADIO:
7305 /*
7306 * The offset is relative to the beginning of the packet
7307 * data, if we have a radio header. (If we don't, this
7308 * is an error.)
7309 */
7310 if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
7311 cstate->linktype != DLT_IEEE802_11_RADIO &&
7312 cstate->linktype != DLT_PRISM_HEADER)
7313 bpf_error(cstate, "radio information not present in capture");
7314
7315 /*
7316 * Load into the X register the offset computed into the
7317 * register specified by "index".
7318 */
7319 s = xfer_to_x(cstate, inst);
7320
7321 /*
7322 * Load the item at that offset.
7323 */
7324 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7325 sappend(s, tmp);
7326 sappend(inst->s, s);
7327 break;
7328
7329 case Q_LINK:
7330 /*
7331 * The offset is relative to the beginning of
7332 * the link-layer header.
7333 *
7334 * XXX - what about ATM LANE? Should the index be
7335 * relative to the beginning of the AAL5 frame, so
7336 * that 0 refers to the beginning of the LE Control
7337 * field, or relative to the beginning of the LAN
7338 * frame, so that 0 refers, for Ethernet LANE, to
7339 * the beginning of the destination address?
7340 */
7341 s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
7342
7343 /*
7344 * If "s" is non-null, it has code to arrange that the
7345 * X register contains the length of the prefix preceding
7346 * the link-layer header. Add to it the offset computed
7347 * into the register specified by "index", and move that
7348 * into the X register. Otherwise, just load into the X
7349 * register the offset computed into the register specified
7350 * by "index".
7351 */
7352 if (s != NULL) {
7353 sappend(s, xfer_to_a(cstate, inst));
7354 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7355 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7356 } else
7357 s = xfer_to_x(cstate, inst);
7358
7359 /*
7360 * Load the item at the sum of the offset we've put in the
7361 * X register and the offset of the start of the link
7362 * layer header (which is 0 if the radio header is
7363 * variable-length; that header length is what we put
7364 * into the X register and then added to the index).
7365 */
7366 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7367 tmp->s.k = cstate->off_linkhdr.constant_part;
7368 sappend(s, tmp);
7369 sappend(inst->s, s);
7370 break;
7371
7372 case Q_IP:
7373 case Q_ARP:
7374 case Q_RARP:
7375 case Q_ATALK:
7376 case Q_DECNET:
7377 case Q_SCA:
7378 case Q_LAT:
7379 case Q_MOPRC:
7380 case Q_MOPDL:
7381 case Q_IPV6:
7382 /*
7383 * The offset is relative to the beginning of
7384 * the network-layer header.
7385 * XXX - are there any cases where we want
7386 * cstate->off_nl_nosnap?
7387 */
7388 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7389
7390 /*
7391 * If "s" is non-null, it has code to arrange that the
7392 * X register contains the variable part of the offset
7393 * of the link-layer payload. Add to it the offset
7394 * computed into the register specified by "index",
7395 * and move that into the X register. Otherwise, just
7396 * load into the X register the offset computed into
7397 * the register specified by "index".
7398 */
7399 if (s != NULL) {
7400 sappend(s, xfer_to_a(cstate, inst));
7401 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7402 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7403 } else
7404 s = xfer_to_x(cstate, inst);
7405
7406 /*
7407 * Load the item at the sum of the offset we've put in the
7408 * X register, the offset of the start of the network
7409 * layer header from the beginning of the link-layer
7410 * payload, and the constant part of the offset of the
7411 * start of the link-layer payload.
7412 */
7413 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7414 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7415 sappend(s, tmp);
7416 sappend(inst->s, s);
7417
7418 /*
7419 * Do the computation only if the packet contains
7420 * the protocol in question.
7421 */
7422 b = gen_proto_abbrev_internal(cstate, proto);
7423 if (inst->b)
7424 gen_and(inst->b, b);
7425 inst->b = b;
7426 break;
7427
7428 case Q_SCTP:
7429 case Q_TCP:
7430 case Q_UDP:
7431 case Q_ICMP:
7432 case Q_IGMP:
7433 case Q_IGRP:
7434 case Q_PIM:
7435 case Q_VRRP:
7436 case Q_CARP:
7437 /*
7438 * The offset is relative to the beginning of
7439 * the transport-layer header.
7440 *
7441 * Load the X register with the length of the IPv4 header
7442 * (plus the offset of the link-layer header, if it's
7443 * a variable-length header), in bytes.
7444 *
7445 * XXX - are there any cases where we want
7446 * cstate->off_nl_nosnap?
7447 * XXX - we should, if we're built with
7448 * IPv6 support, generate code to load either
7449 * IPv4, IPv6, or both, as appropriate.
7450 */
7451 s = gen_loadx_iphdrlen(cstate);
7452
7453 /*
7454 * The X register now contains the sum of the variable
7455 * part of the offset of the link-layer payload and the
7456 * length of the network-layer header.
7457 *
7458 * Load into the A register the offset relative to
7459 * the beginning of the transport layer header,
7460 * add the X register to that, move that to the
7461 * X register, and load with an offset from the
7462 * X register equal to the sum of the constant part of
7463 * the offset of the link-layer payload and the offset,
7464 * relative to the beginning of the link-layer payload,
7465 * of the network-layer header.
7466 */
7467 sappend(s, xfer_to_a(cstate, inst));
7468 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7469 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7470 sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
7471 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
7472 sappend(inst->s, s);
7473
7474 /*
7475 * Do the computation only if the packet contains
7476 * the protocol in question - which is true only
7477 * if this is an IP datagram and is the first or
7478 * only fragment of that datagram.
7479 */
7480 gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
7481 if (inst->b)
7482 gen_and(inst->b, b);
7483 gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
7484 inst->b = b;
7485 break;
7486 case Q_ICMPV6:
7487 /*
7488 * Do the computation only if the packet contains
7489 * the protocol in question.
7490 */
7491 b = gen_proto_abbrev_internal(cstate, Q_IPV6);
7492 if (inst->b) {
7493 gen_and(inst->b, b);
7494 }
7495 inst->b = b;
7496
7497 /*
7498 * Check if we have an icmp6 next header
7499 */
7500 b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
7501 if (inst->b) {
7502 gen_and(inst->b, b);
7503 }
7504 inst->b = b;
7505
7506
7507 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
7508 /*
7509 * If "s" is non-null, it has code to arrange that the
7510 * X register contains the variable part of the offset
7511 * of the link-layer payload. Add to it the offset
7512 * computed into the register specified by "index",
7513 * and move that into the X register. Otherwise, just
7514 * load into the X register the offset computed into
7515 * the register specified by "index".
7516 */
7517 if (s != NULL) {
7518 sappend(s, xfer_to_a(cstate, inst));
7519 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
7520 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
7521 } else {
7522 s = xfer_to_x(cstate, inst);
7523 }
7524
7525 /*
7526 * Load the item at the sum of the offset we've put in the
7527 * X register, the offset of the start of the network
7528 * layer header from the beginning of the link-layer
7529 * payload, and the constant part of the offset of the
7530 * start of the link-layer payload.
7531 */
7532 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
7533 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
7534
7535 sappend(s, tmp);
7536 sappend(inst->s, s);
7537
7538 break;
7539 }
7540 inst->regno = regno;
7541 s = new_stmt(cstate, BPF_ST);
7542 s->s.k = regno;
7543 sappend(inst->s, s);
7544
7545 return inst;
7546 }
7547
7548 struct arth *
7549 gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
7550 bpf_u_int32 size)
7551 {
7552 /*
7553 * Catch errors reported by us and routines below us, and return NULL
7554 * on an error.
7555 */
7556 if (setjmp(cstate->top_ctx))
7557 return (NULL);
7558
7559 return gen_load_internal(cstate, proto, inst, size);
7560 }
7561
7562 static struct block *
7563 gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
7564 struct arth *a1, int reversed)
7565 {
7566 struct slist *s0, *s1, *s2;
7567 struct block *b, *tmp;
7568
7569 s0 = xfer_to_x(cstate, a1);
7570 s1 = xfer_to_a(cstate, a0);
7571 if (code == BPF_JEQ) {
7572 s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
7573 b = new_block(cstate, JMP(code));
7574 sappend(s1, s2);
7575 }
7576 else
7577 b = new_block(cstate, BPF_JMP|code|BPF_X);
7578 if (reversed)
7579 gen_not(b);
7580
7581 sappend(s0, s1);
7582 sappend(a1->s, s0);
7583 sappend(a0->s, a1->s);
7584
7585 b->stmts = a0->s;
7586
7587 free_reg(cstate, a0->regno);
7588 free_reg(cstate, a1->regno);
7589
7590 /* 'and' together protocol checks */
7591 if (a0->b) {
7592 if (a1->b) {
7593 gen_and(a0->b, tmp = a1->b);
7594 }
7595 else
7596 tmp = a0->b;
7597 } else
7598 tmp = a1->b;
7599
7600 if (tmp)
7601 gen_and(tmp, b);
7602
7603 return b;
7604 }
7605
7606 struct block *
7607 gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
7608 struct arth *a1, int reversed)
7609 {
7610 /*
7611 * Catch errors reported by us and routines below us, and return NULL
7612 * on an error.
7613 */
7614 if (setjmp(cstate->top_ctx))
7615 return (NULL);
7616
7617 return gen_relation_internal(cstate, code, a0, a1, reversed);
7618 }
7619
7620 struct arth *
7621 gen_loadlen(compiler_state_t *cstate)
7622 {
7623 int regno;
7624 struct arth *a;
7625 struct slist *s;
7626
7627 /*
7628 * Catch errors reported by us and routines below us, and return NULL
7629 * on an error.
7630 */
7631 if (setjmp(cstate->top_ctx))
7632 return (NULL);
7633
7634 regno = alloc_reg(cstate);
7635 a = (struct arth *)newchunk(cstate, sizeof(*a));
7636 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7637 s->next = new_stmt(cstate, BPF_ST);
7638 s->next->s.k = regno;
7639 a->s = s;
7640 a->regno = regno;
7641
7642 return a;
7643 }
7644
7645 static struct arth *
7646 gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
7647 {
7648 struct arth *a;
7649 struct slist *s;
7650 int reg;
7651
7652 a = (struct arth *)newchunk(cstate, sizeof(*a));
7653
7654 reg = alloc_reg(cstate);
7655
7656 s = new_stmt(cstate, BPF_LD|BPF_IMM);
7657 s->s.k = val;
7658 s->next = new_stmt(cstate, BPF_ST);
7659 s->next->s.k = reg;
7660 a->s = s;
7661 a->regno = reg;
7662
7663 return a;
7664 }
7665
7666 struct arth *
7667 gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
7668 {
7669 /*
7670 * Catch errors reported by us and routines below us, and return NULL
7671 * on an error.
7672 */
7673 if (setjmp(cstate->top_ctx))
7674 return (NULL);
7675
7676 return gen_loadi_internal(cstate, val);
7677 }
7678
7679 /*
7680 * The a_arg dance is to avoid annoying whining by compilers that
7681 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7682 * It's not *used* after setjmp returns.
7683 */
7684 struct arth *
7685 gen_neg(compiler_state_t *cstate, struct arth *a_arg)
7686 {
7687 struct arth *a = a_arg;
7688 struct slist *s;
7689
7690 /*
7691 * Catch errors reported by us and routines below us, and return NULL
7692 * on an error.
7693 */
7694 if (setjmp(cstate->top_ctx))
7695 return (NULL);
7696
7697 s = xfer_to_a(cstate, a);
7698 sappend(a->s, s);
7699 s = new_stmt(cstate, BPF_ALU|BPF_NEG);
7700 s->s.k = 0;
7701 sappend(a->s, s);
7702 s = new_stmt(cstate, BPF_ST);
7703 s->s.k = a->regno;
7704 sappend(a->s, s);
7705
7706 return a;
7707 }
7708
7709 /*
7710 * The a0_arg dance is to avoid annoying whining by compilers that
7711 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7712 * It's not *used* after setjmp returns.
7713 */
7714 struct arth *
7715 gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
7716 struct arth *a1)
7717 {
7718 struct arth *a0 = a0_arg;
7719 struct slist *s0, *s1, *s2;
7720
7721 /*
7722 * Catch errors reported by us and routines below us, and return NULL
7723 * on an error.
7724 */
7725 if (setjmp(cstate->top_ctx))
7726 return (NULL);
7727
7728 /*
7729 * Disallow division by, or modulus by, zero; we do this here
7730 * so that it gets done even if the optimizer is disabled.
7731 *
7732 * Also disallow shifts by a value greater than 31; we do this
7733 * here, for the same reason.
7734 */
7735 if (code == BPF_DIV) {
7736 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7737 bpf_error(cstate, "division by zero");
7738 } else if (code == BPF_MOD) {
7739 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
7740 bpf_error(cstate, "modulus by zero");
7741 } else if (code == BPF_LSH || code == BPF_RSH) {
7742 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)
7743 bpf_error(cstate, "shift by more than 31 bits");
7744 }
7745 s0 = xfer_to_x(cstate, a1);
7746 s1 = xfer_to_a(cstate, a0);
7747 s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
7748
7749 sappend(s1, s2);
7750 sappend(s0, s1);
7751 sappend(a1->s, s0);
7752 sappend(a0->s, a1->s);
7753
7754 free_reg(cstate, a0->regno);
7755 free_reg(cstate, a1->regno);
7756
7757 s0 = new_stmt(cstate, BPF_ST);
7758 a0->regno = s0->s.k = alloc_reg(cstate);
7759 sappend(a0->s, s0);
7760
7761 return a0;
7762 }
7763
7764 /*
7765 * Initialize the table of used registers and the current register.
7766 */
7767 static void
7768 init_regs(compiler_state_t *cstate)
7769 {
7770 cstate->curreg = 0;
7771 memset(cstate->regused, 0, sizeof cstate->regused);
7772 }
7773
7774 /*
7775 * Return the next free register.
7776 */
7777 static int
7778 alloc_reg(compiler_state_t *cstate)
7779 {
7780 int n = BPF_MEMWORDS;
7781
7782 while (--n >= 0) {
7783 if (cstate->regused[cstate->curreg])
7784 cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
7785 else {
7786 cstate->regused[cstate->curreg] = 1;
7787 return cstate->curreg;
7788 }
7789 }
7790 bpf_error(cstate, "too many registers needed to evaluate expression");
7791 /*NOTREACHED*/
7792 }
7793
7794 /*
7795 * Return a register to the table so it can
7796 * be used later.
7797 */
7798 static void
7799 free_reg(compiler_state_t *cstate, int n)
7800 {
7801 cstate->regused[n] = 0;
7802 }
7803
7804 static struct block *
7805 gen_len(compiler_state_t *cstate, int jmp, int n)
7806 {
7807 struct slist *s;
7808 struct block *b;
7809
7810 s = new_stmt(cstate, BPF_LD|BPF_LEN);
7811 b = new_block(cstate, JMP(jmp));
7812 b->stmts = s;
7813 b->s.k = n;
7814
7815 return b;
7816 }
7817
7818 struct block *
7819 gen_greater(compiler_state_t *cstate, int n)
7820 {
7821 /*
7822 * Catch errors reported by us and routines below us, and return NULL
7823 * on an error.
7824 */
7825 if (setjmp(cstate->top_ctx))
7826 return (NULL);
7827
7828 return gen_len(cstate, BPF_JGE, n);
7829 }
7830
7831 /*
7832 * Actually, this is less than or equal.
7833 */
7834 struct block *
7835 gen_less(compiler_state_t *cstate, int n)
7836 {
7837 struct block *b;
7838
7839 /*
7840 * Catch errors reported by us and routines below us, and return NULL
7841 * on an error.
7842 */
7843 if (setjmp(cstate->top_ctx))
7844 return (NULL);
7845
7846 b = gen_len(cstate, BPF_JGT, n);
7847 gen_not(b);
7848
7849 return b;
7850 }
7851
7852 /*
7853 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7854 * the beginning of the link-layer header.
7855 * XXX - that means you can't test values in the radiotap header, but
7856 * as that header is difficult if not impossible to parse generally
7857 * without a loop, that might not be a severe problem. A new keyword
7858 * "radio" could be added for that, although what you'd really want
7859 * would be a way of testing particular radio header values, which
7860 * would generate code appropriate to the radio header in question.
7861 */
7862 struct block *
7863 gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
7864 {
7865 struct block *b;
7866 struct slist *s;
7867
7868 /*
7869 * Catch errors reported by us and routines below us, and return NULL
7870 * on an error.
7871 */
7872 if (setjmp(cstate->top_ctx))
7873 return (NULL);
7874
7875 switch (op) {
7876 default:
7877 abort();
7878
7879 case '=':
7880 return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7881
7882 case '<':
7883 b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7884 return b;
7885
7886 case '>':
7887 b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
7888 return b;
7889
7890 case '|':
7891 s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
7892 break;
7893
7894 case '&':
7895 s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
7896 break;
7897 }
7898 s->s.k = val;
7899 b = new_block(cstate, JMP(BPF_JEQ));
7900 b->stmts = s;
7901 gen_not(b);
7902
7903 return b;
7904 }
7905
7906 static const u_char abroadcast[] = { 0x0 };
7907
7908 struct block *
7909 gen_broadcast(compiler_state_t *cstate, int proto)
7910 {
7911 bpf_u_int32 hostmask;
7912 struct block *b0, *b1, *b2;
7913 static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7914
7915 /*
7916 * Catch errors reported by us and routines below us, and return NULL
7917 * on an error.
7918 */
7919 if (setjmp(cstate->top_ctx))
7920 return (NULL);
7921
7922 switch (proto) {
7923
7924 case Q_DEFAULT:
7925 case Q_LINK:
7926 switch (cstate->linktype) {
7927 case DLT_ARCNET:
7928 case DLT_ARCNET_LINUX:
7929 return gen_ahostop(cstate, abroadcast, Q_DST);
7930 case DLT_EN10MB:
7931 case DLT_NETANALYZER:
7932 case DLT_NETANALYZER_TRANSPARENT:
7933 b1 = gen_prevlinkhdr_check(cstate);
7934 b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
7935 if (b1 != NULL)
7936 gen_and(b1, b0);
7937 return b0;
7938 case DLT_FDDI:
7939 return gen_fhostop(cstate, ebroadcast, Q_DST);
7940 case DLT_IEEE802:
7941 return gen_thostop(cstate, ebroadcast, Q_DST);
7942 case DLT_IEEE802_11:
7943 case DLT_PRISM_HEADER:
7944 case DLT_IEEE802_11_RADIO_AVS:
7945 case DLT_IEEE802_11_RADIO:
7946 case DLT_PPI:
7947 return gen_wlanhostop(cstate, ebroadcast, Q_DST);
7948 case DLT_IP_OVER_FC:
7949 return gen_ipfchostop(cstate, ebroadcast, Q_DST);
7950 default:
7951 bpf_error(cstate, "not a broadcast link");
7952 }
7953 /*NOTREACHED*/
7954
7955 case Q_IP:
7956 /*
7957 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7958 * as an indication that we don't know the netmask, and fail
7959 * in that case.
7960 */
7961 if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
7962 bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
7963 b0 = gen_linktype(cstate, ETHERTYPE_IP);
7964 hostmask = ~cstate->netmask;
7965 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
7966 b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
7967 ~0 & hostmask, hostmask);
7968 gen_or(b1, b2);
7969 gen_and(b0, b2);
7970 return b2;
7971 }
7972 bpf_error(cstate, "only link-layer/IP broadcast filters supported");
7973 /*NOTREACHED*/
7974 }
7975
7976 /*
7977 * Generate code to test the low-order bit of a MAC address (that's
7978 * the bottom bit of the *first* byte).
7979 */
7980 static struct block *
7981 gen_mac_multicast(compiler_state_t *cstate, int offset)
7982 {
7983 register struct block *b0;
7984 register struct slist *s;
7985
7986 /* link[offset] & 1 != 0 */
7987 s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
7988 b0 = new_block(cstate, JMP(BPF_JSET));
7989 b0->s.k = 1;
7990 b0->stmts = s;
7991 return b0;
7992 }
7993
7994 struct block *
7995 gen_multicast(compiler_state_t *cstate, int proto)
7996 {
7997 register struct block *b0, *b1, *b2;
7998 register struct slist *s;
7999
8000 /*
8001 * Catch errors reported by us and routines below us, and return NULL
8002 * on an error.
8003 */
8004 if (setjmp(cstate->top_ctx))
8005 return (NULL);
8006
8007 switch (proto) {
8008
8009 case Q_DEFAULT:
8010 case Q_LINK:
8011 switch (cstate->linktype) {
8012 case DLT_ARCNET:
8013 case DLT_ARCNET_LINUX:
8014 /* all ARCnet multicasts use the same address */
8015 return gen_ahostop(cstate, abroadcast, Q_DST);
8016 case DLT_EN10MB:
8017 case DLT_NETANALYZER:
8018 case DLT_NETANALYZER_TRANSPARENT:
8019 b1 = gen_prevlinkhdr_check(cstate);
8020 /* ether[0] & 1 != 0 */
8021 b0 = gen_mac_multicast(cstate, 0);
8022 if (b1 != NULL)
8023 gen_and(b1, b0);
8024 return b0;
8025 case DLT_FDDI:
8026 /*
8027 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8028 *
8029 * XXX - was that referring to bit-order issues?
8030 */
8031 /* fddi[1] & 1 != 0 */
8032 return gen_mac_multicast(cstate, 1);
8033 case DLT_IEEE802:
8034 /* tr[2] & 1 != 0 */
8035 return gen_mac_multicast(cstate, 2);
8036 case DLT_IEEE802_11:
8037 case DLT_PRISM_HEADER:
8038 case DLT_IEEE802_11_RADIO_AVS:
8039 case DLT_IEEE802_11_RADIO:
8040 case DLT_PPI:
8041 /*
8042 * Oh, yuk.
8043 *
8044 * For control frames, there is no DA.
8045 *
8046 * For management frames, DA is at an
8047 * offset of 4 from the beginning of
8048 * the packet.
8049 *
8050 * For data frames, DA is at an offset
8051 * of 4 from the beginning of the packet
8052 * if To DS is clear and at an offset of
8053 * 16 from the beginning of the packet
8054 * if To DS is set.
8055 */
8056
8057 /*
8058 * Generate the tests to be done for data frames.
8059 *
8060 * First, check for To DS set, i.e. "link[1] & 0x01".
8061 */
8062 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8063 b1 = new_block(cstate, JMP(BPF_JSET));
8064 b1->s.k = 0x01; /* To DS */
8065 b1->stmts = s;
8066
8067 /*
8068 * If To DS is set, the DA is at 16.
8069 */
8070 b0 = gen_mac_multicast(cstate, 16);
8071 gen_and(b1, b0);
8072
8073 /*
8074 * Now, check for To DS not set, i.e. check
8075 * "!(link[1] & 0x01)".
8076 */
8077 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
8078 b2 = new_block(cstate, JMP(BPF_JSET));
8079 b2->s.k = 0x01; /* To DS */
8080 b2->stmts = s;
8081 gen_not(b2);
8082
8083 /*
8084 * If To DS is not set, the DA is at 4.
8085 */
8086 b1 = gen_mac_multicast(cstate, 4);
8087 gen_and(b2, b1);
8088
8089 /*
8090 * Now OR together the last two checks. That gives
8091 * the complete set of checks for data frames.
8092 */
8093 gen_or(b1, b0);
8094
8095 /*
8096 * Now check for a data frame.
8097 * I.e, check "link[0] & 0x08".
8098 */
8099 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8100 b1 = new_block(cstate, JMP(BPF_JSET));
8101 b1->s.k = 0x08;
8102 b1->stmts = s;
8103
8104 /*
8105 * AND that with the checks done for data frames.
8106 */
8107 gen_and(b1, b0);
8108
8109 /*
8110 * If the high-order bit of the type value is 0, this
8111 * is a management frame.
8112 * I.e, check "!(link[0] & 0x08)".
8113 */
8114 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8115 b2 = new_block(cstate, JMP(BPF_JSET));
8116 b2->s.k = 0x08;
8117 b2->stmts = s;
8118 gen_not(b2);
8119
8120 /*
8121 * For management frames, the DA is at 4.
8122 */
8123 b1 = gen_mac_multicast(cstate, 4);
8124 gen_and(b2, b1);
8125
8126 /*
8127 * OR that with the checks done for data frames.
8128 * That gives the checks done for management and
8129 * data frames.
8130 */
8131 gen_or(b1, b0);
8132
8133 /*
8134 * If the low-order bit of the type value is 1,
8135 * this is either a control frame or a frame
8136 * with a reserved type, and thus not a
8137 * frame with an SA.
8138 *
8139 * I.e., check "!(link[0] & 0x04)".
8140 */
8141 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
8142 b1 = new_block(cstate, JMP(BPF_JSET));
8143 b1->s.k = 0x04;
8144 b1->stmts = s;
8145 gen_not(b1);
8146
8147 /*
8148 * AND that with the checks for data and management
8149 * frames.
8150 */
8151 gen_and(b1, b0);
8152 return b0;
8153 case DLT_IP_OVER_FC:
8154 b0 = gen_mac_multicast(cstate, 2);
8155 return b0;
8156 default:
8157 break;
8158 }
8159 /* Link not known to support multicasts */
8160 break;
8161
8162 case Q_IP:
8163 b0 = gen_linktype(cstate, ETHERTYPE_IP);
8164 b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
8165 gen_and(b0, b1);
8166 return b1;
8167
8168 case Q_IPV6:
8169 b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
8170 b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
8171 gen_and(b0, b1);
8172 return b1;
8173 }
8174 bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8175 /*NOTREACHED*/
8176 }
8177
8178 /*
8179 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8180 * Outbound traffic is sent by this machine, while inbound traffic is
8181 * sent by a remote machine (and may include packets destined for a
8182 * unicast or multicast link-layer address we are not subscribing to).
8183 * These are the same definitions implemented by pcap_setdirection().
8184 * Capturing only unicast traffic destined for this host is probably
8185 * better accomplished using a higher-layer filter.
8186 */
8187 struct block *
8188 gen_inbound(compiler_state_t *cstate, int dir)
8189 {
8190 register struct block *b0;
8191
8192 /*
8193 * Catch errors reported by us and routines below us, and return NULL
8194 * on an error.
8195 */
8196 if (setjmp(cstate->top_ctx))
8197 return (NULL);
8198
8199 /*
8200 * Only some data link types support inbound/outbound qualifiers.
8201 */
8202 switch (cstate->linktype) {
8203 case DLT_SLIP:
8204 b0 = gen_relation_internal(cstate, BPF_JEQ,
8205 gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
8206 gen_loadi_internal(cstate, 0),
8207 dir);
8208 break;
8209
8210 case DLT_IPNET:
8211 if (dir) {
8212 /* match outgoing packets */
8213 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
8214 } else {
8215 /* match incoming packets */
8216 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
8217 }
8218 break;
8219
8220 case DLT_LINUX_SLL:
8221 /* match outgoing packets */
8222 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
8223 if (!dir) {
8224 /* to filter on inbound traffic, invert the match */
8225 gen_not(b0);
8226 }
8227 break;
8228
8229 case DLT_LINUX_SLL2:
8230 /* match outgoing packets */
8231 b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
8232 if (!dir) {
8233 /* to filter on inbound traffic, invert the match */
8234 gen_not(b0);
8235 }
8236 break;
8237
8238 #ifdef HAVE_NET_PFVAR_H
8239 case DLT_PFLOG:
8240 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
8241 ((dir == 0) ? PF_IN : PF_OUT));
8242 break;
8243 #endif
8244
8245 case DLT_PPP_PPPD:
8246 if (dir) {
8247 /* match outgoing packets */
8248 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
8249 } else {
8250 /* match incoming packets */
8251 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
8252 }
8253 break;
8254
8255 case DLT_JUNIPER_MFR:
8256 case DLT_JUNIPER_MLFR:
8257 case DLT_JUNIPER_MLPPP:
8258 case DLT_JUNIPER_ATM1:
8259 case DLT_JUNIPER_ATM2:
8260 case DLT_JUNIPER_PPPOE:
8261 case DLT_JUNIPER_PPPOE_ATM:
8262 case DLT_JUNIPER_GGSN:
8263 case DLT_JUNIPER_ES:
8264 case DLT_JUNIPER_MONITOR:
8265 case DLT_JUNIPER_SERVICES:
8266 case DLT_JUNIPER_ETHER:
8267 case DLT_JUNIPER_PPP:
8268 case DLT_JUNIPER_FRELAY:
8269 case DLT_JUNIPER_CHDLC:
8270 case DLT_JUNIPER_VP:
8271 case DLT_JUNIPER_ST:
8272 case DLT_JUNIPER_ISM:
8273 case DLT_JUNIPER_VS:
8274 case DLT_JUNIPER_SRX_E2E:
8275 case DLT_JUNIPER_FIBRECHANNEL:
8276 case DLT_JUNIPER_ATM_CEMIC:
8277
8278 /* juniper flags (including direction) are stored
8279 * the byte after the 3-byte magic number */
8280 if (dir) {
8281 /* match outgoing packets */
8282 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
8283 } else {
8284 /* match incoming packets */
8285 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
8286 }
8287 break;
8288
8289 default:
8290 /*
8291 * If we have packet meta-data indicating a direction,
8292 * and that metadata can be checked by BPF code, check
8293 * it. Otherwise, give up, as this link-layer type has
8294 * nothing in the packet data.
8295 *
8296 * Currently, the only platform where a BPF filter can
8297 * check that metadata is Linux with the in-kernel
8298 * BPF interpreter. If other packet capture mechanisms
8299 * and BPF filters also supported this, it would be
8300 * nice. It would be even better if they made that
8301 * metadata available so that we could provide it
8302 * with newer capture APIs, allowing it to be saved
8303 * in pcapng files.
8304 */
8305 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8306 /*
8307 * This is Linux with PF_PACKET support.
8308 * If this is a *live* capture, we can look at
8309 * special meta-data in the filter expression;
8310 * if it's a savefile, we can't.
8311 */
8312 if (cstate->bpf_pcap->rfile != NULL) {
8313 /* We have a FILE *, so this is a savefile */
8314 bpf_error(cstate, "inbound/outbound not supported on linktype %d when reading savefiles",
8315 cstate->linktype);
8316 b0 = NULL;
8317 /*NOTREACHED*/
8318 }
8319 /* match outgoing packets */
8320 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
8321 PACKET_OUTGOING);
8322 if (!dir) {
8323 /* to filter on inbound traffic, invert the match */
8324 gen_not(b0);
8325 }
8326 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8327 bpf_error(cstate, "inbound/outbound not supported on linktype %d",
8328 cstate->linktype);
8329 /*NOTREACHED*/
8330 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8331 }
8332 return (b0);
8333 }
8334
8335 #ifdef HAVE_NET_PFVAR_H
8336 /* PF firewall log matched interface */
8337 struct block *
8338 gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
8339 {
8340 struct block *b0;
8341 u_int len, off;
8342
8343 /*
8344 * Catch errors reported by us and routines below us, and return NULL
8345 * on an error.
8346 */
8347 if (setjmp(cstate->top_ctx))
8348 return (NULL);
8349
8350 if (cstate->linktype != DLT_PFLOG) {
8351 bpf_error(cstate, "ifname supported only on PF linktype");
8352 /*NOTREACHED*/
8353 }
8354 len = sizeof(((struct pfloghdr *)0)->ifname);
8355 off = offsetof(struct pfloghdr, ifname);
8356 if (strlen(ifname) >= len) {
8357 bpf_error(cstate, "ifname interface names can only be %d characters",
8358 len-1);
8359 /*NOTREACHED*/
8360 }
8361 b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
8362 (const u_char *)ifname);
8363 return (b0);
8364 }
8365
8366 /* PF firewall log ruleset name */
8367 struct block *
8368 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
8369 {
8370 struct block *b0;
8371
8372 /*
8373 * Catch errors reported by us and routines below us, and return NULL
8374 * on an error.
8375 */
8376 if (setjmp(cstate->top_ctx))
8377 return (NULL);
8378
8379 if (cstate->linktype != DLT_PFLOG) {
8380 bpf_error(cstate, "ruleset supported only on PF linktype");
8381 /*NOTREACHED*/
8382 }
8383
8384 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
8385 bpf_error(cstate, "ruleset names can only be %ld characters",
8386 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
8387 /*NOTREACHED*/
8388 }
8389
8390 b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
8391 (u_int)strlen(ruleset), (const u_char *)ruleset);
8392 return (b0);
8393 }
8394
8395 /* PF firewall log rule number */
8396 struct block *
8397 gen_pf_rnr(compiler_state_t *cstate, int rnr)
8398 {
8399 struct block *b0;
8400
8401 /*
8402 * Catch errors reported by us and routines below us, and return NULL
8403 * on an error.
8404 */
8405 if (setjmp(cstate->top_ctx))
8406 return (NULL);
8407
8408 if (cstate->linktype != DLT_PFLOG) {
8409 bpf_error(cstate, "rnr supported only on PF linktype");
8410 /*NOTREACHED*/
8411 }
8412
8413 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
8414 (bpf_u_int32)rnr);
8415 return (b0);
8416 }
8417
8418 /* PF firewall log sub-rule number */
8419 struct block *
8420 gen_pf_srnr(compiler_state_t *cstate, int srnr)
8421 {
8422 struct block *b0;
8423
8424 /*
8425 * Catch errors reported by us and routines below us, and return NULL
8426 * on an error.
8427 */
8428 if (setjmp(cstate->top_ctx))
8429 return (NULL);
8430
8431 if (cstate->linktype != DLT_PFLOG) {
8432 bpf_error(cstate, "srnr supported only on PF linktype");
8433 /*NOTREACHED*/
8434 }
8435
8436 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
8437 (bpf_u_int32)srnr);
8438 return (b0);
8439 }
8440
8441 /* PF firewall log reason code */
8442 struct block *
8443 gen_pf_reason(compiler_state_t *cstate, int reason)
8444 {
8445 struct block *b0;
8446
8447 /*
8448 * Catch errors reported by us and routines below us, and return NULL
8449 * on an error.
8450 */
8451 if (setjmp(cstate->top_ctx))
8452 return (NULL);
8453
8454 if (cstate->linktype != DLT_PFLOG) {
8455 bpf_error(cstate, "reason supported only on PF linktype");
8456 /*NOTREACHED*/
8457 }
8458
8459 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
8460 (bpf_u_int32)reason);
8461 return (b0);
8462 }
8463
8464 /* PF firewall log action */
8465 struct block *
8466 gen_pf_action(compiler_state_t *cstate, int action)
8467 {
8468 struct block *b0;
8469
8470 /*
8471 * Catch errors reported by us and routines below us, and return NULL
8472 * on an error.
8473 */
8474 if (setjmp(cstate->top_ctx))
8475 return (NULL);
8476
8477 if (cstate->linktype != DLT_PFLOG) {
8478 bpf_error(cstate, "action supported only on PF linktype");
8479 /*NOTREACHED*/
8480 }
8481
8482 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
8483 (bpf_u_int32)action);
8484 return (b0);
8485 }
8486 #else /* !HAVE_NET_PFVAR_H */
8487 struct block *
8488 gen_pf_ifname(compiler_state_t *cstate, const char *ifname _U_)
8489 {
8490 /*
8491 * Catch errors reported by us and routines below us, and return NULL
8492 * on an error.
8493 */
8494 if (setjmp(cstate->top_ctx))
8495 return (NULL);
8496
8497 bpf_error(cstate, "libpcap was compiled without pf support");
8498 /*NOTREACHED*/
8499 }
8500
8501 struct block *
8502 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset _U_)
8503 {
8504 /*
8505 * Catch errors reported by us and routines below us, and return NULL
8506 * on an error.
8507 */
8508 if (setjmp(cstate->top_ctx))
8509 return (NULL);
8510
8511 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
8512 /*NOTREACHED*/
8513 }
8514
8515 struct block *
8516 gen_pf_rnr(compiler_state_t *cstate, int rnr _U_)
8517 {
8518 /*
8519 * Catch errors reported by us and routines below us, and return NULL
8520 * on an error.
8521 */
8522 if (setjmp(cstate->top_ctx))
8523 return (NULL);
8524
8525 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
8526 /*NOTREACHED*/
8527 }
8528
8529 struct block *
8530 gen_pf_srnr(compiler_state_t *cstate, int srnr _U_)
8531 {
8532 /*
8533 * Catch errors reported by us and routines below us, and return NULL
8534 * on an error.
8535 */
8536 if (setjmp(cstate->top_ctx))
8537 return (NULL);
8538
8539 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
8540 /*NOTREACHED*/
8541 }
8542
8543 struct block *
8544 gen_pf_reason(compiler_state_t *cstate, int reason _U_)
8545 {
8546 /*
8547 * Catch errors reported by us and routines below us, and return NULL
8548 * on an error.
8549 */
8550 if (setjmp(cstate->top_ctx))
8551 return (NULL);
8552
8553 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
8554 /*NOTREACHED*/
8555 }
8556
8557 struct block *
8558 gen_pf_action(compiler_state_t *cstate, int action _U_)
8559 {
8560 /*
8561 * Catch errors reported by us and routines below us, and return NULL
8562 * on an error.
8563 */
8564 if (setjmp(cstate->top_ctx))
8565 return (NULL);
8566
8567 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
8568 /*NOTREACHED*/
8569 }
8570 #endif /* HAVE_NET_PFVAR_H */
8571
8572 /* IEEE 802.11 wireless header */
8573 struct block *
8574 gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
8575 {
8576 struct block *b0;
8577
8578 /*
8579 * Catch errors reported by us and routines below us, and return NULL
8580 * on an error.
8581 */
8582 if (setjmp(cstate->top_ctx))
8583 return (NULL);
8584
8585 switch (cstate->linktype) {
8586
8587 case DLT_IEEE802_11:
8588 case DLT_PRISM_HEADER:
8589 case DLT_IEEE802_11_RADIO_AVS:
8590 case DLT_IEEE802_11_RADIO:
8591 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
8592 break;
8593
8594 default:
8595 bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
8596 /*NOTREACHED*/
8597 }
8598
8599 return (b0);
8600 }
8601
8602 struct block *
8603 gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
8604 {
8605 struct block *b0;
8606
8607 /*
8608 * Catch errors reported by us and routines below us, and return NULL
8609 * on an error.
8610 */
8611 if (setjmp(cstate->top_ctx))
8612 return (NULL);
8613
8614 switch (cstate->linktype) {
8615
8616 case DLT_IEEE802_11:
8617 case DLT_PRISM_HEADER:
8618 case DLT_IEEE802_11_RADIO_AVS:
8619 case DLT_IEEE802_11_RADIO:
8620 break;
8621
8622 default:
8623 bpf_error(cstate, "frame direction supported only with 802.11 headers");
8624 /*NOTREACHED*/
8625 }
8626
8627 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
8628 IEEE80211_FC1_DIR_MASK);
8629
8630 return (b0);
8631 }
8632
8633 struct block *
8634 gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
8635 {
8636 struct block *b;
8637
8638 /*
8639 * Catch errors reported by us and routines below us, and return NULL
8640 * on an error.
8641 */
8642 if (setjmp(cstate->top_ctx))
8643 return (NULL);
8644
8645 switch (cstate->linktype) {
8646
8647 case DLT_ARCNET:
8648 case DLT_ARCNET_LINUX:
8649 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
8650 q.proto == Q_LINK) {
8651 cstate->e = pcap_ether_aton(s);
8652 if (cstate->e == NULL)
8653 bpf_error(cstate, "malloc");
8654 b = gen_ahostop(cstate, cstate->e, (int)q.dir);
8655 free(cstate->e);
8656 cstate->e = NULL;
8657 return (b);
8658 } else
8659 bpf_error(cstate, "ARCnet address used in non-arc expression");
8660 /*NOTREACHED*/
8661
8662 default:
8663 bpf_error(cstate, "aid supported only on ARCnet");
8664 /*NOTREACHED*/
8665 }
8666 }
8667
8668 static struct block *
8669 gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
8670 {
8671 register struct block *b0, *b1;
8672
8673 switch (dir) {
8674 /* src comes first, different from Ethernet */
8675 case Q_SRC:
8676 return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
8677
8678 case Q_DST:
8679 return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
8680
8681 case Q_AND:
8682 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8683 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8684 gen_and(b0, b1);
8685 return b1;
8686
8687 case Q_DEFAULT:
8688 case Q_OR:
8689 b0 = gen_ahostop(cstate, eaddr, Q_SRC);
8690 b1 = gen_ahostop(cstate, eaddr, Q_DST);
8691 gen_or(b0, b1);
8692 return b1;
8693
8694 case Q_ADDR1:
8695 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
8696 /*NOTREACHED*/
8697
8698 case Q_ADDR2:
8699 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
8700 /*NOTREACHED*/
8701
8702 case Q_ADDR3:
8703 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
8704 /*NOTREACHED*/
8705
8706 case Q_ADDR4:
8707 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
8708 /*NOTREACHED*/
8709
8710 case Q_RA:
8711 bpf_error(cstate, "'ra' is only supported on 802.11");
8712 /*NOTREACHED*/
8713
8714 case Q_TA:
8715 bpf_error(cstate, "'ta' is only supported on 802.11");
8716 /*NOTREACHED*/
8717 }
8718 abort();
8719 /*NOTREACHED*/
8720 }
8721
8722 static struct block *
8723 gen_vlan_tpid_test(compiler_state_t *cstate)
8724 {
8725 struct block *b0, *b1;
8726
8727 /* check for VLAN, including QinQ */
8728 b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
8729 b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
8730 gen_or(b0,b1);
8731 b0 = b1;
8732 b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
8733 gen_or(b0,b1);
8734
8735 return b1;
8736 }
8737
8738 static struct block *
8739 gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
8740 {
8741 if (vlan_num > 0x0fff) {
8742 bpf_error(cstate, "VLAN tag %u greater than maximum %u",
8743 vlan_num, 0x0fff);
8744 }
8745 return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
8746 }
8747
8748 static struct block *
8749 gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8750 int has_vlan_tag)
8751 {
8752 struct block *b0, *b1;
8753
8754 b0 = gen_vlan_tpid_test(cstate);
8755
8756 if (has_vlan_tag) {
8757 b1 = gen_vlan_vid_test(cstate, vlan_num);
8758 gen_and(b0, b1);
8759 b0 = b1;
8760 }
8761
8762 /*
8763 * Both payload and link header type follow the VLAN tags so that
8764 * both need to be updated.
8765 */
8766 cstate->off_linkpl.constant_part += 4;
8767 cstate->off_linktype.constant_part += 4;
8768
8769 return b0;
8770 }
8771
8772 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8773 /* add v to variable part of off */
8774 static void
8775 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
8776 bpf_u_int32 v, struct slist *s)
8777 {
8778 struct slist *s2;
8779
8780 if (!off->is_variable)
8781 off->is_variable = 1;
8782 if (off->reg == -1)
8783 off->reg = alloc_reg(cstate);
8784
8785 s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
8786 s2->s.k = off->reg;
8787 sappend(s, s2);
8788 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
8789 s2->s.k = v;
8790 sappend(s, s2);
8791 s2 = new_stmt(cstate, BPF_ST);
8792 s2->s.k = off->reg;
8793 sappend(s, s2);
8794 }
8795
8796 /*
8797 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8798 * and link type offsets first
8799 */
8800 static void
8801 gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
8802 {
8803 struct slist s;
8804
8805 /* offset determined at run time, shift variable part */
8806 s.next = NULL;
8807 cstate->is_vlan_vloffset = 1;
8808 gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
8809 gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
8810
8811 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8812 sappend(s.next, b_tpid->head->stmts);
8813 b_tpid->head->stmts = s.next;
8814 }
8815
8816 /*
8817 * patch block b_vid (VLAN id test) to load VID value either from packet
8818 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8819 */
8820 static void
8821 gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
8822 {
8823 struct slist *s, *s2, *sjeq;
8824 unsigned cnt;
8825
8826 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8827 s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8828
8829 /* true -> next instructions, false -> beginning of b_vid */
8830 sjeq = new_stmt(cstate, JMP(BPF_JEQ));
8831 sjeq->s.k = 1;
8832 sjeq->s.jf = b_vid->stmts;
8833 sappend(s, sjeq);
8834
8835 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8836 s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
8837 sappend(s, s2);
8838 sjeq->s.jt = s2;
8839
8840 /* Jump to the test in b_vid. We need to jump one instruction before
8841 * the end of the b_vid block so that we only skip loading the TCI
8842 * from packet data and not the 'and' instruction extractging VID.
8843 */
8844 cnt = 0;
8845 for (s2 = b_vid->stmts; s2; s2 = s2->next)
8846 cnt++;
8847 s2 = new_stmt(cstate, JMP(BPF_JA));
8848 s2->s.k = cnt - 1;
8849 sappend(s, s2);
8850
8851 /* insert our statements at the beginning of b_vid */
8852 sappend(s, b_vid->stmts);
8853 b_vid->stmts = s;
8854 }
8855
8856 /*
8857 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8858 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8859 * tag can be either in metadata or in packet data; therefore if the
8860 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8861 * header for VLAN tag. As the decision is done at run time, we need
8862 * update variable part of the offsets
8863 */
8864 static struct block *
8865 gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
8866 int has_vlan_tag)
8867 {
8868 struct block *b0, *b_tpid, *b_vid = NULL;
8869 struct slist *s;
8870
8871 /* generate new filter code based on extracting packet
8872 * metadata */
8873 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
8874 s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
8875
8876 b0 = new_block(cstate, JMP(BPF_JEQ));
8877 b0->stmts = s;
8878 b0->s.k = 1;
8879
8880 /*
8881 * This is tricky. We need to insert the statements updating variable
8882 * parts of offsets before the the traditional TPID and VID tests so
8883 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8884 * we do not want this update to affect those checks. That's why we
8885 * generate both test blocks first and insert the statements updating
8886 * variable parts of both offsets after that. This wouldn't work if
8887 * there already were variable length link header when entering this
8888 * function but gen_vlan_bpf_extensions() isn't called in that case.
8889 */
8890 b_tpid = gen_vlan_tpid_test(cstate);
8891 if (has_vlan_tag)
8892 b_vid = gen_vlan_vid_test(cstate, vlan_num);
8893
8894 gen_vlan_patch_tpid_test(cstate, b_tpid);
8895 gen_or(b0, b_tpid);
8896 b0 = b_tpid;
8897
8898 if (has_vlan_tag) {
8899 gen_vlan_patch_vid_test(cstate, b_vid);
8900 gen_and(b0, b_vid);
8901 b0 = b_vid;
8902 }
8903
8904 return b0;
8905 }
8906 #endif
8907
8908 /*
8909 * support IEEE 802.1Q VLAN trunk over ethernet
8910 */
8911 struct block *
8912 gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
8913 {
8914 struct block *b0;
8915
8916 /*
8917 * Catch errors reported by us and routines below us, and return NULL
8918 * on an error.
8919 */
8920 if (setjmp(cstate->top_ctx))
8921 return (NULL);
8922
8923 /* can't check for VLAN-encapsulated packets inside MPLS */
8924 if (cstate->label_stack_depth > 0)
8925 bpf_error(cstate, "no VLAN match after MPLS");
8926
8927 /*
8928 * Check for a VLAN packet, and then change the offsets to point
8929 * to the type and data fields within the VLAN packet. Just
8930 * increment the offsets, so that we can support a hierarchy, e.g.
8931 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8932 * VLAN 100.
8933 *
8934 * XXX - this is a bit of a kludge. If we were to split the
8935 * compiler into a parser that parses an expression and
8936 * generates an expression tree, and a code generator that
8937 * takes an expression tree (which could come from our
8938 * parser or from some other parser) and generates BPF code,
8939 * we could perhaps make the offsets parameters of routines
8940 * and, in the handler for an "AND" node, pass to subnodes
8941 * other than the VLAN node the adjusted offsets.
8942 *
8943 * This would mean that "vlan" would, instead of changing the
8944 * behavior of *all* tests after it, change only the behavior
8945 * of tests ANDed with it. That would change the documented
8946 * semantics of "vlan", which might break some expressions.
8947 * However, it would mean that "(vlan and ip) or ip" would check
8948 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8949 * checking only for VLAN-encapsulated IP, so that could still
8950 * be considered worth doing; it wouldn't break expressions
8951 * that are of the form "vlan and ..." or "vlan N and ...",
8952 * which I suspect are the most common expressions involving
8953 * "vlan". "vlan or ..." doesn't necessarily do what the user
8954 * would really want, now, as all the "or ..." tests would
8955 * be done assuming a VLAN, even though the "or" could be viewed
8956 * as meaning "or, if this isn't a VLAN packet...".
8957 */
8958 switch (cstate->linktype) {
8959
8960 case DLT_EN10MB:
8961 case DLT_NETANALYZER:
8962 case DLT_NETANALYZER_TRANSPARENT:
8963 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8964 /* Verify that this is the outer part of the packet and
8965 * not encapsulated somehow. */
8966 if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
8967 cstate->off_linkhdr.constant_part ==
8968 cstate->off_outermostlinkhdr.constant_part) {
8969 /*
8970 * Do we need special VLAN handling?
8971 */
8972 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
8973 b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
8974 has_vlan_tag);
8975 else
8976 b0 = gen_vlan_no_bpf_extensions(cstate,
8977 vlan_num, has_vlan_tag);
8978 } else
8979 #endif
8980 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
8981 has_vlan_tag);
8982 break;
8983
8984 case DLT_IEEE802_11:
8985 case DLT_PRISM_HEADER:
8986 case DLT_IEEE802_11_RADIO_AVS:
8987 case DLT_IEEE802_11_RADIO:
8988 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
8989 break;
8990
8991 default:
8992 bpf_error(cstate, "no VLAN support for data link type %d",
8993 cstate->linktype);
8994 /*NOTREACHED*/
8995 }
8996
8997 cstate->vlan_stack_depth++;
8998
8999 return (b0);
9000 }
9001
9002 /*
9003 * support for MPLS
9004 *
9005 * The label_num_arg dance is to avoid annoying whining by compilers that
9006 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9007 * It's not *used* after setjmp returns.
9008 */
9009 struct block *
9010 gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,
9011 int has_label_num)
9012 {
9013 volatile bpf_u_int32 label_num = label_num_arg;
9014 struct block *b0, *b1;
9015
9016 /*
9017 * Catch errors reported by us and routines below us, and return NULL
9018 * on an error.
9019 */
9020 if (setjmp(cstate->top_ctx))
9021 return (NULL);
9022
9023 if (cstate->label_stack_depth > 0) {
9024 /* just match the bottom-of-stack bit clear */
9025 b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
9026 } else {
9027 /*
9028 * We're not in an MPLS stack yet, so check the link-layer
9029 * type against MPLS.
9030 */
9031 switch (cstate->linktype) {
9032
9033 case DLT_C_HDLC: /* fall through */
9034 case DLT_EN10MB:
9035 case DLT_NETANALYZER:
9036 case DLT_NETANALYZER_TRANSPARENT:
9037 b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
9038 break;
9039
9040 case DLT_PPP:
9041 b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
9042 break;
9043
9044 /* FIXME add other DLT_s ...
9045 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9046 * leave it for now */
9047
9048 default:
9049 bpf_error(cstate, "no MPLS support for data link type %d",
9050 cstate->linktype);
9051 /*NOTREACHED*/
9052 }
9053 }
9054
9055 /* If a specific MPLS label is requested, check it */
9056 if (has_label_num) {
9057 if (label_num > 0xFFFFF) {
9058 bpf_error(cstate, "MPLS label %u greater than maximum %u",
9059 label_num, 0xFFFFF);
9060 }
9061 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
9062 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
9063 0xfffff000); /* only compare the first 20 bits */
9064 gen_and(b0, b1);
9065 b0 = b1;
9066 }
9067
9068 /*
9069 * Change the offsets to point to the type and data fields within
9070 * the MPLS packet. Just increment the offsets, so that we
9071 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9072 * capture packets with an outer label of 100000 and an inner
9073 * label of 1024.
9074 *
9075 * Increment the MPLS stack depth as well; this indicates that
9076 * we're checking MPLS-encapsulated headers, to make sure higher
9077 * level code generators don't try to match against IP-related
9078 * protocols such as Q_ARP, Q_RARP etc.
9079 *
9080 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9081 */
9082 cstate->off_nl_nosnap += 4;
9083 cstate->off_nl += 4;
9084 cstate->label_stack_depth++;
9085 return (b0);
9086 }
9087
9088 /*
9089 * Support PPPOE discovery and session.
9090 */
9091 struct block *
9092 gen_pppoed(compiler_state_t *cstate)
9093 {
9094 /*
9095 * Catch errors reported by us and routines below us, and return NULL
9096 * on an error.
9097 */
9098 if (setjmp(cstate->top_ctx))
9099 return (NULL);
9100
9101 /* check for PPPoE discovery */
9102 return gen_linktype(cstate, ETHERTYPE_PPPOED);
9103 }
9104
9105 struct block *
9106 gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
9107 {
9108 struct block *b0, *b1;
9109
9110 /*
9111 * Catch errors reported by us and routines below us, and return NULL
9112 * on an error.
9113 */
9114 if (setjmp(cstate->top_ctx))
9115 return (NULL);
9116
9117 /*
9118 * Test against the PPPoE session link-layer type.
9119 */
9120 b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
9121
9122 /* If a specific session is requested, check PPPoE session id */
9123 if (has_sess_num) {
9124 if (sess_num > 0x0000ffff) {
9125 bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
9126 sess_num, 0x0000ffff);
9127 }
9128 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);
9129 gen_and(b0, b1);
9130 b0 = b1;
9131 }
9132
9133 /*
9134 * Change the offsets to point to the type and data fields within
9135 * the PPP packet, and note that this is PPPoE rather than
9136 * raw PPP.
9137 *
9138 * XXX - this is a bit of a kludge. If we were to split the
9139 * compiler into a parser that parses an expression and
9140 * generates an expression tree, and a code generator that
9141 * takes an expression tree (which could come from our
9142 * parser or from some other parser) and generates BPF code,
9143 * we could perhaps make the offsets parameters of routines
9144 * and, in the handler for an "AND" node, pass to subnodes
9145 * other than the PPPoE node the adjusted offsets.
9146 *
9147 * This would mean that "pppoes" would, instead of changing the
9148 * behavior of *all* tests after it, change only the behavior
9149 * of tests ANDed with it. That would change the documented
9150 * semantics of "pppoes", which might break some expressions.
9151 * However, it would mean that "(pppoes and ip) or ip" would check
9152 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9153 * checking only for VLAN-encapsulated IP, so that could still
9154 * be considered worth doing; it wouldn't break expressions
9155 * that are of the form "pppoes and ..." which I suspect are the
9156 * most common expressions involving "pppoes". "pppoes or ..."
9157 * doesn't necessarily do what the user would really want, now,
9158 * as all the "or ..." tests would be done assuming PPPoE, even
9159 * though the "or" could be viewed as meaning "or, if this isn't
9160 * a PPPoE packet...".
9161 *
9162 * The "network-layer" protocol is PPPoE, which has a 6-byte
9163 * PPPoE header, followed by a PPP packet.
9164 *
9165 * There is no HDLC encapsulation for the PPP packet (it's
9166 * encapsulated in PPPoES instead), so the link-layer type
9167 * starts at the first byte of the PPP packet. For PPPoE,
9168 * that offset is relative to the beginning of the total
9169 * link-layer payload, including any 802.2 LLC header, so
9170 * it's 6 bytes past cstate->off_nl.
9171 */
9172 PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
9173 cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
9174 cstate->off_linkpl.reg);
9175
9176 cstate->off_linktype = cstate->off_linkhdr;
9177 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
9178
9179 cstate->off_nl = 0;
9180 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
9181
9182 return b0;
9183 }
9184
9185 /* Check that this is Geneve and the VNI is correct if
9186 * specified. Parameterized to handle both IPv4 and IPv6. */
9187 static struct block *
9188 gen_geneve_check(compiler_state_t *cstate,
9189 struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
9190 enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
9191 {
9192 struct block *b0, *b1;
9193
9194 b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
9195
9196 /* Check that we are operating on version 0. Otherwise, we
9197 * can't decode the rest of the fields. The version is 2 bits
9198 * in the first byte of the Geneve header. */
9199 b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
9200 gen_and(b0, b1);
9201 b0 = b1;
9202
9203 if (has_vni) {
9204 if (vni > 0xffffff) {
9205 bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
9206 vni, 0xffffff);
9207 }
9208 vni <<= 8; /* VNI is in the upper 3 bytes */
9209 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
9210 gen_and(b0, b1);
9211 b0 = b1;
9212 }
9213
9214 return b0;
9215 }
9216
9217 /* The IPv4 and IPv6 Geneve checks need to do two things:
9218 * - Verify that this actually is Geneve with the right VNI.
9219 * - Place the IP header length (plus variable link prefix if
9220 * needed) into register A to be used later to compute
9221 * the inner packet offsets. */
9222 static struct block *
9223 gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9224 {
9225 struct block *b0, *b1;
9226 struct slist *s, *s1;
9227
9228 b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
9229
9230 /* Load the IP header length into A. */
9231 s = gen_loadx_iphdrlen(cstate);
9232
9233 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9234 sappend(s, s1);
9235
9236 /* Forcibly append these statements to the true condition
9237 * of the protocol check by creating a new block that is
9238 * always true and ANDing them. */
9239 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9240 b1->stmts = s;
9241 b1->s.k = 0;
9242
9243 gen_and(b0, b1);
9244
9245 return b1;
9246 }
9247
9248 static struct block *
9249 gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9250 {
9251 struct block *b0, *b1;
9252 struct slist *s, *s1;
9253
9254 b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
9255
9256 /* Load the IP header length. We need to account for a
9257 * variable length link prefix if there is one. */
9258 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
9259 if (s) {
9260 s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
9261 s1->s.k = 40;
9262 sappend(s, s1);
9263
9264 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9265 s1->s.k = 0;
9266 sappend(s, s1);
9267 } else {
9268 s = new_stmt(cstate, BPF_LD|BPF_IMM);
9269 s->s.k = 40;
9270 }
9271
9272 /* Forcibly append these statements to the true condition
9273 * of the protocol check by creating a new block that is
9274 * always true and ANDing them. */
9275 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9276 sappend(s, s1);
9277
9278 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9279 b1->stmts = s;
9280 b1->s.k = 0;
9281
9282 gen_and(b0, b1);
9283
9284 return b1;
9285 }
9286
9287 /* We need to store three values based on the Geneve header::
9288 * - The offset of the linktype.
9289 * - The offset of the end of the Geneve header.
9290 * - The offset of the end of the encapsulated MAC header. */
9291 static struct slist *
9292 gen_geneve_offsets(compiler_state_t *cstate)
9293 {
9294 struct slist *s, *s1, *s_proto;
9295
9296 /* First we need to calculate the offset of the Geneve header
9297 * itself. This is composed of the IP header previously calculated
9298 * (include any variable link prefix) and stored in A plus the
9299 * fixed sized headers (fixed link prefix, MAC length, and UDP
9300 * header). */
9301 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9302 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
9303
9304 /* Stash this in X since we'll need it later. */
9305 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9306 sappend(s, s1);
9307
9308 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9309 * store it. */
9310 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9311 s1->s.k = 2;
9312 sappend(s, s1);
9313
9314 cstate->off_linktype.reg = alloc_reg(cstate);
9315 cstate->off_linktype.is_variable = 1;
9316 cstate->off_linktype.constant_part = 0;
9317
9318 s1 = new_stmt(cstate, BPF_ST);
9319 s1->s.k = cstate->off_linktype.reg;
9320 sappend(s, s1);
9321
9322 /* Load the Geneve option length and mask and shift to get the
9323 * number of bytes. It is stored in the first byte of the Geneve
9324 * header. */
9325 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
9326 s1->s.k = 0;
9327 sappend(s, s1);
9328
9329 s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
9330 s1->s.k = 0x3f;
9331 sappend(s, s1);
9332
9333 s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
9334 s1->s.k = 4;
9335 sappend(s, s1);
9336
9337 /* Add in the rest of the Geneve base header. */
9338 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9339 s1->s.k = 8;
9340 sappend(s, s1);
9341
9342 /* Add the Geneve header length to its offset and store. */
9343 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
9344 s1->s.k = 0;
9345 sappend(s, s1);
9346
9347 /* Set the encapsulated type as Ethernet. Even though we may
9348 * not actually have Ethernet inside there are two reasons this
9349 * is useful:
9350 * - The linktype field is always in EtherType format regardless
9351 * of whether it is in Geneve or an inner Ethernet frame.
9352 * - The only link layer that we have specific support for is
9353 * Ethernet. We will confirm that the packet actually is
9354 * Ethernet at runtime before executing these checks. */
9355 PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
9356
9357 s1 = new_stmt(cstate, BPF_ST);
9358 s1->s.k = cstate->off_linkhdr.reg;
9359 sappend(s, s1);
9360
9361 /* Calculate whether we have an Ethernet header or just raw IP/
9362 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9363 * and linktype by 14 bytes so that the network header can be found
9364 * seamlessly. Otherwise, keep what we've calculated already. */
9365
9366 /* We have a bare jmp so we can't use the optimizer. */
9367 cstate->no_optimize = 1;
9368
9369 /* Load the EtherType in the Geneve header, 2 bytes in. */
9370 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
9371 s1->s.k = 2;
9372 sappend(s, s1);
9373
9374 /* Load X with the end of the Geneve header. */
9375 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9376 s1->s.k = cstate->off_linkhdr.reg;
9377 sappend(s, s1);
9378
9379 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9380 * end of this check, we should have the total length in X. In
9381 * the non-Ethernet case, it's already there. */
9382 s_proto = new_stmt(cstate, JMP(BPF_JEQ));
9383 s_proto->s.k = ETHERTYPE_TEB;
9384 sappend(s, s_proto);
9385
9386 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
9387 sappend(s, s1);
9388 s_proto->s.jt = s1;
9389
9390 /* Since this is Ethernet, use the EtherType of the payload
9391 * directly as the linktype. Overwrite what we already have. */
9392 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9393 s1->s.k = 12;
9394 sappend(s, s1);
9395
9396 s1 = new_stmt(cstate, BPF_ST);
9397 s1->s.k = cstate->off_linktype.reg;
9398 sappend(s, s1);
9399
9400 /* Advance two bytes further to get the end of the Ethernet
9401 * header. */
9402 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
9403 s1->s.k = 2;
9404 sappend(s, s1);
9405
9406 /* Move the result to X. */
9407 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
9408 sappend(s, s1);
9409
9410 /* Store the final result of our linkpl calculation. */
9411 cstate->off_linkpl.reg = alloc_reg(cstate);
9412 cstate->off_linkpl.is_variable = 1;
9413 cstate->off_linkpl.constant_part = 0;
9414
9415 s1 = new_stmt(cstate, BPF_STX);
9416 s1->s.k = cstate->off_linkpl.reg;
9417 sappend(s, s1);
9418 s_proto->s.jf = s1;
9419
9420 cstate->off_nl = 0;
9421
9422 return s;
9423 }
9424
9425 /* Check to see if this is a Geneve packet. */
9426 struct block *
9427 gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
9428 {
9429 struct block *b0, *b1;
9430 struct slist *s;
9431
9432 /*
9433 * Catch errors reported by us and routines below us, and return NULL
9434 * on an error.
9435 */
9436 if (setjmp(cstate->top_ctx))
9437 return (NULL);
9438
9439 b0 = gen_geneve4(cstate, vni, has_vni);
9440 b1 = gen_geneve6(cstate, vni, has_vni);
9441
9442 gen_or(b0, b1);
9443 b0 = b1;
9444
9445 /* Later filters should act on the payload of the Geneve frame,
9446 * update all of the header pointers. Attach this code so that
9447 * it gets executed in the event that the Geneve filter matches. */
9448 s = gen_geneve_offsets(cstate);
9449
9450 b1 = gen_true(cstate);
9451 sappend(s, b1->stmts);
9452 b1->stmts = s;
9453
9454 gen_and(b0, b1);
9455
9456 cstate->is_geneve = 1;
9457
9458 return b1;
9459 }
9460
9461 /* Check that the encapsulated frame has a link layer header
9462 * for Ethernet filters. */
9463 static struct block *
9464 gen_geneve_ll_check(compiler_state_t *cstate)
9465 {
9466 struct block *b0;
9467 struct slist *s, *s1;
9468
9469 /* The easiest way to see if there is a link layer present
9470 * is to check if the link layer header and payload are not
9471 * the same. */
9472
9473 /* Geneve always generates pure variable offsets so we can
9474 * compare only the registers. */
9475 s = new_stmt(cstate, BPF_LD|BPF_MEM);
9476 s->s.k = cstate->off_linkhdr.reg;
9477
9478 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
9479 s1->s.k = cstate->off_linkpl.reg;
9480 sappend(s, s1);
9481
9482 b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
9483 b0->stmts = s;
9484 b0->s.k = 0;
9485 gen_not(b0);
9486
9487 return b0;
9488 }
9489
9490 static struct block *
9491 gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
9492 bpf_u_int32 jvalue, int jtype, int reverse)
9493 {
9494 struct block *b0;
9495
9496 switch (atmfield) {
9497
9498 case A_VPI:
9499 if (!cstate->is_atm)
9500 bpf_error(cstate, "'vpi' supported only on raw ATM");
9501 if (cstate->off_vpi == OFFSET_NOT_SET)
9502 abort();
9503 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
9504 0xffffffffU, jtype, reverse, jvalue);
9505 break;
9506
9507 case A_VCI:
9508 if (!cstate->is_atm)
9509 bpf_error(cstate, "'vci' supported only on raw ATM");
9510 if (cstate->off_vci == OFFSET_NOT_SET)
9511 abort();
9512 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
9513 0xffffffffU, jtype, reverse, jvalue);
9514 break;
9515
9516 case A_PROTOTYPE:
9517 if (cstate->off_proto == OFFSET_NOT_SET)
9518 abort(); /* XXX - this isn't on FreeBSD */
9519 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9520 0x0fU, jtype, reverse, jvalue);
9521 break;
9522
9523 case A_MSGTYPE:
9524 if (cstate->off_payload == OFFSET_NOT_SET)
9525 abort();
9526 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
9527 0xffffffffU, jtype, reverse, jvalue);
9528 break;
9529
9530 case A_CALLREFTYPE:
9531 if (!cstate->is_atm)
9532 bpf_error(cstate, "'callref' supported only on raw ATM");
9533 if (cstate->off_proto == OFFSET_NOT_SET)
9534 abort();
9535 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
9536 0xffffffffU, jtype, reverse, jvalue);
9537 break;
9538
9539 default:
9540 abort();
9541 }
9542 return b0;
9543 }
9544
9545 static struct block *
9546 gen_atmtype_metac(compiler_state_t *cstate)
9547 {
9548 struct block *b0, *b1;
9549
9550 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9551 b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
9552 gen_and(b0, b1);
9553 return b1;
9554 }
9555
9556 static struct block *
9557 gen_atmtype_sc(compiler_state_t *cstate)
9558 {
9559 struct block *b0, *b1;
9560
9561 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9562 b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
9563 gen_and(b0, b1);
9564 return b1;
9565 }
9566
9567 static struct block *
9568 gen_atmtype_llc(compiler_state_t *cstate)
9569 {
9570 struct block *b0;
9571
9572 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
9573 cstate->linktype = cstate->prevlinktype;
9574 return b0;
9575 }
9576
9577 struct block *
9578 gen_atmfield_code(compiler_state_t *cstate, int atmfield,
9579 bpf_u_int32 jvalue, int jtype, int reverse)
9580 {
9581 /*
9582 * Catch errors reported by us and routines below us, and return NULL
9583 * on an error.
9584 */
9585 if (setjmp(cstate->top_ctx))
9586 return (NULL);
9587
9588 return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
9589 reverse);
9590 }
9591
9592 struct block *
9593 gen_atmtype_abbrev(compiler_state_t *cstate, int type)
9594 {
9595 struct block *b0, *b1;
9596
9597 /*
9598 * Catch errors reported by us and routines below us, and return NULL
9599 * on an error.
9600 */
9601 if (setjmp(cstate->top_ctx))
9602 return (NULL);
9603
9604 switch (type) {
9605
9606 case A_METAC:
9607 /* Get all packets in Meta signalling Circuit */
9608 if (!cstate->is_atm)
9609 bpf_error(cstate, "'metac' supported only on raw ATM");
9610 b1 = gen_atmtype_metac(cstate);
9611 break;
9612
9613 case A_BCC:
9614 /* Get all packets in Broadcast Circuit*/
9615 if (!cstate->is_atm)
9616 bpf_error(cstate, "'bcc' supported only on raw ATM");
9617 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9618 b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
9619 gen_and(b0, b1);
9620 break;
9621
9622 case A_OAMF4SC:
9623 /* Get all cells in Segment OAM F4 circuit*/
9624 if (!cstate->is_atm)
9625 bpf_error(cstate, "'oam4sc' supported only on raw ATM");
9626 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9627 b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9628 gen_and(b0, b1);
9629 break;
9630
9631 case A_OAMF4EC:
9632 /* Get all cells in End-to-End OAM F4 Circuit*/
9633 if (!cstate->is_atm)
9634 bpf_error(cstate, "'oam4ec' supported only on raw ATM");
9635 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9636 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9637 gen_and(b0, b1);
9638 break;
9639
9640 case A_SC:
9641 /* Get all packets in connection Signalling Circuit */
9642 if (!cstate->is_atm)
9643 bpf_error(cstate, "'sc' supported only on raw ATM");
9644 b1 = gen_atmtype_sc(cstate);
9645 break;
9646
9647 case A_ILMIC:
9648 /* Get all packets in ILMI Circuit */
9649 if (!cstate->is_atm)
9650 bpf_error(cstate, "'ilmic' supported only on raw ATM");
9651 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9652 b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
9653 gen_and(b0, b1);
9654 break;
9655
9656 case A_LANE:
9657 /* Get all LANE packets */
9658 if (!cstate->is_atm)
9659 bpf_error(cstate, "'lane' supported only on raw ATM");
9660 b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
9661
9662 /*
9663 * Arrange that all subsequent tests assume LANE
9664 * rather than LLC-encapsulated packets, and set
9665 * the offsets appropriately for LANE-encapsulated
9666 * Ethernet.
9667 *
9668 * We assume LANE means Ethernet, not Token Ring.
9669 */
9670 PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
9671 cstate->off_payload + 2, /* Ethernet header */
9672 -1);
9673 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
9674 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
9675 cstate->off_nl = 0; /* Ethernet II */
9676 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
9677 break;
9678
9679 case A_LLC:
9680 /* Get all LLC-encapsulated packets */
9681 if (!cstate->is_atm)
9682 bpf_error(cstate, "'llc' supported only on raw ATM");
9683 b1 = gen_atmtype_llc(cstate);
9684 break;
9685
9686 default:
9687 abort();
9688 }
9689 return b1;
9690 }
9691
9692 /*
9693 * Filtering for MTP2 messages based on li value
9694 * FISU, length is null
9695 * LSSU, length is 1 or 2
9696 * MSU, length is 3 or more
9697 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9698 */
9699 struct block *
9700 gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
9701 {
9702 struct block *b0, *b1;
9703
9704 /*
9705 * Catch errors reported by us and routines below us, and return NULL
9706 * on an error.
9707 */
9708 if (setjmp(cstate->top_ctx))
9709 return (NULL);
9710
9711 switch (type) {
9712
9713 case M_FISU:
9714 if ( (cstate->linktype != DLT_MTP2) &&
9715 (cstate->linktype != DLT_ERF) &&
9716 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9717 bpf_error(cstate, "'fisu' supported only on MTP2");
9718 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9719 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9720 0x3fU, BPF_JEQ, 0, 0U);
9721 break;
9722
9723 case M_LSSU:
9724 if ( (cstate->linktype != DLT_MTP2) &&
9725 (cstate->linktype != DLT_ERF) &&
9726 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9727 bpf_error(cstate, "'lssu' supported only on MTP2");
9728 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9729 0x3fU, BPF_JGT, 1, 2U);
9730 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9731 0x3fU, BPF_JGT, 0, 0U);
9732 gen_and(b1, b0);
9733 break;
9734
9735 case M_MSU:
9736 if ( (cstate->linktype != DLT_MTP2) &&
9737 (cstate->linktype != DLT_ERF) &&
9738 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9739 bpf_error(cstate, "'msu' supported only on MTP2");
9740 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
9741 0x3fU, BPF_JGT, 0, 2U);
9742 break;
9743
9744 case MH_FISU:
9745 if ( (cstate->linktype != DLT_MTP2) &&
9746 (cstate->linktype != DLT_ERF) &&
9747 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9748 bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
9749 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9750 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9751 0xff80U, BPF_JEQ, 0, 0U);
9752 break;
9753
9754 case MH_LSSU:
9755 if ( (cstate->linktype != DLT_MTP2) &&
9756 (cstate->linktype != DLT_ERF) &&
9757 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9758 bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
9759 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9760 0xff80U, BPF_JGT, 1, 0x0100U);
9761 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9762 0xff80U, BPF_JGT, 0, 0U);
9763 gen_and(b1, b0);
9764 break;
9765
9766 case MH_MSU:
9767 if ( (cstate->linktype != DLT_MTP2) &&
9768 (cstate->linktype != DLT_ERF) &&
9769 (cstate->linktype != DLT_MTP2_WITH_PHDR) )
9770 bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
9771 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
9772 0xff80U, BPF_JGT, 0, 0x0100U);
9773 break;
9774
9775 default:
9776 abort();
9777 }
9778 return b0;
9779 }
9780
9781 /*
9782 * The jvalue_arg dance is to avoid annoying whining by compilers that
9783 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9784 * It's not *used* after setjmp returns.
9785 */
9786 struct block *
9787 gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
9788 bpf_u_int32 jvalue_arg, int jtype, int reverse)
9789 {
9790 volatile bpf_u_int32 jvalue = jvalue_arg;
9791 struct block *b0;
9792 bpf_u_int32 val1 , val2 , val3;
9793 u_int newoff_sio;
9794 u_int newoff_opc;
9795 u_int newoff_dpc;
9796 u_int newoff_sls;
9797
9798 /*
9799 * Catch errors reported by us and routines below us, and return NULL
9800 * on an error.
9801 */
9802 if (setjmp(cstate->top_ctx))
9803 return (NULL);
9804
9805 newoff_sio = cstate->off_sio;
9806 newoff_opc = cstate->off_opc;
9807 newoff_dpc = cstate->off_dpc;
9808 newoff_sls = cstate->off_sls;
9809 switch (mtp3field) {
9810
9811 case MH_SIO:
9812 newoff_sio += 3; /* offset for MTP2_HSL */
9813 /* FALLTHROUGH */
9814
9815 case M_SIO:
9816 if (cstate->off_sio == OFFSET_NOT_SET)
9817 bpf_error(cstate, "'sio' supported only on SS7");
9818 /* sio coded on 1 byte so max value 255 */
9819 if(jvalue > 255)
9820 bpf_error(cstate, "sio value %u too big; max value = 255",
9821 jvalue);
9822 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,
9823 jtype, reverse, jvalue);
9824 break;
9825
9826 case MH_OPC:
9827 newoff_opc += 3;
9828
9829 /* FALLTHROUGH */
9830 case M_OPC:
9831 if (cstate->off_opc == OFFSET_NOT_SET)
9832 bpf_error(cstate, "'opc' supported only on SS7");
9833 /* opc coded on 14 bits so max value 16383 */
9834 if (jvalue > 16383)
9835 bpf_error(cstate, "opc value %u too big; max value = 16383",
9836 jvalue);
9837 /* the following instructions are made to convert jvalue
9838 * to the form used to write opc in an ss7 message*/
9839 val1 = jvalue & 0x00003c00;
9840 val1 = val1 >>10;
9841 val2 = jvalue & 0x000003fc;
9842 val2 = val2 <<6;
9843 val3 = jvalue & 0x00000003;
9844 val3 = val3 <<22;
9845 jvalue = val1 + val2 + val3;
9846 b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,
9847 jtype, reverse, jvalue);
9848 break;
9849
9850 case MH_DPC:
9851 newoff_dpc += 3;
9852 /* FALLTHROUGH */
9853
9854 case M_DPC:
9855 if (cstate->off_dpc == OFFSET_NOT_SET)
9856 bpf_error(cstate, "'dpc' supported only on SS7");
9857 /* dpc coded on 14 bits so max value 16383 */
9858 if (jvalue > 16383)
9859 bpf_error(cstate, "dpc value %u too big; max value = 16383",
9860 jvalue);
9861 /* the following instructions are made to convert jvalue
9862 * to the forme used to write dpc in an ss7 message*/
9863 val1 = jvalue & 0x000000ff;
9864 val1 = val1 << 24;
9865 val2 = jvalue & 0x00003f00;
9866 val2 = val2 << 8;
9867 jvalue = val1 + val2;
9868 b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,
9869 jtype, reverse, jvalue);
9870 break;
9871
9872 case MH_SLS:
9873 newoff_sls += 3;
9874 /* FALLTHROUGH */
9875
9876 case M_SLS:
9877 if (cstate->off_sls == OFFSET_NOT_SET)
9878 bpf_error(cstate, "'sls' supported only on SS7");
9879 /* sls coded on 4 bits so max value 15 */
9880 if (jvalue > 15)
9881 bpf_error(cstate, "sls value %u too big; max value = 15",
9882 jvalue);
9883 /* the following instruction is made to convert jvalue
9884 * to the forme used to write sls in an ss7 message*/
9885 jvalue = jvalue << 4;
9886 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,
9887 jtype, reverse, jvalue);
9888 break;
9889
9890 default:
9891 abort();
9892 }
9893 return b0;
9894 }
9895
9896 static struct block *
9897 gen_msg_abbrev(compiler_state_t *cstate, int type)
9898 {
9899 struct block *b1;
9900
9901 /*
9902 * Q.2931 signalling protocol messages for handling virtual circuits
9903 * establishment and teardown
9904 */
9905 switch (type) {
9906
9907 case A_SETUP:
9908 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
9909 break;
9910
9911 case A_CALLPROCEED:
9912 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
9913 break;
9914
9915 case A_CONNECT:
9916 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
9917 break;
9918
9919 case A_CONNECTACK:
9920 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
9921 break;
9922
9923 case A_RELEASE:
9924 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
9925 break;
9926
9927 case A_RELEASE_DONE:
9928 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
9929 break;
9930
9931 default:
9932 abort();
9933 }
9934 return b1;
9935 }
9936
9937 struct block *
9938 gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
9939 {
9940 struct block *b0, *b1;
9941
9942 /*
9943 * Catch errors reported by us and routines below us, and return NULL
9944 * on an error.
9945 */
9946 if (setjmp(cstate->top_ctx))
9947 return (NULL);
9948
9949 switch (type) {
9950
9951 case A_OAM:
9952 if (!cstate->is_atm)
9953 bpf_error(cstate, "'oam' supported only on raw ATM");
9954 /* OAM F4 type */
9955 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9956 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9957 gen_or(b0, b1);
9958 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9959 gen_and(b0, b1);
9960 break;
9961
9962 case A_OAMF4:
9963 if (!cstate->is_atm)
9964 bpf_error(cstate, "'oamf4' supported only on raw ATM");
9965 /* OAM F4 type */
9966 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
9967 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
9968 gen_or(b0, b1);
9969 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
9970 gen_and(b0, b1);
9971 break;
9972
9973 case A_CONNECTMSG:
9974 /*
9975 * Get Q.2931 signalling messages for switched
9976 * virtual connection
9977 */
9978 if (!cstate->is_atm)
9979 bpf_error(cstate, "'connectmsg' supported only on raw ATM");
9980 b0 = gen_msg_abbrev(cstate, A_SETUP);
9981 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
9982 gen_or(b0, b1);
9983 b0 = gen_msg_abbrev(cstate, A_CONNECT);
9984 gen_or(b0, b1);
9985 b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
9986 gen_or(b0, b1);
9987 b0 = gen_msg_abbrev(cstate, A_RELEASE);
9988 gen_or(b0, b1);
9989 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
9990 gen_or(b0, b1);
9991 b0 = gen_atmtype_sc(cstate);
9992 gen_and(b0, b1);
9993 break;
9994
9995 case A_METACONNECT:
9996 if (!cstate->is_atm)
9997 bpf_error(cstate, "'metaconnect' supported only on raw ATM");
9998 b0 = gen_msg_abbrev(cstate, A_SETUP);
9999 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
10000 gen_or(b0, b1);
10001 b0 = gen_msg_abbrev(cstate, A_CONNECT);
10002 gen_or(b0, b1);
10003 b0 = gen_msg_abbrev(cstate, A_RELEASE);
10004 gen_or(b0, b1);
10005 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
10006 gen_or(b0, b1);
10007 b0 = gen_atmtype_metac(cstate);
10008 gen_and(b0, b1);
10009 break;
10010
10011 default:
10012 abort();
10013 }
10014 return b1;
10015 }