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