1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*#define CHASE_CHAIN*/ |
---|
4 | /* |
---|
5 | * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 |
---|
6 | * The Regents of the University of California. All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that: (1) source code distributions |
---|
10 | * retain the above copyright notice and this paragraph in its entirety, (2) |
---|
11 | * distributions including binary code include the above copyright notice and |
---|
12 | * this paragraph in its entirety in the documentation or other materials |
---|
13 | * provided with the distribution, and (3) all advertising materials mentioning |
---|
14 | * features or use of this software display the following acknowledgement: |
---|
15 | * ``This product includes software developed by the University of California, |
---|
16 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
---|
17 | * the University nor the names of its contributors may be used to endorse |
---|
18 | * or promote products derived from this software without specific prior |
---|
19 | * written permission. |
---|
20 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
---|
21 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
---|
22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
23 | * |
---|
24 | * $FreeBSD$ |
---|
25 | */ |
---|
26 | #ifndef lint |
---|
27 | static const char rcsid[] _U_ = |
---|
28 | "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)"; |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HAVE_CONFIG_H |
---|
32 | #include "config.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifdef WIN32 |
---|
36 | #include <pcap-stdinc.h> |
---|
37 | #else /* WIN32 */ |
---|
38 | #if HAVE_INTTYPES_H |
---|
39 | #include <inttypes.h> |
---|
40 | #elif HAVE_STDINT_H |
---|
41 | #include <stdint.h> |
---|
42 | #endif |
---|
43 | #ifdef HAVE_SYS_BITYPES_H |
---|
44 | #include <sys/bitypes.h> |
---|
45 | #endif |
---|
46 | #include <rtems/bsd/sys/types.h> |
---|
47 | #include <sys/socket.h> |
---|
48 | #endif /* WIN32 */ |
---|
49 | |
---|
50 | /* |
---|
51 | * XXX - why was this included even on UNIX? |
---|
52 | */ |
---|
53 | #ifdef __MINGW32__ |
---|
54 | #include "ip6_misc.h" |
---|
55 | #endif |
---|
56 | |
---|
57 | #ifndef WIN32 |
---|
58 | |
---|
59 | #ifdef __NetBSD__ |
---|
60 | #include <rtems/bsd/sys/param.h> |
---|
61 | #endif |
---|
62 | |
---|
63 | #include <netinet/in.h> |
---|
64 | #include <arpa/inet.h> |
---|
65 | |
---|
66 | #endif /* WIN32 */ |
---|
67 | |
---|
68 | #include <stdlib.h> |
---|
69 | #include <string.h> |
---|
70 | #include <memory.h> |
---|
71 | #include <setjmp.h> |
---|
72 | #include <stdarg.h> |
---|
73 | |
---|
74 | #ifdef MSDOS |
---|
75 | #include "pcap-dos.h" |
---|
76 | #endif |
---|
77 | |
---|
78 | #include "pcap-int.h" |
---|
79 | |
---|
80 | #include "ethertype.h" |
---|
81 | #include "nlpid.h" |
---|
82 | #include "llc.h" |
---|
83 | #include "gencode.h" |
---|
84 | #include "ieee80211.h" |
---|
85 | #include "atmuni31.h" |
---|
86 | #include "sunatmpos.h" |
---|
87 | #include "ppp.h" |
---|
88 | #include "pcap/sll.h" |
---|
89 | #include "pcap/ipnet.h" |
---|
90 | #include "arcnet.h" |
---|
91 | #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER) |
---|
92 | #include <linux/types.h> |
---|
93 | #include <linux/if_packet.h> |
---|
94 | #include <linux/filter.h> |
---|
95 | #endif |
---|
96 | #ifdef HAVE_NET_PFVAR_H |
---|
97 | #include <sys/socket.h> |
---|
98 | #include <net/if.h> |
---|
99 | #include <net/pfvar.h> |
---|
100 | #include <net/if_pflog.h> |
---|
101 | #endif |
---|
102 | #ifndef offsetof |
---|
103 | #define offsetof(s, e) ((size_t)&((s *)0)->e) |
---|
104 | #endif |
---|
105 | #ifdef INET6 |
---|
106 | #ifndef WIN32 |
---|
107 | #include <netdb.h> /* for "struct addrinfo" */ |
---|
108 | #endif /* WIN32 */ |
---|
109 | #endif /*INET6*/ |
---|
110 | #include <pcap/namedb.h> |
---|
111 | |
---|
112 | #define ETHERMTU 1500 |
---|
113 | |
---|
114 | #ifndef IPPROTO_HOPOPTS |
---|
115 | #define IPPROTO_HOPOPTS 0 |
---|
116 | #endif |
---|
117 | #ifndef IPPROTO_ROUTING |
---|
118 | #define IPPROTO_ROUTING 43 |
---|
119 | #endif |
---|
120 | #ifndef IPPROTO_FRAGMENT |
---|
121 | #define IPPROTO_FRAGMENT 44 |
---|
122 | #endif |
---|
123 | #ifndef IPPROTO_DSTOPTS |
---|
124 | #define IPPROTO_DSTOPTS 60 |
---|
125 | #endif |
---|
126 | #ifndef IPPROTO_SCTP |
---|
127 | #define IPPROTO_SCTP 132 |
---|
128 | #endif |
---|
129 | |
---|
130 | #ifdef HAVE_OS_PROTO_H |
---|
131 | #include "os-proto.h" |
---|
132 | #endif |
---|
133 | |
---|
134 | #define JMP(c) ((c)|BPF_JMP|BPF_K) |
---|
135 | |
---|
136 | /* Locals */ |
---|
137 | static jmp_buf top_ctx; |
---|
138 | static pcap_t *bpf_pcap; |
---|
139 | |
---|
140 | /* Hack for updating VLAN, MPLS, and PPPoE offsets. */ |
---|
141 | #ifdef WIN32 |
---|
142 | static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1; |
---|
143 | #else |
---|
144 | static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U; |
---|
145 | #endif |
---|
146 | |
---|
147 | /* XXX */ |
---|
148 | #ifdef PCAP_FDDIPAD |
---|
149 | static int pcap_fddipad; |
---|
150 | #endif |
---|
151 | |
---|
152 | /* VARARGS */ |
---|
153 | void |
---|
154 | bpf_error(const char *fmt, ...) |
---|
155 | { |
---|
156 | va_list ap; |
---|
157 | |
---|
158 | va_start(ap, fmt); |
---|
159 | if (bpf_pcap != NULL) |
---|
160 | (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE, |
---|
161 | fmt, ap); |
---|
162 | va_end(ap); |
---|
163 | longjmp(top_ctx, 1); |
---|
164 | /* NOTREACHED */ |
---|
165 | } |
---|
166 | |
---|
167 | static void init_linktype(pcap_t *); |
---|
168 | |
---|
169 | static void init_regs(void); |
---|
170 | static int alloc_reg(void); |
---|
171 | static void free_reg(int); |
---|
172 | |
---|
173 | static struct block *root; |
---|
174 | |
---|
175 | /* |
---|
176 | * Value passed to gen_load_a() to indicate what the offset argument |
---|
177 | * is relative to. |
---|
178 | */ |
---|
179 | enum e_offrel { |
---|
180 | OR_PACKET, /* relative to the beginning of the packet */ |
---|
181 | OR_LINK, /* relative to the beginning of the link-layer header */ |
---|
182 | OR_MACPL, /* relative to the end of the MAC-layer header */ |
---|
183 | OR_NET, /* relative to the network-layer header */ |
---|
184 | OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */ |
---|
185 | OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */ |
---|
186 | OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */ |
---|
187 | }; |
---|
188 | |
---|
189 | #ifdef INET6 |
---|
190 | /* |
---|
191 | * As errors are handled by a longjmp, anything allocated must be freed |
---|
192 | * in the longjmp handler, so it must be reachable from that handler. |
---|
193 | * One thing that's allocated is the result of pcap_nametoaddrinfo(); |
---|
194 | * it must be freed with freeaddrinfo(). This variable points to any |
---|
195 | * addrinfo structure that would need to be freed. |
---|
196 | */ |
---|
197 | static struct addrinfo *ai; |
---|
198 | #endif |
---|
199 | |
---|
200 | /* |
---|
201 | * We divy out chunks of memory rather than call malloc each time so |
---|
202 | * we don't have to worry about leaking memory. It's probably |
---|
203 | * not a big deal if all this memory was wasted but if this ever |
---|
204 | * goes into a library that would probably not be a good idea. |
---|
205 | * |
---|
206 | * XXX - this *is* in a library.... |
---|
207 | */ |
---|
208 | #define NCHUNKS 16 |
---|
209 | #define CHUNK0SIZE 1024 |
---|
210 | struct chunk { |
---|
211 | u_int n_left; |
---|
212 | void *m; |
---|
213 | }; |
---|
214 | |
---|
215 | static struct chunk chunks[NCHUNKS]; |
---|
216 | static int cur_chunk; |
---|
217 | |
---|
218 | static void *newchunk(u_int); |
---|
219 | static void freechunks(void); |
---|
220 | static inline struct block *new_block(int); |
---|
221 | static inline struct slist *new_stmt(int); |
---|
222 | static struct block *gen_retblk(int); |
---|
223 | static inline void syntax(void); |
---|
224 | |
---|
225 | static void backpatch(struct block *, struct block *); |
---|
226 | static void merge(struct block *, struct block *); |
---|
227 | static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32); |
---|
228 | static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32); |
---|
229 | static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32); |
---|
230 | static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32); |
---|
231 | static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32); |
---|
232 | static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32, |
---|
233 | bpf_u_int32); |
---|
234 | static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *); |
---|
235 | static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32, |
---|
236 | bpf_u_int32, bpf_u_int32, int, bpf_int32); |
---|
237 | static struct slist *gen_load_llrel(u_int, u_int); |
---|
238 | static struct slist *gen_load_macplrel(u_int, u_int); |
---|
239 | static struct slist *gen_load_a(enum e_offrel, u_int, u_int); |
---|
240 | static struct slist *gen_loadx_iphdrlen(void); |
---|
241 | static struct block *gen_uncond(int); |
---|
242 | static inline struct block *gen_true(void); |
---|
243 | static inline struct block *gen_false(void); |
---|
244 | static struct block *gen_ether_linktype(int); |
---|
245 | static struct block *gen_ipnet_linktype(int); |
---|
246 | static struct block *gen_linux_sll_linktype(int); |
---|
247 | static struct slist *gen_load_prism_llprefixlen(void); |
---|
248 | static struct slist *gen_load_avs_llprefixlen(void); |
---|
249 | static struct slist *gen_load_radiotap_llprefixlen(void); |
---|
250 | static struct slist *gen_load_ppi_llprefixlen(void); |
---|
251 | static void insert_compute_vloffsets(struct block *); |
---|
252 | static struct slist *gen_llprefixlen(void); |
---|
253 | static struct slist *gen_off_macpl(void); |
---|
254 | static int ethertype_to_ppptype(int); |
---|
255 | static struct block *gen_linktype(int); |
---|
256 | static struct block *gen_snap(bpf_u_int32, bpf_u_int32); |
---|
257 | static struct block *gen_llc_linktype(int); |
---|
258 | static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int); |
---|
259 | #ifdef INET6 |
---|
260 | static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int); |
---|
261 | #endif |
---|
262 | static struct block *gen_ahostop(const u_char *, int); |
---|
263 | static struct block *gen_ehostop(const u_char *, int); |
---|
264 | static struct block *gen_fhostop(const u_char *, int); |
---|
265 | static struct block *gen_thostop(const u_char *, int); |
---|
266 | static struct block *gen_wlanhostop(const u_char *, int); |
---|
267 | static struct block *gen_ipfchostop(const u_char *, int); |
---|
268 | static struct block *gen_dnhostop(bpf_u_int32, int); |
---|
269 | static struct block *gen_mpls_linktype(int); |
---|
270 | static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int); |
---|
271 | #ifdef INET6 |
---|
272 | static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int); |
---|
273 | #endif |
---|
274 | #ifndef INET6 |
---|
275 | static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int); |
---|
276 | #endif |
---|
277 | static struct block *gen_ipfrag(void); |
---|
278 | static struct block *gen_portatom(int, bpf_int32); |
---|
279 | static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32); |
---|
280 | static struct block *gen_portatom6(int, bpf_int32); |
---|
281 | static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32); |
---|
282 | struct block *gen_portop(int, int, int); |
---|
283 | static struct block *gen_port(int, int, int); |
---|
284 | struct block *gen_portrangeop(int, int, int, int); |
---|
285 | static struct block *gen_portrange(int, int, int, int); |
---|
286 | struct block *gen_portop6(int, int, int); |
---|
287 | static struct block *gen_port6(int, int, int); |
---|
288 | struct block *gen_portrangeop6(int, int, int, int); |
---|
289 | static struct block *gen_portrange6(int, int, int, int); |
---|
290 | static int lookup_proto(const char *, int); |
---|
291 | static struct block *gen_protochain(int, int, int); |
---|
292 | static struct block *gen_proto(int, int, int); |
---|
293 | static struct slist *xfer_to_x(struct arth *); |
---|
294 | static struct slist *xfer_to_a(struct arth *); |
---|
295 | static struct block *gen_mac_multicast(int); |
---|
296 | static struct block *gen_len(int, int); |
---|
297 | static struct block *gen_check_802_11_data_frame(void); |
---|
298 | |
---|
299 | static struct block *gen_ppi_dlt_check(void); |
---|
300 | static struct block *gen_msg_abbrev(int type); |
---|
301 | |
---|
302 | static void * |
---|
303 | newchunk(n) |
---|
304 | u_int n; |
---|
305 | { |
---|
306 | struct chunk *cp; |
---|
307 | int k; |
---|
308 | size_t size; |
---|
309 | |
---|
310 | #ifndef __NetBSD__ |
---|
311 | /* XXX Round up to nearest long. */ |
---|
312 | n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1); |
---|
313 | #else |
---|
314 | /* XXX Round up to structure boundary. */ |
---|
315 | n = ALIGN(n); |
---|
316 | #endif |
---|
317 | |
---|
318 | cp = &chunks[cur_chunk]; |
---|
319 | if (n > cp->n_left) { |
---|
320 | ++cp, k = ++cur_chunk; |
---|
321 | if (k >= NCHUNKS) |
---|
322 | bpf_error("out of memory"); |
---|
323 | size = CHUNK0SIZE << k; |
---|
324 | cp->m = (void *)malloc(size); |
---|
325 | if (cp->m == NULL) |
---|
326 | bpf_error("out of memory"); |
---|
327 | memset((char *)cp->m, 0, size); |
---|
328 | cp->n_left = size; |
---|
329 | if (n > size) |
---|
330 | bpf_error("out of memory"); |
---|
331 | } |
---|
332 | cp->n_left -= n; |
---|
333 | return (void *)((char *)cp->m + cp->n_left); |
---|
334 | } |
---|
335 | |
---|
336 | static void |
---|
337 | freechunks() |
---|
338 | { |
---|
339 | int i; |
---|
340 | |
---|
341 | cur_chunk = 0; |
---|
342 | for (i = 0; i < NCHUNKS; ++i) |
---|
343 | if (chunks[i].m != NULL) { |
---|
344 | free(chunks[i].m); |
---|
345 | chunks[i].m = NULL; |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | /* |
---|
350 | * A strdup whose allocations are freed after code generation is over. |
---|
351 | */ |
---|
352 | char * |
---|
353 | sdup(s) |
---|
354 | register const char *s; |
---|
355 | { |
---|
356 | int n = strlen(s) + 1; |
---|
357 | char *cp = newchunk(n); |
---|
358 | |
---|
359 | strlcpy(cp, s, n); |
---|
360 | return (cp); |
---|
361 | } |
---|
362 | |
---|
363 | static inline struct block * |
---|
364 | new_block(code) |
---|
365 | int code; |
---|
366 | { |
---|
367 | struct block *p; |
---|
368 | |
---|
369 | p = (struct block *)newchunk(sizeof(*p)); |
---|
370 | p->s.code = code; |
---|
371 | p->head = p; |
---|
372 | |
---|
373 | return p; |
---|
374 | } |
---|
375 | |
---|
376 | static inline struct slist * |
---|
377 | new_stmt(code) |
---|
378 | int code; |
---|
379 | { |
---|
380 | struct slist *p; |
---|
381 | |
---|
382 | p = (struct slist *)newchunk(sizeof(*p)); |
---|
383 | p->s.code = code; |
---|
384 | |
---|
385 | return p; |
---|
386 | } |
---|
387 | |
---|
388 | static struct block * |
---|
389 | gen_retblk(v) |
---|
390 | int v; |
---|
391 | { |
---|
392 | struct block *b = new_block(BPF_RET|BPF_K); |
---|
393 | |
---|
394 | b->s.k = v; |
---|
395 | return b; |
---|
396 | } |
---|
397 | |
---|
398 | static inline void |
---|
399 | syntax() |
---|
400 | { |
---|
401 | bpf_error("syntax error in filter expression"); |
---|
402 | } |
---|
403 | |
---|
404 | static bpf_u_int32 netmask; |
---|
405 | static int snaplen; |
---|
406 | int no_optimize; |
---|
407 | #ifdef WIN32 |
---|
408 | static int |
---|
409 | pcap_compile_unsafe(pcap_t *p, struct bpf_program *program, |
---|
410 | const char *buf, int optimize, bpf_u_int32 mask); |
---|
411 | |
---|
412 | int |
---|
413 | pcap_compile(pcap_t *p, struct bpf_program *program, |
---|
414 | const char *buf, int optimize, bpf_u_int32 mask) |
---|
415 | { |
---|
416 | int result; |
---|
417 | |
---|
418 | EnterCriticalSection(&g_PcapCompileCriticalSection); |
---|
419 | |
---|
420 | result = pcap_compile_unsafe(p, program, buf, optimize, mask); |
---|
421 | |
---|
422 | LeaveCriticalSection(&g_PcapCompileCriticalSection); |
---|
423 | |
---|
424 | return result; |
---|
425 | } |
---|
426 | |
---|
427 | static int |
---|
428 | pcap_compile_unsafe(pcap_t *p, struct bpf_program *program, |
---|
429 | const char *buf, int optimize, bpf_u_int32 mask) |
---|
430 | #else /* WIN32 */ |
---|
431 | int |
---|
432 | pcap_compile(pcap_t *p, struct bpf_program *program, |
---|
433 | const char *buf, int optimize, bpf_u_int32 mask) |
---|
434 | #endif /* WIN32 */ |
---|
435 | { |
---|
436 | #ifdef __rtems__ |
---|
437 | int n_errors; |
---|
438 | #else |
---|
439 | extern int n_errors; |
---|
440 | #endif /* __rtems__ */ |
---|
441 | const char * volatile xbuf = buf; |
---|
442 | u_int len; |
---|
443 | |
---|
444 | /* |
---|
445 | * If this pcap_t hasn't been activated, it doesn't have a |
---|
446 | * link-layer type, so we can't use it. |
---|
447 | */ |
---|
448 | if (!p->activated) { |
---|
449 | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
---|
450 | "not-yet-activated pcap_t passed to pcap_compile"); |
---|
451 | return (-1); |
---|
452 | } |
---|
453 | no_optimize = 0; |
---|
454 | n_errors = 0; |
---|
455 | root = NULL; |
---|
456 | bpf_pcap = p; |
---|
457 | init_regs(); |
---|
458 | if (setjmp(top_ctx)) { |
---|
459 | #ifdef INET6 |
---|
460 | if (ai != NULL) { |
---|
461 | freeaddrinfo(ai); |
---|
462 | ai = NULL; |
---|
463 | } |
---|
464 | #endif |
---|
465 | lex_cleanup(); |
---|
466 | freechunks(); |
---|
467 | return (-1); |
---|
468 | } |
---|
469 | |
---|
470 | netmask = mask; |
---|
471 | |
---|
472 | snaplen = pcap_snapshot(p); |
---|
473 | if (snaplen == 0) { |
---|
474 | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
---|
475 | "snaplen of 0 rejects all packets"); |
---|
476 | return -1; |
---|
477 | } |
---|
478 | |
---|
479 | lex_init(xbuf ? xbuf : ""); |
---|
480 | init_linktype(p); |
---|
481 | (void)pcap_parse(); |
---|
482 | |
---|
483 | if (n_errors) |
---|
484 | syntax(); |
---|
485 | |
---|
486 | if (root == NULL) |
---|
487 | root = gen_retblk(snaplen); |
---|
488 | |
---|
489 | if (optimize && !no_optimize) { |
---|
490 | bpf_optimize(&root); |
---|
491 | if (root == NULL || |
---|
492 | (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0)) |
---|
493 | bpf_error("expression rejects all packets"); |
---|
494 | } |
---|
495 | program->bf_insns = icode_to_fcode(root, &len); |
---|
496 | program->bf_len = len; |
---|
497 | |
---|
498 | lex_cleanup(); |
---|
499 | freechunks(); |
---|
500 | return (0); |
---|
501 | } |
---|
502 | |
---|
503 | /* |
---|
504 | * entry point for using the compiler with no pcap open |
---|
505 | * pass in all the stuff that is needed explicitly instead. |
---|
506 | */ |
---|
507 | int |
---|
508 | pcap_compile_nopcap(int snaplen_arg, int linktype_arg, |
---|
509 | struct bpf_program *program, |
---|
510 | const char *buf, int optimize, bpf_u_int32 mask) |
---|
511 | { |
---|
512 | pcap_t *p; |
---|
513 | int ret; |
---|
514 | |
---|
515 | p = pcap_open_dead(linktype_arg, snaplen_arg); |
---|
516 | if (p == NULL) |
---|
517 | return (-1); |
---|
518 | ret = pcap_compile(p, program, buf, optimize, mask); |
---|
519 | pcap_close(p); |
---|
520 | return (ret); |
---|
521 | } |
---|
522 | |
---|
523 | /* |
---|
524 | * Clean up a "struct bpf_program" by freeing all the memory allocated |
---|
525 | * in it. |
---|
526 | */ |
---|
527 | void |
---|
528 | pcap_freecode(struct bpf_program *program) |
---|
529 | { |
---|
530 | program->bf_len = 0; |
---|
531 | if (program->bf_insns != NULL) { |
---|
532 | free((char *)program->bf_insns); |
---|
533 | program->bf_insns = NULL; |
---|
534 | } |
---|
535 | } |
---|
536 | |
---|
537 | /* |
---|
538 | * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates |
---|
539 | * which of the jt and jf fields has been resolved and which is a pointer |
---|
540 | * back to another unresolved block (or nil). At least one of the fields |
---|
541 | * in each block is already resolved. |
---|
542 | */ |
---|
543 | static void |
---|
544 | backpatch(list, target) |
---|
545 | struct block *list, *target; |
---|
546 | { |
---|
547 | struct block *next; |
---|
548 | |
---|
549 | while (list) { |
---|
550 | if (!list->sense) { |
---|
551 | next = JT(list); |
---|
552 | JT(list) = target; |
---|
553 | } else { |
---|
554 | next = JF(list); |
---|
555 | JF(list) = target; |
---|
556 | } |
---|
557 | list = next; |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | /* |
---|
562 | * Merge the lists in b0 and b1, using the 'sense' field to indicate |
---|
563 | * which of jt and jf is the link. |
---|
564 | */ |
---|
565 | static void |
---|
566 | merge(b0, b1) |
---|
567 | struct block *b0, *b1; |
---|
568 | { |
---|
569 | register struct block **p = &b0; |
---|
570 | |
---|
571 | /* Find end of list. */ |
---|
572 | while (*p) |
---|
573 | p = !((*p)->sense) ? &JT(*p) : &JF(*p); |
---|
574 | |
---|
575 | /* Concatenate the lists. */ |
---|
576 | *p = b1; |
---|
577 | } |
---|
578 | |
---|
579 | void |
---|
580 | finish_parse(p) |
---|
581 | struct block *p; |
---|
582 | { |
---|
583 | struct block *ppi_dlt_check; |
---|
584 | |
---|
585 | /* |
---|
586 | * Insert before the statements of the first (root) block any |
---|
587 | * statements needed to load the lengths of any variable-length |
---|
588 | * headers into registers. |
---|
589 | * |
---|
590 | * XXX - a fancier strategy would be to insert those before the |
---|
591 | * statements of all blocks that use those lengths and that |
---|
592 | * have no predecessors that use them, so that we only compute |
---|
593 | * the lengths if we need them. There might be even better |
---|
594 | * approaches than that. |
---|
595 | * |
---|
596 | * However, those strategies would be more complicated, and |
---|
597 | * as we don't generate code to compute a length if the |
---|
598 | * program has no tests that use the length, and as most |
---|
599 | * tests will probably use those lengths, we would just |
---|
600 | * postpone computing the lengths so that it's not done |
---|
601 | * for tests that fail early, and it's not clear that's |
---|
602 | * worth the effort. |
---|
603 | */ |
---|
604 | insert_compute_vloffsets(p->head); |
---|
605 | |
---|
606 | /* |
---|
607 | * For DLT_PPI captures, generate a check of the per-packet |
---|
608 | * DLT value to make sure it's DLT_IEEE802_11. |
---|
609 | */ |
---|
610 | ppi_dlt_check = gen_ppi_dlt_check(); |
---|
611 | if (ppi_dlt_check != NULL) |
---|
612 | gen_and(ppi_dlt_check, p); |
---|
613 | |
---|
614 | backpatch(p, gen_retblk(snaplen)); |
---|
615 | p->sense = !p->sense; |
---|
616 | backpatch(p, gen_retblk(0)); |
---|
617 | root = p->head; |
---|
618 | } |
---|
619 | |
---|
620 | void |
---|
621 | gen_and(b0, b1) |
---|
622 | struct block *b0, *b1; |
---|
623 | { |
---|
624 | backpatch(b0, b1->head); |
---|
625 | b0->sense = !b0->sense; |
---|
626 | b1->sense = !b1->sense; |
---|
627 | merge(b1, b0); |
---|
628 | b1->sense = !b1->sense; |
---|
629 | b1->head = b0->head; |
---|
630 | } |
---|
631 | |
---|
632 | void |
---|
633 | gen_or(b0, b1) |
---|
634 | struct block *b0, *b1; |
---|
635 | { |
---|
636 | b0->sense = !b0->sense; |
---|
637 | backpatch(b0, b1->head); |
---|
638 | b0->sense = !b0->sense; |
---|
639 | merge(b1, b0); |
---|
640 | b1->head = b0->head; |
---|
641 | } |
---|
642 | |
---|
643 | void |
---|
644 | gen_not(b) |
---|
645 | struct block *b; |
---|
646 | { |
---|
647 | b->sense = !b->sense; |
---|
648 | } |
---|
649 | |
---|
650 | static struct block * |
---|
651 | gen_cmp(offrel, offset, size, v) |
---|
652 | enum e_offrel offrel; |
---|
653 | u_int offset, size; |
---|
654 | bpf_int32 v; |
---|
655 | { |
---|
656 | return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v); |
---|
657 | } |
---|
658 | |
---|
659 | static struct block * |
---|
660 | gen_cmp_gt(offrel, offset, size, v) |
---|
661 | enum e_offrel offrel; |
---|
662 | u_int offset, size; |
---|
663 | bpf_int32 v; |
---|
664 | { |
---|
665 | return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v); |
---|
666 | } |
---|
667 | |
---|
668 | static struct block * |
---|
669 | gen_cmp_ge(offrel, offset, size, v) |
---|
670 | enum e_offrel offrel; |
---|
671 | u_int offset, size; |
---|
672 | bpf_int32 v; |
---|
673 | { |
---|
674 | return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v); |
---|
675 | } |
---|
676 | |
---|
677 | static struct block * |
---|
678 | gen_cmp_lt(offrel, offset, size, v) |
---|
679 | enum e_offrel offrel; |
---|
680 | u_int offset, size; |
---|
681 | bpf_int32 v; |
---|
682 | { |
---|
683 | return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v); |
---|
684 | } |
---|
685 | |
---|
686 | static struct block * |
---|
687 | gen_cmp_le(offrel, offset, size, v) |
---|
688 | enum e_offrel offrel; |
---|
689 | u_int offset, size; |
---|
690 | bpf_int32 v; |
---|
691 | { |
---|
692 | return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v); |
---|
693 | } |
---|
694 | |
---|
695 | static struct block * |
---|
696 | gen_mcmp(offrel, offset, size, v, mask) |
---|
697 | enum e_offrel offrel; |
---|
698 | u_int offset, size; |
---|
699 | bpf_int32 v; |
---|
700 | bpf_u_int32 mask; |
---|
701 | { |
---|
702 | return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v); |
---|
703 | } |
---|
704 | |
---|
705 | static struct block * |
---|
706 | gen_bcmp(offrel, offset, size, v) |
---|
707 | enum e_offrel offrel; |
---|
708 | register u_int offset, size; |
---|
709 | register const u_char *v; |
---|
710 | { |
---|
711 | register struct block *b, *tmp; |
---|
712 | |
---|
713 | b = NULL; |
---|
714 | while (size >= 4) { |
---|
715 | register const u_char *p = &v[size - 4]; |
---|
716 | bpf_int32 w = ((bpf_int32)p[0] << 24) | |
---|
717 | ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3]; |
---|
718 | |
---|
719 | tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w); |
---|
720 | if (b != NULL) |
---|
721 | gen_and(b, tmp); |
---|
722 | b = tmp; |
---|
723 | size -= 4; |
---|
724 | } |
---|
725 | while (size >= 2) { |
---|
726 | register const u_char *p = &v[size - 2]; |
---|
727 | bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1]; |
---|
728 | |
---|
729 | tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w); |
---|
730 | if (b != NULL) |
---|
731 | gen_and(b, tmp); |
---|
732 | b = tmp; |
---|
733 | size -= 2; |
---|
734 | } |
---|
735 | if (size > 0) { |
---|
736 | tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]); |
---|
737 | if (b != NULL) |
---|
738 | gen_and(b, tmp); |
---|
739 | b = tmp; |
---|
740 | } |
---|
741 | return b; |
---|
742 | } |
---|
743 | |
---|
744 | /* |
---|
745 | * AND the field of size "size" at offset "offset" relative to the header |
---|
746 | * specified by "offrel" with "mask", and compare it with the value "v" |
---|
747 | * with the test specified by "jtype"; if "reverse" is true, the test |
---|
748 | * should test the opposite of "jtype". |
---|
749 | */ |
---|
750 | static struct block * |
---|
751 | gen_ncmp(offrel, offset, size, mask, jtype, reverse, v) |
---|
752 | enum e_offrel offrel; |
---|
753 | bpf_int32 v; |
---|
754 | bpf_u_int32 offset, size, mask, jtype; |
---|
755 | int reverse; |
---|
756 | { |
---|
757 | struct slist *s, *s2; |
---|
758 | struct block *b; |
---|
759 | |
---|
760 | s = gen_load_a(offrel, offset, size); |
---|
761 | |
---|
762 | if (mask != 0xffffffff) { |
---|
763 | s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K); |
---|
764 | s2->s.k = mask; |
---|
765 | sappend(s, s2); |
---|
766 | } |
---|
767 | |
---|
768 | b = new_block(JMP(jtype)); |
---|
769 | b->stmts = s; |
---|
770 | b->s.k = v; |
---|
771 | if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE)) |
---|
772 | gen_not(b); |
---|
773 | return b; |
---|
774 | } |
---|
775 | |
---|
776 | /* |
---|
777 | * Various code constructs need to know the layout of the data link |
---|
778 | * layer. These variables give the necessary offsets from the beginning |
---|
779 | * of the packet data. |
---|
780 | */ |
---|
781 | |
---|
782 | /* |
---|
783 | * This is the offset of the beginning of the link-layer header from |
---|
784 | * the beginning of the raw packet data. |
---|
785 | * |
---|
786 | * It's usually 0, except for 802.11 with a fixed-length radio header. |
---|
787 | * (For 802.11 with a variable-length radio header, we have to generate |
---|
788 | * code to compute that offset; off_ll is 0 in that case.) |
---|
789 | */ |
---|
790 | static u_int off_ll; |
---|
791 | |
---|
792 | /* |
---|
793 | * If there's a variable-length header preceding the link-layer header, |
---|
794 | * "reg_off_ll" is the register number for a register containing the |
---|
795 | * length of that header, and therefore the offset of the link-layer |
---|
796 | * header from the beginning of the raw packet data. Otherwise, |
---|
797 | * "reg_off_ll" is -1. |
---|
798 | */ |
---|
799 | static int reg_off_ll; |
---|
800 | |
---|
801 | /* |
---|
802 | * This is the offset of the beginning of the MAC-layer header from |
---|
803 | * the beginning of the link-layer header. |
---|
804 | * It's usually 0, except for ATM LANE, where it's the offset, relative |
---|
805 | * to the beginning of the raw packet data, of the Ethernet header, and |
---|
806 | * for Ethernet with various additional information. |
---|
807 | */ |
---|
808 | static u_int off_mac; |
---|
809 | |
---|
810 | /* |
---|
811 | * This is the offset of the beginning of the MAC-layer payload, |
---|
812 | * from the beginning of the raw packet data. |
---|
813 | * |
---|
814 | * I.e., it's the sum of the length of the link-layer header (without, |
---|
815 | * for example, any 802.2 LLC header, so it's the MAC-layer |
---|
816 | * portion of that header), plus any prefix preceding the |
---|
817 | * link-layer header. |
---|
818 | */ |
---|
819 | static u_int off_macpl; |
---|
820 | |
---|
821 | /* |
---|
822 | * This is 1 if the offset of the beginning of the MAC-layer payload |
---|
823 | * from the beginning of the link-layer header is variable-length. |
---|
824 | */ |
---|
825 | static int off_macpl_is_variable; |
---|
826 | |
---|
827 | /* |
---|
828 | * If the link layer has variable_length headers, "reg_off_macpl" |
---|
829 | * is the register number for a register containing the length of the |
---|
830 | * link-layer header plus the length of any variable-length header |
---|
831 | * preceding the link-layer header. Otherwise, "reg_off_macpl" |
---|
832 | * is -1. |
---|
833 | */ |
---|
834 | static int reg_off_macpl; |
---|
835 | |
---|
836 | /* |
---|
837 | * "off_linktype" is the offset to information in the link-layer header |
---|
838 | * giving the packet type. This offset is relative to the beginning |
---|
839 | * of the link-layer header (i.e., it doesn't include off_ll). |
---|
840 | * |
---|
841 | * For Ethernet, it's the offset of the Ethernet type field. |
---|
842 | * |
---|
843 | * For link-layer types that always use 802.2 headers, it's the |
---|
844 | * offset of the LLC header. |
---|
845 | * |
---|
846 | * For PPP, it's the offset of the PPP type field. |
---|
847 | * |
---|
848 | * For Cisco HDLC, it's the offset of the CHDLC type field. |
---|
849 | * |
---|
850 | * For BSD loopback, it's the offset of the AF_ value. |
---|
851 | * |
---|
852 | * For Linux cooked sockets, it's the offset of the type field. |
---|
853 | * |
---|
854 | * It's set to -1 for no encapsulation, in which case, IP is assumed. |
---|
855 | */ |
---|
856 | static u_int off_linktype; |
---|
857 | |
---|
858 | /* |
---|
859 | * TRUE if "pppoes" appeared in the filter; it causes link-layer type |
---|
860 | * checks to check the PPP header, assumed to follow a LAN-style link- |
---|
861 | * layer header and a PPPoE session header. |
---|
862 | */ |
---|
863 | static int is_pppoes = 0; |
---|
864 | |
---|
865 | /* |
---|
866 | * TRUE if the link layer includes an ATM pseudo-header. |
---|
867 | */ |
---|
868 | static int is_atm = 0; |
---|
869 | |
---|
870 | /* |
---|
871 | * TRUE if "lane" appeared in the filter; it causes us to generate |
---|
872 | * code that assumes LANE rather than LLC-encapsulated traffic in SunATM. |
---|
873 | */ |
---|
874 | static int is_lane = 0; |
---|
875 | |
---|
876 | /* |
---|
877 | * These are offsets for the ATM pseudo-header. |
---|
878 | */ |
---|
879 | static u_int off_vpi; |
---|
880 | static u_int off_vci; |
---|
881 | static u_int off_proto; |
---|
882 | |
---|
883 | /* |
---|
884 | * These are offsets for the MTP2 fields. |
---|
885 | */ |
---|
886 | static u_int off_li; |
---|
887 | |
---|
888 | /* |
---|
889 | * These are offsets for the MTP3 fields. |
---|
890 | */ |
---|
891 | static u_int off_sio; |
---|
892 | static u_int off_opc; |
---|
893 | static u_int off_dpc; |
---|
894 | static u_int off_sls; |
---|
895 | |
---|
896 | /* |
---|
897 | * This is the offset of the first byte after the ATM pseudo_header, |
---|
898 | * or -1 if there is no ATM pseudo-header. |
---|
899 | */ |
---|
900 | static u_int off_payload; |
---|
901 | |
---|
902 | /* |
---|
903 | * These are offsets to the beginning of the network-layer header. |
---|
904 | * They are relative to the beginning of the MAC-layer payload (i.e., |
---|
905 | * they don't include off_ll or off_macpl). |
---|
906 | * |
---|
907 | * If the link layer never uses 802.2 LLC: |
---|
908 | * |
---|
909 | * "off_nl" and "off_nl_nosnap" are the same. |
---|
910 | * |
---|
911 | * If the link layer always uses 802.2 LLC: |
---|
912 | * |
---|
913 | * "off_nl" is the offset if there's a SNAP header following |
---|
914 | * the 802.2 header; |
---|
915 | * |
---|
916 | * "off_nl_nosnap" is the offset if there's no SNAP header. |
---|
917 | * |
---|
918 | * If the link layer is Ethernet: |
---|
919 | * |
---|
920 | * "off_nl" is the offset if the packet is an Ethernet II packet |
---|
921 | * (we assume no 802.3+802.2+SNAP); |
---|
922 | * |
---|
923 | * "off_nl_nosnap" is the offset if the packet is an 802.3 packet |
---|
924 | * with an 802.2 header following it. |
---|
925 | */ |
---|
926 | static u_int off_nl; |
---|
927 | static u_int off_nl_nosnap; |
---|
928 | |
---|
929 | static int linktype; |
---|
930 | |
---|
931 | static void |
---|
932 | init_linktype(p) |
---|
933 | pcap_t *p; |
---|
934 | { |
---|
935 | linktype = pcap_datalink(p); |
---|
936 | #ifdef PCAP_FDDIPAD |
---|
937 | pcap_fddipad = p->fddipad; |
---|
938 | #endif |
---|
939 | |
---|
940 | /* |
---|
941 | * Assume it's not raw ATM with a pseudo-header, for now. |
---|
942 | */ |
---|
943 | off_mac = 0; |
---|
944 | is_atm = 0; |
---|
945 | is_lane = 0; |
---|
946 | off_vpi = -1; |
---|
947 | off_vci = -1; |
---|
948 | off_proto = -1; |
---|
949 | off_payload = -1; |
---|
950 | |
---|
951 | /* |
---|
952 | * And that we're not doing PPPoE. |
---|
953 | */ |
---|
954 | is_pppoes = 0; |
---|
955 | |
---|
956 | /* |
---|
957 | * And assume we're not doing SS7. |
---|
958 | */ |
---|
959 | off_li = -1; |
---|
960 | off_sio = -1; |
---|
961 | off_opc = -1; |
---|
962 | off_dpc = -1; |
---|
963 | off_sls = -1; |
---|
964 | |
---|
965 | /* |
---|
966 | * Also assume it's not 802.11. |
---|
967 | */ |
---|
968 | off_ll = 0; |
---|
969 | off_macpl = 0; |
---|
970 | off_macpl_is_variable = 0; |
---|
971 | |
---|
972 | orig_linktype = -1; |
---|
973 | orig_nl = -1; |
---|
974 | label_stack_depth = 0; |
---|
975 | |
---|
976 | reg_off_ll = -1; |
---|
977 | reg_off_macpl = -1; |
---|
978 | |
---|
979 | switch (linktype) { |
---|
980 | |
---|
981 | case DLT_ARCNET: |
---|
982 | off_linktype = 2; |
---|
983 | off_macpl = 6; |
---|
984 | off_nl = 0; /* XXX in reality, variable! */ |
---|
985 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
986 | return; |
---|
987 | |
---|
988 | case DLT_ARCNET_LINUX: |
---|
989 | off_linktype = 4; |
---|
990 | off_macpl = 8; |
---|
991 | off_nl = 0; /* XXX in reality, variable! */ |
---|
992 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
993 | return; |
---|
994 | |
---|
995 | case DLT_EN10MB: |
---|
996 | off_linktype = 12; |
---|
997 | off_macpl = 14; /* Ethernet header length */ |
---|
998 | off_nl = 0; /* Ethernet II */ |
---|
999 | off_nl_nosnap = 3; /* 802.3+802.2 */ |
---|
1000 | return; |
---|
1001 | |
---|
1002 | case DLT_SLIP: |
---|
1003 | /* |
---|
1004 | * SLIP doesn't have a link level type. The 16 byte |
---|
1005 | * header is hacked into our SLIP driver. |
---|
1006 | */ |
---|
1007 | off_linktype = -1; |
---|
1008 | off_macpl = 16; |
---|
1009 | off_nl = 0; |
---|
1010 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1011 | return; |
---|
1012 | |
---|
1013 | case DLT_SLIP_BSDOS: |
---|
1014 | /* XXX this may be the same as the DLT_PPP_BSDOS case */ |
---|
1015 | off_linktype = -1; |
---|
1016 | /* XXX end */ |
---|
1017 | off_macpl = 24; |
---|
1018 | off_nl = 0; |
---|
1019 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1020 | return; |
---|
1021 | |
---|
1022 | case DLT_NULL: |
---|
1023 | case DLT_LOOP: |
---|
1024 | off_linktype = 0; |
---|
1025 | off_macpl = 4; |
---|
1026 | off_nl = 0; |
---|
1027 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1028 | return; |
---|
1029 | |
---|
1030 | case DLT_ENC: |
---|
1031 | off_linktype = 0; |
---|
1032 | off_macpl = 12; |
---|
1033 | off_nl = 0; |
---|
1034 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1035 | return; |
---|
1036 | |
---|
1037 | case DLT_PPP: |
---|
1038 | case DLT_PPP_PPPD: |
---|
1039 | case DLT_C_HDLC: /* BSD/OS Cisco HDLC */ |
---|
1040 | case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */ |
---|
1041 | off_linktype = 2; |
---|
1042 | off_macpl = 4; |
---|
1043 | off_nl = 0; |
---|
1044 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1045 | return; |
---|
1046 | |
---|
1047 | case DLT_PPP_ETHER: |
---|
1048 | /* |
---|
1049 | * This does no include the Ethernet header, and |
---|
1050 | * only covers session state. |
---|
1051 | */ |
---|
1052 | off_linktype = 6; |
---|
1053 | off_macpl = 8; |
---|
1054 | off_nl = 0; |
---|
1055 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1056 | return; |
---|
1057 | |
---|
1058 | case DLT_PPP_BSDOS: |
---|
1059 | off_linktype = 5; |
---|
1060 | off_macpl = 24; |
---|
1061 | off_nl = 0; |
---|
1062 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1063 | return; |
---|
1064 | |
---|
1065 | case DLT_FDDI: |
---|
1066 | /* |
---|
1067 | * FDDI doesn't really have a link-level type field. |
---|
1068 | * We set "off_linktype" to the offset of the LLC header. |
---|
1069 | * |
---|
1070 | * To check for Ethernet types, we assume that SSAP = SNAP |
---|
1071 | * is being used and pick out the encapsulated Ethernet type. |
---|
1072 | * XXX - should we generate code to check for SNAP? |
---|
1073 | */ |
---|
1074 | off_linktype = 13; |
---|
1075 | #ifdef PCAP_FDDIPAD |
---|
1076 | off_linktype += pcap_fddipad; |
---|
1077 | #endif |
---|
1078 | off_macpl = 13; /* FDDI MAC header length */ |
---|
1079 | #ifdef PCAP_FDDIPAD |
---|
1080 | off_macpl += pcap_fddipad; |
---|
1081 | #endif |
---|
1082 | off_nl = 8; /* 802.2+SNAP */ |
---|
1083 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1084 | return; |
---|
1085 | |
---|
1086 | case DLT_IEEE802: |
---|
1087 | /* |
---|
1088 | * Token Ring doesn't really have a link-level type field. |
---|
1089 | * We set "off_linktype" to the offset of the LLC header. |
---|
1090 | * |
---|
1091 | * To check for Ethernet types, we assume that SSAP = SNAP |
---|
1092 | * is being used and pick out the encapsulated Ethernet type. |
---|
1093 | * XXX - should we generate code to check for SNAP? |
---|
1094 | * |
---|
1095 | * XXX - the header is actually variable-length. |
---|
1096 | * Some various Linux patched versions gave 38 |
---|
1097 | * as "off_linktype" and 40 as "off_nl"; however, |
---|
1098 | * if a token ring packet has *no* routing |
---|
1099 | * information, i.e. is not source-routed, the correct |
---|
1100 | * values are 20 and 22, as they are in the vanilla code. |
---|
1101 | * |
---|
1102 | * A packet is source-routed iff the uppermost bit |
---|
1103 | * of the first byte of the source address, at an |
---|
1104 | * offset of 8, has the uppermost bit set. If the |
---|
1105 | * packet is source-routed, the total number of bytes |
---|
1106 | * of routing information is 2 plus bits 0x1F00 of |
---|
1107 | * the 16-bit value at an offset of 14 (shifted right |
---|
1108 | * 8 - figure out which byte that is). |
---|
1109 | */ |
---|
1110 | off_linktype = 14; |
---|
1111 | off_macpl = 14; /* Token Ring MAC header length */ |
---|
1112 | off_nl = 8; /* 802.2+SNAP */ |
---|
1113 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1114 | return; |
---|
1115 | |
---|
1116 | case DLT_IEEE802_11: |
---|
1117 | case DLT_PRISM_HEADER: |
---|
1118 | case DLT_IEEE802_11_RADIO_AVS: |
---|
1119 | case DLT_IEEE802_11_RADIO: |
---|
1120 | /* |
---|
1121 | * 802.11 doesn't really have a link-level type field. |
---|
1122 | * We set "off_linktype" to the offset of the LLC header. |
---|
1123 | * |
---|
1124 | * To check for Ethernet types, we assume that SSAP = SNAP |
---|
1125 | * is being used and pick out the encapsulated Ethernet type. |
---|
1126 | * XXX - should we generate code to check for SNAP? |
---|
1127 | * |
---|
1128 | * We also handle variable-length radio headers here. |
---|
1129 | * The Prism header is in theory variable-length, but in |
---|
1130 | * practice it's always 144 bytes long. However, some |
---|
1131 | * drivers on Linux use ARPHRD_IEEE80211_PRISM, but |
---|
1132 | * sometimes or always supply an AVS header, so we |
---|
1133 | * have to check whether the radio header is a Prism |
---|
1134 | * header or an AVS header, so, in practice, it's |
---|
1135 | * variable-length. |
---|
1136 | */ |
---|
1137 | off_linktype = 24; |
---|
1138 | off_macpl = 0; /* link-layer header is variable-length */ |
---|
1139 | off_macpl_is_variable = 1; |
---|
1140 | off_nl = 8; /* 802.2+SNAP */ |
---|
1141 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1142 | return; |
---|
1143 | |
---|
1144 | case DLT_PPI: |
---|
1145 | /* |
---|
1146 | * At the moment we treat PPI the same way that we treat |
---|
1147 | * normal Radiotap encoded packets. The difference is in |
---|
1148 | * the function that generates the code at the beginning |
---|
1149 | * to compute the header length. Since this code generator |
---|
1150 | * of PPI supports bare 802.11 encapsulation only (i.e. |
---|
1151 | * the encapsulated DLT should be DLT_IEEE802_11) we |
---|
1152 | * generate code to check for this too. |
---|
1153 | */ |
---|
1154 | off_linktype = 24; |
---|
1155 | off_macpl = 0; /* link-layer header is variable-length */ |
---|
1156 | off_macpl_is_variable = 1; |
---|
1157 | off_nl = 8; /* 802.2+SNAP */ |
---|
1158 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1159 | return; |
---|
1160 | |
---|
1161 | case DLT_ATM_RFC1483: |
---|
1162 | case DLT_ATM_CLIP: /* Linux ATM defines this */ |
---|
1163 | /* |
---|
1164 | * assume routed, non-ISO PDUs |
---|
1165 | * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) |
---|
1166 | * |
---|
1167 | * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS, |
---|
1168 | * or PPP with the PPP NLPID (e.g., PPPoA)? The |
---|
1169 | * latter would presumably be treated the way PPPoE |
---|
1170 | * should be, so you can do "pppoe and udp port 2049" |
---|
1171 | * or "pppoa and tcp port 80" and have it check for |
---|
1172 | * PPPo{A,E} and a PPP protocol of IP and.... |
---|
1173 | */ |
---|
1174 | off_linktype = 0; |
---|
1175 | off_macpl = 0; /* packet begins with LLC header */ |
---|
1176 | off_nl = 8; /* 802.2+SNAP */ |
---|
1177 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1178 | return; |
---|
1179 | |
---|
1180 | case DLT_SUNATM: |
---|
1181 | /* |
---|
1182 | * Full Frontal ATM; you get AALn PDUs with an ATM |
---|
1183 | * pseudo-header. |
---|
1184 | */ |
---|
1185 | is_atm = 1; |
---|
1186 | off_vpi = SUNATM_VPI_POS; |
---|
1187 | off_vci = SUNATM_VCI_POS; |
---|
1188 | off_proto = PROTO_POS; |
---|
1189 | off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */ |
---|
1190 | off_payload = SUNATM_PKT_BEGIN_POS; |
---|
1191 | off_linktype = off_payload; |
---|
1192 | off_macpl = off_payload; /* if LLC-encapsulated */ |
---|
1193 | off_nl = 8; /* 802.2+SNAP */ |
---|
1194 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1195 | return; |
---|
1196 | |
---|
1197 | case DLT_RAW: |
---|
1198 | case DLT_IPV4: |
---|
1199 | case DLT_IPV6: |
---|
1200 | off_linktype = -1; |
---|
1201 | off_macpl = 0; |
---|
1202 | off_nl = 0; |
---|
1203 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1204 | return; |
---|
1205 | |
---|
1206 | case DLT_LINUX_SLL: /* fake header for Linux cooked socket */ |
---|
1207 | off_linktype = 14; |
---|
1208 | off_macpl = 16; |
---|
1209 | off_nl = 0; |
---|
1210 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1211 | return; |
---|
1212 | |
---|
1213 | case DLT_LTALK: |
---|
1214 | /* |
---|
1215 | * LocalTalk does have a 1-byte type field in the LLAP header, |
---|
1216 | * but really it just indicates whether there is a "short" or |
---|
1217 | * "long" DDP packet following. |
---|
1218 | */ |
---|
1219 | off_linktype = -1; |
---|
1220 | off_macpl = 0; |
---|
1221 | off_nl = 0; |
---|
1222 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1223 | return; |
---|
1224 | |
---|
1225 | case DLT_IP_OVER_FC: |
---|
1226 | /* |
---|
1227 | * RFC 2625 IP-over-Fibre-Channel doesn't really have a |
---|
1228 | * link-level type field. We set "off_linktype" to the |
---|
1229 | * offset of the LLC header. |
---|
1230 | * |
---|
1231 | * To check for Ethernet types, we assume that SSAP = SNAP |
---|
1232 | * is being used and pick out the encapsulated Ethernet type. |
---|
1233 | * XXX - should we generate code to check for SNAP? RFC |
---|
1234 | * 2625 says SNAP should be used. |
---|
1235 | */ |
---|
1236 | off_linktype = 16; |
---|
1237 | off_macpl = 16; |
---|
1238 | off_nl = 8; /* 802.2+SNAP */ |
---|
1239 | off_nl_nosnap = 3; /* 802.2 */ |
---|
1240 | return; |
---|
1241 | |
---|
1242 | case DLT_FRELAY: |
---|
1243 | /* |
---|
1244 | * XXX - we should set this to handle SNAP-encapsulated |
---|
1245 | * frames (NLPID of 0x80). |
---|
1246 | */ |
---|
1247 | off_linktype = -1; |
---|
1248 | off_macpl = 0; |
---|
1249 | off_nl = 0; |
---|
1250 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1251 | return; |
---|
1252 | |
---|
1253 | /* |
---|
1254 | * the only BPF-interesting FRF.16 frames are non-control frames; |
---|
1255 | * Frame Relay has a variable length link-layer |
---|
1256 | * so lets start with offset 4 for now and increments later on (FIXME); |
---|
1257 | */ |
---|
1258 | case DLT_MFR: |
---|
1259 | off_linktype = -1; |
---|
1260 | off_macpl = 0; |
---|
1261 | off_nl = 4; |
---|
1262 | off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */ |
---|
1263 | return; |
---|
1264 | |
---|
1265 | case DLT_APPLE_IP_OVER_IEEE1394: |
---|
1266 | off_linktype = 16; |
---|
1267 | off_macpl = 18; |
---|
1268 | off_nl = 0; |
---|
1269 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1270 | return; |
---|
1271 | |
---|
1272 | case DLT_SYMANTEC_FIREWALL: |
---|
1273 | off_linktype = 6; |
---|
1274 | off_macpl = 44; |
---|
1275 | off_nl = 0; /* Ethernet II */ |
---|
1276 | off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */ |
---|
1277 | return; |
---|
1278 | |
---|
1279 | #ifdef HAVE_NET_PFVAR_H |
---|
1280 | case DLT_PFLOG: |
---|
1281 | off_linktype = 0; |
---|
1282 | off_macpl = PFLOG_HDRLEN; |
---|
1283 | off_nl = 0; |
---|
1284 | off_nl_nosnap = 0; /* no 802.2 LLC */ |
---|
1285 | return; |
---|
1286 | #endif |
---|
1287 | |
---|
1288 | case DLT_JUNIPER_MFR: |
---|
1289 | case DLT_JUNIPER_MLFR: |
---|
1290 | case DLT_JUNIPER_MLPPP: |
---|
1291 | case DLT_JUNIPER_PPP: |
---|
1292 | case DLT_JUNIPER_CHDLC: |
---|
1293 | case DLT_JUNIPER_FRELAY: |
---|
1294 | off_linktype = 4; |
---|
1295 | off_macpl = 4; |
---|
1296 | off_nl = 0; |
---|
1297 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1298 | return; |
---|
1299 | |
---|
1300 | case DLT_JUNIPER_ATM1: |
---|
1301 | off_linktype = 4; /* in reality variable between 4-8 */ |
---|
1302 | off_macpl = 4; /* in reality variable between 4-8 */ |
---|
1303 | off_nl = 0; |
---|
1304 | off_nl_nosnap = 10; |
---|
1305 | return; |
---|
1306 | |
---|
1307 | case DLT_JUNIPER_ATM2: |
---|
1308 | off_linktype = 8; /* in reality variable between 8-12 */ |
---|
1309 | off_macpl = 8; /* in reality variable between 8-12 */ |
---|
1310 | off_nl = 0; |
---|
1311 | off_nl_nosnap = 10; |
---|
1312 | return; |
---|
1313 | |
---|
1314 | /* frames captured on a Juniper PPPoE service PIC |
---|
1315 | * contain raw ethernet frames */ |
---|
1316 | case DLT_JUNIPER_PPPOE: |
---|
1317 | case DLT_JUNIPER_ETHER: |
---|
1318 | off_macpl = 14; |
---|
1319 | off_linktype = 16; |
---|
1320 | off_nl = 18; /* Ethernet II */ |
---|
1321 | off_nl_nosnap = 21; /* 802.3+802.2 */ |
---|
1322 | return; |
---|
1323 | |
---|
1324 | case DLT_JUNIPER_PPPOE_ATM: |
---|
1325 | off_linktype = 4; |
---|
1326 | off_macpl = 6; |
---|
1327 | off_nl = 0; |
---|
1328 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1329 | return; |
---|
1330 | |
---|
1331 | case DLT_JUNIPER_GGSN: |
---|
1332 | off_linktype = 6; |
---|
1333 | off_macpl = 12; |
---|
1334 | off_nl = 0; |
---|
1335 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1336 | return; |
---|
1337 | |
---|
1338 | case DLT_JUNIPER_ES: |
---|
1339 | off_linktype = 6; |
---|
1340 | off_macpl = -1; /* not really a network layer but raw IP addresses */ |
---|
1341 | off_nl = -1; /* not really a network layer but raw IP addresses */ |
---|
1342 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1343 | return; |
---|
1344 | |
---|
1345 | case DLT_JUNIPER_MONITOR: |
---|
1346 | off_linktype = 12; |
---|
1347 | off_macpl = 12; |
---|
1348 | off_nl = 0; /* raw IP/IP6 header */ |
---|
1349 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1350 | return; |
---|
1351 | |
---|
1352 | case DLT_JUNIPER_SERVICES: |
---|
1353 | off_linktype = 12; |
---|
1354 | off_macpl = -1; /* L3 proto location dep. on cookie type */ |
---|
1355 | off_nl = -1; /* L3 proto location dep. on cookie type */ |
---|
1356 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1357 | return; |
---|
1358 | |
---|
1359 | case DLT_JUNIPER_VP: |
---|
1360 | off_linktype = 18; |
---|
1361 | off_macpl = -1; |
---|
1362 | off_nl = -1; |
---|
1363 | off_nl_nosnap = -1; |
---|
1364 | return; |
---|
1365 | |
---|
1366 | case DLT_JUNIPER_ST: |
---|
1367 | off_linktype = 18; |
---|
1368 | off_macpl = -1; |
---|
1369 | off_nl = -1; |
---|
1370 | off_nl_nosnap = -1; |
---|
1371 | return; |
---|
1372 | |
---|
1373 | case DLT_JUNIPER_ISM: |
---|
1374 | off_linktype = 8; |
---|
1375 | off_macpl = -1; |
---|
1376 | off_nl = -1; |
---|
1377 | off_nl_nosnap = -1; |
---|
1378 | return; |
---|
1379 | |
---|
1380 | case DLT_JUNIPER_VS: |
---|
1381 | case DLT_JUNIPER_SRX_E2E: |
---|
1382 | case DLT_JUNIPER_FIBRECHANNEL: |
---|
1383 | case DLT_JUNIPER_ATM_CEMIC: |
---|
1384 | off_linktype = 8; |
---|
1385 | off_macpl = -1; |
---|
1386 | off_nl = -1; |
---|
1387 | off_nl_nosnap = -1; |
---|
1388 | return; |
---|
1389 | |
---|
1390 | case DLT_MTP2: |
---|
1391 | off_li = 2; |
---|
1392 | off_sio = 3; |
---|
1393 | off_opc = 4; |
---|
1394 | off_dpc = 4; |
---|
1395 | off_sls = 7; |
---|
1396 | off_linktype = -1; |
---|
1397 | off_macpl = -1; |
---|
1398 | off_nl = -1; |
---|
1399 | off_nl_nosnap = -1; |
---|
1400 | return; |
---|
1401 | |
---|
1402 | case DLT_MTP2_WITH_PHDR: |
---|
1403 | off_li = 6; |
---|
1404 | off_sio = 7; |
---|
1405 | off_opc = 8; |
---|
1406 | off_dpc = 8; |
---|
1407 | off_sls = 11; |
---|
1408 | off_linktype = -1; |
---|
1409 | off_macpl = -1; |
---|
1410 | off_nl = -1; |
---|
1411 | off_nl_nosnap = -1; |
---|
1412 | return; |
---|
1413 | |
---|
1414 | case DLT_ERF: |
---|
1415 | off_li = 22; |
---|
1416 | off_sio = 23; |
---|
1417 | off_opc = 24; |
---|
1418 | off_dpc = 24; |
---|
1419 | off_sls = 27; |
---|
1420 | off_linktype = -1; |
---|
1421 | off_macpl = -1; |
---|
1422 | off_nl = -1; |
---|
1423 | off_nl_nosnap = -1; |
---|
1424 | return; |
---|
1425 | |
---|
1426 | case DLT_PFSYNC: |
---|
1427 | off_linktype = -1; |
---|
1428 | off_macpl = 4; |
---|
1429 | off_nl = 0; |
---|
1430 | off_nl_nosnap = 0; |
---|
1431 | return; |
---|
1432 | |
---|
1433 | case DLT_AX25_KISS: |
---|
1434 | /* |
---|
1435 | * Currently, only raw "link[N:M]" filtering is supported. |
---|
1436 | */ |
---|
1437 | off_linktype = -1; /* variable, min 15, max 71 steps of 7 */ |
---|
1438 | off_macpl = -1; |
---|
1439 | off_nl = -1; /* variable, min 16, max 71 steps of 7 */ |
---|
1440 | off_nl_nosnap = -1; /* no 802.2 LLC */ |
---|
1441 | off_mac = 1; /* step over the kiss length byte */ |
---|
1442 | return; |
---|
1443 | |
---|
1444 | case DLT_IPNET: |
---|
1445 | off_linktype = 1; |
---|
1446 | off_macpl = 24; /* ipnet header length */ |
---|
1447 | off_nl = 0; |
---|
1448 | off_nl_nosnap = -1; |
---|
1449 | return; |
---|
1450 | |
---|
1451 | case DLT_NETANALYZER: |
---|
1452 | off_mac = 4; /* MAC header is past 4-byte pseudo-header */ |
---|
1453 | off_linktype = 16; /* includes 4-byte pseudo-header */ |
---|
1454 | off_macpl = 18; /* pseudo-header+Ethernet header length */ |
---|
1455 | off_nl = 0; /* Ethernet II */ |
---|
1456 | off_nl_nosnap = 3; /* 802.3+802.2 */ |
---|
1457 | return; |
---|
1458 | |
---|
1459 | case DLT_NETANALYZER_TRANSPARENT: |
---|
1460 | off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */ |
---|
1461 | off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */ |
---|
1462 | off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */ |
---|
1463 | off_nl = 0; /* Ethernet II */ |
---|
1464 | off_nl_nosnap = 3; /* 802.3+802.2 */ |
---|
1465 | return; |
---|
1466 | |
---|
1467 | default: |
---|
1468 | /* |
---|
1469 | * For values in the range in which we've assigned new |
---|
1470 | * DLT_ values, only raw "link[N:M]" filtering is supported. |
---|
1471 | */ |
---|
1472 | if (linktype >= DLT_MATCHING_MIN && |
---|
1473 | linktype <= DLT_MATCHING_MAX) { |
---|
1474 | off_linktype = -1; |
---|
1475 | off_macpl = -1; |
---|
1476 | off_nl = -1; |
---|
1477 | off_nl_nosnap = -1; |
---|
1478 | return; |
---|
1479 | } |
---|
1480 | |
---|
1481 | } |
---|
1482 | bpf_error("unknown data link type %d", linktype); |
---|
1483 | /* NOTREACHED */ |
---|
1484 | } |
---|
1485 | |
---|
1486 | /* |
---|
1487 | * Load a value relative to the beginning of the link-layer header. |
---|
1488 | * The link-layer header doesn't necessarily begin at the beginning |
---|
1489 | * of the packet data; there might be a variable-length prefix containing |
---|
1490 | * radio information. |
---|
1491 | */ |
---|
1492 | static struct slist * |
---|
1493 | gen_load_llrel(offset, size) |
---|
1494 | u_int offset, size; |
---|
1495 | { |
---|
1496 | struct slist *s, *s2; |
---|
1497 | |
---|
1498 | s = gen_llprefixlen(); |
---|
1499 | |
---|
1500 | /* |
---|
1501 | * If "s" is non-null, it has code to arrange that the X register |
---|
1502 | * contains the length of the prefix preceding the link-layer |
---|
1503 | * header. |
---|
1504 | * |
---|
1505 | * Otherwise, the length of the prefix preceding the link-layer |
---|
1506 | * header is "off_ll". |
---|
1507 | */ |
---|
1508 | if (s != NULL) { |
---|
1509 | /* |
---|
1510 | * There's a variable-length prefix preceding the |
---|
1511 | * link-layer header. "s" points to a list of statements |
---|
1512 | * that put the length of that prefix into the X register. |
---|
1513 | * do an indirect load, to use the X register as an offset. |
---|
1514 | */ |
---|
1515 | s2 = new_stmt(BPF_LD|BPF_IND|size); |
---|
1516 | s2->s.k = offset; |
---|
1517 | sappend(s, s2); |
---|
1518 | } else { |
---|
1519 | /* |
---|
1520 | * There is no variable-length header preceding the |
---|
1521 | * link-layer header; add in off_ll, which, if there's |
---|
1522 | * a fixed-length header preceding the link-layer header, |
---|
1523 | * is the length of that header. |
---|
1524 | */ |
---|
1525 | s = new_stmt(BPF_LD|BPF_ABS|size); |
---|
1526 | s->s.k = offset + off_ll; |
---|
1527 | } |
---|
1528 | return s; |
---|
1529 | } |
---|
1530 | |
---|
1531 | /* |
---|
1532 | * Load a value relative to the beginning of the MAC-layer payload. |
---|
1533 | */ |
---|
1534 | static struct slist * |
---|
1535 | gen_load_macplrel(offset, size) |
---|
1536 | u_int offset, size; |
---|
1537 | { |
---|
1538 | struct slist *s, *s2; |
---|
1539 | |
---|
1540 | s = gen_off_macpl(); |
---|
1541 | |
---|
1542 | /* |
---|
1543 | * If s is non-null, the offset of the MAC-layer payload is |
---|
1544 | * variable, and s points to a list of instructions that |
---|
1545 | * arrange that the X register contains that offset. |
---|
1546 | * |
---|
1547 | * Otherwise, the offset of the MAC-layer payload is constant, |
---|
1548 | * and is in off_macpl. |
---|
1549 | */ |
---|
1550 | if (s != NULL) { |
---|
1551 | /* |
---|
1552 | * The offset of the MAC-layer payload is in the X |
---|
1553 | * register. Do an indirect load, to use the X register |
---|
1554 | * as an offset. |
---|
1555 | */ |
---|
1556 | s2 = new_stmt(BPF_LD|BPF_IND|size); |
---|
1557 | s2->s.k = offset; |
---|
1558 | sappend(s, s2); |
---|
1559 | } else { |
---|
1560 | /* |
---|
1561 | * The offset of the MAC-layer payload is constant, |
---|
1562 | * and is in off_macpl; load the value at that offset |
---|
1563 | * plus the specified offset. |
---|
1564 | */ |
---|
1565 | s = new_stmt(BPF_LD|BPF_ABS|size); |
---|
1566 | s->s.k = off_macpl + offset; |
---|
1567 | } |
---|
1568 | return s; |
---|
1569 | } |
---|
1570 | |
---|
1571 | /* |
---|
1572 | * Load a value relative to the beginning of the specified header. |
---|
1573 | */ |
---|
1574 | static struct slist * |
---|
1575 | gen_load_a(offrel, offset, size) |
---|
1576 | enum e_offrel offrel; |
---|
1577 | u_int offset, size; |
---|
1578 | { |
---|
1579 | struct slist *s, *s2; |
---|
1580 | |
---|
1581 | switch (offrel) { |
---|
1582 | |
---|
1583 | case OR_PACKET: |
---|
1584 | s = new_stmt(BPF_LD|BPF_ABS|size); |
---|
1585 | s->s.k = offset; |
---|
1586 | break; |
---|
1587 | |
---|
1588 | case OR_LINK: |
---|
1589 | s = gen_load_llrel(offset, size); |
---|
1590 | break; |
---|
1591 | |
---|
1592 | case OR_MACPL: |
---|
1593 | s = gen_load_macplrel(offset, size); |
---|
1594 | break; |
---|
1595 | |
---|
1596 | case OR_NET: |
---|
1597 | s = gen_load_macplrel(off_nl + offset, size); |
---|
1598 | break; |
---|
1599 | |
---|
1600 | case OR_NET_NOSNAP: |
---|
1601 | s = gen_load_macplrel(off_nl_nosnap + offset, size); |
---|
1602 | break; |
---|
1603 | |
---|
1604 | case OR_TRAN_IPV4: |
---|
1605 | /* |
---|
1606 | * Load the X register with the length of the IPv4 header |
---|
1607 | * (plus the offset of the link-layer header, if it's |
---|
1608 | * preceded by a variable-length header such as a radio |
---|
1609 | * header), in bytes. |
---|
1610 | */ |
---|
1611 | s = gen_loadx_iphdrlen(); |
---|
1612 | |
---|
1613 | /* |
---|
1614 | * Load the item at {offset of the MAC-layer payload} + |
---|
1615 | * {offset, relative to the start of the MAC-layer |
---|
1616 | * paylod, of the IPv4 header} + {length of the IPv4 header} + |
---|
1617 | * {specified offset}. |
---|
1618 | * |
---|
1619 | * (If the offset of the MAC-layer payload is variable, |
---|
1620 | * it's included in the value in the X register, and |
---|
1621 | * off_macpl is 0.) |
---|
1622 | */ |
---|
1623 | s2 = new_stmt(BPF_LD|BPF_IND|size); |
---|
1624 | s2->s.k = off_macpl + off_nl + offset; |
---|
1625 | sappend(s, s2); |
---|
1626 | break; |
---|
1627 | |
---|
1628 | case OR_TRAN_IPV6: |
---|
1629 | s = gen_load_macplrel(off_nl + 40 + offset, size); |
---|
1630 | break; |
---|
1631 | |
---|
1632 | default: |
---|
1633 | abort(); |
---|
1634 | return NULL; |
---|
1635 | } |
---|
1636 | return s; |
---|
1637 | } |
---|
1638 | |
---|
1639 | /* |
---|
1640 | * Generate code to load into the X register the sum of the length of |
---|
1641 | * the IPv4 header and any variable-length header preceding the link-layer |
---|
1642 | * header. |
---|
1643 | */ |
---|
1644 | static struct slist * |
---|
1645 | gen_loadx_iphdrlen() |
---|
1646 | { |
---|
1647 | struct slist *s, *s2; |
---|
1648 | |
---|
1649 | s = gen_off_macpl(); |
---|
1650 | if (s != NULL) { |
---|
1651 | /* |
---|
1652 | * There's a variable-length prefix preceding the |
---|
1653 | * link-layer header, or the link-layer header is itself |
---|
1654 | * variable-length. "s" points to a list of statements |
---|
1655 | * that put the offset of the MAC-layer payload into |
---|
1656 | * the X register. |
---|
1657 | * |
---|
1658 | * The 4*([k]&0xf) addressing mode can't be used, as we |
---|
1659 | * don't have a constant offset, so we have to load the |
---|
1660 | * value in question into the A register and add to it |
---|
1661 | * the value from the X register. |
---|
1662 | */ |
---|
1663 | s2 = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
1664 | s2->s.k = off_nl; |
---|
1665 | sappend(s, s2); |
---|
1666 | s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K); |
---|
1667 | s2->s.k = 0xf; |
---|
1668 | sappend(s, s2); |
---|
1669 | s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K); |
---|
1670 | s2->s.k = 2; |
---|
1671 | sappend(s, s2); |
---|
1672 | |
---|
1673 | /* |
---|
1674 | * The A register now contains the length of the |
---|
1675 | * IP header. We need to add to it the offset of |
---|
1676 | * the MAC-layer payload, which is still in the X |
---|
1677 | * register, and move the result into the X register. |
---|
1678 | */ |
---|
1679 | sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X)); |
---|
1680 | sappend(s, new_stmt(BPF_MISC|BPF_TAX)); |
---|
1681 | } else { |
---|
1682 | /* |
---|
1683 | * There is no variable-length header preceding the |
---|
1684 | * link-layer header, and the link-layer header is |
---|
1685 | * fixed-length; load the length of the IPv4 header, |
---|
1686 | * which is at an offset of off_nl from the beginning |
---|
1687 | * of the MAC-layer payload, and thus at an offset |
---|
1688 | * of off_mac_pl + off_nl from the beginning of the |
---|
1689 | * raw packet data. |
---|
1690 | */ |
---|
1691 | s = new_stmt(BPF_LDX|BPF_MSH|BPF_B); |
---|
1692 | s->s.k = off_macpl + off_nl; |
---|
1693 | } |
---|
1694 | return s; |
---|
1695 | } |
---|
1696 | |
---|
1697 | static struct block * |
---|
1698 | gen_uncond(rsense) |
---|
1699 | int rsense; |
---|
1700 | { |
---|
1701 | struct block *b; |
---|
1702 | struct slist *s; |
---|
1703 | |
---|
1704 | s = new_stmt(BPF_LD|BPF_IMM); |
---|
1705 | s->s.k = !rsense; |
---|
1706 | b = new_block(JMP(BPF_JEQ)); |
---|
1707 | b->stmts = s; |
---|
1708 | |
---|
1709 | return b; |
---|
1710 | } |
---|
1711 | |
---|
1712 | static inline struct block * |
---|
1713 | gen_true() |
---|
1714 | { |
---|
1715 | return gen_uncond(1); |
---|
1716 | } |
---|
1717 | |
---|
1718 | static inline struct block * |
---|
1719 | gen_false() |
---|
1720 | { |
---|
1721 | return gen_uncond(0); |
---|
1722 | } |
---|
1723 | |
---|
1724 | /* |
---|
1725 | * Byte-swap a 32-bit number. |
---|
1726 | * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on |
---|
1727 | * big-endian platforms.) |
---|
1728 | */ |
---|
1729 | #define SWAPLONG(y) \ |
---|
1730 | ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff)) |
---|
1731 | |
---|
1732 | /* |
---|
1733 | * Generate code to match a particular packet type. |
---|
1734 | * |
---|
1735 | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
---|
1736 | * value, if <= ETHERMTU. We use that to determine whether to |
---|
1737 | * match the type/length field or to check the type/length field for |
---|
1738 | * a value <= ETHERMTU to see whether it's a type field and then do |
---|
1739 | * the appropriate test. |
---|
1740 | */ |
---|
1741 | static struct block * |
---|
1742 | gen_ether_linktype(proto) |
---|
1743 | register int proto; |
---|
1744 | { |
---|
1745 | struct block *b0, *b1; |
---|
1746 | |
---|
1747 | switch (proto) { |
---|
1748 | |
---|
1749 | case LLCSAP_ISONS: |
---|
1750 | case LLCSAP_IP: |
---|
1751 | case LLCSAP_NETBEUI: |
---|
1752 | /* |
---|
1753 | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
---|
1754 | * so we check the DSAP and SSAP. |
---|
1755 | * |
---|
1756 | * LLCSAP_IP checks for IP-over-802.2, rather |
---|
1757 | * than IP-over-Ethernet or IP-over-SNAP. |
---|
1758 | * |
---|
1759 | * XXX - should we check both the DSAP and the |
---|
1760 | * SSAP, like this, or should we check just the |
---|
1761 | * DSAP, as we do for other types <= ETHERMTU |
---|
1762 | * (i.e., other SAP values)? |
---|
1763 | */ |
---|
1764 | b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU); |
---|
1765 | gen_not(b0); |
---|
1766 | b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32) |
---|
1767 | ((proto << 8) | proto)); |
---|
1768 | gen_and(b0, b1); |
---|
1769 | return b1; |
---|
1770 | |
---|
1771 | case LLCSAP_IPX: |
---|
1772 | /* |
---|
1773 | * Check for; |
---|
1774 | * |
---|
1775 | * Ethernet_II frames, which are Ethernet |
---|
1776 | * frames with a frame type of ETHERTYPE_IPX; |
---|
1777 | * |
---|
1778 | * Ethernet_802.3 frames, which are 802.3 |
---|
1779 | * frames (i.e., the type/length field is |
---|
1780 | * a length field, <= ETHERMTU, rather than |
---|
1781 | * a type field) with the first two bytes |
---|
1782 | * after the Ethernet/802.3 header being |
---|
1783 | * 0xFFFF; |
---|
1784 | * |
---|
1785 | * Ethernet_802.2 frames, which are 802.3 |
---|
1786 | * frames with an 802.2 LLC header and |
---|
1787 | * with the IPX LSAP as the DSAP in the LLC |
---|
1788 | * header; |
---|
1789 | * |
---|
1790 | * Ethernet_SNAP frames, which are 802.3 |
---|
1791 | * frames with an LLC header and a SNAP |
---|
1792 | * header and with an OUI of 0x000000 |
---|
1793 | * (encapsulated Ethernet) and a protocol |
---|
1794 | * ID of ETHERTYPE_IPX in the SNAP header. |
---|
1795 | * |
---|
1796 | * XXX - should we generate the same code both |
---|
1797 | * for tests for LLCSAP_IPX and for ETHERTYPE_IPX? |
---|
1798 | */ |
---|
1799 | |
---|
1800 | /* |
---|
1801 | * This generates code to check both for the |
---|
1802 | * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3. |
---|
1803 | */ |
---|
1804 | b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX); |
---|
1805 | b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF); |
---|
1806 | gen_or(b0, b1); |
---|
1807 | |
---|
1808 | /* |
---|
1809 | * Now we add code to check for SNAP frames with |
---|
1810 | * ETHERTYPE_IPX, i.e. Ethernet_SNAP. |
---|
1811 | */ |
---|
1812 | b0 = gen_snap(0x000000, ETHERTYPE_IPX); |
---|
1813 | gen_or(b0, b1); |
---|
1814 | |
---|
1815 | /* |
---|
1816 | * Now we generate code to check for 802.3 |
---|
1817 | * frames in general. |
---|
1818 | */ |
---|
1819 | b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU); |
---|
1820 | gen_not(b0); |
---|
1821 | |
---|
1822 | /* |
---|
1823 | * Now add the check for 802.3 frames before the |
---|
1824 | * check for Ethernet_802.2 and Ethernet_802.3, |
---|
1825 | * as those checks should only be done on 802.3 |
---|
1826 | * frames, not on Ethernet frames. |
---|
1827 | */ |
---|
1828 | gen_and(b0, b1); |
---|
1829 | |
---|
1830 | /* |
---|
1831 | * Now add the check for Ethernet_II frames, and |
---|
1832 | * do that before checking for the other frame |
---|
1833 | * types. |
---|
1834 | */ |
---|
1835 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
1836 | (bpf_int32)ETHERTYPE_IPX); |
---|
1837 | gen_or(b0, b1); |
---|
1838 | return b1; |
---|
1839 | |
---|
1840 | case ETHERTYPE_ATALK: |
---|
1841 | case ETHERTYPE_AARP: |
---|
1842 | /* |
---|
1843 | * EtherTalk (AppleTalk protocols on Ethernet link |
---|
1844 | * layer) may use 802.2 encapsulation. |
---|
1845 | */ |
---|
1846 | |
---|
1847 | /* |
---|
1848 | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
---|
1849 | * we check for an Ethernet type field less than |
---|
1850 | * 1500, which means it's an 802.3 length field. |
---|
1851 | */ |
---|
1852 | b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU); |
---|
1853 | gen_not(b0); |
---|
1854 | |
---|
1855 | /* |
---|
1856 | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
---|
1857 | * SNAP packets with an organization code of |
---|
1858 | * 0x080007 (Apple, for Appletalk) and a protocol |
---|
1859 | * type of ETHERTYPE_ATALK (Appletalk). |
---|
1860 | * |
---|
1861 | * 802.2-encapsulated ETHERTYPE_AARP packets are |
---|
1862 | * SNAP packets with an organization code of |
---|
1863 | * 0x000000 (encapsulated Ethernet) and a protocol |
---|
1864 | * type of ETHERTYPE_AARP (Appletalk ARP). |
---|
1865 | */ |
---|
1866 | if (proto == ETHERTYPE_ATALK) |
---|
1867 | b1 = gen_snap(0x080007, ETHERTYPE_ATALK); |
---|
1868 | else /* proto == ETHERTYPE_AARP */ |
---|
1869 | b1 = gen_snap(0x000000, ETHERTYPE_AARP); |
---|
1870 | gen_and(b0, b1); |
---|
1871 | |
---|
1872 | /* |
---|
1873 | * Check for Ethernet encapsulation (Ethertalk |
---|
1874 | * phase 1?); we just check for the Ethernet |
---|
1875 | * protocol type. |
---|
1876 | */ |
---|
1877 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto); |
---|
1878 | |
---|
1879 | gen_or(b0, b1); |
---|
1880 | return b1; |
---|
1881 | |
---|
1882 | default: |
---|
1883 | if (proto <= ETHERMTU) { |
---|
1884 | /* |
---|
1885 | * This is an LLC SAP value, so the frames |
---|
1886 | * that match would be 802.2 frames. |
---|
1887 | * Check that the frame is an 802.2 frame |
---|
1888 | * (i.e., that the length/type field is |
---|
1889 | * a length field, <= ETHERMTU) and |
---|
1890 | * then check the DSAP. |
---|
1891 | */ |
---|
1892 | b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU); |
---|
1893 | gen_not(b0); |
---|
1894 | b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B, |
---|
1895 | (bpf_int32)proto); |
---|
1896 | gen_and(b0, b1); |
---|
1897 | return b1; |
---|
1898 | } else { |
---|
1899 | /* |
---|
1900 | * This is an Ethernet type, so compare |
---|
1901 | * the length/type field with it (if |
---|
1902 | * the frame is an 802.2 frame, the length |
---|
1903 | * field will be <= ETHERMTU, and, as |
---|
1904 | * "proto" is > ETHERMTU, this test |
---|
1905 | * will fail and the frame won't match, |
---|
1906 | * which is what we want). |
---|
1907 | */ |
---|
1908 | return gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
1909 | (bpf_int32)proto); |
---|
1910 | } |
---|
1911 | } |
---|
1912 | } |
---|
1913 | |
---|
1914 | /* |
---|
1915 | * "proto" is an Ethernet type value and for IPNET, if it is not IPv4 |
---|
1916 | * or IPv6 then we have an error. |
---|
1917 | */ |
---|
1918 | static struct block * |
---|
1919 | gen_ipnet_linktype(proto) |
---|
1920 | register int proto; |
---|
1921 | { |
---|
1922 | switch (proto) { |
---|
1923 | |
---|
1924 | case ETHERTYPE_IP: |
---|
1925 | return gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
1926 | (bpf_int32)IPH_AF_INET); |
---|
1927 | /* NOTREACHED */ |
---|
1928 | |
---|
1929 | case ETHERTYPE_IPV6: |
---|
1930 | return gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
1931 | (bpf_int32)IPH_AF_INET6); |
---|
1932 | /* NOTREACHED */ |
---|
1933 | |
---|
1934 | default: |
---|
1935 | break; |
---|
1936 | } |
---|
1937 | |
---|
1938 | return gen_false(); |
---|
1939 | } |
---|
1940 | |
---|
1941 | /* |
---|
1942 | * Generate code to match a particular packet type. |
---|
1943 | * |
---|
1944 | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
---|
1945 | * value, if <= ETHERMTU. We use that to determine whether to |
---|
1946 | * match the type field or to check the type field for the special |
---|
1947 | * LINUX_SLL_P_802_2 value and then do the appropriate test. |
---|
1948 | */ |
---|
1949 | static struct block * |
---|
1950 | gen_linux_sll_linktype(proto) |
---|
1951 | register int proto; |
---|
1952 | { |
---|
1953 | struct block *b0, *b1; |
---|
1954 | |
---|
1955 | switch (proto) { |
---|
1956 | |
---|
1957 | case LLCSAP_ISONS: |
---|
1958 | case LLCSAP_IP: |
---|
1959 | case LLCSAP_NETBEUI: |
---|
1960 | /* |
---|
1961 | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
---|
1962 | * so we check the DSAP and SSAP. |
---|
1963 | * |
---|
1964 | * LLCSAP_IP checks for IP-over-802.2, rather |
---|
1965 | * than IP-over-Ethernet or IP-over-SNAP. |
---|
1966 | * |
---|
1967 | * XXX - should we check both the DSAP and the |
---|
1968 | * SSAP, like this, or should we check just the |
---|
1969 | * DSAP, as we do for other types <= ETHERMTU |
---|
1970 | * (i.e., other SAP values)? |
---|
1971 | */ |
---|
1972 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2); |
---|
1973 | b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32) |
---|
1974 | ((proto << 8) | proto)); |
---|
1975 | gen_and(b0, b1); |
---|
1976 | return b1; |
---|
1977 | |
---|
1978 | case LLCSAP_IPX: |
---|
1979 | /* |
---|
1980 | * Ethernet_II frames, which are Ethernet |
---|
1981 | * frames with a frame type of ETHERTYPE_IPX; |
---|
1982 | * |
---|
1983 | * Ethernet_802.3 frames, which have a frame |
---|
1984 | * type of LINUX_SLL_P_802_3; |
---|
1985 | * |
---|
1986 | * Ethernet_802.2 frames, which are 802.3 |
---|
1987 | * frames with an 802.2 LLC header (i.e, have |
---|
1988 | * a frame type of LINUX_SLL_P_802_2) and |
---|
1989 | * with the IPX LSAP as the DSAP in the LLC |
---|
1990 | * header; |
---|
1991 | * |
---|
1992 | * Ethernet_SNAP frames, which are 802.3 |
---|
1993 | * frames with an LLC header and a SNAP |
---|
1994 | * header and with an OUI of 0x000000 |
---|
1995 | * (encapsulated Ethernet) and a protocol |
---|
1996 | * ID of ETHERTYPE_IPX in the SNAP header. |
---|
1997 | * |
---|
1998 | * First, do the checks on LINUX_SLL_P_802_2 |
---|
1999 | * frames; generate the check for either |
---|
2000 | * Ethernet_802.2 or Ethernet_SNAP frames, and |
---|
2001 | * then put a check for LINUX_SLL_P_802_2 frames |
---|
2002 | * before it. |
---|
2003 | */ |
---|
2004 | b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX); |
---|
2005 | b1 = gen_snap(0x000000, ETHERTYPE_IPX); |
---|
2006 | gen_or(b0, b1); |
---|
2007 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2); |
---|
2008 | gen_and(b0, b1); |
---|
2009 | |
---|
2010 | /* |
---|
2011 | * Now check for 802.3 frames and OR that with |
---|
2012 | * the previous test. |
---|
2013 | */ |
---|
2014 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3); |
---|
2015 | gen_or(b0, b1); |
---|
2016 | |
---|
2017 | /* |
---|
2018 | * Now add the check for Ethernet_II frames, and |
---|
2019 | * do that before checking for the other frame |
---|
2020 | * types. |
---|
2021 | */ |
---|
2022 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
2023 | (bpf_int32)ETHERTYPE_IPX); |
---|
2024 | gen_or(b0, b1); |
---|
2025 | return b1; |
---|
2026 | |
---|
2027 | case ETHERTYPE_ATALK: |
---|
2028 | case ETHERTYPE_AARP: |
---|
2029 | /* |
---|
2030 | * EtherTalk (AppleTalk protocols on Ethernet link |
---|
2031 | * layer) may use 802.2 encapsulation. |
---|
2032 | */ |
---|
2033 | |
---|
2034 | /* |
---|
2035 | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
---|
2036 | * we check for the 802.2 protocol type in the |
---|
2037 | * "Ethernet type" field. |
---|
2038 | */ |
---|
2039 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2); |
---|
2040 | |
---|
2041 | /* |
---|
2042 | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
---|
2043 | * SNAP packets with an organization code of |
---|
2044 | * 0x080007 (Apple, for Appletalk) and a protocol |
---|
2045 | * type of ETHERTYPE_ATALK (Appletalk). |
---|
2046 | * |
---|
2047 | * 802.2-encapsulated ETHERTYPE_AARP packets are |
---|
2048 | * SNAP packets with an organization code of |
---|
2049 | * 0x000000 (encapsulated Ethernet) and a protocol |
---|
2050 | * type of ETHERTYPE_AARP (Appletalk ARP). |
---|
2051 | */ |
---|
2052 | if (proto == ETHERTYPE_ATALK) |
---|
2053 | b1 = gen_snap(0x080007, ETHERTYPE_ATALK); |
---|
2054 | else /* proto == ETHERTYPE_AARP */ |
---|
2055 | b1 = gen_snap(0x000000, ETHERTYPE_AARP); |
---|
2056 | gen_and(b0, b1); |
---|
2057 | |
---|
2058 | /* |
---|
2059 | * Check for Ethernet encapsulation (Ethertalk |
---|
2060 | * phase 1?); we just check for the Ethernet |
---|
2061 | * protocol type. |
---|
2062 | */ |
---|
2063 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto); |
---|
2064 | |
---|
2065 | gen_or(b0, b1); |
---|
2066 | return b1; |
---|
2067 | |
---|
2068 | default: |
---|
2069 | if (proto <= ETHERMTU) { |
---|
2070 | /* |
---|
2071 | * This is an LLC SAP value, so the frames |
---|
2072 | * that match would be 802.2 frames. |
---|
2073 | * Check for the 802.2 protocol type |
---|
2074 | * in the "Ethernet type" field, and |
---|
2075 | * then check the DSAP. |
---|
2076 | */ |
---|
2077 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
2078 | LINUX_SLL_P_802_2); |
---|
2079 | b1 = gen_cmp(OR_LINK, off_macpl, BPF_B, |
---|
2080 | (bpf_int32)proto); |
---|
2081 | gen_and(b0, b1); |
---|
2082 | return b1; |
---|
2083 | } else { |
---|
2084 | /* |
---|
2085 | * This is an Ethernet type, so compare |
---|
2086 | * the length/type field with it (if |
---|
2087 | * the frame is an 802.2 frame, the length |
---|
2088 | * field will be <= ETHERMTU, and, as |
---|
2089 | * "proto" is > ETHERMTU, this test |
---|
2090 | * will fail and the frame won't match, |
---|
2091 | * which is what we want). |
---|
2092 | */ |
---|
2093 | return gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
2094 | (bpf_int32)proto); |
---|
2095 | } |
---|
2096 | } |
---|
2097 | } |
---|
2098 | |
---|
2099 | static struct slist * |
---|
2100 | gen_load_prism_llprefixlen() |
---|
2101 | { |
---|
2102 | struct slist *s1, *s2; |
---|
2103 | struct slist *sjeq_avs_cookie; |
---|
2104 | struct slist *sjcommon; |
---|
2105 | |
---|
2106 | /* |
---|
2107 | * This code is not compatible with the optimizer, as |
---|
2108 | * we are generating jmp instructions within a normal |
---|
2109 | * slist of instructions |
---|
2110 | */ |
---|
2111 | no_optimize = 1; |
---|
2112 | |
---|
2113 | /* |
---|
2114 | * Generate code to load the length of the radio header into |
---|
2115 | * the register assigned to hold that length, if one has been |
---|
2116 | * assigned. (If one hasn't been assigned, no code we've |
---|
2117 | * generated uses that prefix, so we don't need to generate any |
---|
2118 | * code to load it.) |
---|
2119 | * |
---|
2120 | * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes |
---|
2121 | * or always use the AVS header rather than the Prism header. |
---|
2122 | * We load a 4-byte big-endian value at the beginning of the |
---|
2123 | * raw packet data, and see whether, when masked with 0xFFFFF000, |
---|
2124 | * it's equal to 0x80211000. If so, that indicates that it's |
---|
2125 | * an AVS header (the masked-out bits are the version number). |
---|
2126 | * Otherwise, it's a Prism header. |
---|
2127 | * |
---|
2128 | * XXX - the Prism header is also, in theory, variable-length, |
---|
2129 | * but no known software generates headers that aren't 144 |
---|
2130 | * bytes long. |
---|
2131 | */ |
---|
2132 | if (reg_off_ll != -1) { |
---|
2133 | /* |
---|
2134 | * Load the cookie. |
---|
2135 | */ |
---|
2136 | s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS); |
---|
2137 | s1->s.k = 0; |
---|
2138 | |
---|
2139 | /* |
---|
2140 | * AND it with 0xFFFFF000. |
---|
2141 | */ |
---|
2142 | s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K); |
---|
2143 | s2->s.k = 0xFFFFF000; |
---|
2144 | sappend(s1, s2); |
---|
2145 | |
---|
2146 | /* |
---|
2147 | * Compare with 0x80211000. |
---|
2148 | */ |
---|
2149 | sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ)); |
---|
2150 | sjeq_avs_cookie->s.k = 0x80211000; |
---|
2151 | sappend(s1, sjeq_avs_cookie); |
---|
2152 | |
---|
2153 | /* |
---|
2154 | * If it's AVS: |
---|
2155 | * |
---|
2156 | * The 4 bytes at an offset of 4 from the beginning of |
---|
2157 | * the AVS header are the length of the AVS header. |
---|
2158 | * That field is big-endian. |
---|
2159 | */ |
---|
2160 | s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS); |
---|
2161 | s2->s.k = 4; |
---|
2162 | sappend(s1, s2); |
---|
2163 | sjeq_avs_cookie->s.jt = s2; |
---|
2164 | |
---|
2165 | /* |
---|
2166 | * Now jump to the code to allocate a register |
---|
2167 | * into which to save the header length and |
---|
2168 | * store the length there. (The "jump always" |
---|
2169 | * instruction needs to have the k field set; |
---|
2170 | * it's added to the PC, so, as we're jumping |
---|
2171 | * over a single instruction, it should be 1.) |
---|
2172 | */ |
---|
2173 | sjcommon = new_stmt(JMP(BPF_JA)); |
---|
2174 | sjcommon->s.k = 1; |
---|
2175 | sappend(s1, sjcommon); |
---|
2176 | |
---|
2177 | /* |
---|
2178 | * Now for the code that handles the Prism header. |
---|
2179 | * Just load the length of the Prism header (144) |
---|
2180 | * into the A register. Have the test for an AVS |
---|
2181 | * header branch here if we don't have an AVS header. |
---|
2182 | */ |
---|
2183 | s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM); |
---|
2184 | s2->s.k = 144; |
---|
2185 | sappend(s1, s2); |
---|
2186 | sjeq_avs_cookie->s.jf = s2; |
---|
2187 | |
---|
2188 | /* |
---|
2189 | * Now allocate a register to hold that value and store |
---|
2190 | * it. The code for the AVS header will jump here after |
---|
2191 | * loading the length of the AVS header. |
---|
2192 | */ |
---|
2193 | s2 = new_stmt(BPF_ST); |
---|
2194 | s2->s.k = reg_off_ll; |
---|
2195 | sappend(s1, s2); |
---|
2196 | sjcommon->s.jf = s2; |
---|
2197 | |
---|
2198 | /* |
---|
2199 | * Now move it into the X register. |
---|
2200 | */ |
---|
2201 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2202 | sappend(s1, s2); |
---|
2203 | |
---|
2204 | return (s1); |
---|
2205 | } else |
---|
2206 | return (NULL); |
---|
2207 | } |
---|
2208 | |
---|
2209 | static struct slist * |
---|
2210 | gen_load_avs_llprefixlen() |
---|
2211 | { |
---|
2212 | struct slist *s1, *s2; |
---|
2213 | |
---|
2214 | /* |
---|
2215 | * Generate code to load the length of the AVS header into |
---|
2216 | * the register assigned to hold that length, if one has been |
---|
2217 | * assigned. (If one hasn't been assigned, no code we've |
---|
2218 | * generated uses that prefix, so we don't need to generate any |
---|
2219 | * code to load it.) |
---|
2220 | */ |
---|
2221 | if (reg_off_ll != -1) { |
---|
2222 | /* |
---|
2223 | * The 4 bytes at an offset of 4 from the beginning of |
---|
2224 | * the AVS header are the length of the AVS header. |
---|
2225 | * That field is big-endian. |
---|
2226 | */ |
---|
2227 | s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS); |
---|
2228 | s1->s.k = 4; |
---|
2229 | |
---|
2230 | /* |
---|
2231 | * Now allocate a register to hold that value and store |
---|
2232 | * it. |
---|
2233 | */ |
---|
2234 | s2 = new_stmt(BPF_ST); |
---|
2235 | s2->s.k = reg_off_ll; |
---|
2236 | sappend(s1, s2); |
---|
2237 | |
---|
2238 | /* |
---|
2239 | * Now move it into the X register. |
---|
2240 | */ |
---|
2241 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2242 | sappend(s1, s2); |
---|
2243 | |
---|
2244 | return (s1); |
---|
2245 | } else |
---|
2246 | return (NULL); |
---|
2247 | } |
---|
2248 | |
---|
2249 | static struct slist * |
---|
2250 | gen_load_radiotap_llprefixlen() |
---|
2251 | { |
---|
2252 | struct slist *s1, *s2; |
---|
2253 | |
---|
2254 | /* |
---|
2255 | * Generate code to load the length of the radiotap header into |
---|
2256 | * the register assigned to hold that length, if one has been |
---|
2257 | * assigned. (If one hasn't been assigned, no code we've |
---|
2258 | * generated uses that prefix, so we don't need to generate any |
---|
2259 | * code to load it.) |
---|
2260 | */ |
---|
2261 | if (reg_off_ll != -1) { |
---|
2262 | /* |
---|
2263 | * The 2 bytes at offsets of 2 and 3 from the beginning |
---|
2264 | * of the radiotap header are the length of the radiotap |
---|
2265 | * header; unfortunately, it's little-endian, so we have |
---|
2266 | * to load it a byte at a time and construct the value. |
---|
2267 | */ |
---|
2268 | |
---|
2269 | /* |
---|
2270 | * Load the high-order byte, at an offset of 3, shift it |
---|
2271 | * left a byte, and put the result in the X register. |
---|
2272 | */ |
---|
2273 | s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS); |
---|
2274 | s1->s.k = 3; |
---|
2275 | s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K); |
---|
2276 | sappend(s1, s2); |
---|
2277 | s2->s.k = 8; |
---|
2278 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2279 | sappend(s1, s2); |
---|
2280 | |
---|
2281 | /* |
---|
2282 | * Load the next byte, at an offset of 2, and OR the |
---|
2283 | * value from the X register into it. |
---|
2284 | */ |
---|
2285 | s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS); |
---|
2286 | sappend(s1, s2); |
---|
2287 | s2->s.k = 2; |
---|
2288 | s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X); |
---|
2289 | sappend(s1, s2); |
---|
2290 | |
---|
2291 | /* |
---|
2292 | * Now allocate a register to hold that value and store |
---|
2293 | * it. |
---|
2294 | */ |
---|
2295 | s2 = new_stmt(BPF_ST); |
---|
2296 | s2->s.k = reg_off_ll; |
---|
2297 | sappend(s1, s2); |
---|
2298 | |
---|
2299 | /* |
---|
2300 | * Now move it into the X register. |
---|
2301 | */ |
---|
2302 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2303 | sappend(s1, s2); |
---|
2304 | |
---|
2305 | return (s1); |
---|
2306 | } else |
---|
2307 | return (NULL); |
---|
2308 | } |
---|
2309 | |
---|
2310 | /* |
---|
2311 | * At the moment we treat PPI as normal Radiotap encoded |
---|
2312 | * packets. The difference is in the function that generates |
---|
2313 | * the code at the beginning to compute the header length. |
---|
2314 | * Since this code generator of PPI supports bare 802.11 |
---|
2315 | * encapsulation only (i.e. the encapsulated DLT should be |
---|
2316 | * DLT_IEEE802_11) we generate code to check for this too; |
---|
2317 | * that's done in finish_parse(). |
---|
2318 | */ |
---|
2319 | static struct slist * |
---|
2320 | gen_load_ppi_llprefixlen() |
---|
2321 | { |
---|
2322 | struct slist *s1, *s2; |
---|
2323 | |
---|
2324 | /* |
---|
2325 | * Generate code to load the length of the radiotap header |
---|
2326 | * into the register assigned to hold that length, if one has |
---|
2327 | * been assigned. |
---|
2328 | */ |
---|
2329 | if (reg_off_ll != -1) { |
---|
2330 | /* |
---|
2331 | * The 2 bytes at offsets of 2 and 3 from the beginning |
---|
2332 | * of the radiotap header are the length of the radiotap |
---|
2333 | * header; unfortunately, it's little-endian, so we have |
---|
2334 | * to load it a byte at a time and construct the value. |
---|
2335 | */ |
---|
2336 | |
---|
2337 | /* |
---|
2338 | * Load the high-order byte, at an offset of 3, shift it |
---|
2339 | * left a byte, and put the result in the X register. |
---|
2340 | */ |
---|
2341 | s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS); |
---|
2342 | s1->s.k = 3; |
---|
2343 | s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K); |
---|
2344 | sappend(s1, s2); |
---|
2345 | s2->s.k = 8; |
---|
2346 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2347 | sappend(s1, s2); |
---|
2348 | |
---|
2349 | /* |
---|
2350 | * Load the next byte, at an offset of 2, and OR the |
---|
2351 | * value from the X register into it. |
---|
2352 | */ |
---|
2353 | s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS); |
---|
2354 | sappend(s1, s2); |
---|
2355 | s2->s.k = 2; |
---|
2356 | s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X); |
---|
2357 | sappend(s1, s2); |
---|
2358 | |
---|
2359 | /* |
---|
2360 | * Now allocate a register to hold that value and store |
---|
2361 | * it. |
---|
2362 | */ |
---|
2363 | s2 = new_stmt(BPF_ST); |
---|
2364 | s2->s.k = reg_off_ll; |
---|
2365 | sappend(s1, s2); |
---|
2366 | |
---|
2367 | /* |
---|
2368 | * Now move it into the X register. |
---|
2369 | */ |
---|
2370 | s2 = new_stmt(BPF_MISC|BPF_TAX); |
---|
2371 | sappend(s1, s2); |
---|
2372 | |
---|
2373 | return (s1); |
---|
2374 | } else |
---|
2375 | return (NULL); |
---|
2376 | } |
---|
2377 | |
---|
2378 | /* |
---|
2379 | * Load a value relative to the beginning of the link-layer header after the 802.11 |
---|
2380 | * header, i.e. LLC_SNAP. |
---|
2381 | * The link-layer header doesn't necessarily begin at the beginning |
---|
2382 | * of the packet data; there might be a variable-length prefix containing |
---|
2383 | * radio information. |
---|
2384 | */ |
---|
2385 | static struct slist * |
---|
2386 | gen_load_802_11_header_len(struct slist *s, struct slist *snext) |
---|
2387 | { |
---|
2388 | struct slist *s2; |
---|
2389 | struct slist *sjset_data_frame_1; |
---|
2390 | struct slist *sjset_data_frame_2; |
---|
2391 | struct slist *sjset_qos; |
---|
2392 | struct slist *sjset_radiotap_flags; |
---|
2393 | struct slist *sjset_radiotap_tsft; |
---|
2394 | struct slist *sjset_tsft_datapad, *sjset_notsft_datapad; |
---|
2395 | struct slist *s_roundup; |
---|
2396 | |
---|
2397 | if (reg_off_macpl == -1) { |
---|
2398 | /* |
---|
2399 | * No register has been assigned to the offset of |
---|
2400 | * the MAC-layer payload, which means nobody needs |
---|
2401 | * it; don't bother computing it - just return |
---|
2402 | * what we already have. |
---|
2403 | */ |
---|
2404 | return (s); |
---|
2405 | } |
---|
2406 | |
---|
2407 | /* |
---|
2408 | * This code is not compatible with the optimizer, as |
---|
2409 | * we are generating jmp instructions within a normal |
---|
2410 | * slist of instructions |
---|
2411 | */ |
---|
2412 | no_optimize = 1; |
---|
2413 | |
---|
2414 | /* |
---|
2415 | * If "s" is non-null, it has code to arrange that the X register |
---|
2416 | * contains the length of the prefix preceding the link-layer |
---|
2417 | * header. |
---|
2418 | * |
---|
2419 | * Otherwise, the length of the prefix preceding the link-layer |
---|
2420 | * header is "off_ll". |
---|
2421 | */ |
---|
2422 | if (s == NULL) { |
---|
2423 | /* |
---|
2424 | * There is no variable-length header preceding the |
---|
2425 | * link-layer header. |
---|
2426 | * |
---|
2427 | * Load the length of the fixed-length prefix preceding |
---|
2428 | * the link-layer header (if any) into the X register, |
---|
2429 | * and store it in the reg_off_macpl register. |
---|
2430 | * That length is off_ll. |
---|
2431 | */ |
---|
2432 | s = new_stmt(BPF_LDX|BPF_IMM); |
---|
2433 | s->s.k = off_ll; |
---|
2434 | } |
---|
2435 | |
---|
2436 | /* |
---|
2437 | * The X register contains the offset of the beginning of the |
---|
2438 | * link-layer header; add 24, which is the minimum length |
---|
2439 | * of the MAC header for a data frame, to that, and store it |
---|
2440 | * in reg_off_macpl, and then load the Frame Control field, |
---|
2441 | * which is at the offset in the X register, with an indexed load. |
---|
2442 | */ |
---|
2443 | s2 = new_stmt(BPF_MISC|BPF_TXA); |
---|
2444 | sappend(s, s2); |
---|
2445 | s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
2446 | s2->s.k = 24; |
---|
2447 | sappend(s, s2); |
---|
2448 | s2 = new_stmt(BPF_ST); |
---|
2449 | s2->s.k = reg_off_macpl; |
---|
2450 | sappend(s, s2); |
---|
2451 | |
---|
2452 | s2 = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
2453 | s2->s.k = 0; |
---|
2454 | sappend(s, s2); |
---|
2455 | |
---|
2456 | /* |
---|
2457 | * Check the Frame Control field to see if this is a data frame; |
---|
2458 | * a data frame has the 0x08 bit (b3) in that field set and the |
---|
2459 | * 0x04 bit (b2) clear. |
---|
2460 | */ |
---|
2461 | sjset_data_frame_1 = new_stmt(JMP(BPF_JSET)); |
---|
2462 | sjset_data_frame_1->s.k = 0x08; |
---|
2463 | sappend(s, sjset_data_frame_1); |
---|
2464 | |
---|
2465 | /* |
---|
2466 | * If b3 is set, test b2, otherwise go to the first statement of |
---|
2467 | * the rest of the program. |
---|
2468 | */ |
---|
2469 | sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET)); |
---|
2470 | sjset_data_frame_2->s.k = 0x04; |
---|
2471 | sappend(s, sjset_data_frame_2); |
---|
2472 | sjset_data_frame_1->s.jf = snext; |
---|
2473 | |
---|
2474 | /* |
---|
2475 | * If b2 is not set, this is a data frame; test the QoS bit. |
---|
2476 | * Otherwise, go to the first statement of the rest of the |
---|
2477 | * program. |
---|
2478 | */ |
---|
2479 | sjset_data_frame_2->s.jt = snext; |
---|
2480 | sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET)); |
---|
2481 | sjset_qos->s.k = 0x80; /* QoS bit */ |
---|
2482 | sappend(s, sjset_qos); |
---|
2483 | |
---|
2484 | /* |
---|
2485 | * If it's set, add 2 to reg_off_macpl, to skip the QoS |
---|
2486 | * field. |
---|
2487 | * Otherwise, go to the first statement of the rest of the |
---|
2488 | * program. |
---|
2489 | */ |
---|
2490 | sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM); |
---|
2491 | s2->s.k = reg_off_macpl; |
---|
2492 | sappend(s, s2); |
---|
2493 | s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM); |
---|
2494 | s2->s.k = 2; |
---|
2495 | sappend(s, s2); |
---|
2496 | s2 = new_stmt(BPF_ST); |
---|
2497 | s2->s.k = reg_off_macpl; |
---|
2498 | sappend(s, s2); |
---|
2499 | |
---|
2500 | /* |
---|
2501 | * If we have a radiotap header, look at it to see whether |
---|
2502 | * there's Atheros padding between the MAC-layer header |
---|
2503 | * and the payload. |
---|
2504 | * |
---|
2505 | * Note: all of the fields in the radiotap header are |
---|
2506 | * little-endian, so we byte-swap all of the values |
---|
2507 | * we test against, as they will be loaded as big-endian |
---|
2508 | * values. |
---|
2509 | */ |
---|
2510 | if (linktype == DLT_IEEE802_11_RADIO) { |
---|
2511 | /* |
---|
2512 | * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set |
---|
2513 | * in the presence flag? |
---|
2514 | */ |
---|
2515 | sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W); |
---|
2516 | s2->s.k = 4; |
---|
2517 | sappend(s, s2); |
---|
2518 | |
---|
2519 | sjset_radiotap_flags = new_stmt(JMP(BPF_JSET)); |
---|
2520 | sjset_radiotap_flags->s.k = SWAPLONG(0x00000002); |
---|
2521 | sappend(s, sjset_radiotap_flags); |
---|
2522 | |
---|
2523 | /* |
---|
2524 | * If not, skip all of this. |
---|
2525 | */ |
---|
2526 | sjset_radiotap_flags->s.jf = snext; |
---|
2527 | |
---|
2528 | /* |
---|
2529 | * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set? |
---|
2530 | */ |
---|
2531 | sjset_radiotap_tsft = sjset_radiotap_flags->s.jt = |
---|
2532 | new_stmt(JMP(BPF_JSET)); |
---|
2533 | sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001); |
---|
2534 | sappend(s, sjset_radiotap_tsft); |
---|
2535 | |
---|
2536 | /* |
---|
2537 | * If IEEE80211_RADIOTAP_TSFT is set, the flags field is |
---|
2538 | * at an offset of 16 from the beginning of the raw packet |
---|
2539 | * data (8 bytes for the radiotap header and 8 bytes for |
---|
2540 | * the TSFT field). |
---|
2541 | * |
---|
2542 | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
---|
2543 | * is set. |
---|
2544 | */ |
---|
2545 | sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B); |
---|
2546 | s2->s.k = 16; |
---|
2547 | sappend(s, s2); |
---|
2548 | |
---|
2549 | sjset_tsft_datapad = new_stmt(JMP(BPF_JSET)); |
---|
2550 | sjset_tsft_datapad->s.k = 0x20; |
---|
2551 | sappend(s, sjset_tsft_datapad); |
---|
2552 | |
---|
2553 | /* |
---|
2554 | * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is |
---|
2555 | * at an offset of 8 from the beginning of the raw packet |
---|
2556 | * data (8 bytes for the radiotap header). |
---|
2557 | * |
---|
2558 | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
---|
2559 | * is set. |
---|
2560 | */ |
---|
2561 | sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B); |
---|
2562 | s2->s.k = 8; |
---|
2563 | sappend(s, s2); |
---|
2564 | |
---|
2565 | sjset_notsft_datapad = new_stmt(JMP(BPF_JSET)); |
---|
2566 | sjset_notsft_datapad->s.k = 0x20; |
---|
2567 | sappend(s, sjset_notsft_datapad); |
---|
2568 | |
---|
2569 | /* |
---|
2570 | * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is |
---|
2571 | * set, round the length of the 802.11 header to |
---|
2572 | * a multiple of 4. Do that by adding 3 and then |
---|
2573 | * dividing by and multiplying by 4, which we do by |
---|
2574 | * ANDing with ~3. |
---|
2575 | */ |
---|
2576 | s_roundup = new_stmt(BPF_LD|BPF_MEM); |
---|
2577 | s_roundup->s.k = reg_off_macpl; |
---|
2578 | sappend(s, s_roundup); |
---|
2579 | s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM); |
---|
2580 | s2->s.k = 3; |
---|
2581 | sappend(s, s2); |
---|
2582 | s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM); |
---|
2583 | s2->s.k = ~3; |
---|
2584 | sappend(s, s2); |
---|
2585 | s2 = new_stmt(BPF_ST); |
---|
2586 | s2->s.k = reg_off_macpl; |
---|
2587 | sappend(s, s2); |
---|
2588 | |
---|
2589 | sjset_tsft_datapad->s.jt = s_roundup; |
---|
2590 | sjset_tsft_datapad->s.jf = snext; |
---|
2591 | sjset_notsft_datapad->s.jt = s_roundup; |
---|
2592 | sjset_notsft_datapad->s.jf = snext; |
---|
2593 | } else |
---|
2594 | sjset_qos->s.jf = snext; |
---|
2595 | |
---|
2596 | return s; |
---|
2597 | } |
---|
2598 | |
---|
2599 | static void |
---|
2600 | insert_compute_vloffsets(b) |
---|
2601 | struct block *b; |
---|
2602 | { |
---|
2603 | struct slist *s; |
---|
2604 | |
---|
2605 | /* |
---|
2606 | * For link-layer types that have a variable-length header |
---|
2607 | * preceding the link-layer header, generate code to load |
---|
2608 | * the offset of the link-layer header into the register |
---|
2609 | * assigned to that offset, if any. |
---|
2610 | */ |
---|
2611 | switch (linktype) { |
---|
2612 | |
---|
2613 | case DLT_PRISM_HEADER: |
---|
2614 | s = gen_load_prism_llprefixlen(); |
---|
2615 | break; |
---|
2616 | |
---|
2617 | case DLT_IEEE802_11_RADIO_AVS: |
---|
2618 | s = gen_load_avs_llprefixlen(); |
---|
2619 | break; |
---|
2620 | |
---|
2621 | case DLT_IEEE802_11_RADIO: |
---|
2622 | s = gen_load_radiotap_llprefixlen(); |
---|
2623 | break; |
---|
2624 | |
---|
2625 | case DLT_PPI: |
---|
2626 | s = gen_load_ppi_llprefixlen(); |
---|
2627 | break; |
---|
2628 | |
---|
2629 | default: |
---|
2630 | s = NULL; |
---|
2631 | break; |
---|
2632 | } |
---|
2633 | |
---|
2634 | /* |
---|
2635 | * For link-layer types that have a variable-length link-layer |
---|
2636 | * header, generate code to load the offset of the MAC-layer |
---|
2637 | * payload into the register assigned to that offset, if any. |
---|
2638 | */ |
---|
2639 | switch (linktype) { |
---|
2640 | |
---|
2641 | case DLT_IEEE802_11: |
---|
2642 | case DLT_PRISM_HEADER: |
---|
2643 | case DLT_IEEE802_11_RADIO_AVS: |
---|
2644 | case DLT_IEEE802_11_RADIO: |
---|
2645 | case DLT_PPI: |
---|
2646 | s = gen_load_802_11_header_len(s, b->stmts); |
---|
2647 | break; |
---|
2648 | } |
---|
2649 | |
---|
2650 | /* |
---|
2651 | * If we have any offset-loading code, append all the |
---|
2652 | * existing statements in the block to those statements, |
---|
2653 | * and make the resulting list the list of statements |
---|
2654 | * for the block. |
---|
2655 | */ |
---|
2656 | if (s != NULL) { |
---|
2657 | sappend(s, b->stmts); |
---|
2658 | b->stmts = s; |
---|
2659 | } |
---|
2660 | } |
---|
2661 | |
---|
2662 | static struct block * |
---|
2663 | gen_ppi_dlt_check(void) |
---|
2664 | { |
---|
2665 | struct slist *s_load_dlt; |
---|
2666 | struct block *b; |
---|
2667 | |
---|
2668 | if (linktype == DLT_PPI) |
---|
2669 | { |
---|
2670 | /* Create the statements that check for the DLT |
---|
2671 | */ |
---|
2672 | s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS); |
---|
2673 | s_load_dlt->s.k = 4; |
---|
2674 | |
---|
2675 | b = new_block(JMP(BPF_JEQ)); |
---|
2676 | |
---|
2677 | b->stmts = s_load_dlt; |
---|
2678 | b->s.k = SWAPLONG(DLT_IEEE802_11); |
---|
2679 | } |
---|
2680 | else |
---|
2681 | { |
---|
2682 | b = NULL; |
---|
2683 | } |
---|
2684 | |
---|
2685 | return b; |
---|
2686 | } |
---|
2687 | |
---|
2688 | static struct slist * |
---|
2689 | gen_prism_llprefixlen(void) |
---|
2690 | { |
---|
2691 | struct slist *s; |
---|
2692 | |
---|
2693 | if (reg_off_ll == -1) { |
---|
2694 | /* |
---|
2695 | * We haven't yet assigned a register for the length |
---|
2696 | * of the radio header; allocate one. |
---|
2697 | */ |
---|
2698 | reg_off_ll = alloc_reg(); |
---|
2699 | } |
---|
2700 | |
---|
2701 | /* |
---|
2702 | * Load the register containing the radio length |
---|
2703 | * into the X register. |
---|
2704 | */ |
---|
2705 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
2706 | s->s.k = reg_off_ll; |
---|
2707 | return s; |
---|
2708 | } |
---|
2709 | |
---|
2710 | static struct slist * |
---|
2711 | gen_avs_llprefixlen(void) |
---|
2712 | { |
---|
2713 | struct slist *s; |
---|
2714 | |
---|
2715 | if (reg_off_ll == -1) { |
---|
2716 | /* |
---|
2717 | * We haven't yet assigned a register for the length |
---|
2718 | * of the AVS header; allocate one. |
---|
2719 | */ |
---|
2720 | reg_off_ll = alloc_reg(); |
---|
2721 | } |
---|
2722 | |
---|
2723 | /* |
---|
2724 | * Load the register containing the AVS length |
---|
2725 | * into the X register. |
---|
2726 | */ |
---|
2727 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
2728 | s->s.k = reg_off_ll; |
---|
2729 | return s; |
---|
2730 | } |
---|
2731 | |
---|
2732 | static struct slist * |
---|
2733 | gen_radiotap_llprefixlen(void) |
---|
2734 | { |
---|
2735 | struct slist *s; |
---|
2736 | |
---|
2737 | if (reg_off_ll == -1) { |
---|
2738 | /* |
---|
2739 | * We haven't yet assigned a register for the length |
---|
2740 | * of the radiotap header; allocate one. |
---|
2741 | */ |
---|
2742 | reg_off_ll = alloc_reg(); |
---|
2743 | } |
---|
2744 | |
---|
2745 | /* |
---|
2746 | * Load the register containing the radiotap length |
---|
2747 | * into the X register. |
---|
2748 | */ |
---|
2749 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
2750 | s->s.k = reg_off_ll; |
---|
2751 | return s; |
---|
2752 | } |
---|
2753 | |
---|
2754 | /* |
---|
2755 | * At the moment we treat PPI as normal Radiotap encoded |
---|
2756 | * packets. The difference is in the function that generates |
---|
2757 | * the code at the beginning to compute the header length. |
---|
2758 | * Since this code generator of PPI supports bare 802.11 |
---|
2759 | * encapsulation only (i.e. the encapsulated DLT should be |
---|
2760 | * DLT_IEEE802_11) we generate code to check for this too. |
---|
2761 | */ |
---|
2762 | static struct slist * |
---|
2763 | gen_ppi_llprefixlen(void) |
---|
2764 | { |
---|
2765 | struct slist *s; |
---|
2766 | |
---|
2767 | if (reg_off_ll == -1) { |
---|
2768 | /* |
---|
2769 | * We haven't yet assigned a register for the length |
---|
2770 | * of the radiotap header; allocate one. |
---|
2771 | */ |
---|
2772 | reg_off_ll = alloc_reg(); |
---|
2773 | } |
---|
2774 | |
---|
2775 | /* |
---|
2776 | * Load the register containing the PPI length |
---|
2777 | * into the X register. |
---|
2778 | */ |
---|
2779 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
2780 | s->s.k = reg_off_ll; |
---|
2781 | return s; |
---|
2782 | } |
---|
2783 | |
---|
2784 | /* |
---|
2785 | * Generate code to compute the link-layer header length, if necessary, |
---|
2786 | * putting it into the X register, and to return either a pointer to a |
---|
2787 | * "struct slist" for the list of statements in that code, or NULL if |
---|
2788 | * no code is necessary. |
---|
2789 | */ |
---|
2790 | static struct slist * |
---|
2791 | gen_llprefixlen(void) |
---|
2792 | { |
---|
2793 | switch (linktype) { |
---|
2794 | |
---|
2795 | case DLT_PRISM_HEADER: |
---|
2796 | return gen_prism_llprefixlen(); |
---|
2797 | |
---|
2798 | case DLT_IEEE802_11_RADIO_AVS: |
---|
2799 | return gen_avs_llprefixlen(); |
---|
2800 | |
---|
2801 | case DLT_IEEE802_11_RADIO: |
---|
2802 | return gen_radiotap_llprefixlen(); |
---|
2803 | |
---|
2804 | case DLT_PPI: |
---|
2805 | return gen_ppi_llprefixlen(); |
---|
2806 | |
---|
2807 | default: |
---|
2808 | return NULL; |
---|
2809 | } |
---|
2810 | } |
---|
2811 | |
---|
2812 | /* |
---|
2813 | * Generate code to load the register containing the offset of the |
---|
2814 | * MAC-layer payload into the X register; if no register for that offset |
---|
2815 | * has been allocated, allocate it first. |
---|
2816 | */ |
---|
2817 | static struct slist * |
---|
2818 | gen_off_macpl(void) |
---|
2819 | { |
---|
2820 | struct slist *s; |
---|
2821 | |
---|
2822 | if (off_macpl_is_variable) { |
---|
2823 | if (reg_off_macpl == -1) { |
---|
2824 | /* |
---|
2825 | * We haven't yet assigned a register for the offset |
---|
2826 | * of the MAC-layer payload; allocate one. |
---|
2827 | */ |
---|
2828 | reg_off_macpl = alloc_reg(); |
---|
2829 | } |
---|
2830 | |
---|
2831 | /* |
---|
2832 | * Load the register containing the offset of the MAC-layer |
---|
2833 | * payload into the X register. |
---|
2834 | */ |
---|
2835 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
2836 | s->s.k = reg_off_macpl; |
---|
2837 | return s; |
---|
2838 | } else { |
---|
2839 | /* |
---|
2840 | * That offset isn't variable, so we don't need to |
---|
2841 | * generate any code. |
---|
2842 | */ |
---|
2843 | return NULL; |
---|
2844 | } |
---|
2845 | } |
---|
2846 | |
---|
2847 | /* |
---|
2848 | * Map an Ethernet type to the equivalent PPP type. |
---|
2849 | */ |
---|
2850 | static int |
---|
2851 | ethertype_to_ppptype(proto) |
---|
2852 | int proto; |
---|
2853 | { |
---|
2854 | switch (proto) { |
---|
2855 | |
---|
2856 | case ETHERTYPE_IP: |
---|
2857 | proto = PPP_IP; |
---|
2858 | break; |
---|
2859 | |
---|
2860 | case ETHERTYPE_IPV6: |
---|
2861 | proto = PPP_IPV6; |
---|
2862 | break; |
---|
2863 | |
---|
2864 | case ETHERTYPE_DN: |
---|
2865 | proto = PPP_DECNET; |
---|
2866 | break; |
---|
2867 | |
---|
2868 | case ETHERTYPE_ATALK: |
---|
2869 | proto = PPP_APPLE; |
---|
2870 | break; |
---|
2871 | |
---|
2872 | case ETHERTYPE_NS: |
---|
2873 | proto = PPP_NS; |
---|
2874 | break; |
---|
2875 | |
---|
2876 | case LLCSAP_ISONS: |
---|
2877 | proto = PPP_OSI; |
---|
2878 | break; |
---|
2879 | |
---|
2880 | case LLCSAP_8021D: |
---|
2881 | /* |
---|
2882 | * I'm assuming the "Bridging PDU"s that go |
---|
2883 | * over PPP are Spanning Tree Protocol |
---|
2884 | * Bridging PDUs. |
---|
2885 | */ |
---|
2886 | proto = PPP_BRPDU; |
---|
2887 | break; |
---|
2888 | |
---|
2889 | case LLCSAP_IPX: |
---|
2890 | proto = PPP_IPX; |
---|
2891 | break; |
---|
2892 | } |
---|
2893 | return (proto); |
---|
2894 | } |
---|
2895 | |
---|
2896 | /* |
---|
2897 | * Generate code to match a particular packet type by matching the |
---|
2898 | * link-layer type field or fields in the 802.2 LLC header. |
---|
2899 | * |
---|
2900 | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
---|
2901 | * value, if <= ETHERMTU. |
---|
2902 | */ |
---|
2903 | static struct block * |
---|
2904 | gen_linktype(proto) |
---|
2905 | register int proto; |
---|
2906 | { |
---|
2907 | struct block *b0, *b1, *b2; |
---|
2908 | |
---|
2909 | /* are we checking MPLS-encapsulated packets? */ |
---|
2910 | if (label_stack_depth > 0) { |
---|
2911 | switch (proto) { |
---|
2912 | case ETHERTYPE_IP: |
---|
2913 | case PPP_IP: |
---|
2914 | /* FIXME add other L3 proto IDs */ |
---|
2915 | return gen_mpls_linktype(Q_IP); |
---|
2916 | |
---|
2917 | case ETHERTYPE_IPV6: |
---|
2918 | case PPP_IPV6: |
---|
2919 | /* FIXME add other L3 proto IDs */ |
---|
2920 | return gen_mpls_linktype(Q_IPV6); |
---|
2921 | |
---|
2922 | default: |
---|
2923 | bpf_error("unsupported protocol over mpls"); |
---|
2924 | /* NOTREACHED */ |
---|
2925 | } |
---|
2926 | } |
---|
2927 | |
---|
2928 | /* |
---|
2929 | * Are we testing PPPoE packets? |
---|
2930 | */ |
---|
2931 | if (is_pppoes) { |
---|
2932 | /* |
---|
2933 | * The PPPoE session header is part of the |
---|
2934 | * MAC-layer payload, so all references |
---|
2935 | * should be relative to the beginning of |
---|
2936 | * that payload. |
---|
2937 | */ |
---|
2938 | |
---|
2939 | /* |
---|
2940 | * We use Ethernet protocol types inside libpcap; |
---|
2941 | * map them to the corresponding PPP protocol types. |
---|
2942 | */ |
---|
2943 | proto = ethertype_to_ppptype(proto); |
---|
2944 | return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto); |
---|
2945 | } |
---|
2946 | |
---|
2947 | switch (linktype) { |
---|
2948 | |
---|
2949 | case DLT_EN10MB: |
---|
2950 | case DLT_NETANALYZER: |
---|
2951 | case DLT_NETANALYZER_TRANSPARENT: |
---|
2952 | return gen_ether_linktype(proto); |
---|
2953 | /*NOTREACHED*/ |
---|
2954 | break; |
---|
2955 | |
---|
2956 | case DLT_C_HDLC: |
---|
2957 | switch (proto) { |
---|
2958 | |
---|
2959 | case LLCSAP_ISONS: |
---|
2960 | proto = (proto << 8 | LLCSAP_ISONS); |
---|
2961 | /* fall through */ |
---|
2962 | |
---|
2963 | default: |
---|
2964 | return gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
2965 | (bpf_int32)proto); |
---|
2966 | /*NOTREACHED*/ |
---|
2967 | break; |
---|
2968 | } |
---|
2969 | break; |
---|
2970 | |
---|
2971 | case DLT_IEEE802_11: |
---|
2972 | case DLT_PRISM_HEADER: |
---|
2973 | case DLT_IEEE802_11_RADIO_AVS: |
---|
2974 | case DLT_IEEE802_11_RADIO: |
---|
2975 | case DLT_PPI: |
---|
2976 | /* |
---|
2977 | * Check that we have a data frame. |
---|
2978 | */ |
---|
2979 | b0 = gen_check_802_11_data_frame(); |
---|
2980 | |
---|
2981 | /* |
---|
2982 | * Now check for the specified link-layer type. |
---|
2983 | */ |
---|
2984 | b1 = gen_llc_linktype(proto); |
---|
2985 | gen_and(b0, b1); |
---|
2986 | return b1; |
---|
2987 | /*NOTREACHED*/ |
---|
2988 | break; |
---|
2989 | |
---|
2990 | case DLT_FDDI: |
---|
2991 | /* |
---|
2992 | * XXX - check for asynchronous frames, as per RFC 1103. |
---|
2993 | */ |
---|
2994 | return gen_llc_linktype(proto); |
---|
2995 | /*NOTREACHED*/ |
---|
2996 | break; |
---|
2997 | |
---|
2998 | case DLT_IEEE802: |
---|
2999 | /* |
---|
3000 | * XXX - check for LLC PDUs, as per IEEE 802.5. |
---|
3001 | */ |
---|
3002 | return gen_llc_linktype(proto); |
---|
3003 | /*NOTREACHED*/ |
---|
3004 | break; |
---|
3005 | |
---|
3006 | case DLT_ATM_RFC1483: |
---|
3007 | case DLT_ATM_CLIP: |
---|
3008 | case DLT_IP_OVER_FC: |
---|
3009 | return gen_llc_linktype(proto); |
---|
3010 | /*NOTREACHED*/ |
---|
3011 | break; |
---|
3012 | |
---|
3013 | case DLT_SUNATM: |
---|
3014 | /* |
---|
3015 | * If "is_lane" is set, check for a LANE-encapsulated |
---|
3016 | * version of this protocol, otherwise check for an |
---|
3017 | * LLC-encapsulated version of this protocol. |
---|
3018 | * |
---|
3019 | * We assume LANE means Ethernet, not Token Ring. |
---|
3020 | */ |
---|
3021 | if (is_lane) { |
---|
3022 | /* |
---|
3023 | * Check that the packet doesn't begin with an |
---|
3024 | * LE Control marker. (We've already generated |
---|
3025 | * a test for LANE.) |
---|
3026 | */ |
---|
3027 | b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H, |
---|
3028 | 0xFF00); |
---|
3029 | gen_not(b0); |
---|
3030 | |
---|
3031 | /* |
---|
3032 | * Now generate an Ethernet test. |
---|
3033 | */ |
---|
3034 | b1 = gen_ether_linktype(proto); |
---|
3035 | gen_and(b0, b1); |
---|
3036 | return b1; |
---|
3037 | } else { |
---|
3038 | /* |
---|
3039 | * Check for LLC encapsulation and then check the |
---|
3040 | * protocol. |
---|
3041 | */ |
---|
3042 | b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); |
---|
3043 | b1 = gen_llc_linktype(proto); |
---|
3044 | gen_and(b0, b1); |
---|
3045 | return b1; |
---|
3046 | } |
---|
3047 | /*NOTREACHED*/ |
---|
3048 | break; |
---|
3049 | |
---|
3050 | case DLT_LINUX_SLL: |
---|
3051 | return gen_linux_sll_linktype(proto); |
---|
3052 | /*NOTREACHED*/ |
---|
3053 | break; |
---|
3054 | |
---|
3055 | case DLT_SLIP: |
---|
3056 | case DLT_SLIP_BSDOS: |
---|
3057 | case DLT_RAW: |
---|
3058 | /* |
---|
3059 | * These types don't provide any type field; packets |
---|
3060 | * are always IPv4 or IPv6. |
---|
3061 | * |
---|
3062 | * XXX - for IPv4, check for a version number of 4, and, |
---|
3063 | * for IPv6, check for a version number of 6? |
---|
3064 | */ |
---|
3065 | switch (proto) { |
---|
3066 | |
---|
3067 | case ETHERTYPE_IP: |
---|
3068 | /* Check for a version number of 4. */ |
---|
3069 | return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0); |
---|
3070 | |
---|
3071 | case ETHERTYPE_IPV6: |
---|
3072 | /* Check for a version number of 6. */ |
---|
3073 | return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0); |
---|
3074 | |
---|
3075 | default: |
---|
3076 | return gen_false(); /* always false */ |
---|
3077 | } |
---|
3078 | /*NOTREACHED*/ |
---|
3079 | break; |
---|
3080 | |
---|
3081 | case DLT_IPV4: |
---|
3082 | /* |
---|
3083 | * Raw IPv4, so no type field. |
---|
3084 | */ |
---|
3085 | if (proto == ETHERTYPE_IP) |
---|
3086 | return gen_true(); /* always true */ |
---|
3087 | |
---|
3088 | /* Checking for something other than IPv4; always false */ |
---|
3089 | return gen_false(); |
---|
3090 | /*NOTREACHED*/ |
---|
3091 | break; |
---|
3092 | |
---|
3093 | case DLT_IPV6: |
---|
3094 | /* |
---|
3095 | * Raw IPv6, so no type field. |
---|
3096 | */ |
---|
3097 | if (proto == ETHERTYPE_IPV6) |
---|
3098 | return gen_true(); /* always true */ |
---|
3099 | |
---|
3100 | /* Checking for something other than IPv6; always false */ |
---|
3101 | return gen_false(); |
---|
3102 | /*NOTREACHED*/ |
---|
3103 | break; |
---|
3104 | |
---|
3105 | case DLT_PPP: |
---|
3106 | case DLT_PPP_PPPD: |
---|
3107 | case DLT_PPP_SERIAL: |
---|
3108 | case DLT_PPP_ETHER: |
---|
3109 | /* |
---|
3110 | * We use Ethernet protocol types inside libpcap; |
---|
3111 | * map them to the corresponding PPP protocol types. |
---|
3112 | */ |
---|
3113 | proto = ethertype_to_ppptype(proto); |
---|
3114 | return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto); |
---|
3115 | /*NOTREACHED*/ |
---|
3116 | break; |
---|
3117 | |
---|
3118 | case DLT_PPP_BSDOS: |
---|
3119 | /* |
---|
3120 | * We use Ethernet protocol types inside libpcap; |
---|
3121 | * map them to the corresponding PPP protocol types. |
---|
3122 | */ |
---|
3123 | switch (proto) { |
---|
3124 | |
---|
3125 | case ETHERTYPE_IP: |
---|
3126 | /* |
---|
3127 | * Also check for Van Jacobson-compressed IP. |
---|
3128 | * XXX - do this for other forms of PPP? |
---|
3129 | */ |
---|
3130 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP); |
---|
3131 | b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC); |
---|
3132 | gen_or(b0, b1); |
---|
3133 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC); |
---|
3134 | gen_or(b1, b0); |
---|
3135 | return b0; |
---|
3136 | |
---|
3137 | default: |
---|
3138 | proto = ethertype_to_ppptype(proto); |
---|
3139 | return gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
3140 | (bpf_int32)proto); |
---|
3141 | } |
---|
3142 | /*NOTREACHED*/ |
---|
3143 | break; |
---|
3144 | |
---|
3145 | case DLT_NULL: |
---|
3146 | case DLT_LOOP: |
---|
3147 | case DLT_ENC: |
---|
3148 | /* |
---|
3149 | * For DLT_NULL, the link-layer header is a 32-bit |
---|
3150 | * word containing an AF_ value in *host* byte order, |
---|
3151 | * and for DLT_ENC, the link-layer header begins |
---|
3152 | * with a 32-bit work containing an AF_ value in |
---|
3153 | * host byte order. |
---|
3154 | * |
---|
3155 | * In addition, if we're reading a saved capture file, |
---|
3156 | * the host byte order in the capture may not be the |
---|
3157 | * same as the host byte order on this machine. |
---|
3158 | * |
---|
3159 | * For DLT_LOOP, the link-layer header is a 32-bit |
---|
3160 | * word containing an AF_ value in *network* byte order. |
---|
3161 | * |
---|
3162 | * XXX - AF_ values may, unfortunately, be platform- |
---|
3163 | * dependent; for example, FreeBSD's AF_INET6 is 24 |
---|
3164 | * whilst NetBSD's and OpenBSD's is 26. |
---|
3165 | * |
---|
3166 | * This means that, when reading a capture file, just |
---|
3167 | * checking for our AF_INET6 value won't work if the |
---|
3168 | * capture file came from another OS. |
---|
3169 | */ |
---|
3170 | switch (proto) { |
---|
3171 | |
---|
3172 | case ETHERTYPE_IP: |
---|
3173 | proto = AF_INET; |
---|
3174 | break; |
---|
3175 | |
---|
3176 | #ifdef INET6 |
---|
3177 | case ETHERTYPE_IPV6: |
---|
3178 | proto = AF_INET6; |
---|
3179 | break; |
---|
3180 | #endif |
---|
3181 | |
---|
3182 | default: |
---|
3183 | /* |
---|
3184 | * Not a type on which we support filtering. |
---|
3185 | * XXX - support those that have AF_ values |
---|
3186 | * #defined on this platform, at least? |
---|
3187 | */ |
---|
3188 | return gen_false(); |
---|
3189 | } |
---|
3190 | |
---|
3191 | if (linktype == DLT_NULL || linktype == DLT_ENC) { |
---|
3192 | /* |
---|
3193 | * The AF_ value is in host byte order, but |
---|
3194 | * the BPF interpreter will convert it to |
---|
3195 | * network byte order. |
---|
3196 | * |
---|
3197 | * If this is a save file, and it's from a |
---|
3198 | * machine with the opposite byte order to |
---|
3199 | * ours, we byte-swap the AF_ value. |
---|
3200 | * |
---|
3201 | * Then we run it through "htonl()", and |
---|
3202 | * generate code to compare against the result. |
---|
3203 | */ |
---|
3204 | if (bpf_pcap->sf.rfile != NULL && |
---|
3205 | bpf_pcap->sf.swapped) |
---|
3206 | proto = SWAPLONG(proto); |
---|
3207 | proto = htonl(proto); |
---|
3208 | } |
---|
3209 | return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto)); |
---|
3210 | |
---|
3211 | #ifdef HAVE_NET_PFVAR_H |
---|
3212 | case DLT_PFLOG: |
---|
3213 | /* |
---|
3214 | * af field is host byte order in contrast to the rest of |
---|
3215 | * the packet. |
---|
3216 | */ |
---|
3217 | if (proto == ETHERTYPE_IP) |
---|
3218 | return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af), |
---|
3219 | BPF_B, (bpf_int32)AF_INET)); |
---|
3220 | else if (proto == ETHERTYPE_IPV6) |
---|
3221 | return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af), |
---|
3222 | BPF_B, (bpf_int32)AF_INET6)); |
---|
3223 | else |
---|
3224 | return gen_false(); |
---|
3225 | /*NOTREACHED*/ |
---|
3226 | break; |
---|
3227 | #endif /* HAVE_NET_PFVAR_H */ |
---|
3228 | |
---|
3229 | case DLT_ARCNET: |
---|
3230 | case DLT_ARCNET_LINUX: |
---|
3231 | /* |
---|
3232 | * XXX should we check for first fragment if the protocol |
---|
3233 | * uses PHDS? |
---|
3234 | */ |
---|
3235 | switch (proto) { |
---|
3236 | |
---|
3237 | default: |
---|
3238 | return gen_false(); |
---|
3239 | |
---|
3240 | case ETHERTYPE_IPV6: |
---|
3241 | return (gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3242 | (bpf_int32)ARCTYPE_INET6)); |
---|
3243 | |
---|
3244 | case ETHERTYPE_IP: |
---|
3245 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3246 | (bpf_int32)ARCTYPE_IP); |
---|
3247 | b1 = gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3248 | (bpf_int32)ARCTYPE_IP_OLD); |
---|
3249 | gen_or(b0, b1); |
---|
3250 | return (b1); |
---|
3251 | |
---|
3252 | case ETHERTYPE_ARP: |
---|
3253 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3254 | (bpf_int32)ARCTYPE_ARP); |
---|
3255 | b1 = gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3256 | (bpf_int32)ARCTYPE_ARP_OLD); |
---|
3257 | gen_or(b0, b1); |
---|
3258 | return (b1); |
---|
3259 | |
---|
3260 | case ETHERTYPE_REVARP: |
---|
3261 | return (gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3262 | (bpf_int32)ARCTYPE_REVARP)); |
---|
3263 | |
---|
3264 | case ETHERTYPE_ATALK: |
---|
3265 | return (gen_cmp(OR_LINK, off_linktype, BPF_B, |
---|
3266 | (bpf_int32)ARCTYPE_ATALK)); |
---|
3267 | } |
---|
3268 | /*NOTREACHED*/ |
---|
3269 | break; |
---|
3270 | |
---|
3271 | case DLT_LTALK: |
---|
3272 | switch (proto) { |
---|
3273 | case ETHERTYPE_ATALK: |
---|
3274 | return gen_true(); |
---|
3275 | default: |
---|
3276 | return gen_false(); |
---|
3277 | } |
---|
3278 | /*NOTREACHED*/ |
---|
3279 | break; |
---|
3280 | |
---|
3281 | case DLT_FRELAY: |
---|
3282 | /* |
---|
3283 | * XXX - assumes a 2-byte Frame Relay header with |
---|
3284 | * DLCI and flags. What if the address is longer? |
---|
3285 | */ |
---|
3286 | switch (proto) { |
---|
3287 | |
---|
3288 | case ETHERTYPE_IP: |
---|
3289 | /* |
---|
3290 | * Check for the special NLPID for IP. |
---|
3291 | */ |
---|
3292 | return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc); |
---|
3293 | |
---|
3294 | case ETHERTYPE_IPV6: |
---|
3295 | /* |
---|
3296 | * Check for the special NLPID for IPv6. |
---|
3297 | */ |
---|
3298 | return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e); |
---|
3299 | |
---|
3300 | case LLCSAP_ISONS: |
---|
3301 | /* |
---|
3302 | * Check for several OSI protocols. |
---|
3303 | * |
---|
3304 | * Frame Relay packets typically have an OSI |
---|
3305 | * NLPID at the beginning; we check for each |
---|
3306 | * of them. |
---|
3307 | * |
---|
3308 | * What we check for is the NLPID and a frame |
---|
3309 | * control field of UI, i.e. 0x03 followed |
---|
3310 | * by the NLPID. |
---|
3311 | */ |
---|
3312 | b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP); |
---|
3313 | b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS); |
---|
3314 | b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS); |
---|
3315 | gen_or(b1, b2); |
---|
3316 | gen_or(b0, b2); |
---|
3317 | return b2; |
---|
3318 | |
---|
3319 | default: |
---|
3320 | return gen_false(); |
---|
3321 | } |
---|
3322 | /*NOTREACHED*/ |
---|
3323 | break; |
---|
3324 | |
---|
3325 | case DLT_MFR: |
---|
3326 | bpf_error("Multi-link Frame Relay link-layer type filtering not implemented"); |
---|
3327 | |
---|
3328 | case DLT_JUNIPER_MFR: |
---|
3329 | case DLT_JUNIPER_MLFR: |
---|
3330 | case DLT_JUNIPER_MLPPP: |
---|
3331 | case DLT_JUNIPER_ATM1: |
---|
3332 | case DLT_JUNIPER_ATM2: |
---|
3333 | case DLT_JUNIPER_PPPOE: |
---|
3334 | case DLT_JUNIPER_PPPOE_ATM: |
---|
3335 | case DLT_JUNIPER_GGSN: |
---|
3336 | case DLT_JUNIPER_ES: |
---|
3337 | case DLT_JUNIPER_MONITOR: |
---|
3338 | case DLT_JUNIPER_SERVICES: |
---|
3339 | case DLT_JUNIPER_ETHER: |
---|
3340 | case DLT_JUNIPER_PPP: |
---|
3341 | case DLT_JUNIPER_FRELAY: |
---|
3342 | case DLT_JUNIPER_CHDLC: |
---|
3343 | case DLT_JUNIPER_VP: |
---|
3344 | case DLT_JUNIPER_ST: |
---|
3345 | case DLT_JUNIPER_ISM: |
---|
3346 | case DLT_JUNIPER_VS: |
---|
3347 | case DLT_JUNIPER_SRX_E2E: |
---|
3348 | case DLT_JUNIPER_FIBRECHANNEL: |
---|
3349 | case DLT_JUNIPER_ATM_CEMIC: |
---|
3350 | |
---|
3351 | /* just lets verify the magic number for now - |
---|
3352 | * on ATM we may have up to 6 different encapsulations on the wire |
---|
3353 | * and need a lot of heuristics to figure out that the payload |
---|
3354 | * might be; |
---|
3355 | * |
---|
3356 | * FIXME encapsulation specific BPF_ filters |
---|
3357 | */ |
---|
3358 | return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */ |
---|
3359 | |
---|
3360 | case DLT_IPNET: |
---|
3361 | return gen_ipnet_linktype(proto); |
---|
3362 | |
---|
3363 | case DLT_LINUX_IRDA: |
---|
3364 | bpf_error("IrDA link-layer type filtering not implemented"); |
---|
3365 | |
---|
3366 | case DLT_DOCSIS: |
---|
3367 | bpf_error("DOCSIS link-layer type filtering not implemented"); |
---|
3368 | |
---|
3369 | case DLT_MTP2: |
---|
3370 | case DLT_MTP2_WITH_PHDR: |
---|
3371 | bpf_error("MTP2 link-layer type filtering not implemented"); |
---|
3372 | |
---|
3373 | case DLT_ERF: |
---|
3374 | bpf_error("ERF link-layer type filtering not implemented"); |
---|
3375 | |
---|
3376 | case DLT_PFSYNC: |
---|
3377 | bpf_error("PFSYNC link-layer type filtering not implemented"); |
---|
3378 | |
---|
3379 | case DLT_LINUX_LAPD: |
---|
3380 | bpf_error("LAPD link-layer type filtering not implemented"); |
---|
3381 | |
---|
3382 | case DLT_USB: |
---|
3383 | case DLT_USB_LINUX: |
---|
3384 | case DLT_USB_LINUX_MMAPPED: |
---|
3385 | bpf_error("USB link-layer type filtering not implemented"); |
---|
3386 | |
---|
3387 | case DLT_BLUETOOTH_HCI_H4: |
---|
3388 | case DLT_BLUETOOTH_HCI_H4_WITH_PHDR: |
---|
3389 | bpf_error("Bluetooth link-layer type filtering not implemented"); |
---|
3390 | |
---|
3391 | case DLT_CAN20B: |
---|
3392 | case DLT_CAN_SOCKETCAN: |
---|
3393 | bpf_error("CAN link-layer type filtering not implemented"); |
---|
3394 | |
---|
3395 | case DLT_IEEE802_15_4: |
---|
3396 | case DLT_IEEE802_15_4_LINUX: |
---|
3397 | case DLT_IEEE802_15_4_NONASK_PHY: |
---|
3398 | case DLT_IEEE802_15_4_NOFCS: |
---|
3399 | bpf_error("IEEE 802.15.4 link-layer type filtering not implemented"); |
---|
3400 | |
---|
3401 | case DLT_IEEE802_16_MAC_CPS_RADIO: |
---|
3402 | bpf_error("IEEE 802.16 link-layer type filtering not implemented"); |
---|
3403 | |
---|
3404 | case DLT_SITA: |
---|
3405 | bpf_error("SITA link-layer type filtering not implemented"); |
---|
3406 | |
---|
3407 | case DLT_RAIF1: |
---|
3408 | bpf_error("RAIF1 link-layer type filtering not implemented"); |
---|
3409 | |
---|
3410 | case DLT_IPMB: |
---|
3411 | bpf_error("IPMB link-layer type filtering not implemented"); |
---|
3412 | |
---|
3413 | case DLT_AX25_KISS: |
---|
3414 | bpf_error("AX.25 link-layer type filtering not implemented"); |
---|
3415 | } |
---|
3416 | |
---|
3417 | /* |
---|
3418 | * All the types that have no encapsulation should either be |
---|
3419 | * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if |
---|
3420 | * all packets are IP packets, or should be handled in some |
---|
3421 | * special case, if none of them are (if some are and some |
---|
3422 | * aren't, the lack of encapsulation is a problem, as we'd |
---|
3423 | * have to find some other way of determining the packet type). |
---|
3424 | * |
---|
3425 | * Therefore, if "off_linktype" is -1, there's an error. |
---|
3426 | */ |
---|
3427 | if (off_linktype == (u_int)-1) |
---|
3428 | abort(); |
---|
3429 | |
---|
3430 | /* |
---|
3431 | * Any type not handled above should always have an Ethernet |
---|
3432 | * type at an offset of "off_linktype". |
---|
3433 | */ |
---|
3434 | return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto); |
---|
3435 | } |
---|
3436 | |
---|
3437 | /* |
---|
3438 | * Check for an LLC SNAP packet with a given organization code and |
---|
3439 | * protocol type; we check the entire contents of the 802.2 LLC and |
---|
3440 | * snap headers, checking for DSAP and SSAP of SNAP and a control |
---|
3441 | * field of 0x03 in the LLC header, and for the specified organization |
---|
3442 | * code and protocol type in the SNAP header. |
---|
3443 | */ |
---|
3444 | static struct block * |
---|
3445 | gen_snap(orgcode, ptype) |
---|
3446 | bpf_u_int32 orgcode; |
---|
3447 | bpf_u_int32 ptype; |
---|
3448 | { |
---|
3449 | u_char snapblock[8]; |
---|
3450 | |
---|
3451 | snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */ |
---|
3452 | snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */ |
---|
3453 | snapblock[2] = 0x03; /* control = UI */ |
---|
3454 | snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */ |
---|
3455 | snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */ |
---|
3456 | snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */ |
---|
3457 | snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */ |
---|
3458 | snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */ |
---|
3459 | return gen_bcmp(OR_MACPL, 0, 8, snapblock); |
---|
3460 | } |
---|
3461 | |
---|
3462 | /* |
---|
3463 | * Generate code to match a particular packet type, for link-layer types |
---|
3464 | * using 802.2 LLC headers. |
---|
3465 | * |
---|
3466 | * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used |
---|
3467 | * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues. |
---|
3468 | * |
---|
3469 | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
---|
3470 | * value, if <= ETHERMTU. We use that to determine whether to |
---|
3471 | * match the DSAP or both DSAP and LSAP or to check the OUI and |
---|
3472 | * protocol ID in a SNAP header. |
---|
3473 | */ |
---|
3474 | static struct block * |
---|
3475 | gen_llc_linktype(proto) |
---|
3476 | int proto; |
---|
3477 | { |
---|
3478 | /* |
---|
3479 | * XXX - handle token-ring variable-length header. |
---|
3480 | */ |
---|
3481 | switch (proto) { |
---|
3482 | |
---|
3483 | case LLCSAP_IP: |
---|
3484 | case LLCSAP_ISONS: |
---|
3485 | case LLCSAP_NETBEUI: |
---|
3486 | /* |
---|
3487 | * XXX - should we check both the DSAP and the |
---|
3488 | * SSAP, like this, or should we check just the |
---|
3489 | * DSAP, as we do for other types <= ETHERMTU |
---|
3490 | * (i.e., other SAP values)? |
---|
3491 | */ |
---|
3492 | return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32) |
---|
3493 | ((proto << 8) | proto)); |
---|
3494 | |
---|
3495 | case LLCSAP_IPX: |
---|
3496 | /* |
---|
3497 | * XXX - are there ever SNAP frames for IPX on |
---|
3498 | * non-Ethernet 802.x networks? |
---|
3499 | */ |
---|
3500 | return gen_cmp(OR_MACPL, 0, BPF_B, |
---|
3501 | (bpf_int32)LLCSAP_IPX); |
---|
3502 | |
---|
3503 | case ETHERTYPE_ATALK: |
---|
3504 | /* |
---|
3505 | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
---|
3506 | * SNAP packets with an organization code of |
---|
3507 | * 0x080007 (Apple, for Appletalk) and a protocol |
---|
3508 | * type of ETHERTYPE_ATALK (Appletalk). |
---|
3509 | * |
---|
3510 | * XXX - check for an organization code of |
---|
3511 | * encapsulated Ethernet as well? |
---|
3512 | */ |
---|
3513 | return gen_snap(0x080007, ETHERTYPE_ATALK); |
---|
3514 | |
---|
3515 | default: |
---|
3516 | /* |
---|
3517 | * XXX - we don't have to check for IPX 802.3 |
---|
3518 | * here, but should we check for the IPX Ethertype? |
---|
3519 | */ |
---|
3520 | if (proto <= ETHERMTU) { |
---|
3521 | /* |
---|
3522 | * This is an LLC SAP value, so check |
---|
3523 | * the DSAP. |
---|
3524 | */ |
---|
3525 | return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto); |
---|
3526 | } else { |
---|
3527 | /* |
---|
3528 | * This is an Ethernet type; we assume that it's |
---|
3529 | * unlikely that it'll appear in the right place |
---|
3530 | * at random, and therefore check only the |
---|
3531 | * location that would hold the Ethernet type |
---|
3532 | * in a SNAP frame with an organization code of |
---|
3533 | * 0x000000 (encapsulated Ethernet). |
---|
3534 | * |
---|
3535 | * XXX - if we were to check for the SNAP DSAP and |
---|
3536 | * LSAP, as per XXX, and were also to check for an |
---|
3537 | * organization code of 0x000000 (encapsulated |
---|
3538 | * Ethernet), we'd do |
---|
3539 | * |
---|
3540 | * return gen_snap(0x000000, proto); |
---|
3541 | * |
---|
3542 | * here; for now, we don't, as per the above. |
---|
3543 | * I don't know whether it's worth the extra CPU |
---|
3544 | * time to do the right check or not. |
---|
3545 | */ |
---|
3546 | return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto); |
---|
3547 | } |
---|
3548 | } |
---|
3549 | } |
---|
3550 | |
---|
3551 | static struct block * |
---|
3552 | gen_hostop(addr, mask, dir, proto, src_off, dst_off) |
---|
3553 | bpf_u_int32 addr; |
---|
3554 | bpf_u_int32 mask; |
---|
3555 | int dir, proto; |
---|
3556 | u_int src_off, dst_off; |
---|
3557 | { |
---|
3558 | struct block *b0, *b1; |
---|
3559 | u_int offset; |
---|
3560 | |
---|
3561 | switch (dir) { |
---|
3562 | |
---|
3563 | case Q_SRC: |
---|
3564 | offset = src_off; |
---|
3565 | break; |
---|
3566 | |
---|
3567 | case Q_DST: |
---|
3568 | offset = dst_off; |
---|
3569 | break; |
---|
3570 | |
---|
3571 | case Q_AND: |
---|
3572 | b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off); |
---|
3573 | b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off); |
---|
3574 | gen_and(b0, b1); |
---|
3575 | return b1; |
---|
3576 | |
---|
3577 | case Q_OR: |
---|
3578 | case Q_DEFAULT: |
---|
3579 | b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off); |
---|
3580 | b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off); |
---|
3581 | gen_or(b0, b1); |
---|
3582 | return b1; |
---|
3583 | |
---|
3584 | default: |
---|
3585 | abort(); |
---|
3586 | } |
---|
3587 | b0 = gen_linktype(proto); |
---|
3588 | b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask); |
---|
3589 | gen_and(b0, b1); |
---|
3590 | return b1; |
---|
3591 | } |
---|
3592 | |
---|
3593 | #ifdef INET6 |
---|
3594 | static struct block * |
---|
3595 | gen_hostop6(addr, mask, dir, proto, src_off, dst_off) |
---|
3596 | struct in6_addr *addr; |
---|
3597 | struct in6_addr *mask; |
---|
3598 | int dir, proto; |
---|
3599 | u_int src_off, dst_off; |
---|
3600 | { |
---|
3601 | struct block *b0, *b1; |
---|
3602 | u_int offset; |
---|
3603 | u_int32_t *a, *m; |
---|
3604 | |
---|
3605 | switch (dir) { |
---|
3606 | |
---|
3607 | case Q_SRC: |
---|
3608 | offset = src_off; |
---|
3609 | break; |
---|
3610 | |
---|
3611 | case Q_DST: |
---|
3612 | offset = dst_off; |
---|
3613 | break; |
---|
3614 | |
---|
3615 | case Q_AND: |
---|
3616 | b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off); |
---|
3617 | b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off); |
---|
3618 | gen_and(b0, b1); |
---|
3619 | return b1; |
---|
3620 | |
---|
3621 | case Q_OR: |
---|
3622 | case Q_DEFAULT: |
---|
3623 | b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off); |
---|
3624 | b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off); |
---|
3625 | gen_or(b0, b1); |
---|
3626 | return b1; |
---|
3627 | |
---|
3628 | default: |
---|
3629 | abort(); |
---|
3630 | } |
---|
3631 | /* this order is important */ |
---|
3632 | a = (u_int32_t *)addr; |
---|
3633 | m = (u_int32_t *)mask; |
---|
3634 | b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3])); |
---|
3635 | b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2])); |
---|
3636 | gen_and(b0, b1); |
---|
3637 | b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1])); |
---|
3638 | gen_and(b0, b1); |
---|
3639 | b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0])); |
---|
3640 | gen_and(b0, b1); |
---|
3641 | b0 = gen_linktype(proto); |
---|
3642 | gen_and(b0, b1); |
---|
3643 | return b1; |
---|
3644 | } |
---|
3645 | #endif |
---|
3646 | |
---|
3647 | static struct block * |
---|
3648 | gen_ehostop(eaddr, dir) |
---|
3649 | register const u_char *eaddr; |
---|
3650 | register int dir; |
---|
3651 | { |
---|
3652 | register struct block *b0, *b1; |
---|
3653 | |
---|
3654 | switch (dir) { |
---|
3655 | case Q_SRC: |
---|
3656 | return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr); |
---|
3657 | |
---|
3658 | case Q_DST: |
---|
3659 | return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr); |
---|
3660 | |
---|
3661 | case Q_AND: |
---|
3662 | b0 = gen_ehostop(eaddr, Q_SRC); |
---|
3663 | b1 = gen_ehostop(eaddr, Q_DST); |
---|
3664 | gen_and(b0, b1); |
---|
3665 | return b1; |
---|
3666 | |
---|
3667 | case Q_DEFAULT: |
---|
3668 | case Q_OR: |
---|
3669 | b0 = gen_ehostop(eaddr, Q_SRC); |
---|
3670 | b1 = gen_ehostop(eaddr, Q_DST); |
---|
3671 | gen_or(b0, b1); |
---|
3672 | return b1; |
---|
3673 | |
---|
3674 | case Q_ADDR1: |
---|
3675 | bpf_error("'addr1' is only supported on 802.11 with 802.11 headers"); |
---|
3676 | break; |
---|
3677 | |
---|
3678 | case Q_ADDR2: |
---|
3679 | bpf_error("'addr2' is only supported on 802.11 with 802.11 headers"); |
---|
3680 | break; |
---|
3681 | |
---|
3682 | case Q_ADDR3: |
---|
3683 | bpf_error("'addr3' is only supported on 802.11 with 802.11 headers"); |
---|
3684 | break; |
---|
3685 | |
---|
3686 | case Q_ADDR4: |
---|
3687 | bpf_error("'addr4' is only supported on 802.11 with 802.11 headers"); |
---|
3688 | break; |
---|
3689 | |
---|
3690 | case Q_RA: |
---|
3691 | bpf_error("'ra' is only supported on 802.11 with 802.11 headers"); |
---|
3692 | break; |
---|
3693 | |
---|
3694 | case Q_TA: |
---|
3695 | bpf_error("'ta' is only supported on 802.11 with 802.11 headers"); |
---|
3696 | break; |
---|
3697 | } |
---|
3698 | abort(); |
---|
3699 | /* NOTREACHED */ |
---|
3700 | } |
---|
3701 | |
---|
3702 | /* |
---|
3703 | * Like gen_ehostop, but for DLT_FDDI |
---|
3704 | */ |
---|
3705 | static struct block * |
---|
3706 | gen_fhostop(eaddr, dir) |
---|
3707 | register const u_char *eaddr; |
---|
3708 | register int dir; |
---|
3709 | { |
---|
3710 | struct block *b0, *b1; |
---|
3711 | |
---|
3712 | switch (dir) { |
---|
3713 | case Q_SRC: |
---|
3714 | #ifdef PCAP_FDDIPAD |
---|
3715 | return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr); |
---|
3716 | #else |
---|
3717 | return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr); |
---|
3718 | #endif |
---|
3719 | |
---|
3720 | case Q_DST: |
---|
3721 | #ifdef PCAP_FDDIPAD |
---|
3722 | return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr); |
---|
3723 | #else |
---|
3724 | return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr); |
---|
3725 | #endif |
---|
3726 | |
---|
3727 | case Q_AND: |
---|
3728 | b0 = gen_fhostop(eaddr, Q_SRC); |
---|
3729 | b1 = gen_fhostop(eaddr, Q_DST); |
---|
3730 | gen_and(b0, b1); |
---|
3731 | return b1; |
---|
3732 | |
---|
3733 | case Q_DEFAULT: |
---|
3734 | case Q_OR: |
---|
3735 | b0 = gen_fhostop(eaddr, Q_SRC); |
---|
3736 | b1 = gen_fhostop(eaddr, Q_DST); |
---|
3737 | gen_or(b0, b1); |
---|
3738 | return b1; |
---|
3739 | |
---|
3740 | case Q_ADDR1: |
---|
3741 | bpf_error("'addr1' is only supported on 802.11"); |
---|
3742 | break; |
---|
3743 | |
---|
3744 | case Q_ADDR2: |
---|
3745 | bpf_error("'addr2' is only supported on 802.11"); |
---|
3746 | break; |
---|
3747 | |
---|
3748 | case Q_ADDR3: |
---|
3749 | bpf_error("'addr3' is only supported on 802.11"); |
---|
3750 | break; |
---|
3751 | |
---|
3752 | case Q_ADDR4: |
---|
3753 | bpf_error("'addr4' is only supported on 802.11"); |
---|
3754 | break; |
---|
3755 | |
---|
3756 | case Q_RA: |
---|
3757 | bpf_error("'ra' is only supported on 802.11"); |
---|
3758 | break; |
---|
3759 | |
---|
3760 | case Q_TA: |
---|
3761 | bpf_error("'ta' is only supported on 802.11"); |
---|
3762 | break; |
---|
3763 | } |
---|
3764 | abort(); |
---|
3765 | /* NOTREACHED */ |
---|
3766 | } |
---|
3767 | |
---|
3768 | /* |
---|
3769 | * Like gen_ehostop, but for DLT_IEEE802 (Token Ring) |
---|
3770 | */ |
---|
3771 | static struct block * |
---|
3772 | gen_thostop(eaddr, dir) |
---|
3773 | register const u_char *eaddr; |
---|
3774 | register int dir; |
---|
3775 | { |
---|
3776 | register struct block *b0, *b1; |
---|
3777 | |
---|
3778 | switch (dir) { |
---|
3779 | case Q_SRC: |
---|
3780 | return gen_bcmp(OR_LINK, 8, 6, eaddr); |
---|
3781 | |
---|
3782 | case Q_DST: |
---|
3783 | return gen_bcmp(OR_LINK, 2, 6, eaddr); |
---|
3784 | |
---|
3785 | case Q_AND: |
---|
3786 | b0 = gen_thostop(eaddr, Q_SRC); |
---|
3787 | b1 = gen_thostop(eaddr, Q_DST); |
---|
3788 | gen_and(b0, b1); |
---|
3789 | return b1; |
---|
3790 | |
---|
3791 | case Q_DEFAULT: |
---|
3792 | case Q_OR: |
---|
3793 | b0 = gen_thostop(eaddr, Q_SRC); |
---|
3794 | b1 = gen_thostop(eaddr, Q_DST); |
---|
3795 | gen_or(b0, b1); |
---|
3796 | return b1; |
---|
3797 | |
---|
3798 | case Q_ADDR1: |
---|
3799 | bpf_error("'addr1' is only supported on 802.11"); |
---|
3800 | break; |
---|
3801 | |
---|
3802 | case Q_ADDR2: |
---|
3803 | bpf_error("'addr2' is only supported on 802.11"); |
---|
3804 | break; |
---|
3805 | |
---|
3806 | case Q_ADDR3: |
---|
3807 | bpf_error("'addr3' is only supported on 802.11"); |
---|
3808 | break; |
---|
3809 | |
---|
3810 | case Q_ADDR4: |
---|
3811 | bpf_error("'addr4' is only supported on 802.11"); |
---|
3812 | break; |
---|
3813 | |
---|
3814 | case Q_RA: |
---|
3815 | bpf_error("'ra' is only supported on 802.11"); |
---|
3816 | break; |
---|
3817 | |
---|
3818 | case Q_TA: |
---|
3819 | bpf_error("'ta' is only supported on 802.11"); |
---|
3820 | break; |
---|
3821 | } |
---|
3822 | abort(); |
---|
3823 | /* NOTREACHED */ |
---|
3824 | } |
---|
3825 | |
---|
3826 | /* |
---|
3827 | * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and |
---|
3828 | * various 802.11 + radio headers. |
---|
3829 | */ |
---|
3830 | static struct block * |
---|
3831 | gen_wlanhostop(eaddr, dir) |
---|
3832 | register const u_char *eaddr; |
---|
3833 | register int dir; |
---|
3834 | { |
---|
3835 | register struct block *b0, *b1, *b2; |
---|
3836 | register struct slist *s; |
---|
3837 | |
---|
3838 | #ifdef ENABLE_WLAN_FILTERING_PATCH |
---|
3839 | /* |
---|
3840 | * TODO GV 20070613 |
---|
3841 | * We need to disable the optimizer because the optimizer is buggy |
---|
3842 | * and wipes out some LD instructions generated by the below |
---|
3843 | * code to validate the Frame Control bits |
---|
3844 | */ |
---|
3845 | no_optimize = 1; |
---|
3846 | #endif /* ENABLE_WLAN_FILTERING_PATCH */ |
---|
3847 | |
---|
3848 | switch (dir) { |
---|
3849 | case Q_SRC: |
---|
3850 | /* |
---|
3851 | * Oh, yuk. |
---|
3852 | * |
---|
3853 | * For control frames, there is no SA. |
---|
3854 | * |
---|
3855 | * For management frames, SA is at an |
---|
3856 | * offset of 10 from the beginning of |
---|
3857 | * the packet. |
---|
3858 | * |
---|
3859 | * For data frames, SA is at an offset |
---|
3860 | * of 10 from the beginning of the packet |
---|
3861 | * if From DS is clear, at an offset of |
---|
3862 | * 16 from the beginning of the packet |
---|
3863 | * if From DS is set and To DS is clear, |
---|
3864 | * and an offset of 24 from the beginning |
---|
3865 | * of the packet if From DS is set and To DS |
---|
3866 | * is set. |
---|
3867 | */ |
---|
3868 | |
---|
3869 | /* |
---|
3870 | * Generate the tests to be done for data frames |
---|
3871 | * with From DS set. |
---|
3872 | * |
---|
3873 | * First, check for To DS set, i.e. check "link[1] & 0x01". |
---|
3874 | */ |
---|
3875 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
3876 | b1 = new_block(JMP(BPF_JSET)); |
---|
3877 | b1->s.k = 0x01; /* To DS */ |
---|
3878 | b1->stmts = s; |
---|
3879 | |
---|
3880 | /* |
---|
3881 | * If To DS is set, the SA is at 24. |
---|
3882 | */ |
---|
3883 | b0 = gen_bcmp(OR_LINK, 24, 6, eaddr); |
---|
3884 | gen_and(b1, b0); |
---|
3885 | |
---|
3886 | /* |
---|
3887 | * Now, check for To DS not set, i.e. check |
---|
3888 | * "!(link[1] & 0x01)". |
---|
3889 | */ |
---|
3890 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
3891 | b2 = new_block(JMP(BPF_JSET)); |
---|
3892 | b2->s.k = 0x01; /* To DS */ |
---|
3893 | b2->stmts = s; |
---|
3894 | gen_not(b2); |
---|
3895 | |
---|
3896 | /* |
---|
3897 | * If To DS is not set, the SA is at 16. |
---|
3898 | */ |
---|
3899 | b1 = gen_bcmp(OR_LINK, 16, 6, eaddr); |
---|
3900 | gen_and(b2, b1); |
---|
3901 | |
---|
3902 | /* |
---|
3903 | * Now OR together the last two checks. That gives |
---|
3904 | * the complete set of checks for data frames with |
---|
3905 | * From DS set. |
---|
3906 | */ |
---|
3907 | gen_or(b1, b0); |
---|
3908 | |
---|
3909 | /* |
---|
3910 | * Now check for From DS being set, and AND that with |
---|
3911 | * the ORed-together checks. |
---|
3912 | */ |
---|
3913 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
3914 | b1 = new_block(JMP(BPF_JSET)); |
---|
3915 | b1->s.k = 0x02; /* From DS */ |
---|
3916 | b1->stmts = s; |
---|
3917 | gen_and(b1, b0); |
---|
3918 | |
---|
3919 | /* |
---|
3920 | * Now check for data frames with From DS not set. |
---|
3921 | */ |
---|
3922 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
3923 | b2 = new_block(JMP(BPF_JSET)); |
---|
3924 | b2->s.k = 0x02; /* From DS */ |
---|
3925 | b2->stmts = s; |
---|
3926 | gen_not(b2); |
---|
3927 | |
---|
3928 | /* |
---|
3929 | * If From DS isn't set, the SA is at 10. |
---|
3930 | */ |
---|
3931 | b1 = gen_bcmp(OR_LINK, 10, 6, eaddr); |
---|
3932 | gen_and(b2, b1); |
---|
3933 | |
---|
3934 | /* |
---|
3935 | * Now OR together the checks for data frames with |
---|
3936 | * From DS not set and for data frames with From DS |
---|
3937 | * set; that gives the checks done for data frames. |
---|
3938 | */ |
---|
3939 | gen_or(b1, b0); |
---|
3940 | |
---|
3941 | /* |
---|
3942 | * Now check for a data frame. |
---|
3943 | * I.e, check "link[0] & 0x08". |
---|
3944 | */ |
---|
3945 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
3946 | b1 = new_block(JMP(BPF_JSET)); |
---|
3947 | b1->s.k = 0x08; |
---|
3948 | b1->stmts = s; |
---|
3949 | |
---|
3950 | /* |
---|
3951 | * AND that with the checks done for data frames. |
---|
3952 | */ |
---|
3953 | gen_and(b1, b0); |
---|
3954 | |
---|
3955 | /* |
---|
3956 | * If the high-order bit of the type value is 0, this |
---|
3957 | * is a management frame. |
---|
3958 | * I.e, check "!(link[0] & 0x08)". |
---|
3959 | */ |
---|
3960 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
3961 | b2 = new_block(JMP(BPF_JSET)); |
---|
3962 | b2->s.k = 0x08; |
---|
3963 | b2->stmts = s; |
---|
3964 | gen_not(b2); |
---|
3965 | |
---|
3966 | /* |
---|
3967 | * For management frames, the SA is at 10. |
---|
3968 | */ |
---|
3969 | b1 = gen_bcmp(OR_LINK, 10, 6, eaddr); |
---|
3970 | gen_and(b2, b1); |
---|
3971 | |
---|
3972 | /* |
---|
3973 | * OR that with the checks done for data frames. |
---|
3974 | * That gives the checks done for management and |
---|
3975 | * data frames. |
---|
3976 | */ |
---|
3977 | gen_or(b1, b0); |
---|
3978 | |
---|
3979 | /* |
---|
3980 | * If the low-order bit of the type value is 1, |
---|
3981 | * this is either a control frame or a frame |
---|
3982 | * with a reserved type, and thus not a |
---|
3983 | * frame with an SA. |
---|
3984 | * |
---|
3985 | * I.e., check "!(link[0] & 0x04)". |
---|
3986 | */ |
---|
3987 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
3988 | b1 = new_block(JMP(BPF_JSET)); |
---|
3989 | b1->s.k = 0x04; |
---|
3990 | b1->stmts = s; |
---|
3991 | gen_not(b1); |
---|
3992 | |
---|
3993 | /* |
---|
3994 | * AND that with the checks for data and management |
---|
3995 | * frames. |
---|
3996 | */ |
---|
3997 | gen_and(b1, b0); |
---|
3998 | return b0; |
---|
3999 | |
---|
4000 | case Q_DST: |
---|
4001 | /* |
---|
4002 | * Oh, yuk. |
---|
4003 | * |
---|
4004 | * For control frames, there is no DA. |
---|
4005 | * |
---|
4006 | * For management frames, DA is at an |
---|
4007 | * offset of 4 from the beginning of |
---|
4008 | * the packet. |
---|
4009 | * |
---|
4010 | * For data frames, DA is at an offset |
---|
4011 | * of 4 from the beginning of the packet |
---|
4012 | * if To DS is clear and at an offset of |
---|
4013 | * 16 from the beginning of the packet |
---|
4014 | * if To DS is set. |
---|
4015 | */ |
---|
4016 | |
---|
4017 | /* |
---|
4018 | * Generate the tests to be done for data frames. |
---|
4019 | * |
---|
4020 | * First, check for To DS set, i.e. "link[1] & 0x01". |
---|
4021 | */ |
---|
4022 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
4023 | b1 = new_block(JMP(BPF_JSET)); |
---|
4024 | b1->s.k = 0x01; /* To DS */ |
---|
4025 | b1->stmts = s; |
---|
4026 | |
---|
4027 | /* |
---|
4028 | * If To DS is set, the DA is at 16. |
---|
4029 | */ |
---|
4030 | b0 = gen_bcmp(OR_LINK, 16, 6, eaddr); |
---|
4031 | gen_and(b1, b0); |
---|
4032 | |
---|
4033 | /* |
---|
4034 | * Now, check for To DS not set, i.e. check |
---|
4035 | * "!(link[1] & 0x01)". |
---|
4036 | */ |
---|
4037 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
4038 | b2 = new_block(JMP(BPF_JSET)); |
---|
4039 | b2->s.k = 0x01; /* To DS */ |
---|
4040 | b2->stmts = s; |
---|
4041 | gen_not(b2); |
---|
4042 | |
---|
4043 | /* |
---|
4044 | * If To DS is not set, the DA is at 4. |
---|
4045 | */ |
---|
4046 | b1 = gen_bcmp(OR_LINK, 4, 6, eaddr); |
---|
4047 | gen_and(b2, b1); |
---|
4048 | |
---|
4049 | /* |
---|
4050 | * Now OR together the last two checks. That gives |
---|
4051 | * the complete set of checks for data frames. |
---|
4052 | */ |
---|
4053 | gen_or(b1, b0); |
---|
4054 | |
---|
4055 | /* |
---|
4056 | * Now check for a data frame. |
---|
4057 | * I.e, check "link[0] & 0x08". |
---|
4058 | */ |
---|
4059 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
4060 | b1 = new_block(JMP(BPF_JSET)); |
---|
4061 | b1->s.k = 0x08; |
---|
4062 | b1->stmts = s; |
---|
4063 | |
---|
4064 | /* |
---|
4065 | * AND that with the checks done for data frames. |
---|
4066 | */ |
---|
4067 | gen_and(b1, b0); |
---|
4068 | |
---|
4069 | /* |
---|
4070 | * If the high-order bit of the type value is 0, this |
---|
4071 | * is a management frame. |
---|
4072 | * I.e, check "!(link[0] & 0x08)". |
---|
4073 | */ |
---|
4074 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
4075 | b2 = new_block(JMP(BPF_JSET)); |
---|
4076 | b2->s.k = 0x08; |
---|
4077 | b2->stmts = s; |
---|
4078 | gen_not(b2); |
---|
4079 | |
---|
4080 | /* |
---|
4081 | * For management frames, the DA is at 4. |
---|
4082 | */ |
---|
4083 | b1 = gen_bcmp(OR_LINK, 4, 6, eaddr); |
---|
4084 | gen_and(b2, b1); |
---|
4085 | |
---|
4086 | /* |
---|
4087 | * OR that with the checks done for data frames. |
---|
4088 | * That gives the checks done for management and |
---|
4089 | * data frames. |
---|
4090 | */ |
---|
4091 | gen_or(b1, b0); |
---|
4092 | |
---|
4093 | /* |
---|
4094 | * If the low-order bit of the type value is 1, |
---|
4095 | * this is either a control frame or a frame |
---|
4096 | * with a reserved type, and thus not a |
---|
4097 | * frame with an SA. |
---|
4098 | * |
---|
4099 | * I.e., check "!(link[0] & 0x04)". |
---|
4100 | */ |
---|
4101 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
4102 | b1 = new_block(JMP(BPF_JSET)); |
---|
4103 | b1->s.k = 0x04; |
---|
4104 | b1->stmts = s; |
---|
4105 | gen_not(b1); |
---|
4106 | |
---|
4107 | /* |
---|
4108 | * AND that with the checks for data and management |
---|
4109 | * frames. |
---|
4110 | */ |
---|
4111 | gen_and(b1, b0); |
---|
4112 | return b0; |
---|
4113 | |
---|
4114 | case Q_RA: |
---|
4115 | /* |
---|
4116 | * Not present in management frames; addr1 in other |
---|
4117 | * frames. |
---|
4118 | */ |
---|
4119 | |
---|
4120 | /* |
---|
4121 | * If the high-order bit of the type value is 0, this |
---|
4122 | * is a management frame. |
---|
4123 | * I.e, check "(link[0] & 0x08)". |
---|
4124 | */ |
---|
4125 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
4126 | b1 = new_block(JMP(BPF_JSET)); |
---|
4127 | b1->s.k = 0x08; |
---|
4128 | b1->stmts = s; |
---|
4129 | |
---|
4130 | /* |
---|
4131 | * Check addr1. |
---|
4132 | */ |
---|
4133 | b0 = gen_bcmp(OR_LINK, 4, 6, eaddr); |
---|
4134 | |
---|
4135 | /* |
---|
4136 | * AND that with the check of addr1. |
---|
4137 | */ |
---|
4138 | gen_and(b1, b0); |
---|
4139 | return (b0); |
---|
4140 | |
---|
4141 | case Q_TA: |
---|
4142 | /* |
---|
4143 | * Not present in management frames; addr2, if present, |
---|
4144 | * in other frames. |
---|
4145 | */ |
---|
4146 | |
---|
4147 | /* |
---|
4148 | * Not present in CTS or ACK control frames. |
---|
4149 | */ |
---|
4150 | b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
---|
4151 | IEEE80211_FC0_TYPE_MASK); |
---|
4152 | gen_not(b0); |
---|
4153 | b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
---|
4154 | IEEE80211_FC0_SUBTYPE_MASK); |
---|
4155 | gen_not(b1); |
---|
4156 | b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
---|
4157 | IEEE80211_FC0_SUBTYPE_MASK); |
---|
4158 | gen_not(b2); |
---|
4159 | gen_and(b1, b2); |
---|
4160 | gen_or(b0, b2); |
---|
4161 | |
---|
4162 | /* |
---|
4163 | * If the high-order bit of the type value is 0, this |
---|
4164 | * is a management frame. |
---|
4165 | * I.e, check "(link[0] & 0x08)". |
---|
4166 | */ |
---|
4167 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
4168 | b1 = new_block(JMP(BPF_JSET)); |
---|
4169 | b1->s.k = 0x08; |
---|
4170 | b1->stmts = s; |
---|
4171 | |
---|
4172 | /* |
---|
4173 | * AND that with the check for frames other than |
---|
4174 | * CTS and ACK frames. |
---|
4175 | */ |
---|
4176 | gen_and(b1, b2); |
---|
4177 | |
---|
4178 | /* |
---|
4179 | * Check addr2. |
---|
4180 | */ |
---|
4181 | b1 = gen_bcmp(OR_LINK, 10, 6, eaddr); |
---|
4182 | gen_and(b2, b1); |
---|
4183 | return b1; |
---|
4184 | |
---|
4185 | /* |
---|
4186 | * XXX - add BSSID keyword? |
---|
4187 | */ |
---|
4188 | case Q_ADDR1: |
---|
4189 | return (gen_bcmp(OR_LINK, 4, 6, eaddr)); |
---|
4190 | |
---|
4191 | case Q_ADDR2: |
---|
4192 | /* |
---|
4193 | * Not present in CTS or ACK control frames. |
---|
4194 | */ |
---|
4195 | b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
---|
4196 | IEEE80211_FC0_TYPE_MASK); |
---|
4197 | gen_not(b0); |
---|
4198 | b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
---|
4199 | IEEE80211_FC0_SUBTYPE_MASK); |
---|
4200 | gen_not(b1); |
---|
4201 | b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
---|
4202 | IEEE80211_FC0_SUBTYPE_MASK); |
---|
4203 | gen_not(b2); |
---|
4204 | gen_and(b1, b2); |
---|
4205 | gen_or(b0, b2); |
---|
4206 | b1 = gen_bcmp(OR_LINK, 10, 6, eaddr); |
---|
4207 | gen_and(b2, b1); |
---|
4208 | return b1; |
---|
4209 | |
---|
4210 | case Q_ADDR3: |
---|
4211 | /* |
---|
4212 | * Not present in control frames. |
---|
4213 | */ |
---|
4214 | b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
---|
4215 | IEEE80211_FC0_TYPE_MASK); |
---|
4216 | gen_not(b0); |
---|
4217 | b1 = gen_bcmp(OR_LINK, 16, 6, eaddr); |
---|
4218 | gen_and(b0, b1); |
---|
4219 | return b1; |
---|
4220 | |
---|
4221 | case Q_ADDR4: |
---|
4222 | /* |
---|
4223 | * Present only if the direction mask has both "From DS" |
---|
4224 | * and "To DS" set. Neither control frames nor management |
---|
4225 | * frames should have both of those set, so we don't |
---|
4226 | * check the frame type. |
---|
4227 | */ |
---|
4228 | b0 = gen_mcmp(OR_LINK, 1, BPF_B, |
---|
4229 | IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK); |
---|
4230 | b1 = gen_bcmp(OR_LINK, 24, 6, eaddr); |
---|
4231 | gen_and(b0, b1); |
---|
4232 | return b1; |
---|
4233 | |
---|
4234 | case Q_AND: |
---|
4235 | b0 = gen_wlanhostop(eaddr, Q_SRC); |
---|
4236 | b1 = gen_wlanhostop(eaddr, Q_DST); |
---|
4237 | gen_and(b0, b1); |
---|
4238 | return b1; |
---|
4239 | |
---|
4240 | case Q_DEFAULT: |
---|
4241 | case Q_OR: |
---|
4242 | b0 = gen_wlanhostop(eaddr, Q_SRC); |
---|
4243 | b1 = gen_wlanhostop(eaddr, Q_DST); |
---|
4244 | gen_or(b0, b1); |
---|
4245 | return b1; |
---|
4246 | } |
---|
4247 | abort(); |
---|
4248 | /* NOTREACHED */ |
---|
4249 | } |
---|
4250 | |
---|
4251 | /* |
---|
4252 | * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel. |
---|
4253 | * (We assume that the addresses are IEEE 48-bit MAC addresses, |
---|
4254 | * as the RFC states.) |
---|
4255 | */ |
---|
4256 | static struct block * |
---|
4257 | gen_ipfchostop(eaddr, dir) |
---|
4258 | register const u_char *eaddr; |
---|
4259 | register int dir; |
---|
4260 | { |
---|
4261 | register struct block *b0, *b1; |
---|
4262 | |
---|
4263 | switch (dir) { |
---|
4264 | case Q_SRC: |
---|
4265 | return gen_bcmp(OR_LINK, 10, 6, eaddr); |
---|
4266 | |
---|
4267 | case Q_DST: |
---|
4268 | return gen_bcmp(OR_LINK, 2, 6, eaddr); |
---|
4269 | |
---|
4270 | case Q_AND: |
---|
4271 | b0 = gen_ipfchostop(eaddr, Q_SRC); |
---|
4272 | b1 = gen_ipfchostop(eaddr, Q_DST); |
---|
4273 | gen_and(b0, b1); |
---|
4274 | return b1; |
---|
4275 | |
---|
4276 | case Q_DEFAULT: |
---|
4277 | case Q_OR: |
---|
4278 | b0 = gen_ipfchostop(eaddr, Q_SRC); |
---|
4279 | b1 = gen_ipfchostop(eaddr, Q_DST); |
---|
4280 | gen_or(b0, b1); |
---|
4281 | return b1; |
---|
4282 | |
---|
4283 | case Q_ADDR1: |
---|
4284 | bpf_error("'addr1' is only supported on 802.11"); |
---|
4285 | break; |
---|
4286 | |
---|
4287 | case Q_ADDR2: |
---|
4288 | bpf_error("'addr2' is only supported on 802.11"); |
---|
4289 | break; |
---|
4290 | |
---|
4291 | case Q_ADDR3: |
---|
4292 | bpf_error("'addr3' is only supported on 802.11"); |
---|
4293 | break; |
---|
4294 | |
---|
4295 | case Q_ADDR4: |
---|
4296 | bpf_error("'addr4' is only supported on 802.11"); |
---|
4297 | break; |
---|
4298 | |
---|
4299 | case Q_RA: |
---|
4300 | bpf_error("'ra' is only supported on 802.11"); |
---|
4301 | break; |
---|
4302 | |
---|
4303 | case Q_TA: |
---|
4304 | bpf_error("'ta' is only supported on 802.11"); |
---|
4305 | break; |
---|
4306 | } |
---|
4307 | abort(); |
---|
4308 | /* NOTREACHED */ |
---|
4309 | } |
---|
4310 | |
---|
4311 | /* |
---|
4312 | * This is quite tricky because there may be pad bytes in front of the |
---|
4313 | * DECNET header, and then there are two possible data packet formats that |
---|
4314 | * carry both src and dst addresses, plus 5 packet types in a format that |
---|
4315 | * carries only the src node, plus 2 types that use a different format and |
---|
4316 | * also carry just the src node. |
---|
4317 | * |
---|
4318 | * Yuck. |
---|
4319 | * |
---|
4320 | * Instead of doing those all right, we just look for data packets with |
---|
4321 | * 0 or 1 bytes of padding. If you want to look at other packets, that |
---|
4322 | * will require a lot more hacking. |
---|
4323 | * |
---|
4324 | * To add support for filtering on DECNET "areas" (network numbers) |
---|
4325 | * one would want to add a "mask" argument to this routine. That would |
---|
4326 | * make the filter even more inefficient, although one could be clever |
---|
4327 | * and not generate masking instructions if the mask is 0xFFFF. |
---|
4328 | */ |
---|
4329 | static struct block * |
---|
4330 | gen_dnhostop(addr, dir) |
---|
4331 | bpf_u_int32 addr; |
---|
4332 | int dir; |
---|
4333 | { |
---|
4334 | struct block *b0, *b1, *b2, *tmp; |
---|
4335 | u_int offset_lh; /* offset if long header is received */ |
---|
4336 | u_int offset_sh; /* offset if short header is received */ |
---|
4337 | |
---|
4338 | switch (dir) { |
---|
4339 | |
---|
4340 | case Q_DST: |
---|
4341 | offset_sh = 1; /* follows flags */ |
---|
4342 | offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ |
---|
4343 | break; |
---|
4344 | |
---|
4345 | case Q_SRC: |
---|
4346 | offset_sh = 3; /* follows flags, dstnode */ |
---|
4347 | offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ |
---|
4348 | break; |
---|
4349 | |
---|
4350 | case Q_AND: |
---|
4351 | /* Inefficient because we do our Calvinball dance twice */ |
---|
4352 | b0 = gen_dnhostop(addr, Q_SRC); |
---|
4353 | b1 = gen_dnhostop(addr, Q_DST); |
---|
4354 | gen_and(b0, b1); |
---|
4355 | return b1; |
---|
4356 | |
---|
4357 | case Q_OR: |
---|
4358 | case Q_DEFAULT: |
---|
4359 | /* Inefficient because we do our Calvinball dance twice */ |
---|
4360 | b0 = gen_dnhostop(addr, Q_SRC); |
---|
4361 | b1 = gen_dnhostop(addr, Q_DST); |
---|
4362 | gen_or(b0, b1); |
---|
4363 | return b1; |
---|
4364 | |
---|
4365 | case Q_ISO: |
---|
4366 | bpf_error("ISO host filtering not implemented"); |
---|
4367 | |
---|
4368 | default: |
---|
4369 | abort(); |
---|
4370 | } |
---|
4371 | b0 = gen_linktype(ETHERTYPE_DN); |
---|
4372 | /* Check for pad = 1, long header case */ |
---|
4373 | tmp = gen_mcmp(OR_NET, 2, BPF_H, |
---|
4374 | (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF)); |
---|
4375 | b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh, |
---|
4376 | BPF_H, (bpf_int32)ntohs((u_short)addr)); |
---|
4377 | gen_and(tmp, b1); |
---|
4378 | /* Check for pad = 0, long header case */ |
---|
4379 | tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7); |
---|
4380 | b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
---|
4381 | gen_and(tmp, b2); |
---|
4382 | gen_or(b2, b1); |
---|
4383 | /* Check for pad = 1, short header case */ |
---|
4384 | tmp = gen_mcmp(OR_NET, 2, BPF_H, |
---|
4385 | (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF)); |
---|
4386 | b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
---|
4387 | gen_and(tmp, b2); |
---|
4388 | gen_or(b2, b1); |
---|
4389 | /* Check for pad = 0, short header case */ |
---|
4390 | tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7); |
---|
4391 | b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
---|
4392 | gen_and(tmp, b2); |
---|
4393 | gen_or(b2, b1); |
---|
4394 | |
---|
4395 | /* Combine with test for linktype */ |
---|
4396 | gen_and(b0, b1); |
---|
4397 | return b1; |
---|
4398 | } |
---|
4399 | |
---|
4400 | /* |
---|
4401 | * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets; |
---|
4402 | * test the bottom-of-stack bit, and then check the version number |
---|
4403 | * field in the IP header. |
---|
4404 | */ |
---|
4405 | static struct block * |
---|
4406 | gen_mpls_linktype(proto) |
---|
4407 | int proto; |
---|
4408 | { |
---|
4409 | struct block *b0, *b1; |
---|
4410 | |
---|
4411 | switch (proto) { |
---|
4412 | |
---|
4413 | case Q_IP: |
---|
4414 | /* match the bottom-of-stack bit */ |
---|
4415 | b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01); |
---|
4416 | /* match the IPv4 version number */ |
---|
4417 | b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0); |
---|
4418 | gen_and(b0, b1); |
---|
4419 | return b1; |
---|
4420 | |
---|
4421 | case Q_IPV6: |
---|
4422 | /* match the bottom-of-stack bit */ |
---|
4423 | b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01); |
---|
4424 | /* match the IPv4 version number */ |
---|
4425 | b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0); |
---|
4426 | gen_and(b0, b1); |
---|
4427 | return b1; |
---|
4428 | |
---|
4429 | default: |
---|
4430 | abort(); |
---|
4431 | } |
---|
4432 | } |
---|
4433 | |
---|
4434 | static struct block * |
---|
4435 | gen_host(addr, mask, proto, dir, type) |
---|
4436 | bpf_u_int32 addr; |
---|
4437 | bpf_u_int32 mask; |
---|
4438 | int proto; |
---|
4439 | int dir; |
---|
4440 | int type; |
---|
4441 | { |
---|
4442 | struct block *b0, *b1; |
---|
4443 | const char *typestr; |
---|
4444 | |
---|
4445 | if (type == Q_NET) |
---|
4446 | typestr = "net"; |
---|
4447 | else |
---|
4448 | typestr = "host"; |
---|
4449 | |
---|
4450 | switch (proto) { |
---|
4451 | |
---|
4452 | case Q_DEFAULT: |
---|
4453 | b0 = gen_host(addr, mask, Q_IP, dir, type); |
---|
4454 | /* |
---|
4455 | * Only check for non-IPv4 addresses if we're not |
---|
4456 | * checking MPLS-encapsulated packets. |
---|
4457 | */ |
---|
4458 | if (label_stack_depth == 0) { |
---|
4459 | b1 = gen_host(addr, mask, Q_ARP, dir, type); |
---|
4460 | gen_or(b0, b1); |
---|
4461 | b0 = gen_host(addr, mask, Q_RARP, dir, type); |
---|
4462 | gen_or(b1, b0); |
---|
4463 | } |
---|
4464 | return b0; |
---|
4465 | |
---|
4466 | case Q_IP: |
---|
4467 | return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16); |
---|
4468 | |
---|
4469 | case Q_RARP: |
---|
4470 | return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24); |
---|
4471 | |
---|
4472 | case Q_ARP: |
---|
4473 | return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24); |
---|
4474 | |
---|
4475 | case Q_TCP: |
---|
4476 | bpf_error("'tcp' modifier applied to %s", typestr); |
---|
4477 | |
---|
4478 | case Q_SCTP: |
---|
4479 | bpf_error("'sctp' modifier applied to %s", typestr); |
---|
4480 | |
---|
4481 | case Q_UDP: |
---|
4482 | bpf_error("'udp' modifier applied to %s", typestr); |
---|
4483 | |
---|
4484 | case Q_ICMP: |
---|
4485 | bpf_error("'icmp' modifier applied to %s", typestr); |
---|
4486 | |
---|
4487 | case Q_IGMP: |
---|
4488 | bpf_error("'igmp' modifier applied to %s", typestr); |
---|
4489 | |
---|
4490 | case Q_IGRP: |
---|
4491 | bpf_error("'igrp' modifier applied to %s", typestr); |
---|
4492 | |
---|
4493 | case Q_PIM: |
---|
4494 | bpf_error("'pim' modifier applied to %s", typestr); |
---|
4495 | |
---|
4496 | case Q_VRRP: |
---|
4497 | bpf_error("'vrrp' modifier applied to %s", typestr); |
---|
4498 | |
---|
4499 | case Q_CARP: |
---|
4500 | bpf_error("'carp' modifier applied to %s", typestr); |
---|
4501 | |
---|
4502 | case Q_ATALK: |
---|
4503 | bpf_error("ATALK host filtering not implemented"); |
---|
4504 | |
---|
4505 | case Q_AARP: |
---|
4506 | bpf_error("AARP host filtering not implemented"); |
---|
4507 | |
---|
4508 | case Q_DECNET: |
---|
4509 | return gen_dnhostop(addr, dir); |
---|
4510 | |
---|
4511 | case Q_SCA: |
---|
4512 | bpf_error("SCA host filtering not implemented"); |
---|
4513 | |
---|
4514 | case Q_LAT: |
---|
4515 | bpf_error("LAT host filtering not implemented"); |
---|
4516 | |
---|
4517 | case Q_MOPDL: |
---|
4518 | bpf_error("MOPDL host filtering not implemented"); |
---|
4519 | |
---|
4520 | case Q_MOPRC: |
---|
4521 | bpf_error("MOPRC host filtering not implemented"); |
---|
4522 | |
---|
4523 | case Q_IPV6: |
---|
4524 | bpf_error("'ip6' modifier applied to ip host"); |
---|
4525 | |
---|
4526 | case Q_ICMPV6: |
---|
4527 | bpf_error("'icmp6' modifier applied to %s", typestr); |
---|
4528 | |
---|
4529 | case Q_AH: |
---|
4530 | bpf_error("'ah' modifier applied to %s", typestr); |
---|
4531 | |
---|
4532 | case Q_ESP: |
---|
4533 | bpf_error("'esp' modifier applied to %s", typestr); |
---|
4534 | |
---|
4535 | case Q_ISO: |
---|
4536 | bpf_error("ISO host filtering not implemented"); |
---|
4537 | |
---|
4538 | case Q_ESIS: |
---|
4539 | bpf_error("'esis' modifier applied to %s", typestr); |
---|
4540 | |
---|
4541 | case Q_ISIS: |
---|
4542 | bpf_error("'isis' modifier applied to %s", typestr); |
---|
4543 | |
---|
4544 | case Q_CLNP: |
---|
4545 | bpf_error("'clnp' modifier applied to %s", typestr); |
---|
4546 | |
---|
4547 | case Q_STP: |
---|
4548 | bpf_error("'stp' modifier applied to %s", typestr); |
---|
4549 | |
---|
4550 | case Q_IPX: |
---|
4551 | bpf_error("IPX host filtering not implemented"); |
---|
4552 | |
---|
4553 | case Q_NETBEUI: |
---|
4554 | bpf_error("'netbeui' modifier applied to %s", typestr); |
---|
4555 | |
---|
4556 | case Q_RADIO: |
---|
4557 | bpf_error("'radio' modifier applied to %s", typestr); |
---|
4558 | |
---|
4559 | default: |
---|
4560 | abort(); |
---|
4561 | } |
---|
4562 | /* NOTREACHED */ |
---|
4563 | } |
---|
4564 | |
---|
4565 | #ifdef INET6 |
---|
4566 | static struct block * |
---|
4567 | gen_host6(addr, mask, proto, dir, type) |
---|
4568 | struct in6_addr *addr; |
---|
4569 | struct in6_addr *mask; |
---|
4570 | int proto; |
---|
4571 | int dir; |
---|
4572 | int type; |
---|
4573 | { |
---|
4574 | const char *typestr; |
---|
4575 | |
---|
4576 | if (type == Q_NET) |
---|
4577 | typestr = "net"; |
---|
4578 | else |
---|
4579 | typestr = "host"; |
---|
4580 | |
---|
4581 | switch (proto) { |
---|
4582 | |
---|
4583 | case Q_DEFAULT: |
---|
4584 | return gen_host6(addr, mask, Q_IPV6, dir, type); |
---|
4585 | |
---|
4586 | case Q_IP: |
---|
4587 | bpf_error("'ip' modifier applied to ip6 %s", typestr); |
---|
4588 | |
---|
4589 | case Q_RARP: |
---|
4590 | bpf_error("'rarp' modifier applied to ip6 %s", typestr); |
---|
4591 | |
---|
4592 | case Q_ARP: |
---|
4593 | bpf_error("'arp' modifier applied to ip6 %s", typestr); |
---|
4594 | |
---|
4595 | case Q_SCTP: |
---|
4596 | bpf_error("'sctp' modifier applied to %s", typestr); |
---|
4597 | |
---|
4598 | case Q_TCP: |
---|
4599 | bpf_error("'tcp' modifier applied to %s", typestr); |
---|
4600 | |
---|
4601 | case Q_UDP: |
---|
4602 | bpf_error("'udp' modifier applied to %s", typestr); |
---|
4603 | |
---|
4604 | case Q_ICMP: |
---|
4605 | bpf_error("'icmp' modifier applied to %s", typestr); |
---|
4606 | |
---|
4607 | case Q_IGMP: |
---|
4608 | bpf_error("'igmp' modifier applied to %s", typestr); |
---|
4609 | |
---|
4610 | case Q_IGRP: |
---|
4611 | bpf_error("'igrp' modifier applied to %s", typestr); |
---|
4612 | |
---|
4613 | case Q_PIM: |
---|
4614 | bpf_error("'pim' modifier applied to %s", typestr); |
---|
4615 | |
---|
4616 | case Q_VRRP: |
---|
4617 | bpf_error("'vrrp' modifier applied to %s", typestr); |
---|
4618 | |
---|
4619 | case Q_CARP: |
---|
4620 | bpf_error("'carp' modifier applied to %s", typestr); |
---|
4621 | |
---|
4622 | case Q_ATALK: |
---|
4623 | bpf_error("ATALK host filtering not implemented"); |
---|
4624 | |
---|
4625 | case Q_AARP: |
---|
4626 | bpf_error("AARP host filtering not implemented"); |
---|
4627 | |
---|
4628 | case Q_DECNET: |
---|
4629 | bpf_error("'decnet' modifier applied to ip6 %s", typestr); |
---|
4630 | |
---|
4631 | case Q_SCA: |
---|
4632 | bpf_error("SCA host filtering not implemented"); |
---|
4633 | |
---|
4634 | case Q_LAT: |
---|
4635 | bpf_error("LAT host filtering not implemented"); |
---|
4636 | |
---|
4637 | case Q_MOPDL: |
---|
4638 | bpf_error("MOPDL host filtering not implemented"); |
---|
4639 | |
---|
4640 | case Q_MOPRC: |
---|
4641 | bpf_error("MOPRC host filtering not implemented"); |
---|
4642 | |
---|
4643 | case Q_IPV6: |
---|
4644 | return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24); |
---|
4645 | |
---|
4646 | case Q_ICMPV6: |
---|
4647 | bpf_error("'icmp6' modifier applied to %s", typestr); |
---|
4648 | |
---|
4649 | case Q_AH: |
---|
4650 | bpf_error("'ah' modifier applied to %s", typestr); |
---|
4651 | |
---|
4652 | case Q_ESP: |
---|
4653 | bpf_error("'esp' modifier applied to %s", typestr); |
---|
4654 | |
---|
4655 | case Q_ISO: |
---|
4656 | bpf_error("ISO host filtering not implemented"); |
---|
4657 | |
---|
4658 | case Q_ESIS: |
---|
4659 | bpf_error("'esis' modifier applied to %s", typestr); |
---|
4660 | |
---|
4661 | case Q_ISIS: |
---|
4662 | bpf_error("'isis' modifier applied to %s", typestr); |
---|
4663 | |
---|
4664 | case Q_CLNP: |
---|
4665 | bpf_error("'clnp' modifier applied to %s", typestr); |
---|
4666 | |
---|
4667 | case Q_STP: |
---|
4668 | bpf_error("'stp' modifier applied to %s", typestr); |
---|
4669 | |
---|
4670 | case Q_IPX: |
---|
4671 | bpf_error("IPX host filtering not implemented"); |
---|
4672 | |
---|
4673 | case Q_NETBEUI: |
---|
4674 | bpf_error("'netbeui' modifier applied to %s", typestr); |
---|
4675 | |
---|
4676 | case Q_RADIO: |
---|
4677 | bpf_error("'radio' modifier applied to %s", typestr); |
---|
4678 | |
---|
4679 | default: |
---|
4680 | abort(); |
---|
4681 | } |
---|
4682 | /* NOTREACHED */ |
---|
4683 | } |
---|
4684 | #endif |
---|
4685 | |
---|
4686 | #ifndef INET6 |
---|
4687 | static struct block * |
---|
4688 | gen_gateway(eaddr, alist, proto, dir) |
---|
4689 | const u_char *eaddr; |
---|
4690 | bpf_u_int32 **alist; |
---|
4691 | int proto; |
---|
4692 | int dir; |
---|
4693 | { |
---|
4694 | struct block *b0, *b1, *tmp; |
---|
4695 | |
---|
4696 | if (dir != 0) |
---|
4697 | bpf_error("direction applied to 'gateway'"); |
---|
4698 | |
---|
4699 | switch (proto) { |
---|
4700 | case Q_DEFAULT: |
---|
4701 | case Q_IP: |
---|
4702 | case Q_ARP: |
---|
4703 | case Q_RARP: |
---|
4704 | switch (linktype) { |
---|
4705 | case DLT_EN10MB: |
---|
4706 | case DLT_NETANALYZER: |
---|
4707 | case DLT_NETANALYZER_TRANSPARENT: |
---|
4708 | b0 = gen_ehostop(eaddr, Q_OR); |
---|
4709 | break; |
---|
4710 | case DLT_FDDI: |
---|
4711 | b0 = gen_fhostop(eaddr, Q_OR); |
---|
4712 | break; |
---|
4713 | case DLT_IEEE802: |
---|
4714 | b0 = gen_thostop(eaddr, Q_OR); |
---|
4715 | break; |
---|
4716 | case DLT_IEEE802_11: |
---|
4717 | case DLT_PRISM_HEADER: |
---|
4718 | case DLT_IEEE802_11_RADIO_AVS: |
---|
4719 | case DLT_IEEE802_11_RADIO: |
---|
4720 | case DLT_PPI: |
---|
4721 | b0 = gen_wlanhostop(eaddr, Q_OR); |
---|
4722 | break; |
---|
4723 | case DLT_SUNATM: |
---|
4724 | if (!is_lane) |
---|
4725 | bpf_error( |
---|
4726 | "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
---|
4727 | /* |
---|
4728 | * Check that the packet doesn't begin with an |
---|
4729 | * LE Control marker. (We've already generated |
---|
4730 | * a test for LANE.) |
---|
4731 | */ |
---|
4732 | b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, |
---|
4733 | BPF_H, 0xFF00); |
---|
4734 | gen_not(b1); |
---|
4735 | |
---|
4736 | /* |
---|
4737 | * Now check the MAC address. |
---|
4738 | */ |
---|
4739 | b0 = gen_ehostop(eaddr, Q_OR); |
---|
4740 | gen_and(b1, b0); |
---|
4741 | break; |
---|
4742 | case DLT_IP_OVER_FC: |
---|
4743 | b0 = gen_ipfchostop(eaddr, Q_OR); |
---|
4744 | break; |
---|
4745 | default: |
---|
4746 | bpf_error( |
---|
4747 | "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
---|
4748 | } |
---|
4749 | b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST); |
---|
4750 | while (*alist) { |
---|
4751 | tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR, |
---|
4752 | Q_HOST); |
---|
4753 | gen_or(b1, tmp); |
---|
4754 | b1 = tmp; |
---|
4755 | } |
---|
4756 | gen_not(b1); |
---|
4757 | gen_and(b0, b1); |
---|
4758 | return b1; |
---|
4759 | } |
---|
4760 | bpf_error("illegal modifier of 'gateway'"); |
---|
4761 | /* NOTREACHED */ |
---|
4762 | } |
---|
4763 | #endif |
---|
4764 | |
---|
4765 | struct block * |
---|
4766 | gen_proto_abbrev(proto) |
---|
4767 | int proto; |
---|
4768 | { |
---|
4769 | struct block *b0; |
---|
4770 | struct block *b1; |
---|
4771 | |
---|
4772 | switch (proto) { |
---|
4773 | |
---|
4774 | case Q_SCTP: |
---|
4775 | b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT); |
---|
4776 | b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT); |
---|
4777 | gen_or(b0, b1); |
---|
4778 | break; |
---|
4779 | |
---|
4780 | case Q_TCP: |
---|
4781 | b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT); |
---|
4782 | b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT); |
---|
4783 | gen_or(b0, b1); |
---|
4784 | break; |
---|
4785 | |
---|
4786 | case Q_UDP: |
---|
4787 | b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT); |
---|
4788 | b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT); |
---|
4789 | gen_or(b0, b1); |
---|
4790 | break; |
---|
4791 | |
---|
4792 | case Q_ICMP: |
---|
4793 | b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT); |
---|
4794 | break; |
---|
4795 | |
---|
4796 | #ifndef IPPROTO_IGMP |
---|
4797 | #define IPPROTO_IGMP 2 |
---|
4798 | #endif |
---|
4799 | |
---|
4800 | case Q_IGMP: |
---|
4801 | b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT); |
---|
4802 | break; |
---|
4803 | |
---|
4804 | #ifndef IPPROTO_IGRP |
---|
4805 | #define IPPROTO_IGRP 9 |
---|
4806 | #endif |
---|
4807 | case Q_IGRP: |
---|
4808 | b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT); |
---|
4809 | break; |
---|
4810 | |
---|
4811 | #ifndef IPPROTO_PIM |
---|
4812 | #define IPPROTO_PIM 103 |
---|
4813 | #endif |
---|
4814 | |
---|
4815 | case Q_PIM: |
---|
4816 | b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT); |
---|
4817 | b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT); |
---|
4818 | gen_or(b0, b1); |
---|
4819 | break; |
---|
4820 | |
---|
4821 | #ifndef IPPROTO_VRRP |
---|
4822 | #define IPPROTO_VRRP 112 |
---|
4823 | #endif |
---|
4824 | |
---|
4825 | case Q_VRRP: |
---|
4826 | b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT); |
---|
4827 | break; |
---|
4828 | |
---|
4829 | #ifndef IPPROTO_CARP |
---|
4830 | #define IPPROTO_CARP 112 |
---|
4831 | #endif |
---|
4832 | |
---|
4833 | case Q_CARP: |
---|
4834 | b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT); |
---|
4835 | break; |
---|
4836 | |
---|
4837 | case Q_IP: |
---|
4838 | b1 = gen_linktype(ETHERTYPE_IP); |
---|
4839 | break; |
---|
4840 | |
---|
4841 | case Q_ARP: |
---|
4842 | b1 = gen_linktype(ETHERTYPE_ARP); |
---|
4843 | break; |
---|
4844 | |
---|
4845 | case Q_RARP: |
---|
4846 | b1 = gen_linktype(ETHERTYPE_REVARP); |
---|
4847 | break; |
---|
4848 | |
---|
4849 | case Q_LINK: |
---|
4850 | bpf_error("link layer applied in wrong context"); |
---|
4851 | |
---|
4852 | case Q_ATALK: |
---|
4853 | b1 = gen_linktype(ETHERTYPE_ATALK); |
---|
4854 | break; |
---|
4855 | |
---|
4856 | case Q_AARP: |
---|
4857 | b1 = gen_linktype(ETHERTYPE_AARP); |
---|
4858 | break; |
---|
4859 | |
---|
4860 | case Q_DECNET: |
---|
4861 | b1 = gen_linktype(ETHERTYPE_DN); |
---|
4862 | break; |
---|
4863 | |
---|
4864 | case Q_SCA: |
---|
4865 | b1 = gen_linktype(ETHERTYPE_SCA); |
---|
4866 | break; |
---|
4867 | |
---|
4868 | case Q_LAT: |
---|
4869 | b1 = gen_linktype(ETHERTYPE_LAT); |
---|
4870 | break; |
---|
4871 | |
---|
4872 | case Q_MOPDL: |
---|
4873 | b1 = gen_linktype(ETHERTYPE_MOPDL); |
---|
4874 | break; |
---|
4875 | |
---|
4876 | case Q_MOPRC: |
---|
4877 | b1 = gen_linktype(ETHERTYPE_MOPRC); |
---|
4878 | break; |
---|
4879 | |
---|
4880 | case Q_IPV6: |
---|
4881 | b1 = gen_linktype(ETHERTYPE_IPV6); |
---|
4882 | break; |
---|
4883 | |
---|
4884 | #ifndef IPPROTO_ICMPV6 |
---|
4885 | #define IPPROTO_ICMPV6 58 |
---|
4886 | #endif |
---|
4887 | case Q_ICMPV6: |
---|
4888 | b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT); |
---|
4889 | break; |
---|
4890 | |
---|
4891 | #ifndef IPPROTO_AH |
---|
4892 | #define IPPROTO_AH 51 |
---|
4893 | #endif |
---|
4894 | case Q_AH: |
---|
4895 | b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT); |
---|
4896 | b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT); |
---|
4897 | gen_or(b0, b1); |
---|
4898 | break; |
---|
4899 | |
---|
4900 | #ifndef IPPROTO_ESP |
---|
4901 | #define IPPROTO_ESP 50 |
---|
4902 | #endif |
---|
4903 | case Q_ESP: |
---|
4904 | b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT); |
---|
4905 | b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT); |
---|
4906 | gen_or(b0, b1); |
---|
4907 | break; |
---|
4908 | |
---|
4909 | case Q_ISO: |
---|
4910 | b1 = gen_linktype(LLCSAP_ISONS); |
---|
4911 | break; |
---|
4912 | |
---|
4913 | case Q_ESIS: |
---|
4914 | b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT); |
---|
4915 | break; |
---|
4916 | |
---|
4917 | case Q_ISIS: |
---|
4918 | b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT); |
---|
4919 | break; |
---|
4920 | |
---|
4921 | case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */ |
---|
4922 | b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); |
---|
4923 | b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ |
---|
4924 | gen_or(b0, b1); |
---|
4925 | b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); |
---|
4926 | gen_or(b0, b1); |
---|
4927 | b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4928 | gen_or(b0, b1); |
---|
4929 | b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4930 | gen_or(b0, b1); |
---|
4931 | break; |
---|
4932 | |
---|
4933 | case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */ |
---|
4934 | b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); |
---|
4935 | b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ |
---|
4936 | gen_or(b0, b1); |
---|
4937 | b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); |
---|
4938 | gen_or(b0, b1); |
---|
4939 | b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4940 | gen_or(b0, b1); |
---|
4941 | b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4942 | gen_or(b0, b1); |
---|
4943 | break; |
---|
4944 | |
---|
4945 | case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */ |
---|
4946 | b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); |
---|
4947 | b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); |
---|
4948 | gen_or(b0, b1); |
---|
4949 | b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); |
---|
4950 | gen_or(b0, b1); |
---|
4951 | break; |
---|
4952 | |
---|
4953 | case Q_ISIS_LSP: |
---|
4954 | b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); |
---|
4955 | b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); |
---|
4956 | gen_or(b0, b1); |
---|
4957 | break; |
---|
4958 | |
---|
4959 | case Q_ISIS_SNP: |
---|
4960 | b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4961 | b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4962 | gen_or(b0, b1); |
---|
4963 | b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4964 | gen_or(b0, b1); |
---|
4965 | b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4966 | gen_or(b0, b1); |
---|
4967 | break; |
---|
4968 | |
---|
4969 | case Q_ISIS_CSNP: |
---|
4970 | b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4971 | b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
---|
4972 | gen_or(b0, b1); |
---|
4973 | break; |
---|
4974 | |
---|
4975 | case Q_ISIS_PSNP: |
---|
4976 | b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4977 | b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
---|
4978 | gen_or(b0, b1); |
---|
4979 | break; |
---|
4980 | |
---|
4981 | case Q_CLNP: |
---|
4982 | b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT); |
---|
4983 | break; |
---|
4984 | |
---|
4985 | case Q_STP: |
---|
4986 | b1 = gen_linktype(LLCSAP_8021D); |
---|
4987 | break; |
---|
4988 | |
---|
4989 | case Q_IPX: |
---|
4990 | b1 = gen_linktype(LLCSAP_IPX); |
---|
4991 | break; |
---|
4992 | |
---|
4993 | case Q_NETBEUI: |
---|
4994 | b1 = gen_linktype(LLCSAP_NETBEUI); |
---|
4995 | break; |
---|
4996 | |
---|
4997 | case Q_RADIO: |
---|
4998 | bpf_error("'radio' is not a valid protocol type"); |
---|
4999 | |
---|
5000 | default: |
---|
5001 | abort(); |
---|
5002 | } |
---|
5003 | return b1; |
---|
5004 | } |
---|
5005 | |
---|
5006 | static struct block * |
---|
5007 | gen_ipfrag() |
---|
5008 | { |
---|
5009 | struct slist *s; |
---|
5010 | struct block *b; |
---|
5011 | |
---|
5012 | /* not IPv4 frag other than the first frag */ |
---|
5013 | s = gen_load_a(OR_NET, 6, BPF_H); |
---|
5014 | b = new_block(JMP(BPF_JSET)); |
---|
5015 | b->s.k = 0x1fff; |
---|
5016 | b->stmts = s; |
---|
5017 | gen_not(b); |
---|
5018 | |
---|
5019 | return b; |
---|
5020 | } |
---|
5021 | |
---|
5022 | /* |
---|
5023 | * Generate a comparison to a port value in the transport-layer header |
---|
5024 | * at the specified offset from the beginning of that header. |
---|
5025 | * |
---|
5026 | * XXX - this handles a variable-length prefix preceding the link-layer |
---|
5027 | * header, such as the radiotap or AVS radio prefix, but doesn't handle |
---|
5028 | * variable-length link-layer headers (such as Token Ring or 802.11 |
---|
5029 | * headers). |
---|
5030 | */ |
---|
5031 | static struct block * |
---|
5032 | gen_portatom(off, v) |
---|
5033 | int off; |
---|
5034 | bpf_int32 v; |
---|
5035 | { |
---|
5036 | return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v); |
---|
5037 | } |
---|
5038 | |
---|
5039 | static struct block * |
---|
5040 | gen_portatom6(off, v) |
---|
5041 | int off; |
---|
5042 | bpf_int32 v; |
---|
5043 | { |
---|
5044 | return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v); |
---|
5045 | } |
---|
5046 | |
---|
5047 | struct block * |
---|
5048 | gen_portop(port, proto, dir) |
---|
5049 | int port, proto, dir; |
---|
5050 | { |
---|
5051 | struct block *b0, *b1, *tmp; |
---|
5052 | |
---|
5053 | /* ip proto 'proto' and not a fragment other than the first fragment */ |
---|
5054 | tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto); |
---|
5055 | b0 = gen_ipfrag(); |
---|
5056 | gen_and(tmp, b0); |
---|
5057 | |
---|
5058 | switch (dir) { |
---|
5059 | case Q_SRC: |
---|
5060 | b1 = gen_portatom(0, (bpf_int32)port); |
---|
5061 | break; |
---|
5062 | |
---|
5063 | case Q_DST: |
---|
5064 | b1 = gen_portatom(2, (bpf_int32)port); |
---|
5065 | break; |
---|
5066 | |
---|
5067 | case Q_OR: |
---|
5068 | case Q_DEFAULT: |
---|
5069 | tmp = gen_portatom(0, (bpf_int32)port); |
---|
5070 | b1 = gen_portatom(2, (bpf_int32)port); |
---|
5071 | gen_or(tmp, b1); |
---|
5072 | break; |
---|
5073 | |
---|
5074 | case Q_AND: |
---|
5075 | tmp = gen_portatom(0, (bpf_int32)port); |
---|
5076 | b1 = gen_portatom(2, (bpf_int32)port); |
---|
5077 | gen_and(tmp, b1); |
---|
5078 | break; |
---|
5079 | |
---|
5080 | default: |
---|
5081 | abort(); |
---|
5082 | } |
---|
5083 | gen_and(b0, b1); |
---|
5084 | |
---|
5085 | return b1; |
---|
5086 | } |
---|
5087 | |
---|
5088 | static struct block * |
---|
5089 | gen_port(port, ip_proto, dir) |
---|
5090 | int port; |
---|
5091 | int ip_proto; |
---|
5092 | int dir; |
---|
5093 | { |
---|
5094 | struct block *b0, *b1, *tmp; |
---|
5095 | |
---|
5096 | /* |
---|
5097 | * ether proto ip |
---|
5098 | * |
---|
5099 | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
---|
5100 | * not LLC encapsulation with LLCSAP_IP. |
---|
5101 | * |
---|
5102 | * For IEEE 802 networks - which includes 802.5 token ring |
---|
5103 | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
---|
5104 | * says that SNAP encapsulation is used, not LLC encapsulation |
---|
5105 | * with LLCSAP_IP. |
---|
5106 | * |
---|
5107 | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
---|
5108 | * RFC 2225 say that SNAP encapsulation is used, not LLC |
---|
5109 | * encapsulation with LLCSAP_IP. |
---|
5110 | * |
---|
5111 | * So we always check for ETHERTYPE_IP. |
---|
5112 | */ |
---|
5113 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
5114 | |
---|
5115 | switch (ip_proto) { |
---|
5116 | case IPPROTO_UDP: |
---|
5117 | case IPPROTO_TCP: |
---|
5118 | case IPPROTO_SCTP: |
---|
5119 | b1 = gen_portop(port, ip_proto, dir); |
---|
5120 | break; |
---|
5121 | |
---|
5122 | case PROTO_UNDEF: |
---|
5123 | tmp = gen_portop(port, IPPROTO_TCP, dir); |
---|
5124 | b1 = gen_portop(port, IPPROTO_UDP, dir); |
---|
5125 | gen_or(tmp, b1); |
---|
5126 | tmp = gen_portop(port, IPPROTO_SCTP, dir); |
---|
5127 | gen_or(tmp, b1); |
---|
5128 | break; |
---|
5129 | |
---|
5130 | default: |
---|
5131 | abort(); |
---|
5132 | } |
---|
5133 | gen_and(b0, b1); |
---|
5134 | return b1; |
---|
5135 | } |
---|
5136 | |
---|
5137 | struct block * |
---|
5138 | gen_portop6(port, proto, dir) |
---|
5139 | int port, proto, dir; |
---|
5140 | { |
---|
5141 | struct block *b0, *b1, *tmp; |
---|
5142 | |
---|
5143 | /* ip6 proto 'proto' */ |
---|
5144 | /* XXX - catch the first fragment of a fragmented packet? */ |
---|
5145 | b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto); |
---|
5146 | |
---|
5147 | switch (dir) { |
---|
5148 | case Q_SRC: |
---|
5149 | b1 = gen_portatom6(0, (bpf_int32)port); |
---|
5150 | break; |
---|
5151 | |
---|
5152 | case Q_DST: |
---|
5153 | b1 = gen_portatom6(2, (bpf_int32)port); |
---|
5154 | break; |
---|
5155 | |
---|
5156 | case Q_OR: |
---|
5157 | case Q_DEFAULT: |
---|
5158 | tmp = gen_portatom6(0, (bpf_int32)port); |
---|
5159 | b1 = gen_portatom6(2, (bpf_int32)port); |
---|
5160 | gen_or(tmp, b1); |
---|
5161 | break; |
---|
5162 | |
---|
5163 | case Q_AND: |
---|
5164 | tmp = gen_portatom6(0, (bpf_int32)port); |
---|
5165 | b1 = gen_portatom6(2, (bpf_int32)port); |
---|
5166 | gen_and(tmp, b1); |
---|
5167 | break; |
---|
5168 | |
---|
5169 | default: |
---|
5170 | abort(); |
---|
5171 | } |
---|
5172 | gen_and(b0, b1); |
---|
5173 | |
---|
5174 | return b1; |
---|
5175 | } |
---|
5176 | |
---|
5177 | static struct block * |
---|
5178 | gen_port6(port, ip_proto, dir) |
---|
5179 | int port; |
---|
5180 | int ip_proto; |
---|
5181 | int dir; |
---|
5182 | { |
---|
5183 | struct block *b0, *b1, *tmp; |
---|
5184 | |
---|
5185 | /* link proto ip6 */ |
---|
5186 | b0 = gen_linktype(ETHERTYPE_IPV6); |
---|
5187 | |
---|
5188 | switch (ip_proto) { |
---|
5189 | case IPPROTO_UDP: |
---|
5190 | case IPPROTO_TCP: |
---|
5191 | case IPPROTO_SCTP: |
---|
5192 | b1 = gen_portop6(port, ip_proto, dir); |
---|
5193 | break; |
---|
5194 | |
---|
5195 | case PROTO_UNDEF: |
---|
5196 | tmp = gen_portop6(port, IPPROTO_TCP, dir); |
---|
5197 | b1 = gen_portop6(port, IPPROTO_UDP, dir); |
---|
5198 | gen_or(tmp, b1); |
---|
5199 | tmp = gen_portop6(port, IPPROTO_SCTP, dir); |
---|
5200 | gen_or(tmp, b1); |
---|
5201 | break; |
---|
5202 | |
---|
5203 | default: |
---|
5204 | abort(); |
---|
5205 | } |
---|
5206 | gen_and(b0, b1); |
---|
5207 | return b1; |
---|
5208 | } |
---|
5209 | |
---|
5210 | /* gen_portrange code */ |
---|
5211 | static struct block * |
---|
5212 | gen_portrangeatom(off, v1, v2) |
---|
5213 | int off; |
---|
5214 | bpf_int32 v1, v2; |
---|
5215 | { |
---|
5216 | struct block *b1, *b2; |
---|
5217 | |
---|
5218 | if (v1 > v2) { |
---|
5219 | /* |
---|
5220 | * Reverse the order of the ports, so v1 is the lower one. |
---|
5221 | */ |
---|
5222 | bpf_int32 vtemp; |
---|
5223 | |
---|
5224 | vtemp = v1; |
---|
5225 | v1 = v2; |
---|
5226 | v2 = vtemp; |
---|
5227 | } |
---|
5228 | |
---|
5229 | b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1); |
---|
5230 | b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2); |
---|
5231 | |
---|
5232 | gen_and(b1, b2); |
---|
5233 | |
---|
5234 | return b2; |
---|
5235 | } |
---|
5236 | |
---|
5237 | struct block * |
---|
5238 | gen_portrangeop(port1, port2, proto, dir) |
---|
5239 | int port1, port2; |
---|
5240 | int proto; |
---|
5241 | int dir; |
---|
5242 | { |
---|
5243 | struct block *b0, *b1, *tmp; |
---|
5244 | |
---|
5245 | /* ip proto 'proto' and not a fragment other than the first fragment */ |
---|
5246 | tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto); |
---|
5247 | b0 = gen_ipfrag(); |
---|
5248 | gen_and(tmp, b0); |
---|
5249 | |
---|
5250 | switch (dir) { |
---|
5251 | case Q_SRC: |
---|
5252 | b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5253 | break; |
---|
5254 | |
---|
5255 | case Q_DST: |
---|
5256 | b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5257 | break; |
---|
5258 | |
---|
5259 | case Q_OR: |
---|
5260 | case Q_DEFAULT: |
---|
5261 | tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5262 | b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5263 | gen_or(tmp, b1); |
---|
5264 | break; |
---|
5265 | |
---|
5266 | case Q_AND: |
---|
5267 | tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5268 | b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5269 | gen_and(tmp, b1); |
---|
5270 | break; |
---|
5271 | |
---|
5272 | default: |
---|
5273 | abort(); |
---|
5274 | } |
---|
5275 | gen_and(b0, b1); |
---|
5276 | |
---|
5277 | return b1; |
---|
5278 | } |
---|
5279 | |
---|
5280 | static struct block * |
---|
5281 | gen_portrange(port1, port2, ip_proto, dir) |
---|
5282 | int port1, port2; |
---|
5283 | int ip_proto; |
---|
5284 | int dir; |
---|
5285 | { |
---|
5286 | struct block *b0, *b1, *tmp; |
---|
5287 | |
---|
5288 | /* link proto ip */ |
---|
5289 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
5290 | |
---|
5291 | switch (ip_proto) { |
---|
5292 | case IPPROTO_UDP: |
---|
5293 | case IPPROTO_TCP: |
---|
5294 | case IPPROTO_SCTP: |
---|
5295 | b1 = gen_portrangeop(port1, port2, ip_proto, dir); |
---|
5296 | break; |
---|
5297 | |
---|
5298 | case PROTO_UNDEF: |
---|
5299 | tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir); |
---|
5300 | b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir); |
---|
5301 | gen_or(tmp, b1); |
---|
5302 | tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir); |
---|
5303 | gen_or(tmp, b1); |
---|
5304 | break; |
---|
5305 | |
---|
5306 | default: |
---|
5307 | abort(); |
---|
5308 | } |
---|
5309 | gen_and(b0, b1); |
---|
5310 | return b1; |
---|
5311 | } |
---|
5312 | |
---|
5313 | static struct block * |
---|
5314 | gen_portrangeatom6(off, v1, v2) |
---|
5315 | int off; |
---|
5316 | bpf_int32 v1, v2; |
---|
5317 | { |
---|
5318 | struct block *b1, *b2; |
---|
5319 | |
---|
5320 | if (v1 > v2) { |
---|
5321 | /* |
---|
5322 | * Reverse the order of the ports, so v1 is the lower one. |
---|
5323 | */ |
---|
5324 | bpf_int32 vtemp; |
---|
5325 | |
---|
5326 | vtemp = v1; |
---|
5327 | v1 = v2; |
---|
5328 | v2 = vtemp; |
---|
5329 | } |
---|
5330 | |
---|
5331 | b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1); |
---|
5332 | b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2); |
---|
5333 | |
---|
5334 | gen_and(b1, b2); |
---|
5335 | |
---|
5336 | return b2; |
---|
5337 | } |
---|
5338 | |
---|
5339 | struct block * |
---|
5340 | gen_portrangeop6(port1, port2, proto, dir) |
---|
5341 | int port1, port2; |
---|
5342 | int proto; |
---|
5343 | int dir; |
---|
5344 | { |
---|
5345 | struct block *b0, *b1, *tmp; |
---|
5346 | |
---|
5347 | /* ip6 proto 'proto' */ |
---|
5348 | /* XXX - catch the first fragment of a fragmented packet? */ |
---|
5349 | b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto); |
---|
5350 | |
---|
5351 | switch (dir) { |
---|
5352 | case Q_SRC: |
---|
5353 | b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5354 | break; |
---|
5355 | |
---|
5356 | case Q_DST: |
---|
5357 | b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5358 | break; |
---|
5359 | |
---|
5360 | case Q_OR: |
---|
5361 | case Q_DEFAULT: |
---|
5362 | tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5363 | b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5364 | gen_or(tmp, b1); |
---|
5365 | break; |
---|
5366 | |
---|
5367 | case Q_AND: |
---|
5368 | tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2); |
---|
5369 | b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2); |
---|
5370 | gen_and(tmp, b1); |
---|
5371 | break; |
---|
5372 | |
---|
5373 | default: |
---|
5374 | abort(); |
---|
5375 | } |
---|
5376 | gen_and(b0, b1); |
---|
5377 | |
---|
5378 | return b1; |
---|
5379 | } |
---|
5380 | |
---|
5381 | static struct block * |
---|
5382 | gen_portrange6(port1, port2, ip_proto, dir) |
---|
5383 | int port1, port2; |
---|
5384 | int ip_proto; |
---|
5385 | int dir; |
---|
5386 | { |
---|
5387 | struct block *b0, *b1, *tmp; |
---|
5388 | |
---|
5389 | /* link proto ip6 */ |
---|
5390 | b0 = gen_linktype(ETHERTYPE_IPV6); |
---|
5391 | |
---|
5392 | switch (ip_proto) { |
---|
5393 | case IPPROTO_UDP: |
---|
5394 | case IPPROTO_TCP: |
---|
5395 | case IPPROTO_SCTP: |
---|
5396 | b1 = gen_portrangeop6(port1, port2, ip_proto, dir); |
---|
5397 | break; |
---|
5398 | |
---|
5399 | case PROTO_UNDEF: |
---|
5400 | tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir); |
---|
5401 | b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir); |
---|
5402 | gen_or(tmp, b1); |
---|
5403 | tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir); |
---|
5404 | gen_or(tmp, b1); |
---|
5405 | break; |
---|
5406 | |
---|
5407 | default: |
---|
5408 | abort(); |
---|
5409 | } |
---|
5410 | gen_and(b0, b1); |
---|
5411 | return b1; |
---|
5412 | } |
---|
5413 | |
---|
5414 | static int |
---|
5415 | lookup_proto(name, proto) |
---|
5416 | register const char *name; |
---|
5417 | register int proto; |
---|
5418 | { |
---|
5419 | register int v; |
---|
5420 | |
---|
5421 | switch (proto) { |
---|
5422 | |
---|
5423 | case Q_DEFAULT: |
---|
5424 | case Q_IP: |
---|
5425 | case Q_IPV6: |
---|
5426 | v = pcap_nametoproto(name); |
---|
5427 | if (v == PROTO_UNDEF) |
---|
5428 | bpf_error("unknown ip proto '%s'", name); |
---|
5429 | break; |
---|
5430 | |
---|
5431 | case Q_LINK: |
---|
5432 | /* XXX should look up h/w protocol type based on linktype */ |
---|
5433 | v = pcap_nametoeproto(name); |
---|
5434 | if (v == PROTO_UNDEF) { |
---|
5435 | v = pcap_nametollc(name); |
---|
5436 | if (v == PROTO_UNDEF) |
---|
5437 | bpf_error("unknown ether proto '%s'", name); |
---|
5438 | } |
---|
5439 | break; |
---|
5440 | |
---|
5441 | case Q_ISO: |
---|
5442 | if (strcmp(name, "esis") == 0) |
---|
5443 | v = ISO9542_ESIS; |
---|
5444 | else if (strcmp(name, "isis") == 0) |
---|
5445 | v = ISO10589_ISIS; |
---|
5446 | else if (strcmp(name, "clnp") == 0) |
---|
5447 | v = ISO8473_CLNP; |
---|
5448 | else |
---|
5449 | bpf_error("unknown osi proto '%s'", name); |
---|
5450 | break; |
---|
5451 | |
---|
5452 | default: |
---|
5453 | v = PROTO_UNDEF; |
---|
5454 | break; |
---|
5455 | } |
---|
5456 | return v; |
---|
5457 | } |
---|
5458 | |
---|
5459 | #if 0 |
---|
5460 | struct stmt * |
---|
5461 | gen_joinsp(s, n) |
---|
5462 | struct stmt **s; |
---|
5463 | int n; |
---|
5464 | { |
---|
5465 | return NULL; |
---|
5466 | } |
---|
5467 | #endif |
---|
5468 | |
---|
5469 | static struct block * |
---|
5470 | gen_protochain(v, proto, dir) |
---|
5471 | int v; |
---|
5472 | int proto; |
---|
5473 | int dir; |
---|
5474 | { |
---|
5475 | #ifdef NO_PROTOCHAIN |
---|
5476 | return gen_proto(v, proto, dir); |
---|
5477 | #else |
---|
5478 | struct block *b0, *b; |
---|
5479 | struct slist *s[100]; |
---|
5480 | int fix2, fix3, fix4, fix5; |
---|
5481 | int ahcheck, again, end; |
---|
5482 | int i, max; |
---|
5483 | int reg2 = alloc_reg(); |
---|
5484 | |
---|
5485 | memset(s, 0, sizeof(s)); |
---|
5486 | fix2 = fix3 = fix4 = fix5 = 0; |
---|
5487 | |
---|
5488 | switch (proto) { |
---|
5489 | case Q_IP: |
---|
5490 | case Q_IPV6: |
---|
5491 | break; |
---|
5492 | case Q_DEFAULT: |
---|
5493 | b0 = gen_protochain(v, Q_IP, dir); |
---|
5494 | b = gen_protochain(v, Q_IPV6, dir); |
---|
5495 | gen_or(b0, b); |
---|
5496 | return b; |
---|
5497 | default: |
---|
5498 | bpf_error("bad protocol applied for 'protochain'"); |
---|
5499 | /*NOTREACHED*/ |
---|
5500 | } |
---|
5501 | |
---|
5502 | /* |
---|
5503 | * We don't handle variable-length prefixes before the link-layer |
---|
5504 | * header, or variable-length link-layer headers, here yet. |
---|
5505 | * We might want to add BPF instructions to do the protochain |
---|
5506 | * work, to simplify that and, on platforms that have a BPF |
---|
5507 | * interpreter with the new instructions, let the filtering |
---|
5508 | * be done in the kernel. (We already require a modified BPF |
---|
5509 | * engine to do the protochain stuff, to support backward |
---|
5510 | * branches, and backward branch support is unlikely to appear |
---|
5511 | * in kernel BPF engines.) |
---|
5512 | */ |
---|
5513 | switch (linktype) { |
---|
5514 | |
---|
5515 | case DLT_IEEE802_11: |
---|
5516 | case DLT_PRISM_HEADER: |
---|
5517 | case DLT_IEEE802_11_RADIO_AVS: |
---|
5518 | case DLT_IEEE802_11_RADIO: |
---|
5519 | case DLT_PPI: |
---|
5520 | bpf_error("'protochain' not supported with 802.11"); |
---|
5521 | } |
---|
5522 | |
---|
5523 | no_optimize = 1; /*this code is not compatible with optimzer yet */ |
---|
5524 | |
---|
5525 | /* |
---|
5526 | * s[0] is a dummy entry to protect other BPF insn from damage |
---|
5527 | * by s[fix] = foo with uninitialized variable "fix". It is somewhat |
---|
5528 | * hard to find interdependency made by jump table fixup. |
---|
5529 | */ |
---|
5530 | i = 0; |
---|
5531 | s[i] = new_stmt(0); /*dummy*/ |
---|
5532 | i++; |
---|
5533 | |
---|
5534 | switch (proto) { |
---|
5535 | case Q_IP: |
---|
5536 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
5537 | |
---|
5538 | /* A = ip->ip_p */ |
---|
5539 | s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B); |
---|
5540 | s[i]->s.k = off_macpl + off_nl + 9; |
---|
5541 | i++; |
---|
5542 | /* X = ip->ip_hl << 2 */ |
---|
5543 | s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B); |
---|
5544 | s[i]->s.k = off_macpl + off_nl; |
---|
5545 | i++; |
---|
5546 | break; |
---|
5547 | |
---|
5548 | case Q_IPV6: |
---|
5549 | b0 = gen_linktype(ETHERTYPE_IPV6); |
---|
5550 | |
---|
5551 | /* A = ip6->ip_nxt */ |
---|
5552 | s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B); |
---|
5553 | s[i]->s.k = off_macpl + off_nl + 6; |
---|
5554 | i++; |
---|
5555 | /* X = sizeof(struct ip6_hdr) */ |
---|
5556 | s[i] = new_stmt(BPF_LDX|BPF_IMM); |
---|
5557 | s[i]->s.k = 40; |
---|
5558 | i++; |
---|
5559 | break; |
---|
5560 | |
---|
5561 | default: |
---|
5562 | bpf_error("unsupported proto to gen_protochain"); |
---|
5563 | /*NOTREACHED*/ |
---|
5564 | } |
---|
5565 | |
---|
5566 | /* again: if (A == v) goto end; else fall through; */ |
---|
5567 | again = i; |
---|
5568 | s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5569 | s[i]->s.k = v; |
---|
5570 | s[i]->s.jt = NULL; /*later*/ |
---|
5571 | s[i]->s.jf = NULL; /*update in next stmt*/ |
---|
5572 | fix5 = i; |
---|
5573 | i++; |
---|
5574 | |
---|
5575 | #ifndef IPPROTO_NONE |
---|
5576 | #define IPPROTO_NONE 59 |
---|
5577 | #endif |
---|
5578 | /* if (A == IPPROTO_NONE) goto end */ |
---|
5579 | s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5580 | s[i]->s.jt = NULL; /*later*/ |
---|
5581 | s[i]->s.jf = NULL; /*update in next stmt*/ |
---|
5582 | s[i]->s.k = IPPROTO_NONE; |
---|
5583 | s[fix5]->s.jf = s[i]; |
---|
5584 | fix2 = i; |
---|
5585 | i++; |
---|
5586 | |
---|
5587 | if (proto == Q_IPV6) { |
---|
5588 | int v6start, v6end, v6advance, j; |
---|
5589 | |
---|
5590 | v6start = i; |
---|
5591 | /* if (A == IPPROTO_HOPOPTS) goto v6advance */ |
---|
5592 | s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5593 | s[i]->s.jt = NULL; /*later*/ |
---|
5594 | s[i]->s.jf = NULL; /*update in next stmt*/ |
---|
5595 | s[i]->s.k = IPPROTO_HOPOPTS; |
---|
5596 | s[fix2]->s.jf = s[i]; |
---|
5597 | i++; |
---|
5598 | /* if (A == IPPROTO_DSTOPTS) goto v6advance */ |
---|
5599 | s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5600 | s[i]->s.jt = NULL; /*later*/ |
---|
5601 | s[i]->s.jf = NULL; /*update in next stmt*/ |
---|
5602 | s[i]->s.k = IPPROTO_DSTOPTS; |
---|
5603 | i++; |
---|
5604 | /* if (A == IPPROTO_ROUTING) goto v6advance */ |
---|
5605 | s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5606 | s[i]->s.jt = NULL; /*later*/ |
---|
5607 | s[i]->s.jf = NULL; /*update in next stmt*/ |
---|
5608 | s[i]->s.k = IPPROTO_ROUTING; |
---|
5609 | i++; |
---|
5610 | /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ |
---|
5611 | s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5612 | s[i]->s.jt = NULL; /*later*/ |
---|
5613 | s[i]->s.jf = NULL; /*later*/ |
---|
5614 | s[i]->s.k = IPPROTO_FRAGMENT; |
---|
5615 | fix3 = i; |
---|
5616 | v6end = i; |
---|
5617 | i++; |
---|
5618 | |
---|
5619 | /* v6advance: */ |
---|
5620 | v6advance = i; |
---|
5621 | |
---|
5622 | /* |
---|
5623 | * in short, |
---|
5624 | * A = P[X + packet head]; |
---|
5625 | * X = X + (P[X + packet head + 1] + 1) * 8; |
---|
5626 | */ |
---|
5627 | /* A = P[X + packet head] */ |
---|
5628 | s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
5629 | s[i]->s.k = off_macpl + off_nl; |
---|
5630 | i++; |
---|
5631 | /* MEM[reg2] = A */ |
---|
5632 | s[i] = new_stmt(BPF_ST); |
---|
5633 | s[i]->s.k = reg2; |
---|
5634 | i++; |
---|
5635 | /* A = P[X + packet head + 1]; */ |
---|
5636 | s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
5637 | s[i]->s.k = off_macpl + off_nl + 1; |
---|
5638 | i++; |
---|
5639 | /* A += 1 */ |
---|
5640 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
5641 | s[i]->s.k = 1; |
---|
5642 | i++; |
---|
5643 | /* A *= 8 */ |
---|
5644 | s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K); |
---|
5645 | s[i]->s.k = 8; |
---|
5646 | i++; |
---|
5647 | /* A += X */ |
---|
5648 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X); |
---|
5649 | s[i]->s.k = 0; |
---|
5650 | i++; |
---|
5651 | /* X = A; */ |
---|
5652 | s[i] = new_stmt(BPF_MISC|BPF_TAX); |
---|
5653 | i++; |
---|
5654 | /* A = MEM[reg2] */ |
---|
5655 | s[i] = new_stmt(BPF_LD|BPF_MEM); |
---|
5656 | s[i]->s.k = reg2; |
---|
5657 | i++; |
---|
5658 | |
---|
5659 | /* goto again; (must use BPF_JA for backward jump) */ |
---|
5660 | s[i] = new_stmt(BPF_JMP|BPF_JA); |
---|
5661 | s[i]->s.k = again - i - 1; |
---|
5662 | s[i - 1]->s.jf = s[i]; |
---|
5663 | i++; |
---|
5664 | |
---|
5665 | /* fixup */ |
---|
5666 | for (j = v6start; j <= v6end; j++) |
---|
5667 | s[j]->s.jt = s[v6advance]; |
---|
5668 | } else { |
---|
5669 | /* nop */ |
---|
5670 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
5671 | s[i]->s.k = 0; |
---|
5672 | s[fix2]->s.jf = s[i]; |
---|
5673 | i++; |
---|
5674 | } |
---|
5675 | |
---|
5676 | /* ahcheck: */ |
---|
5677 | ahcheck = i; |
---|
5678 | /* if (A == IPPROTO_AH) then fall through; else goto end; */ |
---|
5679 | s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); |
---|
5680 | s[i]->s.jt = NULL; /*later*/ |
---|
5681 | s[i]->s.jf = NULL; /*later*/ |
---|
5682 | s[i]->s.k = IPPROTO_AH; |
---|
5683 | if (fix3) |
---|
5684 | s[fix3]->s.jf = s[ahcheck]; |
---|
5685 | fix4 = i; |
---|
5686 | i++; |
---|
5687 | |
---|
5688 | /* |
---|
5689 | * in short, |
---|
5690 | * A = P[X]; |
---|
5691 | * X = X + (P[X + 1] + 2) * 4; |
---|
5692 | */ |
---|
5693 | /* A = X */ |
---|
5694 | s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA); |
---|
5695 | i++; |
---|
5696 | /* A = P[X + packet head]; */ |
---|
5697 | s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
5698 | s[i]->s.k = off_macpl + off_nl; |
---|
5699 | i++; |
---|
5700 | /* MEM[reg2] = A */ |
---|
5701 | s[i] = new_stmt(BPF_ST); |
---|
5702 | s[i]->s.k = reg2; |
---|
5703 | i++; |
---|
5704 | /* A = X */ |
---|
5705 | s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA); |
---|
5706 | i++; |
---|
5707 | /* A += 1 */ |
---|
5708 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
5709 | s[i]->s.k = 1; |
---|
5710 | i++; |
---|
5711 | /* X = A */ |
---|
5712 | s[i] = new_stmt(BPF_MISC|BPF_TAX); |
---|
5713 | i++; |
---|
5714 | /* A = P[X + packet head] */ |
---|
5715 | s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); |
---|
5716 | s[i]->s.k = off_macpl + off_nl; |
---|
5717 | i++; |
---|
5718 | /* A += 2 */ |
---|
5719 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
5720 | s[i]->s.k = 2; |
---|
5721 | i++; |
---|
5722 | /* A *= 4 */ |
---|
5723 | s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K); |
---|
5724 | s[i]->s.k = 4; |
---|
5725 | i++; |
---|
5726 | /* X = A; */ |
---|
5727 | s[i] = new_stmt(BPF_MISC|BPF_TAX); |
---|
5728 | i++; |
---|
5729 | /* A = MEM[reg2] */ |
---|
5730 | s[i] = new_stmt(BPF_LD|BPF_MEM); |
---|
5731 | s[i]->s.k = reg2; |
---|
5732 | i++; |
---|
5733 | |
---|
5734 | /* goto again; (must use BPF_JA for backward jump) */ |
---|
5735 | s[i] = new_stmt(BPF_JMP|BPF_JA); |
---|
5736 | s[i]->s.k = again - i - 1; |
---|
5737 | i++; |
---|
5738 | |
---|
5739 | /* end: nop */ |
---|
5740 | end = i; |
---|
5741 | s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); |
---|
5742 | s[i]->s.k = 0; |
---|
5743 | s[fix2]->s.jt = s[end]; |
---|
5744 | s[fix4]->s.jf = s[end]; |
---|
5745 | s[fix5]->s.jt = s[end]; |
---|
5746 | i++; |
---|
5747 | |
---|
5748 | /* |
---|
5749 | * make slist chain |
---|
5750 | */ |
---|
5751 | max = i; |
---|
5752 | for (i = 0; i < max - 1; i++) |
---|
5753 | s[i]->next = s[i + 1]; |
---|
5754 | s[max - 1]->next = NULL; |
---|
5755 | |
---|
5756 | /* |
---|
5757 | * emit final check |
---|
5758 | */ |
---|
5759 | b = new_block(JMP(BPF_JEQ)); |
---|
5760 | b->stmts = s[1]; /*remember, s[0] is dummy*/ |
---|
5761 | b->s.k = v; |
---|
5762 | |
---|
5763 | free_reg(reg2); |
---|
5764 | |
---|
5765 | gen_and(b0, b); |
---|
5766 | return b; |
---|
5767 | #endif |
---|
5768 | } |
---|
5769 | |
---|
5770 | static struct block * |
---|
5771 | gen_check_802_11_data_frame() |
---|
5772 | { |
---|
5773 | struct slist *s; |
---|
5774 | struct block *b0, *b1; |
---|
5775 | |
---|
5776 | /* |
---|
5777 | * A data frame has the 0x08 bit (b3) in the frame control field set |
---|
5778 | * and the 0x04 bit (b2) clear. |
---|
5779 | */ |
---|
5780 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
5781 | b0 = new_block(JMP(BPF_JSET)); |
---|
5782 | b0->s.k = 0x08; |
---|
5783 | b0->stmts = s; |
---|
5784 | |
---|
5785 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
5786 | b1 = new_block(JMP(BPF_JSET)); |
---|
5787 | b1->s.k = 0x04; |
---|
5788 | b1->stmts = s; |
---|
5789 | gen_not(b1); |
---|
5790 | |
---|
5791 | gen_and(b1, b0); |
---|
5792 | |
---|
5793 | return b0; |
---|
5794 | } |
---|
5795 | |
---|
5796 | /* |
---|
5797 | * Generate code that checks whether the packet is a packet for protocol |
---|
5798 | * <proto> and whether the type field in that protocol's header has |
---|
5799 | * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an |
---|
5800 | * IP packet and checks the protocol number in the IP header against <v>. |
---|
5801 | * |
---|
5802 | * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks |
---|
5803 | * against Q_IP and Q_IPV6. |
---|
5804 | */ |
---|
5805 | static struct block * |
---|
5806 | gen_proto(v, proto, dir) |
---|
5807 | int v; |
---|
5808 | int proto; |
---|
5809 | int dir; |
---|
5810 | { |
---|
5811 | struct block *b0, *b1; |
---|
5812 | #ifndef CHASE_CHAIN |
---|
5813 | struct block *b2; |
---|
5814 | #endif |
---|
5815 | |
---|
5816 | if (dir != Q_DEFAULT) |
---|
5817 | bpf_error("direction applied to 'proto'"); |
---|
5818 | |
---|
5819 | switch (proto) { |
---|
5820 | case Q_DEFAULT: |
---|
5821 | b0 = gen_proto(v, Q_IP, dir); |
---|
5822 | b1 = gen_proto(v, Q_IPV6, dir); |
---|
5823 | gen_or(b0, b1); |
---|
5824 | return b1; |
---|
5825 | |
---|
5826 | case Q_IP: |
---|
5827 | /* |
---|
5828 | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
---|
5829 | * not LLC encapsulation with LLCSAP_IP. |
---|
5830 | * |
---|
5831 | * For IEEE 802 networks - which includes 802.5 token ring |
---|
5832 | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
---|
5833 | * says that SNAP encapsulation is used, not LLC encapsulation |
---|
5834 | * with LLCSAP_IP. |
---|
5835 | * |
---|
5836 | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
---|
5837 | * RFC 2225 say that SNAP encapsulation is used, not LLC |
---|
5838 | * encapsulation with LLCSAP_IP. |
---|
5839 | * |
---|
5840 | * So we always check for ETHERTYPE_IP. |
---|
5841 | */ |
---|
5842 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
5843 | #ifndef CHASE_CHAIN |
---|
5844 | b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v); |
---|
5845 | #else |
---|
5846 | b1 = gen_protochain(v, Q_IP); |
---|
5847 | #endif |
---|
5848 | gen_and(b0, b1); |
---|
5849 | return b1; |
---|
5850 | |
---|
5851 | case Q_ISO: |
---|
5852 | switch (linktype) { |
---|
5853 | |
---|
5854 | case DLT_FRELAY: |
---|
5855 | /* |
---|
5856 | * Frame Relay packets typically have an OSI |
---|
5857 | * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)" |
---|
5858 | * generates code to check for all the OSI |
---|
5859 | * NLPIDs, so calling it and then adding a check |
---|
5860 | * for the particular NLPID for which we're |
---|
5861 | * looking is bogus, as we can just check for |
---|
5862 | * the NLPID. |
---|
5863 | * |
---|
5864 | * What we check for is the NLPID and a frame |
---|
5865 | * control field value of UI, i.e. 0x03 followed |
---|
5866 | * by the NLPID. |
---|
5867 | * |
---|
5868 | * XXX - assumes a 2-byte Frame Relay header with |
---|
5869 | * DLCI and flags. What if the address is longer? |
---|
5870 | * |
---|
5871 | * XXX - what about SNAP-encapsulated frames? |
---|
5872 | */ |
---|
5873 | return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v); |
---|
5874 | /*NOTREACHED*/ |
---|
5875 | break; |
---|
5876 | |
---|
5877 | case DLT_C_HDLC: |
---|
5878 | /* |
---|
5879 | * Cisco uses an Ethertype lookalike - for OSI, |
---|
5880 | * it's 0xfefe. |
---|
5881 | */ |
---|
5882 | b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS); |
---|
5883 | /* OSI in C-HDLC is stuffed with a fudge byte */ |
---|
5884 | b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v); |
---|
5885 | gen_and(b0, b1); |
---|
5886 | return b1; |
---|
5887 | |
---|
5888 | default: |
---|
5889 | b0 = gen_linktype(LLCSAP_ISONS); |
---|
5890 | b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v); |
---|
5891 | gen_and(b0, b1); |
---|
5892 | return b1; |
---|
5893 | } |
---|
5894 | |
---|
5895 | case Q_ISIS: |
---|
5896 | b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT); |
---|
5897 | /* |
---|
5898 | * 4 is the offset of the PDU type relative to the IS-IS |
---|
5899 | * header. |
---|
5900 | */ |
---|
5901 | b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v); |
---|
5902 | gen_and(b0, b1); |
---|
5903 | return b1; |
---|
5904 | |
---|
5905 | case Q_ARP: |
---|
5906 | bpf_error("arp does not encapsulate another protocol"); |
---|
5907 | /* NOTREACHED */ |
---|
5908 | |
---|
5909 | case Q_RARP: |
---|
5910 | bpf_error("rarp does not encapsulate another protocol"); |
---|
5911 | /* NOTREACHED */ |
---|
5912 | |
---|
5913 | case Q_ATALK: |
---|
5914 | bpf_error("atalk encapsulation is not specifiable"); |
---|
5915 | /* NOTREACHED */ |
---|
5916 | |
---|
5917 | case Q_DECNET: |
---|
5918 | bpf_error("decnet encapsulation is not specifiable"); |
---|
5919 | /* NOTREACHED */ |
---|
5920 | |
---|
5921 | case Q_SCA: |
---|
5922 | bpf_error("sca does not encapsulate another protocol"); |
---|
5923 | /* NOTREACHED */ |
---|
5924 | |
---|
5925 | case Q_LAT: |
---|
5926 | bpf_error("lat does not encapsulate another protocol"); |
---|
5927 | /* NOTREACHED */ |
---|
5928 | |
---|
5929 | case Q_MOPRC: |
---|
5930 | bpf_error("moprc does not encapsulate another protocol"); |
---|
5931 | /* NOTREACHED */ |
---|
5932 | |
---|
5933 | case Q_MOPDL: |
---|
5934 | bpf_error("mopdl does not encapsulate another protocol"); |
---|
5935 | /* NOTREACHED */ |
---|
5936 | |
---|
5937 | case Q_LINK: |
---|
5938 | return gen_linktype(v); |
---|
5939 | |
---|
5940 | case Q_UDP: |
---|
5941 | bpf_error("'udp proto' is bogus"); |
---|
5942 | /* NOTREACHED */ |
---|
5943 | |
---|
5944 | case Q_TCP: |
---|
5945 | bpf_error("'tcp proto' is bogus"); |
---|
5946 | /* NOTREACHED */ |
---|
5947 | |
---|
5948 | case Q_SCTP: |
---|
5949 | bpf_error("'sctp proto' is bogus"); |
---|
5950 | /* NOTREACHED */ |
---|
5951 | |
---|
5952 | case Q_ICMP: |
---|
5953 | bpf_error("'icmp proto' is bogus"); |
---|
5954 | /* NOTREACHED */ |
---|
5955 | |
---|
5956 | case Q_IGMP: |
---|
5957 | bpf_error("'igmp proto' is bogus"); |
---|
5958 | /* NOTREACHED */ |
---|
5959 | |
---|
5960 | case Q_IGRP: |
---|
5961 | bpf_error("'igrp proto' is bogus"); |
---|
5962 | /* NOTREACHED */ |
---|
5963 | |
---|
5964 | case Q_PIM: |
---|
5965 | bpf_error("'pim proto' is bogus"); |
---|
5966 | /* NOTREACHED */ |
---|
5967 | |
---|
5968 | case Q_VRRP: |
---|
5969 | bpf_error("'vrrp proto' is bogus"); |
---|
5970 | /* NOTREACHED */ |
---|
5971 | |
---|
5972 | case Q_CARP: |
---|
5973 | bpf_error("'carp proto' is bogus"); |
---|
5974 | /* NOTREACHED */ |
---|
5975 | |
---|
5976 | case Q_IPV6: |
---|
5977 | b0 = gen_linktype(ETHERTYPE_IPV6); |
---|
5978 | #ifndef CHASE_CHAIN |
---|
5979 | /* |
---|
5980 | * Also check for a fragment header before the final |
---|
5981 | * header. |
---|
5982 | */ |
---|
5983 | b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT); |
---|
5984 | b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v); |
---|
5985 | gen_and(b2, b1); |
---|
5986 | b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v); |
---|
5987 | gen_or(b2, b1); |
---|
5988 | #else |
---|
5989 | b1 = gen_protochain(v, Q_IPV6); |
---|
5990 | #endif |
---|
5991 | gen_and(b0, b1); |
---|
5992 | return b1; |
---|
5993 | |
---|
5994 | case Q_ICMPV6: |
---|
5995 | bpf_error("'icmp6 proto' is bogus"); |
---|
5996 | |
---|
5997 | case Q_AH: |
---|
5998 | bpf_error("'ah proto' is bogus"); |
---|
5999 | |
---|
6000 | case Q_ESP: |
---|
6001 | bpf_error("'ah proto' is bogus"); |
---|
6002 | |
---|
6003 | case Q_STP: |
---|
6004 | bpf_error("'stp proto' is bogus"); |
---|
6005 | |
---|
6006 | case Q_IPX: |
---|
6007 | bpf_error("'ipx proto' is bogus"); |
---|
6008 | |
---|
6009 | case Q_NETBEUI: |
---|
6010 | bpf_error("'netbeui proto' is bogus"); |
---|
6011 | |
---|
6012 | case Q_RADIO: |
---|
6013 | bpf_error("'radio proto' is bogus"); |
---|
6014 | |
---|
6015 | default: |
---|
6016 | abort(); |
---|
6017 | /* NOTREACHED */ |
---|
6018 | } |
---|
6019 | /* NOTREACHED */ |
---|
6020 | } |
---|
6021 | |
---|
6022 | struct block * |
---|
6023 | gen_scode(name, q) |
---|
6024 | register const char *name; |
---|
6025 | struct qual q; |
---|
6026 | { |
---|
6027 | int proto = q.proto; |
---|
6028 | int dir = q.dir; |
---|
6029 | int tproto; |
---|
6030 | u_char *eaddr; |
---|
6031 | bpf_u_int32 mask, addr; |
---|
6032 | #ifndef INET6 |
---|
6033 | bpf_u_int32 **alist; |
---|
6034 | #else |
---|
6035 | int tproto6; |
---|
6036 | struct sockaddr_in *sin4; |
---|
6037 | struct sockaddr_in6 *sin6; |
---|
6038 | struct addrinfo *res, *res0; |
---|
6039 | struct in6_addr mask128; |
---|
6040 | #endif /*INET6*/ |
---|
6041 | struct block *b, *tmp; |
---|
6042 | int port, real_proto; |
---|
6043 | int port1, port2; |
---|
6044 | |
---|
6045 | switch (q.addr) { |
---|
6046 | |
---|
6047 | case Q_NET: |
---|
6048 | addr = pcap_nametonetaddr(name); |
---|
6049 | if (addr == 0) |
---|
6050 | bpf_error("unknown network '%s'", name); |
---|
6051 | /* Left justify network addr and calculate its network mask */ |
---|
6052 | mask = 0xffffffff; |
---|
6053 | while (addr && (addr & 0xff000000) == 0) { |
---|
6054 | addr <<= 8; |
---|
6055 | mask <<= 8; |
---|
6056 | } |
---|
6057 | return gen_host(addr, mask, proto, dir, q.addr); |
---|
6058 | |
---|
6059 | case Q_DEFAULT: |
---|
6060 | case Q_HOST: |
---|
6061 | if (proto == Q_LINK) { |
---|
6062 | switch (linktype) { |
---|
6063 | |
---|
6064 | case DLT_EN10MB: |
---|
6065 | case DLT_NETANALYZER: |
---|
6066 | case DLT_NETANALYZER_TRANSPARENT: |
---|
6067 | eaddr = pcap_ether_hostton(name); |
---|
6068 | if (eaddr == NULL) |
---|
6069 | bpf_error( |
---|
6070 | "unknown ether host '%s'", name); |
---|
6071 | b = gen_ehostop(eaddr, dir); |
---|
6072 | free(eaddr); |
---|
6073 | return b; |
---|
6074 | |
---|
6075 | case DLT_FDDI: |
---|
6076 | eaddr = pcap_ether_hostton(name); |
---|
6077 | if (eaddr == NULL) |
---|
6078 | bpf_error( |
---|
6079 | "unknown FDDI host '%s'", name); |
---|
6080 | b = gen_fhostop(eaddr, dir); |
---|
6081 | free(eaddr); |
---|
6082 | return b; |
---|
6083 | |
---|
6084 | case DLT_IEEE802: |
---|
6085 | eaddr = pcap_ether_hostton(name); |
---|
6086 | if (eaddr == NULL) |
---|
6087 | bpf_error( |
---|
6088 | "unknown token ring host '%s'", name); |
---|
6089 | b = gen_thostop(eaddr, dir); |
---|
6090 | free(eaddr); |
---|
6091 | return b; |
---|
6092 | |
---|
6093 | case DLT_IEEE802_11: |
---|
6094 | case DLT_PRISM_HEADER: |
---|
6095 | case DLT_IEEE802_11_RADIO_AVS: |
---|
6096 | case DLT_IEEE802_11_RADIO: |
---|
6097 | case DLT_PPI: |
---|
6098 | eaddr = pcap_ether_hostton(name); |
---|
6099 | if (eaddr == NULL) |
---|
6100 | bpf_error( |
---|
6101 | "unknown 802.11 host '%s'", name); |
---|
6102 | b = gen_wlanhostop(eaddr, dir); |
---|
6103 | free(eaddr); |
---|
6104 | return b; |
---|
6105 | |
---|
6106 | case DLT_IP_OVER_FC: |
---|
6107 | eaddr = pcap_ether_hostton(name); |
---|
6108 | if (eaddr == NULL) |
---|
6109 | bpf_error( |
---|
6110 | "unknown Fibre Channel host '%s'", name); |
---|
6111 | b = gen_ipfchostop(eaddr, dir); |
---|
6112 | free(eaddr); |
---|
6113 | return b; |
---|
6114 | |
---|
6115 | case DLT_SUNATM: |
---|
6116 | if (!is_lane) |
---|
6117 | break; |
---|
6118 | |
---|
6119 | /* |
---|
6120 | * Check that the packet doesn't begin |
---|
6121 | * with an LE Control marker. (We've |
---|
6122 | * already generated a test for LANE.) |
---|
6123 | */ |
---|
6124 | tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, |
---|
6125 | BPF_H, 0xFF00); |
---|
6126 | gen_not(tmp); |
---|
6127 | |
---|
6128 | eaddr = pcap_ether_hostton(name); |
---|
6129 | if (eaddr == NULL) |
---|
6130 | bpf_error( |
---|
6131 | "unknown ether host '%s'", name); |
---|
6132 | b = gen_ehostop(eaddr, dir); |
---|
6133 | gen_and(tmp, b); |
---|
6134 | free(eaddr); |
---|
6135 | return b; |
---|
6136 | } |
---|
6137 | |
---|
6138 | bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name"); |
---|
6139 | } else if (proto == Q_DECNET) { |
---|
6140 | unsigned short dn_addr = __pcap_nametodnaddr(name); |
---|
6141 | /* |
---|
6142 | * I don't think DECNET hosts can be multihomed, so |
---|
6143 | * there is no need to build up a list of addresses |
---|
6144 | */ |
---|
6145 | return (gen_host(dn_addr, 0, proto, dir, q.addr)); |
---|
6146 | } else { |
---|
6147 | #ifndef INET6 |
---|
6148 | alist = pcap_nametoaddr(name); |
---|
6149 | if (alist == NULL || *alist == NULL) |
---|
6150 | bpf_error("unknown host '%s'", name); |
---|
6151 | tproto = proto; |
---|
6152 | if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT) |
---|
6153 | tproto = Q_IP; |
---|
6154 | b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr); |
---|
6155 | while (*alist) { |
---|
6156 | tmp = gen_host(**alist++, 0xffffffff, |
---|
6157 | tproto, dir, q.addr); |
---|
6158 | gen_or(b, tmp); |
---|
6159 | b = tmp; |
---|
6160 | } |
---|
6161 | return b; |
---|
6162 | #else |
---|
6163 | memset(&mask128, 0xff, sizeof(mask128)); |
---|
6164 | res0 = res = pcap_nametoaddrinfo(name); |
---|
6165 | if (res == NULL) |
---|
6166 | bpf_error("unknown host '%s'", name); |
---|
6167 | ai = res; |
---|
6168 | b = tmp = NULL; |
---|
6169 | tproto = tproto6 = proto; |
---|
6170 | if (off_linktype == -1 && tproto == Q_DEFAULT) { |
---|
6171 | tproto = Q_IP; |
---|
6172 | tproto6 = Q_IPV6; |
---|
6173 | } |
---|
6174 | for (res = res0; res; res = res->ai_next) { |
---|
6175 | switch (res->ai_family) { |
---|
6176 | case AF_INET: |
---|
6177 | if (tproto == Q_IPV6) |
---|
6178 | continue; |
---|
6179 | |
---|
6180 | sin4 = (struct sockaddr_in *) |
---|
6181 | res->ai_addr; |
---|
6182 | tmp = gen_host(ntohl(sin4->sin_addr.s_addr), |
---|
6183 | 0xffffffff, tproto, dir, q.addr); |
---|
6184 | break; |
---|
6185 | case AF_INET6: |
---|
6186 | if (tproto6 == Q_IP) |
---|
6187 | continue; |
---|
6188 | |
---|
6189 | sin6 = (struct sockaddr_in6 *) |
---|
6190 | res->ai_addr; |
---|
6191 | tmp = gen_host6(&sin6->sin6_addr, |
---|
6192 | &mask128, tproto6, dir, q.addr); |
---|
6193 | break; |
---|
6194 | default: |
---|
6195 | continue; |
---|
6196 | } |
---|
6197 | if (b) |
---|
6198 | gen_or(b, tmp); |
---|
6199 | b = tmp; |
---|
6200 | } |
---|
6201 | ai = NULL; |
---|
6202 | freeaddrinfo(res0); |
---|
6203 | if (b == NULL) { |
---|
6204 | bpf_error("unknown host '%s'%s", name, |
---|
6205 | (proto == Q_DEFAULT) |
---|
6206 | ? "" |
---|
6207 | : " for specified address family"); |
---|
6208 | } |
---|
6209 | return b; |
---|
6210 | #endif /*INET6*/ |
---|
6211 | } |
---|
6212 | |
---|
6213 | case Q_PORT: |
---|
6214 | if (proto != Q_DEFAULT && |
---|
6215 | proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) |
---|
6216 | bpf_error("illegal qualifier of 'port'"); |
---|
6217 | if (pcap_nametoport(name, &port, &real_proto) == 0) |
---|
6218 | bpf_error("unknown port '%s'", name); |
---|
6219 | if (proto == Q_UDP) { |
---|
6220 | if (real_proto == IPPROTO_TCP) |
---|
6221 | bpf_error("port '%s' is tcp", name); |
---|
6222 | else if (real_proto == IPPROTO_SCTP) |
---|
6223 | bpf_error("port '%s' is sctp", name); |
---|
6224 | else |
---|
6225 | /* override PROTO_UNDEF */ |
---|
6226 | real_proto = IPPROTO_UDP; |
---|
6227 | } |
---|
6228 | if (proto == Q_TCP) { |
---|
6229 | if (real_proto == IPPROTO_UDP) |
---|
6230 | bpf_error("port '%s' is udp", name); |
---|
6231 | |
---|
6232 | else if (real_proto == IPPROTO_SCTP) |
---|
6233 | bpf_error("port '%s' is sctp", name); |
---|
6234 | else |
---|
6235 | /* override PROTO_UNDEF */ |
---|
6236 | real_proto = IPPROTO_TCP; |
---|
6237 | } |
---|
6238 | if (proto == Q_SCTP) { |
---|
6239 | if (real_proto == IPPROTO_UDP) |
---|
6240 | bpf_error("port '%s' is udp", name); |
---|
6241 | |
---|
6242 | else if (real_proto == IPPROTO_TCP) |
---|
6243 | bpf_error("port '%s' is tcp", name); |
---|
6244 | else |
---|
6245 | /* override PROTO_UNDEF */ |
---|
6246 | real_proto = IPPROTO_SCTP; |
---|
6247 | } |
---|
6248 | if (port < 0) |
---|
6249 | bpf_error("illegal port number %d < 0", port); |
---|
6250 | if (port > 65535) |
---|
6251 | bpf_error("illegal port number %d > 65535", port); |
---|
6252 | b = gen_port(port, real_proto, dir); |
---|
6253 | gen_or(gen_port6(port, real_proto, dir), b); |
---|
6254 | return b; |
---|
6255 | |
---|
6256 | case Q_PORTRANGE: |
---|
6257 | if (proto != Q_DEFAULT && |
---|
6258 | proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) |
---|
6259 | bpf_error("illegal qualifier of 'portrange'"); |
---|
6260 | if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0) |
---|
6261 | bpf_error("unknown port in range '%s'", name); |
---|
6262 | if (proto == Q_UDP) { |
---|
6263 | if (real_proto == IPPROTO_TCP) |
---|
6264 | bpf_error("port in range '%s' is tcp", name); |
---|
6265 | else if (real_proto == IPPROTO_SCTP) |
---|
6266 | bpf_error("port in range '%s' is sctp", name); |
---|
6267 | else |
---|
6268 | /* override PROTO_UNDEF */ |
---|
6269 | real_proto = IPPROTO_UDP; |
---|
6270 | } |
---|
6271 | if (proto == Q_TCP) { |
---|
6272 | if (real_proto == IPPROTO_UDP) |
---|
6273 | bpf_error("port in range '%s' is udp", name); |
---|
6274 | else if (real_proto == IPPROTO_SCTP) |
---|
6275 | bpf_error("port in range '%s' is sctp", name); |
---|
6276 | else |
---|
6277 | /* override PROTO_UNDEF */ |
---|
6278 | real_proto = IPPROTO_TCP; |
---|
6279 | } |
---|
6280 | if (proto == Q_SCTP) { |
---|
6281 | if (real_proto == IPPROTO_UDP) |
---|
6282 | bpf_error("port in range '%s' is udp", name); |
---|
6283 | else if (real_proto == IPPROTO_TCP) |
---|
6284 | bpf_error("port in range '%s' is tcp", name); |
---|
6285 | else |
---|
6286 | /* override PROTO_UNDEF */ |
---|
6287 | real_proto = IPPROTO_SCTP; |
---|
6288 | } |
---|
6289 | if (port1 < 0) |
---|
6290 | bpf_error("illegal port number %d < 0", port1); |
---|
6291 | if (port1 > 65535) |
---|
6292 | bpf_error("illegal port number %d > 65535", port1); |
---|
6293 | if (port2 < 0) |
---|
6294 | bpf_error("illegal port number %d < 0", port2); |
---|
6295 | if (port2 > 65535) |
---|
6296 | bpf_error("illegal port number %d > 65535", port2); |
---|
6297 | |
---|
6298 | b = gen_portrange(port1, port2, real_proto, dir); |
---|
6299 | gen_or(gen_portrange6(port1, port2, real_proto, dir), b); |
---|
6300 | return b; |
---|
6301 | |
---|
6302 | case Q_GATEWAY: |
---|
6303 | #ifndef INET6 |
---|
6304 | eaddr = pcap_ether_hostton(name); |
---|
6305 | if (eaddr == NULL) |
---|
6306 | bpf_error("unknown ether host: %s", name); |
---|
6307 | |
---|
6308 | alist = pcap_nametoaddr(name); |
---|
6309 | if (alist == NULL || *alist == NULL) |
---|
6310 | bpf_error("unknown host '%s'", name); |
---|
6311 | b = gen_gateway(eaddr, alist, proto, dir); |
---|
6312 | free(eaddr); |
---|
6313 | return b; |
---|
6314 | #else |
---|
6315 | bpf_error("'gateway' not supported in this configuration"); |
---|
6316 | #endif /*INET6*/ |
---|
6317 | |
---|
6318 | case Q_PROTO: |
---|
6319 | real_proto = lookup_proto(name, proto); |
---|
6320 | if (real_proto >= 0) |
---|
6321 | return gen_proto(real_proto, proto, dir); |
---|
6322 | else |
---|
6323 | bpf_error("unknown protocol: %s", name); |
---|
6324 | |
---|
6325 | case Q_PROTOCHAIN: |
---|
6326 | real_proto = lookup_proto(name, proto); |
---|
6327 | if (real_proto >= 0) |
---|
6328 | return gen_protochain(real_proto, proto, dir); |
---|
6329 | else |
---|
6330 | bpf_error("unknown protocol: %s", name); |
---|
6331 | |
---|
6332 | case Q_UNDEF: |
---|
6333 | syntax(); |
---|
6334 | /* NOTREACHED */ |
---|
6335 | } |
---|
6336 | abort(); |
---|
6337 | /* NOTREACHED */ |
---|
6338 | } |
---|
6339 | |
---|
6340 | struct block * |
---|
6341 | gen_mcode(s1, s2, masklen, q) |
---|
6342 | register const char *s1, *s2; |
---|
6343 | register int masklen; |
---|
6344 | struct qual q; |
---|
6345 | { |
---|
6346 | register int nlen, mlen; |
---|
6347 | bpf_u_int32 n, m; |
---|
6348 | |
---|
6349 | nlen = __pcap_atoin(s1, &n); |
---|
6350 | /* Promote short ipaddr */ |
---|
6351 | n <<= 32 - nlen; |
---|
6352 | |
---|
6353 | if (s2 != NULL) { |
---|
6354 | mlen = __pcap_atoin(s2, &m); |
---|
6355 | /* Promote short ipaddr */ |
---|
6356 | m <<= 32 - mlen; |
---|
6357 | if ((n & ~m) != 0) |
---|
6358 | bpf_error("non-network bits set in \"%s mask %s\"", |
---|
6359 | s1, s2); |
---|
6360 | } else { |
---|
6361 | /* Convert mask len to mask */ |
---|
6362 | if (masklen > 32) |
---|
6363 | bpf_error("mask length must be <= 32"); |
---|
6364 | if (masklen == 0) { |
---|
6365 | /* |
---|
6366 | * X << 32 is not guaranteed by C to be 0; it's |
---|
6367 | * undefined. |
---|
6368 | */ |
---|
6369 | m = 0; |
---|
6370 | } else |
---|
6371 | m = 0xffffffff << (32 - masklen); |
---|
6372 | if ((n & ~m) != 0) |
---|
6373 | bpf_error("non-network bits set in \"%s/%d\"", |
---|
6374 | s1, masklen); |
---|
6375 | } |
---|
6376 | |
---|
6377 | switch (q.addr) { |
---|
6378 | |
---|
6379 | case Q_NET: |
---|
6380 | return gen_host(n, m, q.proto, q.dir, q.addr); |
---|
6381 | |
---|
6382 | default: |
---|
6383 | bpf_error("Mask syntax for networks only"); |
---|
6384 | /* NOTREACHED */ |
---|
6385 | } |
---|
6386 | /* NOTREACHED */ |
---|
6387 | return NULL; |
---|
6388 | } |
---|
6389 | |
---|
6390 | struct block * |
---|
6391 | gen_ncode(s, v, q) |
---|
6392 | register const char *s; |
---|
6393 | bpf_u_int32 v; |
---|
6394 | struct qual q; |
---|
6395 | { |
---|
6396 | bpf_u_int32 mask; |
---|
6397 | int proto = q.proto; |
---|
6398 | int dir = q.dir; |
---|
6399 | register int vlen; |
---|
6400 | |
---|
6401 | if (s == NULL) |
---|
6402 | vlen = 32; |
---|
6403 | else if (q.proto == Q_DECNET) |
---|
6404 | vlen = __pcap_atodn(s, &v); |
---|
6405 | else |
---|
6406 | vlen = __pcap_atoin(s, &v); |
---|
6407 | |
---|
6408 | switch (q.addr) { |
---|
6409 | |
---|
6410 | case Q_DEFAULT: |
---|
6411 | case Q_HOST: |
---|
6412 | case Q_NET: |
---|
6413 | if (proto == Q_DECNET) |
---|
6414 | return gen_host(v, 0, proto, dir, q.addr); |
---|
6415 | else if (proto == Q_LINK) { |
---|
6416 | bpf_error("illegal link layer address"); |
---|
6417 | } else { |
---|
6418 | mask = 0xffffffff; |
---|
6419 | if (s == NULL && q.addr == Q_NET) { |
---|
6420 | /* Promote short net number */ |
---|
6421 | while (v && (v & 0xff000000) == 0) { |
---|
6422 | v <<= 8; |
---|
6423 | mask <<= 8; |
---|
6424 | } |
---|
6425 | } else { |
---|
6426 | /* Promote short ipaddr */ |
---|
6427 | v <<= 32 - vlen; |
---|
6428 | mask <<= 32 - vlen; |
---|
6429 | } |
---|
6430 | return gen_host(v, mask, proto, dir, q.addr); |
---|
6431 | } |
---|
6432 | |
---|
6433 | case Q_PORT: |
---|
6434 | if (proto == Q_UDP) |
---|
6435 | proto = IPPROTO_UDP; |
---|
6436 | else if (proto == Q_TCP) |
---|
6437 | proto = IPPROTO_TCP; |
---|
6438 | else if (proto == Q_SCTP) |
---|
6439 | proto = IPPROTO_SCTP; |
---|
6440 | else if (proto == Q_DEFAULT) |
---|
6441 | proto = PROTO_UNDEF; |
---|
6442 | else |
---|
6443 | bpf_error("illegal qualifier of 'port'"); |
---|
6444 | |
---|
6445 | if (v > 65535) |
---|
6446 | bpf_error("illegal port number %u > 65535", v); |
---|
6447 | |
---|
6448 | { |
---|
6449 | struct block *b; |
---|
6450 | b = gen_port((int)v, proto, dir); |
---|
6451 | gen_or(gen_port6((int)v, proto, dir), b); |
---|
6452 | return b; |
---|
6453 | } |
---|
6454 | |
---|
6455 | case Q_PORTRANGE: |
---|
6456 | if (proto == Q_UDP) |
---|
6457 | proto = IPPROTO_UDP; |
---|
6458 | else if (proto == Q_TCP) |
---|
6459 | proto = IPPROTO_TCP; |
---|
6460 | else if (proto == Q_SCTP) |
---|
6461 | proto = IPPROTO_SCTP; |
---|
6462 | else if (proto == Q_DEFAULT) |
---|
6463 | proto = PROTO_UNDEF; |
---|
6464 | else |
---|
6465 | bpf_error("illegal qualifier of 'portrange'"); |
---|
6466 | |
---|
6467 | if (v > 65535) |
---|
6468 | bpf_error("illegal port number %u > 65535", v); |
---|
6469 | |
---|
6470 | { |
---|
6471 | struct block *b; |
---|
6472 | b = gen_portrange((int)v, (int)v, proto, dir); |
---|
6473 | gen_or(gen_portrange6((int)v, (int)v, proto, dir), b); |
---|
6474 | return b; |
---|
6475 | } |
---|
6476 | |
---|
6477 | case Q_GATEWAY: |
---|
6478 | bpf_error("'gateway' requires a name"); |
---|
6479 | /* NOTREACHED */ |
---|
6480 | |
---|
6481 | case Q_PROTO: |
---|
6482 | return gen_proto((int)v, proto, dir); |
---|
6483 | |
---|
6484 | case Q_PROTOCHAIN: |
---|
6485 | return gen_protochain((int)v, proto, dir); |
---|
6486 | |
---|
6487 | case Q_UNDEF: |
---|
6488 | syntax(); |
---|
6489 | /* NOTREACHED */ |
---|
6490 | |
---|
6491 | default: |
---|
6492 | abort(); |
---|
6493 | /* NOTREACHED */ |
---|
6494 | } |
---|
6495 | /* NOTREACHED */ |
---|
6496 | } |
---|
6497 | |
---|
6498 | #ifdef INET6 |
---|
6499 | struct block * |
---|
6500 | gen_mcode6(s1, s2, masklen, q) |
---|
6501 | register const char *s1, *s2; |
---|
6502 | register int masklen; |
---|
6503 | struct qual q; |
---|
6504 | { |
---|
6505 | struct addrinfo *res; |
---|
6506 | struct in6_addr *addr; |
---|
6507 | struct in6_addr mask; |
---|
6508 | struct block *b; |
---|
6509 | u_int32_t *a, *m; |
---|
6510 | |
---|
6511 | if (s2) |
---|
6512 | bpf_error("no mask %s supported", s2); |
---|
6513 | |
---|
6514 | res = pcap_nametoaddrinfo(s1); |
---|
6515 | if (!res) |
---|
6516 | bpf_error("invalid ip6 address %s", s1); |
---|
6517 | ai = res; |
---|
6518 | if (res->ai_next) |
---|
6519 | bpf_error("%s resolved to multiple address", s1); |
---|
6520 | addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; |
---|
6521 | |
---|
6522 | if (sizeof(mask) * 8 < masklen) |
---|
6523 | bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8)); |
---|
6524 | memset(&mask, 0, sizeof(mask)); |
---|
6525 | memset(&mask, 0xff, masklen / 8); |
---|
6526 | if (masklen % 8) { |
---|
6527 | mask.s6_addr[masklen / 8] = |
---|
6528 | (0xff << (8 - masklen % 8)) & 0xff; |
---|
6529 | } |
---|
6530 | |
---|
6531 | a = (u_int32_t *)addr; |
---|
6532 | m = (u_int32_t *)&mask; |
---|
6533 | if ((a[0] & ~m[0]) || (a[1] & ~m[1]) |
---|
6534 | || (a[2] & ~m[2]) || (a[3] & ~m[3])) { |
---|
6535 | bpf_error("non-network bits set in \"%s/%d\"", s1, masklen); |
---|
6536 | } |
---|
6537 | |
---|
6538 | switch (q.addr) { |
---|
6539 | |
---|
6540 | case Q_DEFAULT: |
---|
6541 | case Q_HOST: |
---|
6542 | if (masklen != 128) |
---|
6543 | bpf_error("Mask syntax for networks only"); |
---|
6544 | /* FALLTHROUGH */ |
---|
6545 | |
---|
6546 | case Q_NET: |
---|
6547 | b = gen_host6(addr, &mask, q.proto, q.dir, q.addr); |
---|
6548 | ai = NULL; |
---|
6549 | freeaddrinfo(res); |
---|
6550 | return b; |
---|
6551 | |
---|
6552 | default: |
---|
6553 | bpf_error("invalid qualifier against IPv6 address"); |
---|
6554 | /* NOTREACHED */ |
---|
6555 | } |
---|
6556 | return NULL; |
---|
6557 | } |
---|
6558 | #endif /*INET6*/ |
---|
6559 | |
---|
6560 | struct block * |
---|
6561 | gen_ecode(eaddr, q) |
---|
6562 | register const u_char *eaddr; |
---|
6563 | struct qual q; |
---|
6564 | { |
---|
6565 | struct block *b, *tmp; |
---|
6566 | |
---|
6567 | if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) { |
---|
6568 | switch (linktype) { |
---|
6569 | case DLT_EN10MB: |
---|
6570 | case DLT_NETANALYZER: |
---|
6571 | case DLT_NETANALYZER_TRANSPARENT: |
---|
6572 | return gen_ehostop(eaddr, (int)q.dir); |
---|
6573 | case DLT_FDDI: |
---|
6574 | return gen_fhostop(eaddr, (int)q.dir); |
---|
6575 | case DLT_IEEE802: |
---|
6576 | return gen_thostop(eaddr, (int)q.dir); |
---|
6577 | case DLT_IEEE802_11: |
---|
6578 | case DLT_PRISM_HEADER: |
---|
6579 | case DLT_IEEE802_11_RADIO_AVS: |
---|
6580 | case DLT_IEEE802_11_RADIO: |
---|
6581 | case DLT_PPI: |
---|
6582 | return gen_wlanhostop(eaddr, (int)q.dir); |
---|
6583 | case DLT_SUNATM: |
---|
6584 | if (is_lane) { |
---|
6585 | /* |
---|
6586 | * Check that the packet doesn't begin with an |
---|
6587 | * LE Control marker. (We've already generated |
---|
6588 | * a test for LANE.) |
---|
6589 | */ |
---|
6590 | tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H, |
---|
6591 | 0xFF00); |
---|
6592 | gen_not(tmp); |
---|
6593 | |
---|
6594 | /* |
---|
6595 | * Now check the MAC address. |
---|
6596 | */ |
---|
6597 | b = gen_ehostop(eaddr, (int)q.dir); |
---|
6598 | gen_and(tmp, b); |
---|
6599 | return b; |
---|
6600 | } |
---|
6601 | break; |
---|
6602 | case DLT_IP_OVER_FC: |
---|
6603 | return gen_ipfchostop(eaddr, (int)q.dir); |
---|
6604 | default: |
---|
6605 | bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
---|
6606 | break; |
---|
6607 | } |
---|
6608 | } |
---|
6609 | bpf_error("ethernet address used in non-ether expression"); |
---|
6610 | /* NOTREACHED */ |
---|
6611 | return NULL; |
---|
6612 | } |
---|
6613 | |
---|
6614 | void |
---|
6615 | sappend(s0, s1) |
---|
6616 | struct slist *s0, *s1; |
---|
6617 | { |
---|
6618 | /* |
---|
6619 | * This is definitely not the best way to do this, but the |
---|
6620 | * lists will rarely get long. |
---|
6621 | */ |
---|
6622 | while (s0->next) |
---|
6623 | s0 = s0->next; |
---|
6624 | s0->next = s1; |
---|
6625 | } |
---|
6626 | |
---|
6627 | static struct slist * |
---|
6628 | xfer_to_x(a) |
---|
6629 | struct arth *a; |
---|
6630 | { |
---|
6631 | struct slist *s; |
---|
6632 | |
---|
6633 | s = new_stmt(BPF_LDX|BPF_MEM); |
---|
6634 | s->s.k = a->regno; |
---|
6635 | return s; |
---|
6636 | } |
---|
6637 | |
---|
6638 | static struct slist * |
---|
6639 | xfer_to_a(a) |
---|
6640 | struct arth *a; |
---|
6641 | { |
---|
6642 | struct slist *s; |
---|
6643 | |
---|
6644 | s = new_stmt(BPF_LD|BPF_MEM); |
---|
6645 | s->s.k = a->regno; |
---|
6646 | return s; |
---|
6647 | } |
---|
6648 | |
---|
6649 | /* |
---|
6650 | * Modify "index" to use the value stored into its register as an |
---|
6651 | * offset relative to the beginning of the header for the protocol |
---|
6652 | * "proto", and allocate a register and put an item "size" bytes long |
---|
6653 | * (1, 2, or 4) at that offset into that register, making it the register |
---|
6654 | * for "index". |
---|
6655 | */ |
---|
6656 | struct arth * |
---|
6657 | gen_load(proto, inst, size) |
---|
6658 | int proto; |
---|
6659 | struct arth *inst; |
---|
6660 | int size; |
---|
6661 | { |
---|
6662 | struct slist *s, *tmp; |
---|
6663 | struct block *b; |
---|
6664 | int regno = alloc_reg(); |
---|
6665 | |
---|
6666 | free_reg(inst->regno); |
---|
6667 | switch (size) { |
---|
6668 | |
---|
6669 | default: |
---|
6670 | bpf_error("data size must be 1, 2, or 4"); |
---|
6671 | |
---|
6672 | case 1: |
---|
6673 | size = BPF_B; |
---|
6674 | break; |
---|
6675 | |
---|
6676 | case 2: |
---|
6677 | size = BPF_H; |
---|
6678 | break; |
---|
6679 | |
---|
6680 | case 4: |
---|
6681 | size = BPF_W; |
---|
6682 | break; |
---|
6683 | } |
---|
6684 | switch (proto) { |
---|
6685 | default: |
---|
6686 | bpf_error("unsupported index operation"); |
---|
6687 | |
---|
6688 | case Q_RADIO: |
---|
6689 | /* |
---|
6690 | * The offset is relative to the beginning of the packet |
---|
6691 | * data, if we have a radio header. (If we don't, this |
---|
6692 | * is an error.) |
---|
6693 | */ |
---|
6694 | if (linktype != DLT_IEEE802_11_RADIO_AVS && |
---|
6695 | linktype != DLT_IEEE802_11_RADIO && |
---|
6696 | linktype != DLT_PRISM_HEADER) |
---|
6697 | bpf_error("radio information not present in capture"); |
---|
6698 | |
---|
6699 | /* |
---|
6700 | * Load into the X register the offset computed into the |
---|
6701 | * register specified by "index". |
---|
6702 | */ |
---|
6703 | s = xfer_to_x(inst); |
---|
6704 | |
---|
6705 | /* |
---|
6706 | * Load the item at that offset. |
---|
6707 | */ |
---|
6708 | tmp = new_stmt(BPF_LD|BPF_IND|size); |
---|
6709 | sappend(s, tmp); |
---|
6710 | sappend(inst->s, s); |
---|
6711 | break; |
---|
6712 | |
---|
6713 | case Q_LINK: |
---|
6714 | /* |
---|
6715 | * The offset is relative to the beginning of |
---|
6716 | * the link-layer header. |
---|
6717 | * |
---|
6718 | * XXX - what about ATM LANE? Should the index be |
---|
6719 | * relative to the beginning of the AAL5 frame, so |
---|
6720 | * that 0 refers to the beginning of the LE Control |
---|
6721 | * field, or relative to the beginning of the LAN |
---|
6722 | * frame, so that 0 refers, for Ethernet LANE, to |
---|
6723 | * the beginning of the destination address? |
---|
6724 | */ |
---|
6725 | s = gen_llprefixlen(); |
---|
6726 | |
---|
6727 | /* |
---|
6728 | * If "s" is non-null, it has code to arrange that the |
---|
6729 | * X register contains the length of the prefix preceding |
---|
6730 | * the link-layer header. Add to it the offset computed |
---|
6731 | * into the register specified by "index", and move that |
---|
6732 | * into the X register. Otherwise, just load into the X |
---|
6733 | * register the offset computed into the register specified |
---|
6734 | * by "index". |
---|
6735 | */ |
---|
6736 | if (s != NULL) { |
---|
6737 | sappend(s, xfer_to_a(inst)); |
---|
6738 | sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X)); |
---|
6739 | sappend(s, new_stmt(BPF_MISC|BPF_TAX)); |
---|
6740 | } else |
---|
6741 | s = xfer_to_x(inst); |
---|
6742 | |
---|
6743 | /* |
---|
6744 | * Load the item at the sum of the offset we've put in the |
---|
6745 | * X register and the offset of the start of the link |
---|
6746 | * layer header (which is 0 if the radio header is |
---|
6747 | * variable-length; that header length is what we put |
---|
6748 | * into the X register and then added to the index). |
---|
6749 | */ |
---|
6750 | tmp = new_stmt(BPF_LD|BPF_IND|size); |
---|
6751 | tmp->s.k = off_ll; |
---|
6752 | sappend(s, tmp); |
---|
6753 | sappend(inst->s, s); |
---|
6754 | break; |
---|
6755 | |
---|
6756 | case Q_IP: |
---|
6757 | case Q_ARP: |
---|
6758 | case Q_RARP: |
---|
6759 | case Q_ATALK: |
---|
6760 | case Q_DECNET: |
---|
6761 | case Q_SCA: |
---|
6762 | case Q_LAT: |
---|
6763 | case Q_MOPRC: |
---|
6764 | case Q_MOPDL: |
---|
6765 | case Q_IPV6: |
---|
6766 | /* |
---|
6767 | * The offset is relative to the beginning of |
---|
6768 | * the network-layer header. |
---|
6769 | * XXX - are there any cases where we want |
---|
6770 | * off_nl_nosnap? |
---|
6771 | */ |
---|
6772 | s = gen_off_macpl(); |
---|
6773 | |
---|
6774 | /* |
---|
6775 | * If "s" is non-null, it has code to arrange that the |
---|
6776 | * X register contains the offset of the MAC-layer |
---|
6777 | * payload. Add to it the offset computed into the |
---|
6778 | * register specified by "index", and move that into |
---|
6779 | * the X register. Otherwise, just load into the X |
---|
6780 | * register the offset computed into the register specified |
---|
6781 | * by "index". |
---|
6782 | */ |
---|
6783 | if (s != NULL) { |
---|
6784 | sappend(s, xfer_to_a(inst)); |
---|
6785 | sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X)); |
---|
6786 | sappend(s, new_stmt(BPF_MISC|BPF_TAX)); |
---|
6787 | } else |
---|
6788 | s = xfer_to_x(inst); |
---|
6789 | |
---|
6790 | /* |
---|
6791 | * Load the item at the sum of the offset we've put in the |
---|
6792 | * X register, the offset of the start of the network |
---|
6793 | * layer header from the beginning of the MAC-layer |
---|
6794 | * payload, and the purported offset of the start of the |
---|
6795 | * MAC-layer payload (which might be 0 if there's a |
---|
6796 | * variable-length prefix before the link-layer header |
---|
6797 | * or the link-layer header itself is variable-length; |
---|
6798 | * the variable-length offset of the start of the |
---|
6799 | * MAC-layer payload is what we put into the X register |
---|
6800 | * and then added to the index). |
---|
6801 | */ |
---|
6802 | tmp = new_stmt(BPF_LD|BPF_IND|size); |
---|
6803 | tmp->s.k = off_macpl + off_nl; |
---|
6804 | sappend(s, tmp); |
---|
6805 | sappend(inst->s, s); |
---|
6806 | |
---|
6807 | /* |
---|
6808 | * Do the computation only if the packet contains |
---|
6809 | * the protocol in question. |
---|
6810 | */ |
---|
6811 | b = gen_proto_abbrev(proto); |
---|
6812 | if (inst->b) |
---|
6813 | gen_and(inst->b, b); |
---|
6814 | inst->b = b; |
---|
6815 | break; |
---|
6816 | |
---|
6817 | case Q_SCTP: |
---|
6818 | case Q_TCP: |
---|
6819 | case Q_UDP: |
---|
6820 | case Q_ICMP: |
---|
6821 | case Q_IGMP: |
---|
6822 | case Q_IGRP: |
---|
6823 | case Q_PIM: |
---|
6824 | case Q_VRRP: |
---|
6825 | case Q_CARP: |
---|
6826 | /* |
---|
6827 | * The offset is relative to the beginning of |
---|
6828 | * the transport-layer header. |
---|
6829 | * |
---|
6830 | * Load the X register with the length of the IPv4 header |
---|
6831 | * (plus the offset of the link-layer header, if it's |
---|
6832 | * a variable-length header), in bytes. |
---|
6833 | * |
---|
6834 | * XXX - are there any cases where we want |
---|
6835 | * off_nl_nosnap? |
---|
6836 | * XXX - we should, if we're built with |
---|
6837 | * IPv6 support, generate code to load either |
---|
6838 | * IPv4, IPv6, or both, as appropriate. |
---|
6839 | */ |
---|
6840 | s = gen_loadx_iphdrlen(); |
---|
6841 | |
---|
6842 | /* |
---|
6843 | * The X register now contains the sum of the length |
---|
6844 | * of any variable-length header preceding the link-layer |
---|
6845 | * header, any variable-length link-layer header, and the |
---|
6846 | * length of the network-layer header. |
---|
6847 | * |
---|
6848 | * Load into the A register the offset relative to |
---|
6849 | * the beginning of the transport layer header, |
---|
6850 | * add the X register to that, move that to the |
---|
6851 | * X register, and load with an offset from the |
---|
6852 | * X register equal to the offset of the network |
---|
6853 | * layer header relative to the beginning of |
---|
6854 | * the MAC-layer payload plus the fixed-length |
---|
6855 | * portion of the offset of the MAC-layer payload |
---|
6856 | * from the beginning of the raw packet data. |
---|
6857 | */ |
---|
6858 | sappend(s, xfer_to_a(inst)); |
---|
6859 | sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X)); |
---|
6860 | sappend(s, new_stmt(BPF_MISC|BPF_TAX)); |
---|
6861 | sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size)); |
---|
6862 | tmp->s.k = off_macpl + off_nl; |
---|
6863 | sappend(inst->s, s); |
---|
6864 | |
---|
6865 | /* |
---|
6866 | * Do the computation only if the packet contains |
---|
6867 | * the protocol in question - which is true only |
---|
6868 | * if this is an IP datagram and is the first or |
---|
6869 | * only fragment of that datagram. |
---|
6870 | */ |
---|
6871 | gen_and(gen_proto_abbrev(proto), b = gen_ipfrag()); |
---|
6872 | if (inst->b) |
---|
6873 | gen_and(inst->b, b); |
---|
6874 | gen_and(gen_proto_abbrev(Q_IP), b); |
---|
6875 | inst->b = b; |
---|
6876 | break; |
---|
6877 | case Q_ICMPV6: |
---|
6878 | bpf_error("IPv6 upper-layer protocol is not supported by proto[x]"); |
---|
6879 | /*NOTREACHED*/ |
---|
6880 | } |
---|
6881 | inst->regno = regno; |
---|
6882 | s = new_stmt(BPF_ST); |
---|
6883 | s->s.k = regno; |
---|
6884 | sappend(inst->s, s); |
---|
6885 | |
---|
6886 | return inst; |
---|
6887 | } |
---|
6888 | |
---|
6889 | struct block * |
---|
6890 | gen_relation(code, a0, a1, reversed) |
---|
6891 | int code; |
---|
6892 | struct arth *a0, *a1; |
---|
6893 | int reversed; |
---|
6894 | { |
---|
6895 | struct slist *s0, *s1, *s2; |
---|
6896 | struct block *b, *tmp; |
---|
6897 | |
---|
6898 | s0 = xfer_to_x(a1); |
---|
6899 | s1 = xfer_to_a(a0); |
---|
6900 | if (code == BPF_JEQ) { |
---|
6901 | s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X); |
---|
6902 | b = new_block(JMP(code)); |
---|
6903 | sappend(s1, s2); |
---|
6904 | } |
---|
6905 | else |
---|
6906 | b = new_block(BPF_JMP|code|BPF_X); |
---|
6907 | if (reversed) |
---|
6908 | gen_not(b); |
---|
6909 | |
---|
6910 | sappend(s0, s1); |
---|
6911 | sappend(a1->s, s0); |
---|
6912 | sappend(a0->s, a1->s); |
---|
6913 | |
---|
6914 | b->stmts = a0->s; |
---|
6915 | |
---|
6916 | free_reg(a0->regno); |
---|
6917 | free_reg(a1->regno); |
---|
6918 | |
---|
6919 | /* 'and' together protocol checks */ |
---|
6920 | if (a0->b) { |
---|
6921 | if (a1->b) { |
---|
6922 | gen_and(a0->b, tmp = a1->b); |
---|
6923 | } |
---|
6924 | else |
---|
6925 | tmp = a0->b; |
---|
6926 | } else |
---|
6927 | tmp = a1->b; |
---|
6928 | |
---|
6929 | if (tmp) |
---|
6930 | gen_and(tmp, b); |
---|
6931 | |
---|
6932 | return b; |
---|
6933 | } |
---|
6934 | |
---|
6935 | struct arth * |
---|
6936 | gen_loadlen() |
---|
6937 | { |
---|
6938 | int regno = alloc_reg(); |
---|
6939 | struct arth *a = (struct arth *)newchunk(sizeof(*a)); |
---|
6940 | struct slist *s; |
---|
6941 | |
---|
6942 | s = new_stmt(BPF_LD|BPF_LEN); |
---|
6943 | s->next = new_stmt(BPF_ST); |
---|
6944 | s->next->s.k = regno; |
---|
6945 | a->s = s; |
---|
6946 | a->regno = regno; |
---|
6947 | |
---|
6948 | return a; |
---|
6949 | } |
---|
6950 | |
---|
6951 | struct arth * |
---|
6952 | gen_loadi(val) |
---|
6953 | int val; |
---|
6954 | { |
---|
6955 | struct arth *a; |
---|
6956 | struct slist *s; |
---|
6957 | int reg; |
---|
6958 | |
---|
6959 | a = (struct arth *)newchunk(sizeof(*a)); |
---|
6960 | |
---|
6961 | reg = alloc_reg(); |
---|
6962 | |
---|
6963 | s = new_stmt(BPF_LD|BPF_IMM); |
---|
6964 | s->s.k = val; |
---|
6965 | s->next = new_stmt(BPF_ST); |
---|
6966 | s->next->s.k = reg; |
---|
6967 | a->s = s; |
---|
6968 | a->regno = reg; |
---|
6969 | |
---|
6970 | return a; |
---|
6971 | } |
---|
6972 | |
---|
6973 | struct arth * |
---|
6974 | gen_neg(a) |
---|
6975 | struct arth *a; |
---|
6976 | { |
---|
6977 | struct slist *s; |
---|
6978 | |
---|
6979 | s = xfer_to_a(a); |
---|
6980 | sappend(a->s, s); |
---|
6981 | s = new_stmt(BPF_ALU|BPF_NEG); |
---|
6982 | s->s.k = 0; |
---|
6983 | sappend(a->s, s); |
---|
6984 | s = new_stmt(BPF_ST); |
---|
6985 | s->s.k = a->regno; |
---|
6986 | sappend(a->s, s); |
---|
6987 | |
---|
6988 | return a; |
---|
6989 | } |
---|
6990 | |
---|
6991 | struct arth * |
---|
6992 | gen_arth(code, a0, a1) |
---|
6993 | int code; |
---|
6994 | struct arth *a0, *a1; |
---|
6995 | { |
---|
6996 | struct slist *s0, *s1, *s2; |
---|
6997 | |
---|
6998 | s0 = xfer_to_x(a1); |
---|
6999 | s1 = xfer_to_a(a0); |
---|
7000 | s2 = new_stmt(BPF_ALU|BPF_X|code); |
---|
7001 | |
---|
7002 | sappend(s1, s2); |
---|
7003 | sappend(s0, s1); |
---|
7004 | sappend(a1->s, s0); |
---|
7005 | sappend(a0->s, a1->s); |
---|
7006 | |
---|
7007 | free_reg(a0->regno); |
---|
7008 | free_reg(a1->regno); |
---|
7009 | |
---|
7010 | s0 = new_stmt(BPF_ST); |
---|
7011 | a0->regno = s0->s.k = alloc_reg(); |
---|
7012 | sappend(a0->s, s0); |
---|
7013 | |
---|
7014 | return a0; |
---|
7015 | } |
---|
7016 | |
---|
7017 | /* |
---|
7018 | * Here we handle simple allocation of the scratch registers. |
---|
7019 | * If too many registers are alloc'd, the allocator punts. |
---|
7020 | */ |
---|
7021 | static int regused[BPF_MEMWORDS]; |
---|
7022 | static int curreg; |
---|
7023 | |
---|
7024 | /* |
---|
7025 | * Initialize the table of used registers and the current register. |
---|
7026 | */ |
---|
7027 | static void |
---|
7028 | init_regs() |
---|
7029 | { |
---|
7030 | curreg = 0; |
---|
7031 | memset(regused, 0, sizeof regused); |
---|
7032 | } |
---|
7033 | |
---|
7034 | /* |
---|
7035 | * Return the next free register. |
---|
7036 | */ |
---|
7037 | static int |
---|
7038 | alloc_reg() |
---|
7039 | { |
---|
7040 | int n = BPF_MEMWORDS; |
---|
7041 | |
---|
7042 | while (--n >= 0) { |
---|
7043 | if (regused[curreg]) |
---|
7044 | curreg = (curreg + 1) % BPF_MEMWORDS; |
---|
7045 | else { |
---|
7046 | regused[curreg] = 1; |
---|
7047 | return curreg; |
---|
7048 | } |
---|
7049 | } |
---|
7050 | bpf_error("too many registers needed to evaluate expression"); |
---|
7051 | /* NOTREACHED */ |
---|
7052 | return 0; |
---|
7053 | } |
---|
7054 | |
---|
7055 | /* |
---|
7056 | * Return a register to the table so it can |
---|
7057 | * be used later. |
---|
7058 | */ |
---|
7059 | static void |
---|
7060 | free_reg(n) |
---|
7061 | int n; |
---|
7062 | { |
---|
7063 | regused[n] = 0; |
---|
7064 | } |
---|
7065 | |
---|
7066 | static struct block * |
---|
7067 | gen_len(jmp, n) |
---|
7068 | int jmp, n; |
---|
7069 | { |
---|
7070 | struct slist *s; |
---|
7071 | struct block *b; |
---|
7072 | |
---|
7073 | s = new_stmt(BPF_LD|BPF_LEN); |
---|
7074 | b = new_block(JMP(jmp)); |
---|
7075 | b->stmts = s; |
---|
7076 | b->s.k = n; |
---|
7077 | |
---|
7078 | return b; |
---|
7079 | } |
---|
7080 | |
---|
7081 | struct block * |
---|
7082 | gen_greater(n) |
---|
7083 | int n; |
---|
7084 | { |
---|
7085 | return gen_len(BPF_JGE, n); |
---|
7086 | } |
---|
7087 | |
---|
7088 | /* |
---|
7089 | * Actually, this is less than or equal. |
---|
7090 | */ |
---|
7091 | struct block * |
---|
7092 | gen_less(n) |
---|
7093 | int n; |
---|
7094 | { |
---|
7095 | struct block *b; |
---|
7096 | |
---|
7097 | b = gen_len(BPF_JGT, n); |
---|
7098 | gen_not(b); |
---|
7099 | |
---|
7100 | return b; |
---|
7101 | } |
---|
7102 | |
---|
7103 | /* |
---|
7104 | * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to |
---|
7105 | * the beginning of the link-layer header. |
---|
7106 | * XXX - that means you can't test values in the radiotap header, but |
---|
7107 | * as that header is difficult if not impossible to parse generally |
---|
7108 | * without a loop, that might not be a severe problem. A new keyword |
---|
7109 | * "radio" could be added for that, although what you'd really want |
---|
7110 | * would be a way of testing particular radio header values, which |
---|
7111 | * would generate code appropriate to the radio header in question. |
---|
7112 | */ |
---|
7113 | struct block * |
---|
7114 | gen_byteop(op, idx, val) |
---|
7115 | int op, idx, val; |
---|
7116 | { |
---|
7117 | struct block *b; |
---|
7118 | struct slist *s; |
---|
7119 | |
---|
7120 | switch (op) { |
---|
7121 | default: |
---|
7122 | abort(); |
---|
7123 | |
---|
7124 | case '=': |
---|
7125 | return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val); |
---|
7126 | |
---|
7127 | case '<': |
---|
7128 | b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val); |
---|
7129 | return b; |
---|
7130 | |
---|
7131 | case '>': |
---|
7132 | b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val); |
---|
7133 | return b; |
---|
7134 | |
---|
7135 | case '|': |
---|
7136 | s = new_stmt(BPF_ALU|BPF_OR|BPF_K); |
---|
7137 | break; |
---|
7138 | |
---|
7139 | case '&': |
---|
7140 | s = new_stmt(BPF_ALU|BPF_AND|BPF_K); |
---|
7141 | break; |
---|
7142 | } |
---|
7143 | s->s.k = val; |
---|
7144 | b = new_block(JMP(BPF_JEQ)); |
---|
7145 | b->stmts = s; |
---|
7146 | gen_not(b); |
---|
7147 | |
---|
7148 | return b; |
---|
7149 | } |
---|
7150 | |
---|
7151 | static u_char abroadcast[] = { 0x0 }; |
---|
7152 | |
---|
7153 | struct block * |
---|
7154 | gen_broadcast(proto) |
---|
7155 | int proto; |
---|
7156 | { |
---|
7157 | bpf_u_int32 hostmask; |
---|
7158 | struct block *b0, *b1, *b2; |
---|
7159 | static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
---|
7160 | |
---|
7161 | switch (proto) { |
---|
7162 | |
---|
7163 | case Q_DEFAULT: |
---|
7164 | case Q_LINK: |
---|
7165 | switch (linktype) { |
---|
7166 | case DLT_ARCNET: |
---|
7167 | case DLT_ARCNET_LINUX: |
---|
7168 | return gen_ahostop(abroadcast, Q_DST); |
---|
7169 | case DLT_EN10MB: |
---|
7170 | case DLT_NETANALYZER: |
---|
7171 | case DLT_NETANALYZER_TRANSPARENT: |
---|
7172 | return gen_ehostop(ebroadcast, Q_DST); |
---|
7173 | case DLT_FDDI: |
---|
7174 | return gen_fhostop(ebroadcast, Q_DST); |
---|
7175 | case DLT_IEEE802: |
---|
7176 | return gen_thostop(ebroadcast, Q_DST); |
---|
7177 | case DLT_IEEE802_11: |
---|
7178 | case DLT_PRISM_HEADER: |
---|
7179 | case DLT_IEEE802_11_RADIO_AVS: |
---|
7180 | case DLT_IEEE802_11_RADIO: |
---|
7181 | case DLT_PPI: |
---|
7182 | return gen_wlanhostop(ebroadcast, Q_DST); |
---|
7183 | case DLT_IP_OVER_FC: |
---|
7184 | return gen_ipfchostop(ebroadcast, Q_DST); |
---|
7185 | case DLT_SUNATM: |
---|
7186 | if (is_lane) { |
---|
7187 | /* |
---|
7188 | * Check that the packet doesn't begin with an |
---|
7189 | * LE Control marker. (We've already generated |
---|
7190 | * a test for LANE.) |
---|
7191 | */ |
---|
7192 | b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, |
---|
7193 | BPF_H, 0xFF00); |
---|
7194 | gen_not(b1); |
---|
7195 | |
---|
7196 | /* |
---|
7197 | * Now check the MAC address. |
---|
7198 | */ |
---|
7199 | b0 = gen_ehostop(ebroadcast, Q_DST); |
---|
7200 | gen_and(b1, b0); |
---|
7201 | return b0; |
---|
7202 | } |
---|
7203 | break; |
---|
7204 | default: |
---|
7205 | bpf_error("not a broadcast link"); |
---|
7206 | } |
---|
7207 | break; |
---|
7208 | |
---|
7209 | case Q_IP: |
---|
7210 | /* |
---|
7211 | * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff) |
---|
7212 | * as an indication that we don't know the netmask, and fail |
---|
7213 | * in that case. |
---|
7214 | */ |
---|
7215 | if (netmask == PCAP_NETMASK_UNKNOWN) |
---|
7216 | bpf_error("netmask not known, so 'ip broadcast' not supported"); |
---|
7217 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
7218 | hostmask = ~netmask; |
---|
7219 | b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask); |
---|
7220 | b2 = gen_mcmp(OR_NET, 16, BPF_W, |
---|
7221 | (bpf_int32)(~0 & hostmask), hostmask); |
---|
7222 | gen_or(b1, b2); |
---|
7223 | gen_and(b0, b2); |
---|
7224 | return b2; |
---|
7225 | } |
---|
7226 | bpf_error("only link-layer/IP broadcast filters supported"); |
---|
7227 | /* NOTREACHED */ |
---|
7228 | return NULL; |
---|
7229 | } |
---|
7230 | |
---|
7231 | /* |
---|
7232 | * Generate code to test the low-order bit of a MAC address (that's |
---|
7233 | * the bottom bit of the *first* byte). |
---|
7234 | */ |
---|
7235 | static struct block * |
---|
7236 | gen_mac_multicast(offset) |
---|
7237 | int offset; |
---|
7238 | { |
---|
7239 | register struct block *b0; |
---|
7240 | register struct slist *s; |
---|
7241 | |
---|
7242 | /* link[offset] & 1 != 0 */ |
---|
7243 | s = gen_load_a(OR_LINK, offset, BPF_B); |
---|
7244 | b0 = new_block(JMP(BPF_JSET)); |
---|
7245 | b0->s.k = 1; |
---|
7246 | b0->stmts = s; |
---|
7247 | return b0; |
---|
7248 | } |
---|
7249 | |
---|
7250 | struct block * |
---|
7251 | gen_multicast(proto) |
---|
7252 | int proto; |
---|
7253 | { |
---|
7254 | register struct block *b0, *b1, *b2; |
---|
7255 | register struct slist *s; |
---|
7256 | |
---|
7257 | switch (proto) { |
---|
7258 | |
---|
7259 | case Q_DEFAULT: |
---|
7260 | case Q_LINK: |
---|
7261 | switch (linktype) { |
---|
7262 | case DLT_ARCNET: |
---|
7263 | case DLT_ARCNET_LINUX: |
---|
7264 | /* all ARCnet multicasts use the same address */ |
---|
7265 | return gen_ahostop(abroadcast, Q_DST); |
---|
7266 | case DLT_EN10MB: |
---|
7267 | case DLT_NETANALYZER: |
---|
7268 | case DLT_NETANALYZER_TRANSPARENT: |
---|
7269 | /* ether[0] & 1 != 0 */ |
---|
7270 | return gen_mac_multicast(0); |
---|
7271 | case DLT_FDDI: |
---|
7272 | /* |
---|
7273 | * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX |
---|
7274 | * |
---|
7275 | * XXX - was that referring to bit-order issues? |
---|
7276 | */ |
---|
7277 | /* fddi[1] & 1 != 0 */ |
---|
7278 | return gen_mac_multicast(1); |
---|
7279 | case DLT_IEEE802: |
---|
7280 | /* tr[2] & 1 != 0 */ |
---|
7281 | return gen_mac_multicast(2); |
---|
7282 | case DLT_IEEE802_11: |
---|
7283 | case DLT_PRISM_HEADER: |
---|
7284 | case DLT_IEEE802_11_RADIO_AVS: |
---|
7285 | case DLT_IEEE802_11_RADIO: |
---|
7286 | case DLT_PPI: |
---|
7287 | /* |
---|
7288 | * Oh, yuk. |
---|
7289 | * |
---|
7290 | * For control frames, there is no DA. |
---|
7291 | * |
---|
7292 | * For management frames, DA is at an |
---|
7293 | * offset of 4 from the beginning of |
---|
7294 | * the packet. |
---|
7295 | * |
---|
7296 | * For data frames, DA is at an offset |
---|
7297 | * of 4 from the beginning of the packet |
---|
7298 | * if To DS is clear and at an offset of |
---|
7299 | * 16 from the beginning of the packet |
---|
7300 | * if To DS is set. |
---|
7301 | */ |
---|
7302 | |
---|
7303 | /* |
---|
7304 | * Generate the tests to be done for data frames. |
---|
7305 | * |
---|
7306 | * First, check for To DS set, i.e. "link[1] & 0x01". |
---|
7307 | */ |
---|
7308 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
7309 | b1 = new_block(JMP(BPF_JSET)); |
---|
7310 | b1->s.k = 0x01; /* To DS */ |
---|
7311 | b1->stmts = s; |
---|
7312 | |
---|
7313 | /* |
---|
7314 | * If To DS is set, the DA is at 16. |
---|
7315 | */ |
---|
7316 | b0 = gen_mac_multicast(16); |
---|
7317 | gen_and(b1, b0); |
---|
7318 | |
---|
7319 | /* |
---|
7320 | * Now, check for To DS not set, i.e. check |
---|
7321 | * "!(link[1] & 0x01)". |
---|
7322 | */ |
---|
7323 | s = gen_load_a(OR_LINK, 1, BPF_B); |
---|
7324 | b2 = new_block(JMP(BPF_JSET)); |
---|
7325 | b2->s.k = 0x01; /* To DS */ |
---|
7326 | b2->stmts = s; |
---|
7327 | gen_not(b2); |
---|
7328 | |
---|
7329 | /* |
---|
7330 | * If To DS is not set, the DA is at 4. |
---|
7331 | */ |
---|
7332 | b1 = gen_mac_multicast(4); |
---|
7333 | gen_and(b2, b1); |
---|
7334 | |
---|
7335 | /* |
---|
7336 | * Now OR together the last two checks. That gives |
---|
7337 | * the complete set of checks for data frames. |
---|
7338 | */ |
---|
7339 | gen_or(b1, b0); |
---|
7340 | |
---|
7341 | /* |
---|
7342 | * Now check for a data frame. |
---|
7343 | * I.e, check "link[0] & 0x08". |
---|
7344 | */ |
---|
7345 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
7346 | b1 = new_block(JMP(BPF_JSET)); |
---|
7347 | b1->s.k = 0x08; |
---|
7348 | b1->stmts = s; |
---|
7349 | |
---|
7350 | /* |
---|
7351 | * AND that with the checks done for data frames. |
---|
7352 | */ |
---|
7353 | gen_and(b1, b0); |
---|
7354 | |
---|
7355 | /* |
---|
7356 | * If the high-order bit of the type value is 0, this |
---|
7357 | * is a management frame. |
---|
7358 | * I.e, check "!(link[0] & 0x08)". |
---|
7359 | */ |
---|
7360 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
7361 | b2 = new_block(JMP(BPF_JSET)); |
---|
7362 | b2->s.k = 0x08; |
---|
7363 | b2->stmts = s; |
---|
7364 | gen_not(b2); |
---|
7365 | |
---|
7366 | /* |
---|
7367 | * For management frames, the DA is at 4. |
---|
7368 | */ |
---|
7369 | b1 = gen_mac_multicast(4); |
---|
7370 | gen_and(b2, b1); |
---|
7371 | |
---|
7372 | /* |
---|
7373 | * OR that with the checks done for data frames. |
---|
7374 | * That gives the checks done for management and |
---|
7375 | * data frames. |
---|
7376 | */ |
---|
7377 | gen_or(b1, b0); |
---|
7378 | |
---|
7379 | /* |
---|
7380 | * If the low-order bit of the type value is 1, |
---|
7381 | * this is either a control frame or a frame |
---|
7382 | * with a reserved type, and thus not a |
---|
7383 | * frame with an SA. |
---|
7384 | * |
---|
7385 | * I.e., check "!(link[0] & 0x04)". |
---|
7386 | */ |
---|
7387 | s = gen_load_a(OR_LINK, 0, BPF_B); |
---|
7388 | b1 = new_block(JMP(BPF_JSET)); |
---|
7389 | b1->s.k = 0x04; |
---|
7390 | b1->stmts = s; |
---|
7391 | gen_not(b1); |
---|
7392 | |
---|
7393 | /* |
---|
7394 | * AND that with the checks for data and management |
---|
7395 | * frames. |
---|
7396 | */ |
---|
7397 | gen_and(b1, b0); |
---|
7398 | return b0; |
---|
7399 | case DLT_IP_OVER_FC: |
---|
7400 | b0 = gen_mac_multicast(2); |
---|
7401 | return b0; |
---|
7402 | case DLT_SUNATM: |
---|
7403 | if (is_lane) { |
---|
7404 | /* |
---|
7405 | * Check that the packet doesn't begin with an |
---|
7406 | * LE Control marker. (We've already generated |
---|
7407 | * a test for LANE.) |
---|
7408 | */ |
---|
7409 | b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, |
---|
7410 | BPF_H, 0xFF00); |
---|
7411 | gen_not(b1); |
---|
7412 | |
---|
7413 | /* ether[off_mac] & 1 != 0 */ |
---|
7414 | b0 = gen_mac_multicast(off_mac); |
---|
7415 | gen_and(b1, b0); |
---|
7416 | return b0; |
---|
7417 | } |
---|
7418 | break; |
---|
7419 | default: |
---|
7420 | break; |
---|
7421 | } |
---|
7422 | /* Link not known to support multicasts */ |
---|
7423 | break; |
---|
7424 | |
---|
7425 | case Q_IP: |
---|
7426 | b0 = gen_linktype(ETHERTYPE_IP); |
---|
7427 | b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224); |
---|
7428 | gen_and(b0, b1); |
---|
7429 | return b1; |
---|
7430 | |
---|
7431 | case Q_IPV6: |
---|
7432 | b0 = gen_linktype(ETHERTYPE_IPV6); |
---|
7433 | b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255); |
---|
7434 | gen_and(b0, b1); |
---|
7435 | return b1; |
---|
7436 | } |
---|
7437 | bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel"); |
---|
7438 | /* NOTREACHED */ |
---|
7439 | return NULL; |
---|
7440 | } |
---|
7441 | |
---|
7442 | /* |
---|
7443 | * Filter on inbound (dir == 0) or outbound (dir == 1) traffic. |
---|
7444 | * Outbound traffic is sent by this machine, while inbound traffic is |
---|
7445 | * sent by a remote machine (and may include packets destined for a |
---|
7446 | * unicast or multicast link-layer address we are not subscribing to). |
---|
7447 | * These are the same definitions implemented by pcap_setdirection(). |
---|
7448 | * Capturing only unicast traffic destined for this host is probably |
---|
7449 | * better accomplished using a higher-layer filter. |
---|
7450 | */ |
---|
7451 | struct block * |
---|
7452 | gen_inbound(dir) |
---|
7453 | int dir; |
---|
7454 | { |
---|
7455 | register struct block *b0; |
---|
7456 | |
---|
7457 | /* |
---|
7458 | * Only some data link types support inbound/outbound qualifiers. |
---|
7459 | */ |
---|
7460 | switch (linktype) { |
---|
7461 | case DLT_SLIP: |
---|
7462 | b0 = gen_relation(BPF_JEQ, |
---|
7463 | gen_load(Q_LINK, gen_loadi(0), 1), |
---|
7464 | gen_loadi(0), |
---|
7465 | dir); |
---|
7466 | break; |
---|
7467 | |
---|
7468 | case DLT_IPNET: |
---|
7469 | if (dir) { |
---|
7470 | /* match outgoing packets */ |
---|
7471 | b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND); |
---|
7472 | } else { |
---|
7473 | /* match incoming packets */ |
---|
7474 | b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND); |
---|
7475 | } |
---|
7476 | break; |
---|
7477 | |
---|
7478 | case DLT_LINUX_SLL: |
---|
7479 | /* match outgoing packets */ |
---|
7480 | b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING); |
---|
7481 | if (!dir) { |
---|
7482 | /* to filter on inbound traffic, invert the match */ |
---|
7483 | gen_not(b0); |
---|
7484 | } |
---|
7485 | break; |
---|
7486 | |
---|
7487 | #ifdef HAVE_NET_PFVAR_H |
---|
7488 | case DLT_PFLOG: |
---|
7489 | b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B, |
---|
7490 | (bpf_int32)((dir == 0) ? PF_IN : PF_OUT)); |
---|
7491 | break; |
---|
7492 | #endif |
---|
7493 | |
---|
7494 | case DLT_PPP_PPPD: |
---|
7495 | if (dir) { |
---|
7496 | /* match outgoing packets */ |
---|
7497 | b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT); |
---|
7498 | } else { |
---|
7499 | /* match incoming packets */ |
---|
7500 | b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN); |
---|
7501 | } |
---|
7502 | break; |
---|
7503 | |
---|
7504 | case DLT_JUNIPER_MFR: |
---|
7505 | case DLT_JUNIPER_MLFR: |
---|
7506 | case DLT_JUNIPER_MLPPP: |
---|
7507 | case DLT_JUNIPER_ATM1: |
---|
7508 | case DLT_JUNIPER_ATM2: |
---|
7509 | case DLT_JUNIPER_PPPOE: |
---|
7510 | case DLT_JUNIPER_PPPOE_ATM: |
---|
7511 | case DLT_JUNIPER_GGSN: |
---|
7512 | case DLT_JUNIPER_ES: |
---|
7513 | case DLT_JUNIPER_MONITOR: |
---|
7514 | case DLT_JUNIPER_SERVICES: |
---|
7515 | case DLT_JUNIPER_ETHER: |
---|
7516 | case DLT_JUNIPER_PPP: |
---|
7517 | case DLT_JUNIPER_FRELAY: |
---|
7518 | case DLT_JUNIPER_CHDLC: |
---|
7519 | case DLT_JUNIPER_VP: |
---|
7520 | case DLT_JUNIPER_ST: |
---|
7521 | case DLT_JUNIPER_ISM: |
---|
7522 | case DLT_JUNIPER_VS: |
---|
7523 | case DLT_JUNIPER_SRX_E2E: |
---|
7524 | case DLT_JUNIPER_FIBRECHANNEL: |
---|
7525 | case DLT_JUNIPER_ATM_CEMIC: |
---|
7526 | |
---|
7527 | /* juniper flags (including direction) are stored |
---|
7528 | * the byte after the 3-byte magic number */ |
---|
7529 | if (dir) { |
---|
7530 | /* match outgoing packets */ |
---|
7531 | b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01); |
---|
7532 | } else { |
---|
7533 | /* match incoming packets */ |
---|
7534 | b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01); |
---|
7535 | } |
---|
7536 | break; |
---|
7537 | |
---|
7538 | default: |
---|
7539 | /* |
---|
7540 | * If we have packet meta-data indicating a direction, |
---|
7541 | * check it, otherwise give up as this link-layer type |
---|
7542 | * has nothing in the packet data. |
---|
7543 | */ |
---|
7544 | #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER) |
---|
7545 | /* |
---|
7546 | * We infer that this is Linux with PF_PACKET support. |
---|
7547 | * If this is a *live* capture, we can look at |
---|
7548 | * special meta-data in the filter expression; |
---|
7549 | * if it's a savefile, we can't. |
---|
7550 | */ |
---|
7551 | if (bpf_pcap->sf.rfile != NULL) { |
---|
7552 | /* We have a FILE *, so this is a savefile */ |
---|
7553 | bpf_error("inbound/outbound not supported on linktype %d when reading savefiles", |
---|
7554 | linktype); |
---|
7555 | b0 = NULL; |
---|
7556 | /* NOTREACHED */ |
---|
7557 | } |
---|
7558 | /* match outgoing packets */ |
---|
7559 | b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H, |
---|
7560 | PACKET_OUTGOING); |
---|
7561 | if (!dir) { |
---|
7562 | /* to filter on inbound traffic, invert the match */ |
---|
7563 | gen_not(b0); |
---|
7564 | } |
---|
7565 | #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */ |
---|
7566 | bpf_error("inbound/outbound not supported on linktype %d", |
---|
7567 | linktype); |
---|
7568 | b0 = NULL; |
---|
7569 | /* NOTREACHED */ |
---|
7570 | #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */ |
---|
7571 | } |
---|
7572 | return (b0); |
---|
7573 | } |
---|
7574 | |
---|
7575 | #ifdef HAVE_NET_PFVAR_H |
---|
7576 | /* PF firewall log matched interface */ |
---|
7577 | struct block * |
---|
7578 | gen_pf_ifname(const char *ifname) |
---|
7579 | { |
---|
7580 | struct block *b0; |
---|
7581 | u_int len, off; |
---|
7582 | |
---|
7583 | if (linktype != DLT_PFLOG) { |
---|
7584 | bpf_error("ifname supported only on PF linktype"); |
---|
7585 | /* NOTREACHED */ |
---|
7586 | } |
---|
7587 | len = sizeof(((struct pfloghdr *)0)->ifname); |
---|
7588 | off = offsetof(struct pfloghdr, ifname); |
---|
7589 | if (strlen(ifname) >= len) { |
---|
7590 | bpf_error("ifname interface names can only be %d characters", |
---|
7591 | len-1); |
---|
7592 | /* NOTREACHED */ |
---|
7593 | } |
---|
7594 | b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname); |
---|
7595 | return (b0); |
---|
7596 | } |
---|
7597 | |
---|
7598 | /* PF firewall log ruleset name */ |
---|
7599 | struct block * |
---|
7600 | gen_pf_ruleset(char *ruleset) |
---|
7601 | { |
---|
7602 | struct block *b0; |
---|
7603 | |
---|
7604 | if (linktype != DLT_PFLOG) { |
---|
7605 | bpf_error("ruleset supported only on PF linktype"); |
---|
7606 | /* NOTREACHED */ |
---|
7607 | } |
---|
7608 | |
---|
7609 | if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { |
---|
7610 | bpf_error("ruleset names can only be %ld characters", |
---|
7611 | (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1)); |
---|
7612 | /* NOTREACHED */ |
---|
7613 | } |
---|
7614 | |
---|
7615 | b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset), |
---|
7616 | strlen(ruleset), (const u_char *)ruleset); |
---|
7617 | return (b0); |
---|
7618 | } |
---|
7619 | |
---|
7620 | /* PF firewall log rule number */ |
---|
7621 | struct block * |
---|
7622 | gen_pf_rnr(int rnr) |
---|
7623 | { |
---|
7624 | struct block *b0; |
---|
7625 | |
---|
7626 | if (linktype != DLT_PFLOG) { |
---|
7627 | bpf_error("rnr supported only on PF linktype"); |
---|
7628 | /* NOTREACHED */ |
---|
7629 | } |
---|
7630 | |
---|
7631 | b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W, |
---|
7632 | (bpf_int32)rnr); |
---|
7633 | return (b0); |
---|
7634 | } |
---|
7635 | |
---|
7636 | /* PF firewall log sub-rule number */ |
---|
7637 | struct block * |
---|
7638 | gen_pf_srnr(int srnr) |
---|
7639 | { |
---|
7640 | struct block *b0; |
---|
7641 | |
---|
7642 | if (linktype != DLT_PFLOG) { |
---|
7643 | bpf_error("srnr supported only on PF linktype"); |
---|
7644 | /* NOTREACHED */ |
---|
7645 | } |
---|
7646 | |
---|
7647 | b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W, |
---|
7648 | (bpf_int32)srnr); |
---|
7649 | return (b0); |
---|
7650 | } |
---|
7651 | |
---|
7652 | /* PF firewall log reason code */ |
---|
7653 | struct block * |
---|
7654 | gen_pf_reason(int reason) |
---|
7655 | { |
---|
7656 | struct block *b0; |
---|
7657 | |
---|
7658 | if (linktype != DLT_PFLOG) { |
---|
7659 | bpf_error("reason supported only on PF linktype"); |
---|
7660 | /* NOTREACHED */ |
---|
7661 | } |
---|
7662 | |
---|
7663 | b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B, |
---|
7664 | (bpf_int32)reason); |
---|
7665 | return (b0); |
---|
7666 | } |
---|
7667 | |
---|
7668 | /* PF firewall log action */ |
---|
7669 | struct block * |
---|
7670 | gen_pf_action(int action) |
---|
7671 | { |
---|
7672 | struct block *b0; |
---|
7673 | |
---|
7674 | if (linktype != DLT_PFLOG) { |
---|
7675 | bpf_error("action supported only on PF linktype"); |
---|
7676 | /* NOTREACHED */ |
---|
7677 | } |
---|
7678 | |
---|
7679 | b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B, |
---|
7680 | (bpf_int32)action); |
---|
7681 | return (b0); |
---|
7682 | } |
---|
7683 | #else /* !HAVE_NET_PFVAR_H */ |
---|
7684 | struct block * |
---|
7685 | gen_pf_ifname(const char *ifname) |
---|
7686 | { |
---|
7687 | bpf_error("libpcap was compiled without pf support"); |
---|
7688 | /* NOTREACHED */ |
---|
7689 | return (NULL); |
---|
7690 | } |
---|
7691 | |
---|
7692 | struct block * |
---|
7693 | gen_pf_ruleset(char *ruleset) |
---|
7694 | { |
---|
7695 | bpf_error("libpcap was compiled on a machine without pf support"); |
---|
7696 | /* NOTREACHED */ |
---|
7697 | return (NULL); |
---|
7698 | } |
---|
7699 | |
---|
7700 | struct block * |
---|
7701 | gen_pf_rnr(int rnr) |
---|
7702 | { |
---|
7703 | bpf_error("libpcap was compiled on a machine without pf support"); |
---|
7704 | /* NOTREACHED */ |
---|
7705 | return (NULL); |
---|
7706 | } |
---|
7707 | |
---|
7708 | struct block * |
---|
7709 | gen_pf_srnr(int srnr) |
---|
7710 | { |
---|
7711 | bpf_error("libpcap was compiled on a machine without pf support"); |
---|
7712 | /* NOTREACHED */ |
---|
7713 | return (NULL); |
---|
7714 | } |
---|
7715 | |
---|
7716 | struct block * |
---|
7717 | gen_pf_reason(int reason) |
---|
7718 | { |
---|
7719 | bpf_error("libpcap was compiled on a machine without pf support"); |
---|
7720 | /* NOTREACHED */ |
---|
7721 | return (NULL); |
---|
7722 | } |
---|
7723 | |
---|
7724 | struct block * |
---|
7725 | gen_pf_action(int action) |
---|
7726 | { |
---|
7727 | bpf_error("libpcap was compiled on a machine without pf support"); |
---|
7728 | /* NOTREACHED */ |
---|
7729 | return (NULL); |
---|
7730 | } |
---|
7731 | #endif /* HAVE_NET_PFVAR_H */ |
---|
7732 | |
---|
7733 | /* IEEE 802.11 wireless header */ |
---|
7734 | struct block * |
---|
7735 | gen_p80211_type(int type, int mask) |
---|
7736 | { |
---|
7737 | struct block *b0; |
---|
7738 | |
---|
7739 | switch (linktype) { |
---|
7740 | |
---|
7741 | case DLT_IEEE802_11: |
---|
7742 | case DLT_PRISM_HEADER: |
---|
7743 | case DLT_IEEE802_11_RADIO_AVS: |
---|
7744 | case DLT_IEEE802_11_RADIO: |
---|
7745 | b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type, |
---|
7746 | (bpf_int32)mask); |
---|
7747 | break; |
---|
7748 | |
---|
7749 | default: |
---|
7750 | bpf_error("802.11 link-layer types supported only on 802.11"); |
---|
7751 | /* NOTREACHED */ |
---|
7752 | } |
---|
7753 | |
---|
7754 | return (b0); |
---|
7755 | } |
---|
7756 | |
---|
7757 | struct block * |
---|
7758 | gen_p80211_fcdir(int fcdir) |
---|
7759 | { |
---|
7760 | struct block *b0; |
---|
7761 | |
---|
7762 | switch (linktype) { |
---|
7763 | |
---|
7764 | case DLT_IEEE802_11: |
---|
7765 | case DLT_PRISM_HEADER: |
---|
7766 | case DLT_IEEE802_11_RADIO_AVS: |
---|
7767 | case DLT_IEEE802_11_RADIO: |
---|
7768 | break; |
---|
7769 | |
---|
7770 | default: |
---|
7771 | bpf_error("frame direction supported only with 802.11 headers"); |
---|
7772 | /* NOTREACHED */ |
---|
7773 | } |
---|
7774 | |
---|
7775 | b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir, |
---|
7776 | (bpf_u_int32)IEEE80211_FC1_DIR_MASK); |
---|
7777 | |
---|
7778 | return (b0); |
---|
7779 | } |
---|
7780 | |
---|
7781 | struct block * |
---|
7782 | gen_acode(eaddr, q) |
---|
7783 | register const u_char *eaddr; |
---|
7784 | struct qual q; |
---|
7785 | { |
---|
7786 | switch (linktype) { |
---|
7787 | |
---|
7788 | case DLT_ARCNET: |
---|
7789 | case DLT_ARCNET_LINUX: |
---|
7790 | if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && |
---|
7791 | q.proto == Q_LINK) |
---|
7792 | return (gen_ahostop(eaddr, (int)q.dir)); |
---|
7793 | else { |
---|
7794 | bpf_error("ARCnet address used in non-arc expression"); |
---|
7795 | /* NOTREACHED */ |
---|
7796 | } |
---|
7797 | break; |
---|
7798 | |
---|
7799 | default: |
---|
7800 | bpf_error("aid supported only on ARCnet"); |
---|
7801 | /* NOTREACHED */ |
---|
7802 | } |
---|
7803 | bpf_error("ARCnet address used in non-arc expression"); |
---|
7804 | /* NOTREACHED */ |
---|
7805 | return NULL; |
---|
7806 | } |
---|
7807 | |
---|
7808 | static struct block * |
---|
7809 | gen_ahostop(eaddr, dir) |
---|
7810 | register const u_char *eaddr; |
---|
7811 | register int dir; |
---|
7812 | { |
---|
7813 | register struct block *b0, *b1; |
---|
7814 | |
---|
7815 | switch (dir) { |
---|
7816 | /* src comes first, different from Ethernet */ |
---|
7817 | case Q_SRC: |
---|
7818 | return gen_bcmp(OR_LINK, 0, 1, eaddr); |
---|
7819 | |
---|
7820 | case Q_DST: |
---|
7821 | return gen_bcmp(OR_LINK, 1, 1, eaddr); |
---|
7822 | |
---|
7823 | case Q_AND: |
---|
7824 | b0 = gen_ahostop(eaddr, Q_SRC); |
---|
7825 | b1 = gen_ahostop(eaddr, Q_DST); |
---|
7826 | gen_and(b0, b1); |
---|
7827 | return b1; |
---|
7828 | |
---|
7829 | case Q_DEFAULT: |
---|
7830 | case Q_OR: |
---|
7831 | b0 = gen_ahostop(eaddr, Q_SRC); |
---|
7832 | b1 = gen_ahostop(eaddr, Q_DST); |
---|
7833 | gen_or(b0, b1); |
---|
7834 | return b1; |
---|
7835 | |
---|
7836 | case Q_ADDR1: |
---|
7837 | bpf_error("'addr1' is only supported on 802.11"); |
---|
7838 | break; |
---|
7839 | |
---|
7840 | case Q_ADDR2: |
---|
7841 | bpf_error("'addr2' is only supported on 802.11"); |
---|
7842 | break; |
---|
7843 | |
---|
7844 | case Q_ADDR3: |
---|
7845 | bpf_error("'addr3' is only supported on 802.11"); |
---|
7846 | break; |
---|
7847 | |
---|
7848 | case Q_ADDR4: |
---|
7849 | bpf_error("'addr4' is only supported on 802.11"); |
---|
7850 | break; |
---|
7851 | |
---|
7852 | case Q_RA: |
---|
7853 | bpf_error("'ra' is only supported on 802.11"); |
---|
7854 | break; |
---|
7855 | |
---|
7856 | case Q_TA: |
---|
7857 | bpf_error("'ta' is only supported on 802.11"); |
---|
7858 | break; |
---|
7859 | } |
---|
7860 | abort(); |
---|
7861 | /* NOTREACHED */ |
---|
7862 | } |
---|
7863 | |
---|
7864 | /* |
---|
7865 | * support IEEE 802.1Q VLAN trunk over ethernet |
---|
7866 | */ |
---|
7867 | struct block * |
---|
7868 | gen_vlan(vlan_num) |
---|
7869 | int vlan_num; |
---|
7870 | { |
---|
7871 | struct block *b0, *b1; |
---|
7872 | |
---|
7873 | /* can't check for VLAN-encapsulated packets inside MPLS */ |
---|
7874 | if (label_stack_depth > 0) |
---|
7875 | bpf_error("no VLAN match after MPLS"); |
---|
7876 | |
---|
7877 | /* |
---|
7878 | * Check for a VLAN packet, and then change the offsets to point |
---|
7879 | * to the type and data fields within the VLAN packet. Just |
---|
7880 | * increment the offsets, so that we can support a hierarchy, e.g. |
---|
7881 | * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within |
---|
7882 | * VLAN 100. |
---|
7883 | * |
---|
7884 | * XXX - this is a bit of a kludge. If we were to split the |
---|
7885 | * compiler into a parser that parses an expression and |
---|
7886 | * generates an expression tree, and a code generator that |
---|
7887 | * takes an expression tree (which could come from our |
---|
7888 | * parser or from some other parser) and generates BPF code, |
---|
7889 | * we could perhaps make the offsets parameters of routines |
---|
7890 | * and, in the handler for an "AND" node, pass to subnodes |
---|
7891 | * other than the VLAN node the adjusted offsets. |
---|
7892 | * |
---|
7893 | * This would mean that "vlan" would, instead of changing the |
---|
7894 | * behavior of *all* tests after it, change only the behavior |
---|
7895 | * of tests ANDed with it. That would change the documented |
---|
7896 | * semantics of "vlan", which might break some expressions. |
---|
7897 | * However, it would mean that "(vlan and ip) or ip" would check |
---|
7898 | * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than |
---|
7899 | * checking only for VLAN-encapsulated IP, so that could still |
---|
7900 | * be considered worth doing; it wouldn't break expressions |
---|
7901 | * that are of the form "vlan and ..." or "vlan N and ...", |
---|
7902 | * which I suspect are the most common expressions involving |
---|
7903 | * "vlan". "vlan or ..." doesn't necessarily do what the user |
---|
7904 | * would really want, now, as all the "or ..." tests would |
---|
7905 | * be done assuming a VLAN, even though the "or" could be viewed |
---|
7906 | * as meaning "or, if this isn't a VLAN packet...". |
---|
7907 | */ |
---|
7908 | orig_nl = off_nl; |
---|
7909 | |
---|
7910 | switch (linktype) { |
---|
7911 | |
---|
7912 | case DLT_EN10MB: |
---|
7913 | case DLT_NETANALYZER: |
---|
7914 | case DLT_NETANALYZER_TRANSPARENT: |
---|
7915 | /* check for VLAN, including QinQ */ |
---|
7916 | b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
7917 | (bpf_int32)ETHERTYPE_8021Q); |
---|
7918 | b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, |
---|
7919 | (bpf_int32)ETHERTYPE_8021QINQ); |
---|
7920 | gen_or(b0,b1); |
---|
7921 | b0 = b1; |
---|
7922 | |
---|
7923 | /* If a specific VLAN is requested, check VLAN id */ |
---|
7924 | if (vlan_num >= 0) { |
---|
7925 | b1 = gen_mcmp(OR_MACPL, 0, BPF_H, |
---|
7926 | (bpf_int32)vlan_num, 0x0fff); |
---|
7927 | gen_and(b0, b1); |
---|
7928 | b0 = b1; |
---|
7929 | } |
---|
7930 | |
---|
7931 | off_macpl += 4; |
---|
7932 | off_linktype += 4; |
---|
7933 | #if 0 |
---|
7934 | off_nl_nosnap += 4; |
---|
7935 | off_nl += 4; |
---|
7936 | #endif |
---|
7937 | break; |
---|
7938 | |
---|
7939 | default: |
---|
7940 | bpf_error("no VLAN support for data link type %d", |
---|
7941 | linktype); |
---|
7942 | /*NOTREACHED*/ |
---|
7943 | } |
---|
7944 | |
---|
7945 | return (b0); |
---|
7946 | } |
---|
7947 | |
---|
7948 | /* |
---|
7949 | * support for MPLS |
---|
7950 | */ |
---|
7951 | struct block * |
---|
7952 | gen_mpls(label_num) |
---|
7953 | int label_num; |
---|
7954 | { |
---|
7955 | struct block *b0,*b1; |
---|
7956 | |
---|
7957 | /* |
---|
7958 | * Change the offsets to point to the type and data fields within |
---|
7959 | * the MPLS packet. Just increment the offsets, so that we |
---|
7960 | * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to |
---|
7961 | * capture packets with an outer label of 100000 and an inner |
---|
7962 | * label of 1024. |
---|
7963 | * |
---|
7964 | * XXX - this is a bit of a kludge. See comments in gen_vlan(). |
---|
7965 | */ |
---|
7966 | orig_nl = off_nl; |
---|
7967 | |
---|
7968 | if (label_stack_depth > 0) { |
---|
7969 | /* just match the bottom-of-stack bit clear */ |
---|
7970 | b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01); |
---|
7971 | } else { |
---|
7972 | /* |
---|
7973 | * Indicate that we're checking MPLS-encapsulated headers, |
---|
7974 | * to make sure higher level code generators don't try to |
---|
7975 | * match against IP-related protocols such as Q_ARP, Q_RARP |
---|
7976 | * etc. |
---|
7977 | */ |
---|
7978 | switch (linktype) { |
---|
7979 | |
---|
7980 | case DLT_C_HDLC: /* fall through */ |
---|
7981 | case DLT_EN10MB: |
---|
7982 | case DLT_NETANALYZER: |
---|
7983 | case DLT_NETANALYZER_TRANSPARENT: |
---|
7984 | b0 = gen_linktype(ETHERTYPE_MPLS); |
---|
7985 | break; |
---|
7986 | |
---|
7987 | case DLT_PPP: |
---|
7988 | b0 = gen_linktype(PPP_MPLS_UCAST); |
---|
7989 | break; |
---|
7990 | |
---|
7991 | /* FIXME add other DLT_s ... |
---|
7992 | * for Frame-Relay/and ATM this may get messy due to SNAP headers |
---|
7993 | * leave it for now */ |
---|
7994 | |
---|
7995 | default: |
---|
7996 | bpf_error("no MPLS support for data link type %d", |
---|
7997 | linktype); |
---|
7998 | b0 = NULL; |
---|
7999 | /*NOTREACHED*/ |
---|
8000 | break; |
---|
8001 | } |
---|
8002 | } |
---|
8003 | |
---|
8004 | /* If a specific MPLS label is requested, check it */ |
---|
8005 | if (label_num >= 0) { |
---|
8006 | label_num = label_num << 12; /* label is shifted 12 bits on the wire */ |
---|
8007 | b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num, |
---|
8008 | 0xfffff000); /* only compare the first 20 bits */ |
---|
8009 | gen_and(b0, b1); |
---|
8010 | b0 = b1; |
---|
8011 | } |
---|
8012 | |
---|
8013 | off_nl_nosnap += 4; |
---|
8014 | off_nl += 4; |
---|
8015 | label_stack_depth++; |
---|
8016 | return (b0); |
---|
8017 | } |
---|
8018 | |
---|
8019 | /* |
---|
8020 | * Support PPPOE discovery and session. |
---|
8021 | */ |
---|
8022 | struct block * |
---|
8023 | gen_pppoed() |
---|
8024 | { |
---|
8025 | /* check for PPPoE discovery */ |
---|
8026 | return gen_linktype((bpf_int32)ETHERTYPE_PPPOED); |
---|
8027 | } |
---|
8028 | |
---|
8029 | struct block * |
---|
8030 | gen_pppoes() |
---|
8031 | { |
---|
8032 | struct block *b0; |
---|
8033 | |
---|
8034 | /* |
---|
8035 | * Test against the PPPoE session link-layer type. |
---|
8036 | */ |
---|
8037 | b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES); |
---|
8038 | |
---|
8039 | /* |
---|
8040 | * Change the offsets to point to the type and data fields within |
---|
8041 | * the PPP packet, and note that this is PPPoE rather than |
---|
8042 | * raw PPP. |
---|
8043 | * |
---|
8044 | * XXX - this is a bit of a kludge. If we were to split the |
---|
8045 | * compiler into a parser that parses an expression and |
---|
8046 | * generates an expression tree, and a code generator that |
---|
8047 | * takes an expression tree (which could come from our |
---|
8048 | * parser or from some other parser) and generates BPF code, |
---|
8049 | * we could perhaps make the offsets parameters of routines |
---|
8050 | * and, in the handler for an "AND" node, pass to subnodes |
---|
8051 | * other than the PPPoE node the adjusted offsets. |
---|
8052 | * |
---|
8053 | * This would mean that "pppoes" would, instead of changing the |
---|
8054 | * behavior of *all* tests after it, change only the behavior |
---|
8055 | * of tests ANDed with it. That would change the documented |
---|
8056 | * semantics of "pppoes", which might break some expressions. |
---|
8057 | * However, it would mean that "(pppoes and ip) or ip" would check |
---|
8058 | * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than |
---|
8059 | * checking only for VLAN-encapsulated IP, so that could still |
---|
8060 | * be considered worth doing; it wouldn't break expressions |
---|
8061 | * that are of the form "pppoes and ..." which I suspect are the |
---|
8062 | * most common expressions involving "pppoes". "pppoes or ..." |
---|
8063 | * doesn't necessarily do what the user would really want, now, |
---|
8064 | * as all the "or ..." tests would be done assuming PPPoE, even |
---|
8065 | * though the "or" could be viewed as meaning "or, if this isn't |
---|
8066 | * a PPPoE packet...". |
---|
8067 | */ |
---|
8068 | orig_linktype = off_linktype; /* save original values */ |
---|
8069 | orig_nl = off_nl; |
---|
8070 | is_pppoes = 1; |
---|
8071 | |
---|
8072 | /* |
---|
8073 | * The "network-layer" protocol is PPPoE, which has a 6-byte |
---|
8074 | * PPPoE header, followed by a PPP packet. |
---|
8075 | * |
---|
8076 | * There is no HDLC encapsulation for the PPP packet (it's |
---|
8077 | * encapsulated in PPPoES instead), so the link-layer type |
---|
8078 | * starts at the first byte of the PPP packet. For PPPoE, |
---|
8079 | * that offset is relative to the beginning of the total |
---|
8080 | * link-layer payload, including any 802.2 LLC header, so |
---|
8081 | * it's 6 bytes past off_nl. |
---|
8082 | */ |
---|
8083 | off_linktype = off_nl + 6; |
---|
8084 | |
---|
8085 | /* |
---|
8086 | * The network-layer offsets are relative to the beginning |
---|
8087 | * of the MAC-layer payload; that's past the 6-byte |
---|
8088 | * PPPoE header and the 2-byte PPP header. |
---|
8089 | */ |
---|
8090 | off_nl = 6+2; |
---|
8091 | off_nl_nosnap = 6+2; |
---|
8092 | |
---|
8093 | return b0; |
---|
8094 | } |
---|
8095 | |
---|
8096 | struct block * |
---|
8097 | gen_atmfield_code(atmfield, jvalue, jtype, reverse) |
---|
8098 | int atmfield; |
---|
8099 | bpf_int32 jvalue; |
---|
8100 | bpf_u_int32 jtype; |
---|
8101 | int reverse; |
---|
8102 | { |
---|
8103 | struct block *b0; |
---|
8104 | |
---|
8105 | switch (atmfield) { |
---|
8106 | |
---|
8107 | case A_VPI: |
---|
8108 | if (!is_atm) |
---|
8109 | bpf_error("'vpi' supported only on raw ATM"); |
---|
8110 | if (off_vpi == (u_int)-1) |
---|
8111 | abort(); |
---|
8112 | b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype, |
---|
8113 | reverse, jvalue); |
---|
8114 | break; |
---|
8115 | |
---|
8116 | case A_VCI: |
---|
8117 | if (!is_atm) |
---|
8118 | bpf_error("'vci' supported only on raw ATM"); |
---|
8119 | if (off_vci == (u_int)-1) |
---|
8120 | abort(); |
---|
8121 | b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype, |
---|
8122 | reverse, jvalue); |
---|
8123 | break; |
---|
8124 | |
---|
8125 | case A_PROTOTYPE: |
---|
8126 | if (off_proto == (u_int)-1) |
---|
8127 | abort(); /* XXX - this isn't on FreeBSD */ |
---|
8128 | b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype, |
---|
8129 | reverse, jvalue); |
---|
8130 | break; |
---|
8131 | |
---|
8132 | case A_MSGTYPE: |
---|
8133 | if (off_payload == (u_int)-1) |
---|
8134 | abort(); |
---|
8135 | b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B, |
---|
8136 | 0xffffffff, jtype, reverse, jvalue); |
---|
8137 | break; |
---|
8138 | |
---|
8139 | case A_CALLREFTYPE: |
---|
8140 | if (!is_atm) |
---|
8141 | bpf_error("'callref' supported only on raw ATM"); |
---|
8142 | if (off_proto == (u_int)-1) |
---|
8143 | abort(); |
---|
8144 | b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff, |
---|
8145 | jtype, reverse, jvalue); |
---|
8146 | break; |
---|
8147 | |
---|
8148 | default: |
---|
8149 | abort(); |
---|
8150 | } |
---|
8151 | return b0; |
---|
8152 | } |
---|
8153 | |
---|
8154 | struct block * |
---|
8155 | gen_atmtype_abbrev(type) |
---|
8156 | int type; |
---|
8157 | { |
---|
8158 | struct block *b0, *b1; |
---|
8159 | |
---|
8160 | switch (type) { |
---|
8161 | |
---|
8162 | case A_METAC: |
---|
8163 | /* Get all packets in Meta signalling Circuit */ |
---|
8164 | if (!is_atm) |
---|
8165 | bpf_error("'metac' supported only on raw ATM"); |
---|
8166 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8167 | b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0); |
---|
8168 | gen_and(b0, b1); |
---|
8169 | break; |
---|
8170 | |
---|
8171 | case A_BCC: |
---|
8172 | /* Get all packets in Broadcast Circuit*/ |
---|
8173 | if (!is_atm) |
---|
8174 | bpf_error("'bcc' supported only on raw ATM"); |
---|
8175 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8176 | b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0); |
---|
8177 | gen_and(b0, b1); |
---|
8178 | break; |
---|
8179 | |
---|
8180 | case A_OAMF4SC: |
---|
8181 | /* Get all cells in Segment OAM F4 circuit*/ |
---|
8182 | if (!is_atm) |
---|
8183 | bpf_error("'oam4sc' supported only on raw ATM"); |
---|
8184 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8185 | b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0); |
---|
8186 | gen_and(b0, b1); |
---|
8187 | break; |
---|
8188 | |
---|
8189 | case A_OAMF4EC: |
---|
8190 | /* Get all cells in End-to-End OAM F4 Circuit*/ |
---|
8191 | if (!is_atm) |
---|
8192 | bpf_error("'oam4ec' supported only on raw ATM"); |
---|
8193 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8194 | b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0); |
---|
8195 | gen_and(b0, b1); |
---|
8196 | break; |
---|
8197 | |
---|
8198 | case A_SC: |
---|
8199 | /* Get all packets in connection Signalling Circuit */ |
---|
8200 | if (!is_atm) |
---|
8201 | bpf_error("'sc' supported only on raw ATM"); |
---|
8202 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8203 | b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0); |
---|
8204 | gen_and(b0, b1); |
---|
8205 | break; |
---|
8206 | |
---|
8207 | case A_ILMIC: |
---|
8208 | /* Get all packets in ILMI Circuit */ |
---|
8209 | if (!is_atm) |
---|
8210 | bpf_error("'ilmic' supported only on raw ATM"); |
---|
8211 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8212 | b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0); |
---|
8213 | gen_and(b0, b1); |
---|
8214 | break; |
---|
8215 | |
---|
8216 | case A_LANE: |
---|
8217 | /* Get all LANE packets */ |
---|
8218 | if (!is_atm) |
---|
8219 | bpf_error("'lane' supported only on raw ATM"); |
---|
8220 | b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0); |
---|
8221 | |
---|
8222 | /* |
---|
8223 | * Arrange that all subsequent tests assume LANE |
---|
8224 | * rather than LLC-encapsulated packets, and set |
---|
8225 | * the offsets appropriately for LANE-encapsulated |
---|
8226 | * Ethernet. |
---|
8227 | * |
---|
8228 | * "off_mac" is the offset of the Ethernet header, |
---|
8229 | * which is 2 bytes past the ATM pseudo-header |
---|
8230 | * (skipping the pseudo-header and 2-byte LE Client |
---|
8231 | * field). The other offsets are Ethernet offsets |
---|
8232 | * relative to "off_mac". |
---|
8233 | */ |
---|
8234 | is_lane = 1; |
---|
8235 | off_mac = off_payload + 2; /* MAC header */ |
---|
8236 | off_linktype = off_mac + 12; |
---|
8237 | off_macpl = off_mac + 14; /* Ethernet */ |
---|
8238 | off_nl = 0; /* Ethernet II */ |
---|
8239 | off_nl_nosnap = 3; /* 802.3+802.2 */ |
---|
8240 | break; |
---|
8241 | |
---|
8242 | case A_LLC: |
---|
8243 | /* Get all LLC-encapsulated packets */ |
---|
8244 | if (!is_atm) |
---|
8245 | bpf_error("'llc' supported only on raw ATM"); |
---|
8246 | b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); |
---|
8247 | is_lane = 0; |
---|
8248 | break; |
---|
8249 | |
---|
8250 | default: |
---|
8251 | abort(); |
---|
8252 | } |
---|
8253 | return b1; |
---|
8254 | } |
---|
8255 | |
---|
8256 | /* |
---|
8257 | * Filtering for MTP2 messages based on li value |
---|
8258 | * FISU, length is null |
---|
8259 | * LSSU, length is 1 or 2 |
---|
8260 | * MSU, length is 3 or more |
---|
8261 | */ |
---|
8262 | struct block * |
---|
8263 | gen_mtp2type_abbrev(type) |
---|
8264 | int type; |
---|
8265 | { |
---|
8266 | struct block *b0, *b1; |
---|
8267 | |
---|
8268 | switch (type) { |
---|
8269 | |
---|
8270 | case M_FISU: |
---|
8271 | if ( (linktype != DLT_MTP2) && |
---|
8272 | (linktype != DLT_ERF) && |
---|
8273 | (linktype != DLT_MTP2_WITH_PHDR) ) |
---|
8274 | bpf_error("'fisu' supported only on MTP2"); |
---|
8275 | /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */ |
---|
8276 | b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0); |
---|
8277 | break; |
---|
8278 | |
---|
8279 | case M_LSSU: |
---|
8280 | if ( (linktype != DLT_MTP2) && |
---|
8281 | (linktype != DLT_ERF) && |
---|
8282 | (linktype != DLT_MTP2_WITH_PHDR) ) |
---|
8283 | bpf_error("'lssu' supported only on MTP2"); |
---|
8284 | b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2); |
---|
8285 | b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0); |
---|
8286 | gen_and(b1, b0); |
---|
8287 | break; |
---|
8288 | |
---|
8289 | case M_MSU: |
---|
8290 | if ( (linktype != DLT_MTP2) && |
---|
8291 | (linktype != DLT_ERF) && |
---|
8292 | (linktype != DLT_MTP2_WITH_PHDR) ) |
---|
8293 | bpf_error("'msu' supported only on MTP2"); |
---|
8294 | b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2); |
---|
8295 | break; |
---|
8296 | |
---|
8297 | default: |
---|
8298 | abort(); |
---|
8299 | } |
---|
8300 | return b0; |
---|
8301 | } |
---|
8302 | |
---|
8303 | struct block * |
---|
8304 | gen_mtp3field_code(mtp3field, jvalue, jtype, reverse) |
---|
8305 | int mtp3field; |
---|
8306 | bpf_u_int32 jvalue; |
---|
8307 | bpf_u_int32 jtype; |
---|
8308 | int reverse; |
---|
8309 | { |
---|
8310 | struct block *b0; |
---|
8311 | bpf_u_int32 val1 , val2 , val3; |
---|
8312 | |
---|
8313 | switch (mtp3field) { |
---|
8314 | |
---|
8315 | case M_SIO: |
---|
8316 | if (off_sio == (u_int)-1) |
---|
8317 | bpf_error("'sio' supported only on SS7"); |
---|
8318 | /* sio coded on 1 byte so max value 255 */ |
---|
8319 | if(jvalue > 255) |
---|
8320 | bpf_error("sio value %u too big; max value = 255", |
---|
8321 | jvalue); |
---|
8322 | b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff, |
---|
8323 | (u_int)jtype, reverse, (u_int)jvalue); |
---|
8324 | break; |
---|
8325 | |
---|
8326 | case M_OPC: |
---|
8327 | if (off_opc == (u_int)-1) |
---|
8328 | bpf_error("'opc' supported only on SS7"); |
---|
8329 | /* opc coded on 14 bits so max value 16383 */ |
---|
8330 | if (jvalue > 16383) |
---|
8331 | bpf_error("opc value %u too big; max value = 16383", |
---|
8332 | jvalue); |
---|
8333 | /* the following instructions are made to convert jvalue |
---|
8334 | * to the form used to write opc in an ss7 message*/ |
---|
8335 | val1 = jvalue & 0x00003c00; |
---|
8336 | val1 = val1 >>10; |
---|
8337 | val2 = jvalue & 0x000003fc; |
---|
8338 | val2 = val2 <<6; |
---|
8339 | val3 = jvalue & 0x00000003; |
---|
8340 | val3 = val3 <<22; |
---|
8341 | jvalue = val1 + val2 + val3; |
---|
8342 | b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f, |
---|
8343 | (u_int)jtype, reverse, (u_int)jvalue); |
---|
8344 | break; |
---|
8345 | |
---|
8346 | case M_DPC: |
---|
8347 | if (off_dpc == (u_int)-1) |
---|
8348 | bpf_error("'dpc' supported only on SS7"); |
---|
8349 | /* dpc coded on 14 bits so max value 16383 */ |
---|
8350 | if (jvalue > 16383) |
---|
8351 | bpf_error("dpc value %u too big; max value = 16383", |
---|
8352 | jvalue); |
---|
8353 | /* the following instructions are made to convert jvalue |
---|
8354 | * to the forme used to write dpc in an ss7 message*/ |
---|
8355 | val1 = jvalue & 0x000000ff; |
---|
8356 | val1 = val1 << 24; |
---|
8357 | val2 = jvalue & 0x00003f00; |
---|
8358 | val2 = val2 << 8; |
---|
8359 | jvalue = val1 + val2; |
---|
8360 | b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000, |
---|
8361 | (u_int)jtype, reverse, (u_int)jvalue); |
---|
8362 | break; |
---|
8363 | |
---|
8364 | case M_SLS: |
---|
8365 | if (off_sls == (u_int)-1) |
---|
8366 | bpf_error("'sls' supported only on SS7"); |
---|
8367 | /* sls coded on 4 bits so max value 15 */ |
---|
8368 | if (jvalue > 15) |
---|
8369 | bpf_error("sls value %u too big; max value = 15", |
---|
8370 | jvalue); |
---|
8371 | /* the following instruction is made to convert jvalue |
---|
8372 | * to the forme used to write sls in an ss7 message*/ |
---|
8373 | jvalue = jvalue << 4; |
---|
8374 | b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0, |
---|
8375 | (u_int)jtype,reverse, (u_int)jvalue); |
---|
8376 | break; |
---|
8377 | |
---|
8378 | default: |
---|
8379 | abort(); |
---|
8380 | } |
---|
8381 | return b0; |
---|
8382 | } |
---|
8383 | |
---|
8384 | static struct block * |
---|
8385 | gen_msg_abbrev(type) |
---|
8386 | int type; |
---|
8387 | { |
---|
8388 | struct block *b1; |
---|
8389 | |
---|
8390 | /* |
---|
8391 | * Q.2931 signalling protocol messages for handling virtual circuits |
---|
8392 | * establishment and teardown |
---|
8393 | */ |
---|
8394 | switch (type) { |
---|
8395 | |
---|
8396 | case A_SETUP: |
---|
8397 | b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0); |
---|
8398 | break; |
---|
8399 | |
---|
8400 | case A_CALLPROCEED: |
---|
8401 | b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0); |
---|
8402 | break; |
---|
8403 | |
---|
8404 | case A_CONNECT: |
---|
8405 | b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0); |
---|
8406 | break; |
---|
8407 | |
---|
8408 | case A_CONNECTACK: |
---|
8409 | b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0); |
---|
8410 | break; |
---|
8411 | |
---|
8412 | case A_RELEASE: |
---|
8413 | b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0); |
---|
8414 | break; |
---|
8415 | |
---|
8416 | case A_RELEASE_DONE: |
---|
8417 | b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0); |
---|
8418 | break; |
---|
8419 | |
---|
8420 | default: |
---|
8421 | abort(); |
---|
8422 | } |
---|
8423 | return b1; |
---|
8424 | } |
---|
8425 | |
---|
8426 | struct block * |
---|
8427 | gen_atmmulti_abbrev(type) |
---|
8428 | int type; |
---|
8429 | { |
---|
8430 | struct block *b0, *b1; |
---|
8431 | |
---|
8432 | switch (type) { |
---|
8433 | |
---|
8434 | case A_OAM: |
---|
8435 | if (!is_atm) |
---|
8436 | bpf_error("'oam' supported only on raw ATM"); |
---|
8437 | b1 = gen_atmmulti_abbrev(A_OAMF4); |
---|
8438 | break; |
---|
8439 | |
---|
8440 | case A_OAMF4: |
---|
8441 | if (!is_atm) |
---|
8442 | bpf_error("'oamf4' supported only on raw ATM"); |
---|
8443 | /* OAM F4 type */ |
---|
8444 | b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0); |
---|
8445 | b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0); |
---|
8446 | gen_or(b0, b1); |
---|
8447 | b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0); |
---|
8448 | gen_and(b0, b1); |
---|
8449 | break; |
---|
8450 | |
---|
8451 | case A_CONNECTMSG: |
---|
8452 | /* |
---|
8453 | * Get Q.2931 signalling messages for switched |
---|
8454 | * virtual connection |
---|
8455 | */ |
---|
8456 | if (!is_atm) |
---|
8457 | bpf_error("'connectmsg' supported only on raw ATM"); |
---|
8458 | b0 = gen_msg_abbrev(A_SETUP); |
---|
8459 | b1 = gen_msg_abbrev(A_CALLPROCEED); |
---|
8460 | gen_or(b0, b1); |
---|
8461 | b0 = gen_msg_abbrev(A_CONNECT); |
---|
8462 | gen_or(b0, b1); |
---|
8463 | b0 = gen_msg_abbrev(A_CONNECTACK); |
---|
8464 | gen_or(b0, b1); |
---|
8465 | b0 = gen_msg_abbrev(A_RELEASE); |
---|
8466 | gen_or(b0, b1); |
---|
8467 | b0 = gen_msg_abbrev(A_RELEASE_DONE); |
---|
8468 | gen_or(b0, b1); |
---|
8469 | b0 = gen_atmtype_abbrev(A_SC); |
---|
8470 | gen_and(b0, b1); |
---|
8471 | break; |
---|
8472 | |
---|
8473 | case A_METACONNECT: |
---|
8474 | if (!is_atm) |
---|
8475 | bpf_error("'metaconnect' supported only on raw ATM"); |
---|
8476 | b0 = gen_msg_abbrev(A_SETUP); |
---|
8477 | b1 = gen_msg_abbrev(A_CALLPROCEED); |
---|
8478 | gen_or(b0, b1); |
---|
8479 | b0 = gen_msg_abbrev(A_CONNECT); |
---|
8480 | gen_or(b0, b1); |
---|
8481 | b0 = gen_msg_abbrev(A_RELEASE); |
---|
8482 | gen_or(b0, b1); |
---|
8483 | b0 = gen_msg_abbrev(A_RELEASE_DONE); |
---|
8484 | gen_or(b0, b1); |
---|
8485 | b0 = gen_atmtype_abbrev(A_METAC); |
---|
8486 | gen_and(b0, b1); |
---|
8487 | break; |
---|
8488 | |
---|
8489 | default: |
---|
8490 | abort(); |
---|
8491 | } |
---|
8492 | return b1; |
---|
8493 | } |
---|