/src/libpcap-1.9.1/gencode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*#define CHASE_CHAIN*/ |
2 | | /* |
3 | | * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 |
4 | | * The Regents of the University of California. All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that: (1) source code distributions |
8 | | * retain the above copyright notice and this paragraph in its entirety, (2) |
9 | | * distributions including binary code include the above copyright notice and |
10 | | * this paragraph in its entirety in the documentation or other materials |
11 | | * provided with the distribution, and (3) all advertising materials mentioning |
12 | | * features or use of this software display the following acknowledgement: |
13 | | * ``This product includes software developed by the University of California, |
14 | | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
15 | | * the University nor the names of its contributors may be used to endorse |
16 | | * or promote products derived from this software without specific prior |
17 | | * written permission. |
18 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
19 | | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
20 | | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
21 | | */ |
22 | | |
23 | | #ifdef HAVE_CONFIG_H |
24 | | #include <config.h> |
25 | | #endif |
26 | | |
27 | | #include <pcap-types.h> |
28 | | #ifdef _WIN32 |
29 | | #include <ws2tcpip.h> |
30 | | #else |
31 | | #include <sys/socket.h> |
32 | | |
33 | | #ifdef __NetBSD__ |
34 | | #include <sys/param.h> |
35 | | #endif |
36 | | |
37 | | #include <netinet/in.h> |
38 | | #include <arpa/inet.h> |
39 | | #endif /* _WIN32 */ |
40 | | |
41 | | #include <stdlib.h> |
42 | | #include <string.h> |
43 | | #include <memory.h> |
44 | | #include <setjmp.h> |
45 | | #include <stdarg.h> |
46 | | |
47 | | #ifdef MSDOS |
48 | | #include "pcap-dos.h" |
49 | | #endif |
50 | | |
51 | | #include "pcap-int.h" |
52 | | |
53 | | #include "extract.h" |
54 | | |
55 | | #include "ethertype.h" |
56 | | #include "nlpid.h" |
57 | | #include "llc.h" |
58 | | #include "gencode.h" |
59 | | #include "ieee80211.h" |
60 | | #include "atmuni31.h" |
61 | | #include "sunatmpos.h" |
62 | | #include "ppp.h" |
63 | | #include "pcap/sll.h" |
64 | | #include "pcap/ipnet.h" |
65 | | #include "arcnet.h" |
66 | | |
67 | | #include "grammar.h" |
68 | | #include "scanner.h" |
69 | | |
70 | | #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) |
71 | | #include <linux/types.h> |
72 | | #include <linux/if_packet.h> |
73 | | #include <linux/filter.h> |
74 | | #endif |
75 | | |
76 | | #ifdef HAVE_NET_PFVAR_H |
77 | | #include <sys/socket.h> |
78 | | #include <net/if.h> |
79 | | #include <net/pfvar.h> |
80 | | #include <net/if_pflog.h> |
81 | | #endif |
82 | | |
83 | | #ifndef offsetof |
84 | | #define offsetof(s, e) ((size_t)&((s *)0)->e) |
85 | | #endif |
86 | | |
87 | | #ifdef _WIN32 |
88 | | #ifdef INET6 |
89 | | #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) |
90 | | /* IPv6 address */ |
91 | | struct in6_addr |
92 | | { |
93 | | union |
94 | | { |
95 | | uint8_t u6_addr8[16]; |
96 | | uint16_t u6_addr16[8]; |
97 | | uint32_t u6_addr32[4]; |
98 | | } in6_u; |
99 | | #define s6_addr in6_u.u6_addr8 |
100 | | #define s6_addr16 in6_u.u6_addr16 |
101 | | #define s6_addr32 in6_u.u6_addr32 |
102 | | #define s6_addr64 in6_u.u6_addr64 |
103 | | }; |
104 | | |
105 | | typedef unsigned short sa_family_t; |
106 | | |
107 | | #define __SOCKADDR_COMMON(sa_prefix) \ |
108 | | sa_family_t sa_prefix##family |
109 | | |
110 | | /* Ditto, for IPv6. */ |
111 | | struct sockaddr_in6 |
112 | | { |
113 | | __SOCKADDR_COMMON (sin6_); |
114 | | uint16_t sin6_port; /* Transport layer port # */ |
115 | | uint32_t sin6_flowinfo; /* IPv6 flow information */ |
116 | | struct in6_addr sin6_addr; /* IPv6 address */ |
117 | | }; |
118 | | |
119 | | #ifndef EAI_ADDRFAMILY |
120 | | struct addrinfo { |
121 | | int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ |
122 | | int ai_family; /* PF_xxx */ |
123 | | int ai_socktype; /* SOCK_xxx */ |
124 | | int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ |
125 | | size_t ai_addrlen; /* length of ai_addr */ |
126 | | char *ai_canonname; /* canonical name for hostname */ |
127 | | struct sockaddr *ai_addr; /* binary address */ |
128 | | struct addrinfo *ai_next; /* next structure in linked list */ |
129 | | }; |
130 | | #endif /* EAI_ADDRFAMILY */ |
131 | | #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */ |
132 | | #endif /* INET6 */ |
133 | | #else /* _WIN32 */ |
134 | | #include <netdb.h> /* for "struct addrinfo" */ |
135 | | #endif /* _WIN32 */ |
136 | | #include <pcap/namedb.h> |
137 | | |
138 | | #include "nametoaddr.h" |
139 | | |
140 | 0 | #define ETHERMTU 1500 |
141 | | |
142 | | #ifndef ETHERTYPE_TEB |
143 | 0 | #define ETHERTYPE_TEB 0x6558 |
144 | | #endif |
145 | | |
146 | | #ifndef IPPROTO_HOPOPTS |
147 | | #define IPPROTO_HOPOPTS 0 |
148 | | #endif |
149 | | #ifndef IPPROTO_ROUTING |
150 | | #define IPPROTO_ROUTING 43 |
151 | | #endif |
152 | | #ifndef IPPROTO_FRAGMENT |
153 | | #define IPPROTO_FRAGMENT 44 |
154 | | #endif |
155 | | #ifndef IPPROTO_DSTOPTS |
156 | | #define IPPROTO_DSTOPTS 60 |
157 | | #endif |
158 | | #ifndef IPPROTO_SCTP |
159 | | #define IPPROTO_SCTP 132 |
160 | | #endif |
161 | | |
162 | 0 | #define GENEVE_PORT 6081 |
163 | | |
164 | | #ifdef HAVE_OS_PROTO_H |
165 | | #include "os-proto.h" |
166 | | #endif |
167 | | |
168 | 0 | #define JMP(c) ((c)|BPF_JMP|BPF_K) |
169 | | |
170 | | /* |
171 | | * "Push" the current value of the link-layer header type and link-layer |
172 | | * header offset onto a "stack", and set a new value. (It's not a |
173 | | * full-blown stack; we keep only the top two items.) |
174 | | */ |
175 | 0 | #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \ |
176 | 0 | { \ |
177 | 0 | (cs)->prevlinktype = (cs)->linktype; \ |
178 | 0 | (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \ |
179 | 0 | (cs)->linktype = (new_linktype); \ |
180 | 0 | (cs)->off_linkhdr.is_variable = (new_is_variable); \ |
181 | 0 | (cs)->off_linkhdr.constant_part = (new_constant_part); \ |
182 | 0 | (cs)->off_linkhdr.reg = (new_reg); \ |
183 | 0 | (cs)->is_geneve = 0; \ |
184 | 0 | } |
185 | | |
186 | | /* |
187 | | * Offset "not set" value. |
188 | | */ |
189 | 0 | #define OFFSET_NOT_SET 0xffffffffU |
190 | | |
191 | | /* |
192 | | * Absolute offsets, which are offsets from the beginning of the raw |
193 | | * packet data, are, in the general case, the sum of a variable value |
194 | | * and a constant value; the variable value may be absent, in which |
195 | | * case the offset is only the constant value, and the constant value |
196 | | * may be zero, in which case the offset is only the variable value. |
197 | | * |
198 | | * bpf_abs_offset is a structure containing all that information: |
199 | | * |
200 | | * is_variable is 1 if there's a variable part. |
201 | | * |
202 | | * constant_part is the constant part of the value, possibly zero; |
203 | | * |
204 | | * if is_variable is 1, reg is the register number for a register |
205 | | * containing the variable value if the register has been assigned, |
206 | | * and -1 otherwise. |
207 | | */ |
208 | | typedef struct { |
209 | | int is_variable; |
210 | | u_int constant_part; |
211 | | int reg; |
212 | | } bpf_abs_offset; |
213 | | |
214 | | /* |
215 | | * Value passed to gen_load_a() to indicate what the offset argument |
216 | | * is relative to the beginning of. |
217 | | */ |
218 | | enum e_offrel { |
219 | | OR_PACKET, /* full packet data */ |
220 | | OR_LINKHDR, /* link-layer header */ |
221 | | OR_PREVLINKHDR, /* previous link-layer header */ |
222 | | OR_LLC, /* 802.2 LLC header */ |
223 | | OR_PREVMPLSHDR, /* previous MPLS header */ |
224 | | OR_LINKTYPE, /* link-layer type */ |
225 | | OR_LINKPL, /* link-layer payload */ |
226 | | OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */ |
227 | | OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */ |
228 | | OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */ |
229 | | }; |
230 | | |
231 | | /* |
232 | | * We divy out chunks of memory rather than call malloc each time so |
233 | | * we don't have to worry about leaking memory. It's probably |
234 | | * not a big deal if all this memory was wasted but if this ever |
235 | | * goes into a library that would probably not be a good idea. |
236 | | * |
237 | | * XXX - this *is* in a library.... |
238 | | */ |
239 | 0 | #define NCHUNKS 16 |
240 | 0 | #define CHUNK0SIZE 1024 |
241 | | struct chunk { |
242 | | size_t n_left; |
243 | | void *m; |
244 | | }; |
245 | | |
246 | | /* Code generator state */ |
247 | | |
248 | | struct _compiler_state { |
249 | | jmp_buf top_ctx; |
250 | | pcap_t *bpf_pcap; |
251 | | |
252 | | struct icode ic; |
253 | | |
254 | | int snaplen; |
255 | | |
256 | | int linktype; |
257 | | int prevlinktype; |
258 | | int outermostlinktype; |
259 | | |
260 | | bpf_u_int32 netmask; |
261 | | int no_optimize; |
262 | | |
263 | | /* Hack for handling VLAN and MPLS stacks. */ |
264 | | u_int label_stack_depth; |
265 | | u_int vlan_stack_depth; |
266 | | |
267 | | /* XXX */ |
268 | | u_int pcap_fddipad; |
269 | | |
270 | | /* |
271 | | * As errors are handled by a longjmp, anything allocated must |
272 | | * be freed in the longjmp handler, so it must be reachable |
273 | | * from that handler. |
274 | | * |
275 | | * One thing that's allocated is the result of pcap_nametoaddrinfo(); |
276 | | * it must be freed with freeaddrinfo(). This variable points to |
277 | | * any addrinfo structure that would need to be freed. |
278 | | */ |
279 | | struct addrinfo *ai; |
280 | | |
281 | | /* |
282 | | * Another thing that's allocated is the result of pcap_ether_aton(); |
283 | | * it must be freed with free(). This variable points to any |
284 | | * address that would need to be freed. |
285 | | */ |
286 | | u_char *e; |
287 | | |
288 | | /* |
289 | | * Various code constructs need to know the layout of the packet. |
290 | | * These values give the necessary offsets from the beginning |
291 | | * of the packet data. |
292 | | */ |
293 | | |
294 | | /* |
295 | | * Absolute offset of the beginning of the link-layer header. |
296 | | */ |
297 | | bpf_abs_offset off_linkhdr; |
298 | | |
299 | | /* |
300 | | * If we're checking a link-layer header for a packet encapsulated |
301 | | * in another protocol layer, this is the equivalent information |
302 | | * for the previous layers' link-layer header from the beginning |
303 | | * of the raw packet data. |
304 | | */ |
305 | | bpf_abs_offset off_prevlinkhdr; |
306 | | |
307 | | /* |
308 | | * This is the equivalent information for the outermost layers' |
309 | | * link-layer header. |
310 | | */ |
311 | | bpf_abs_offset off_outermostlinkhdr; |
312 | | |
313 | | /* |
314 | | * Absolute offset of the beginning of the link-layer payload. |
315 | | */ |
316 | | bpf_abs_offset off_linkpl; |
317 | | |
318 | | /* |
319 | | * "off_linktype" is the offset to information in the link-layer |
320 | | * header giving the packet type. This is an absolute offset |
321 | | * from the beginning of the packet. |
322 | | * |
323 | | * For Ethernet, it's the offset of the Ethernet type field; this |
324 | | * means that it must have a value that skips VLAN tags. |
325 | | * |
326 | | * For link-layer types that always use 802.2 headers, it's the |
327 | | * offset of the LLC header; this means that it must have a value |
328 | | * that skips VLAN tags. |
329 | | * |
330 | | * For PPP, it's the offset of the PPP type field. |
331 | | * |
332 | | * For Cisco HDLC, it's the offset of the CHDLC type field. |
333 | | * |
334 | | * For BSD loopback, it's the offset of the AF_ value. |
335 | | * |
336 | | * For Linux cooked sockets, it's the offset of the type field. |
337 | | * |
338 | | * off_linktype.constant_part is set to OFFSET_NOT_SET for no |
339 | | * encapsulation, in which case, IP is assumed. |
340 | | */ |
341 | | bpf_abs_offset off_linktype; |
342 | | |
343 | | /* |
344 | | * TRUE if the link layer includes an ATM pseudo-header. |
345 | | */ |
346 | | int is_atm; |
347 | | |
348 | | /* |
349 | | * TRUE if "geneve" appeared in the filter; it causes us to |
350 | | * generate code that checks for a Geneve header and assume |
351 | | * that later filters apply to the encapsulated payload. |
352 | | */ |
353 | | int is_geneve; |
354 | | |
355 | | /* |
356 | | * TRUE if we need variable length part of VLAN offset |
357 | | */ |
358 | | int is_vlan_vloffset; |
359 | | |
360 | | /* |
361 | | * These are offsets for the ATM pseudo-header. |
362 | | */ |
363 | | u_int off_vpi; |
364 | | u_int off_vci; |
365 | | u_int off_proto; |
366 | | |
367 | | /* |
368 | | * These are offsets for the MTP2 fields. |
369 | | */ |
370 | | u_int off_li; |
371 | | u_int off_li_hsl; |
372 | | |
373 | | /* |
374 | | * These are offsets for the MTP3 fields. |
375 | | */ |
376 | | u_int off_sio; |
377 | | u_int off_opc; |
378 | | u_int off_dpc; |
379 | | u_int off_sls; |
380 | | |
381 | | /* |
382 | | * This is the offset of the first byte after the ATM pseudo_header, |
383 | | * or -1 if there is no ATM pseudo-header. |
384 | | */ |
385 | | u_int off_payload; |
386 | | |
387 | | /* |
388 | | * These are offsets to the beginning of the network-layer header. |
389 | | * They are relative to the beginning of the link-layer payload |
390 | | * (i.e., they don't include off_linkhdr.constant_part or |
391 | | * off_linkpl.constant_part). |
392 | | * |
393 | | * If the link layer never uses 802.2 LLC: |
394 | | * |
395 | | * "off_nl" and "off_nl_nosnap" are the same. |
396 | | * |
397 | | * If the link layer always uses 802.2 LLC: |
398 | | * |
399 | | * "off_nl" is the offset if there's a SNAP header following |
400 | | * the 802.2 header; |
401 | | * |
402 | | * "off_nl_nosnap" is the offset if there's no SNAP header. |
403 | | * |
404 | | * If the link layer is Ethernet: |
405 | | * |
406 | | * "off_nl" is the offset if the packet is an Ethernet II packet |
407 | | * (we assume no 802.3+802.2+SNAP); |
408 | | * |
409 | | * "off_nl_nosnap" is the offset if the packet is an 802.3 packet |
410 | | * with an 802.2 header following it. |
411 | | */ |
412 | | u_int off_nl; |
413 | | u_int off_nl_nosnap; |
414 | | |
415 | | /* |
416 | | * Here we handle simple allocation of the scratch registers. |
417 | | * If too many registers are alloc'd, the allocator punts. |
418 | | */ |
419 | | int regused[BPF_MEMWORDS]; |
420 | | int curreg; |
421 | | |
422 | | /* |
423 | | * Memory chunks. |
424 | | */ |
425 | | struct chunk chunks[NCHUNKS]; |
426 | | int cur_chunk; |
427 | | }; |
428 | | |
429 | | /* |
430 | | * For use by routines outside this file. |
431 | | */ |
432 | | /* VARARGS */ |
433 | | void |
434 | | bpf_set_error(compiler_state_t *cstate, const char *fmt, ...) |
435 | 0 | { |
436 | 0 | va_list ap; |
437 | |
|
438 | 0 | va_start(ap, fmt); |
439 | 0 | (void)pcap_vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
440 | 0 | fmt, ap); |
441 | 0 | va_end(ap); |
442 | 0 | } |
443 | | |
444 | | /* |
445 | | * For use *ONLY* in routines in this file. |
446 | | */ |
447 | | static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...) |
448 | | PCAP_PRINTFLIKE(2, 3); |
449 | | |
450 | | /* VARARGS */ |
451 | | static void PCAP_NORETURN |
452 | | bpf_error(compiler_state_t *cstate, const char *fmt, ...) |
453 | 0 | { |
454 | 0 | va_list ap; |
455 | |
|
456 | 0 | va_start(ap, fmt); |
457 | 0 | (void)pcap_vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
458 | 0 | fmt, ap); |
459 | 0 | va_end(ap); |
460 | 0 | longjmp(cstate->top_ctx, 1); |
461 | | /*NOTREACHED*/ |
462 | 0 | } |
463 | | |
464 | | static int init_linktype(compiler_state_t *, pcap_t *); |
465 | | |
466 | | static void init_regs(compiler_state_t *); |
467 | | static int alloc_reg(compiler_state_t *); |
468 | | static void free_reg(compiler_state_t *, int); |
469 | | |
470 | | static void initchunks(compiler_state_t *cstate); |
471 | | static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t); |
472 | | static void *newchunk(compiler_state_t *cstate, size_t); |
473 | | static void freechunks(compiler_state_t *cstate); |
474 | | static inline struct block *new_block(compiler_state_t *cstate, int); |
475 | | static inline struct slist *new_stmt(compiler_state_t *cstate, int); |
476 | | static struct block *gen_retblk(compiler_state_t *cstate, int); |
477 | | static inline void syntax(compiler_state_t *cstate); |
478 | | |
479 | | static void backpatch(struct block *, struct block *); |
480 | | static void merge(struct block *, struct block *); |
481 | | static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int, |
482 | | u_int, bpf_int32); |
483 | | static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int, |
484 | | u_int, bpf_int32); |
485 | | static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int, |
486 | | u_int, bpf_int32); |
487 | | static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int, |
488 | | u_int, bpf_int32); |
489 | | static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int, |
490 | | u_int, bpf_int32); |
491 | | static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int, |
492 | | u_int, bpf_int32, bpf_u_int32); |
493 | | static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int, |
494 | | u_int, const u_char *); |
495 | | static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, bpf_u_int32, |
496 | | bpf_u_int32, bpf_u_int32, bpf_u_int32, int, bpf_int32); |
497 | | static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *, |
498 | | u_int, u_int); |
499 | | static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int, |
500 | | u_int); |
501 | | static struct slist *gen_loadx_iphdrlen(compiler_state_t *); |
502 | | static struct block *gen_uncond(compiler_state_t *, int); |
503 | | static inline struct block *gen_true(compiler_state_t *); |
504 | | static inline struct block *gen_false(compiler_state_t *); |
505 | | static struct block *gen_ether_linktype(compiler_state_t *, int); |
506 | | static struct block *gen_ipnet_linktype(compiler_state_t *, int); |
507 | | static struct block *gen_linux_sll_linktype(compiler_state_t *, int); |
508 | | static struct slist *gen_load_prism_llprefixlen(compiler_state_t *); |
509 | | static struct slist *gen_load_avs_llprefixlen(compiler_state_t *); |
510 | | static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *); |
511 | | static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *); |
512 | | static void insert_compute_vloffsets(compiler_state_t *, struct block *); |
513 | | static struct slist *gen_abs_offset_varpart(compiler_state_t *, |
514 | | bpf_abs_offset *); |
515 | | static int ethertype_to_ppptype(int); |
516 | | static struct block *gen_linktype(compiler_state_t *, int); |
517 | | static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32); |
518 | | static struct block *gen_llc_linktype(compiler_state_t *, int); |
519 | | static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
520 | | int, int, u_int, u_int); |
521 | | #ifdef INET6 |
522 | | static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *, |
523 | | struct in6_addr *, int, int, u_int, u_int); |
524 | | #endif |
525 | | static struct block *gen_ahostop(compiler_state_t *, const u_char *, int); |
526 | | static struct block *gen_ehostop(compiler_state_t *, const u_char *, int); |
527 | | static struct block *gen_fhostop(compiler_state_t *, const u_char *, int); |
528 | | static struct block *gen_thostop(compiler_state_t *, const u_char *, int); |
529 | | static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int); |
530 | | static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int); |
531 | | static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int); |
532 | | static struct block *gen_mpls_linktype(compiler_state_t *, int); |
533 | | static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
534 | | int, int, int); |
535 | | #ifdef INET6 |
536 | | static struct block *gen_host6(compiler_state_t *, struct in6_addr *, |
537 | | struct in6_addr *, int, int, int); |
538 | | #endif |
539 | | #ifndef INET6 |
540 | | static struct block *gen_gateway(compiler_state_t *, const u_char *, |
541 | | struct addrinfo *, int, int); |
542 | | #endif |
543 | | static struct block *gen_ipfrag(compiler_state_t *); |
544 | | static struct block *gen_portatom(compiler_state_t *, int, bpf_int32); |
545 | | static struct block *gen_portrangeatom(compiler_state_t *, int, bpf_int32, |
546 | | bpf_int32); |
547 | | static struct block *gen_portatom6(compiler_state_t *, int, bpf_int32); |
548 | | static struct block *gen_portrangeatom6(compiler_state_t *, int, bpf_int32, |
549 | | bpf_int32); |
550 | | struct block *gen_portop(compiler_state_t *, int, int, int); |
551 | | static struct block *gen_port(compiler_state_t *, int, int, int); |
552 | | struct block *gen_portrangeop(compiler_state_t *, int, int, int, int); |
553 | | static struct block *gen_portrange(compiler_state_t *, int, int, int, int); |
554 | | struct block *gen_portop6(compiler_state_t *, int, int, int); |
555 | | static struct block *gen_port6(compiler_state_t *, int, int, int); |
556 | | struct block *gen_portrangeop6(compiler_state_t *, int, int, int, int); |
557 | | static struct block *gen_portrange6(compiler_state_t *, int, int, int, int); |
558 | | static int lookup_proto(compiler_state_t *, const char *, int); |
559 | | static struct block *gen_protochain(compiler_state_t *, int, int, int); |
560 | | static struct block *gen_proto(compiler_state_t *, int, int, int); |
561 | | static struct slist *xfer_to_x(compiler_state_t *, struct arth *); |
562 | | static struct slist *xfer_to_a(compiler_state_t *, struct arth *); |
563 | | static struct block *gen_mac_multicast(compiler_state_t *, int); |
564 | | static struct block *gen_len(compiler_state_t *, int, int); |
565 | | static struct block *gen_check_802_11_data_frame(compiler_state_t *); |
566 | | static struct block *gen_geneve_ll_check(compiler_state_t *cstate); |
567 | | |
568 | | static struct block *gen_ppi_dlt_check(compiler_state_t *); |
569 | | static struct block *gen_atmfield_code_internal(compiler_state_t *, int, |
570 | | bpf_int32, bpf_u_int32, int); |
571 | | static struct block *gen_atmtype_llc(compiler_state_t *); |
572 | | static struct block *gen_msg_abbrev(compiler_state_t *, int type); |
573 | | |
574 | | static void |
575 | | initchunks(compiler_state_t *cstate) |
576 | 0 | { |
577 | 0 | int i; |
578 | |
|
579 | 0 | for (i = 0; i < NCHUNKS; i++) { |
580 | 0 | cstate->chunks[i].n_left = 0; |
581 | 0 | cstate->chunks[i].m = NULL; |
582 | 0 | } |
583 | 0 | cstate->cur_chunk = 0; |
584 | 0 | } |
585 | | |
586 | | static void * |
587 | | newchunk_nolongjmp(compiler_state_t *cstate, size_t n) |
588 | 0 | { |
589 | 0 | struct chunk *cp; |
590 | 0 | int k; |
591 | 0 | size_t size; |
592 | |
|
593 | 0 | #ifndef __NetBSD__ |
594 | | /* XXX Round up to nearest long. */ |
595 | 0 | n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1); |
596 | | #else |
597 | | /* XXX Round up to structure boundary. */ |
598 | | n = ALIGN(n); |
599 | | #endif |
600 | |
|
601 | 0 | cp = &cstate->chunks[cstate->cur_chunk]; |
602 | 0 | if (n > cp->n_left) { |
603 | 0 | ++cp; |
604 | 0 | k = ++cstate->cur_chunk; |
605 | 0 | if (k >= NCHUNKS) { |
606 | 0 | bpf_set_error(cstate, "out of memory"); |
607 | 0 | return (NULL); |
608 | 0 | } |
609 | 0 | size = CHUNK0SIZE << k; |
610 | 0 | cp->m = (void *)malloc(size); |
611 | 0 | if (cp->m == NULL) { |
612 | 0 | bpf_set_error(cstate, "out of memory"); |
613 | 0 | return (NULL); |
614 | 0 | } |
615 | 0 | memset((char *)cp->m, 0, size); |
616 | 0 | cp->n_left = size; |
617 | 0 | if (n > size) { |
618 | 0 | bpf_set_error(cstate, "out of memory"); |
619 | 0 | return (NULL); |
620 | 0 | } |
621 | 0 | } |
622 | 0 | cp->n_left -= n; |
623 | 0 | return (void *)((char *)cp->m + cp->n_left); |
624 | 0 | } |
625 | | |
626 | | static void * |
627 | | newchunk(compiler_state_t *cstate, size_t n) |
628 | 0 | { |
629 | 0 | void *p; |
630 | |
|
631 | 0 | p = newchunk_nolongjmp(cstate, n); |
632 | 0 | if (p == NULL) { |
633 | 0 | longjmp(cstate->top_ctx, 1); |
634 | | /*NOTREACHED*/ |
635 | 0 | } |
636 | 0 | return (p); |
637 | 0 | } |
638 | | |
639 | | static void |
640 | | freechunks(compiler_state_t *cstate) |
641 | 0 | { |
642 | 0 | int i; |
643 | |
|
644 | 0 | for (i = 0; i < NCHUNKS; ++i) |
645 | 0 | if (cstate->chunks[i].m != NULL) |
646 | 0 | free(cstate->chunks[i].m); |
647 | 0 | } |
648 | | |
649 | | /* |
650 | | * A strdup whose allocations are freed after code generation is over. |
651 | | * This is used by the lexical analyzer, so it can't longjmp; it just |
652 | | * returns NULL on an allocation error, and the callers must check |
653 | | * for it. |
654 | | */ |
655 | | char * |
656 | | sdup(compiler_state_t *cstate, const char *s) |
657 | 0 | { |
658 | 0 | size_t n = strlen(s) + 1; |
659 | 0 | char *cp = newchunk_nolongjmp(cstate, n); |
660 | |
|
661 | 0 | if (cp == NULL) |
662 | 0 | return (NULL); |
663 | 0 | pcap_strlcpy(cp, s, n); |
664 | 0 | return (cp); |
665 | 0 | } |
666 | | |
667 | | static inline struct block * |
668 | | new_block(compiler_state_t *cstate, int code) |
669 | 0 | { |
670 | 0 | struct block *p; |
671 | |
|
672 | 0 | p = (struct block *)newchunk(cstate, sizeof(*p)); |
673 | 0 | p->s.code = code; |
674 | 0 | p->head = p; |
675 | |
|
676 | 0 | return p; |
677 | 0 | } |
678 | | |
679 | | static inline struct slist * |
680 | | new_stmt(compiler_state_t *cstate, int code) |
681 | 0 | { |
682 | 0 | struct slist *p; |
683 | |
|
684 | 0 | p = (struct slist *)newchunk(cstate, sizeof(*p)); |
685 | 0 | p->s.code = code; |
686 | |
|
687 | 0 | return p; |
688 | 0 | } |
689 | | |
690 | | static struct block * |
691 | | gen_retblk(compiler_state_t *cstate, int v) |
692 | 0 | { |
693 | 0 | struct block *b = new_block(cstate, BPF_RET|BPF_K); |
694 | |
|
695 | 0 | b->s.k = v; |
696 | 0 | return b; |
697 | 0 | } |
698 | | |
699 | | static inline PCAP_NORETURN_DEF void |
700 | | syntax(compiler_state_t *cstate) |
701 | 0 | { |
702 | 0 | bpf_error(cstate, "syntax error in filter expression"); |
703 | 0 | } |
704 | | |
705 | | int |
706 | | pcap_compile(pcap_t *p, struct bpf_program *program, |
707 | | const char *buf, int optimize, bpf_u_int32 mask) |
708 | 0 | { |
709 | | #ifdef _WIN32 |
710 | | static int done = 0; |
711 | | #endif |
712 | 0 | compiler_state_t cstate; |
713 | 0 | const char * volatile xbuf = buf; |
714 | 0 | yyscan_t scanner = NULL; |
715 | 0 | volatile YY_BUFFER_STATE in_buffer = NULL; |
716 | 0 | u_int len; |
717 | 0 | int rc; |
718 | | |
719 | | /* |
720 | | * If this pcap_t hasn't been activated, it doesn't have a |
721 | | * link-layer type, so we can't use it. |
722 | | */ |
723 | 0 | if (!p->activated) { |
724 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
725 | 0 | "not-yet-activated pcap_t passed to pcap_compile"); |
726 | 0 | return (-1); |
727 | 0 | } |
728 | | |
729 | | #ifdef _WIN32 |
730 | | if (!done) |
731 | | pcap_wsockinit(); |
732 | | done = 1; |
733 | | #endif |
734 | | |
735 | | #ifdef ENABLE_REMOTE |
736 | | /* |
737 | | * If the device on which we're capturing need to be notified |
738 | | * that a new filter is being compiled, do so. |
739 | | * |
740 | | * This allows them to save a copy of it, in case, for example, |
741 | | * they're implementing a form of remote packet capture, and |
742 | | * want the remote machine to filter out the packets in which |
743 | | * it's sending the packets it's captured. |
744 | | * |
745 | | * XXX - the fact that we happen to be compiling a filter |
746 | | * doesn't necessarily mean we'll be installing it as the |
747 | | * filter for this pcap_t; we might be running it from userland |
748 | | * on captured packets to do packet classification. We really |
749 | | * need a better way of handling this, but this is all that |
750 | | * the WinPcap remote capture code did. |
751 | | */ |
752 | | if (p->save_current_filter_op != NULL) |
753 | | (p->save_current_filter_op)(p, buf); |
754 | | #endif |
755 | | |
756 | 0 | initchunks(&cstate); |
757 | 0 | cstate.no_optimize = 0; |
758 | 0 | #ifdef INET6 |
759 | 0 | cstate.ai = NULL; |
760 | 0 | #endif |
761 | 0 | cstate.e = NULL; |
762 | 0 | cstate.ic.root = NULL; |
763 | 0 | cstate.ic.cur_mark = 0; |
764 | 0 | cstate.bpf_pcap = p; |
765 | 0 | init_regs(&cstate); |
766 | |
|
767 | 0 | cstate.netmask = mask; |
768 | |
|
769 | 0 | cstate.snaplen = pcap_snapshot(p); |
770 | 0 | if (cstate.snaplen == 0) { |
771 | 0 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
772 | 0 | "snaplen of 0 rejects all packets"); |
773 | 0 | rc = -1; |
774 | 0 | goto quit; |
775 | 0 | } |
776 | | |
777 | 0 | if (pcap_lex_init(&scanner) != 0) |
778 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
779 | 0 | errno, "can't initialize scanner"); |
780 | 0 | in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner); |
781 | | |
782 | | /* |
783 | | * Associate the compiler state with the lexical analyzer |
784 | | * state. |
785 | | */ |
786 | 0 | pcap_set_extra(&cstate, scanner); |
787 | |
|
788 | 0 | if (init_linktype(&cstate, p) == -1) { |
789 | 0 | rc = -1; |
790 | 0 | goto quit; |
791 | 0 | } |
792 | 0 | if (pcap_parse(scanner, &cstate) != 0) { |
793 | 0 | #ifdef INET6 |
794 | 0 | if (cstate.ai != NULL) |
795 | 0 | freeaddrinfo(cstate.ai); |
796 | 0 | #endif |
797 | 0 | if (cstate.e != NULL) |
798 | 0 | free(cstate.e); |
799 | 0 | rc = -1; |
800 | 0 | goto quit; |
801 | 0 | } |
802 | | |
803 | 0 | if (cstate.ic.root == NULL) { |
804 | | /* |
805 | | * Catch errors reported by gen_retblk(). |
806 | | */ |
807 | 0 | if (setjmp(cstate.top_ctx)) { |
808 | 0 | rc = -1; |
809 | 0 | goto quit; |
810 | 0 | } |
811 | 0 | cstate.ic.root = gen_retblk(&cstate, cstate.snaplen); |
812 | 0 | } |
813 | | |
814 | 0 | if (optimize && !cstate.no_optimize) { |
815 | 0 | if (bpf_optimize(&cstate.ic, p->errbuf) == -1) { |
816 | | /* Failure */ |
817 | 0 | rc = -1; |
818 | 0 | goto quit; |
819 | 0 | } |
820 | 0 | if (cstate.ic.root == NULL || |
821 | 0 | (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) { |
822 | 0 | (void)pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
823 | 0 | "expression rejects all packets"); |
824 | 0 | rc = -1; |
825 | 0 | goto quit; |
826 | 0 | } |
827 | 0 | } |
828 | 0 | program->bf_insns = icode_to_fcode(&cstate.ic, |
829 | 0 | cstate.ic.root, &len, p->errbuf); |
830 | 0 | if (program->bf_insns == NULL) { |
831 | | /* Failure */ |
832 | 0 | rc = -1; |
833 | 0 | goto quit; |
834 | 0 | } |
835 | 0 | program->bf_len = len; |
836 | |
|
837 | 0 | rc = 0; /* We're all okay */ |
838 | |
|
839 | 0 | quit: |
840 | | /* |
841 | | * Clean up everything for the lexical analyzer. |
842 | | */ |
843 | 0 | if (in_buffer != NULL) |
844 | 0 | pcap__delete_buffer(in_buffer, scanner); |
845 | 0 | if (scanner != NULL) |
846 | 0 | pcap_lex_destroy(scanner); |
847 | | |
848 | | /* |
849 | | * Clean up our own allocated memory. |
850 | | */ |
851 | 0 | freechunks(&cstate); |
852 | |
|
853 | 0 | return (rc); |
854 | 0 | } |
855 | | |
856 | | /* |
857 | | * entry point for using the compiler with no pcap open |
858 | | * pass in all the stuff that is needed explicitly instead. |
859 | | */ |
860 | | int |
861 | | pcap_compile_nopcap(int snaplen_arg, int linktype_arg, |
862 | | struct bpf_program *program, |
863 | | const char *buf, int optimize, bpf_u_int32 mask) |
864 | 0 | { |
865 | 0 | pcap_t *p; |
866 | 0 | int ret; |
867 | |
|
868 | 0 | p = pcap_open_dead(linktype_arg, snaplen_arg); |
869 | 0 | if (p == NULL) |
870 | 0 | return (-1); |
871 | 0 | ret = pcap_compile(p, program, buf, optimize, mask); |
872 | 0 | pcap_close(p); |
873 | 0 | return (ret); |
874 | 0 | } |
875 | | |
876 | | /* |
877 | | * Clean up a "struct bpf_program" by freeing all the memory allocated |
878 | | * in it. |
879 | | */ |
880 | | void |
881 | | pcap_freecode(struct bpf_program *program) |
882 | 11.6k | { |
883 | 11.6k | program->bf_len = 0; |
884 | 11.6k | if (program->bf_insns != NULL) { |
885 | 0 | free((char *)program->bf_insns); |
886 | 0 | program->bf_insns = NULL; |
887 | 0 | } |
888 | 11.6k | } |
889 | | |
890 | | /* |
891 | | * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates |
892 | | * which of the jt and jf fields has been resolved and which is a pointer |
893 | | * back to another unresolved block (or nil). At least one of the fields |
894 | | * in each block is already resolved. |
895 | | */ |
896 | | static void |
897 | | backpatch(struct block *list, struct block *target) |
898 | 0 | { |
899 | 0 | struct block *next; |
900 | |
|
901 | 0 | while (list) { |
902 | 0 | if (!list->sense) { |
903 | 0 | next = JT(list); |
904 | 0 | JT(list) = target; |
905 | 0 | } else { |
906 | 0 | next = JF(list); |
907 | 0 | JF(list) = target; |
908 | 0 | } |
909 | 0 | list = next; |
910 | 0 | } |
911 | 0 | } |
912 | | |
913 | | /* |
914 | | * Merge the lists in b0 and b1, using the 'sense' field to indicate |
915 | | * which of jt and jf is the link. |
916 | | */ |
917 | | static void |
918 | | merge(struct block *b0, struct block *b1) |
919 | 0 | { |
920 | 0 | register struct block **p = &b0; |
921 | | |
922 | | /* Find end of list. */ |
923 | 0 | while (*p) |
924 | 0 | p = !((*p)->sense) ? &JT(*p) : &JF(*p); |
925 | | |
926 | | /* Concatenate the lists. */ |
927 | 0 | *p = b1; |
928 | 0 | } |
929 | | |
930 | | int |
931 | | finish_parse(compiler_state_t *cstate, struct block *p) |
932 | 0 | { |
933 | 0 | struct block *ppi_dlt_check; |
934 | | |
935 | | /* |
936 | | * Catch errors reported by us and routines below us, and return -1 |
937 | | * on an error. |
938 | | */ |
939 | 0 | if (setjmp(cstate->top_ctx)) |
940 | 0 | return (-1); |
941 | | |
942 | | /* |
943 | | * Insert before the statements of the first (root) block any |
944 | | * statements needed to load the lengths of any variable-length |
945 | | * headers into registers. |
946 | | * |
947 | | * XXX - a fancier strategy would be to insert those before the |
948 | | * statements of all blocks that use those lengths and that |
949 | | * have no predecessors that use them, so that we only compute |
950 | | * the lengths if we need them. There might be even better |
951 | | * approaches than that. |
952 | | * |
953 | | * However, those strategies would be more complicated, and |
954 | | * as we don't generate code to compute a length if the |
955 | | * program has no tests that use the length, and as most |
956 | | * tests will probably use those lengths, we would just |
957 | | * postpone computing the lengths so that it's not done |
958 | | * for tests that fail early, and it's not clear that's |
959 | | * worth the effort. |
960 | | */ |
961 | 0 | insert_compute_vloffsets(cstate, p->head); |
962 | | |
963 | | /* |
964 | | * For DLT_PPI captures, generate a check of the per-packet |
965 | | * DLT value to make sure it's DLT_IEEE802_11. |
966 | | * |
967 | | * XXX - TurboCap cards use DLT_PPI for Ethernet. |
968 | | * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header |
969 | | * with appropriate Ethernet information and use that rather |
970 | | * than using something such as DLT_PPI where you don't know |
971 | | * the link-layer header type until runtime, which, in the |
972 | | * general case, would force us to generate both Ethernet *and* |
973 | | * 802.11 code (*and* anything else for which PPI is used) |
974 | | * and choose between them early in the BPF program? |
975 | | */ |
976 | 0 | ppi_dlt_check = gen_ppi_dlt_check(cstate); |
977 | 0 | if (ppi_dlt_check != NULL) |
978 | 0 | gen_and(ppi_dlt_check, p); |
979 | |
|
980 | 0 | backpatch(p, gen_retblk(cstate, cstate->snaplen)); |
981 | 0 | p->sense = !p->sense; |
982 | 0 | backpatch(p, gen_retblk(cstate, 0)); |
983 | 0 | cstate->ic.root = p->head; |
984 | 0 | return (0); |
985 | 0 | } |
986 | | |
987 | | void |
988 | | gen_and(struct block *b0, struct block *b1) |
989 | 0 | { |
990 | 0 | backpatch(b0, b1->head); |
991 | 0 | b0->sense = !b0->sense; |
992 | 0 | b1->sense = !b1->sense; |
993 | 0 | merge(b1, b0); |
994 | 0 | b1->sense = !b1->sense; |
995 | 0 | b1->head = b0->head; |
996 | 0 | } |
997 | | |
998 | | void |
999 | | gen_or(struct block *b0, struct block *b1) |
1000 | 0 | { |
1001 | 0 | b0->sense = !b0->sense; |
1002 | 0 | backpatch(b0, b1->head); |
1003 | 0 | b0->sense = !b0->sense; |
1004 | 0 | merge(b1, b0); |
1005 | 0 | b1->head = b0->head; |
1006 | 0 | } |
1007 | | |
1008 | | void |
1009 | | gen_not(struct block *b) |
1010 | 0 | { |
1011 | 0 | b->sense = !b->sense; |
1012 | 0 | } |
1013 | | |
1014 | | static struct block * |
1015 | | gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1016 | | u_int size, bpf_int32 v) |
1017 | 0 | { |
1018 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v); |
1019 | 0 | } |
1020 | | |
1021 | | static struct block * |
1022 | | gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1023 | | u_int size, bpf_int32 v) |
1024 | 0 | { |
1025 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v); |
1026 | 0 | } |
1027 | | |
1028 | | static struct block * |
1029 | | gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1030 | | u_int size, bpf_int32 v) |
1031 | 0 | { |
1032 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v); |
1033 | 0 | } |
1034 | | |
1035 | | static struct block * |
1036 | | gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1037 | | u_int size, bpf_int32 v) |
1038 | 0 | { |
1039 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v); |
1040 | 0 | } |
1041 | | |
1042 | | static struct block * |
1043 | | gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1044 | | u_int size, bpf_int32 v) |
1045 | 0 | { |
1046 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v); |
1047 | 0 | } |
1048 | | |
1049 | | static struct block * |
1050 | | gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1051 | | u_int size, bpf_int32 v, bpf_u_int32 mask) |
1052 | 0 | { |
1053 | 0 | return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v); |
1054 | 0 | } |
1055 | | |
1056 | | static struct block * |
1057 | | gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1058 | | u_int size, const u_char *v) |
1059 | 0 | { |
1060 | 0 | register struct block *b, *tmp; |
1061 | | |
1062 | | /* |
1063 | | * XXX - the actual *instructions* do unsigned comparisons on |
1064 | | * most platforms, and the load instructions don't do sign |
1065 | | * extension, so gen_cmp() should really take an unsigned |
1066 | | * value argument. |
1067 | | * |
1068 | | * As the load instructons also don't do sign-extension, we |
1069 | | * fetch the values from the byte array as unsigned. We don't |
1070 | | * want to use the signed versions of the extract calls. |
1071 | | */ |
1072 | 0 | b = NULL; |
1073 | 0 | while (size >= 4) { |
1074 | 0 | register const u_char *p = &v[size - 4]; |
1075 | |
|
1076 | 0 | tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, |
1077 | 0 | (bpf_int32)EXTRACT_32BITS(p)); |
1078 | 0 | if (b != NULL) |
1079 | 0 | gen_and(b, tmp); |
1080 | 0 | b = tmp; |
1081 | 0 | size -= 4; |
1082 | 0 | } |
1083 | 0 | while (size >= 2) { |
1084 | 0 | register const u_char *p = &v[size - 2]; |
1085 | |
|
1086 | 0 | tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, |
1087 | 0 | (bpf_int32)EXTRACT_16BITS(p)); |
1088 | 0 | if (b != NULL) |
1089 | 0 | gen_and(b, tmp); |
1090 | 0 | b = tmp; |
1091 | 0 | size -= 2; |
1092 | 0 | } |
1093 | 0 | if (size > 0) { |
1094 | 0 | tmp = gen_cmp(cstate, offrel, offset, BPF_B, (bpf_int32)v[0]); |
1095 | 0 | if (b != NULL) |
1096 | 0 | gen_and(b, tmp); |
1097 | 0 | b = tmp; |
1098 | 0 | } |
1099 | 0 | return b; |
1100 | 0 | } |
1101 | | |
1102 | | /* |
1103 | | * AND the field of size "size" at offset "offset" relative to the header |
1104 | | * specified by "offrel" with "mask", and compare it with the value "v" |
1105 | | * with the test specified by "jtype"; if "reverse" is true, the test |
1106 | | * should test the opposite of "jtype". |
1107 | | */ |
1108 | | static struct block * |
1109 | | gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, bpf_u_int32 offset, |
1110 | | bpf_u_int32 size, bpf_u_int32 mask, bpf_u_int32 jtype, int reverse, |
1111 | | bpf_int32 v) |
1112 | 0 | { |
1113 | 0 | struct slist *s, *s2; |
1114 | 0 | struct block *b; |
1115 | |
|
1116 | 0 | s = gen_load_a(cstate, offrel, offset, size); |
1117 | |
|
1118 | 0 | if (mask != 0xffffffff) { |
1119 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
1120 | 0 | s2->s.k = mask; |
1121 | 0 | sappend(s, s2); |
1122 | 0 | } |
1123 | |
|
1124 | 0 | b = new_block(cstate, JMP(jtype)); |
1125 | 0 | b->stmts = s; |
1126 | 0 | b->s.k = v; |
1127 | 0 | if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE)) |
1128 | 0 | gen_not(b); |
1129 | 0 | return b; |
1130 | 0 | } |
1131 | | |
1132 | | static int |
1133 | | init_linktype(compiler_state_t *cstate, pcap_t *p) |
1134 | 0 | { |
1135 | 0 | cstate->pcap_fddipad = p->fddipad; |
1136 | | |
1137 | | /* |
1138 | | * We start out with only one link-layer header. |
1139 | | */ |
1140 | 0 | cstate->outermostlinktype = pcap_datalink(p); |
1141 | 0 | cstate->off_outermostlinkhdr.constant_part = 0; |
1142 | 0 | cstate->off_outermostlinkhdr.is_variable = 0; |
1143 | 0 | cstate->off_outermostlinkhdr.reg = -1; |
1144 | |
|
1145 | 0 | cstate->prevlinktype = cstate->outermostlinktype; |
1146 | 0 | cstate->off_prevlinkhdr.constant_part = 0; |
1147 | 0 | cstate->off_prevlinkhdr.is_variable = 0; |
1148 | 0 | cstate->off_prevlinkhdr.reg = -1; |
1149 | |
|
1150 | 0 | cstate->linktype = cstate->outermostlinktype; |
1151 | 0 | cstate->off_linkhdr.constant_part = 0; |
1152 | 0 | cstate->off_linkhdr.is_variable = 0; |
1153 | 0 | cstate->off_linkhdr.reg = -1; |
1154 | | |
1155 | | /* |
1156 | | * XXX |
1157 | | */ |
1158 | 0 | cstate->off_linkpl.constant_part = 0; |
1159 | 0 | cstate->off_linkpl.is_variable = 0; |
1160 | 0 | cstate->off_linkpl.reg = -1; |
1161 | |
|
1162 | 0 | cstate->off_linktype.constant_part = 0; |
1163 | 0 | cstate->off_linktype.is_variable = 0; |
1164 | 0 | cstate->off_linktype.reg = -1; |
1165 | | |
1166 | | /* |
1167 | | * Assume it's not raw ATM with a pseudo-header, for now. |
1168 | | */ |
1169 | 0 | cstate->is_atm = 0; |
1170 | 0 | cstate->off_vpi = OFFSET_NOT_SET; |
1171 | 0 | cstate->off_vci = OFFSET_NOT_SET; |
1172 | 0 | cstate->off_proto = OFFSET_NOT_SET; |
1173 | 0 | cstate->off_payload = OFFSET_NOT_SET; |
1174 | | |
1175 | | /* |
1176 | | * And not Geneve. |
1177 | | */ |
1178 | 0 | cstate->is_geneve = 0; |
1179 | | |
1180 | | /* |
1181 | | * No variable length VLAN offset by default |
1182 | | */ |
1183 | 0 | cstate->is_vlan_vloffset = 0; |
1184 | | |
1185 | | /* |
1186 | | * And assume we're not doing SS7. |
1187 | | */ |
1188 | 0 | cstate->off_li = OFFSET_NOT_SET; |
1189 | 0 | cstate->off_li_hsl = OFFSET_NOT_SET; |
1190 | 0 | cstate->off_sio = OFFSET_NOT_SET; |
1191 | 0 | cstate->off_opc = OFFSET_NOT_SET; |
1192 | 0 | cstate->off_dpc = OFFSET_NOT_SET; |
1193 | 0 | cstate->off_sls = OFFSET_NOT_SET; |
1194 | |
|
1195 | 0 | cstate->label_stack_depth = 0; |
1196 | 0 | cstate->vlan_stack_depth = 0; |
1197 | |
|
1198 | 0 | switch (cstate->linktype) { |
1199 | | |
1200 | 0 | case DLT_ARCNET: |
1201 | 0 | cstate->off_linktype.constant_part = 2; |
1202 | 0 | cstate->off_linkpl.constant_part = 6; |
1203 | 0 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1204 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1205 | 0 | break; |
1206 | | |
1207 | 0 | case DLT_ARCNET_LINUX: |
1208 | 0 | cstate->off_linktype.constant_part = 4; |
1209 | 0 | cstate->off_linkpl.constant_part = 8; |
1210 | 0 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1211 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1212 | 0 | break; |
1213 | | |
1214 | 0 | case DLT_EN10MB: |
1215 | 0 | cstate->off_linktype.constant_part = 12; |
1216 | 0 | cstate->off_linkpl.constant_part = 14; /* Ethernet header length */ |
1217 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
1218 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
1219 | 0 | break; |
1220 | | |
1221 | 0 | case DLT_SLIP: |
1222 | | /* |
1223 | | * SLIP doesn't have a link level type. The 16 byte |
1224 | | * header is hacked into our SLIP driver. |
1225 | | */ |
1226 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1227 | 0 | cstate->off_linkpl.constant_part = 16; |
1228 | 0 | cstate->off_nl = 0; |
1229 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1230 | 0 | break; |
1231 | | |
1232 | 0 | case DLT_SLIP_BSDOS: |
1233 | | /* XXX this may be the same as the DLT_PPP_BSDOS case */ |
1234 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1235 | | /* XXX end */ |
1236 | 0 | cstate->off_linkpl.constant_part = 24; |
1237 | 0 | cstate->off_nl = 0; |
1238 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1239 | 0 | break; |
1240 | | |
1241 | 0 | case DLT_NULL: |
1242 | 0 | case DLT_LOOP: |
1243 | 0 | cstate->off_linktype.constant_part = 0; |
1244 | 0 | cstate->off_linkpl.constant_part = 4; |
1245 | 0 | cstate->off_nl = 0; |
1246 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1247 | 0 | break; |
1248 | | |
1249 | 0 | case DLT_ENC: |
1250 | 0 | cstate->off_linktype.constant_part = 0; |
1251 | 0 | cstate->off_linkpl.constant_part = 12; |
1252 | 0 | cstate->off_nl = 0; |
1253 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1254 | 0 | break; |
1255 | | |
1256 | 0 | case DLT_PPP: |
1257 | 0 | case DLT_PPP_PPPD: |
1258 | 0 | case DLT_C_HDLC: /* BSD/OS Cisco HDLC */ |
1259 | 0 | case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */ |
1260 | 0 | cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */ |
1261 | 0 | cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */ |
1262 | 0 | cstate->off_nl = 0; |
1263 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1264 | 0 | break; |
1265 | | |
1266 | 0 | case DLT_PPP_ETHER: |
1267 | | /* |
1268 | | * This does no include the Ethernet header, and |
1269 | | * only covers session state. |
1270 | | */ |
1271 | 0 | cstate->off_linktype.constant_part = 6; |
1272 | 0 | cstate->off_linkpl.constant_part = 8; |
1273 | 0 | cstate->off_nl = 0; |
1274 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1275 | 0 | break; |
1276 | | |
1277 | 0 | case DLT_PPP_BSDOS: |
1278 | 0 | cstate->off_linktype.constant_part = 5; |
1279 | 0 | cstate->off_linkpl.constant_part = 24; |
1280 | 0 | cstate->off_nl = 0; |
1281 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1282 | 0 | break; |
1283 | | |
1284 | 0 | case DLT_FDDI: |
1285 | | /* |
1286 | | * FDDI doesn't really have a link-level type field. |
1287 | | * We set "off_linktype" to the offset of the LLC header. |
1288 | | * |
1289 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1290 | | * is being used and pick out the encapsulated Ethernet type. |
1291 | | * XXX - should we generate code to check for SNAP? |
1292 | | */ |
1293 | 0 | cstate->off_linktype.constant_part = 13; |
1294 | 0 | cstate->off_linktype.constant_part += cstate->pcap_fddipad; |
1295 | 0 | cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */ |
1296 | 0 | cstate->off_linkpl.constant_part += cstate->pcap_fddipad; |
1297 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1298 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1299 | 0 | break; |
1300 | | |
1301 | 0 | case DLT_IEEE802: |
1302 | | /* |
1303 | | * Token Ring doesn't really have a link-level type field. |
1304 | | * We set "off_linktype" to the offset of the LLC header. |
1305 | | * |
1306 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1307 | | * is being used and pick out the encapsulated Ethernet type. |
1308 | | * XXX - should we generate code to check for SNAP? |
1309 | | * |
1310 | | * XXX - the header is actually variable-length. |
1311 | | * Some various Linux patched versions gave 38 |
1312 | | * as "off_linktype" and 40 as "off_nl"; however, |
1313 | | * if a token ring packet has *no* routing |
1314 | | * information, i.e. is not source-routed, the correct |
1315 | | * values are 20 and 22, as they are in the vanilla code. |
1316 | | * |
1317 | | * A packet is source-routed iff the uppermost bit |
1318 | | * of the first byte of the source address, at an |
1319 | | * offset of 8, has the uppermost bit set. If the |
1320 | | * packet is source-routed, the total number of bytes |
1321 | | * of routing information is 2 plus bits 0x1F00 of |
1322 | | * the 16-bit value at an offset of 14 (shifted right |
1323 | | * 8 - figure out which byte that is). |
1324 | | */ |
1325 | 0 | cstate->off_linktype.constant_part = 14; |
1326 | 0 | cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */ |
1327 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1328 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1329 | 0 | break; |
1330 | | |
1331 | 0 | case DLT_PRISM_HEADER: |
1332 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
1333 | 0 | case DLT_IEEE802_11_RADIO: |
1334 | 0 | cstate->off_linkhdr.is_variable = 1; |
1335 | | /* Fall through, 802.11 doesn't have a variable link |
1336 | | * prefix but is otherwise the same. */ |
1337 | | /* FALLTHROUGH */ |
1338 | |
|
1339 | 0 | case DLT_IEEE802_11: |
1340 | | /* |
1341 | | * 802.11 doesn't really have a link-level type field. |
1342 | | * We set "off_linktype.constant_part" to the offset of |
1343 | | * the LLC header. |
1344 | | * |
1345 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1346 | | * is being used and pick out the encapsulated Ethernet type. |
1347 | | * XXX - should we generate code to check for SNAP? |
1348 | | * |
1349 | | * We also handle variable-length radio headers here. |
1350 | | * The Prism header is in theory variable-length, but in |
1351 | | * practice it's always 144 bytes long. However, some |
1352 | | * drivers on Linux use ARPHRD_IEEE80211_PRISM, but |
1353 | | * sometimes or always supply an AVS header, so we |
1354 | | * have to check whether the radio header is a Prism |
1355 | | * header or an AVS header, so, in practice, it's |
1356 | | * variable-length. |
1357 | | */ |
1358 | 0 | cstate->off_linktype.constant_part = 24; |
1359 | 0 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1360 | 0 | cstate->off_linkpl.is_variable = 1; |
1361 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1362 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1363 | 0 | break; |
1364 | | |
1365 | 0 | case DLT_PPI: |
1366 | | /* |
1367 | | * At the moment we treat PPI the same way that we treat |
1368 | | * normal Radiotap encoded packets. The difference is in |
1369 | | * the function that generates the code at the beginning |
1370 | | * to compute the header length. Since this code generator |
1371 | | * of PPI supports bare 802.11 encapsulation only (i.e. |
1372 | | * the encapsulated DLT should be DLT_IEEE802_11) we |
1373 | | * generate code to check for this too. |
1374 | | */ |
1375 | 0 | cstate->off_linktype.constant_part = 24; |
1376 | 0 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1377 | 0 | cstate->off_linkpl.is_variable = 1; |
1378 | 0 | cstate->off_linkhdr.is_variable = 1; |
1379 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1380 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1381 | 0 | break; |
1382 | | |
1383 | 0 | case DLT_ATM_RFC1483: |
1384 | 0 | case DLT_ATM_CLIP: /* Linux ATM defines this */ |
1385 | | /* |
1386 | | * assume routed, non-ISO PDUs |
1387 | | * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) |
1388 | | * |
1389 | | * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS, |
1390 | | * or PPP with the PPP NLPID (e.g., PPPoA)? The |
1391 | | * latter would presumably be treated the way PPPoE |
1392 | | * should be, so you can do "pppoe and udp port 2049" |
1393 | | * or "pppoa and tcp port 80" and have it check for |
1394 | | * PPPo{A,E} and a PPP protocol of IP and.... |
1395 | | */ |
1396 | 0 | cstate->off_linktype.constant_part = 0; |
1397 | 0 | cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */ |
1398 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1399 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1400 | 0 | break; |
1401 | | |
1402 | 0 | case DLT_SUNATM: |
1403 | | /* |
1404 | | * Full Frontal ATM; you get AALn PDUs with an ATM |
1405 | | * pseudo-header. |
1406 | | */ |
1407 | 0 | cstate->is_atm = 1; |
1408 | 0 | cstate->off_vpi = SUNATM_VPI_POS; |
1409 | 0 | cstate->off_vci = SUNATM_VCI_POS; |
1410 | 0 | cstate->off_proto = PROTO_POS; |
1411 | 0 | cstate->off_payload = SUNATM_PKT_BEGIN_POS; |
1412 | 0 | cstate->off_linktype.constant_part = cstate->off_payload; |
1413 | 0 | cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */ |
1414 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1415 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1416 | 0 | break; |
1417 | | |
1418 | 0 | case DLT_RAW: |
1419 | 0 | case DLT_IPV4: |
1420 | 0 | case DLT_IPV6: |
1421 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1422 | 0 | cstate->off_linkpl.constant_part = 0; |
1423 | 0 | cstate->off_nl = 0; |
1424 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1425 | 0 | break; |
1426 | | |
1427 | 0 | case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */ |
1428 | 0 | cstate->off_linktype.constant_part = 14; |
1429 | 0 | cstate->off_linkpl.constant_part = 16; |
1430 | 0 | cstate->off_nl = 0; |
1431 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1432 | 0 | break; |
1433 | | |
1434 | 0 | case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */ |
1435 | 0 | cstate->off_linktype.constant_part = 0; |
1436 | 0 | cstate->off_linkpl.constant_part = 20; |
1437 | 0 | cstate->off_nl = 0; |
1438 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1439 | 0 | break; |
1440 | | |
1441 | 0 | case DLT_LTALK: |
1442 | | /* |
1443 | | * LocalTalk does have a 1-byte type field in the LLAP header, |
1444 | | * but really it just indicates whether there is a "short" or |
1445 | | * "long" DDP packet following. |
1446 | | */ |
1447 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1448 | 0 | cstate->off_linkpl.constant_part = 0; |
1449 | 0 | cstate->off_nl = 0; |
1450 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1451 | 0 | break; |
1452 | | |
1453 | 0 | case DLT_IP_OVER_FC: |
1454 | | /* |
1455 | | * RFC 2625 IP-over-Fibre-Channel doesn't really have a |
1456 | | * link-level type field. We set "off_linktype" to the |
1457 | | * offset of the LLC header. |
1458 | | * |
1459 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1460 | | * is being used and pick out the encapsulated Ethernet type. |
1461 | | * XXX - should we generate code to check for SNAP? RFC |
1462 | | * 2625 says SNAP should be used. |
1463 | | */ |
1464 | 0 | cstate->off_linktype.constant_part = 16; |
1465 | 0 | cstate->off_linkpl.constant_part = 16; |
1466 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1467 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1468 | 0 | break; |
1469 | | |
1470 | 0 | case DLT_FRELAY: |
1471 | | /* |
1472 | | * XXX - we should set this to handle SNAP-encapsulated |
1473 | | * frames (NLPID of 0x80). |
1474 | | */ |
1475 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1476 | 0 | cstate->off_linkpl.constant_part = 0; |
1477 | 0 | cstate->off_nl = 0; |
1478 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1479 | 0 | break; |
1480 | | |
1481 | | /* |
1482 | | * the only BPF-interesting FRF.16 frames are non-control frames; |
1483 | | * Frame Relay has a variable length link-layer |
1484 | | * so lets start with offset 4 for now and increments later on (FIXME); |
1485 | | */ |
1486 | 0 | case DLT_MFR: |
1487 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1488 | 0 | cstate->off_linkpl.constant_part = 0; |
1489 | 0 | cstate->off_nl = 4; |
1490 | 0 | cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */ |
1491 | 0 | break; |
1492 | | |
1493 | 0 | case DLT_APPLE_IP_OVER_IEEE1394: |
1494 | 0 | cstate->off_linktype.constant_part = 16; |
1495 | 0 | cstate->off_linkpl.constant_part = 18; |
1496 | 0 | cstate->off_nl = 0; |
1497 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1498 | 0 | break; |
1499 | | |
1500 | 0 | case DLT_SYMANTEC_FIREWALL: |
1501 | 0 | cstate->off_linktype.constant_part = 6; |
1502 | 0 | cstate->off_linkpl.constant_part = 44; |
1503 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
1504 | 0 | cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */ |
1505 | 0 | break; |
1506 | | |
1507 | | #ifdef HAVE_NET_PFVAR_H |
1508 | | case DLT_PFLOG: |
1509 | | cstate->off_linktype.constant_part = 0; |
1510 | | cstate->off_linkpl.constant_part = PFLOG_HDRLEN; |
1511 | | cstate->off_nl = 0; |
1512 | | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1513 | | break; |
1514 | | #endif |
1515 | | |
1516 | 0 | case DLT_JUNIPER_MFR: |
1517 | 0 | case DLT_JUNIPER_MLFR: |
1518 | 0 | case DLT_JUNIPER_MLPPP: |
1519 | 0 | case DLT_JUNIPER_PPP: |
1520 | 0 | case DLT_JUNIPER_CHDLC: |
1521 | 0 | case DLT_JUNIPER_FRELAY: |
1522 | 0 | cstate->off_linktype.constant_part = 4; |
1523 | 0 | cstate->off_linkpl.constant_part = 4; |
1524 | 0 | cstate->off_nl = 0; |
1525 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1526 | 0 | break; |
1527 | | |
1528 | 0 | case DLT_JUNIPER_ATM1: |
1529 | 0 | cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */ |
1530 | 0 | cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */ |
1531 | 0 | cstate->off_nl = 0; |
1532 | 0 | cstate->off_nl_nosnap = 10; |
1533 | 0 | break; |
1534 | | |
1535 | 0 | case DLT_JUNIPER_ATM2: |
1536 | 0 | cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */ |
1537 | 0 | cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */ |
1538 | 0 | cstate->off_nl = 0; |
1539 | 0 | cstate->off_nl_nosnap = 10; |
1540 | 0 | break; |
1541 | | |
1542 | | /* frames captured on a Juniper PPPoE service PIC |
1543 | | * contain raw ethernet frames */ |
1544 | 0 | case DLT_JUNIPER_PPPOE: |
1545 | 0 | case DLT_JUNIPER_ETHER: |
1546 | 0 | cstate->off_linkpl.constant_part = 14; |
1547 | 0 | cstate->off_linktype.constant_part = 16; |
1548 | 0 | cstate->off_nl = 18; /* Ethernet II */ |
1549 | 0 | cstate->off_nl_nosnap = 21; /* 802.3+802.2 */ |
1550 | 0 | break; |
1551 | | |
1552 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
1553 | 0 | cstate->off_linktype.constant_part = 4; |
1554 | 0 | cstate->off_linkpl.constant_part = 6; |
1555 | 0 | cstate->off_nl = 0; |
1556 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1557 | 0 | break; |
1558 | | |
1559 | 0 | case DLT_JUNIPER_GGSN: |
1560 | 0 | cstate->off_linktype.constant_part = 6; |
1561 | 0 | cstate->off_linkpl.constant_part = 12; |
1562 | 0 | cstate->off_nl = 0; |
1563 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1564 | 0 | break; |
1565 | | |
1566 | 0 | case DLT_JUNIPER_ES: |
1567 | 0 | cstate->off_linktype.constant_part = 6; |
1568 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
1569 | 0 | cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
1570 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1571 | 0 | break; |
1572 | | |
1573 | 0 | case DLT_JUNIPER_MONITOR: |
1574 | 0 | cstate->off_linktype.constant_part = 12; |
1575 | 0 | cstate->off_linkpl.constant_part = 12; |
1576 | 0 | cstate->off_nl = 0; /* raw IP/IP6 header */ |
1577 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1578 | 0 | break; |
1579 | | |
1580 | 0 | case DLT_BACNET_MS_TP: |
1581 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1582 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1583 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1584 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1585 | 0 | break; |
1586 | | |
1587 | 0 | case DLT_JUNIPER_SERVICES: |
1588 | 0 | cstate->off_linktype.constant_part = 12; |
1589 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
1590 | 0 | cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
1591 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1592 | 0 | break; |
1593 | | |
1594 | 0 | case DLT_JUNIPER_VP: |
1595 | 0 | cstate->off_linktype.constant_part = 18; |
1596 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1597 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1598 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1599 | 0 | break; |
1600 | | |
1601 | 0 | case DLT_JUNIPER_ST: |
1602 | 0 | cstate->off_linktype.constant_part = 18; |
1603 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1604 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1605 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1606 | 0 | break; |
1607 | | |
1608 | 0 | case DLT_JUNIPER_ISM: |
1609 | 0 | cstate->off_linktype.constant_part = 8; |
1610 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1611 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1612 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1613 | 0 | break; |
1614 | | |
1615 | 0 | case DLT_JUNIPER_VS: |
1616 | 0 | case DLT_JUNIPER_SRX_E2E: |
1617 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
1618 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
1619 | 0 | cstate->off_linktype.constant_part = 8; |
1620 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1621 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1622 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1623 | 0 | break; |
1624 | | |
1625 | 0 | case DLT_MTP2: |
1626 | 0 | cstate->off_li = 2; |
1627 | 0 | cstate->off_li_hsl = 4; |
1628 | 0 | cstate->off_sio = 3; |
1629 | 0 | cstate->off_opc = 4; |
1630 | 0 | cstate->off_dpc = 4; |
1631 | 0 | cstate->off_sls = 7; |
1632 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1633 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1634 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1635 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1636 | 0 | break; |
1637 | | |
1638 | 0 | case DLT_MTP2_WITH_PHDR: |
1639 | 0 | cstate->off_li = 6; |
1640 | 0 | cstate->off_li_hsl = 8; |
1641 | 0 | cstate->off_sio = 7; |
1642 | 0 | cstate->off_opc = 8; |
1643 | 0 | cstate->off_dpc = 8; |
1644 | 0 | cstate->off_sls = 11; |
1645 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1646 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1647 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1648 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1649 | 0 | break; |
1650 | | |
1651 | 0 | case DLT_ERF: |
1652 | 0 | cstate->off_li = 22; |
1653 | 0 | cstate->off_li_hsl = 24; |
1654 | 0 | cstate->off_sio = 23; |
1655 | 0 | cstate->off_opc = 24; |
1656 | 0 | cstate->off_dpc = 24; |
1657 | 0 | cstate->off_sls = 27; |
1658 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1659 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1660 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1661 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1662 | 0 | break; |
1663 | | |
1664 | 0 | case DLT_PFSYNC: |
1665 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1666 | 0 | cstate->off_linkpl.constant_part = 4; |
1667 | 0 | cstate->off_nl = 0; |
1668 | 0 | cstate->off_nl_nosnap = 0; |
1669 | 0 | break; |
1670 | | |
1671 | 0 | case DLT_AX25_KISS: |
1672 | | /* |
1673 | | * Currently, only raw "link[N:M]" filtering is supported. |
1674 | | */ |
1675 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
1676 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1677 | 0 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
1678 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1679 | 0 | break; |
1680 | | |
1681 | 0 | case DLT_IPNET: |
1682 | 0 | cstate->off_linktype.constant_part = 1; |
1683 | 0 | cstate->off_linkpl.constant_part = 24; /* ipnet header length */ |
1684 | 0 | cstate->off_nl = 0; |
1685 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1686 | 0 | break; |
1687 | | |
1688 | 0 | case DLT_NETANALYZER: |
1689 | 0 | cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */ |
1690 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
1691 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */ |
1692 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
1693 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
1694 | 0 | break; |
1695 | | |
1696 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
1697 | 0 | cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */ |
1698 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
1699 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */ |
1700 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
1701 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
1702 | 0 | break; |
1703 | | |
1704 | 0 | default: |
1705 | | /* |
1706 | | * For values in the range in which we've assigned new |
1707 | | * DLT_ values, only raw "link[N:M]" filtering is supported. |
1708 | | */ |
1709 | 0 | if (cstate->linktype >= DLT_MATCHING_MIN && |
1710 | 0 | cstate->linktype <= DLT_MATCHING_MAX) { |
1711 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1712 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
1713 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
1714 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
1715 | 0 | } else { |
1716 | 0 | bpf_set_error(cstate, "unknown data link type %d", cstate->linktype); |
1717 | 0 | return (-1); |
1718 | 0 | } |
1719 | 0 | break; |
1720 | 0 | } |
1721 | | |
1722 | 0 | cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr; |
1723 | 0 | return (0); |
1724 | 0 | } |
1725 | | |
1726 | | /* |
1727 | | * Load a value relative to the specified absolute offset. |
1728 | | */ |
1729 | | static struct slist * |
1730 | | gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset, |
1731 | | u_int offset, u_int size) |
1732 | 0 | { |
1733 | 0 | struct slist *s, *s2; |
1734 | |
|
1735 | 0 | s = gen_abs_offset_varpart(cstate, abs_offset); |
1736 | | |
1737 | | /* |
1738 | | * If "s" is non-null, it has code to arrange that the X register |
1739 | | * contains the variable part of the absolute offset, so we |
1740 | | * generate a load relative to that, with an offset of |
1741 | | * abs_offset->constant_part + offset. |
1742 | | * |
1743 | | * Otherwise, we can do an absolute load with an offset of |
1744 | | * abs_offset->constant_part + offset. |
1745 | | */ |
1746 | 0 | if (s != NULL) { |
1747 | | /* |
1748 | | * "s" points to a list of statements that puts the |
1749 | | * variable part of the absolute offset into the X register. |
1750 | | * Do an indirect load, to use the X register as an offset. |
1751 | | */ |
1752 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
1753 | 0 | s2->s.k = abs_offset->constant_part + offset; |
1754 | 0 | sappend(s, s2); |
1755 | 0 | } else { |
1756 | | /* |
1757 | | * There is no variable part of the absolute offset, so |
1758 | | * just do an absolute load. |
1759 | | */ |
1760 | 0 | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
1761 | 0 | s->s.k = abs_offset->constant_part + offset; |
1762 | 0 | } |
1763 | 0 | return s; |
1764 | 0 | } |
1765 | | |
1766 | | /* |
1767 | | * Load a value relative to the beginning of the specified header. |
1768 | | */ |
1769 | | static struct slist * |
1770 | | gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1771 | | u_int size) |
1772 | 0 | { |
1773 | 0 | struct slist *s, *s2; |
1774 | | |
1775 | | /* |
1776 | | * Squelch warnings from compilers that *don't* assume that |
1777 | | * offrel always has a valid enum value and therefore don't |
1778 | | * assume that we'll always go through one of the case arms. |
1779 | | * |
1780 | | * If we have a default case, compilers that *do* assume that |
1781 | | * will then complain about the default case code being |
1782 | | * unreachable. |
1783 | | * |
1784 | | * Damned if you do, damned if you don't. |
1785 | | */ |
1786 | 0 | s = NULL; |
1787 | |
|
1788 | 0 | switch (offrel) { |
1789 | | |
1790 | 0 | case OR_PACKET: |
1791 | 0 | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
1792 | 0 | s->s.k = offset; |
1793 | 0 | break; |
1794 | | |
1795 | 0 | case OR_LINKHDR: |
1796 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size); |
1797 | 0 | break; |
1798 | | |
1799 | 0 | case OR_PREVLINKHDR: |
1800 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size); |
1801 | 0 | break; |
1802 | | |
1803 | 0 | case OR_LLC: |
1804 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size); |
1805 | 0 | break; |
1806 | | |
1807 | 0 | case OR_PREVMPLSHDR: |
1808 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size); |
1809 | 0 | break; |
1810 | | |
1811 | 0 | case OR_LINKPL: |
1812 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size); |
1813 | 0 | break; |
1814 | | |
1815 | 0 | case OR_LINKPL_NOSNAP: |
1816 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size); |
1817 | 0 | break; |
1818 | | |
1819 | 0 | case OR_LINKTYPE: |
1820 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size); |
1821 | 0 | break; |
1822 | | |
1823 | 0 | case OR_TRAN_IPV4: |
1824 | | /* |
1825 | | * Load the X register with the length of the IPv4 header |
1826 | | * (plus the offset of the link-layer header, if it's |
1827 | | * preceded by a variable-length header such as a radio |
1828 | | * header), in bytes. |
1829 | | */ |
1830 | 0 | s = gen_loadx_iphdrlen(cstate); |
1831 | | |
1832 | | /* |
1833 | | * Load the item at {offset of the link-layer payload} + |
1834 | | * {offset, relative to the start of the link-layer |
1835 | | * paylod, of the IPv4 header} + {length of the IPv4 header} + |
1836 | | * {specified offset}. |
1837 | | * |
1838 | | * If the offset of the link-layer payload is variable, |
1839 | | * the variable part of that offset is included in the |
1840 | | * value in the X register, and we include the constant |
1841 | | * part in the offset of the load. |
1842 | | */ |
1843 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
1844 | 0 | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset; |
1845 | 0 | sappend(s, s2); |
1846 | 0 | break; |
1847 | | |
1848 | 0 | case OR_TRAN_IPV6: |
1849 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size); |
1850 | 0 | break; |
1851 | 0 | } |
1852 | 0 | return s; |
1853 | 0 | } |
1854 | | |
1855 | | /* |
1856 | | * Generate code to load into the X register the sum of the length of |
1857 | | * the IPv4 header and the variable part of the offset of the link-layer |
1858 | | * payload. |
1859 | | */ |
1860 | | static struct slist * |
1861 | | gen_loadx_iphdrlen(compiler_state_t *cstate) |
1862 | 0 | { |
1863 | 0 | struct slist *s, *s2; |
1864 | |
|
1865 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
1866 | 0 | if (s != NULL) { |
1867 | | /* |
1868 | | * The offset of the link-layer payload has a variable |
1869 | | * part. "s" points to a list of statements that put |
1870 | | * the variable part of that offset into the X register. |
1871 | | * |
1872 | | * The 4*([k]&0xf) addressing mode can't be used, as we |
1873 | | * don't have a constant offset, so we have to load the |
1874 | | * value in question into the A register and add to it |
1875 | | * the value from the X register. |
1876 | | */ |
1877 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
1878 | 0 | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
1879 | 0 | sappend(s, s2); |
1880 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
1881 | 0 | s2->s.k = 0xf; |
1882 | 0 | sappend(s, s2); |
1883 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
1884 | 0 | s2->s.k = 2; |
1885 | 0 | sappend(s, s2); |
1886 | | |
1887 | | /* |
1888 | | * The A register now contains the length of the IP header. |
1889 | | * We need to add to it the variable part of the offset of |
1890 | | * the link-layer payload, which is still in the X |
1891 | | * register, and move the result into the X register. |
1892 | | */ |
1893 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
1894 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
1895 | 0 | } else { |
1896 | | /* |
1897 | | * The offset of the link-layer payload is a constant, |
1898 | | * so no code was generated to load the (non-existent) |
1899 | | * variable part of that offset. |
1900 | | * |
1901 | | * This means we can use the 4*([k]&0xf) addressing |
1902 | | * mode. Load the length of the IPv4 header, which |
1903 | | * is at an offset of cstate->off_nl from the beginning of |
1904 | | * the link-layer payload, and thus at an offset of |
1905 | | * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning |
1906 | | * of the raw packet data, using that addressing mode. |
1907 | | */ |
1908 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); |
1909 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
1910 | 0 | } |
1911 | 0 | return s; |
1912 | 0 | } |
1913 | | |
1914 | | |
1915 | | static struct block * |
1916 | | gen_uncond(compiler_state_t *cstate, int rsense) |
1917 | 0 | { |
1918 | 0 | struct block *b; |
1919 | 0 | struct slist *s; |
1920 | |
|
1921 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
1922 | 0 | s->s.k = !rsense; |
1923 | 0 | b = new_block(cstate, JMP(BPF_JEQ)); |
1924 | 0 | b->stmts = s; |
1925 | |
|
1926 | 0 | return b; |
1927 | 0 | } |
1928 | | |
1929 | | static inline struct block * |
1930 | | gen_true(compiler_state_t *cstate) |
1931 | 0 | { |
1932 | 0 | return gen_uncond(cstate, 1); |
1933 | 0 | } |
1934 | | |
1935 | | static inline struct block * |
1936 | | gen_false(compiler_state_t *cstate) |
1937 | 0 | { |
1938 | 0 | return gen_uncond(cstate, 0); |
1939 | 0 | } |
1940 | | |
1941 | | /* |
1942 | | * Byte-swap a 32-bit number. |
1943 | | * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on |
1944 | | * big-endian platforms.) |
1945 | | */ |
1946 | 0 | #define SWAPLONG(y) \ |
1947 | 0 | ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff)) |
1948 | | |
1949 | | /* |
1950 | | * Generate code to match a particular packet type. |
1951 | | * |
1952 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
1953 | | * value, if <= ETHERMTU. We use that to determine whether to |
1954 | | * match the type/length field or to check the type/length field for |
1955 | | * a value <= ETHERMTU to see whether it's a type field and then do |
1956 | | * the appropriate test. |
1957 | | */ |
1958 | | static struct block * |
1959 | | gen_ether_linktype(compiler_state_t *cstate, int proto) |
1960 | 0 | { |
1961 | 0 | struct block *b0, *b1; |
1962 | |
|
1963 | 0 | switch (proto) { |
1964 | | |
1965 | 0 | case LLCSAP_ISONS: |
1966 | 0 | case LLCSAP_IP: |
1967 | 0 | case LLCSAP_NETBEUI: |
1968 | | /* |
1969 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
1970 | | * so we check the DSAP and SSAP. |
1971 | | * |
1972 | | * LLCSAP_IP checks for IP-over-802.2, rather |
1973 | | * than IP-over-Ethernet or IP-over-SNAP. |
1974 | | * |
1975 | | * XXX - should we check both the DSAP and the |
1976 | | * SSAP, like this, or should we check just the |
1977 | | * DSAP, as we do for other types <= ETHERMTU |
1978 | | * (i.e., other SAP values)? |
1979 | | */ |
1980 | 0 | b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
1981 | 0 | gen_not(b0); |
1982 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32) |
1983 | 0 | ((proto << 8) | proto)); |
1984 | 0 | gen_and(b0, b1); |
1985 | 0 | return b1; |
1986 | | |
1987 | 0 | case LLCSAP_IPX: |
1988 | | /* |
1989 | | * Check for; |
1990 | | * |
1991 | | * Ethernet_II frames, which are Ethernet |
1992 | | * frames with a frame type of ETHERTYPE_IPX; |
1993 | | * |
1994 | | * Ethernet_802.3 frames, which are 802.3 |
1995 | | * frames (i.e., the type/length field is |
1996 | | * a length field, <= ETHERMTU, rather than |
1997 | | * a type field) with the first two bytes |
1998 | | * after the Ethernet/802.3 header being |
1999 | | * 0xFFFF; |
2000 | | * |
2001 | | * Ethernet_802.2 frames, which are 802.3 |
2002 | | * frames with an 802.2 LLC header and |
2003 | | * with the IPX LSAP as the DSAP in the LLC |
2004 | | * header; |
2005 | | * |
2006 | | * Ethernet_SNAP frames, which are 802.3 |
2007 | | * frames with an LLC header and a SNAP |
2008 | | * header and with an OUI of 0x000000 |
2009 | | * (encapsulated Ethernet) and a protocol |
2010 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2011 | | * |
2012 | | * XXX - should we generate the same code both |
2013 | | * for tests for LLCSAP_IPX and for ETHERTYPE_IPX? |
2014 | | */ |
2015 | | |
2016 | | /* |
2017 | | * This generates code to check both for the |
2018 | | * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3. |
2019 | | */ |
2020 | 0 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX); |
2021 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF); |
2022 | 0 | gen_or(b0, b1); |
2023 | | |
2024 | | /* |
2025 | | * Now we add code to check for SNAP frames with |
2026 | | * ETHERTYPE_IPX, i.e. Ethernet_SNAP. |
2027 | | */ |
2028 | 0 | b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2029 | 0 | gen_or(b0, b1); |
2030 | | |
2031 | | /* |
2032 | | * Now we generate code to check for 802.3 |
2033 | | * frames in general. |
2034 | | */ |
2035 | 0 | b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2036 | 0 | gen_not(b0); |
2037 | | |
2038 | | /* |
2039 | | * Now add the check for 802.3 frames before the |
2040 | | * check for Ethernet_802.2 and Ethernet_802.3, |
2041 | | * as those checks should only be done on 802.3 |
2042 | | * frames, not on Ethernet frames. |
2043 | | */ |
2044 | 0 | gen_and(b0, b1); |
2045 | | |
2046 | | /* |
2047 | | * Now add the check for Ethernet_II frames, and |
2048 | | * do that before checking for the other frame |
2049 | | * types. |
2050 | | */ |
2051 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX); |
2052 | 0 | gen_or(b0, b1); |
2053 | 0 | return b1; |
2054 | | |
2055 | 0 | case ETHERTYPE_ATALK: |
2056 | 0 | case ETHERTYPE_AARP: |
2057 | | /* |
2058 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2059 | | * layer) may use 802.2 encapsulation. |
2060 | | */ |
2061 | | |
2062 | | /* |
2063 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2064 | | * we check for an Ethernet type field less than |
2065 | | * 1500, which means it's an 802.3 length field. |
2066 | | */ |
2067 | 0 | b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2068 | 0 | gen_not(b0); |
2069 | | |
2070 | | /* |
2071 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2072 | | * SNAP packets with an organization code of |
2073 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2074 | | * type of ETHERTYPE_ATALK (Appletalk). |
2075 | | * |
2076 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2077 | | * SNAP packets with an organization code of |
2078 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2079 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2080 | | */ |
2081 | 0 | if (proto == ETHERTYPE_ATALK) |
2082 | 0 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2083 | 0 | else /* proto == ETHERTYPE_AARP */ |
2084 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2085 | 0 | gen_and(b0, b1); |
2086 | | |
2087 | | /* |
2088 | | * Check for Ethernet encapsulation (Ethertalk |
2089 | | * phase 1?); we just check for the Ethernet |
2090 | | * protocol type. |
2091 | | */ |
2092 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
2093 | |
|
2094 | 0 | gen_or(b0, b1); |
2095 | 0 | return b1; |
2096 | | |
2097 | 0 | default: |
2098 | 0 | if (proto <= ETHERMTU) { |
2099 | | /* |
2100 | | * This is an LLC SAP value, so the frames |
2101 | | * that match would be 802.2 frames. |
2102 | | * Check that the frame is an 802.2 frame |
2103 | | * (i.e., that the length/type field is |
2104 | | * a length field, <= ETHERMTU) and |
2105 | | * then check the DSAP. |
2106 | | */ |
2107 | 0 | b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2108 | 0 | gen_not(b0); |
2109 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, (bpf_int32)proto); |
2110 | 0 | gen_and(b0, b1); |
2111 | 0 | return b1; |
2112 | 0 | } else { |
2113 | | /* |
2114 | | * This is an Ethernet type, so compare |
2115 | | * the length/type field with it (if |
2116 | | * the frame is an 802.2 frame, the length |
2117 | | * field will be <= ETHERMTU, and, as |
2118 | | * "proto" is > ETHERMTU, this test |
2119 | | * will fail and the frame won't match, |
2120 | | * which is what we want). |
2121 | | */ |
2122 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
2123 | 0 | (bpf_int32)proto); |
2124 | 0 | } |
2125 | 0 | } |
2126 | 0 | } |
2127 | | |
2128 | | static struct block * |
2129 | | gen_loopback_linktype(compiler_state_t *cstate, int proto) |
2130 | 0 | { |
2131 | | /* |
2132 | | * For DLT_NULL, the link-layer header is a 32-bit word |
2133 | | * containing an AF_ value in *host* byte order, and for |
2134 | | * DLT_ENC, the link-layer header begins with a 32-bit |
2135 | | * word containing an AF_ value in host byte order. |
2136 | | * |
2137 | | * In addition, if we're reading a saved capture file, |
2138 | | * the host byte order in the capture may not be the |
2139 | | * same as the host byte order on this machine. |
2140 | | * |
2141 | | * For DLT_LOOP, the link-layer header is a 32-bit |
2142 | | * word containing an AF_ value in *network* byte order. |
2143 | | */ |
2144 | 0 | if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) { |
2145 | | /* |
2146 | | * The AF_ value is in host byte order, but the BPF |
2147 | | * interpreter will convert it to network byte order. |
2148 | | * |
2149 | | * If this is a save file, and it's from a machine |
2150 | | * with the opposite byte order to ours, we byte-swap |
2151 | | * the AF_ value. |
2152 | | * |
2153 | | * Then we run it through "htonl()", and generate |
2154 | | * code to compare against the result. |
2155 | | */ |
2156 | 0 | if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped) |
2157 | 0 | proto = SWAPLONG(proto); |
2158 | 0 | proto = htonl(proto); |
2159 | 0 | } |
2160 | 0 | return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, (bpf_int32)proto)); |
2161 | 0 | } |
2162 | | |
2163 | | /* |
2164 | | * "proto" is an Ethernet type value and for IPNET, if it is not IPv4 |
2165 | | * or IPv6 then we have an error. |
2166 | | */ |
2167 | | static struct block * |
2168 | | gen_ipnet_linktype(compiler_state_t *cstate, int proto) |
2169 | 0 | { |
2170 | 0 | switch (proto) { |
2171 | | |
2172 | 0 | case ETHERTYPE_IP: |
2173 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, (bpf_int32)IPH_AF_INET); |
2174 | | /*NOTREACHED*/ |
2175 | | |
2176 | 0 | case ETHERTYPE_IPV6: |
2177 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
2178 | 0 | (bpf_int32)IPH_AF_INET6); |
2179 | | /*NOTREACHED*/ |
2180 | | |
2181 | 0 | default: |
2182 | 0 | break; |
2183 | 0 | } |
2184 | | |
2185 | 0 | return gen_false(cstate); |
2186 | 0 | } |
2187 | | |
2188 | | /* |
2189 | | * Generate code to match a particular packet type. |
2190 | | * |
2191 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
2192 | | * value, if <= ETHERMTU. We use that to determine whether to |
2193 | | * match the type field or to check the type field for the special |
2194 | | * LINUX_SLL_P_802_2 value and then do the appropriate test. |
2195 | | */ |
2196 | | static struct block * |
2197 | | gen_linux_sll_linktype(compiler_state_t *cstate, int proto) |
2198 | 0 | { |
2199 | 0 | struct block *b0, *b1; |
2200 | |
|
2201 | 0 | switch (proto) { |
2202 | | |
2203 | 0 | case LLCSAP_ISONS: |
2204 | 0 | case LLCSAP_IP: |
2205 | 0 | case LLCSAP_NETBEUI: |
2206 | | /* |
2207 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
2208 | | * so we check the DSAP and SSAP. |
2209 | | * |
2210 | | * LLCSAP_IP checks for IP-over-802.2, rather |
2211 | | * than IP-over-Ethernet or IP-over-SNAP. |
2212 | | * |
2213 | | * XXX - should we check both the DSAP and the |
2214 | | * SSAP, like this, or should we check just the |
2215 | | * DSAP, as we do for other types <= ETHERMTU |
2216 | | * (i.e., other SAP values)? |
2217 | | */ |
2218 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2219 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32) |
2220 | 0 | ((proto << 8) | proto)); |
2221 | 0 | gen_and(b0, b1); |
2222 | 0 | return b1; |
2223 | | |
2224 | 0 | case LLCSAP_IPX: |
2225 | | /* |
2226 | | * Ethernet_II frames, which are Ethernet |
2227 | | * frames with a frame type of ETHERTYPE_IPX; |
2228 | | * |
2229 | | * Ethernet_802.3 frames, which have a frame |
2230 | | * type of LINUX_SLL_P_802_3; |
2231 | | * |
2232 | | * Ethernet_802.2 frames, which are 802.3 |
2233 | | * frames with an 802.2 LLC header (i.e, have |
2234 | | * a frame type of LINUX_SLL_P_802_2) and |
2235 | | * with the IPX LSAP as the DSAP in the LLC |
2236 | | * header; |
2237 | | * |
2238 | | * Ethernet_SNAP frames, which are 802.3 |
2239 | | * frames with an LLC header and a SNAP |
2240 | | * header and with an OUI of 0x000000 |
2241 | | * (encapsulated Ethernet) and a protocol |
2242 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2243 | | * |
2244 | | * First, do the checks on LINUX_SLL_P_802_2 |
2245 | | * frames; generate the check for either |
2246 | | * Ethernet_802.2 or Ethernet_SNAP frames, and |
2247 | | * then put a check for LINUX_SLL_P_802_2 frames |
2248 | | * before it. |
2249 | | */ |
2250 | 0 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX); |
2251 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2252 | 0 | gen_or(b0, b1); |
2253 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2254 | 0 | gen_and(b0, b1); |
2255 | | |
2256 | | /* |
2257 | | * Now check for 802.3 frames and OR that with |
2258 | | * the previous test. |
2259 | | */ |
2260 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3); |
2261 | 0 | gen_or(b0, b1); |
2262 | | |
2263 | | /* |
2264 | | * Now add the check for Ethernet_II frames, and |
2265 | | * do that before checking for the other frame |
2266 | | * types. |
2267 | | */ |
2268 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX); |
2269 | 0 | gen_or(b0, b1); |
2270 | 0 | return b1; |
2271 | | |
2272 | 0 | case ETHERTYPE_ATALK: |
2273 | 0 | case ETHERTYPE_AARP: |
2274 | | /* |
2275 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2276 | | * layer) may use 802.2 encapsulation. |
2277 | | */ |
2278 | | |
2279 | | /* |
2280 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2281 | | * we check for the 802.2 protocol type in the |
2282 | | * "Ethernet type" field. |
2283 | | */ |
2284 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2285 | | |
2286 | | /* |
2287 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2288 | | * SNAP packets with an organization code of |
2289 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2290 | | * type of ETHERTYPE_ATALK (Appletalk). |
2291 | | * |
2292 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2293 | | * SNAP packets with an organization code of |
2294 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2295 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2296 | | */ |
2297 | 0 | if (proto == ETHERTYPE_ATALK) |
2298 | 0 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2299 | 0 | else /* proto == ETHERTYPE_AARP */ |
2300 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2301 | 0 | gen_and(b0, b1); |
2302 | | |
2303 | | /* |
2304 | | * Check for Ethernet encapsulation (Ethertalk |
2305 | | * phase 1?); we just check for the Ethernet |
2306 | | * protocol type. |
2307 | | */ |
2308 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
2309 | |
|
2310 | 0 | gen_or(b0, b1); |
2311 | 0 | return b1; |
2312 | | |
2313 | 0 | default: |
2314 | 0 | if (proto <= ETHERMTU) { |
2315 | | /* |
2316 | | * This is an LLC SAP value, so the frames |
2317 | | * that match would be 802.2 frames. |
2318 | | * Check for the 802.2 protocol type |
2319 | | * in the "Ethernet type" field, and |
2320 | | * then check the DSAP. |
2321 | | */ |
2322 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2323 | 0 | b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B, |
2324 | 0 | (bpf_int32)proto); |
2325 | 0 | gen_and(b0, b1); |
2326 | 0 | return b1; |
2327 | 0 | } else { |
2328 | | /* |
2329 | | * This is an Ethernet type, so compare |
2330 | | * the length/type field with it (if |
2331 | | * the frame is an 802.2 frame, the length |
2332 | | * field will be <= ETHERMTU, and, as |
2333 | | * "proto" is > ETHERMTU, this test |
2334 | | * will fail and the frame won't match, |
2335 | | * which is what we want). |
2336 | | */ |
2337 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
2338 | 0 | } |
2339 | 0 | } |
2340 | 0 | } |
2341 | | |
2342 | | static struct slist * |
2343 | | gen_load_prism_llprefixlen(compiler_state_t *cstate) |
2344 | 0 | { |
2345 | 0 | struct slist *s1, *s2; |
2346 | 0 | struct slist *sjeq_avs_cookie; |
2347 | 0 | struct slist *sjcommon; |
2348 | | |
2349 | | /* |
2350 | | * This code is not compatible with the optimizer, as |
2351 | | * we are generating jmp instructions within a normal |
2352 | | * slist of instructions |
2353 | | */ |
2354 | 0 | cstate->no_optimize = 1; |
2355 | | |
2356 | | /* |
2357 | | * Generate code to load the length of the radio header into |
2358 | | * the register assigned to hold that length, if one has been |
2359 | | * assigned. (If one hasn't been assigned, no code we've |
2360 | | * generated uses that prefix, so we don't need to generate any |
2361 | | * code to load it.) |
2362 | | * |
2363 | | * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes |
2364 | | * or always use the AVS header rather than the Prism header. |
2365 | | * We load a 4-byte big-endian value at the beginning of the |
2366 | | * raw packet data, and see whether, when masked with 0xFFFFF000, |
2367 | | * it's equal to 0x80211000. If so, that indicates that it's |
2368 | | * an AVS header (the masked-out bits are the version number). |
2369 | | * Otherwise, it's a Prism header. |
2370 | | * |
2371 | | * XXX - the Prism header is also, in theory, variable-length, |
2372 | | * but no known software generates headers that aren't 144 |
2373 | | * bytes long. |
2374 | | */ |
2375 | 0 | if (cstate->off_linkhdr.reg != -1) { |
2376 | | /* |
2377 | | * Load the cookie. |
2378 | | */ |
2379 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2380 | 0 | s1->s.k = 0; |
2381 | | |
2382 | | /* |
2383 | | * AND it with 0xFFFFF000. |
2384 | | */ |
2385 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2386 | 0 | s2->s.k = 0xFFFFF000; |
2387 | 0 | sappend(s1, s2); |
2388 | | |
2389 | | /* |
2390 | | * Compare with 0x80211000. |
2391 | | */ |
2392 | 0 | sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ)); |
2393 | 0 | sjeq_avs_cookie->s.k = 0x80211000; |
2394 | 0 | sappend(s1, sjeq_avs_cookie); |
2395 | | |
2396 | | /* |
2397 | | * If it's AVS: |
2398 | | * |
2399 | | * The 4 bytes at an offset of 4 from the beginning of |
2400 | | * the AVS header are the length of the AVS header. |
2401 | | * That field is big-endian. |
2402 | | */ |
2403 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2404 | 0 | s2->s.k = 4; |
2405 | 0 | sappend(s1, s2); |
2406 | 0 | sjeq_avs_cookie->s.jt = s2; |
2407 | | |
2408 | | /* |
2409 | | * Now jump to the code to allocate a register |
2410 | | * into which to save the header length and |
2411 | | * store the length there. (The "jump always" |
2412 | | * instruction needs to have the k field set; |
2413 | | * it's added to the PC, so, as we're jumping |
2414 | | * over a single instruction, it should be 1.) |
2415 | | */ |
2416 | 0 | sjcommon = new_stmt(cstate, JMP(BPF_JA)); |
2417 | 0 | sjcommon->s.k = 1; |
2418 | 0 | sappend(s1, sjcommon); |
2419 | | |
2420 | | /* |
2421 | | * Now for the code that handles the Prism header. |
2422 | | * Just load the length of the Prism header (144) |
2423 | | * into the A register. Have the test for an AVS |
2424 | | * header branch here if we don't have an AVS header. |
2425 | | */ |
2426 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
2427 | 0 | s2->s.k = 144; |
2428 | 0 | sappend(s1, s2); |
2429 | 0 | sjeq_avs_cookie->s.jf = s2; |
2430 | | |
2431 | | /* |
2432 | | * Now allocate a register to hold that value and store |
2433 | | * it. The code for the AVS header will jump here after |
2434 | | * loading the length of the AVS header. |
2435 | | */ |
2436 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2437 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
2438 | 0 | sappend(s1, s2); |
2439 | 0 | sjcommon->s.jf = s2; |
2440 | | |
2441 | | /* |
2442 | | * Now move it into the X register. |
2443 | | */ |
2444 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2445 | 0 | sappend(s1, s2); |
2446 | |
|
2447 | 0 | return (s1); |
2448 | 0 | } else |
2449 | 0 | return (NULL); |
2450 | 0 | } |
2451 | | |
2452 | | static struct slist * |
2453 | | gen_load_avs_llprefixlen(compiler_state_t *cstate) |
2454 | 0 | { |
2455 | 0 | struct slist *s1, *s2; |
2456 | | |
2457 | | /* |
2458 | | * Generate code to load the length of the AVS header into |
2459 | | * the register assigned to hold that length, if one has been |
2460 | | * assigned. (If one hasn't been assigned, no code we've |
2461 | | * generated uses that prefix, so we don't need to generate any |
2462 | | * code to load it.) |
2463 | | */ |
2464 | 0 | if (cstate->off_linkhdr.reg != -1) { |
2465 | | /* |
2466 | | * The 4 bytes at an offset of 4 from the beginning of |
2467 | | * the AVS header are the length of the AVS header. |
2468 | | * That field is big-endian. |
2469 | | */ |
2470 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2471 | 0 | s1->s.k = 4; |
2472 | | |
2473 | | /* |
2474 | | * Now allocate a register to hold that value and store |
2475 | | * it. |
2476 | | */ |
2477 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2478 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
2479 | 0 | sappend(s1, s2); |
2480 | | |
2481 | | /* |
2482 | | * Now move it into the X register. |
2483 | | */ |
2484 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2485 | 0 | sappend(s1, s2); |
2486 | |
|
2487 | 0 | return (s1); |
2488 | 0 | } else |
2489 | 0 | return (NULL); |
2490 | 0 | } |
2491 | | |
2492 | | static struct slist * |
2493 | | gen_load_radiotap_llprefixlen(compiler_state_t *cstate) |
2494 | 0 | { |
2495 | 0 | struct slist *s1, *s2; |
2496 | | |
2497 | | /* |
2498 | | * Generate code to load the length of the radiotap header into |
2499 | | * the register assigned to hold that length, if one has been |
2500 | | * assigned. (If one hasn't been assigned, no code we've |
2501 | | * generated uses that prefix, so we don't need to generate any |
2502 | | * code to load it.) |
2503 | | */ |
2504 | 0 | if (cstate->off_linkhdr.reg != -1) { |
2505 | | /* |
2506 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
2507 | | * of the radiotap header are the length of the radiotap |
2508 | | * header; unfortunately, it's little-endian, so we have |
2509 | | * to load it a byte at a time and construct the value. |
2510 | | */ |
2511 | | |
2512 | | /* |
2513 | | * Load the high-order byte, at an offset of 3, shift it |
2514 | | * left a byte, and put the result in the X register. |
2515 | | */ |
2516 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2517 | 0 | s1->s.k = 3; |
2518 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
2519 | 0 | sappend(s1, s2); |
2520 | 0 | s2->s.k = 8; |
2521 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2522 | 0 | sappend(s1, s2); |
2523 | | |
2524 | | /* |
2525 | | * Load the next byte, at an offset of 2, and OR the |
2526 | | * value from the X register into it. |
2527 | | */ |
2528 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2529 | 0 | sappend(s1, s2); |
2530 | 0 | s2->s.k = 2; |
2531 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
2532 | 0 | sappend(s1, s2); |
2533 | | |
2534 | | /* |
2535 | | * Now allocate a register to hold that value and store |
2536 | | * it. |
2537 | | */ |
2538 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2539 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
2540 | 0 | sappend(s1, s2); |
2541 | | |
2542 | | /* |
2543 | | * Now move it into the X register. |
2544 | | */ |
2545 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2546 | 0 | sappend(s1, s2); |
2547 | |
|
2548 | 0 | return (s1); |
2549 | 0 | } else |
2550 | 0 | return (NULL); |
2551 | 0 | } |
2552 | | |
2553 | | /* |
2554 | | * At the moment we treat PPI as normal Radiotap encoded |
2555 | | * packets. The difference is in the function that generates |
2556 | | * the code at the beginning to compute the header length. |
2557 | | * Since this code generator of PPI supports bare 802.11 |
2558 | | * encapsulation only (i.e. the encapsulated DLT should be |
2559 | | * DLT_IEEE802_11) we generate code to check for this too; |
2560 | | * that's done in finish_parse(). |
2561 | | */ |
2562 | | static struct slist * |
2563 | | gen_load_ppi_llprefixlen(compiler_state_t *cstate) |
2564 | 0 | { |
2565 | 0 | struct slist *s1, *s2; |
2566 | | |
2567 | | /* |
2568 | | * Generate code to load the length of the radiotap header |
2569 | | * into the register assigned to hold that length, if one has |
2570 | | * been assigned. |
2571 | | */ |
2572 | 0 | if (cstate->off_linkhdr.reg != -1) { |
2573 | | /* |
2574 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
2575 | | * of the radiotap header are the length of the radiotap |
2576 | | * header; unfortunately, it's little-endian, so we have |
2577 | | * to load it a byte at a time and construct the value. |
2578 | | */ |
2579 | | |
2580 | | /* |
2581 | | * Load the high-order byte, at an offset of 3, shift it |
2582 | | * left a byte, and put the result in the X register. |
2583 | | */ |
2584 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2585 | 0 | s1->s.k = 3; |
2586 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
2587 | 0 | sappend(s1, s2); |
2588 | 0 | s2->s.k = 8; |
2589 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2590 | 0 | sappend(s1, s2); |
2591 | | |
2592 | | /* |
2593 | | * Load the next byte, at an offset of 2, and OR the |
2594 | | * value from the X register into it. |
2595 | | */ |
2596 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2597 | 0 | sappend(s1, s2); |
2598 | 0 | s2->s.k = 2; |
2599 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
2600 | 0 | sappend(s1, s2); |
2601 | | |
2602 | | /* |
2603 | | * Now allocate a register to hold that value and store |
2604 | | * it. |
2605 | | */ |
2606 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2607 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
2608 | 0 | sappend(s1, s2); |
2609 | | |
2610 | | /* |
2611 | | * Now move it into the X register. |
2612 | | */ |
2613 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2614 | 0 | sappend(s1, s2); |
2615 | |
|
2616 | 0 | return (s1); |
2617 | 0 | } else |
2618 | 0 | return (NULL); |
2619 | 0 | } |
2620 | | |
2621 | | /* |
2622 | | * Load a value relative to the beginning of the link-layer header after the 802.11 |
2623 | | * header, i.e. LLC_SNAP. |
2624 | | * The link-layer header doesn't necessarily begin at the beginning |
2625 | | * of the packet data; there might be a variable-length prefix containing |
2626 | | * radio information. |
2627 | | */ |
2628 | | static struct slist * |
2629 | | gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext) |
2630 | 0 | { |
2631 | 0 | struct slist *s2; |
2632 | 0 | struct slist *sjset_data_frame_1; |
2633 | 0 | struct slist *sjset_data_frame_2; |
2634 | 0 | struct slist *sjset_qos; |
2635 | 0 | struct slist *sjset_radiotap_flags_present; |
2636 | 0 | struct slist *sjset_radiotap_ext_present; |
2637 | 0 | struct slist *sjset_radiotap_tsft_present; |
2638 | 0 | struct slist *sjset_tsft_datapad, *sjset_notsft_datapad; |
2639 | 0 | struct slist *s_roundup; |
2640 | |
|
2641 | 0 | if (cstate->off_linkpl.reg == -1) { |
2642 | | /* |
2643 | | * No register has been assigned to the offset of |
2644 | | * the link-layer payload, which means nobody needs |
2645 | | * it; don't bother computing it - just return |
2646 | | * what we already have. |
2647 | | */ |
2648 | 0 | return (s); |
2649 | 0 | } |
2650 | | |
2651 | | /* |
2652 | | * This code is not compatible with the optimizer, as |
2653 | | * we are generating jmp instructions within a normal |
2654 | | * slist of instructions |
2655 | | */ |
2656 | 0 | cstate->no_optimize = 1; |
2657 | | |
2658 | | /* |
2659 | | * If "s" is non-null, it has code to arrange that the X register |
2660 | | * contains the length of the prefix preceding the link-layer |
2661 | | * header. |
2662 | | * |
2663 | | * Otherwise, the length of the prefix preceding the link-layer |
2664 | | * header is "off_outermostlinkhdr.constant_part". |
2665 | | */ |
2666 | 0 | if (s == NULL) { |
2667 | | /* |
2668 | | * There is no variable-length header preceding the |
2669 | | * link-layer header. |
2670 | | * |
2671 | | * Load the length of the fixed-length prefix preceding |
2672 | | * the link-layer header (if any) into the X register, |
2673 | | * and store it in the cstate->off_linkpl.reg register. |
2674 | | * That length is off_outermostlinkhdr.constant_part. |
2675 | | */ |
2676 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_IMM); |
2677 | 0 | s->s.k = cstate->off_outermostlinkhdr.constant_part; |
2678 | 0 | } |
2679 | | |
2680 | | /* |
2681 | | * The X register contains the offset of the beginning of the |
2682 | | * link-layer header; add 24, which is the minimum length |
2683 | | * of the MAC header for a data frame, to that, and store it |
2684 | | * in cstate->off_linkpl.reg, and then load the Frame Control field, |
2685 | | * which is at the offset in the X register, with an indexed load. |
2686 | | */ |
2687 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
2688 | 0 | sappend(s, s2); |
2689 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
2690 | 0 | s2->s.k = 24; |
2691 | 0 | sappend(s, s2); |
2692 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2693 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2694 | 0 | sappend(s, s2); |
2695 | |
|
2696 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
2697 | 0 | s2->s.k = 0; |
2698 | 0 | sappend(s, s2); |
2699 | | |
2700 | | /* |
2701 | | * Check the Frame Control field to see if this is a data frame; |
2702 | | * a data frame has the 0x08 bit (b3) in that field set and the |
2703 | | * 0x04 bit (b2) clear. |
2704 | | */ |
2705 | 0 | sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET)); |
2706 | 0 | sjset_data_frame_1->s.k = 0x08; |
2707 | 0 | sappend(s, sjset_data_frame_1); |
2708 | | |
2709 | | /* |
2710 | | * If b3 is set, test b2, otherwise go to the first statement of |
2711 | | * the rest of the program. |
2712 | | */ |
2713 | 0 | sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET)); |
2714 | 0 | sjset_data_frame_2->s.k = 0x04; |
2715 | 0 | sappend(s, sjset_data_frame_2); |
2716 | 0 | sjset_data_frame_1->s.jf = snext; |
2717 | | |
2718 | | /* |
2719 | | * If b2 is not set, this is a data frame; test the QoS bit. |
2720 | | * Otherwise, go to the first statement of the rest of the |
2721 | | * program. |
2722 | | */ |
2723 | 0 | sjset_data_frame_2->s.jt = snext; |
2724 | 0 | sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET)); |
2725 | 0 | sjset_qos->s.k = 0x80; /* QoS bit */ |
2726 | 0 | sappend(s, sjset_qos); |
2727 | | |
2728 | | /* |
2729 | | * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS |
2730 | | * field. |
2731 | | * Otherwise, go to the first statement of the rest of the |
2732 | | * program. |
2733 | | */ |
2734 | 0 | sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
2735 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2736 | 0 | sappend(s, s2); |
2737 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
2738 | 0 | s2->s.k = 2; |
2739 | 0 | sappend(s, s2); |
2740 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2741 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2742 | 0 | sappend(s, s2); |
2743 | | |
2744 | | /* |
2745 | | * If we have a radiotap header, look at it to see whether |
2746 | | * there's Atheros padding between the MAC-layer header |
2747 | | * and the payload. |
2748 | | * |
2749 | | * Note: all of the fields in the radiotap header are |
2750 | | * little-endian, so we byte-swap all of the values |
2751 | | * we test against, as they will be loaded as big-endian |
2752 | | * values. |
2753 | | * |
2754 | | * XXX - in the general case, we would have to scan through |
2755 | | * *all* the presence bits, if there's more than one word of |
2756 | | * presence bits. That would require a loop, meaning that |
2757 | | * we wouldn't be able to run the filter in the kernel. |
2758 | | * |
2759 | | * We assume here that the Atheros adapters that insert the |
2760 | | * annoying padding don't have multiple antennae and therefore |
2761 | | * do not generate radiotap headers with multiple presence words. |
2762 | | */ |
2763 | 0 | if (cstate->linktype == DLT_IEEE802_11_RADIO) { |
2764 | | /* |
2765 | | * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set |
2766 | | * in the first presence flag word? |
2767 | | */ |
2768 | 0 | sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W); |
2769 | 0 | s2->s.k = 4; |
2770 | 0 | sappend(s, s2); |
2771 | |
|
2772 | 0 | sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET)); |
2773 | 0 | sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002); |
2774 | 0 | sappend(s, sjset_radiotap_flags_present); |
2775 | | |
2776 | | /* |
2777 | | * If not, skip all of this. |
2778 | | */ |
2779 | 0 | sjset_radiotap_flags_present->s.jf = snext; |
2780 | | |
2781 | | /* |
2782 | | * Otherwise, is the "extension" bit set in that word? |
2783 | | */ |
2784 | 0 | sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET)); |
2785 | 0 | sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000); |
2786 | 0 | sappend(s, sjset_radiotap_ext_present); |
2787 | 0 | sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present; |
2788 | | |
2789 | | /* |
2790 | | * If so, skip all of this. |
2791 | | */ |
2792 | 0 | sjset_radiotap_ext_present->s.jt = snext; |
2793 | | |
2794 | | /* |
2795 | | * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set? |
2796 | | */ |
2797 | 0 | sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET)); |
2798 | 0 | sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001); |
2799 | 0 | sappend(s, sjset_radiotap_tsft_present); |
2800 | 0 | sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present; |
2801 | | |
2802 | | /* |
2803 | | * If IEEE80211_RADIOTAP_TSFT is set, the flags field is |
2804 | | * at an offset of 16 from the beginning of the raw packet |
2805 | | * data (8 bytes for the radiotap header and 8 bytes for |
2806 | | * the TSFT field). |
2807 | | * |
2808 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
2809 | | * is set. |
2810 | | */ |
2811 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
2812 | 0 | s2->s.k = 16; |
2813 | 0 | sappend(s, s2); |
2814 | 0 | sjset_radiotap_tsft_present->s.jt = s2; |
2815 | |
|
2816 | 0 | sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); |
2817 | 0 | sjset_tsft_datapad->s.k = 0x20; |
2818 | 0 | sappend(s, sjset_tsft_datapad); |
2819 | | |
2820 | | /* |
2821 | | * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is |
2822 | | * at an offset of 8 from the beginning of the raw packet |
2823 | | * data (8 bytes for the radiotap header). |
2824 | | * |
2825 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
2826 | | * is set. |
2827 | | */ |
2828 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
2829 | 0 | s2->s.k = 8; |
2830 | 0 | sappend(s, s2); |
2831 | 0 | sjset_radiotap_tsft_present->s.jf = s2; |
2832 | |
|
2833 | 0 | sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); |
2834 | 0 | sjset_notsft_datapad->s.k = 0x20; |
2835 | 0 | sappend(s, sjset_notsft_datapad); |
2836 | | |
2837 | | /* |
2838 | | * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is |
2839 | | * set, round the length of the 802.11 header to |
2840 | | * a multiple of 4. Do that by adding 3 and then |
2841 | | * dividing by and multiplying by 4, which we do by |
2842 | | * ANDing with ~3. |
2843 | | */ |
2844 | 0 | s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM); |
2845 | 0 | s_roundup->s.k = cstate->off_linkpl.reg; |
2846 | 0 | sappend(s, s_roundup); |
2847 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
2848 | 0 | s2->s.k = 3; |
2849 | 0 | sappend(s, s2); |
2850 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM); |
2851 | 0 | s2->s.k = ~3; |
2852 | 0 | sappend(s, s2); |
2853 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2854 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2855 | 0 | sappend(s, s2); |
2856 | |
|
2857 | 0 | sjset_tsft_datapad->s.jt = s_roundup; |
2858 | 0 | sjset_tsft_datapad->s.jf = snext; |
2859 | 0 | sjset_notsft_datapad->s.jt = s_roundup; |
2860 | 0 | sjset_notsft_datapad->s.jf = snext; |
2861 | 0 | } else |
2862 | 0 | sjset_qos->s.jf = snext; |
2863 | |
|
2864 | 0 | return s; |
2865 | 0 | } |
2866 | | |
2867 | | static void |
2868 | | insert_compute_vloffsets(compiler_state_t *cstate, struct block *b) |
2869 | 0 | { |
2870 | 0 | struct slist *s; |
2871 | | |
2872 | | /* There is an implicit dependency between the link |
2873 | | * payload and link header since the payload computation |
2874 | | * includes the variable part of the header. Therefore, |
2875 | | * if nobody else has allocated a register for the link |
2876 | | * header and we need it, do it now. */ |
2877 | 0 | if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable && |
2878 | 0 | cstate->off_linkhdr.reg == -1) |
2879 | 0 | cstate->off_linkhdr.reg = alloc_reg(cstate); |
2880 | | |
2881 | | /* |
2882 | | * For link-layer types that have a variable-length header |
2883 | | * preceding the link-layer header, generate code to load |
2884 | | * the offset of the link-layer header into the register |
2885 | | * assigned to that offset, if any. |
2886 | | * |
2887 | | * XXX - this, and the next switch statement, won't handle |
2888 | | * encapsulation of 802.11 or 802.11+radio information in |
2889 | | * some other protocol stack. That's significantly more |
2890 | | * complicated. |
2891 | | */ |
2892 | 0 | switch (cstate->outermostlinktype) { |
2893 | | |
2894 | 0 | case DLT_PRISM_HEADER: |
2895 | 0 | s = gen_load_prism_llprefixlen(cstate); |
2896 | 0 | break; |
2897 | | |
2898 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
2899 | 0 | s = gen_load_avs_llprefixlen(cstate); |
2900 | 0 | break; |
2901 | | |
2902 | 0 | case DLT_IEEE802_11_RADIO: |
2903 | 0 | s = gen_load_radiotap_llprefixlen(cstate); |
2904 | 0 | break; |
2905 | | |
2906 | 0 | case DLT_PPI: |
2907 | 0 | s = gen_load_ppi_llprefixlen(cstate); |
2908 | 0 | break; |
2909 | | |
2910 | 0 | default: |
2911 | 0 | s = NULL; |
2912 | 0 | break; |
2913 | 0 | } |
2914 | | |
2915 | | /* |
2916 | | * For link-layer types that have a variable-length link-layer |
2917 | | * header, generate code to load the offset of the link-layer |
2918 | | * payload into the register assigned to that offset, if any. |
2919 | | */ |
2920 | 0 | switch (cstate->outermostlinktype) { |
2921 | | |
2922 | 0 | case DLT_IEEE802_11: |
2923 | 0 | case DLT_PRISM_HEADER: |
2924 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
2925 | 0 | case DLT_IEEE802_11_RADIO: |
2926 | 0 | case DLT_PPI: |
2927 | 0 | s = gen_load_802_11_header_len(cstate, s, b->stmts); |
2928 | 0 | break; |
2929 | 0 | } |
2930 | | |
2931 | | /* |
2932 | | * If there there is no initialization yet and we need variable |
2933 | | * length offsets for VLAN, initialize them to zero |
2934 | | */ |
2935 | 0 | if (s == NULL && cstate->is_vlan_vloffset) { |
2936 | 0 | struct slist *s2; |
2937 | |
|
2938 | 0 | if (cstate->off_linkpl.reg == -1) |
2939 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
2940 | 0 | if (cstate->off_linktype.reg == -1) |
2941 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
2942 | |
|
2943 | 0 | s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
2944 | 0 | s->s.k = 0; |
2945 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2946 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2947 | 0 | sappend(s, s2); |
2948 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2949 | 0 | s2->s.k = cstate->off_linktype.reg; |
2950 | 0 | sappend(s, s2); |
2951 | 0 | } |
2952 | | |
2953 | | /* |
2954 | | * If we have any offset-loading code, append all the |
2955 | | * existing statements in the block to those statements, |
2956 | | * and make the resulting list the list of statements |
2957 | | * for the block. |
2958 | | */ |
2959 | 0 | if (s != NULL) { |
2960 | 0 | sappend(s, b->stmts); |
2961 | 0 | b->stmts = s; |
2962 | 0 | } |
2963 | 0 | } |
2964 | | |
2965 | | static struct block * |
2966 | | gen_ppi_dlt_check(compiler_state_t *cstate) |
2967 | 0 | { |
2968 | 0 | struct slist *s_load_dlt; |
2969 | 0 | struct block *b; |
2970 | |
|
2971 | 0 | if (cstate->linktype == DLT_PPI) |
2972 | 0 | { |
2973 | | /* Create the statements that check for the DLT |
2974 | | */ |
2975 | 0 | s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2976 | 0 | s_load_dlt->s.k = 4; |
2977 | |
|
2978 | 0 | b = new_block(cstate, JMP(BPF_JEQ)); |
2979 | |
|
2980 | 0 | b->stmts = s_load_dlt; |
2981 | 0 | b->s.k = SWAPLONG(DLT_IEEE802_11); |
2982 | 0 | } |
2983 | 0 | else |
2984 | 0 | { |
2985 | 0 | b = NULL; |
2986 | 0 | } |
2987 | |
|
2988 | 0 | return b; |
2989 | 0 | } |
2990 | | |
2991 | | /* |
2992 | | * Take an absolute offset, and: |
2993 | | * |
2994 | | * if it has no variable part, return NULL; |
2995 | | * |
2996 | | * if it has a variable part, generate code to load the register |
2997 | | * containing that variable part into the X register, returning |
2998 | | * a pointer to that code - if no register for that offset has |
2999 | | * been allocated, allocate it first. |
3000 | | * |
3001 | | * (The code to set that register will be generated later, but will |
3002 | | * be placed earlier in the code sequence.) |
3003 | | */ |
3004 | | static struct slist * |
3005 | | gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off) |
3006 | 0 | { |
3007 | 0 | struct slist *s; |
3008 | |
|
3009 | 0 | if (off->is_variable) { |
3010 | 0 | if (off->reg == -1) { |
3011 | | /* |
3012 | | * We haven't yet assigned a register for the |
3013 | | * variable part of the offset of the link-layer |
3014 | | * header; allocate one. |
3015 | | */ |
3016 | 0 | off->reg = alloc_reg(cstate); |
3017 | 0 | } |
3018 | | |
3019 | | /* |
3020 | | * Load the register containing the variable part of the |
3021 | | * offset of the link-layer header into the X register. |
3022 | | */ |
3023 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_MEM); |
3024 | 0 | s->s.k = off->reg; |
3025 | 0 | return s; |
3026 | 0 | } else { |
3027 | | /* |
3028 | | * That offset isn't variable, there's no variable part, |
3029 | | * so we don't need to generate any code. |
3030 | | */ |
3031 | 0 | return NULL; |
3032 | 0 | } |
3033 | 0 | } |
3034 | | |
3035 | | /* |
3036 | | * Map an Ethernet type to the equivalent PPP type. |
3037 | | */ |
3038 | | static int |
3039 | | ethertype_to_ppptype(int proto) |
3040 | 0 | { |
3041 | 0 | switch (proto) { |
3042 | | |
3043 | 0 | case ETHERTYPE_IP: |
3044 | 0 | proto = PPP_IP; |
3045 | 0 | break; |
3046 | | |
3047 | 0 | case ETHERTYPE_IPV6: |
3048 | 0 | proto = PPP_IPV6; |
3049 | 0 | break; |
3050 | | |
3051 | 0 | case ETHERTYPE_DN: |
3052 | 0 | proto = PPP_DECNET; |
3053 | 0 | break; |
3054 | | |
3055 | 0 | case ETHERTYPE_ATALK: |
3056 | 0 | proto = PPP_APPLE; |
3057 | 0 | break; |
3058 | | |
3059 | 0 | case ETHERTYPE_NS: |
3060 | 0 | proto = PPP_NS; |
3061 | 0 | break; |
3062 | | |
3063 | 0 | case LLCSAP_ISONS: |
3064 | 0 | proto = PPP_OSI; |
3065 | 0 | break; |
3066 | | |
3067 | 0 | case LLCSAP_8021D: |
3068 | | /* |
3069 | | * I'm assuming the "Bridging PDU"s that go |
3070 | | * over PPP are Spanning Tree Protocol |
3071 | | * Bridging PDUs. |
3072 | | */ |
3073 | 0 | proto = PPP_BRPDU; |
3074 | 0 | break; |
3075 | | |
3076 | 0 | case LLCSAP_IPX: |
3077 | 0 | proto = PPP_IPX; |
3078 | 0 | break; |
3079 | 0 | } |
3080 | 0 | return (proto); |
3081 | 0 | } |
3082 | | |
3083 | | /* |
3084 | | * Generate any tests that, for encapsulation of a link-layer packet |
3085 | | * inside another protocol stack, need to be done to check for those |
3086 | | * link-layer packets (and that haven't already been done by a check |
3087 | | * for that encapsulation). |
3088 | | */ |
3089 | | static struct block * |
3090 | | gen_prevlinkhdr_check(compiler_state_t *cstate) |
3091 | 0 | { |
3092 | 0 | struct block *b0; |
3093 | |
|
3094 | 0 | if (cstate->is_geneve) |
3095 | 0 | return gen_geneve_ll_check(cstate); |
3096 | | |
3097 | 0 | switch (cstate->prevlinktype) { |
3098 | | |
3099 | 0 | case DLT_SUNATM: |
3100 | | /* |
3101 | | * This is LANE-encapsulated Ethernet; check that the LANE |
3102 | | * packet doesn't begin with an LE Control marker, i.e. |
3103 | | * that it's data, not a control message. |
3104 | | * |
3105 | | * (We've already generated a test for LANE.) |
3106 | | */ |
3107 | 0 | b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00); |
3108 | 0 | gen_not(b0); |
3109 | 0 | return b0; |
3110 | | |
3111 | 0 | default: |
3112 | | /* |
3113 | | * No such tests are necessary. |
3114 | | */ |
3115 | 0 | return NULL; |
3116 | 0 | } |
3117 | | /*NOTREACHED*/ |
3118 | 0 | } |
3119 | | |
3120 | | /* |
3121 | | * The three different values we should check for when checking for an |
3122 | | * IPv6 packet with DLT_NULL. |
3123 | | */ |
3124 | 0 | #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */ |
3125 | 0 | #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */ |
3126 | 0 | #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */ |
3127 | | |
3128 | | /* |
3129 | | * Generate code to match a particular packet type by matching the |
3130 | | * link-layer type field or fields in the 802.2 LLC header. |
3131 | | * |
3132 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
3133 | | * value, if <= ETHERMTU. |
3134 | | */ |
3135 | | static struct block * |
3136 | | gen_linktype(compiler_state_t *cstate, int proto) |
3137 | 0 | { |
3138 | 0 | struct block *b0, *b1, *b2; |
3139 | 0 | const char *description; |
3140 | | |
3141 | | /* are we checking MPLS-encapsulated packets? */ |
3142 | 0 | if (cstate->label_stack_depth > 0) { |
3143 | 0 | switch (proto) { |
3144 | 0 | case ETHERTYPE_IP: |
3145 | 0 | case PPP_IP: |
3146 | | /* FIXME add other L3 proto IDs */ |
3147 | 0 | return gen_mpls_linktype(cstate, Q_IP); |
3148 | | |
3149 | 0 | case ETHERTYPE_IPV6: |
3150 | 0 | case PPP_IPV6: |
3151 | | /* FIXME add other L3 proto IDs */ |
3152 | 0 | return gen_mpls_linktype(cstate, Q_IPV6); |
3153 | | |
3154 | 0 | default: |
3155 | 0 | bpf_error(cstate, "unsupported protocol over mpls"); |
3156 | | /*NOTREACHED*/ |
3157 | 0 | } |
3158 | 0 | } |
3159 | | |
3160 | 0 | switch (cstate->linktype) { |
3161 | | |
3162 | 0 | case DLT_EN10MB: |
3163 | 0 | case DLT_NETANALYZER: |
3164 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
3165 | | /* Geneve has an EtherType regardless of whether there is an |
3166 | | * L2 header. */ |
3167 | 0 | if (!cstate->is_geneve) |
3168 | 0 | b0 = gen_prevlinkhdr_check(cstate); |
3169 | 0 | else |
3170 | 0 | b0 = NULL; |
3171 | |
|
3172 | 0 | b1 = gen_ether_linktype(cstate, proto); |
3173 | 0 | if (b0 != NULL) |
3174 | 0 | gen_and(b0, b1); |
3175 | 0 | return b1; |
3176 | | /*NOTREACHED*/ |
3177 | | |
3178 | 0 | case DLT_C_HDLC: |
3179 | 0 | switch (proto) { |
3180 | | |
3181 | 0 | case LLCSAP_ISONS: |
3182 | 0 | proto = (proto << 8 | LLCSAP_ISONS); |
3183 | | /* fall through */ |
3184 | |
|
3185 | 0 | default: |
3186 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
3187 | | /*NOTREACHED*/ |
3188 | 0 | } |
3189 | | |
3190 | 0 | case DLT_IEEE802_11: |
3191 | 0 | case DLT_PRISM_HEADER: |
3192 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
3193 | 0 | case DLT_IEEE802_11_RADIO: |
3194 | 0 | case DLT_PPI: |
3195 | | /* |
3196 | | * Check that we have a data frame. |
3197 | | */ |
3198 | 0 | b0 = gen_check_802_11_data_frame(cstate); |
3199 | | |
3200 | | /* |
3201 | | * Now check for the specified link-layer type. |
3202 | | */ |
3203 | 0 | b1 = gen_llc_linktype(cstate, proto); |
3204 | 0 | gen_and(b0, b1); |
3205 | 0 | return b1; |
3206 | | /*NOTREACHED*/ |
3207 | | |
3208 | 0 | case DLT_FDDI: |
3209 | | /* |
3210 | | * XXX - check for LLC frames. |
3211 | | */ |
3212 | 0 | return gen_llc_linktype(cstate, proto); |
3213 | | /*NOTREACHED*/ |
3214 | | |
3215 | 0 | case DLT_IEEE802: |
3216 | | /* |
3217 | | * XXX - check for LLC PDUs, as per IEEE 802.5. |
3218 | | */ |
3219 | 0 | return gen_llc_linktype(cstate, proto); |
3220 | | /*NOTREACHED*/ |
3221 | | |
3222 | 0 | case DLT_ATM_RFC1483: |
3223 | 0 | case DLT_ATM_CLIP: |
3224 | 0 | case DLT_IP_OVER_FC: |
3225 | 0 | return gen_llc_linktype(cstate, proto); |
3226 | | /*NOTREACHED*/ |
3227 | | |
3228 | 0 | case DLT_SUNATM: |
3229 | | /* |
3230 | | * Check for an LLC-encapsulated version of this protocol; |
3231 | | * if we were checking for LANE, linktype would no longer |
3232 | | * be DLT_SUNATM. |
3233 | | * |
3234 | | * Check for LLC encapsulation and then check the protocol. |
3235 | | */ |
3236 | 0 | b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); |
3237 | 0 | b1 = gen_llc_linktype(cstate, proto); |
3238 | 0 | gen_and(b0, b1); |
3239 | 0 | return b1; |
3240 | | /*NOTREACHED*/ |
3241 | | |
3242 | 0 | case DLT_LINUX_SLL: |
3243 | 0 | return gen_linux_sll_linktype(cstate, proto); |
3244 | | /*NOTREACHED*/ |
3245 | | |
3246 | 0 | case DLT_SLIP: |
3247 | 0 | case DLT_SLIP_BSDOS: |
3248 | 0 | case DLT_RAW: |
3249 | | /* |
3250 | | * These types don't provide any type field; packets |
3251 | | * are always IPv4 or IPv6. |
3252 | | * |
3253 | | * XXX - for IPv4, check for a version number of 4, and, |
3254 | | * for IPv6, check for a version number of 6? |
3255 | | */ |
3256 | 0 | switch (proto) { |
3257 | | |
3258 | 0 | case ETHERTYPE_IP: |
3259 | | /* Check for a version number of 4. */ |
3260 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0); |
3261 | | |
3262 | 0 | case ETHERTYPE_IPV6: |
3263 | | /* Check for a version number of 6. */ |
3264 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0); |
3265 | | |
3266 | 0 | default: |
3267 | 0 | return gen_false(cstate); /* always false */ |
3268 | 0 | } |
3269 | | /*NOTREACHED*/ |
3270 | | |
3271 | 0 | case DLT_IPV4: |
3272 | | /* |
3273 | | * Raw IPv4, so no type field. |
3274 | | */ |
3275 | 0 | if (proto == ETHERTYPE_IP) |
3276 | 0 | return gen_true(cstate); /* always true */ |
3277 | | |
3278 | | /* Checking for something other than IPv4; always false */ |
3279 | 0 | return gen_false(cstate); |
3280 | | /*NOTREACHED*/ |
3281 | | |
3282 | 0 | case DLT_IPV6: |
3283 | | /* |
3284 | | * Raw IPv6, so no type field. |
3285 | | */ |
3286 | 0 | if (proto == ETHERTYPE_IPV6) |
3287 | 0 | return gen_true(cstate); /* always true */ |
3288 | | |
3289 | | /* Checking for something other than IPv6; always false */ |
3290 | 0 | return gen_false(cstate); |
3291 | | /*NOTREACHED*/ |
3292 | | |
3293 | 0 | case DLT_PPP: |
3294 | 0 | case DLT_PPP_PPPD: |
3295 | 0 | case DLT_PPP_SERIAL: |
3296 | 0 | case DLT_PPP_ETHER: |
3297 | | /* |
3298 | | * We use Ethernet protocol types inside libpcap; |
3299 | | * map them to the corresponding PPP protocol types. |
3300 | | */ |
3301 | 0 | proto = ethertype_to_ppptype(proto); |
3302 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
3303 | | /*NOTREACHED*/ |
3304 | | |
3305 | 0 | case DLT_PPP_BSDOS: |
3306 | | /* |
3307 | | * We use Ethernet protocol types inside libpcap; |
3308 | | * map them to the corresponding PPP protocol types. |
3309 | | */ |
3310 | 0 | switch (proto) { |
3311 | | |
3312 | 0 | case ETHERTYPE_IP: |
3313 | | /* |
3314 | | * Also check for Van Jacobson-compressed IP. |
3315 | | * XXX - do this for other forms of PPP? |
3316 | | */ |
3317 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP); |
3318 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC); |
3319 | 0 | gen_or(b0, b1); |
3320 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC); |
3321 | 0 | gen_or(b1, b0); |
3322 | 0 | return b0; |
3323 | | |
3324 | 0 | default: |
3325 | 0 | proto = ethertype_to_ppptype(proto); |
3326 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
3327 | 0 | (bpf_int32)proto); |
3328 | 0 | } |
3329 | | /*NOTREACHED*/ |
3330 | | |
3331 | 0 | case DLT_NULL: |
3332 | 0 | case DLT_LOOP: |
3333 | 0 | case DLT_ENC: |
3334 | 0 | switch (proto) { |
3335 | | |
3336 | 0 | case ETHERTYPE_IP: |
3337 | 0 | return (gen_loopback_linktype(cstate, AF_INET)); |
3338 | | |
3339 | 0 | case ETHERTYPE_IPV6: |
3340 | | /* |
3341 | | * AF_ values may, unfortunately, be platform- |
3342 | | * dependent; AF_INET isn't, because everybody |
3343 | | * used 4.2BSD's value, but AF_INET6 is, because |
3344 | | * 4.2BSD didn't have a value for it (given that |
3345 | | * IPv6 didn't exist back in the early 1980's), |
3346 | | * and they all picked their own values. |
3347 | | * |
3348 | | * This means that, if we're reading from a |
3349 | | * savefile, we need to check for all the |
3350 | | * possible values. |
3351 | | * |
3352 | | * If we're doing a live capture, we only need |
3353 | | * to check for this platform's value; however, |
3354 | | * Npcap uses 24, which isn't Windows's AF_INET6 |
3355 | | * value. (Given the multiple different values, |
3356 | | * programs that read pcap files shouldn't be |
3357 | | * checking for their platform's AF_INET6 value |
3358 | | * anyway, they should check for all of the |
3359 | | * possible values. and they might as well do |
3360 | | * that even for live captures.) |
3361 | | */ |
3362 | 0 | if (cstate->bpf_pcap->rfile != NULL) { |
3363 | | /* |
3364 | | * Savefile - check for all three |
3365 | | * possible IPv6 values. |
3366 | | */ |
3367 | 0 | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD); |
3368 | 0 | b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD); |
3369 | 0 | gen_or(b0, b1); |
3370 | 0 | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN); |
3371 | 0 | gen_or(b0, b1); |
3372 | 0 | return (b1); |
3373 | 0 | } else { |
3374 | | /* |
3375 | | * Live capture, so we only need to |
3376 | | * check for the value used on this |
3377 | | * platform. |
3378 | | */ |
3379 | | #ifdef _WIN32 |
3380 | | /* |
3381 | | * Npcap doesn't use Windows's AF_INET6, |
3382 | | * as that collides with AF_IPX on |
3383 | | * some BSDs (both have the value 23). |
3384 | | * Instead, it uses 24. |
3385 | | */ |
3386 | | return (gen_loopback_linktype(cstate, 24)); |
3387 | | #else /* _WIN32 */ |
3388 | 0 | #ifdef AF_INET6 |
3389 | 0 | return (gen_loopback_linktype(cstate, AF_INET6)); |
3390 | | #else /* AF_INET6 */ |
3391 | | /* |
3392 | | * I guess this platform doesn't support |
3393 | | * IPv6, so we just reject all packets. |
3394 | | */ |
3395 | | return gen_false(cstate); |
3396 | | #endif /* AF_INET6 */ |
3397 | 0 | #endif /* _WIN32 */ |
3398 | 0 | } |
3399 | | |
3400 | 0 | default: |
3401 | | /* |
3402 | | * Not a type on which we support filtering. |
3403 | | * XXX - support those that have AF_ values |
3404 | | * #defined on this platform, at least? |
3405 | | */ |
3406 | 0 | return gen_false(cstate); |
3407 | 0 | } |
3408 | | |
3409 | | #ifdef HAVE_NET_PFVAR_H |
3410 | | case DLT_PFLOG: |
3411 | | /* |
3412 | | * af field is host byte order in contrast to the rest of |
3413 | | * the packet. |
3414 | | */ |
3415 | | if (proto == ETHERTYPE_IP) |
3416 | | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3417 | | BPF_B, (bpf_int32)AF_INET)); |
3418 | | else if (proto == ETHERTYPE_IPV6) |
3419 | | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3420 | | BPF_B, (bpf_int32)AF_INET6)); |
3421 | | else |
3422 | | return gen_false(cstate); |
3423 | | /*NOTREACHED*/ |
3424 | | #endif /* HAVE_NET_PFVAR_H */ |
3425 | | |
3426 | 0 | case DLT_ARCNET: |
3427 | 0 | case DLT_ARCNET_LINUX: |
3428 | | /* |
3429 | | * XXX should we check for first fragment if the protocol |
3430 | | * uses PHDS? |
3431 | | */ |
3432 | 0 | switch (proto) { |
3433 | | |
3434 | 0 | default: |
3435 | 0 | return gen_false(cstate); |
3436 | | |
3437 | 0 | case ETHERTYPE_IPV6: |
3438 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3439 | 0 | (bpf_int32)ARCTYPE_INET6)); |
3440 | | |
3441 | 0 | case ETHERTYPE_IP: |
3442 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3443 | 0 | (bpf_int32)ARCTYPE_IP); |
3444 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3445 | 0 | (bpf_int32)ARCTYPE_IP_OLD); |
3446 | 0 | gen_or(b0, b1); |
3447 | 0 | return (b1); |
3448 | | |
3449 | 0 | case ETHERTYPE_ARP: |
3450 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3451 | 0 | (bpf_int32)ARCTYPE_ARP); |
3452 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3453 | 0 | (bpf_int32)ARCTYPE_ARP_OLD); |
3454 | 0 | gen_or(b0, b1); |
3455 | 0 | return (b1); |
3456 | | |
3457 | 0 | case ETHERTYPE_REVARP: |
3458 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3459 | 0 | (bpf_int32)ARCTYPE_REVARP)); |
3460 | | |
3461 | 0 | case ETHERTYPE_ATALK: |
3462 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3463 | 0 | (bpf_int32)ARCTYPE_ATALK)); |
3464 | 0 | } |
3465 | | /*NOTREACHED*/ |
3466 | | |
3467 | 0 | case DLT_LTALK: |
3468 | 0 | switch (proto) { |
3469 | 0 | case ETHERTYPE_ATALK: |
3470 | 0 | return gen_true(cstate); |
3471 | 0 | default: |
3472 | 0 | return gen_false(cstate); |
3473 | 0 | } |
3474 | | /*NOTREACHED*/ |
3475 | | |
3476 | 0 | case DLT_FRELAY: |
3477 | | /* |
3478 | | * XXX - assumes a 2-byte Frame Relay header with |
3479 | | * DLCI and flags. What if the address is longer? |
3480 | | */ |
3481 | 0 | switch (proto) { |
3482 | | |
3483 | 0 | case ETHERTYPE_IP: |
3484 | | /* |
3485 | | * Check for the special NLPID for IP. |
3486 | | */ |
3487 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc); |
3488 | | |
3489 | 0 | case ETHERTYPE_IPV6: |
3490 | | /* |
3491 | | * Check for the special NLPID for IPv6. |
3492 | | */ |
3493 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e); |
3494 | | |
3495 | 0 | case LLCSAP_ISONS: |
3496 | | /* |
3497 | | * Check for several OSI protocols. |
3498 | | * |
3499 | | * Frame Relay packets typically have an OSI |
3500 | | * NLPID at the beginning; we check for each |
3501 | | * of them. |
3502 | | * |
3503 | | * What we check for is the NLPID and a frame |
3504 | | * control field of UI, i.e. 0x03 followed |
3505 | | * by the NLPID. |
3506 | | */ |
3507 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP); |
3508 | 0 | b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS); |
3509 | 0 | b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS); |
3510 | 0 | gen_or(b1, b2); |
3511 | 0 | gen_or(b0, b2); |
3512 | 0 | return b2; |
3513 | | |
3514 | 0 | default: |
3515 | 0 | return gen_false(cstate); |
3516 | 0 | } |
3517 | | /*NOTREACHED*/ |
3518 | | |
3519 | 0 | case DLT_MFR: |
3520 | 0 | bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented"); |
3521 | | |
3522 | 0 | case DLT_JUNIPER_MFR: |
3523 | 0 | case DLT_JUNIPER_MLFR: |
3524 | 0 | case DLT_JUNIPER_MLPPP: |
3525 | 0 | case DLT_JUNIPER_ATM1: |
3526 | 0 | case DLT_JUNIPER_ATM2: |
3527 | 0 | case DLT_JUNIPER_PPPOE: |
3528 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
3529 | 0 | case DLT_JUNIPER_GGSN: |
3530 | 0 | case DLT_JUNIPER_ES: |
3531 | 0 | case DLT_JUNIPER_MONITOR: |
3532 | 0 | case DLT_JUNIPER_SERVICES: |
3533 | 0 | case DLT_JUNIPER_ETHER: |
3534 | 0 | case DLT_JUNIPER_PPP: |
3535 | 0 | case DLT_JUNIPER_FRELAY: |
3536 | 0 | case DLT_JUNIPER_CHDLC: |
3537 | 0 | case DLT_JUNIPER_VP: |
3538 | 0 | case DLT_JUNIPER_ST: |
3539 | 0 | case DLT_JUNIPER_ISM: |
3540 | 0 | case DLT_JUNIPER_VS: |
3541 | 0 | case DLT_JUNIPER_SRX_E2E: |
3542 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
3543 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
3544 | | |
3545 | | /* just lets verify the magic number for now - |
3546 | | * on ATM we may have up to 6 different encapsulations on the wire |
3547 | | * and need a lot of heuristics to figure out that the payload |
3548 | | * might be; |
3549 | | * |
3550 | | * FIXME encapsulation specific BPF_ filters |
3551 | | */ |
3552 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */ |
3553 | | |
3554 | 0 | case DLT_BACNET_MS_TP: |
3555 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000); |
3556 | | |
3557 | 0 | case DLT_IPNET: |
3558 | 0 | return gen_ipnet_linktype(cstate, proto); |
3559 | | |
3560 | 0 | case DLT_LINUX_IRDA: |
3561 | 0 | bpf_error(cstate, "IrDA link-layer type filtering not implemented"); |
3562 | | |
3563 | 0 | case DLT_DOCSIS: |
3564 | 0 | bpf_error(cstate, "DOCSIS link-layer type filtering not implemented"); |
3565 | | |
3566 | 0 | case DLT_MTP2: |
3567 | 0 | case DLT_MTP2_WITH_PHDR: |
3568 | 0 | bpf_error(cstate, "MTP2 link-layer type filtering not implemented"); |
3569 | | |
3570 | 0 | case DLT_ERF: |
3571 | 0 | bpf_error(cstate, "ERF link-layer type filtering not implemented"); |
3572 | | |
3573 | 0 | case DLT_PFSYNC: |
3574 | 0 | bpf_error(cstate, "PFSYNC link-layer type filtering not implemented"); |
3575 | | |
3576 | 0 | case DLT_LINUX_LAPD: |
3577 | 0 | bpf_error(cstate, "LAPD link-layer type filtering not implemented"); |
3578 | | |
3579 | 0 | case DLT_USB_FREEBSD: |
3580 | 0 | case DLT_USB_LINUX: |
3581 | 0 | case DLT_USB_LINUX_MMAPPED: |
3582 | 0 | case DLT_USBPCAP: |
3583 | 0 | bpf_error(cstate, "USB link-layer type filtering not implemented"); |
3584 | | |
3585 | 0 | case DLT_BLUETOOTH_HCI_H4: |
3586 | 0 | case DLT_BLUETOOTH_HCI_H4_WITH_PHDR: |
3587 | 0 | bpf_error(cstate, "Bluetooth link-layer type filtering not implemented"); |
3588 | | |
3589 | 0 | case DLT_CAN20B: |
3590 | 0 | case DLT_CAN_SOCKETCAN: |
3591 | 0 | bpf_error(cstate, "CAN link-layer type filtering not implemented"); |
3592 | | |
3593 | 0 | case DLT_IEEE802_15_4: |
3594 | 0 | case DLT_IEEE802_15_4_LINUX: |
3595 | 0 | case DLT_IEEE802_15_4_NONASK_PHY: |
3596 | 0 | case DLT_IEEE802_15_4_NOFCS: |
3597 | 0 | bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented"); |
3598 | | |
3599 | 0 | case DLT_IEEE802_16_MAC_CPS_RADIO: |
3600 | 0 | bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented"); |
3601 | | |
3602 | 0 | case DLT_SITA: |
3603 | 0 | bpf_error(cstate, "SITA link-layer type filtering not implemented"); |
3604 | | |
3605 | 0 | case DLT_RAIF1: |
3606 | 0 | bpf_error(cstate, "RAIF1 link-layer type filtering not implemented"); |
3607 | | |
3608 | 0 | case DLT_IPMB_KONTRON: |
3609 | 0 | case DLT_IPMB_LINUX: |
3610 | 0 | bpf_error(cstate, "IPMB link-layer type filtering not implemented"); |
3611 | | |
3612 | 0 | case DLT_AX25_KISS: |
3613 | 0 | bpf_error(cstate, "AX.25 link-layer type filtering not implemented"); |
3614 | | |
3615 | 0 | case DLT_NFLOG: |
3616 | | /* Using the fixed-size NFLOG header it is possible to tell only |
3617 | | * the address family of the packet, other meaningful data is |
3618 | | * either missing or behind TLVs. |
3619 | | */ |
3620 | 0 | bpf_error(cstate, "NFLOG link-layer type filtering not implemented"); |
3621 | | |
3622 | 0 | default: |
3623 | | /* |
3624 | | * Does this link-layer header type have a field |
3625 | | * indicating the type of the next protocol? If |
3626 | | * so, off_linktype.constant_part will be the offset of that |
3627 | | * field in the packet; if not, it will be OFFSET_NOT_SET. |
3628 | | */ |
3629 | 0 | if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) { |
3630 | | /* |
3631 | | * Yes; assume it's an Ethernet type. (If |
3632 | | * it's not, it needs to be handled specially |
3633 | | * above.) |
3634 | | */ |
3635 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto); |
3636 | | /*NOTREACHED */ |
3637 | 0 | } else { |
3638 | | /* |
3639 | | * No; report an error. |
3640 | | */ |
3641 | 0 | description = pcap_datalink_val_to_description_or_dlt(cstate->linktype); |
3642 | 0 | bpf_error(cstate, "%s link-layer type filtering not implemented", |
3643 | 0 | description); |
3644 | | /*NOTREACHED */ |
3645 | 0 | } |
3646 | 0 | } |
3647 | 0 | } |
3648 | | |
3649 | | /* |
3650 | | * Check for an LLC SNAP packet with a given organization code and |
3651 | | * protocol type; we check the entire contents of the 802.2 LLC and |
3652 | | * snap headers, checking for DSAP and SSAP of SNAP and a control |
3653 | | * field of 0x03 in the LLC header, and for the specified organization |
3654 | | * code and protocol type in the SNAP header. |
3655 | | */ |
3656 | | static struct block * |
3657 | | gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype) |
3658 | 0 | { |
3659 | 0 | u_char snapblock[8]; |
3660 | |
|
3661 | 0 | snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */ |
3662 | 0 | snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */ |
3663 | 0 | snapblock[2] = 0x03; /* control = UI */ |
3664 | 0 | snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */ |
3665 | 0 | snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */ |
3666 | 0 | snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */ |
3667 | 0 | snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */ |
3668 | 0 | snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */ |
3669 | 0 | return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock); |
3670 | 0 | } |
3671 | | |
3672 | | /* |
3673 | | * Generate code to match frames with an LLC header. |
3674 | | */ |
3675 | | static struct block * |
3676 | | gen_llc_internal(compiler_state_t *cstate) |
3677 | 0 | { |
3678 | 0 | struct block *b0, *b1; |
3679 | |
|
3680 | 0 | switch (cstate->linktype) { |
3681 | | |
3682 | 0 | case DLT_EN10MB: |
3683 | | /* |
3684 | | * We check for an Ethernet type field less than |
3685 | | * 1500, which means it's an 802.3 length field. |
3686 | | */ |
3687 | 0 | b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
3688 | 0 | gen_not(b0); |
3689 | | |
3690 | | /* |
3691 | | * Now check for the purported DSAP and SSAP not being |
3692 | | * 0xFF, to rule out NetWare-over-802.3. |
3693 | | */ |
3694 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF); |
3695 | 0 | gen_not(b1); |
3696 | 0 | gen_and(b0, b1); |
3697 | 0 | return b1; |
3698 | | |
3699 | 0 | case DLT_SUNATM: |
3700 | | /* |
3701 | | * We check for LLC traffic. |
3702 | | */ |
3703 | 0 | b0 = gen_atmtype_llc(cstate); |
3704 | 0 | return b0; |
3705 | | |
3706 | 0 | case DLT_IEEE802: /* Token Ring */ |
3707 | | /* |
3708 | | * XXX - check for LLC frames. |
3709 | | */ |
3710 | 0 | return gen_true(cstate); |
3711 | | |
3712 | 0 | case DLT_FDDI: |
3713 | | /* |
3714 | | * XXX - check for LLC frames. |
3715 | | */ |
3716 | 0 | return gen_true(cstate); |
3717 | | |
3718 | 0 | case DLT_ATM_RFC1483: |
3719 | | /* |
3720 | | * For LLC encapsulation, these are defined to have an |
3721 | | * 802.2 LLC header. |
3722 | | * |
3723 | | * For VC encapsulation, they don't, but there's no |
3724 | | * way to check for that; the protocol used on the VC |
3725 | | * is negotiated out of band. |
3726 | | */ |
3727 | 0 | return gen_true(cstate); |
3728 | | |
3729 | 0 | case DLT_IEEE802_11: |
3730 | 0 | case DLT_PRISM_HEADER: |
3731 | 0 | case DLT_IEEE802_11_RADIO: |
3732 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
3733 | 0 | case DLT_PPI: |
3734 | | /* |
3735 | | * Check that we have a data frame. |
3736 | | */ |
3737 | 0 | b0 = gen_check_802_11_data_frame(cstate); |
3738 | 0 | return b0; |
3739 | | |
3740 | 0 | default: |
3741 | 0 | bpf_error(cstate, "'llc' not supported for %s", |
3742 | 0 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
3743 | | /*NOTREACHED*/ |
3744 | 0 | } |
3745 | 0 | } |
3746 | | |
3747 | | struct block * |
3748 | | gen_llc(compiler_state_t *cstate) |
3749 | 0 | { |
3750 | | /* |
3751 | | * Catch errors reported by us and routines below us, and return NULL |
3752 | | * on an error. |
3753 | | */ |
3754 | 0 | if (setjmp(cstate->top_ctx)) |
3755 | 0 | return (NULL); |
3756 | | |
3757 | 0 | return gen_llc_internal(cstate); |
3758 | 0 | } |
3759 | | |
3760 | | struct block * |
3761 | | gen_llc_i(compiler_state_t *cstate) |
3762 | 0 | { |
3763 | 0 | struct block *b0, *b1; |
3764 | 0 | struct slist *s; |
3765 | | |
3766 | | /* |
3767 | | * Catch errors reported by us and routines below us, and return NULL |
3768 | | * on an error. |
3769 | | */ |
3770 | 0 | if (setjmp(cstate->top_ctx)) |
3771 | 0 | return (NULL); |
3772 | | |
3773 | | /* |
3774 | | * Check whether this is an LLC frame. |
3775 | | */ |
3776 | 0 | b0 = gen_llc_internal(cstate); |
3777 | | |
3778 | | /* |
3779 | | * Load the control byte and test the low-order bit; it must |
3780 | | * be clear for I frames. |
3781 | | */ |
3782 | 0 | s = gen_load_a(cstate, OR_LLC, 2, BPF_B); |
3783 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
3784 | 0 | b1->s.k = 0x01; |
3785 | 0 | b1->stmts = s; |
3786 | 0 | gen_not(b1); |
3787 | 0 | gen_and(b0, b1); |
3788 | 0 | return b1; |
3789 | 0 | } |
3790 | | |
3791 | | struct block * |
3792 | | gen_llc_s(compiler_state_t *cstate) |
3793 | 0 | { |
3794 | 0 | struct block *b0, *b1; |
3795 | | |
3796 | | /* |
3797 | | * Catch errors reported by us and routines below us, and return NULL |
3798 | | * on an error. |
3799 | | */ |
3800 | 0 | if (setjmp(cstate->top_ctx)) |
3801 | 0 | return (NULL); |
3802 | | |
3803 | | /* |
3804 | | * Check whether this is an LLC frame. |
3805 | | */ |
3806 | 0 | b0 = gen_llc_internal(cstate); |
3807 | | |
3808 | | /* |
3809 | | * Now compare the low-order 2 bit of the control byte against |
3810 | | * the appropriate value for S frames. |
3811 | | */ |
3812 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03); |
3813 | 0 | gen_and(b0, b1); |
3814 | 0 | return b1; |
3815 | 0 | } |
3816 | | |
3817 | | struct block * |
3818 | | gen_llc_u(compiler_state_t *cstate) |
3819 | 0 | { |
3820 | 0 | struct block *b0, *b1; |
3821 | | |
3822 | | /* |
3823 | | * Catch errors reported by us and routines below us, and return NULL |
3824 | | * on an error. |
3825 | | */ |
3826 | 0 | if (setjmp(cstate->top_ctx)) |
3827 | 0 | return (NULL); |
3828 | | |
3829 | | /* |
3830 | | * Check whether this is an LLC frame. |
3831 | | */ |
3832 | 0 | b0 = gen_llc_internal(cstate); |
3833 | | |
3834 | | /* |
3835 | | * Now compare the low-order 2 bit of the control byte against |
3836 | | * the appropriate value for U frames. |
3837 | | */ |
3838 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03); |
3839 | 0 | gen_and(b0, b1); |
3840 | 0 | return b1; |
3841 | 0 | } |
3842 | | |
3843 | | struct block * |
3844 | | gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
3845 | 0 | { |
3846 | 0 | struct block *b0, *b1; |
3847 | | |
3848 | | /* |
3849 | | * Catch errors reported by us and routines below us, and return NULL |
3850 | | * on an error. |
3851 | | */ |
3852 | 0 | if (setjmp(cstate->top_ctx)) |
3853 | 0 | return (NULL); |
3854 | | |
3855 | | /* |
3856 | | * Check whether this is an LLC frame. |
3857 | | */ |
3858 | 0 | b0 = gen_llc_internal(cstate); |
3859 | | |
3860 | | /* |
3861 | | * Now check for an S frame with the appropriate type. |
3862 | | */ |
3863 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK); |
3864 | 0 | gen_and(b0, b1); |
3865 | 0 | return b1; |
3866 | 0 | } |
3867 | | |
3868 | | struct block * |
3869 | | gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
3870 | 0 | { |
3871 | 0 | struct block *b0, *b1; |
3872 | | |
3873 | | /* |
3874 | | * Catch errors reported by us and routines below us, and return NULL |
3875 | | * on an error. |
3876 | | */ |
3877 | 0 | if (setjmp(cstate->top_ctx)) |
3878 | 0 | return (NULL); |
3879 | | |
3880 | | /* |
3881 | | * Check whether this is an LLC frame. |
3882 | | */ |
3883 | 0 | b0 = gen_llc_internal(cstate); |
3884 | | |
3885 | | /* |
3886 | | * Now check for a U frame with the appropriate type. |
3887 | | */ |
3888 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK); |
3889 | 0 | gen_and(b0, b1); |
3890 | 0 | return b1; |
3891 | 0 | } |
3892 | | |
3893 | | /* |
3894 | | * Generate code to match a particular packet type, for link-layer types |
3895 | | * using 802.2 LLC headers. |
3896 | | * |
3897 | | * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used |
3898 | | * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues. |
3899 | | * |
3900 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
3901 | | * value, if <= ETHERMTU. We use that to determine whether to |
3902 | | * match the DSAP or both DSAP and LSAP or to check the OUI and |
3903 | | * protocol ID in a SNAP header. |
3904 | | */ |
3905 | | static struct block * |
3906 | | gen_llc_linktype(compiler_state_t *cstate, int proto) |
3907 | 0 | { |
3908 | | /* |
3909 | | * XXX - handle token-ring variable-length header. |
3910 | | */ |
3911 | 0 | switch (proto) { |
3912 | | |
3913 | 0 | case LLCSAP_IP: |
3914 | 0 | case LLCSAP_ISONS: |
3915 | 0 | case LLCSAP_NETBEUI: |
3916 | | /* |
3917 | | * XXX - should we check both the DSAP and the |
3918 | | * SSAP, like this, or should we check just the |
3919 | | * DSAP, as we do for other SAP values? |
3920 | | */ |
3921 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32) |
3922 | 0 | ((proto << 8) | proto)); |
3923 | | |
3924 | 0 | case LLCSAP_IPX: |
3925 | | /* |
3926 | | * XXX - are there ever SNAP frames for IPX on |
3927 | | * non-Ethernet 802.x networks? |
3928 | | */ |
3929 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, |
3930 | 0 | (bpf_int32)LLCSAP_IPX); |
3931 | | |
3932 | 0 | case ETHERTYPE_ATALK: |
3933 | | /* |
3934 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
3935 | | * SNAP packets with an organization code of |
3936 | | * 0x080007 (Apple, for Appletalk) and a protocol |
3937 | | * type of ETHERTYPE_ATALK (Appletalk). |
3938 | | * |
3939 | | * XXX - check for an organization code of |
3940 | | * encapsulated Ethernet as well? |
3941 | | */ |
3942 | 0 | return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
3943 | | |
3944 | 0 | default: |
3945 | | /* |
3946 | | * XXX - we don't have to check for IPX 802.3 |
3947 | | * here, but should we check for the IPX Ethertype? |
3948 | | */ |
3949 | 0 | if (proto <= ETHERMTU) { |
3950 | | /* |
3951 | | * This is an LLC SAP value, so check |
3952 | | * the DSAP. |
3953 | | */ |
3954 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, (bpf_int32)proto); |
3955 | 0 | } else { |
3956 | | /* |
3957 | | * This is an Ethernet type; we assume that it's |
3958 | | * unlikely that it'll appear in the right place |
3959 | | * at random, and therefore check only the |
3960 | | * location that would hold the Ethernet type |
3961 | | * in a SNAP frame with an organization code of |
3962 | | * 0x000000 (encapsulated Ethernet). |
3963 | | * |
3964 | | * XXX - if we were to check for the SNAP DSAP and |
3965 | | * LSAP, as per XXX, and were also to check for an |
3966 | | * organization code of 0x000000 (encapsulated |
3967 | | * Ethernet), we'd do |
3968 | | * |
3969 | | * return gen_snap(cstate, 0x000000, proto); |
3970 | | * |
3971 | | * here; for now, we don't, as per the above. |
3972 | | * I don't know whether it's worth the extra CPU |
3973 | | * time to do the right check or not. |
3974 | | */ |
3975 | 0 | return gen_cmp(cstate, OR_LLC, 6, BPF_H, (bpf_int32)proto); |
3976 | 0 | } |
3977 | 0 | } |
3978 | 0 | } |
3979 | | |
3980 | | static struct block * |
3981 | | gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
3982 | | int dir, int proto, u_int src_off, u_int dst_off) |
3983 | 0 | { |
3984 | 0 | struct block *b0, *b1; |
3985 | 0 | u_int offset; |
3986 | |
|
3987 | 0 | switch (dir) { |
3988 | | |
3989 | 0 | case Q_SRC: |
3990 | 0 | offset = src_off; |
3991 | 0 | break; |
3992 | | |
3993 | 0 | case Q_DST: |
3994 | 0 | offset = dst_off; |
3995 | 0 | break; |
3996 | | |
3997 | 0 | case Q_AND: |
3998 | 0 | b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off); |
3999 | 0 | b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off); |
4000 | 0 | gen_and(b0, b1); |
4001 | 0 | return b1; |
4002 | | |
4003 | 0 | case Q_DEFAULT: |
4004 | 0 | case Q_OR: |
4005 | 0 | b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off); |
4006 | 0 | b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off); |
4007 | 0 | gen_or(b0, b1); |
4008 | 0 | return b1; |
4009 | | |
4010 | 0 | case Q_ADDR1: |
4011 | 0 | bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4012 | | /*NOTREACHED*/ |
4013 | | |
4014 | 0 | case Q_ADDR2: |
4015 | 0 | bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4016 | | /*NOTREACHED*/ |
4017 | | |
4018 | 0 | case Q_ADDR3: |
4019 | 0 | bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4020 | | /*NOTREACHED*/ |
4021 | | |
4022 | 0 | case Q_ADDR4: |
4023 | 0 | bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4024 | | /*NOTREACHED*/ |
4025 | | |
4026 | 0 | case Q_RA: |
4027 | 0 | bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4028 | | /*NOTREACHED*/ |
4029 | | |
4030 | 0 | case Q_TA: |
4031 | 0 | bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4032 | | /*NOTREACHED*/ |
4033 | | |
4034 | 0 | default: |
4035 | 0 | abort(); |
4036 | | /*NOTREACHED*/ |
4037 | 0 | } |
4038 | 0 | b0 = gen_linktype(cstate, proto); |
4039 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, (bpf_int32)addr, mask); |
4040 | 0 | gen_and(b0, b1); |
4041 | 0 | return b1; |
4042 | 0 | } |
4043 | | |
4044 | | #ifdef INET6 |
4045 | | static struct block * |
4046 | | gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr, |
4047 | | struct in6_addr *mask, int dir, int proto, u_int src_off, u_int dst_off) |
4048 | 0 | { |
4049 | 0 | struct block *b0, *b1; |
4050 | 0 | u_int offset; |
4051 | 0 | uint32_t *a, *m; |
4052 | |
|
4053 | 0 | switch (dir) { |
4054 | | |
4055 | 0 | case Q_SRC: |
4056 | 0 | offset = src_off; |
4057 | 0 | break; |
4058 | | |
4059 | 0 | case Q_DST: |
4060 | 0 | offset = dst_off; |
4061 | 0 | break; |
4062 | | |
4063 | 0 | case Q_AND: |
4064 | 0 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off); |
4065 | 0 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off); |
4066 | 0 | gen_and(b0, b1); |
4067 | 0 | return b1; |
4068 | | |
4069 | 0 | case Q_DEFAULT: |
4070 | 0 | case Q_OR: |
4071 | 0 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off); |
4072 | 0 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off); |
4073 | 0 | gen_or(b0, b1); |
4074 | 0 | return b1; |
4075 | | |
4076 | 0 | case Q_ADDR1: |
4077 | 0 | bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4078 | | /*NOTREACHED*/ |
4079 | | |
4080 | 0 | case Q_ADDR2: |
4081 | 0 | bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4082 | | /*NOTREACHED*/ |
4083 | | |
4084 | 0 | case Q_ADDR3: |
4085 | 0 | bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4086 | | /*NOTREACHED*/ |
4087 | | |
4088 | 0 | case Q_ADDR4: |
4089 | 0 | bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4090 | | /*NOTREACHED*/ |
4091 | | |
4092 | 0 | case Q_RA: |
4093 | 0 | bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4094 | | /*NOTREACHED*/ |
4095 | | |
4096 | 0 | case Q_TA: |
4097 | 0 | bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4098 | | /*NOTREACHED*/ |
4099 | | |
4100 | 0 | default: |
4101 | 0 | abort(); |
4102 | | /*NOTREACHED*/ |
4103 | 0 | } |
4104 | | /* this order is important */ |
4105 | 0 | a = (uint32_t *)addr; |
4106 | 0 | m = (uint32_t *)mask; |
4107 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3])); |
4108 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2])); |
4109 | 0 | gen_and(b0, b1); |
4110 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1])); |
4111 | 0 | gen_and(b0, b1); |
4112 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0])); |
4113 | 0 | gen_and(b0, b1); |
4114 | 0 | b0 = gen_linktype(cstate, proto); |
4115 | 0 | gen_and(b0, b1); |
4116 | 0 | return b1; |
4117 | 0 | } |
4118 | | #endif |
4119 | | |
4120 | | static struct block * |
4121 | | gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4122 | 0 | { |
4123 | 0 | register struct block *b0, *b1; |
4124 | |
|
4125 | 0 | switch (dir) { |
4126 | 0 | case Q_SRC: |
4127 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr); |
4128 | | |
4129 | 0 | case Q_DST: |
4130 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr); |
4131 | | |
4132 | 0 | case Q_AND: |
4133 | 0 | b0 = gen_ehostop(cstate, eaddr, Q_SRC); |
4134 | 0 | b1 = gen_ehostop(cstate, eaddr, Q_DST); |
4135 | 0 | gen_and(b0, b1); |
4136 | 0 | return b1; |
4137 | | |
4138 | 0 | case Q_DEFAULT: |
4139 | 0 | case Q_OR: |
4140 | 0 | b0 = gen_ehostop(cstate, eaddr, Q_SRC); |
4141 | 0 | b1 = gen_ehostop(cstate, eaddr, Q_DST); |
4142 | 0 | gen_or(b0, b1); |
4143 | 0 | return b1; |
4144 | | |
4145 | 0 | case Q_ADDR1: |
4146 | 0 | bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers"); |
4147 | | /*NOTREACHED*/ |
4148 | | |
4149 | 0 | case Q_ADDR2: |
4150 | 0 | bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers"); |
4151 | | /*NOTREACHED*/ |
4152 | | |
4153 | 0 | case Q_ADDR3: |
4154 | 0 | bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers"); |
4155 | | /*NOTREACHED*/ |
4156 | | |
4157 | 0 | case Q_ADDR4: |
4158 | 0 | bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers"); |
4159 | | /*NOTREACHED*/ |
4160 | | |
4161 | 0 | case Q_RA: |
4162 | 0 | bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers"); |
4163 | | /*NOTREACHED*/ |
4164 | | |
4165 | 0 | case Q_TA: |
4166 | 0 | bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers"); |
4167 | | /*NOTREACHED*/ |
4168 | 0 | } |
4169 | 0 | abort(); |
4170 | | /*NOTREACHED*/ |
4171 | 0 | } |
4172 | | |
4173 | | /* |
4174 | | * Like gen_ehostop, but for DLT_FDDI |
4175 | | */ |
4176 | | static struct block * |
4177 | | gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4178 | 0 | { |
4179 | 0 | struct block *b0, *b1; |
4180 | |
|
4181 | 0 | switch (dir) { |
4182 | 0 | case Q_SRC: |
4183 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr); |
4184 | | |
4185 | 0 | case Q_DST: |
4186 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr); |
4187 | | |
4188 | 0 | case Q_AND: |
4189 | 0 | b0 = gen_fhostop(cstate, eaddr, Q_SRC); |
4190 | 0 | b1 = gen_fhostop(cstate, eaddr, Q_DST); |
4191 | 0 | gen_and(b0, b1); |
4192 | 0 | return b1; |
4193 | | |
4194 | 0 | case Q_DEFAULT: |
4195 | 0 | case Q_OR: |
4196 | 0 | b0 = gen_fhostop(cstate, eaddr, Q_SRC); |
4197 | 0 | b1 = gen_fhostop(cstate, eaddr, Q_DST); |
4198 | 0 | gen_or(b0, b1); |
4199 | 0 | return b1; |
4200 | | |
4201 | 0 | case Q_ADDR1: |
4202 | 0 | bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); |
4203 | | /*NOTREACHED*/ |
4204 | | |
4205 | 0 | case Q_ADDR2: |
4206 | 0 | bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); |
4207 | | /*NOTREACHED*/ |
4208 | | |
4209 | 0 | case Q_ADDR3: |
4210 | 0 | bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); |
4211 | | /*NOTREACHED*/ |
4212 | | |
4213 | 0 | case Q_ADDR4: |
4214 | 0 | bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); |
4215 | | /*NOTREACHED*/ |
4216 | | |
4217 | 0 | case Q_RA: |
4218 | 0 | bpf_error(cstate, "'ra' is only supported on 802.11"); |
4219 | | /*NOTREACHED*/ |
4220 | | |
4221 | 0 | case Q_TA: |
4222 | 0 | bpf_error(cstate, "'ta' is only supported on 802.11"); |
4223 | | /*NOTREACHED*/ |
4224 | 0 | } |
4225 | 0 | abort(); |
4226 | | /*NOTREACHED*/ |
4227 | 0 | } |
4228 | | |
4229 | | /* |
4230 | | * Like gen_ehostop, but for DLT_IEEE802 (Token Ring) |
4231 | | */ |
4232 | | static struct block * |
4233 | | gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4234 | 0 | { |
4235 | 0 | register struct block *b0, *b1; |
4236 | |
|
4237 | 0 | switch (dir) { |
4238 | 0 | case Q_SRC: |
4239 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr); |
4240 | | |
4241 | 0 | case Q_DST: |
4242 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr); |
4243 | | |
4244 | 0 | case Q_AND: |
4245 | 0 | b0 = gen_thostop(cstate, eaddr, Q_SRC); |
4246 | 0 | b1 = gen_thostop(cstate, eaddr, Q_DST); |
4247 | 0 | gen_and(b0, b1); |
4248 | 0 | return b1; |
4249 | | |
4250 | 0 | case Q_DEFAULT: |
4251 | 0 | case Q_OR: |
4252 | 0 | b0 = gen_thostop(cstate, eaddr, Q_SRC); |
4253 | 0 | b1 = gen_thostop(cstate, eaddr, Q_DST); |
4254 | 0 | gen_or(b0, b1); |
4255 | 0 | return b1; |
4256 | | |
4257 | 0 | case Q_ADDR1: |
4258 | 0 | bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); |
4259 | | /*NOTREACHED*/ |
4260 | | |
4261 | 0 | case Q_ADDR2: |
4262 | 0 | bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); |
4263 | | /*NOTREACHED*/ |
4264 | | |
4265 | 0 | case Q_ADDR3: |
4266 | 0 | bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); |
4267 | | /*NOTREACHED*/ |
4268 | | |
4269 | 0 | case Q_ADDR4: |
4270 | 0 | bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); |
4271 | | /*NOTREACHED*/ |
4272 | | |
4273 | 0 | case Q_RA: |
4274 | 0 | bpf_error(cstate, "'ra' is only supported on 802.11"); |
4275 | | /*NOTREACHED*/ |
4276 | | |
4277 | 0 | case Q_TA: |
4278 | 0 | bpf_error(cstate, "'ta' is only supported on 802.11"); |
4279 | | /*NOTREACHED*/ |
4280 | 0 | } |
4281 | 0 | abort(); |
4282 | | /*NOTREACHED*/ |
4283 | 0 | } |
4284 | | |
4285 | | /* |
4286 | | * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and |
4287 | | * various 802.11 + radio headers. |
4288 | | */ |
4289 | | static struct block * |
4290 | | gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4291 | 0 | { |
4292 | 0 | register struct block *b0, *b1, *b2; |
4293 | 0 | register struct slist *s; |
4294 | |
|
4295 | | #ifdef ENABLE_WLAN_FILTERING_PATCH |
4296 | | /* |
4297 | | * TODO GV 20070613 |
4298 | | * We need to disable the optimizer because the optimizer is buggy |
4299 | | * and wipes out some LD instructions generated by the below |
4300 | | * code to validate the Frame Control bits |
4301 | | */ |
4302 | | cstate->no_optimize = 1; |
4303 | | #endif /* ENABLE_WLAN_FILTERING_PATCH */ |
4304 | |
|
4305 | 0 | switch (dir) { |
4306 | 0 | case Q_SRC: |
4307 | | /* |
4308 | | * Oh, yuk. |
4309 | | * |
4310 | | * For control frames, there is no SA. |
4311 | | * |
4312 | | * For management frames, SA is at an |
4313 | | * offset of 10 from the beginning of |
4314 | | * the packet. |
4315 | | * |
4316 | | * For data frames, SA is at an offset |
4317 | | * of 10 from the beginning of the packet |
4318 | | * if From DS is clear, at an offset of |
4319 | | * 16 from the beginning of the packet |
4320 | | * if From DS is set and To DS is clear, |
4321 | | * and an offset of 24 from the beginning |
4322 | | * of the packet if From DS is set and To DS |
4323 | | * is set. |
4324 | | */ |
4325 | | |
4326 | | /* |
4327 | | * Generate the tests to be done for data frames |
4328 | | * with From DS set. |
4329 | | * |
4330 | | * First, check for To DS set, i.e. check "link[1] & 0x01". |
4331 | | */ |
4332 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4333 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4334 | 0 | b1->s.k = 0x01; /* To DS */ |
4335 | 0 | b1->stmts = s; |
4336 | | |
4337 | | /* |
4338 | | * If To DS is set, the SA is at 24. |
4339 | | */ |
4340 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4341 | 0 | gen_and(b1, b0); |
4342 | | |
4343 | | /* |
4344 | | * Now, check for To DS not set, i.e. check |
4345 | | * "!(link[1] & 0x01)". |
4346 | | */ |
4347 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4348 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
4349 | 0 | b2->s.k = 0x01; /* To DS */ |
4350 | 0 | b2->stmts = s; |
4351 | 0 | gen_not(b2); |
4352 | | |
4353 | | /* |
4354 | | * If To DS is not set, the SA is at 16. |
4355 | | */ |
4356 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4357 | 0 | gen_and(b2, b1); |
4358 | | |
4359 | | /* |
4360 | | * Now OR together the last two checks. That gives |
4361 | | * the complete set of checks for data frames with |
4362 | | * From DS set. |
4363 | | */ |
4364 | 0 | gen_or(b1, b0); |
4365 | | |
4366 | | /* |
4367 | | * Now check for From DS being set, and AND that with |
4368 | | * the ORed-together checks. |
4369 | | */ |
4370 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4371 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4372 | 0 | b1->s.k = 0x02; /* From DS */ |
4373 | 0 | b1->stmts = s; |
4374 | 0 | gen_and(b1, b0); |
4375 | | |
4376 | | /* |
4377 | | * Now check for data frames with From DS not set. |
4378 | | */ |
4379 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4380 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
4381 | 0 | b2->s.k = 0x02; /* From DS */ |
4382 | 0 | b2->stmts = s; |
4383 | 0 | gen_not(b2); |
4384 | | |
4385 | | /* |
4386 | | * If From DS isn't set, the SA is at 10. |
4387 | | */ |
4388 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4389 | 0 | gen_and(b2, b1); |
4390 | | |
4391 | | /* |
4392 | | * Now OR together the checks for data frames with |
4393 | | * From DS not set and for data frames with From DS |
4394 | | * set; that gives the checks done for data frames. |
4395 | | */ |
4396 | 0 | gen_or(b1, b0); |
4397 | | |
4398 | | /* |
4399 | | * Now check for a data frame. |
4400 | | * I.e, check "link[0] & 0x08". |
4401 | | */ |
4402 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4403 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4404 | 0 | b1->s.k = 0x08; |
4405 | 0 | b1->stmts = s; |
4406 | | |
4407 | | /* |
4408 | | * AND that with the checks done for data frames. |
4409 | | */ |
4410 | 0 | gen_and(b1, b0); |
4411 | | |
4412 | | /* |
4413 | | * If the high-order bit of the type value is 0, this |
4414 | | * is a management frame. |
4415 | | * I.e, check "!(link[0] & 0x08)". |
4416 | | */ |
4417 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4418 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
4419 | 0 | b2->s.k = 0x08; |
4420 | 0 | b2->stmts = s; |
4421 | 0 | gen_not(b2); |
4422 | | |
4423 | | /* |
4424 | | * For management frames, the SA is at 10. |
4425 | | */ |
4426 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4427 | 0 | gen_and(b2, b1); |
4428 | | |
4429 | | /* |
4430 | | * OR that with the checks done for data frames. |
4431 | | * That gives the checks done for management and |
4432 | | * data frames. |
4433 | | */ |
4434 | 0 | gen_or(b1, b0); |
4435 | | |
4436 | | /* |
4437 | | * If the low-order bit of the type value is 1, |
4438 | | * this is either a control frame or a frame |
4439 | | * with a reserved type, and thus not a |
4440 | | * frame with an SA. |
4441 | | * |
4442 | | * I.e., check "!(link[0] & 0x04)". |
4443 | | */ |
4444 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4445 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4446 | 0 | b1->s.k = 0x04; |
4447 | 0 | b1->stmts = s; |
4448 | 0 | gen_not(b1); |
4449 | | |
4450 | | /* |
4451 | | * AND that with the checks for data and management |
4452 | | * frames. |
4453 | | */ |
4454 | 0 | gen_and(b1, b0); |
4455 | 0 | return b0; |
4456 | | |
4457 | 0 | case Q_DST: |
4458 | | /* |
4459 | | * Oh, yuk. |
4460 | | * |
4461 | | * For control frames, there is no DA. |
4462 | | * |
4463 | | * For management frames, DA is at an |
4464 | | * offset of 4 from the beginning of |
4465 | | * the packet. |
4466 | | * |
4467 | | * For data frames, DA is at an offset |
4468 | | * of 4 from the beginning of the packet |
4469 | | * if To DS is clear and at an offset of |
4470 | | * 16 from the beginning of the packet |
4471 | | * if To DS is set. |
4472 | | */ |
4473 | | |
4474 | | /* |
4475 | | * Generate the tests to be done for data frames. |
4476 | | * |
4477 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
4478 | | */ |
4479 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4480 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4481 | 0 | b1->s.k = 0x01; /* To DS */ |
4482 | 0 | b1->stmts = s; |
4483 | | |
4484 | | /* |
4485 | | * If To DS is set, the DA is at 16. |
4486 | | */ |
4487 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4488 | 0 | gen_and(b1, b0); |
4489 | | |
4490 | | /* |
4491 | | * Now, check for To DS not set, i.e. check |
4492 | | * "!(link[1] & 0x01)". |
4493 | | */ |
4494 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4495 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
4496 | 0 | b2->s.k = 0x01; /* To DS */ |
4497 | 0 | b2->stmts = s; |
4498 | 0 | gen_not(b2); |
4499 | | |
4500 | | /* |
4501 | | * If To DS is not set, the DA is at 4. |
4502 | | */ |
4503 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4504 | 0 | gen_and(b2, b1); |
4505 | | |
4506 | | /* |
4507 | | * Now OR together the last two checks. That gives |
4508 | | * the complete set of checks for data frames. |
4509 | | */ |
4510 | 0 | gen_or(b1, b0); |
4511 | | |
4512 | | /* |
4513 | | * Now check for a data frame. |
4514 | | * I.e, check "link[0] & 0x08". |
4515 | | */ |
4516 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4517 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4518 | 0 | b1->s.k = 0x08; |
4519 | 0 | b1->stmts = s; |
4520 | | |
4521 | | /* |
4522 | | * AND that with the checks done for data frames. |
4523 | | */ |
4524 | 0 | gen_and(b1, b0); |
4525 | | |
4526 | | /* |
4527 | | * If the high-order bit of the type value is 0, this |
4528 | | * is a management frame. |
4529 | | * I.e, check "!(link[0] & 0x08)". |
4530 | | */ |
4531 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4532 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
4533 | 0 | b2->s.k = 0x08; |
4534 | 0 | b2->stmts = s; |
4535 | 0 | gen_not(b2); |
4536 | | |
4537 | | /* |
4538 | | * For management frames, the DA is at 4. |
4539 | | */ |
4540 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4541 | 0 | gen_and(b2, b1); |
4542 | | |
4543 | | /* |
4544 | | * OR that with the checks done for data frames. |
4545 | | * That gives the checks done for management and |
4546 | | * data frames. |
4547 | | */ |
4548 | 0 | gen_or(b1, b0); |
4549 | | |
4550 | | /* |
4551 | | * If the low-order bit of the type value is 1, |
4552 | | * this is either a control frame or a frame |
4553 | | * with a reserved type, and thus not a |
4554 | | * frame with an SA. |
4555 | | * |
4556 | | * I.e., check "!(link[0] & 0x04)". |
4557 | | */ |
4558 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4559 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4560 | 0 | b1->s.k = 0x04; |
4561 | 0 | b1->stmts = s; |
4562 | 0 | gen_not(b1); |
4563 | | |
4564 | | /* |
4565 | | * AND that with the checks for data and management |
4566 | | * frames. |
4567 | | */ |
4568 | 0 | gen_and(b1, b0); |
4569 | 0 | return b0; |
4570 | | |
4571 | 0 | case Q_AND: |
4572 | 0 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4573 | 0 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4574 | 0 | gen_and(b0, b1); |
4575 | 0 | return b1; |
4576 | | |
4577 | 0 | case Q_DEFAULT: |
4578 | 0 | case Q_OR: |
4579 | 0 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4580 | 0 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4581 | 0 | gen_or(b0, b1); |
4582 | 0 | return b1; |
4583 | | |
4584 | | /* |
4585 | | * XXX - add BSSID keyword? |
4586 | | */ |
4587 | 0 | case Q_ADDR1: |
4588 | 0 | return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr)); |
4589 | | |
4590 | 0 | case Q_ADDR2: |
4591 | | /* |
4592 | | * Not present in CTS or ACK control frames. |
4593 | | */ |
4594 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4595 | 0 | IEEE80211_FC0_TYPE_MASK); |
4596 | 0 | gen_not(b0); |
4597 | 0 | b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4598 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4599 | 0 | gen_not(b1); |
4600 | 0 | b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4601 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4602 | 0 | gen_not(b2); |
4603 | 0 | gen_and(b1, b2); |
4604 | 0 | gen_or(b0, b2); |
4605 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4606 | 0 | gen_and(b2, b1); |
4607 | 0 | return b1; |
4608 | | |
4609 | 0 | case Q_ADDR3: |
4610 | | /* |
4611 | | * Not present in control frames. |
4612 | | */ |
4613 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4614 | 0 | IEEE80211_FC0_TYPE_MASK); |
4615 | 0 | gen_not(b0); |
4616 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4617 | 0 | gen_and(b0, b1); |
4618 | 0 | return b1; |
4619 | | |
4620 | 0 | case Q_ADDR4: |
4621 | | /* |
4622 | | * Present only if the direction mask has both "From DS" |
4623 | | * and "To DS" set. Neither control frames nor management |
4624 | | * frames should have both of those set, so we don't |
4625 | | * check the frame type. |
4626 | | */ |
4627 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, |
4628 | 0 | IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK); |
4629 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4630 | 0 | gen_and(b0, b1); |
4631 | 0 | return b1; |
4632 | | |
4633 | 0 | case Q_RA: |
4634 | | /* |
4635 | | * Not present in management frames; addr1 in other |
4636 | | * frames. |
4637 | | */ |
4638 | | |
4639 | | /* |
4640 | | * If the high-order bit of the type value is 0, this |
4641 | | * is a management frame. |
4642 | | * I.e, check "(link[0] & 0x08)". |
4643 | | */ |
4644 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4645 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4646 | 0 | b1->s.k = 0x08; |
4647 | 0 | b1->stmts = s; |
4648 | | |
4649 | | /* |
4650 | | * Check addr1. |
4651 | | */ |
4652 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4653 | | |
4654 | | /* |
4655 | | * AND that with the check of addr1. |
4656 | | */ |
4657 | 0 | gen_and(b1, b0); |
4658 | 0 | return (b0); |
4659 | | |
4660 | 0 | case Q_TA: |
4661 | | /* |
4662 | | * Not present in management frames; addr2, if present, |
4663 | | * in other frames. |
4664 | | */ |
4665 | | |
4666 | | /* |
4667 | | * Not present in CTS or ACK control frames. |
4668 | | */ |
4669 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4670 | 0 | IEEE80211_FC0_TYPE_MASK); |
4671 | 0 | gen_not(b0); |
4672 | 0 | b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4673 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4674 | 0 | gen_not(b1); |
4675 | 0 | b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4676 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4677 | 0 | gen_not(b2); |
4678 | 0 | gen_and(b1, b2); |
4679 | 0 | gen_or(b0, b2); |
4680 | | |
4681 | | /* |
4682 | | * If the high-order bit of the type value is 0, this |
4683 | | * is a management frame. |
4684 | | * I.e, check "(link[0] & 0x08)". |
4685 | | */ |
4686 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4687 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
4688 | 0 | b1->s.k = 0x08; |
4689 | 0 | b1->stmts = s; |
4690 | | |
4691 | | /* |
4692 | | * AND that with the check for frames other than |
4693 | | * CTS and ACK frames. |
4694 | | */ |
4695 | 0 | gen_and(b1, b2); |
4696 | | |
4697 | | /* |
4698 | | * Check addr2. |
4699 | | */ |
4700 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4701 | 0 | gen_and(b2, b1); |
4702 | 0 | return b1; |
4703 | 0 | } |
4704 | 0 | abort(); |
4705 | | /*NOTREACHED*/ |
4706 | 0 | } |
4707 | | |
4708 | | /* |
4709 | | * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel. |
4710 | | * (We assume that the addresses are IEEE 48-bit MAC addresses, |
4711 | | * as the RFC states.) |
4712 | | */ |
4713 | | static struct block * |
4714 | | gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4715 | 0 | { |
4716 | 0 | register struct block *b0, *b1; |
4717 | |
|
4718 | 0 | switch (dir) { |
4719 | 0 | case Q_SRC: |
4720 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4721 | | |
4722 | 0 | case Q_DST: |
4723 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr); |
4724 | | |
4725 | 0 | case Q_AND: |
4726 | 0 | b0 = gen_ipfchostop(cstate, eaddr, Q_SRC); |
4727 | 0 | b1 = gen_ipfchostop(cstate, eaddr, Q_DST); |
4728 | 0 | gen_and(b0, b1); |
4729 | 0 | return b1; |
4730 | | |
4731 | 0 | case Q_DEFAULT: |
4732 | 0 | case Q_OR: |
4733 | 0 | b0 = gen_ipfchostop(cstate, eaddr, Q_SRC); |
4734 | 0 | b1 = gen_ipfchostop(cstate, eaddr, Q_DST); |
4735 | 0 | gen_or(b0, b1); |
4736 | 0 | return b1; |
4737 | | |
4738 | 0 | case Q_ADDR1: |
4739 | 0 | bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); |
4740 | | /*NOTREACHED*/ |
4741 | | |
4742 | 0 | case Q_ADDR2: |
4743 | 0 | bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); |
4744 | | /*NOTREACHED*/ |
4745 | | |
4746 | 0 | case Q_ADDR3: |
4747 | 0 | bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); |
4748 | | /*NOTREACHED*/ |
4749 | | |
4750 | 0 | case Q_ADDR4: |
4751 | 0 | bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); |
4752 | | /*NOTREACHED*/ |
4753 | | |
4754 | 0 | case Q_RA: |
4755 | 0 | bpf_error(cstate, "'ra' is only supported on 802.11"); |
4756 | | /*NOTREACHED*/ |
4757 | | |
4758 | 0 | case Q_TA: |
4759 | 0 | bpf_error(cstate, "'ta' is only supported on 802.11"); |
4760 | | /*NOTREACHED*/ |
4761 | 0 | } |
4762 | 0 | abort(); |
4763 | | /*NOTREACHED*/ |
4764 | 0 | } |
4765 | | |
4766 | | /* |
4767 | | * This is quite tricky because there may be pad bytes in front of the |
4768 | | * DECNET header, and then there are two possible data packet formats that |
4769 | | * carry both src and dst addresses, plus 5 packet types in a format that |
4770 | | * carries only the src node, plus 2 types that use a different format and |
4771 | | * also carry just the src node. |
4772 | | * |
4773 | | * Yuck. |
4774 | | * |
4775 | | * Instead of doing those all right, we just look for data packets with |
4776 | | * 0 or 1 bytes of padding. If you want to look at other packets, that |
4777 | | * will require a lot more hacking. |
4778 | | * |
4779 | | * To add support for filtering on DECNET "areas" (network numbers) |
4780 | | * one would want to add a "mask" argument to this routine. That would |
4781 | | * make the filter even more inefficient, although one could be clever |
4782 | | * and not generate masking instructions if the mask is 0xFFFF. |
4783 | | */ |
4784 | | static struct block * |
4785 | | gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir) |
4786 | 0 | { |
4787 | 0 | struct block *b0, *b1, *b2, *tmp; |
4788 | 0 | u_int offset_lh; /* offset if long header is received */ |
4789 | 0 | u_int offset_sh; /* offset if short header is received */ |
4790 | |
|
4791 | 0 | switch (dir) { |
4792 | | |
4793 | 0 | case Q_DST: |
4794 | 0 | offset_sh = 1; /* follows flags */ |
4795 | 0 | offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ |
4796 | 0 | break; |
4797 | | |
4798 | 0 | case Q_SRC: |
4799 | 0 | offset_sh = 3; /* follows flags, dstnode */ |
4800 | 0 | offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ |
4801 | 0 | break; |
4802 | | |
4803 | 0 | case Q_AND: |
4804 | | /* Inefficient because we do our Calvinball dance twice */ |
4805 | 0 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4806 | 0 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4807 | 0 | gen_and(b0, b1); |
4808 | 0 | return b1; |
4809 | | |
4810 | 0 | case Q_DEFAULT: |
4811 | 0 | case Q_OR: |
4812 | | /* Inefficient because we do our Calvinball dance twice */ |
4813 | 0 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4814 | 0 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4815 | 0 | gen_or(b0, b1); |
4816 | 0 | return b1; |
4817 | | |
4818 | 0 | case Q_ADDR1: |
4819 | 0 | bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4820 | | /*NOTREACHED*/ |
4821 | | |
4822 | 0 | case Q_ADDR2: |
4823 | 0 | bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4824 | | /*NOTREACHED*/ |
4825 | | |
4826 | 0 | case Q_ADDR3: |
4827 | 0 | bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4828 | | /*NOTREACHED*/ |
4829 | | |
4830 | 0 | case Q_ADDR4: |
4831 | 0 | bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); |
4832 | | /*NOTREACHED*/ |
4833 | | |
4834 | 0 | case Q_RA: |
4835 | 0 | bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4836 | | /*NOTREACHED*/ |
4837 | | |
4838 | 0 | case Q_TA: |
4839 | 0 | bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); |
4840 | | /*NOTREACHED*/ |
4841 | | |
4842 | 0 | default: |
4843 | 0 | abort(); |
4844 | | /*NOTREACHED*/ |
4845 | 0 | } |
4846 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_DN); |
4847 | | /* Check for pad = 1, long header case */ |
4848 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, |
4849 | 0 | (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF)); |
4850 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh, |
4851 | 0 | BPF_H, (bpf_int32)ntohs((u_short)addr)); |
4852 | 0 | gen_and(tmp, b1); |
4853 | | /* Check for pad = 0, long header case */ |
4854 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7); |
4855 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
4856 | 0 | gen_and(tmp, b2); |
4857 | 0 | gen_or(b2, b1); |
4858 | | /* Check for pad = 1, short header case */ |
4859 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, |
4860 | 0 | (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF)); |
4861 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
4862 | 0 | gen_and(tmp, b2); |
4863 | 0 | gen_or(b2, b1); |
4864 | | /* Check for pad = 0, short header case */ |
4865 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7); |
4866 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr)); |
4867 | 0 | gen_and(tmp, b2); |
4868 | 0 | gen_or(b2, b1); |
4869 | | |
4870 | | /* Combine with test for cstate->linktype */ |
4871 | 0 | gen_and(b0, b1); |
4872 | 0 | return b1; |
4873 | 0 | } |
4874 | | |
4875 | | /* |
4876 | | * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets; |
4877 | | * test the bottom-of-stack bit, and then check the version number |
4878 | | * field in the IP header. |
4879 | | */ |
4880 | | static struct block * |
4881 | | gen_mpls_linktype(compiler_state_t *cstate, int proto) |
4882 | 0 | { |
4883 | 0 | struct block *b0, *b1; |
4884 | |
|
4885 | 0 | switch (proto) { |
4886 | | |
4887 | 0 | case Q_IP: |
4888 | | /* match the bottom-of-stack bit */ |
4889 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
4890 | | /* match the IPv4 version number */ |
4891 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0); |
4892 | 0 | gen_and(b0, b1); |
4893 | 0 | return b1; |
4894 | | |
4895 | 0 | case Q_IPV6: |
4896 | | /* match the bottom-of-stack bit */ |
4897 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
4898 | | /* match the IPv4 version number */ |
4899 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0); |
4900 | 0 | gen_and(b0, b1); |
4901 | 0 | return b1; |
4902 | | |
4903 | 0 | default: |
4904 | 0 | abort(); |
4905 | 0 | } |
4906 | 0 | } |
4907 | | |
4908 | | static struct block * |
4909 | | gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
4910 | | int proto, int dir, int type) |
4911 | 0 | { |
4912 | 0 | struct block *b0, *b1; |
4913 | 0 | const char *typestr; |
4914 | |
|
4915 | 0 | if (type == Q_NET) |
4916 | 0 | typestr = "net"; |
4917 | 0 | else |
4918 | 0 | typestr = "host"; |
4919 | |
|
4920 | 0 | switch (proto) { |
4921 | | |
4922 | 0 | case Q_DEFAULT: |
4923 | 0 | b0 = gen_host(cstate, addr, mask, Q_IP, dir, type); |
4924 | | /* |
4925 | | * Only check for non-IPv4 addresses if we're not |
4926 | | * checking MPLS-encapsulated packets. |
4927 | | */ |
4928 | 0 | if (cstate->label_stack_depth == 0) { |
4929 | 0 | b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type); |
4930 | 0 | gen_or(b0, b1); |
4931 | 0 | b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type); |
4932 | 0 | gen_or(b1, b0); |
4933 | 0 | } |
4934 | 0 | return b0; |
4935 | | |
4936 | 0 | case Q_LINK: |
4937 | 0 | bpf_error(cstate, "link-layer modifier applied to %s", typestr); |
4938 | | |
4939 | 0 | case Q_IP: |
4940 | 0 | return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16); |
4941 | | |
4942 | 0 | case Q_RARP: |
4943 | 0 | return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24); |
4944 | | |
4945 | 0 | case Q_ARP: |
4946 | 0 | return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24); |
4947 | | |
4948 | 0 | case Q_SCTP: |
4949 | 0 | bpf_error(cstate, "'sctp' modifier applied to %s", typestr); |
4950 | | |
4951 | 0 | case Q_TCP: |
4952 | 0 | bpf_error(cstate, "'tcp' modifier applied to %s", typestr); |
4953 | | |
4954 | 0 | case Q_UDP: |
4955 | 0 | bpf_error(cstate, "'udp' modifier applied to %s", typestr); |
4956 | | |
4957 | 0 | case Q_ICMP: |
4958 | 0 | bpf_error(cstate, "'icmp' modifier applied to %s", typestr); |
4959 | | |
4960 | 0 | case Q_IGMP: |
4961 | 0 | bpf_error(cstate, "'igmp' modifier applied to %s", typestr); |
4962 | | |
4963 | 0 | case Q_IGRP: |
4964 | 0 | bpf_error(cstate, "'igrp' modifier applied to %s", typestr); |
4965 | | |
4966 | 0 | case Q_ATALK: |
4967 | 0 | bpf_error(cstate, "AppleTalk host filtering not implemented"); |
4968 | | |
4969 | 0 | case Q_DECNET: |
4970 | 0 | return gen_dnhostop(cstate, addr, dir); |
4971 | | |
4972 | 0 | case Q_LAT: |
4973 | 0 | bpf_error(cstate, "LAT host filtering not implemented"); |
4974 | | |
4975 | 0 | case Q_SCA: |
4976 | 0 | bpf_error(cstate, "SCA host filtering not implemented"); |
4977 | | |
4978 | 0 | case Q_MOPRC: |
4979 | 0 | bpf_error(cstate, "MOPRC host filtering not implemented"); |
4980 | | |
4981 | 0 | case Q_MOPDL: |
4982 | 0 | bpf_error(cstate, "MOPDL host filtering not implemented"); |
4983 | | |
4984 | 0 | case Q_IPV6: |
4985 | 0 | bpf_error(cstate, "'ip6' modifier applied to ip host"); |
4986 | | |
4987 | 0 | case Q_ICMPV6: |
4988 | 0 | bpf_error(cstate, "'icmp6' modifier applied to %s", typestr); |
4989 | | |
4990 | 0 | case Q_AH: |
4991 | 0 | bpf_error(cstate, "'ah' modifier applied to %s", typestr); |
4992 | | |
4993 | 0 | case Q_ESP: |
4994 | 0 | bpf_error(cstate, "'esp' modifier applied to %s", typestr); |
4995 | | |
4996 | 0 | case Q_PIM: |
4997 | 0 | bpf_error(cstate, "'pim' modifier applied to %s", typestr); |
4998 | | |
4999 | 0 | case Q_VRRP: |
5000 | 0 | bpf_error(cstate, "'vrrp' modifier applied to %s", typestr); |
5001 | | |
5002 | 0 | case Q_AARP: |
5003 | 0 | bpf_error(cstate, "AARP host filtering not implemented"); |
5004 | | |
5005 | 0 | case Q_ISO: |
5006 | 0 | bpf_error(cstate, "ISO host filtering not implemented"); |
5007 | | |
5008 | 0 | case Q_ESIS: |
5009 | 0 | bpf_error(cstate, "'esis' modifier applied to %s", typestr); |
5010 | | |
5011 | 0 | case Q_ISIS: |
5012 | 0 | bpf_error(cstate, "'isis' modifier applied to %s", typestr); |
5013 | | |
5014 | 0 | case Q_CLNP: |
5015 | 0 | bpf_error(cstate, "'clnp' modifier applied to %s", typestr); |
5016 | | |
5017 | 0 | case Q_STP: |
5018 | 0 | bpf_error(cstate, "'stp' modifier applied to %s", typestr); |
5019 | | |
5020 | 0 | case Q_IPX: |
5021 | 0 | bpf_error(cstate, "IPX host filtering not implemented"); |
5022 | | |
5023 | 0 | case Q_NETBEUI: |
5024 | 0 | bpf_error(cstate, "'netbeui' modifier applied to %s", typestr); |
5025 | | |
5026 | 0 | case Q_ISIS_L1: |
5027 | 0 | bpf_error(cstate, "'l1' modifier applied to %s", typestr); |
5028 | | |
5029 | 0 | case Q_ISIS_L2: |
5030 | 0 | bpf_error(cstate, "'l2' modifier applied to %s", typestr); |
5031 | | |
5032 | 0 | case Q_ISIS_IIH: |
5033 | 0 | bpf_error(cstate, "'iih' modifier applied to %s", typestr); |
5034 | | |
5035 | 0 | case Q_ISIS_SNP: |
5036 | 0 | bpf_error(cstate, "'snp' modifier applied to %s", typestr); |
5037 | | |
5038 | 0 | case Q_ISIS_CSNP: |
5039 | 0 | bpf_error(cstate, "'csnp' modifier applied to %s", typestr); |
5040 | | |
5041 | 0 | case Q_ISIS_PSNP: |
5042 | 0 | bpf_error(cstate, "'psnp' modifier applied to %s", typestr); |
5043 | | |
5044 | 0 | case Q_ISIS_LSP: |
5045 | 0 | bpf_error(cstate, "'lsp' modifier applied to %s", typestr); |
5046 | | |
5047 | 0 | case Q_RADIO: |
5048 | 0 | bpf_error(cstate, "'radio' modifier applied to %s", typestr); |
5049 | | |
5050 | 0 | case Q_CARP: |
5051 | 0 | bpf_error(cstate, "'carp' modifier applied to %s", typestr); |
5052 | | |
5053 | 0 | default: |
5054 | 0 | abort(); |
5055 | 0 | } |
5056 | | /*NOTREACHED*/ |
5057 | 0 | } |
5058 | | |
5059 | | #ifdef INET6 |
5060 | | static struct block * |
5061 | | gen_host6(compiler_state_t *cstate, struct in6_addr *addr, |
5062 | | struct in6_addr *mask, int proto, int dir, int type) |
5063 | 0 | { |
5064 | 0 | const char *typestr; |
5065 | |
|
5066 | 0 | if (type == Q_NET) |
5067 | 0 | typestr = "net"; |
5068 | 0 | else |
5069 | 0 | typestr = "host"; |
5070 | |
|
5071 | 0 | switch (proto) { |
5072 | | |
5073 | 0 | case Q_DEFAULT: |
5074 | 0 | return gen_host6(cstate, addr, mask, Q_IPV6, dir, type); |
5075 | | |
5076 | 0 | case Q_LINK: |
5077 | 0 | bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr); |
5078 | | |
5079 | 0 | case Q_IP: |
5080 | 0 | bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr); |
5081 | | |
5082 | 0 | case Q_RARP: |
5083 | 0 | bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr); |
5084 | | |
5085 | 0 | case Q_ARP: |
5086 | 0 | bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr); |
5087 | | |
5088 | 0 | case Q_SCTP: |
5089 | 0 | bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr); |
5090 | | |
5091 | 0 | case Q_TCP: |
5092 | 0 | bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr); |
5093 | | |
5094 | 0 | case Q_UDP: |
5095 | 0 | bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr); |
5096 | | |
5097 | 0 | case Q_ICMP: |
5098 | 0 | bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr); |
5099 | | |
5100 | 0 | case Q_IGMP: |
5101 | 0 | bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr); |
5102 | | |
5103 | 0 | case Q_IGRP: |
5104 | 0 | bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr); |
5105 | | |
5106 | 0 | case Q_ATALK: |
5107 | 0 | bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr); |
5108 | | |
5109 | 0 | case Q_DECNET: |
5110 | 0 | bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr); |
5111 | | |
5112 | 0 | case Q_LAT: |
5113 | 0 | bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr); |
5114 | | |
5115 | 0 | case Q_SCA: |
5116 | 0 | bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr); |
5117 | | |
5118 | 0 | case Q_MOPRC: |
5119 | 0 | bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr); |
5120 | | |
5121 | 0 | case Q_MOPDL: |
5122 | 0 | bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr); |
5123 | | |
5124 | 0 | case Q_IPV6: |
5125 | 0 | return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24); |
5126 | | |
5127 | 0 | case Q_ICMPV6: |
5128 | 0 | bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr); |
5129 | | |
5130 | 0 | case Q_AH: |
5131 | 0 | bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr); |
5132 | | |
5133 | 0 | case Q_ESP: |
5134 | 0 | bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr); |
5135 | | |
5136 | 0 | case Q_PIM: |
5137 | 0 | bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr); |
5138 | | |
5139 | 0 | case Q_VRRP: |
5140 | 0 | bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr); |
5141 | | |
5142 | 0 | case Q_AARP: |
5143 | 0 | bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr); |
5144 | | |
5145 | 0 | case Q_ISO: |
5146 | 0 | bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr); |
5147 | | |
5148 | 0 | case Q_ESIS: |
5149 | 0 | bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr); |
5150 | | |
5151 | 0 | case Q_ISIS: |
5152 | 0 | bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr); |
5153 | | |
5154 | 0 | case Q_CLNP: |
5155 | 0 | bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr); |
5156 | | |
5157 | 0 | case Q_STP: |
5158 | 0 | bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr); |
5159 | | |
5160 | 0 | case Q_IPX: |
5161 | 0 | bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr); |
5162 | | |
5163 | 0 | case Q_NETBEUI: |
5164 | 0 | bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr); |
5165 | | |
5166 | 0 | case Q_ISIS_L1: |
5167 | 0 | bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr); |
5168 | | |
5169 | 0 | case Q_ISIS_L2: |
5170 | 0 | bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr); |
5171 | | |
5172 | 0 | case Q_ISIS_IIH: |
5173 | 0 | bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr); |
5174 | | |
5175 | 0 | case Q_ISIS_SNP: |
5176 | 0 | bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr); |
5177 | | |
5178 | 0 | case Q_ISIS_CSNP: |
5179 | 0 | bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr); |
5180 | | |
5181 | 0 | case Q_ISIS_PSNP: |
5182 | 0 | bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr); |
5183 | | |
5184 | 0 | case Q_ISIS_LSP: |
5185 | 0 | bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr); |
5186 | | |
5187 | 0 | case Q_RADIO: |
5188 | 0 | bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr); |
5189 | | |
5190 | 0 | case Q_CARP: |
5191 | 0 | bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr); |
5192 | | |
5193 | 0 | default: |
5194 | 0 | abort(); |
5195 | 0 | } |
5196 | | /*NOTREACHED*/ |
5197 | 0 | } |
5198 | | #endif |
5199 | | |
5200 | | #ifndef INET6 |
5201 | | static struct block * |
5202 | | gen_gateway(compiler_state_t *cstate, const u_char *eaddr, |
5203 | | struct addrinfo *alist, int proto, int dir) |
5204 | | { |
5205 | | struct block *b0, *b1, *tmp; |
5206 | | struct addrinfo *ai; |
5207 | | struct sockaddr_in *sin; |
5208 | | |
5209 | | if (dir != 0) |
5210 | | bpf_error(cstate, "direction applied to 'gateway'"); |
5211 | | |
5212 | | switch (proto) { |
5213 | | case Q_DEFAULT: |
5214 | | case Q_IP: |
5215 | | case Q_ARP: |
5216 | | case Q_RARP: |
5217 | | switch (cstate->linktype) { |
5218 | | case DLT_EN10MB: |
5219 | | case DLT_NETANALYZER: |
5220 | | case DLT_NETANALYZER_TRANSPARENT: |
5221 | | b1 = gen_prevlinkhdr_check(cstate); |
5222 | | b0 = gen_ehostop(cstate, eaddr, Q_OR); |
5223 | | if (b1 != NULL) |
5224 | | gen_and(b1, b0); |
5225 | | break; |
5226 | | case DLT_FDDI: |
5227 | | b0 = gen_fhostop(cstate, eaddr, Q_OR); |
5228 | | break; |
5229 | | case DLT_IEEE802: |
5230 | | b0 = gen_thostop(cstate, eaddr, Q_OR); |
5231 | | break; |
5232 | | case DLT_IEEE802_11: |
5233 | | case DLT_PRISM_HEADER: |
5234 | | case DLT_IEEE802_11_RADIO_AVS: |
5235 | | case DLT_IEEE802_11_RADIO: |
5236 | | case DLT_PPI: |
5237 | | b0 = gen_wlanhostop(cstate, eaddr, Q_OR); |
5238 | | break; |
5239 | | case DLT_SUNATM: |
5240 | | /* |
5241 | | * This is LLC-multiplexed traffic; if it were |
5242 | | * LANE, cstate->linktype would have been set to |
5243 | | * DLT_EN10MB. |
5244 | | */ |
5245 | | bpf_error(cstate, |
5246 | | "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
5247 | | break; |
5248 | | case DLT_IP_OVER_FC: |
5249 | | b0 = gen_ipfchostop(cstate, eaddr, Q_OR); |
5250 | | break; |
5251 | | default: |
5252 | | bpf_error(cstate, |
5253 | | "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
5254 | | } |
5255 | | b1 = NULL; |
5256 | | for (ai = alist; ai != NULL; ai = ai->ai_next) { |
5257 | | /* |
5258 | | * Does it have an address? |
5259 | | */ |
5260 | | if (ai->ai_addr != NULL) { |
5261 | | /* |
5262 | | * Yes. Is it an IPv4 address? |
5263 | | */ |
5264 | | if (ai->ai_addr->sa_family == AF_INET) { |
5265 | | /* |
5266 | | * Generate an entry for it. |
5267 | | */ |
5268 | | sin = (struct sockaddr_in *)ai->ai_addr; |
5269 | | tmp = gen_host(cstate, |
5270 | | ntohl(sin->sin_addr.s_addr), |
5271 | | 0xffffffff, proto, Q_OR, Q_HOST); |
5272 | | /* |
5273 | | * Is it the *first* IPv4 address? |
5274 | | */ |
5275 | | if (b1 == NULL) { |
5276 | | /* |
5277 | | * Yes, so start with it. |
5278 | | */ |
5279 | | b1 = tmp; |
5280 | | } else { |
5281 | | /* |
5282 | | * No, so OR it into the |
5283 | | * existing set of |
5284 | | * addresses. |
5285 | | */ |
5286 | | gen_or(b1, tmp); |
5287 | | b1 = tmp; |
5288 | | } |
5289 | | } |
5290 | | } |
5291 | | } |
5292 | | if (b1 == NULL) { |
5293 | | /* |
5294 | | * No IPv4 addresses found. |
5295 | | */ |
5296 | | return (NULL); |
5297 | | } |
5298 | | gen_not(b1); |
5299 | | gen_and(b0, b1); |
5300 | | return b1; |
5301 | | } |
5302 | | bpf_error(cstate, "illegal modifier of 'gateway'"); |
5303 | | /*NOTREACHED*/ |
5304 | | } |
5305 | | #endif |
5306 | | |
5307 | | static struct block * |
5308 | | gen_proto_abbrev_internal(compiler_state_t *cstate, int proto) |
5309 | 0 | { |
5310 | 0 | struct block *b0; |
5311 | 0 | struct block *b1; |
5312 | |
|
5313 | 0 | switch (proto) { |
5314 | | |
5315 | 0 | case Q_SCTP: |
5316 | 0 | b1 = gen_proto(cstate, IPPROTO_SCTP, Q_IP, Q_DEFAULT); |
5317 | 0 | b0 = gen_proto(cstate, IPPROTO_SCTP, Q_IPV6, Q_DEFAULT); |
5318 | 0 | gen_or(b0, b1); |
5319 | 0 | break; |
5320 | | |
5321 | 0 | case Q_TCP: |
5322 | 0 | b1 = gen_proto(cstate, IPPROTO_TCP, Q_IP, Q_DEFAULT); |
5323 | 0 | b0 = gen_proto(cstate, IPPROTO_TCP, Q_IPV6, Q_DEFAULT); |
5324 | 0 | gen_or(b0, b1); |
5325 | 0 | break; |
5326 | | |
5327 | 0 | case Q_UDP: |
5328 | 0 | b1 = gen_proto(cstate, IPPROTO_UDP, Q_IP, Q_DEFAULT); |
5329 | 0 | b0 = gen_proto(cstate, IPPROTO_UDP, Q_IPV6, Q_DEFAULT); |
5330 | 0 | gen_or(b0, b1); |
5331 | 0 | break; |
5332 | | |
5333 | 0 | case Q_ICMP: |
5334 | 0 | b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT); |
5335 | 0 | break; |
5336 | | |
5337 | | #ifndef IPPROTO_IGMP |
5338 | | #define IPPROTO_IGMP 2 |
5339 | | #endif |
5340 | | |
5341 | 0 | case Q_IGMP: |
5342 | 0 | b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT); |
5343 | 0 | break; |
5344 | | |
5345 | 0 | #ifndef IPPROTO_IGRP |
5346 | 0 | #define IPPROTO_IGRP 9 |
5347 | 0 | #endif |
5348 | 0 | case Q_IGRP: |
5349 | 0 | b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT); |
5350 | 0 | break; |
5351 | | |
5352 | | #ifndef IPPROTO_PIM |
5353 | | #define IPPROTO_PIM 103 |
5354 | | #endif |
5355 | | |
5356 | 0 | case Q_PIM: |
5357 | 0 | b1 = gen_proto(cstate, IPPROTO_PIM, Q_IP, Q_DEFAULT); |
5358 | 0 | b0 = gen_proto(cstate, IPPROTO_PIM, Q_IPV6, Q_DEFAULT); |
5359 | 0 | gen_or(b0, b1); |
5360 | 0 | break; |
5361 | | |
5362 | 0 | #ifndef IPPROTO_VRRP |
5363 | 0 | #define IPPROTO_VRRP 112 |
5364 | 0 | #endif |
5365 | | |
5366 | 0 | case Q_VRRP: |
5367 | 0 | b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT); |
5368 | 0 | break; |
5369 | | |
5370 | 0 | #ifndef IPPROTO_CARP |
5371 | 0 | #define IPPROTO_CARP 112 |
5372 | 0 | #endif |
5373 | | |
5374 | 0 | case Q_CARP: |
5375 | 0 | b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT); |
5376 | 0 | break; |
5377 | | |
5378 | 0 | case Q_IP: |
5379 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_IP); |
5380 | 0 | break; |
5381 | | |
5382 | 0 | case Q_ARP: |
5383 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_ARP); |
5384 | 0 | break; |
5385 | | |
5386 | 0 | case Q_RARP: |
5387 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_REVARP); |
5388 | 0 | break; |
5389 | | |
5390 | 0 | case Q_LINK: |
5391 | 0 | bpf_error(cstate, "link layer applied in wrong context"); |
5392 | | |
5393 | 0 | case Q_ATALK: |
5394 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_ATALK); |
5395 | 0 | break; |
5396 | | |
5397 | 0 | case Q_AARP: |
5398 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_AARP); |
5399 | 0 | break; |
5400 | | |
5401 | 0 | case Q_DECNET: |
5402 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_DN); |
5403 | 0 | break; |
5404 | | |
5405 | 0 | case Q_SCA: |
5406 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_SCA); |
5407 | 0 | break; |
5408 | | |
5409 | 0 | case Q_LAT: |
5410 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_LAT); |
5411 | 0 | break; |
5412 | | |
5413 | 0 | case Q_MOPDL: |
5414 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_MOPDL); |
5415 | 0 | break; |
5416 | | |
5417 | 0 | case Q_MOPRC: |
5418 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_MOPRC); |
5419 | 0 | break; |
5420 | | |
5421 | 0 | case Q_IPV6: |
5422 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_IPV6); |
5423 | 0 | break; |
5424 | | |
5425 | | #ifndef IPPROTO_ICMPV6 |
5426 | | #define IPPROTO_ICMPV6 58 |
5427 | | #endif |
5428 | 0 | case Q_ICMPV6: |
5429 | 0 | b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT); |
5430 | 0 | break; |
5431 | | |
5432 | | #ifndef IPPROTO_AH |
5433 | | #define IPPROTO_AH 51 |
5434 | | #endif |
5435 | 0 | case Q_AH: |
5436 | 0 | b1 = gen_proto(cstate, IPPROTO_AH, Q_IP, Q_DEFAULT); |
5437 | 0 | b0 = gen_proto(cstate, IPPROTO_AH, Q_IPV6, Q_DEFAULT); |
5438 | 0 | gen_or(b0, b1); |
5439 | 0 | break; |
5440 | | |
5441 | | #ifndef IPPROTO_ESP |
5442 | | #define IPPROTO_ESP 50 |
5443 | | #endif |
5444 | 0 | case Q_ESP: |
5445 | 0 | b1 = gen_proto(cstate, IPPROTO_ESP, Q_IP, Q_DEFAULT); |
5446 | 0 | b0 = gen_proto(cstate, IPPROTO_ESP, Q_IPV6, Q_DEFAULT); |
5447 | 0 | gen_or(b0, b1); |
5448 | 0 | break; |
5449 | | |
5450 | 0 | case Q_ISO: |
5451 | 0 | b1 = gen_linktype(cstate, LLCSAP_ISONS); |
5452 | 0 | break; |
5453 | | |
5454 | 0 | case Q_ESIS: |
5455 | 0 | b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT); |
5456 | 0 | break; |
5457 | | |
5458 | 0 | case Q_ISIS: |
5459 | 0 | b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT); |
5460 | 0 | break; |
5461 | | |
5462 | 0 | case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */ |
5463 | 0 | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); |
5464 | 0 | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ |
5465 | 0 | gen_or(b0, b1); |
5466 | 0 | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); |
5467 | 0 | gen_or(b0, b1); |
5468 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
5469 | 0 | gen_or(b0, b1); |
5470 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
5471 | 0 | gen_or(b0, b1); |
5472 | 0 | break; |
5473 | | |
5474 | 0 | case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */ |
5475 | 0 | b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); |
5476 | 0 | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ |
5477 | 0 | gen_or(b0, b1); |
5478 | 0 | b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); |
5479 | 0 | gen_or(b0, b1); |
5480 | 0 | b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
5481 | 0 | gen_or(b0, b1); |
5482 | 0 | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
5483 | 0 | gen_or(b0, b1); |
5484 | 0 | break; |
5485 | | |
5486 | 0 | case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */ |
5487 | 0 | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); |
5488 | 0 | b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); |
5489 | 0 | gen_or(b0, b1); |
5490 | 0 | b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); |
5491 | 0 | gen_or(b0, b1); |
5492 | 0 | break; |
5493 | | |
5494 | 0 | case Q_ISIS_LSP: |
5495 | 0 | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); |
5496 | 0 | b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); |
5497 | 0 | gen_or(b0, b1); |
5498 | 0 | break; |
5499 | | |
5500 | 0 | case Q_ISIS_SNP: |
5501 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
5502 | 0 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
5503 | 0 | gen_or(b0, b1); |
5504 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
5505 | 0 | gen_or(b0, b1); |
5506 | 0 | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
5507 | 0 | gen_or(b0, b1); |
5508 | 0 | break; |
5509 | | |
5510 | 0 | case Q_ISIS_CSNP: |
5511 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); |
5512 | 0 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); |
5513 | 0 | gen_or(b0, b1); |
5514 | 0 | break; |
5515 | | |
5516 | 0 | case Q_ISIS_PSNP: |
5517 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); |
5518 | 0 | b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); |
5519 | 0 | gen_or(b0, b1); |
5520 | 0 | break; |
5521 | | |
5522 | 0 | case Q_CLNP: |
5523 | 0 | b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT); |
5524 | 0 | break; |
5525 | | |
5526 | 0 | case Q_STP: |
5527 | 0 | b1 = gen_linktype(cstate, LLCSAP_8021D); |
5528 | 0 | break; |
5529 | | |
5530 | 0 | case Q_IPX: |
5531 | 0 | b1 = gen_linktype(cstate, LLCSAP_IPX); |
5532 | 0 | break; |
5533 | | |
5534 | 0 | case Q_NETBEUI: |
5535 | 0 | b1 = gen_linktype(cstate, LLCSAP_NETBEUI); |
5536 | 0 | break; |
5537 | | |
5538 | 0 | case Q_RADIO: |
5539 | 0 | bpf_error(cstate, "'radio' is not a valid protocol type"); |
5540 | | |
5541 | 0 | default: |
5542 | 0 | abort(); |
5543 | 0 | } |
5544 | 0 | return b1; |
5545 | 0 | } |
5546 | | |
5547 | | struct block * |
5548 | | gen_proto_abbrev(compiler_state_t *cstate, int proto) |
5549 | 0 | { |
5550 | | /* |
5551 | | * Catch errors reported by us and routines below us, and return NULL |
5552 | | * on an error. |
5553 | | */ |
5554 | 0 | if (setjmp(cstate->top_ctx)) |
5555 | 0 | return (NULL); |
5556 | | |
5557 | 0 | return gen_proto_abbrev_internal(cstate, proto); |
5558 | 0 | } |
5559 | | |
5560 | | static struct block * |
5561 | | gen_ipfrag(compiler_state_t *cstate) |
5562 | 0 | { |
5563 | 0 | struct slist *s; |
5564 | 0 | struct block *b; |
5565 | | |
5566 | | /* not IPv4 frag other than the first frag */ |
5567 | 0 | s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H); |
5568 | 0 | b = new_block(cstate, JMP(BPF_JSET)); |
5569 | 0 | b->s.k = 0x1fff; |
5570 | 0 | b->stmts = s; |
5571 | 0 | gen_not(b); |
5572 | |
|
5573 | 0 | return b; |
5574 | 0 | } |
5575 | | |
5576 | | /* |
5577 | | * Generate a comparison to a port value in the transport-layer header |
5578 | | * at the specified offset from the beginning of that header. |
5579 | | * |
5580 | | * XXX - this handles a variable-length prefix preceding the link-layer |
5581 | | * header, such as the radiotap or AVS radio prefix, but doesn't handle |
5582 | | * variable-length link-layer headers (such as Token Ring or 802.11 |
5583 | | * headers). |
5584 | | */ |
5585 | | static struct block * |
5586 | | gen_portatom(compiler_state_t *cstate, int off, bpf_int32 v) |
5587 | 0 | { |
5588 | 0 | return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v); |
5589 | 0 | } |
5590 | | |
5591 | | static struct block * |
5592 | | gen_portatom6(compiler_state_t *cstate, int off, bpf_int32 v) |
5593 | 0 | { |
5594 | 0 | return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v); |
5595 | 0 | } |
5596 | | |
5597 | | struct block * |
5598 | | gen_portop(compiler_state_t *cstate, int port, int proto, int dir) |
5599 | 0 | { |
5600 | 0 | struct block *b0, *b1, *tmp; |
5601 | | |
5602 | | /* ip proto 'proto' and not a fragment other than the first fragment */ |
5603 | 0 | tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto); |
5604 | 0 | b0 = gen_ipfrag(cstate); |
5605 | 0 | gen_and(tmp, b0); |
5606 | |
|
5607 | 0 | switch (dir) { |
5608 | 0 | case Q_SRC: |
5609 | 0 | b1 = gen_portatom(cstate, 0, (bpf_int32)port); |
5610 | 0 | break; |
5611 | | |
5612 | 0 | case Q_DST: |
5613 | 0 | b1 = gen_portatom(cstate, 2, (bpf_int32)port); |
5614 | 0 | break; |
5615 | | |
5616 | 0 | case Q_AND: |
5617 | 0 | tmp = gen_portatom(cstate, 0, (bpf_int32)port); |
5618 | 0 | b1 = gen_portatom(cstate, 2, (bpf_int32)port); |
5619 | 0 | gen_and(tmp, b1); |
5620 | 0 | break; |
5621 | | |
5622 | 0 | case Q_DEFAULT: |
5623 | 0 | case Q_OR: |
5624 | 0 | tmp = gen_portatom(cstate, 0, (bpf_int32)port); |
5625 | 0 | b1 = gen_portatom(cstate, 2, (bpf_int32)port); |
5626 | 0 | gen_or(tmp, b1); |
5627 | 0 | break; |
5628 | | |
5629 | 0 | case Q_ADDR1: |
5630 | 0 | bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports"); |
5631 | | /*NOTREACHED*/ |
5632 | | |
5633 | 0 | case Q_ADDR2: |
5634 | 0 | bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports"); |
5635 | | /*NOTREACHED*/ |
5636 | | |
5637 | 0 | case Q_ADDR3: |
5638 | 0 | bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports"); |
5639 | | /*NOTREACHED*/ |
5640 | | |
5641 | 0 | case Q_ADDR4: |
5642 | 0 | bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports"); |
5643 | | /*NOTREACHED*/ |
5644 | | |
5645 | 0 | case Q_RA: |
5646 | 0 | bpf_error(cstate, "'ra' is not a valid qualifier for ports"); |
5647 | | /*NOTREACHED*/ |
5648 | | |
5649 | 0 | case Q_TA: |
5650 | 0 | bpf_error(cstate, "'ta' is not a valid qualifier for ports"); |
5651 | | /*NOTREACHED*/ |
5652 | | |
5653 | 0 | default: |
5654 | 0 | abort(); |
5655 | | /*NOTREACHED*/ |
5656 | 0 | } |
5657 | 0 | gen_and(b0, b1); |
5658 | |
|
5659 | 0 | return b1; |
5660 | 0 | } |
5661 | | |
5662 | | static struct block * |
5663 | | gen_port(compiler_state_t *cstate, int port, int ip_proto, int dir) |
5664 | 0 | { |
5665 | 0 | struct block *b0, *b1, *tmp; |
5666 | | |
5667 | | /* |
5668 | | * ether proto ip |
5669 | | * |
5670 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
5671 | | * not LLC encapsulation with LLCSAP_IP. |
5672 | | * |
5673 | | * For IEEE 802 networks - which includes 802.5 token ring |
5674 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
5675 | | * says that SNAP encapsulation is used, not LLC encapsulation |
5676 | | * with LLCSAP_IP. |
5677 | | * |
5678 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
5679 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
5680 | | * encapsulation with LLCSAP_IP. |
5681 | | * |
5682 | | * So we always check for ETHERTYPE_IP. |
5683 | | */ |
5684 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
5685 | |
|
5686 | 0 | switch (ip_proto) { |
5687 | 0 | case IPPROTO_UDP: |
5688 | 0 | case IPPROTO_TCP: |
5689 | 0 | case IPPROTO_SCTP: |
5690 | 0 | b1 = gen_portop(cstate, port, ip_proto, dir); |
5691 | 0 | break; |
5692 | | |
5693 | 0 | case PROTO_UNDEF: |
5694 | 0 | tmp = gen_portop(cstate, port, IPPROTO_TCP, dir); |
5695 | 0 | b1 = gen_portop(cstate, port, IPPROTO_UDP, dir); |
5696 | 0 | gen_or(tmp, b1); |
5697 | 0 | tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir); |
5698 | 0 | gen_or(tmp, b1); |
5699 | 0 | break; |
5700 | | |
5701 | 0 | default: |
5702 | 0 | abort(); |
5703 | 0 | } |
5704 | 0 | gen_and(b0, b1); |
5705 | 0 | return b1; |
5706 | 0 | } |
5707 | | |
5708 | | struct block * |
5709 | | gen_portop6(compiler_state_t *cstate, int port, int proto, int dir) |
5710 | 0 | { |
5711 | 0 | struct block *b0, *b1, *tmp; |
5712 | | |
5713 | | /* ip6 proto 'proto' */ |
5714 | | /* XXX - catch the first fragment of a fragmented packet? */ |
5715 | 0 | b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto); |
5716 | |
|
5717 | 0 | switch (dir) { |
5718 | 0 | case Q_SRC: |
5719 | 0 | b1 = gen_portatom6(cstate, 0, (bpf_int32)port); |
5720 | 0 | break; |
5721 | | |
5722 | 0 | case Q_DST: |
5723 | 0 | b1 = gen_portatom6(cstate, 2, (bpf_int32)port); |
5724 | 0 | break; |
5725 | | |
5726 | 0 | case Q_AND: |
5727 | 0 | tmp = gen_portatom6(cstate, 0, (bpf_int32)port); |
5728 | 0 | b1 = gen_portatom6(cstate, 2, (bpf_int32)port); |
5729 | 0 | gen_and(tmp, b1); |
5730 | 0 | break; |
5731 | | |
5732 | 0 | case Q_DEFAULT: |
5733 | 0 | case Q_OR: |
5734 | 0 | tmp = gen_portatom6(cstate, 0, (bpf_int32)port); |
5735 | 0 | b1 = gen_portatom6(cstate, 2, (bpf_int32)port); |
5736 | 0 | gen_or(tmp, b1); |
5737 | 0 | break; |
5738 | | |
5739 | 0 | default: |
5740 | 0 | abort(); |
5741 | 0 | } |
5742 | 0 | gen_and(b0, b1); |
5743 | |
|
5744 | 0 | return b1; |
5745 | 0 | } |
5746 | | |
5747 | | static struct block * |
5748 | | gen_port6(compiler_state_t *cstate, int port, int ip_proto, int dir) |
5749 | 0 | { |
5750 | 0 | struct block *b0, *b1, *tmp; |
5751 | | |
5752 | | /* link proto ip6 */ |
5753 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
5754 | |
|
5755 | 0 | switch (ip_proto) { |
5756 | 0 | case IPPROTO_UDP: |
5757 | 0 | case IPPROTO_TCP: |
5758 | 0 | case IPPROTO_SCTP: |
5759 | 0 | b1 = gen_portop6(cstate, port, ip_proto, dir); |
5760 | 0 | break; |
5761 | | |
5762 | 0 | case PROTO_UNDEF: |
5763 | 0 | tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir); |
5764 | 0 | b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir); |
5765 | 0 | gen_or(tmp, b1); |
5766 | 0 | tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir); |
5767 | 0 | gen_or(tmp, b1); |
5768 | 0 | break; |
5769 | | |
5770 | 0 | default: |
5771 | 0 | abort(); |
5772 | 0 | } |
5773 | 0 | gen_and(b0, b1); |
5774 | 0 | return b1; |
5775 | 0 | } |
5776 | | |
5777 | | /* gen_portrange code */ |
5778 | | static struct block * |
5779 | | gen_portrangeatom(compiler_state_t *cstate, int off, bpf_int32 v1, |
5780 | | bpf_int32 v2) |
5781 | 0 | { |
5782 | 0 | struct block *b1, *b2; |
5783 | |
|
5784 | 0 | if (v1 > v2) { |
5785 | | /* |
5786 | | * Reverse the order of the ports, so v1 is the lower one. |
5787 | | */ |
5788 | 0 | bpf_int32 vtemp; |
5789 | |
|
5790 | 0 | vtemp = v1; |
5791 | 0 | v1 = v2; |
5792 | 0 | v2 = vtemp; |
5793 | 0 | } |
5794 | |
|
5795 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1); |
5796 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2); |
5797 | |
|
5798 | 0 | gen_and(b1, b2); |
5799 | |
|
5800 | 0 | return b2; |
5801 | 0 | } |
5802 | | |
5803 | | struct block * |
5804 | | gen_portrangeop(compiler_state_t *cstate, int port1, int port2, int proto, |
5805 | | int dir) |
5806 | 0 | { |
5807 | 0 | struct block *b0, *b1, *tmp; |
5808 | | |
5809 | | /* ip proto 'proto' and not a fragment other than the first fragment */ |
5810 | 0 | tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)proto); |
5811 | 0 | b0 = gen_ipfrag(cstate); |
5812 | 0 | gen_and(tmp, b0); |
5813 | |
|
5814 | 0 | switch (dir) { |
5815 | 0 | case Q_SRC: |
5816 | 0 | b1 = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5817 | 0 | break; |
5818 | | |
5819 | 0 | case Q_DST: |
5820 | 0 | b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5821 | 0 | break; |
5822 | | |
5823 | 0 | case Q_AND: |
5824 | 0 | tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5825 | 0 | b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5826 | 0 | gen_and(tmp, b1); |
5827 | 0 | break; |
5828 | | |
5829 | 0 | case Q_DEFAULT: |
5830 | 0 | case Q_OR: |
5831 | 0 | tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5832 | 0 | b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5833 | 0 | gen_or(tmp, b1); |
5834 | 0 | break; |
5835 | | |
5836 | 0 | case Q_ADDR1: |
5837 | 0 | bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges"); |
5838 | | /*NOTREACHED*/ |
5839 | | |
5840 | 0 | case Q_ADDR2: |
5841 | 0 | bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges"); |
5842 | | /*NOTREACHED*/ |
5843 | | |
5844 | 0 | case Q_ADDR3: |
5845 | 0 | bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges"); |
5846 | | /*NOTREACHED*/ |
5847 | | |
5848 | 0 | case Q_ADDR4: |
5849 | 0 | bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges"); |
5850 | | /*NOTREACHED*/ |
5851 | | |
5852 | 0 | case Q_RA: |
5853 | 0 | bpf_error(cstate, "'ra' is not a valid qualifier for port ranges"); |
5854 | | /*NOTREACHED*/ |
5855 | | |
5856 | 0 | case Q_TA: |
5857 | 0 | bpf_error(cstate, "'ta' is not a valid qualifier for port ranges"); |
5858 | | /*NOTREACHED*/ |
5859 | | |
5860 | 0 | default: |
5861 | 0 | abort(); |
5862 | | /*NOTREACHED*/ |
5863 | 0 | } |
5864 | 0 | gen_and(b0, b1); |
5865 | |
|
5866 | 0 | return b1; |
5867 | 0 | } |
5868 | | |
5869 | | static struct block * |
5870 | | gen_portrange(compiler_state_t *cstate, int port1, int port2, int ip_proto, |
5871 | | int dir) |
5872 | 0 | { |
5873 | 0 | struct block *b0, *b1, *tmp; |
5874 | | |
5875 | | /* link proto ip */ |
5876 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
5877 | |
|
5878 | 0 | switch (ip_proto) { |
5879 | 0 | case IPPROTO_UDP: |
5880 | 0 | case IPPROTO_TCP: |
5881 | 0 | case IPPROTO_SCTP: |
5882 | 0 | b1 = gen_portrangeop(cstate, port1, port2, ip_proto, dir); |
5883 | 0 | break; |
5884 | | |
5885 | 0 | case PROTO_UNDEF: |
5886 | 0 | tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir); |
5887 | 0 | b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir); |
5888 | 0 | gen_or(tmp, b1); |
5889 | 0 | tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir); |
5890 | 0 | gen_or(tmp, b1); |
5891 | 0 | break; |
5892 | | |
5893 | 0 | default: |
5894 | 0 | abort(); |
5895 | 0 | } |
5896 | 0 | gen_and(b0, b1); |
5897 | 0 | return b1; |
5898 | 0 | } |
5899 | | |
5900 | | static struct block * |
5901 | | gen_portrangeatom6(compiler_state_t *cstate, int off, bpf_int32 v1, |
5902 | | bpf_int32 v2) |
5903 | 0 | { |
5904 | 0 | struct block *b1, *b2; |
5905 | |
|
5906 | 0 | if (v1 > v2) { |
5907 | | /* |
5908 | | * Reverse the order of the ports, so v1 is the lower one. |
5909 | | */ |
5910 | 0 | bpf_int32 vtemp; |
5911 | |
|
5912 | 0 | vtemp = v1; |
5913 | 0 | v1 = v2; |
5914 | 0 | v2 = vtemp; |
5915 | 0 | } |
5916 | |
|
5917 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1); |
5918 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2); |
5919 | |
|
5920 | 0 | gen_and(b1, b2); |
5921 | |
|
5922 | 0 | return b2; |
5923 | 0 | } |
5924 | | |
5925 | | struct block * |
5926 | | gen_portrangeop6(compiler_state_t *cstate, int port1, int port2, int proto, |
5927 | | int dir) |
5928 | 0 | { |
5929 | 0 | struct block *b0, *b1, *tmp; |
5930 | | |
5931 | | /* ip6 proto 'proto' */ |
5932 | | /* XXX - catch the first fragment of a fragmented packet? */ |
5933 | 0 | b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)proto); |
5934 | |
|
5935 | 0 | switch (dir) { |
5936 | 0 | case Q_SRC: |
5937 | 0 | b1 = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5938 | 0 | break; |
5939 | | |
5940 | 0 | case Q_DST: |
5941 | 0 | b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5942 | 0 | break; |
5943 | | |
5944 | 0 | case Q_AND: |
5945 | 0 | tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5946 | 0 | b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5947 | 0 | gen_and(tmp, b1); |
5948 | 0 | break; |
5949 | | |
5950 | 0 | case Q_DEFAULT: |
5951 | 0 | case Q_OR: |
5952 | 0 | tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2); |
5953 | 0 | b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2); |
5954 | 0 | gen_or(tmp, b1); |
5955 | 0 | break; |
5956 | | |
5957 | 0 | default: |
5958 | 0 | abort(); |
5959 | 0 | } |
5960 | 0 | gen_and(b0, b1); |
5961 | |
|
5962 | 0 | return b1; |
5963 | 0 | } |
5964 | | |
5965 | | static struct block * |
5966 | | gen_portrange6(compiler_state_t *cstate, int port1, int port2, int ip_proto, |
5967 | | int dir) |
5968 | 0 | { |
5969 | 0 | struct block *b0, *b1, *tmp; |
5970 | | |
5971 | | /* link proto ip6 */ |
5972 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
5973 | |
|
5974 | 0 | switch (ip_proto) { |
5975 | 0 | case IPPROTO_UDP: |
5976 | 0 | case IPPROTO_TCP: |
5977 | 0 | case IPPROTO_SCTP: |
5978 | 0 | b1 = gen_portrangeop6(cstate, port1, port2, ip_proto, dir); |
5979 | 0 | break; |
5980 | | |
5981 | 0 | case PROTO_UNDEF: |
5982 | 0 | tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir); |
5983 | 0 | b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir); |
5984 | 0 | gen_or(tmp, b1); |
5985 | 0 | tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir); |
5986 | 0 | gen_or(tmp, b1); |
5987 | 0 | break; |
5988 | | |
5989 | 0 | default: |
5990 | 0 | abort(); |
5991 | 0 | } |
5992 | 0 | gen_and(b0, b1); |
5993 | 0 | return b1; |
5994 | 0 | } |
5995 | | |
5996 | | static int |
5997 | | lookup_proto(compiler_state_t *cstate, const char *name, int proto) |
5998 | 0 | { |
5999 | 0 | register int v; |
6000 | |
|
6001 | 0 | switch (proto) { |
6002 | | |
6003 | 0 | case Q_DEFAULT: |
6004 | 0 | case Q_IP: |
6005 | 0 | case Q_IPV6: |
6006 | 0 | v = pcap_nametoproto(name); |
6007 | 0 | if (v == PROTO_UNDEF) |
6008 | 0 | bpf_error(cstate, "unknown ip proto '%s'", name); |
6009 | 0 | break; |
6010 | | |
6011 | 0 | case Q_LINK: |
6012 | | /* XXX should look up h/w protocol type based on cstate->linktype */ |
6013 | 0 | v = pcap_nametoeproto(name); |
6014 | 0 | if (v == PROTO_UNDEF) { |
6015 | 0 | v = pcap_nametollc(name); |
6016 | 0 | if (v == PROTO_UNDEF) |
6017 | 0 | bpf_error(cstate, "unknown ether proto '%s'", name); |
6018 | 0 | } |
6019 | 0 | break; |
6020 | | |
6021 | 0 | case Q_ISO: |
6022 | 0 | if (strcmp(name, "esis") == 0) |
6023 | 0 | v = ISO9542_ESIS; |
6024 | 0 | else if (strcmp(name, "isis") == 0) |
6025 | 0 | v = ISO10589_ISIS; |
6026 | 0 | else if (strcmp(name, "clnp") == 0) |
6027 | 0 | v = ISO8473_CLNP; |
6028 | 0 | else |
6029 | 0 | bpf_error(cstate, "unknown osi proto '%s'", name); |
6030 | 0 | break; |
6031 | | |
6032 | 0 | default: |
6033 | 0 | v = PROTO_UNDEF; |
6034 | 0 | break; |
6035 | 0 | } |
6036 | 0 | return v; |
6037 | 0 | } |
6038 | | |
6039 | | #if 0 |
6040 | | struct stmt * |
6041 | | gen_joinsp(struct stmt **s, int n) |
6042 | | { |
6043 | | return NULL; |
6044 | | } |
6045 | | #endif |
6046 | | |
6047 | | static struct block * |
6048 | | gen_protochain(compiler_state_t *cstate, int v, int proto, int dir) |
6049 | 0 | { |
6050 | | #ifdef NO_PROTOCHAIN |
6051 | | return gen_proto(cstate, v, proto, dir); |
6052 | | #else |
6053 | 0 | struct block *b0, *b; |
6054 | 0 | struct slist *s[100]; |
6055 | 0 | int fix2, fix3, fix4, fix5; |
6056 | 0 | int ahcheck, again, end; |
6057 | 0 | int i, max; |
6058 | 0 | int reg2 = alloc_reg(cstate); |
6059 | |
|
6060 | 0 | memset(s, 0, sizeof(s)); |
6061 | 0 | fix3 = fix4 = fix5 = 0; |
6062 | |
|
6063 | 0 | switch (proto) { |
6064 | 0 | case Q_IP: |
6065 | 0 | case Q_IPV6: |
6066 | 0 | break; |
6067 | 0 | case Q_DEFAULT: |
6068 | 0 | b0 = gen_protochain(cstate, v, Q_IP, dir); |
6069 | 0 | b = gen_protochain(cstate, v, Q_IPV6, dir); |
6070 | 0 | gen_or(b0, b); |
6071 | 0 | return b; |
6072 | 0 | default: |
6073 | 0 | bpf_error(cstate, "bad protocol applied for 'protochain'"); |
6074 | | /*NOTREACHED*/ |
6075 | 0 | } |
6076 | | |
6077 | | /* |
6078 | | * We don't handle variable-length prefixes before the link-layer |
6079 | | * header, or variable-length link-layer headers, here yet. |
6080 | | * We might want to add BPF instructions to do the protochain |
6081 | | * work, to simplify that and, on platforms that have a BPF |
6082 | | * interpreter with the new instructions, let the filtering |
6083 | | * be done in the kernel. (We already require a modified BPF |
6084 | | * engine to do the protochain stuff, to support backward |
6085 | | * branches, and backward branch support is unlikely to appear |
6086 | | * in kernel BPF engines.) |
6087 | | */ |
6088 | 0 | if (cstate->off_linkpl.is_variable) |
6089 | 0 | bpf_error(cstate, "'protochain' not supported with variable length headers"); |
6090 | | |
6091 | 0 | cstate->no_optimize = 1; /* this code is not compatible with optimizer yet */ |
6092 | | |
6093 | | /* |
6094 | | * s[0] is a dummy entry to protect other BPF insn from damage |
6095 | | * by s[fix] = foo with uninitialized variable "fix". It is somewhat |
6096 | | * hard to find interdependency made by jump table fixup. |
6097 | | */ |
6098 | 0 | i = 0; |
6099 | 0 | s[i] = new_stmt(cstate, 0); /*dummy*/ |
6100 | 0 | i++; |
6101 | |
|
6102 | 0 | switch (proto) { |
6103 | 0 | case Q_IP: |
6104 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6105 | | |
6106 | | /* A = ip->ip_p */ |
6107 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
6108 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9; |
6109 | 0 | i++; |
6110 | | /* X = ip->ip_hl << 2 */ |
6111 | 0 | s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); |
6112 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6113 | 0 | i++; |
6114 | 0 | break; |
6115 | | |
6116 | 0 | case Q_IPV6: |
6117 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6118 | | |
6119 | | /* A = ip6->ip_nxt */ |
6120 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
6121 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6; |
6122 | 0 | i++; |
6123 | | /* X = sizeof(struct ip6_hdr) */ |
6124 | 0 | s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM); |
6125 | 0 | s[i]->s.k = 40; |
6126 | 0 | i++; |
6127 | 0 | break; |
6128 | | |
6129 | 0 | default: |
6130 | 0 | bpf_error(cstate, "unsupported proto to gen_protochain"); |
6131 | | /*NOTREACHED*/ |
6132 | 0 | } |
6133 | | |
6134 | | /* again: if (A == v) goto end; else fall through; */ |
6135 | 0 | again = i; |
6136 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6137 | 0 | s[i]->s.k = v; |
6138 | 0 | s[i]->s.jt = NULL; /*later*/ |
6139 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6140 | 0 | fix5 = i; |
6141 | 0 | i++; |
6142 | |
|
6143 | | #ifndef IPPROTO_NONE |
6144 | | #define IPPROTO_NONE 59 |
6145 | | #endif |
6146 | | /* if (A == IPPROTO_NONE) goto end */ |
6147 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6148 | 0 | s[i]->s.jt = NULL; /*later*/ |
6149 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6150 | 0 | s[i]->s.k = IPPROTO_NONE; |
6151 | 0 | s[fix5]->s.jf = s[i]; |
6152 | 0 | fix2 = i; |
6153 | 0 | i++; |
6154 | |
|
6155 | 0 | if (proto == Q_IPV6) { |
6156 | 0 | int v6start, v6end, v6advance, j; |
6157 | |
|
6158 | 0 | v6start = i; |
6159 | | /* if (A == IPPROTO_HOPOPTS) goto v6advance */ |
6160 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6161 | 0 | s[i]->s.jt = NULL; /*later*/ |
6162 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6163 | 0 | s[i]->s.k = IPPROTO_HOPOPTS; |
6164 | 0 | s[fix2]->s.jf = s[i]; |
6165 | 0 | i++; |
6166 | | /* if (A == IPPROTO_DSTOPTS) goto v6advance */ |
6167 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6168 | 0 | s[i]->s.jt = NULL; /*later*/ |
6169 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6170 | 0 | s[i]->s.k = IPPROTO_DSTOPTS; |
6171 | 0 | i++; |
6172 | | /* if (A == IPPROTO_ROUTING) goto v6advance */ |
6173 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6174 | 0 | s[i]->s.jt = NULL; /*later*/ |
6175 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6176 | 0 | s[i]->s.k = IPPROTO_ROUTING; |
6177 | 0 | i++; |
6178 | | /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ |
6179 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6180 | 0 | s[i]->s.jt = NULL; /*later*/ |
6181 | 0 | s[i]->s.jf = NULL; /*later*/ |
6182 | 0 | s[i]->s.k = IPPROTO_FRAGMENT; |
6183 | 0 | fix3 = i; |
6184 | 0 | v6end = i; |
6185 | 0 | i++; |
6186 | | |
6187 | | /* v6advance: */ |
6188 | 0 | v6advance = i; |
6189 | | |
6190 | | /* |
6191 | | * in short, |
6192 | | * A = P[X + packet head]; |
6193 | | * X = X + (P[X + packet head + 1] + 1) * 8; |
6194 | | */ |
6195 | | /* A = P[X + packet head] */ |
6196 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6197 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6198 | 0 | i++; |
6199 | | /* MEM[reg2] = A */ |
6200 | 0 | s[i] = new_stmt(cstate, BPF_ST); |
6201 | 0 | s[i]->s.k = reg2; |
6202 | 0 | i++; |
6203 | | /* A = P[X + packet head + 1]; */ |
6204 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6205 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1; |
6206 | 0 | i++; |
6207 | | /* A += 1 */ |
6208 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6209 | 0 | s[i]->s.k = 1; |
6210 | 0 | i++; |
6211 | | /* A *= 8 */ |
6212 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6213 | 0 | s[i]->s.k = 8; |
6214 | 0 | i++; |
6215 | | /* A += X */ |
6216 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
6217 | 0 | s[i]->s.k = 0; |
6218 | 0 | i++; |
6219 | | /* X = A; */ |
6220 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6221 | 0 | i++; |
6222 | | /* A = MEM[reg2] */ |
6223 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6224 | 0 | s[i]->s.k = reg2; |
6225 | 0 | i++; |
6226 | | |
6227 | | /* goto again; (must use BPF_JA for backward jump) */ |
6228 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); |
6229 | 0 | s[i]->s.k = again - i - 1; |
6230 | 0 | s[i - 1]->s.jf = s[i]; |
6231 | 0 | i++; |
6232 | | |
6233 | | /* fixup */ |
6234 | 0 | for (j = v6start; j <= v6end; j++) |
6235 | 0 | s[j]->s.jt = s[v6advance]; |
6236 | 0 | } else { |
6237 | | /* nop */ |
6238 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6239 | 0 | s[i]->s.k = 0; |
6240 | 0 | s[fix2]->s.jf = s[i]; |
6241 | 0 | i++; |
6242 | 0 | } |
6243 | | |
6244 | | /* ahcheck: */ |
6245 | 0 | ahcheck = i; |
6246 | | /* if (A == IPPROTO_AH) then fall through; else goto end; */ |
6247 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6248 | 0 | s[i]->s.jt = NULL; /*later*/ |
6249 | 0 | s[i]->s.jf = NULL; /*later*/ |
6250 | 0 | s[i]->s.k = IPPROTO_AH; |
6251 | 0 | if (fix3) |
6252 | 0 | s[fix3]->s.jf = s[ahcheck]; |
6253 | 0 | fix4 = i; |
6254 | 0 | i++; |
6255 | | |
6256 | | /* |
6257 | | * in short, |
6258 | | * A = P[X]; |
6259 | | * X = X + (P[X + 1] + 2) * 4; |
6260 | | */ |
6261 | | /* A = X */ |
6262 | 0 | s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); |
6263 | 0 | i++; |
6264 | | /* A = P[X + packet head]; */ |
6265 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6266 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6267 | 0 | i++; |
6268 | | /* MEM[reg2] = A */ |
6269 | 0 | s[i] = new_stmt(cstate, BPF_ST); |
6270 | 0 | s[i]->s.k = reg2; |
6271 | 0 | i++; |
6272 | | /* A = X */ |
6273 | 0 | s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); |
6274 | 0 | i++; |
6275 | | /* A += 1 */ |
6276 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6277 | 0 | s[i]->s.k = 1; |
6278 | 0 | i++; |
6279 | | /* X = A */ |
6280 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6281 | 0 | i++; |
6282 | | /* A = P[X + packet head] */ |
6283 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6284 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6285 | 0 | i++; |
6286 | | /* A += 2 */ |
6287 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6288 | 0 | s[i]->s.k = 2; |
6289 | 0 | i++; |
6290 | | /* A *= 4 */ |
6291 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6292 | 0 | s[i]->s.k = 4; |
6293 | 0 | i++; |
6294 | | /* X = A; */ |
6295 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6296 | 0 | i++; |
6297 | | /* A = MEM[reg2] */ |
6298 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6299 | 0 | s[i]->s.k = reg2; |
6300 | 0 | i++; |
6301 | | |
6302 | | /* goto again; (must use BPF_JA for backward jump) */ |
6303 | 0 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); |
6304 | 0 | s[i]->s.k = again - i - 1; |
6305 | 0 | i++; |
6306 | | |
6307 | | /* end: nop */ |
6308 | 0 | end = i; |
6309 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6310 | 0 | s[i]->s.k = 0; |
6311 | 0 | s[fix2]->s.jt = s[end]; |
6312 | 0 | s[fix4]->s.jf = s[end]; |
6313 | 0 | s[fix5]->s.jt = s[end]; |
6314 | 0 | i++; |
6315 | | |
6316 | | /* |
6317 | | * make slist chain |
6318 | | */ |
6319 | 0 | max = i; |
6320 | 0 | for (i = 0; i < max - 1; i++) |
6321 | 0 | s[i]->next = s[i + 1]; |
6322 | 0 | s[max - 1]->next = NULL; |
6323 | | |
6324 | | /* |
6325 | | * emit final check |
6326 | | */ |
6327 | 0 | b = new_block(cstate, JMP(BPF_JEQ)); |
6328 | 0 | b->stmts = s[1]; /*remember, s[0] is dummy*/ |
6329 | 0 | b->s.k = v; |
6330 | |
|
6331 | 0 | free_reg(cstate, reg2); |
6332 | |
|
6333 | 0 | gen_and(b0, b); |
6334 | 0 | return b; |
6335 | 0 | #endif |
6336 | 0 | } |
6337 | | |
6338 | | static struct block * |
6339 | | gen_check_802_11_data_frame(compiler_state_t *cstate) |
6340 | 0 | { |
6341 | 0 | struct slist *s; |
6342 | 0 | struct block *b0, *b1; |
6343 | | |
6344 | | /* |
6345 | | * A data frame has the 0x08 bit (b3) in the frame control field set |
6346 | | * and the 0x04 bit (b2) clear. |
6347 | | */ |
6348 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
6349 | 0 | b0 = new_block(cstate, JMP(BPF_JSET)); |
6350 | 0 | b0->s.k = 0x08; |
6351 | 0 | b0->stmts = s; |
6352 | |
|
6353 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
6354 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
6355 | 0 | b1->s.k = 0x04; |
6356 | 0 | b1->stmts = s; |
6357 | 0 | gen_not(b1); |
6358 | |
|
6359 | 0 | gen_and(b1, b0); |
6360 | |
|
6361 | 0 | return b0; |
6362 | 0 | } |
6363 | | |
6364 | | /* |
6365 | | * Generate code that checks whether the packet is a packet for protocol |
6366 | | * <proto> and whether the type field in that protocol's header has |
6367 | | * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an |
6368 | | * IP packet and checks the protocol number in the IP header against <v>. |
6369 | | * |
6370 | | * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks |
6371 | | * against Q_IP and Q_IPV6. |
6372 | | */ |
6373 | | static struct block * |
6374 | | gen_proto(compiler_state_t *cstate, int v, int proto, int dir) |
6375 | 0 | { |
6376 | 0 | struct block *b0, *b1; |
6377 | 0 | #ifndef CHASE_CHAIN |
6378 | 0 | struct block *b2; |
6379 | 0 | #endif |
6380 | |
|
6381 | 0 | if (dir != Q_DEFAULT) |
6382 | 0 | bpf_error(cstate, "direction applied to 'proto'"); |
6383 | | |
6384 | 0 | switch (proto) { |
6385 | 0 | case Q_DEFAULT: |
6386 | 0 | b0 = gen_proto(cstate, v, Q_IP, dir); |
6387 | 0 | b1 = gen_proto(cstate, v, Q_IPV6, dir); |
6388 | 0 | gen_or(b0, b1); |
6389 | 0 | return b1; |
6390 | | |
6391 | 0 | case Q_LINK: |
6392 | 0 | return gen_linktype(cstate, v); |
6393 | | |
6394 | 0 | case Q_IP: |
6395 | | /* |
6396 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
6397 | | * not LLC encapsulation with LLCSAP_IP. |
6398 | | * |
6399 | | * For IEEE 802 networks - which includes 802.5 token ring |
6400 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
6401 | | * says that SNAP encapsulation is used, not LLC encapsulation |
6402 | | * with LLCSAP_IP. |
6403 | | * |
6404 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
6405 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
6406 | | * encapsulation with LLCSAP_IP. |
6407 | | * |
6408 | | * So we always check for ETHERTYPE_IP. |
6409 | | */ |
6410 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6411 | 0 | #ifndef CHASE_CHAIN |
6412 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, (bpf_int32)v); |
6413 | | #else |
6414 | | b1 = gen_protochain(cstate, v, Q_IP); |
6415 | | #endif |
6416 | 0 | gen_and(b0, b1); |
6417 | 0 | return b1; |
6418 | | |
6419 | 0 | case Q_ARP: |
6420 | 0 | bpf_error(cstate, "arp does not encapsulate another protocol"); |
6421 | | /*NOTREACHED*/ |
6422 | | |
6423 | 0 | case Q_RARP: |
6424 | 0 | bpf_error(cstate, "rarp does not encapsulate another protocol"); |
6425 | | /*NOTREACHED*/ |
6426 | | |
6427 | 0 | case Q_SCTP: |
6428 | 0 | bpf_error(cstate, "'sctp proto' is bogus"); |
6429 | | /*NOTREACHED*/ |
6430 | | |
6431 | 0 | case Q_TCP: |
6432 | 0 | bpf_error(cstate, "'tcp proto' is bogus"); |
6433 | | /*NOTREACHED*/ |
6434 | | |
6435 | 0 | case Q_UDP: |
6436 | 0 | bpf_error(cstate, "'udp proto' is bogus"); |
6437 | | /*NOTREACHED*/ |
6438 | | |
6439 | 0 | case Q_ICMP: |
6440 | 0 | bpf_error(cstate, "'icmp proto' is bogus"); |
6441 | | /*NOTREACHED*/ |
6442 | | |
6443 | 0 | case Q_IGMP: |
6444 | 0 | bpf_error(cstate, "'igmp proto' is bogus"); |
6445 | | /*NOTREACHED*/ |
6446 | | |
6447 | 0 | case Q_IGRP: |
6448 | 0 | bpf_error(cstate, "'igrp proto' is bogus"); |
6449 | | /*NOTREACHED*/ |
6450 | | |
6451 | 0 | case Q_ATALK: |
6452 | 0 | bpf_error(cstate, "AppleTalk encapsulation is not specifiable"); |
6453 | | /*NOTREACHED*/ |
6454 | | |
6455 | 0 | case Q_DECNET: |
6456 | 0 | bpf_error(cstate, "DECNET encapsulation is not specifiable"); |
6457 | | /*NOTREACHED*/ |
6458 | | |
6459 | 0 | case Q_LAT: |
6460 | 0 | bpf_error(cstate, "LAT does not encapsulate another protocol"); |
6461 | | /*NOTREACHED*/ |
6462 | | |
6463 | 0 | case Q_SCA: |
6464 | 0 | bpf_error(cstate, "SCA does not encapsulate another protocol"); |
6465 | | /*NOTREACHED*/ |
6466 | | |
6467 | 0 | case Q_MOPRC: |
6468 | 0 | bpf_error(cstate, "MOPRC does not encapsulate another protocol"); |
6469 | | /*NOTREACHED*/ |
6470 | | |
6471 | 0 | case Q_MOPDL: |
6472 | 0 | bpf_error(cstate, "MOPDL does not encapsulate another protocol"); |
6473 | | /*NOTREACHED*/ |
6474 | | |
6475 | 0 | case Q_IPV6: |
6476 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6477 | 0 | #ifndef CHASE_CHAIN |
6478 | | /* |
6479 | | * Also check for a fragment header before the final |
6480 | | * header. |
6481 | | */ |
6482 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT); |
6483 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, (bpf_int32)v); |
6484 | 0 | gen_and(b2, b1); |
6485 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, (bpf_int32)v); |
6486 | 0 | gen_or(b2, b1); |
6487 | | #else |
6488 | | b1 = gen_protochain(cstate, v, Q_IPV6); |
6489 | | #endif |
6490 | 0 | gen_and(b0, b1); |
6491 | 0 | return b1; |
6492 | | |
6493 | 0 | case Q_ICMPV6: |
6494 | 0 | bpf_error(cstate, "'icmp6 proto' is bogus"); |
6495 | | /*NOTREACHED*/ |
6496 | | |
6497 | 0 | case Q_AH: |
6498 | 0 | bpf_error(cstate, "'ah proto' is bogus"); |
6499 | | /*NOTREACHED*/ |
6500 | | |
6501 | 0 | case Q_ESP: |
6502 | 0 | bpf_error(cstate, "'ah proto' is bogus"); |
6503 | | /*NOTREACHED*/ |
6504 | | |
6505 | 0 | case Q_PIM: |
6506 | 0 | bpf_error(cstate, "'pim proto' is bogus"); |
6507 | | /*NOTREACHED*/ |
6508 | | |
6509 | 0 | case Q_VRRP: |
6510 | 0 | bpf_error(cstate, "'vrrp proto' is bogus"); |
6511 | | /*NOTREACHED*/ |
6512 | | |
6513 | 0 | case Q_AARP: |
6514 | 0 | bpf_error(cstate, "'aarp proto' is bogus"); |
6515 | | /*NOTREACHED*/ |
6516 | | |
6517 | 0 | case Q_ISO: |
6518 | 0 | switch (cstate->linktype) { |
6519 | | |
6520 | 0 | case DLT_FRELAY: |
6521 | | /* |
6522 | | * Frame Relay packets typically have an OSI |
6523 | | * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)" |
6524 | | * generates code to check for all the OSI |
6525 | | * NLPIDs, so calling it and then adding a check |
6526 | | * for the particular NLPID for which we're |
6527 | | * looking is bogus, as we can just check for |
6528 | | * the NLPID. |
6529 | | * |
6530 | | * What we check for is the NLPID and a frame |
6531 | | * control field value of UI, i.e. 0x03 followed |
6532 | | * by the NLPID. |
6533 | | * |
6534 | | * XXX - assumes a 2-byte Frame Relay header with |
6535 | | * DLCI and flags. What if the address is longer? |
6536 | | * |
6537 | | * XXX - what about SNAP-encapsulated frames? |
6538 | | */ |
6539 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v); |
6540 | | /*NOTREACHED*/ |
6541 | | |
6542 | 0 | case DLT_C_HDLC: |
6543 | | /* |
6544 | | * Cisco uses an Ethertype lookalike - for OSI, |
6545 | | * it's 0xfefe. |
6546 | | */ |
6547 | 0 | b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS); |
6548 | | /* OSI in C-HDLC is stuffed with a fudge byte */ |
6549 | 0 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (long)v); |
6550 | 0 | gen_and(b0, b1); |
6551 | 0 | return b1; |
6552 | | |
6553 | 0 | default: |
6554 | 0 | b0 = gen_linktype(cstate, LLCSAP_ISONS); |
6555 | 0 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (long)v); |
6556 | 0 | gen_and(b0, b1); |
6557 | 0 | return b1; |
6558 | 0 | } |
6559 | | |
6560 | 0 | case Q_ESIS: |
6561 | 0 | bpf_error(cstate, "'esis proto' is bogus"); |
6562 | | /*NOTREACHED*/ |
6563 | | |
6564 | 0 | case Q_ISIS: |
6565 | 0 | b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT); |
6566 | | /* |
6567 | | * 4 is the offset of the PDU type relative to the IS-IS |
6568 | | * header. |
6569 | | */ |
6570 | 0 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (long)v); |
6571 | 0 | gen_and(b0, b1); |
6572 | 0 | return b1; |
6573 | | |
6574 | 0 | case Q_CLNP: |
6575 | 0 | bpf_error(cstate, "'clnp proto' is not supported"); |
6576 | | /*NOTREACHED*/ |
6577 | | |
6578 | 0 | case Q_STP: |
6579 | 0 | bpf_error(cstate, "'stp proto' is bogus"); |
6580 | | /*NOTREACHED*/ |
6581 | | |
6582 | 0 | case Q_IPX: |
6583 | 0 | bpf_error(cstate, "'ipx proto' is bogus"); |
6584 | | /*NOTREACHED*/ |
6585 | | |
6586 | 0 | case Q_NETBEUI: |
6587 | 0 | bpf_error(cstate, "'netbeui proto' is bogus"); |
6588 | | /*NOTREACHED*/ |
6589 | | |
6590 | 0 | case Q_ISIS_L1: |
6591 | 0 | bpf_error(cstate, "'l1 proto' is bogus"); |
6592 | | /*NOTREACHED*/ |
6593 | | |
6594 | 0 | case Q_ISIS_L2: |
6595 | 0 | bpf_error(cstate, "'l2 proto' is bogus"); |
6596 | | /*NOTREACHED*/ |
6597 | | |
6598 | 0 | case Q_ISIS_IIH: |
6599 | 0 | bpf_error(cstate, "'iih proto' is bogus"); |
6600 | | /*NOTREACHED*/ |
6601 | | |
6602 | 0 | case Q_ISIS_SNP: |
6603 | 0 | bpf_error(cstate, "'snp proto' is bogus"); |
6604 | | /*NOTREACHED*/ |
6605 | | |
6606 | 0 | case Q_ISIS_CSNP: |
6607 | 0 | bpf_error(cstate, "'csnp proto' is bogus"); |
6608 | | /*NOTREACHED*/ |
6609 | | |
6610 | 0 | case Q_ISIS_PSNP: |
6611 | 0 | bpf_error(cstate, "'psnp proto' is bogus"); |
6612 | | /*NOTREACHED*/ |
6613 | | |
6614 | 0 | case Q_ISIS_LSP: |
6615 | 0 | bpf_error(cstate, "'lsp proto' is bogus"); |
6616 | | /*NOTREACHED*/ |
6617 | | |
6618 | 0 | case Q_RADIO: |
6619 | 0 | bpf_error(cstate, "'radio proto' is bogus"); |
6620 | | /*NOTREACHED*/ |
6621 | | |
6622 | 0 | case Q_CARP: |
6623 | 0 | bpf_error(cstate, "'carp proto' is bogus"); |
6624 | | /*NOTREACHED*/ |
6625 | | |
6626 | 0 | default: |
6627 | 0 | abort(); |
6628 | | /*NOTREACHED*/ |
6629 | 0 | } |
6630 | | /*NOTREACHED*/ |
6631 | 0 | } |
6632 | | |
6633 | | struct block * |
6634 | | gen_scode(compiler_state_t *cstate, const char *name, struct qual q) |
6635 | 0 | { |
6636 | 0 | int proto = q.proto; |
6637 | 0 | int dir = q.dir; |
6638 | 0 | int tproto; |
6639 | 0 | u_char *eaddr; |
6640 | 0 | bpf_u_int32 mask, addr; |
6641 | 0 | struct addrinfo *res, *res0; |
6642 | 0 | struct sockaddr_in *sin4; |
6643 | 0 | #ifdef INET6 |
6644 | 0 | int tproto6; |
6645 | 0 | struct sockaddr_in6 *sin6; |
6646 | 0 | struct in6_addr mask128; |
6647 | 0 | #endif /*INET6*/ |
6648 | 0 | struct block *b, *tmp; |
6649 | 0 | int port, real_proto; |
6650 | 0 | int port1, port2; |
6651 | | |
6652 | | /* |
6653 | | * Catch errors reported by us and routines below us, and return NULL |
6654 | | * on an error. |
6655 | | */ |
6656 | 0 | if (setjmp(cstate->top_ctx)) |
6657 | 0 | return (NULL); |
6658 | | |
6659 | 0 | switch (q.addr) { |
6660 | | |
6661 | 0 | case Q_NET: |
6662 | 0 | addr = pcap_nametonetaddr(name); |
6663 | 0 | if (addr == 0) |
6664 | 0 | bpf_error(cstate, "unknown network '%s'", name); |
6665 | | /* Left justify network addr and calculate its network mask */ |
6666 | 0 | mask = 0xffffffff; |
6667 | 0 | while (addr && (addr & 0xff000000) == 0) { |
6668 | 0 | addr <<= 8; |
6669 | 0 | mask <<= 8; |
6670 | 0 | } |
6671 | 0 | return gen_host(cstate, addr, mask, proto, dir, q.addr); |
6672 | | |
6673 | 0 | case Q_DEFAULT: |
6674 | 0 | case Q_HOST: |
6675 | 0 | if (proto == Q_LINK) { |
6676 | 0 | switch (cstate->linktype) { |
6677 | | |
6678 | 0 | case DLT_EN10MB: |
6679 | 0 | case DLT_NETANALYZER: |
6680 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
6681 | 0 | eaddr = pcap_ether_hostton(name); |
6682 | 0 | if (eaddr == NULL) |
6683 | 0 | bpf_error(cstate, |
6684 | 0 | "unknown ether host '%s'", name); |
6685 | 0 | tmp = gen_prevlinkhdr_check(cstate); |
6686 | 0 | b = gen_ehostop(cstate, eaddr, dir); |
6687 | 0 | if (tmp != NULL) |
6688 | 0 | gen_and(tmp, b); |
6689 | 0 | free(eaddr); |
6690 | 0 | return b; |
6691 | | |
6692 | 0 | case DLT_FDDI: |
6693 | 0 | eaddr = pcap_ether_hostton(name); |
6694 | 0 | if (eaddr == NULL) |
6695 | 0 | bpf_error(cstate, |
6696 | 0 | "unknown FDDI host '%s'", name); |
6697 | 0 | b = gen_fhostop(cstate, eaddr, dir); |
6698 | 0 | free(eaddr); |
6699 | 0 | return b; |
6700 | | |
6701 | 0 | case DLT_IEEE802: |
6702 | 0 | eaddr = pcap_ether_hostton(name); |
6703 | 0 | if (eaddr == NULL) |
6704 | 0 | bpf_error(cstate, |
6705 | 0 | "unknown token ring host '%s'", name); |
6706 | 0 | b = gen_thostop(cstate, eaddr, dir); |
6707 | 0 | free(eaddr); |
6708 | 0 | return b; |
6709 | | |
6710 | 0 | case DLT_IEEE802_11: |
6711 | 0 | case DLT_PRISM_HEADER: |
6712 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
6713 | 0 | case DLT_IEEE802_11_RADIO: |
6714 | 0 | case DLT_PPI: |
6715 | 0 | eaddr = pcap_ether_hostton(name); |
6716 | 0 | if (eaddr == NULL) |
6717 | 0 | bpf_error(cstate, |
6718 | 0 | "unknown 802.11 host '%s'", name); |
6719 | 0 | b = gen_wlanhostop(cstate, eaddr, dir); |
6720 | 0 | free(eaddr); |
6721 | 0 | return b; |
6722 | | |
6723 | 0 | case DLT_IP_OVER_FC: |
6724 | 0 | eaddr = pcap_ether_hostton(name); |
6725 | 0 | if (eaddr == NULL) |
6726 | 0 | bpf_error(cstate, |
6727 | 0 | "unknown Fibre Channel host '%s'", name); |
6728 | 0 | b = gen_ipfchostop(cstate, eaddr, dir); |
6729 | 0 | free(eaddr); |
6730 | 0 | return b; |
6731 | 0 | } |
6732 | | |
6733 | 0 | bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name"); |
6734 | 0 | } else if (proto == Q_DECNET) { |
6735 | 0 | unsigned short dn_addr; |
6736 | |
|
6737 | 0 | if (!__pcap_nametodnaddr(name, &dn_addr)) { |
6738 | | #ifdef DECNETLIB |
6739 | | bpf_error(cstate, "unknown decnet host name '%s'\n", name); |
6740 | | #else |
6741 | 0 | bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n", |
6742 | 0 | name); |
6743 | 0 | #endif |
6744 | 0 | } |
6745 | | /* |
6746 | | * I don't think DECNET hosts can be multihomed, so |
6747 | | * there is no need to build up a list of addresses |
6748 | | */ |
6749 | 0 | return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr)); |
6750 | 0 | } else { |
6751 | 0 | #ifdef INET6 |
6752 | 0 | memset(&mask128, 0xff, sizeof(mask128)); |
6753 | 0 | #endif |
6754 | 0 | res0 = res = pcap_nametoaddrinfo(name); |
6755 | 0 | if (res == NULL) |
6756 | 0 | bpf_error(cstate, "unknown host '%s'", name); |
6757 | 0 | cstate->ai = res; |
6758 | 0 | b = tmp = NULL; |
6759 | 0 | tproto = proto; |
6760 | 0 | #ifdef INET6 |
6761 | 0 | tproto6 = proto; |
6762 | 0 | #endif |
6763 | 0 | if (cstate->off_linktype.constant_part == OFFSET_NOT_SET && |
6764 | 0 | tproto == Q_DEFAULT) { |
6765 | 0 | tproto = Q_IP; |
6766 | 0 | #ifdef INET6 |
6767 | 0 | tproto6 = Q_IPV6; |
6768 | 0 | #endif |
6769 | 0 | } |
6770 | 0 | for (res = res0; res; res = res->ai_next) { |
6771 | 0 | switch (res->ai_family) { |
6772 | 0 | case AF_INET: |
6773 | 0 | #ifdef INET6 |
6774 | 0 | if (tproto == Q_IPV6) |
6775 | 0 | continue; |
6776 | 0 | #endif |
6777 | | |
6778 | 0 | sin4 = (struct sockaddr_in *) |
6779 | 0 | res->ai_addr; |
6780 | 0 | tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr), |
6781 | 0 | 0xffffffff, tproto, dir, q.addr); |
6782 | 0 | break; |
6783 | 0 | #ifdef INET6 |
6784 | 0 | case AF_INET6: |
6785 | 0 | if (tproto6 == Q_IP) |
6786 | 0 | continue; |
6787 | | |
6788 | 0 | sin6 = (struct sockaddr_in6 *) |
6789 | 0 | res->ai_addr; |
6790 | 0 | tmp = gen_host6(cstate, &sin6->sin6_addr, |
6791 | 0 | &mask128, tproto6, dir, q.addr); |
6792 | 0 | break; |
6793 | 0 | #endif |
6794 | 0 | default: |
6795 | 0 | continue; |
6796 | 0 | } |
6797 | 0 | if (b) |
6798 | 0 | gen_or(b, tmp); |
6799 | 0 | b = tmp; |
6800 | 0 | } |
6801 | 0 | cstate->ai = NULL; |
6802 | 0 | freeaddrinfo(res0); |
6803 | 0 | if (b == NULL) { |
6804 | 0 | bpf_error(cstate, "unknown host '%s'%s", name, |
6805 | 0 | (proto == Q_DEFAULT) |
6806 | 0 | ? "" |
6807 | 0 | : " for specified address family"); |
6808 | 0 | } |
6809 | 0 | return b; |
6810 | 0 | } |
6811 | | |
6812 | 0 | case Q_PORT: |
6813 | 0 | if (proto != Q_DEFAULT && |
6814 | 0 | proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) |
6815 | 0 | bpf_error(cstate, "illegal qualifier of 'port'"); |
6816 | 0 | if (pcap_nametoport(name, &port, &real_proto) == 0) |
6817 | 0 | bpf_error(cstate, "unknown port '%s'", name); |
6818 | 0 | if (proto == Q_UDP) { |
6819 | 0 | if (real_proto == IPPROTO_TCP) |
6820 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6821 | 0 | else if (real_proto == IPPROTO_SCTP) |
6822 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6823 | 0 | else |
6824 | | /* override PROTO_UNDEF */ |
6825 | 0 | real_proto = IPPROTO_UDP; |
6826 | 0 | } |
6827 | 0 | if (proto == Q_TCP) { |
6828 | 0 | if (real_proto == IPPROTO_UDP) |
6829 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6830 | | |
6831 | 0 | else if (real_proto == IPPROTO_SCTP) |
6832 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6833 | 0 | else |
6834 | | /* override PROTO_UNDEF */ |
6835 | 0 | real_proto = IPPROTO_TCP; |
6836 | 0 | } |
6837 | 0 | if (proto == Q_SCTP) { |
6838 | 0 | if (real_proto == IPPROTO_UDP) |
6839 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6840 | | |
6841 | 0 | else if (real_proto == IPPROTO_TCP) |
6842 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6843 | 0 | else |
6844 | | /* override PROTO_UNDEF */ |
6845 | 0 | real_proto = IPPROTO_SCTP; |
6846 | 0 | } |
6847 | 0 | if (port < 0) |
6848 | 0 | bpf_error(cstate, "illegal port number %d < 0", port); |
6849 | 0 | if (port > 65535) |
6850 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port); |
6851 | 0 | b = gen_port(cstate, port, real_proto, dir); |
6852 | 0 | gen_or(gen_port6(cstate, port, real_proto, dir), b); |
6853 | 0 | return b; |
6854 | | |
6855 | 0 | case Q_PORTRANGE: |
6856 | 0 | if (proto != Q_DEFAULT && |
6857 | 0 | proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) |
6858 | 0 | bpf_error(cstate, "illegal qualifier of 'portrange'"); |
6859 | 0 | if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0) |
6860 | 0 | bpf_error(cstate, "unknown port in range '%s'", name); |
6861 | 0 | if (proto == Q_UDP) { |
6862 | 0 | if (real_proto == IPPROTO_TCP) |
6863 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6864 | 0 | else if (real_proto == IPPROTO_SCTP) |
6865 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6866 | 0 | else |
6867 | | /* override PROTO_UNDEF */ |
6868 | 0 | real_proto = IPPROTO_UDP; |
6869 | 0 | } |
6870 | 0 | if (proto == Q_TCP) { |
6871 | 0 | if (real_proto == IPPROTO_UDP) |
6872 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6873 | 0 | else if (real_proto == IPPROTO_SCTP) |
6874 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6875 | 0 | else |
6876 | | /* override PROTO_UNDEF */ |
6877 | 0 | real_proto = IPPROTO_TCP; |
6878 | 0 | } |
6879 | 0 | if (proto == Q_SCTP) { |
6880 | 0 | if (real_proto == IPPROTO_UDP) |
6881 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6882 | 0 | else if (real_proto == IPPROTO_TCP) |
6883 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6884 | 0 | else |
6885 | | /* override PROTO_UNDEF */ |
6886 | 0 | real_proto = IPPROTO_SCTP; |
6887 | 0 | } |
6888 | 0 | if (port1 < 0) |
6889 | 0 | bpf_error(cstate, "illegal port number %d < 0", port1); |
6890 | 0 | if (port1 > 65535) |
6891 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port1); |
6892 | 0 | if (port2 < 0) |
6893 | 0 | bpf_error(cstate, "illegal port number %d < 0", port2); |
6894 | 0 | if (port2 > 65535) |
6895 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port2); |
6896 | | |
6897 | 0 | b = gen_portrange(cstate, port1, port2, real_proto, dir); |
6898 | 0 | gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b); |
6899 | 0 | return b; |
6900 | | |
6901 | 0 | case Q_GATEWAY: |
6902 | | #ifndef INET6 |
6903 | | eaddr = pcap_ether_hostton(name); |
6904 | | if (eaddr == NULL) |
6905 | | bpf_error(cstate, "unknown ether host: %s", name); |
6906 | | |
6907 | | res = pcap_nametoaddrinfo(name); |
6908 | | cstate->ai = res; |
6909 | | if (res == NULL) |
6910 | | bpf_error(cstate, "unknown host '%s'", name); |
6911 | | b = gen_gateway(cstate, eaddr, res, proto, dir); |
6912 | | cstate->ai = NULL; |
6913 | | freeaddrinfo(res); |
6914 | | if (b == NULL) |
6915 | | bpf_error(cstate, "unknown host '%s'", name); |
6916 | | return b; |
6917 | | #else |
6918 | 0 | bpf_error(cstate, "'gateway' not supported in this configuration"); |
6919 | 0 | #endif /*INET6*/ |
6920 | | |
6921 | 0 | case Q_PROTO: |
6922 | 0 | real_proto = lookup_proto(cstate, name, proto); |
6923 | 0 | if (real_proto >= 0) |
6924 | 0 | return gen_proto(cstate, real_proto, proto, dir); |
6925 | 0 | else |
6926 | 0 | bpf_error(cstate, "unknown protocol: %s", name); |
6927 | | |
6928 | 0 | case Q_PROTOCHAIN: |
6929 | 0 | real_proto = lookup_proto(cstate, name, proto); |
6930 | 0 | if (real_proto >= 0) |
6931 | 0 | return gen_protochain(cstate, real_proto, proto, dir); |
6932 | 0 | else |
6933 | 0 | bpf_error(cstate, "unknown protocol: %s", name); |
6934 | | |
6935 | 0 | case Q_UNDEF: |
6936 | 0 | syntax(cstate); |
6937 | | /*NOTREACHED*/ |
6938 | 0 | } |
6939 | 0 | abort(); |
6940 | | /*NOTREACHED*/ |
6941 | 0 | } |
6942 | | |
6943 | | struct block * |
6944 | | gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2, |
6945 | | unsigned int masklen, struct qual q) |
6946 | 0 | { |
6947 | 0 | register int nlen, mlen; |
6948 | 0 | bpf_u_int32 n, m; |
6949 | | |
6950 | | /* |
6951 | | * Catch errors reported by us and routines below us, and return NULL |
6952 | | * on an error. |
6953 | | */ |
6954 | 0 | if (setjmp(cstate->top_ctx)) |
6955 | 0 | return (NULL); |
6956 | | |
6957 | 0 | nlen = __pcap_atoin(s1, &n); |
6958 | | /* Promote short ipaddr */ |
6959 | 0 | n <<= 32 - nlen; |
6960 | |
|
6961 | 0 | if (s2 != NULL) { |
6962 | 0 | mlen = __pcap_atoin(s2, &m); |
6963 | | /* Promote short ipaddr */ |
6964 | 0 | m <<= 32 - mlen; |
6965 | 0 | if ((n & ~m) != 0) |
6966 | 0 | bpf_error(cstate, "non-network bits set in \"%s mask %s\"", |
6967 | 0 | s1, s2); |
6968 | 0 | } else { |
6969 | | /* Convert mask len to mask */ |
6970 | 0 | if (masklen > 32) |
6971 | 0 | bpf_error(cstate, "mask length must be <= 32"); |
6972 | 0 | if (masklen == 0) { |
6973 | | /* |
6974 | | * X << 32 is not guaranteed by C to be 0; it's |
6975 | | * undefined. |
6976 | | */ |
6977 | 0 | m = 0; |
6978 | 0 | } else |
6979 | 0 | m = 0xffffffff << (32 - masklen); |
6980 | 0 | if ((n & ~m) != 0) |
6981 | 0 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", |
6982 | 0 | s1, masklen); |
6983 | 0 | } |
6984 | | |
6985 | 0 | switch (q.addr) { |
6986 | | |
6987 | 0 | case Q_NET: |
6988 | 0 | return gen_host(cstate, n, m, q.proto, q.dir, q.addr); |
6989 | | |
6990 | 0 | default: |
6991 | 0 | bpf_error(cstate, "Mask syntax for networks only"); |
6992 | | /*NOTREACHED*/ |
6993 | 0 | } |
6994 | | /*NOTREACHED*/ |
6995 | 0 | } |
6996 | | |
6997 | | struct block * |
6998 | | gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q) |
6999 | 0 | { |
7000 | 0 | bpf_u_int32 mask; |
7001 | 0 | int proto; |
7002 | 0 | int dir; |
7003 | 0 | register int vlen; |
7004 | | |
7005 | | /* |
7006 | | * Catch errors reported by us and routines below us, and return NULL |
7007 | | * on an error. |
7008 | | */ |
7009 | 0 | if (setjmp(cstate->top_ctx)) |
7010 | 0 | return (NULL); |
7011 | | |
7012 | 0 | proto = q.proto; |
7013 | 0 | dir = q.dir; |
7014 | 0 | if (s == NULL) |
7015 | 0 | vlen = 32; |
7016 | 0 | else if (q.proto == Q_DECNET) { |
7017 | 0 | vlen = __pcap_atodn(s, &v); |
7018 | 0 | if (vlen == 0) |
7019 | 0 | bpf_error(cstate, "malformed decnet address '%s'", s); |
7020 | 0 | } else |
7021 | 0 | vlen = __pcap_atoin(s, &v); |
7022 | | |
7023 | 0 | switch (q.addr) { |
7024 | | |
7025 | 0 | case Q_DEFAULT: |
7026 | 0 | case Q_HOST: |
7027 | 0 | case Q_NET: |
7028 | 0 | if (proto == Q_DECNET) |
7029 | 0 | return gen_host(cstate, v, 0, proto, dir, q.addr); |
7030 | 0 | else if (proto == Q_LINK) { |
7031 | 0 | bpf_error(cstate, "illegal link layer address"); |
7032 | 0 | } else { |
7033 | 0 | mask = 0xffffffff; |
7034 | 0 | if (s == NULL && q.addr == Q_NET) { |
7035 | | /* Promote short net number */ |
7036 | 0 | while (v && (v & 0xff000000) == 0) { |
7037 | 0 | v <<= 8; |
7038 | 0 | mask <<= 8; |
7039 | 0 | } |
7040 | 0 | } else { |
7041 | | /* Promote short ipaddr */ |
7042 | 0 | v <<= 32 - vlen; |
7043 | 0 | mask <<= 32 - vlen ; |
7044 | 0 | } |
7045 | 0 | return gen_host(cstate, v, mask, proto, dir, q.addr); |
7046 | 0 | } |
7047 | | |
7048 | 0 | case Q_PORT: |
7049 | 0 | if (proto == Q_UDP) |
7050 | 0 | proto = IPPROTO_UDP; |
7051 | 0 | else if (proto == Q_TCP) |
7052 | 0 | proto = IPPROTO_TCP; |
7053 | 0 | else if (proto == Q_SCTP) |
7054 | 0 | proto = IPPROTO_SCTP; |
7055 | 0 | else if (proto == Q_DEFAULT) |
7056 | 0 | proto = PROTO_UNDEF; |
7057 | 0 | else |
7058 | 0 | bpf_error(cstate, "illegal qualifier of 'port'"); |
7059 | | |
7060 | 0 | if (v > 65535) |
7061 | 0 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7062 | | |
7063 | 0 | { |
7064 | 0 | struct block *b; |
7065 | 0 | b = gen_port(cstate, (int)v, proto, dir); |
7066 | 0 | gen_or(gen_port6(cstate, (int)v, proto, dir), b); |
7067 | 0 | return b; |
7068 | 0 | } |
7069 | | |
7070 | 0 | case Q_PORTRANGE: |
7071 | 0 | if (proto == Q_UDP) |
7072 | 0 | proto = IPPROTO_UDP; |
7073 | 0 | else if (proto == Q_TCP) |
7074 | 0 | proto = IPPROTO_TCP; |
7075 | 0 | else if (proto == Q_SCTP) |
7076 | 0 | proto = IPPROTO_SCTP; |
7077 | 0 | else if (proto == Q_DEFAULT) |
7078 | 0 | proto = PROTO_UNDEF; |
7079 | 0 | else |
7080 | 0 | bpf_error(cstate, "illegal qualifier of 'portrange'"); |
7081 | | |
7082 | 0 | if (v > 65535) |
7083 | 0 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7084 | | |
7085 | 0 | { |
7086 | 0 | struct block *b; |
7087 | 0 | b = gen_portrange(cstate, (int)v, (int)v, proto, dir); |
7088 | 0 | gen_or(gen_portrange6(cstate, (int)v, (int)v, proto, dir), b); |
7089 | 0 | return b; |
7090 | 0 | } |
7091 | | |
7092 | 0 | case Q_GATEWAY: |
7093 | 0 | bpf_error(cstate, "'gateway' requires a name"); |
7094 | | /*NOTREACHED*/ |
7095 | | |
7096 | 0 | case Q_PROTO: |
7097 | 0 | return gen_proto(cstate, (int)v, proto, dir); |
7098 | | |
7099 | 0 | case Q_PROTOCHAIN: |
7100 | 0 | return gen_protochain(cstate, (int)v, proto, dir); |
7101 | | |
7102 | 0 | case Q_UNDEF: |
7103 | 0 | syntax(cstate); |
7104 | | /*NOTREACHED*/ |
7105 | | |
7106 | 0 | default: |
7107 | 0 | abort(); |
7108 | | /*NOTREACHED*/ |
7109 | 0 | } |
7110 | | /*NOTREACHED*/ |
7111 | 0 | } |
7112 | | |
7113 | | #ifdef INET6 |
7114 | | struct block * |
7115 | | gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2, |
7116 | | unsigned int masklen, struct qual q) |
7117 | 0 | { |
7118 | 0 | struct addrinfo *res; |
7119 | 0 | struct in6_addr *addr; |
7120 | 0 | struct in6_addr mask; |
7121 | 0 | struct block *b; |
7122 | 0 | uint32_t *a, *m; |
7123 | | |
7124 | | /* |
7125 | | * Catch errors reported by us and routines below us, and return NULL |
7126 | | * on an error. |
7127 | | */ |
7128 | 0 | if (setjmp(cstate->top_ctx)) |
7129 | 0 | return (NULL); |
7130 | | |
7131 | 0 | if (s2) |
7132 | 0 | bpf_error(cstate, "no mask %s supported", s2); |
7133 | | |
7134 | 0 | res = pcap_nametoaddrinfo(s1); |
7135 | 0 | if (!res) |
7136 | 0 | bpf_error(cstate, "invalid ip6 address %s", s1); |
7137 | 0 | cstate->ai = res; |
7138 | 0 | if (res->ai_next) |
7139 | 0 | bpf_error(cstate, "%s resolved to multiple address", s1); |
7140 | 0 | addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; |
7141 | |
|
7142 | 0 | if (sizeof(mask) * 8 < masklen) |
7143 | 0 | bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask) * 8)); |
7144 | 0 | memset(&mask, 0, sizeof(mask)); |
7145 | 0 | memset(&mask, 0xff, masklen / 8); |
7146 | 0 | if (masklen % 8) { |
7147 | 0 | mask.s6_addr[masklen / 8] = |
7148 | 0 | (0xff << (8 - masklen % 8)) & 0xff; |
7149 | 0 | } |
7150 | |
|
7151 | 0 | a = (uint32_t *)addr; |
7152 | 0 | m = (uint32_t *)&mask; |
7153 | 0 | if ((a[0] & ~m[0]) || (a[1] & ~m[1]) |
7154 | 0 | || (a[2] & ~m[2]) || (a[3] & ~m[3])) { |
7155 | 0 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen); |
7156 | 0 | } |
7157 | | |
7158 | 0 | switch (q.addr) { |
7159 | | |
7160 | 0 | case Q_DEFAULT: |
7161 | 0 | case Q_HOST: |
7162 | 0 | if (masklen != 128) |
7163 | 0 | bpf_error(cstate, "Mask syntax for networks only"); |
7164 | | /* FALLTHROUGH */ |
7165 | | |
7166 | 0 | case Q_NET: |
7167 | 0 | b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr); |
7168 | 0 | cstate->ai = NULL; |
7169 | 0 | freeaddrinfo(res); |
7170 | 0 | return b; |
7171 | | |
7172 | 0 | default: |
7173 | 0 | bpf_error(cstate, "invalid qualifier against IPv6 address"); |
7174 | | /*NOTREACHED*/ |
7175 | 0 | } |
7176 | 0 | } |
7177 | | #endif /*INET6*/ |
7178 | | |
7179 | | struct block * |
7180 | | gen_ecode(compiler_state_t *cstate, const char *s, struct qual q) |
7181 | 0 | { |
7182 | 0 | struct block *b, *tmp; |
7183 | | |
7184 | | /* |
7185 | | * Catch errors reported by us and routines below us, and return NULL |
7186 | | * on an error. |
7187 | | */ |
7188 | 0 | if (setjmp(cstate->top_ctx)) |
7189 | 0 | return (NULL); |
7190 | | |
7191 | 0 | if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) { |
7192 | 0 | cstate->e = pcap_ether_aton(s); |
7193 | 0 | if (cstate->e == NULL) |
7194 | 0 | bpf_error(cstate, "malloc"); |
7195 | 0 | switch (cstate->linktype) { |
7196 | 0 | case DLT_EN10MB: |
7197 | 0 | case DLT_NETANALYZER: |
7198 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
7199 | 0 | tmp = gen_prevlinkhdr_check(cstate); |
7200 | 0 | b = gen_ehostop(cstate, cstate->e, (int)q.dir); |
7201 | 0 | if (tmp != NULL) |
7202 | 0 | gen_and(tmp, b); |
7203 | 0 | break; |
7204 | 0 | case DLT_FDDI: |
7205 | 0 | b = gen_fhostop(cstate, cstate->e, (int)q.dir); |
7206 | 0 | break; |
7207 | 0 | case DLT_IEEE802: |
7208 | 0 | b = gen_thostop(cstate, cstate->e, (int)q.dir); |
7209 | 0 | break; |
7210 | 0 | case DLT_IEEE802_11: |
7211 | 0 | case DLT_PRISM_HEADER: |
7212 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
7213 | 0 | case DLT_IEEE802_11_RADIO: |
7214 | 0 | case DLT_PPI: |
7215 | 0 | b = gen_wlanhostop(cstate, cstate->e, (int)q.dir); |
7216 | 0 | break; |
7217 | 0 | case DLT_IP_OVER_FC: |
7218 | 0 | b = gen_ipfchostop(cstate, cstate->e, (int)q.dir); |
7219 | 0 | break; |
7220 | 0 | default: |
7221 | 0 | free(cstate->e); |
7222 | 0 | cstate->e = NULL; |
7223 | 0 | bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); |
7224 | | /*NOTREACHED*/ |
7225 | 0 | } |
7226 | 0 | free(cstate->e); |
7227 | 0 | cstate->e = NULL; |
7228 | 0 | return (b); |
7229 | 0 | } |
7230 | 0 | bpf_error(cstate, "ethernet address used in non-ether expression"); |
7231 | | /*NOTREACHED*/ |
7232 | 0 | } |
7233 | | |
7234 | | void |
7235 | | sappend(struct slist *s0, struct slist *s1) |
7236 | 0 | { |
7237 | | /* |
7238 | | * This is definitely not the best way to do this, but the |
7239 | | * lists will rarely get long. |
7240 | | */ |
7241 | 0 | while (s0->next) |
7242 | 0 | s0 = s0->next; |
7243 | 0 | s0->next = s1; |
7244 | 0 | } |
7245 | | |
7246 | | static struct slist * |
7247 | | xfer_to_x(compiler_state_t *cstate, struct arth *a) |
7248 | 0 | { |
7249 | 0 | struct slist *s; |
7250 | |
|
7251 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_MEM); |
7252 | 0 | s->s.k = a->regno; |
7253 | 0 | return s; |
7254 | 0 | } |
7255 | | |
7256 | | static struct slist * |
7257 | | xfer_to_a(compiler_state_t *cstate, struct arth *a) |
7258 | 0 | { |
7259 | 0 | struct slist *s; |
7260 | |
|
7261 | 0 | s = new_stmt(cstate, BPF_LD|BPF_MEM); |
7262 | 0 | s->s.k = a->regno; |
7263 | 0 | return s; |
7264 | 0 | } |
7265 | | |
7266 | | /* |
7267 | | * Modify "index" to use the value stored into its register as an |
7268 | | * offset relative to the beginning of the header for the protocol |
7269 | | * "proto", and allocate a register and put an item "size" bytes long |
7270 | | * (1, 2, or 4) at that offset into that register, making it the register |
7271 | | * for "index". |
7272 | | */ |
7273 | | static struct arth * |
7274 | | gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst, int size) |
7275 | 0 | { |
7276 | 0 | struct slist *s, *tmp; |
7277 | 0 | struct block *b; |
7278 | 0 | int regno = alloc_reg(cstate); |
7279 | |
|
7280 | 0 | free_reg(cstate, inst->regno); |
7281 | 0 | switch (size) { |
7282 | | |
7283 | 0 | default: |
7284 | 0 | bpf_error(cstate, "data size must be 1, 2, or 4"); |
7285 | | |
7286 | 0 | case 1: |
7287 | 0 | size = BPF_B; |
7288 | 0 | break; |
7289 | | |
7290 | 0 | case 2: |
7291 | 0 | size = BPF_H; |
7292 | 0 | break; |
7293 | | |
7294 | 0 | case 4: |
7295 | 0 | size = BPF_W; |
7296 | 0 | break; |
7297 | 0 | } |
7298 | 0 | switch (proto) { |
7299 | 0 | default: |
7300 | 0 | bpf_error(cstate, "unsupported index operation"); |
7301 | | |
7302 | 0 | case Q_RADIO: |
7303 | | /* |
7304 | | * The offset is relative to the beginning of the packet |
7305 | | * data, if we have a radio header. (If we don't, this |
7306 | | * is an error.) |
7307 | | */ |
7308 | 0 | if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS && |
7309 | 0 | cstate->linktype != DLT_IEEE802_11_RADIO && |
7310 | 0 | cstate->linktype != DLT_PRISM_HEADER) |
7311 | 0 | bpf_error(cstate, "radio information not present in capture"); |
7312 | | |
7313 | | /* |
7314 | | * Load into the X register the offset computed into the |
7315 | | * register specified by "index". |
7316 | | */ |
7317 | 0 | s = xfer_to_x(cstate, inst); |
7318 | | |
7319 | | /* |
7320 | | * Load the item at that offset. |
7321 | | */ |
7322 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size); |
7323 | 0 | sappend(s, tmp); |
7324 | 0 | sappend(inst->s, s); |
7325 | 0 | break; |
7326 | | |
7327 | 0 | case Q_LINK: |
7328 | | /* |
7329 | | * The offset is relative to the beginning of |
7330 | | * the link-layer header. |
7331 | | * |
7332 | | * XXX - what about ATM LANE? Should the index be |
7333 | | * relative to the beginning of the AAL5 frame, so |
7334 | | * that 0 refers to the beginning of the LE Control |
7335 | | * field, or relative to the beginning of the LAN |
7336 | | * frame, so that 0 refers, for Ethernet LANE, to |
7337 | | * the beginning of the destination address? |
7338 | | */ |
7339 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr); |
7340 | | |
7341 | | /* |
7342 | | * If "s" is non-null, it has code to arrange that the |
7343 | | * X register contains the length of the prefix preceding |
7344 | | * the link-layer header. Add to it the offset computed |
7345 | | * into the register specified by "index", and move that |
7346 | | * into the X register. Otherwise, just load into the X |
7347 | | * register the offset computed into the register specified |
7348 | | * by "index". |
7349 | | */ |
7350 | 0 | if (s != NULL) { |
7351 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7352 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7353 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7354 | 0 | } else |
7355 | 0 | s = xfer_to_x(cstate, inst); |
7356 | | |
7357 | | /* |
7358 | | * Load the item at the sum of the offset we've put in the |
7359 | | * X register and the offset of the start of the link |
7360 | | * layer header (which is 0 if the radio header is |
7361 | | * variable-length; that header length is what we put |
7362 | | * into the X register and then added to the index). |
7363 | | */ |
7364 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size); |
7365 | 0 | tmp->s.k = cstate->off_linkhdr.constant_part; |
7366 | 0 | sappend(s, tmp); |
7367 | 0 | sappend(inst->s, s); |
7368 | 0 | break; |
7369 | | |
7370 | 0 | case Q_IP: |
7371 | 0 | case Q_ARP: |
7372 | 0 | case Q_RARP: |
7373 | 0 | case Q_ATALK: |
7374 | 0 | case Q_DECNET: |
7375 | 0 | case Q_SCA: |
7376 | 0 | case Q_LAT: |
7377 | 0 | case Q_MOPRC: |
7378 | 0 | case Q_MOPDL: |
7379 | 0 | case Q_IPV6: |
7380 | | /* |
7381 | | * The offset is relative to the beginning of |
7382 | | * the network-layer header. |
7383 | | * XXX - are there any cases where we want |
7384 | | * cstate->off_nl_nosnap? |
7385 | | */ |
7386 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7387 | | |
7388 | | /* |
7389 | | * If "s" is non-null, it has code to arrange that the |
7390 | | * X register contains the variable part of the offset |
7391 | | * of the link-layer payload. Add to it the offset |
7392 | | * computed into the register specified by "index", |
7393 | | * and move that into the X register. Otherwise, just |
7394 | | * load into the X register the offset computed into |
7395 | | * the register specified by "index". |
7396 | | */ |
7397 | 0 | if (s != NULL) { |
7398 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7399 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7400 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7401 | 0 | } else |
7402 | 0 | s = xfer_to_x(cstate, inst); |
7403 | | |
7404 | | /* |
7405 | | * Load the item at the sum of the offset we've put in the |
7406 | | * X register, the offset of the start of the network |
7407 | | * layer header from the beginning of the link-layer |
7408 | | * payload, and the constant part of the offset of the |
7409 | | * start of the link-layer payload. |
7410 | | */ |
7411 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size); |
7412 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7413 | 0 | sappend(s, tmp); |
7414 | 0 | sappend(inst->s, s); |
7415 | | |
7416 | | /* |
7417 | | * Do the computation only if the packet contains |
7418 | | * the protocol in question. |
7419 | | */ |
7420 | 0 | b = gen_proto_abbrev_internal(cstate, proto); |
7421 | 0 | if (inst->b) |
7422 | 0 | gen_and(inst->b, b); |
7423 | 0 | inst->b = b; |
7424 | 0 | break; |
7425 | | |
7426 | 0 | case Q_SCTP: |
7427 | 0 | case Q_TCP: |
7428 | 0 | case Q_UDP: |
7429 | 0 | case Q_ICMP: |
7430 | 0 | case Q_IGMP: |
7431 | 0 | case Q_IGRP: |
7432 | 0 | case Q_PIM: |
7433 | 0 | case Q_VRRP: |
7434 | 0 | case Q_CARP: |
7435 | | /* |
7436 | | * The offset is relative to the beginning of |
7437 | | * the transport-layer header. |
7438 | | * |
7439 | | * Load the X register with the length of the IPv4 header |
7440 | | * (plus the offset of the link-layer header, if it's |
7441 | | * a variable-length header), in bytes. |
7442 | | * |
7443 | | * XXX - are there any cases where we want |
7444 | | * cstate->off_nl_nosnap? |
7445 | | * XXX - we should, if we're built with |
7446 | | * IPv6 support, generate code to load either |
7447 | | * IPv4, IPv6, or both, as appropriate. |
7448 | | */ |
7449 | 0 | s = gen_loadx_iphdrlen(cstate); |
7450 | | |
7451 | | /* |
7452 | | * The X register now contains the sum of the variable |
7453 | | * part of the offset of the link-layer payload and the |
7454 | | * length of the network-layer header. |
7455 | | * |
7456 | | * Load into the A register the offset relative to |
7457 | | * the beginning of the transport layer header, |
7458 | | * add the X register to that, move that to the |
7459 | | * X register, and load with an offset from the |
7460 | | * X register equal to the sum of the constant part of |
7461 | | * the offset of the link-layer payload and the offset, |
7462 | | * relative to the beginning of the link-layer payload, |
7463 | | * of the network-layer header. |
7464 | | */ |
7465 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7466 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7467 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7468 | 0 | sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size)); |
7469 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7470 | 0 | sappend(inst->s, s); |
7471 | | |
7472 | | /* |
7473 | | * Do the computation only if the packet contains |
7474 | | * the protocol in question - which is true only |
7475 | | * if this is an IP datagram and is the first or |
7476 | | * only fragment of that datagram. |
7477 | | */ |
7478 | 0 | gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate)); |
7479 | 0 | if (inst->b) |
7480 | 0 | gen_and(inst->b, b); |
7481 | 0 | gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b); |
7482 | 0 | inst->b = b; |
7483 | 0 | break; |
7484 | 0 | case Q_ICMPV6: |
7485 | | /* |
7486 | | * Do the computation only if the packet contains |
7487 | | * the protocol in question. |
7488 | | */ |
7489 | 0 | b = gen_proto_abbrev_internal(cstate, Q_IPV6); |
7490 | 0 | if (inst->b) { |
7491 | 0 | gen_and(inst->b, b); |
7492 | 0 | } |
7493 | 0 | inst->b = b; |
7494 | | |
7495 | | /* |
7496 | | * Check if we have an icmp6 next header |
7497 | | */ |
7498 | 0 | b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58); |
7499 | 0 | if (inst->b) { |
7500 | 0 | gen_and(inst->b, b); |
7501 | 0 | } |
7502 | 0 | inst->b = b; |
7503 | | |
7504 | |
|
7505 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7506 | | /* |
7507 | | * If "s" is non-null, it has code to arrange that the |
7508 | | * X register contains the variable part of the offset |
7509 | | * of the link-layer payload. Add to it the offset |
7510 | | * computed into the register specified by "index", |
7511 | | * and move that into the X register. Otherwise, just |
7512 | | * load into the X register the offset computed into |
7513 | | * the register specified by "index". |
7514 | | */ |
7515 | 0 | if (s != NULL) { |
7516 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7517 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7518 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7519 | 0 | } else { |
7520 | 0 | s = xfer_to_x(cstate, inst); |
7521 | 0 | } |
7522 | | |
7523 | | /* |
7524 | | * Load the item at the sum of the offset we've put in the |
7525 | | * X register, the offset of the start of the network |
7526 | | * layer header from the beginning of the link-layer |
7527 | | * payload, and the constant part of the offset of the |
7528 | | * start of the link-layer payload. |
7529 | | */ |
7530 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size); |
7531 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40; |
7532 | |
|
7533 | 0 | sappend(s, tmp); |
7534 | 0 | sappend(inst->s, s); |
7535 | |
|
7536 | 0 | break; |
7537 | 0 | } |
7538 | 0 | inst->regno = regno; |
7539 | 0 | s = new_stmt(cstate, BPF_ST); |
7540 | 0 | s->s.k = regno; |
7541 | 0 | sappend(inst->s, s); |
7542 | |
|
7543 | 0 | return inst; |
7544 | 0 | } |
7545 | | |
7546 | | struct arth * |
7547 | | gen_load(compiler_state_t *cstate, int proto, struct arth *inst, int size) |
7548 | 0 | { |
7549 | | /* |
7550 | | * Catch errors reported by us and routines below us, and return NULL |
7551 | | * on an error. |
7552 | | */ |
7553 | 0 | if (setjmp(cstate->top_ctx)) |
7554 | 0 | return (NULL); |
7555 | | |
7556 | 0 | return gen_load_internal(cstate, proto, inst, size); |
7557 | 0 | } |
7558 | | |
7559 | | static struct block * |
7560 | | gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0, |
7561 | | struct arth *a1, int reversed) |
7562 | 0 | { |
7563 | 0 | struct slist *s0, *s1, *s2; |
7564 | 0 | struct block *b, *tmp; |
7565 | |
|
7566 | 0 | s0 = xfer_to_x(cstate, a1); |
7567 | 0 | s1 = xfer_to_a(cstate, a0); |
7568 | 0 | if (code == BPF_JEQ) { |
7569 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X); |
7570 | 0 | b = new_block(cstate, JMP(code)); |
7571 | 0 | sappend(s1, s2); |
7572 | 0 | } |
7573 | 0 | else |
7574 | 0 | b = new_block(cstate, BPF_JMP|code|BPF_X); |
7575 | 0 | if (reversed) |
7576 | 0 | gen_not(b); |
7577 | |
|
7578 | 0 | sappend(s0, s1); |
7579 | 0 | sappend(a1->s, s0); |
7580 | 0 | sappend(a0->s, a1->s); |
7581 | |
|
7582 | 0 | b->stmts = a0->s; |
7583 | |
|
7584 | 0 | free_reg(cstate, a0->regno); |
7585 | 0 | free_reg(cstate, a1->regno); |
7586 | | |
7587 | | /* 'and' together protocol checks */ |
7588 | 0 | if (a0->b) { |
7589 | 0 | if (a1->b) { |
7590 | 0 | gen_and(a0->b, tmp = a1->b); |
7591 | 0 | } |
7592 | 0 | else |
7593 | 0 | tmp = a0->b; |
7594 | 0 | } else |
7595 | 0 | tmp = a1->b; |
7596 | |
|
7597 | 0 | if (tmp) |
7598 | 0 | gen_and(tmp, b); |
7599 | |
|
7600 | 0 | return b; |
7601 | 0 | } |
7602 | | |
7603 | | struct block * |
7604 | | gen_relation(compiler_state_t *cstate, int code, struct arth *a0, |
7605 | | struct arth *a1, int reversed) |
7606 | 0 | { |
7607 | | /* |
7608 | | * Catch errors reported by us and routines below us, and return NULL |
7609 | | * on an error. |
7610 | | */ |
7611 | 0 | if (setjmp(cstate->top_ctx)) |
7612 | 0 | return (NULL); |
7613 | | |
7614 | 0 | return gen_relation_internal(cstate, code, a0, a1, reversed); |
7615 | 0 | } |
7616 | | |
7617 | | struct arth * |
7618 | | gen_loadlen(compiler_state_t *cstate) |
7619 | 0 | { |
7620 | 0 | int regno; |
7621 | 0 | struct arth *a; |
7622 | 0 | struct slist *s; |
7623 | | |
7624 | | /* |
7625 | | * Catch errors reported by us and routines below us, and return NULL |
7626 | | * on an error. |
7627 | | */ |
7628 | 0 | if (setjmp(cstate->top_ctx)) |
7629 | 0 | return (NULL); |
7630 | | |
7631 | 0 | regno = alloc_reg(cstate); |
7632 | 0 | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7633 | 0 | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7634 | 0 | s->next = new_stmt(cstate, BPF_ST); |
7635 | 0 | s->next->s.k = regno; |
7636 | 0 | a->s = s; |
7637 | 0 | a->regno = regno; |
7638 | |
|
7639 | 0 | return a; |
7640 | 0 | } |
7641 | | |
7642 | | static struct arth * |
7643 | | gen_loadi_internal(compiler_state_t *cstate, int val) |
7644 | 0 | { |
7645 | 0 | struct arth *a; |
7646 | 0 | struct slist *s; |
7647 | 0 | int reg; |
7648 | |
|
7649 | 0 | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7650 | |
|
7651 | 0 | reg = alloc_reg(cstate); |
7652 | |
|
7653 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
7654 | 0 | s->s.k = val; |
7655 | 0 | s->next = new_stmt(cstate, BPF_ST); |
7656 | 0 | s->next->s.k = reg; |
7657 | 0 | a->s = s; |
7658 | 0 | a->regno = reg; |
7659 | |
|
7660 | 0 | return a; |
7661 | 0 | } |
7662 | | |
7663 | | struct arth * |
7664 | | gen_loadi(compiler_state_t *cstate, int val) |
7665 | 0 | { |
7666 | | /* |
7667 | | * Catch errors reported by us and routines below us, and return NULL |
7668 | | * on an error. |
7669 | | */ |
7670 | 0 | if (setjmp(cstate->top_ctx)) |
7671 | 0 | return (NULL); |
7672 | | |
7673 | 0 | return gen_loadi_internal(cstate, val); |
7674 | 0 | } |
7675 | | |
7676 | | /* |
7677 | | * The a_arg dance is to avoid annoying whining by compilers that |
7678 | | * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7679 | | * It's not *used* after setjmp returns. |
7680 | | */ |
7681 | | struct arth * |
7682 | | gen_neg(compiler_state_t *cstate, struct arth *a_arg) |
7683 | 0 | { |
7684 | 0 | struct arth *a = a_arg; |
7685 | 0 | struct slist *s; |
7686 | | |
7687 | | /* |
7688 | | * Catch errors reported by us and routines below us, and return NULL |
7689 | | * on an error. |
7690 | | */ |
7691 | 0 | if (setjmp(cstate->top_ctx)) |
7692 | 0 | return (NULL); |
7693 | | |
7694 | 0 | s = xfer_to_a(cstate, a); |
7695 | 0 | sappend(a->s, s); |
7696 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_NEG); |
7697 | 0 | s->s.k = 0; |
7698 | 0 | sappend(a->s, s); |
7699 | 0 | s = new_stmt(cstate, BPF_ST); |
7700 | 0 | s->s.k = a->regno; |
7701 | 0 | sappend(a->s, s); |
7702 | |
|
7703 | 0 | return a; |
7704 | 0 | } |
7705 | | |
7706 | | /* |
7707 | | * The a0_arg dance is to avoid annoying whining by compilers that |
7708 | | * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7709 | | * It's not *used* after setjmp returns. |
7710 | | */ |
7711 | | struct arth * |
7712 | | gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg, |
7713 | | struct arth *a1) |
7714 | 0 | { |
7715 | 0 | struct arth *a0 = a0_arg; |
7716 | 0 | struct slist *s0, *s1, *s2; |
7717 | | |
7718 | | /* |
7719 | | * Catch errors reported by us and routines below us, and return NULL |
7720 | | * on an error. |
7721 | | */ |
7722 | 0 | if (setjmp(cstate->top_ctx)) |
7723 | 0 | return (NULL); |
7724 | | |
7725 | | /* |
7726 | | * Disallow division by, or modulus by, zero; we do this here |
7727 | | * so that it gets done even if the optimizer is disabled. |
7728 | | * |
7729 | | * Also disallow shifts by a value greater than 31; we do this |
7730 | | * here, for the same reason. |
7731 | | */ |
7732 | 0 | if (code == BPF_DIV) { |
7733 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7734 | 0 | bpf_error(cstate, "division by zero"); |
7735 | 0 | } else if (code == BPF_MOD) { |
7736 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7737 | 0 | bpf_error(cstate, "modulus by zero"); |
7738 | 0 | } else if (code == BPF_LSH || code == BPF_RSH) { |
7739 | | /* |
7740 | | * XXX - we need to make up our minds as to what integers |
7741 | | * are signed and what integers are unsigned in BPF programs |
7742 | | * and in our IR. |
7743 | | */ |
7744 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && |
7745 | 0 | (a1->s->s.k < 0 || a1->s->s.k > 31)) |
7746 | 0 | bpf_error(cstate, "shift by more than 31 bits"); |
7747 | 0 | } |
7748 | 0 | s0 = xfer_to_x(cstate, a1); |
7749 | 0 | s1 = xfer_to_a(cstate, a0); |
7750 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_X|code); |
7751 | |
|
7752 | 0 | sappend(s1, s2); |
7753 | 0 | sappend(s0, s1); |
7754 | 0 | sappend(a1->s, s0); |
7755 | 0 | sappend(a0->s, a1->s); |
7756 | |
|
7757 | 0 | free_reg(cstate, a0->regno); |
7758 | 0 | free_reg(cstate, a1->regno); |
7759 | |
|
7760 | 0 | s0 = new_stmt(cstate, BPF_ST); |
7761 | 0 | a0->regno = s0->s.k = alloc_reg(cstate); |
7762 | 0 | sappend(a0->s, s0); |
7763 | |
|
7764 | 0 | return a0; |
7765 | 0 | } |
7766 | | |
7767 | | /* |
7768 | | * Initialize the table of used registers and the current register. |
7769 | | */ |
7770 | | static void |
7771 | | init_regs(compiler_state_t *cstate) |
7772 | 0 | { |
7773 | 0 | cstate->curreg = 0; |
7774 | 0 | memset(cstate->regused, 0, sizeof cstate->regused); |
7775 | 0 | } |
7776 | | |
7777 | | /* |
7778 | | * Return the next free register. |
7779 | | */ |
7780 | | static int |
7781 | | alloc_reg(compiler_state_t *cstate) |
7782 | 0 | { |
7783 | 0 | int n = BPF_MEMWORDS; |
7784 | |
|
7785 | 0 | while (--n >= 0) { |
7786 | 0 | if (cstate->regused[cstate->curreg]) |
7787 | 0 | cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS; |
7788 | 0 | else { |
7789 | 0 | cstate->regused[cstate->curreg] = 1; |
7790 | 0 | return cstate->curreg; |
7791 | 0 | } |
7792 | 0 | } |
7793 | 0 | bpf_error(cstate, "too many registers needed to evaluate expression"); |
7794 | | /*NOTREACHED*/ |
7795 | 0 | } |
7796 | | |
7797 | | /* |
7798 | | * Return a register to the table so it can |
7799 | | * be used later. |
7800 | | */ |
7801 | | static void |
7802 | | free_reg(compiler_state_t *cstate, int n) |
7803 | 0 | { |
7804 | 0 | cstate->regused[n] = 0; |
7805 | 0 | } |
7806 | | |
7807 | | static struct block * |
7808 | | gen_len(compiler_state_t *cstate, int jmp, int n) |
7809 | 0 | { |
7810 | 0 | struct slist *s; |
7811 | 0 | struct block *b; |
7812 | |
|
7813 | 0 | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7814 | 0 | b = new_block(cstate, JMP(jmp)); |
7815 | 0 | b->stmts = s; |
7816 | 0 | b->s.k = n; |
7817 | |
|
7818 | 0 | return b; |
7819 | 0 | } |
7820 | | |
7821 | | struct block * |
7822 | | gen_greater(compiler_state_t *cstate, int n) |
7823 | 0 | { |
7824 | | /* |
7825 | | * Catch errors reported by us and routines below us, and return NULL |
7826 | | * on an error. |
7827 | | */ |
7828 | 0 | if (setjmp(cstate->top_ctx)) |
7829 | 0 | return (NULL); |
7830 | | |
7831 | 0 | return gen_len(cstate, BPF_JGE, n); |
7832 | 0 | } |
7833 | | |
7834 | | /* |
7835 | | * Actually, this is less than or equal. |
7836 | | */ |
7837 | | struct block * |
7838 | | gen_less(compiler_state_t *cstate, int n) |
7839 | 0 | { |
7840 | 0 | struct block *b; |
7841 | | |
7842 | | /* |
7843 | | * Catch errors reported by us and routines below us, and return NULL |
7844 | | * on an error. |
7845 | | */ |
7846 | 0 | if (setjmp(cstate->top_ctx)) |
7847 | 0 | return (NULL); |
7848 | | |
7849 | 0 | b = gen_len(cstate, BPF_JGT, n); |
7850 | 0 | gen_not(b); |
7851 | |
|
7852 | 0 | return b; |
7853 | 0 | } |
7854 | | |
7855 | | /* |
7856 | | * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to |
7857 | | * the beginning of the link-layer header. |
7858 | | * XXX - that means you can't test values in the radiotap header, but |
7859 | | * as that header is difficult if not impossible to parse generally |
7860 | | * without a loop, that might not be a severe problem. A new keyword |
7861 | | * "radio" could be added for that, although what you'd really want |
7862 | | * would be a way of testing particular radio header values, which |
7863 | | * would generate code appropriate to the radio header in question. |
7864 | | */ |
7865 | | struct block * |
7866 | | gen_byteop(compiler_state_t *cstate, int op, int idx, int val) |
7867 | 0 | { |
7868 | 0 | struct block *b; |
7869 | 0 | struct slist *s; |
7870 | | |
7871 | | /* |
7872 | | * Catch errors reported by us and routines below us, and return NULL |
7873 | | * on an error. |
7874 | | */ |
7875 | 0 | if (setjmp(cstate->top_ctx)) |
7876 | 0 | return (NULL); |
7877 | | |
7878 | 0 | switch (op) { |
7879 | 0 | default: |
7880 | 0 | abort(); |
7881 | | |
7882 | 0 | case '=': |
7883 | 0 | return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val); |
7884 | | |
7885 | 0 | case '<': |
7886 | 0 | b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val); |
7887 | 0 | return b; |
7888 | | |
7889 | 0 | case '>': |
7890 | 0 | b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val); |
7891 | 0 | return b; |
7892 | | |
7893 | 0 | case '|': |
7894 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K); |
7895 | 0 | break; |
7896 | | |
7897 | 0 | case '&': |
7898 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
7899 | 0 | break; |
7900 | 0 | } |
7901 | 0 | s->s.k = val; |
7902 | 0 | b = new_block(cstate, JMP(BPF_JEQ)); |
7903 | 0 | b->stmts = s; |
7904 | 0 | gen_not(b); |
7905 | |
|
7906 | 0 | return b; |
7907 | 0 | } |
7908 | | |
7909 | | static const u_char abroadcast[] = { 0x0 }; |
7910 | | |
7911 | | struct block * |
7912 | | gen_broadcast(compiler_state_t *cstate, int proto) |
7913 | 0 | { |
7914 | 0 | bpf_u_int32 hostmask; |
7915 | 0 | struct block *b0, *b1, *b2; |
7916 | 0 | static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
7917 | | |
7918 | | /* |
7919 | | * Catch errors reported by us and routines below us, and return NULL |
7920 | | * on an error. |
7921 | | */ |
7922 | 0 | if (setjmp(cstate->top_ctx)) |
7923 | 0 | return (NULL); |
7924 | | |
7925 | 0 | switch (proto) { |
7926 | | |
7927 | 0 | case Q_DEFAULT: |
7928 | 0 | case Q_LINK: |
7929 | 0 | switch (cstate->linktype) { |
7930 | 0 | case DLT_ARCNET: |
7931 | 0 | case DLT_ARCNET_LINUX: |
7932 | 0 | return gen_ahostop(cstate, abroadcast, Q_DST); |
7933 | 0 | case DLT_EN10MB: |
7934 | 0 | case DLT_NETANALYZER: |
7935 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
7936 | 0 | b1 = gen_prevlinkhdr_check(cstate); |
7937 | 0 | b0 = gen_ehostop(cstate, ebroadcast, Q_DST); |
7938 | 0 | if (b1 != NULL) |
7939 | 0 | gen_and(b1, b0); |
7940 | 0 | return b0; |
7941 | 0 | case DLT_FDDI: |
7942 | 0 | return gen_fhostop(cstate, ebroadcast, Q_DST); |
7943 | 0 | case DLT_IEEE802: |
7944 | 0 | return gen_thostop(cstate, ebroadcast, Q_DST); |
7945 | 0 | case DLT_IEEE802_11: |
7946 | 0 | case DLT_PRISM_HEADER: |
7947 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
7948 | 0 | case DLT_IEEE802_11_RADIO: |
7949 | 0 | case DLT_PPI: |
7950 | 0 | return gen_wlanhostop(cstate, ebroadcast, Q_DST); |
7951 | 0 | case DLT_IP_OVER_FC: |
7952 | 0 | return gen_ipfchostop(cstate, ebroadcast, Q_DST); |
7953 | 0 | default: |
7954 | 0 | bpf_error(cstate, "not a broadcast link"); |
7955 | 0 | } |
7956 | | /*NOTREACHED*/ |
7957 | | |
7958 | 0 | case Q_IP: |
7959 | | /* |
7960 | | * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff) |
7961 | | * as an indication that we don't know the netmask, and fail |
7962 | | * in that case. |
7963 | | */ |
7964 | 0 | if (cstate->netmask == PCAP_NETMASK_UNKNOWN) |
7965 | 0 | bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported"); |
7966 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
7967 | 0 | hostmask = ~cstate->netmask; |
7968 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, (bpf_int32)0, hostmask); |
7969 | 0 | b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, |
7970 | 0 | (bpf_int32)(~0 & hostmask), hostmask); |
7971 | 0 | gen_or(b1, b2); |
7972 | 0 | gen_and(b0, b2); |
7973 | 0 | return b2; |
7974 | 0 | } |
7975 | 0 | bpf_error(cstate, "only link-layer/IP broadcast filters supported"); |
7976 | | /*NOTREACHED*/ |
7977 | 0 | } |
7978 | | |
7979 | | /* |
7980 | | * Generate code to test the low-order bit of a MAC address (that's |
7981 | | * the bottom bit of the *first* byte). |
7982 | | */ |
7983 | | static struct block * |
7984 | | gen_mac_multicast(compiler_state_t *cstate, int offset) |
7985 | 0 | { |
7986 | 0 | register struct block *b0; |
7987 | 0 | register struct slist *s; |
7988 | | |
7989 | | /* link[offset] & 1 != 0 */ |
7990 | 0 | s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B); |
7991 | 0 | b0 = new_block(cstate, JMP(BPF_JSET)); |
7992 | 0 | b0->s.k = 1; |
7993 | 0 | b0->stmts = s; |
7994 | 0 | return b0; |
7995 | 0 | } |
7996 | | |
7997 | | struct block * |
7998 | | gen_multicast(compiler_state_t *cstate, int proto) |
7999 | 0 | { |
8000 | 0 | register struct block *b0, *b1, *b2; |
8001 | 0 | register struct slist *s; |
8002 | | |
8003 | | /* |
8004 | | * Catch errors reported by us and routines below us, and return NULL |
8005 | | * on an error. |
8006 | | */ |
8007 | 0 | if (setjmp(cstate->top_ctx)) |
8008 | 0 | return (NULL); |
8009 | | |
8010 | 0 | switch (proto) { |
8011 | | |
8012 | 0 | case Q_DEFAULT: |
8013 | 0 | case Q_LINK: |
8014 | 0 | switch (cstate->linktype) { |
8015 | 0 | case DLT_ARCNET: |
8016 | 0 | case DLT_ARCNET_LINUX: |
8017 | | /* all ARCnet multicasts use the same address */ |
8018 | 0 | return gen_ahostop(cstate, abroadcast, Q_DST); |
8019 | 0 | case DLT_EN10MB: |
8020 | 0 | case DLT_NETANALYZER: |
8021 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
8022 | 0 | b1 = gen_prevlinkhdr_check(cstate); |
8023 | | /* ether[0] & 1 != 0 */ |
8024 | 0 | b0 = gen_mac_multicast(cstate, 0); |
8025 | 0 | if (b1 != NULL) |
8026 | 0 | gen_and(b1, b0); |
8027 | 0 | return b0; |
8028 | 0 | case DLT_FDDI: |
8029 | | /* |
8030 | | * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX |
8031 | | * |
8032 | | * XXX - was that referring to bit-order issues? |
8033 | | */ |
8034 | | /* fddi[1] & 1 != 0 */ |
8035 | 0 | return gen_mac_multicast(cstate, 1); |
8036 | 0 | case DLT_IEEE802: |
8037 | | /* tr[2] & 1 != 0 */ |
8038 | 0 | return gen_mac_multicast(cstate, 2); |
8039 | 0 | case DLT_IEEE802_11: |
8040 | 0 | case DLT_PRISM_HEADER: |
8041 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8042 | 0 | case DLT_IEEE802_11_RADIO: |
8043 | 0 | case DLT_PPI: |
8044 | | /* |
8045 | | * Oh, yuk. |
8046 | | * |
8047 | | * For control frames, there is no DA. |
8048 | | * |
8049 | | * For management frames, DA is at an |
8050 | | * offset of 4 from the beginning of |
8051 | | * the packet. |
8052 | | * |
8053 | | * For data frames, DA is at an offset |
8054 | | * of 4 from the beginning of the packet |
8055 | | * if To DS is clear and at an offset of |
8056 | | * 16 from the beginning of the packet |
8057 | | * if To DS is set. |
8058 | | */ |
8059 | | |
8060 | | /* |
8061 | | * Generate the tests to be done for data frames. |
8062 | | * |
8063 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
8064 | | */ |
8065 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8066 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
8067 | 0 | b1->s.k = 0x01; /* To DS */ |
8068 | 0 | b1->stmts = s; |
8069 | | |
8070 | | /* |
8071 | | * If To DS is set, the DA is at 16. |
8072 | | */ |
8073 | 0 | b0 = gen_mac_multicast(cstate, 16); |
8074 | 0 | gen_and(b1, b0); |
8075 | | |
8076 | | /* |
8077 | | * Now, check for To DS not set, i.e. check |
8078 | | * "!(link[1] & 0x01)". |
8079 | | */ |
8080 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8081 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
8082 | 0 | b2->s.k = 0x01; /* To DS */ |
8083 | 0 | b2->stmts = s; |
8084 | 0 | gen_not(b2); |
8085 | | |
8086 | | /* |
8087 | | * If To DS is not set, the DA is at 4. |
8088 | | */ |
8089 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8090 | 0 | gen_and(b2, b1); |
8091 | | |
8092 | | /* |
8093 | | * Now OR together the last two checks. That gives |
8094 | | * the complete set of checks for data frames. |
8095 | | */ |
8096 | 0 | gen_or(b1, b0); |
8097 | | |
8098 | | /* |
8099 | | * Now check for a data frame. |
8100 | | * I.e, check "link[0] & 0x08". |
8101 | | */ |
8102 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8103 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
8104 | 0 | b1->s.k = 0x08; |
8105 | 0 | b1->stmts = s; |
8106 | | |
8107 | | /* |
8108 | | * AND that with the checks done for data frames. |
8109 | | */ |
8110 | 0 | gen_and(b1, b0); |
8111 | | |
8112 | | /* |
8113 | | * If the high-order bit of the type value is 0, this |
8114 | | * is a management frame. |
8115 | | * I.e, check "!(link[0] & 0x08)". |
8116 | | */ |
8117 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8118 | 0 | b2 = new_block(cstate, JMP(BPF_JSET)); |
8119 | 0 | b2->s.k = 0x08; |
8120 | 0 | b2->stmts = s; |
8121 | 0 | gen_not(b2); |
8122 | | |
8123 | | /* |
8124 | | * For management frames, the DA is at 4. |
8125 | | */ |
8126 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8127 | 0 | gen_and(b2, b1); |
8128 | | |
8129 | | /* |
8130 | | * OR that with the checks done for data frames. |
8131 | | * That gives the checks done for management and |
8132 | | * data frames. |
8133 | | */ |
8134 | 0 | gen_or(b1, b0); |
8135 | | |
8136 | | /* |
8137 | | * If the low-order bit of the type value is 1, |
8138 | | * this is either a control frame or a frame |
8139 | | * with a reserved type, and thus not a |
8140 | | * frame with an SA. |
8141 | | * |
8142 | | * I.e., check "!(link[0] & 0x04)". |
8143 | | */ |
8144 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8145 | 0 | b1 = new_block(cstate, JMP(BPF_JSET)); |
8146 | 0 | b1->s.k = 0x04; |
8147 | 0 | b1->stmts = s; |
8148 | 0 | gen_not(b1); |
8149 | | |
8150 | | /* |
8151 | | * AND that with the checks for data and management |
8152 | | * frames. |
8153 | | */ |
8154 | 0 | gen_and(b1, b0); |
8155 | 0 | return b0; |
8156 | 0 | case DLT_IP_OVER_FC: |
8157 | 0 | b0 = gen_mac_multicast(cstate, 2); |
8158 | 0 | return b0; |
8159 | 0 | default: |
8160 | 0 | break; |
8161 | 0 | } |
8162 | | /* Link not known to support multicasts */ |
8163 | 0 | break; |
8164 | | |
8165 | 0 | case Q_IP: |
8166 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
8167 | 0 | b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, (bpf_int32)224); |
8168 | 0 | gen_and(b0, b1); |
8169 | 0 | return b1; |
8170 | | |
8171 | 0 | case Q_IPV6: |
8172 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
8173 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, (bpf_int32)255); |
8174 | 0 | gen_and(b0, b1); |
8175 | 0 | return b1; |
8176 | 0 | } |
8177 | 0 | bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel"); |
8178 | | /*NOTREACHED*/ |
8179 | 0 | } |
8180 | | |
8181 | | /* |
8182 | | * Filter on inbound (dir == 0) or outbound (dir == 1) traffic. |
8183 | | * Outbound traffic is sent by this machine, while inbound traffic is |
8184 | | * sent by a remote machine (and may include packets destined for a |
8185 | | * unicast or multicast link-layer address we are not subscribing to). |
8186 | | * These are the same definitions implemented by pcap_setdirection(). |
8187 | | * Capturing only unicast traffic destined for this host is probably |
8188 | | * better accomplished using a higher-layer filter. |
8189 | | */ |
8190 | | struct block * |
8191 | | gen_inbound(compiler_state_t *cstate, int dir) |
8192 | 0 | { |
8193 | 0 | register struct block *b0; |
8194 | | |
8195 | | /* |
8196 | | * Catch errors reported by us and routines below us, and return NULL |
8197 | | * on an error. |
8198 | | */ |
8199 | 0 | if (setjmp(cstate->top_ctx)) |
8200 | 0 | return (NULL); |
8201 | | |
8202 | | /* |
8203 | | * Only some data link types support inbound/outbound qualifiers. |
8204 | | */ |
8205 | 0 | switch (cstate->linktype) { |
8206 | 0 | case DLT_SLIP: |
8207 | 0 | b0 = gen_relation_internal(cstate, BPF_JEQ, |
8208 | 0 | gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1), |
8209 | 0 | gen_loadi_internal(cstate, 0), |
8210 | 0 | dir); |
8211 | 0 | break; |
8212 | | |
8213 | 0 | case DLT_IPNET: |
8214 | 0 | if (dir) { |
8215 | | /* match outgoing packets */ |
8216 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND); |
8217 | 0 | } else { |
8218 | | /* match incoming packets */ |
8219 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND); |
8220 | 0 | } |
8221 | 0 | break; |
8222 | | |
8223 | 0 | case DLT_LINUX_SLL: |
8224 | | /* match outgoing packets */ |
8225 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING); |
8226 | 0 | if (!dir) { |
8227 | | /* to filter on inbound traffic, invert the match */ |
8228 | 0 | gen_not(b0); |
8229 | 0 | } |
8230 | 0 | break; |
8231 | | |
8232 | 0 | case DLT_LINUX_SLL2: |
8233 | | /* match outgoing packets */ |
8234 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING); |
8235 | 0 | if (!dir) { |
8236 | | /* to filter on inbound traffic, invert the match */ |
8237 | 0 | gen_not(b0); |
8238 | 0 | } |
8239 | 0 | break; |
8240 | | |
8241 | | #ifdef HAVE_NET_PFVAR_H |
8242 | | case DLT_PFLOG: |
8243 | | b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B, |
8244 | | (bpf_int32)((dir == 0) ? PF_IN : PF_OUT)); |
8245 | | break; |
8246 | | #endif |
8247 | | |
8248 | 0 | case DLT_PPP_PPPD: |
8249 | 0 | if (dir) { |
8250 | | /* match outgoing packets */ |
8251 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT); |
8252 | 0 | } else { |
8253 | | /* match incoming packets */ |
8254 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN); |
8255 | 0 | } |
8256 | 0 | break; |
8257 | | |
8258 | 0 | case DLT_JUNIPER_MFR: |
8259 | 0 | case DLT_JUNIPER_MLFR: |
8260 | 0 | case DLT_JUNIPER_MLPPP: |
8261 | 0 | case DLT_JUNIPER_ATM1: |
8262 | 0 | case DLT_JUNIPER_ATM2: |
8263 | 0 | case DLT_JUNIPER_PPPOE: |
8264 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
8265 | 0 | case DLT_JUNIPER_GGSN: |
8266 | 0 | case DLT_JUNIPER_ES: |
8267 | 0 | case DLT_JUNIPER_MONITOR: |
8268 | 0 | case DLT_JUNIPER_SERVICES: |
8269 | 0 | case DLT_JUNIPER_ETHER: |
8270 | 0 | case DLT_JUNIPER_PPP: |
8271 | 0 | case DLT_JUNIPER_FRELAY: |
8272 | 0 | case DLT_JUNIPER_CHDLC: |
8273 | 0 | case DLT_JUNIPER_VP: |
8274 | 0 | case DLT_JUNIPER_ST: |
8275 | 0 | case DLT_JUNIPER_ISM: |
8276 | 0 | case DLT_JUNIPER_VS: |
8277 | 0 | case DLT_JUNIPER_SRX_E2E: |
8278 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
8279 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
8280 | | |
8281 | | /* juniper flags (including direction) are stored |
8282 | | * the byte after the 3-byte magic number */ |
8283 | 0 | if (dir) { |
8284 | | /* match outgoing packets */ |
8285 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01); |
8286 | 0 | } else { |
8287 | | /* match incoming packets */ |
8288 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01); |
8289 | 0 | } |
8290 | 0 | break; |
8291 | | |
8292 | 0 | default: |
8293 | | /* |
8294 | | * If we have packet meta-data indicating a direction, |
8295 | | * and that metadata can be checked by BPF code, check |
8296 | | * it. Otherwise, give up, as this link-layer type has |
8297 | | * nothing in the packet data. |
8298 | | * |
8299 | | * Currently, the only platform where a BPF filter can |
8300 | | * check that metadata is Linux with the in-kernel |
8301 | | * BPF interpreter. If other packet capture mechanisms |
8302 | | * and BPF filters also supported this, it would be |
8303 | | * nice. It would be even better if they made that |
8304 | | * metadata available so that we could provide it |
8305 | | * with newer capture APIs, allowing it to be saved |
8306 | | * in pcapng files. |
8307 | | */ |
8308 | 0 | #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) |
8309 | | /* |
8310 | | * This is Linux with PF_PACKET support. |
8311 | | * If this is a *live* capture, we can look at |
8312 | | * special meta-data in the filter expression; |
8313 | | * if it's a savefile, we can't. |
8314 | | */ |
8315 | 0 | if (cstate->bpf_pcap->rfile != NULL) { |
8316 | | /* We have a FILE *, so this is a savefile */ |
8317 | 0 | bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles", |
8318 | 0 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8319 | 0 | b0 = NULL; |
8320 | | /*NOTREACHED*/ |
8321 | 0 | } |
8322 | | /* match outgoing packets */ |
8323 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H, |
8324 | 0 | PACKET_OUTGOING); |
8325 | 0 | if (!dir) { |
8326 | | /* to filter on inbound traffic, invert the match */ |
8327 | 0 | gen_not(b0); |
8328 | 0 | } |
8329 | | #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */ |
8330 | | bpf_error(cstate, "inbound/outbound not supported on %s", |
8331 | | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8332 | | /*NOTREACHED*/ |
8333 | | #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */ |
8334 | 0 | } |
8335 | 0 | return (b0); |
8336 | 0 | } |
8337 | | |
8338 | | #ifdef HAVE_NET_PFVAR_H |
8339 | | /* PF firewall log matched interface */ |
8340 | | struct block * |
8341 | | gen_pf_ifname(compiler_state_t *cstate, const char *ifname) |
8342 | | { |
8343 | | struct block *b0; |
8344 | | u_int len, off; |
8345 | | |
8346 | | /* |
8347 | | * Catch errors reported by us and routines below us, and return NULL |
8348 | | * on an error. |
8349 | | */ |
8350 | | if (setjmp(cstate->top_ctx)) |
8351 | | return (NULL); |
8352 | | |
8353 | | if (cstate->linktype != DLT_PFLOG) { |
8354 | | bpf_error(cstate, "ifname supported only on PF linktype"); |
8355 | | /*NOTREACHED*/ |
8356 | | } |
8357 | | len = sizeof(((struct pfloghdr *)0)->ifname); |
8358 | | off = offsetof(struct pfloghdr, ifname); |
8359 | | if (strlen(ifname) >= len) { |
8360 | | bpf_error(cstate, "ifname interface names can only be %d characters", |
8361 | | len-1); |
8362 | | /*NOTREACHED*/ |
8363 | | } |
8364 | | b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname), |
8365 | | (const u_char *)ifname); |
8366 | | return (b0); |
8367 | | } |
8368 | | |
8369 | | /* PF firewall log ruleset name */ |
8370 | | struct block * |
8371 | | gen_pf_ruleset(compiler_state_t *cstate, char *ruleset) |
8372 | | { |
8373 | | struct block *b0; |
8374 | | |
8375 | | /* |
8376 | | * Catch errors reported by us and routines below us, and return NULL |
8377 | | * on an error. |
8378 | | */ |
8379 | | if (setjmp(cstate->top_ctx)) |
8380 | | return (NULL); |
8381 | | |
8382 | | if (cstate->linktype != DLT_PFLOG) { |
8383 | | bpf_error(cstate, "ruleset supported only on PF linktype"); |
8384 | | /*NOTREACHED*/ |
8385 | | } |
8386 | | |
8387 | | if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { |
8388 | | bpf_error(cstate, "ruleset names can only be %ld characters", |
8389 | | (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1)); |
8390 | | /*NOTREACHED*/ |
8391 | | } |
8392 | | |
8393 | | b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset), |
8394 | | (u_int)strlen(ruleset), (const u_char *)ruleset); |
8395 | | return (b0); |
8396 | | } |
8397 | | |
8398 | | /* PF firewall log rule number */ |
8399 | | struct block * |
8400 | | gen_pf_rnr(compiler_state_t *cstate, int rnr) |
8401 | | { |
8402 | | struct block *b0; |
8403 | | |
8404 | | /* |
8405 | | * Catch errors reported by us and routines below us, and return NULL |
8406 | | * on an error. |
8407 | | */ |
8408 | | if (setjmp(cstate->top_ctx)) |
8409 | | return (NULL); |
8410 | | |
8411 | | if (cstate->linktype != DLT_PFLOG) { |
8412 | | bpf_error(cstate, "rnr supported only on PF linktype"); |
8413 | | /*NOTREACHED*/ |
8414 | | } |
8415 | | |
8416 | | b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W, |
8417 | | (bpf_int32)rnr); |
8418 | | return (b0); |
8419 | | } |
8420 | | |
8421 | | /* PF firewall log sub-rule number */ |
8422 | | struct block * |
8423 | | gen_pf_srnr(compiler_state_t *cstate, int srnr) |
8424 | | { |
8425 | | struct block *b0; |
8426 | | |
8427 | | /* |
8428 | | * Catch errors reported by us and routines below us, and return NULL |
8429 | | * on an error. |
8430 | | */ |
8431 | | if (setjmp(cstate->top_ctx)) |
8432 | | return (NULL); |
8433 | | |
8434 | | if (cstate->linktype != DLT_PFLOG) { |
8435 | | bpf_error(cstate, "srnr supported only on PF linktype"); |
8436 | | /*NOTREACHED*/ |
8437 | | } |
8438 | | |
8439 | | b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W, |
8440 | | (bpf_int32)srnr); |
8441 | | return (b0); |
8442 | | } |
8443 | | |
8444 | | /* PF firewall log reason code */ |
8445 | | struct block * |
8446 | | gen_pf_reason(compiler_state_t *cstate, int reason) |
8447 | | { |
8448 | | struct block *b0; |
8449 | | |
8450 | | /* |
8451 | | * Catch errors reported by us and routines below us, and return NULL |
8452 | | * on an error. |
8453 | | */ |
8454 | | if (setjmp(cstate->top_ctx)) |
8455 | | return (NULL); |
8456 | | |
8457 | | if (cstate->linktype != DLT_PFLOG) { |
8458 | | bpf_error(cstate, "reason supported only on PF linktype"); |
8459 | | /*NOTREACHED*/ |
8460 | | } |
8461 | | |
8462 | | b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B, |
8463 | | (bpf_int32)reason); |
8464 | | return (b0); |
8465 | | } |
8466 | | |
8467 | | /* PF firewall log action */ |
8468 | | struct block * |
8469 | | gen_pf_action(compiler_state_t *cstate, int action) |
8470 | | { |
8471 | | struct block *b0; |
8472 | | |
8473 | | /* |
8474 | | * Catch errors reported by us and routines below us, and return NULL |
8475 | | * on an error. |
8476 | | */ |
8477 | | if (setjmp(cstate->top_ctx)) |
8478 | | return (NULL); |
8479 | | |
8480 | | if (cstate->linktype != DLT_PFLOG) { |
8481 | | bpf_error(cstate, "action supported only on PF linktype"); |
8482 | | /*NOTREACHED*/ |
8483 | | } |
8484 | | |
8485 | | b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B, |
8486 | | (bpf_int32)action); |
8487 | | return (b0); |
8488 | | } |
8489 | | #else /* !HAVE_NET_PFVAR_H */ |
8490 | | struct block * |
8491 | | gen_pf_ifname(compiler_state_t *cstate, const char *ifname _U_) |
8492 | 0 | { |
8493 | | /* |
8494 | | * Catch errors reported by us and routines below us, and return NULL |
8495 | | * on an error. |
8496 | | */ |
8497 | 0 | if (setjmp(cstate->top_ctx)) |
8498 | 0 | return (NULL); |
8499 | | |
8500 | 0 | bpf_error(cstate, "libpcap was compiled without pf support"); |
8501 | | /*NOTREACHED*/ |
8502 | 0 | } |
8503 | | |
8504 | | struct block * |
8505 | | gen_pf_ruleset(compiler_state_t *cstate, char *ruleset _U_) |
8506 | 0 | { |
8507 | | /* |
8508 | | * Catch errors reported by us and routines below us, and return NULL |
8509 | | * on an error. |
8510 | | */ |
8511 | 0 | if (setjmp(cstate->top_ctx)) |
8512 | 0 | return (NULL); |
8513 | | |
8514 | 0 | bpf_error(cstate, "libpcap was compiled on a machine without pf support"); |
8515 | | /*NOTREACHED*/ |
8516 | 0 | } |
8517 | | |
8518 | | struct block * |
8519 | | gen_pf_rnr(compiler_state_t *cstate, int rnr _U_) |
8520 | 0 | { |
8521 | | /* |
8522 | | * Catch errors reported by us and routines below us, and return NULL |
8523 | | * on an error. |
8524 | | */ |
8525 | 0 | if (setjmp(cstate->top_ctx)) |
8526 | 0 | return (NULL); |
8527 | | |
8528 | 0 | bpf_error(cstate, "libpcap was compiled on a machine without pf support"); |
8529 | | /*NOTREACHED*/ |
8530 | 0 | } |
8531 | | |
8532 | | struct block * |
8533 | | gen_pf_srnr(compiler_state_t *cstate, int srnr _U_) |
8534 | 0 | { |
8535 | | /* |
8536 | | * Catch errors reported by us and routines below us, and return NULL |
8537 | | * on an error. |
8538 | | */ |
8539 | 0 | if (setjmp(cstate->top_ctx)) |
8540 | 0 | return (NULL); |
8541 | | |
8542 | 0 | bpf_error(cstate, "libpcap was compiled on a machine without pf support"); |
8543 | | /*NOTREACHED*/ |
8544 | 0 | } |
8545 | | |
8546 | | struct block * |
8547 | | gen_pf_reason(compiler_state_t *cstate, int reason _U_) |
8548 | 0 | { |
8549 | | /* |
8550 | | * Catch errors reported by us and routines below us, and return NULL |
8551 | | * on an error. |
8552 | | */ |
8553 | 0 | if (setjmp(cstate->top_ctx)) |
8554 | 0 | return (NULL); |
8555 | | |
8556 | 0 | bpf_error(cstate, "libpcap was compiled on a machine without pf support"); |
8557 | | /*NOTREACHED*/ |
8558 | 0 | } |
8559 | | |
8560 | | struct block * |
8561 | | gen_pf_action(compiler_state_t *cstate, int action _U_) |
8562 | 0 | { |
8563 | | /* |
8564 | | * Catch errors reported by us and routines below us, and return NULL |
8565 | | * on an error. |
8566 | | */ |
8567 | 0 | if (setjmp(cstate->top_ctx)) |
8568 | 0 | return (NULL); |
8569 | | |
8570 | 0 | bpf_error(cstate, "libpcap was compiled on a machine without pf support"); |
8571 | | /*NOTREACHED*/ |
8572 | 0 | } |
8573 | | #endif /* HAVE_NET_PFVAR_H */ |
8574 | | |
8575 | | /* IEEE 802.11 wireless header */ |
8576 | | struct block * |
8577 | | gen_p80211_type(compiler_state_t *cstate, int type, int mask) |
8578 | 0 | { |
8579 | 0 | struct block *b0; |
8580 | | |
8581 | | /* |
8582 | | * Catch errors reported by us and routines below us, and return NULL |
8583 | | * on an error. |
8584 | | */ |
8585 | 0 | if (setjmp(cstate->top_ctx)) |
8586 | 0 | return (NULL); |
8587 | | |
8588 | 0 | switch (cstate->linktype) { |
8589 | | |
8590 | 0 | case DLT_IEEE802_11: |
8591 | 0 | case DLT_PRISM_HEADER: |
8592 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8593 | 0 | case DLT_IEEE802_11_RADIO: |
8594 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, (bpf_int32)type, |
8595 | 0 | (bpf_int32)mask); |
8596 | 0 | break; |
8597 | | |
8598 | 0 | default: |
8599 | 0 | bpf_error(cstate, "802.11 link-layer types supported only on 802.11"); |
8600 | | /*NOTREACHED*/ |
8601 | 0 | } |
8602 | | |
8603 | 0 | return (b0); |
8604 | 0 | } |
8605 | | |
8606 | | struct block * |
8607 | | gen_p80211_fcdir(compiler_state_t *cstate, int fcdir) |
8608 | 0 | { |
8609 | 0 | struct block *b0; |
8610 | | |
8611 | | /* |
8612 | | * Catch errors reported by us and routines below us, and return NULL |
8613 | | * on an error. |
8614 | | */ |
8615 | 0 | if (setjmp(cstate->top_ctx)) |
8616 | 0 | return (NULL); |
8617 | | |
8618 | 0 | switch (cstate->linktype) { |
8619 | | |
8620 | 0 | case DLT_IEEE802_11: |
8621 | 0 | case DLT_PRISM_HEADER: |
8622 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8623 | 0 | case DLT_IEEE802_11_RADIO: |
8624 | 0 | break; |
8625 | | |
8626 | 0 | default: |
8627 | 0 | bpf_error(cstate, "frame direction supported only with 802.11 headers"); |
8628 | | /*NOTREACHED*/ |
8629 | 0 | } |
8630 | | |
8631 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, (bpf_int32)fcdir, |
8632 | 0 | (bpf_u_int32)IEEE80211_FC1_DIR_MASK); |
8633 | |
|
8634 | 0 | return (b0); |
8635 | 0 | } |
8636 | | |
8637 | | struct block * |
8638 | | gen_acode(compiler_state_t *cstate, const char *s, struct qual q) |
8639 | 0 | { |
8640 | 0 | struct block *b; |
8641 | | |
8642 | | /* |
8643 | | * Catch errors reported by us and routines below us, and return NULL |
8644 | | * on an error. |
8645 | | */ |
8646 | 0 | if (setjmp(cstate->top_ctx)) |
8647 | 0 | return (NULL); |
8648 | | |
8649 | 0 | switch (cstate->linktype) { |
8650 | | |
8651 | 0 | case DLT_ARCNET: |
8652 | 0 | case DLT_ARCNET_LINUX: |
8653 | 0 | if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && |
8654 | 0 | q.proto == Q_LINK) { |
8655 | 0 | cstate->e = pcap_ether_aton(s); |
8656 | 0 | if (cstate->e == NULL) |
8657 | 0 | bpf_error(cstate, "malloc"); |
8658 | 0 | b = gen_ahostop(cstate, cstate->e, (int)q.dir); |
8659 | 0 | free(cstate->e); |
8660 | 0 | cstate->e = NULL; |
8661 | 0 | return (b); |
8662 | 0 | } else |
8663 | 0 | bpf_error(cstate, "ARCnet address used in non-arc expression"); |
8664 | | /*NOTREACHED*/ |
8665 | | |
8666 | 0 | default: |
8667 | 0 | bpf_error(cstate, "aid supported only on ARCnet"); |
8668 | | /*NOTREACHED*/ |
8669 | 0 | } |
8670 | 0 | } |
8671 | | |
8672 | | static struct block * |
8673 | | gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
8674 | 0 | { |
8675 | 0 | register struct block *b0, *b1; |
8676 | |
|
8677 | 0 | switch (dir) { |
8678 | | /* src comes first, different from Ethernet */ |
8679 | 0 | case Q_SRC: |
8680 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr); |
8681 | | |
8682 | 0 | case Q_DST: |
8683 | 0 | return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr); |
8684 | | |
8685 | 0 | case Q_AND: |
8686 | 0 | b0 = gen_ahostop(cstate, eaddr, Q_SRC); |
8687 | 0 | b1 = gen_ahostop(cstate, eaddr, Q_DST); |
8688 | 0 | gen_and(b0, b1); |
8689 | 0 | return b1; |
8690 | | |
8691 | 0 | case Q_DEFAULT: |
8692 | 0 | case Q_OR: |
8693 | 0 | b0 = gen_ahostop(cstate, eaddr, Q_SRC); |
8694 | 0 | b1 = gen_ahostop(cstate, eaddr, Q_DST); |
8695 | 0 | gen_or(b0, b1); |
8696 | 0 | return b1; |
8697 | | |
8698 | 0 | case Q_ADDR1: |
8699 | 0 | bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); |
8700 | | /*NOTREACHED*/ |
8701 | | |
8702 | 0 | case Q_ADDR2: |
8703 | 0 | bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); |
8704 | | /*NOTREACHED*/ |
8705 | | |
8706 | 0 | case Q_ADDR3: |
8707 | 0 | bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); |
8708 | | /*NOTREACHED*/ |
8709 | | |
8710 | 0 | case Q_ADDR4: |
8711 | 0 | bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); |
8712 | | /*NOTREACHED*/ |
8713 | | |
8714 | 0 | case Q_RA: |
8715 | 0 | bpf_error(cstate, "'ra' is only supported on 802.11"); |
8716 | | /*NOTREACHED*/ |
8717 | | |
8718 | 0 | case Q_TA: |
8719 | 0 | bpf_error(cstate, "'ta' is only supported on 802.11"); |
8720 | | /*NOTREACHED*/ |
8721 | 0 | } |
8722 | 0 | abort(); |
8723 | | /*NOTREACHED*/ |
8724 | 0 | } |
8725 | | |
8726 | | static struct block * |
8727 | | gen_vlan_tpid_test(compiler_state_t *cstate) |
8728 | 0 | { |
8729 | 0 | struct block *b0, *b1; |
8730 | | |
8731 | | /* check for VLAN, including QinQ */ |
8732 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_8021Q); |
8733 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_8021AD); |
8734 | 0 | gen_or(b0,b1); |
8735 | 0 | b0 = b1; |
8736 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ); |
8737 | 0 | gen_or(b0,b1); |
8738 | |
|
8739 | 0 | return b1; |
8740 | 0 | } |
8741 | | |
8742 | | static struct block * |
8743 | | gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num) |
8744 | 0 | { |
8745 | 0 | if (vlan_num > 0x0fff) { |
8746 | 0 | bpf_error(cstate, "VLAN tag %u greater than maximum %u", |
8747 | 0 | vlan_num, 0x0fff); |
8748 | 0 | } |
8749 | 0 | return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, (bpf_int32)vlan_num, 0x0fff); |
8750 | 0 | } |
8751 | | |
8752 | | static struct block * |
8753 | | gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8754 | | int has_vlan_tag) |
8755 | 0 | { |
8756 | 0 | struct block *b0, *b1; |
8757 | |
|
8758 | 0 | b0 = gen_vlan_tpid_test(cstate); |
8759 | |
|
8760 | 0 | if (has_vlan_tag) { |
8761 | 0 | b1 = gen_vlan_vid_test(cstate, vlan_num); |
8762 | 0 | gen_and(b0, b1); |
8763 | 0 | b0 = b1; |
8764 | 0 | } |
8765 | | |
8766 | | /* |
8767 | | * Both payload and link header type follow the VLAN tags so that |
8768 | | * both need to be updated. |
8769 | | */ |
8770 | 0 | cstate->off_linkpl.constant_part += 4; |
8771 | 0 | cstate->off_linktype.constant_part += 4; |
8772 | |
|
8773 | 0 | return b0; |
8774 | 0 | } |
8775 | | |
8776 | | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8777 | | /* add v to variable part of off */ |
8778 | | static void |
8779 | | gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, int v, struct slist *s) |
8780 | 0 | { |
8781 | 0 | struct slist *s2; |
8782 | |
|
8783 | 0 | if (!off->is_variable) |
8784 | 0 | off->is_variable = 1; |
8785 | 0 | if (off->reg == -1) |
8786 | 0 | off->reg = alloc_reg(cstate); |
8787 | |
|
8788 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
8789 | 0 | s2->s.k = off->reg; |
8790 | 0 | sappend(s, s2); |
8791 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
8792 | 0 | s2->s.k = v; |
8793 | 0 | sappend(s, s2); |
8794 | 0 | s2 = new_stmt(cstate, BPF_ST); |
8795 | 0 | s2->s.k = off->reg; |
8796 | 0 | sappend(s, s2); |
8797 | 0 | } |
8798 | | |
8799 | | /* |
8800 | | * patch block b_tpid (VLAN TPID test) to update variable parts of link payload |
8801 | | * and link type offsets first |
8802 | | */ |
8803 | | static void |
8804 | | gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid) |
8805 | 0 | { |
8806 | 0 | struct slist s; |
8807 | | |
8808 | | /* offset determined at run time, shift variable part */ |
8809 | 0 | s.next = NULL; |
8810 | 0 | cstate->is_vlan_vloffset = 1; |
8811 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s); |
8812 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s); |
8813 | | |
8814 | | /* we get a pointer to a chain of or-ed blocks, patch first of them */ |
8815 | 0 | sappend(s.next, b_tpid->head->stmts); |
8816 | 0 | b_tpid->head->stmts = s.next; |
8817 | 0 | } |
8818 | | |
8819 | | /* |
8820 | | * patch block b_vid (VLAN id test) to load VID value either from packet |
8821 | | * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true |
8822 | | */ |
8823 | | static void |
8824 | | gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid) |
8825 | 0 | { |
8826 | 0 | struct slist *s, *s2, *sjeq; |
8827 | 0 | unsigned cnt; |
8828 | |
|
8829 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8830 | 0 | s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT; |
8831 | | |
8832 | | /* true -> next instructions, false -> beginning of b_vid */ |
8833 | 0 | sjeq = new_stmt(cstate, JMP(BPF_JEQ)); |
8834 | 0 | sjeq->s.k = 1; |
8835 | 0 | sjeq->s.jf = b_vid->stmts; |
8836 | 0 | sappend(s, sjeq); |
8837 | |
|
8838 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8839 | 0 | s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG; |
8840 | 0 | sappend(s, s2); |
8841 | 0 | sjeq->s.jt = s2; |
8842 | | |
8843 | | /* Jump to the test in b_vid. We need to jump one instruction before |
8844 | | * the end of the b_vid block so that we only skip loading the TCI |
8845 | | * from packet data and not the 'and' instruction extractging VID. |
8846 | | */ |
8847 | 0 | cnt = 0; |
8848 | 0 | for (s2 = b_vid->stmts; s2; s2 = s2->next) |
8849 | 0 | cnt++; |
8850 | 0 | s2 = new_stmt(cstate, JMP(BPF_JA)); |
8851 | 0 | s2->s.k = cnt - 1; |
8852 | 0 | sappend(s, s2); |
8853 | | |
8854 | | /* insert our statements at the beginning of b_vid */ |
8855 | 0 | sappend(s, b_vid->stmts); |
8856 | 0 | b_vid->stmts = s; |
8857 | 0 | } |
8858 | | |
8859 | | /* |
8860 | | * Generate check for "vlan" or "vlan <id>" on systems with support for BPF |
8861 | | * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN |
8862 | | * tag can be either in metadata or in packet data; therefore if the |
8863 | | * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link |
8864 | | * header for VLAN tag. As the decision is done at run time, we need |
8865 | | * update variable part of the offsets |
8866 | | */ |
8867 | | static struct block * |
8868 | | gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8869 | | int has_vlan_tag) |
8870 | 0 | { |
8871 | 0 | struct block *b0, *b_tpid, *b_vid = NULL; |
8872 | 0 | struct slist *s; |
8873 | | |
8874 | | /* generate new filter code based on extracting packet |
8875 | | * metadata */ |
8876 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8877 | 0 | s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT; |
8878 | |
|
8879 | 0 | b0 = new_block(cstate, JMP(BPF_JEQ)); |
8880 | 0 | b0->stmts = s; |
8881 | 0 | b0->s.k = 1; |
8882 | | |
8883 | | /* |
8884 | | * This is tricky. We need to insert the statements updating variable |
8885 | | * parts of offsets before the the traditional TPID and VID tests so |
8886 | | * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but |
8887 | | * we do not want this update to affect those checks. That's why we |
8888 | | * generate both test blocks first and insert the statements updating |
8889 | | * variable parts of both offsets after that. This wouldn't work if |
8890 | | * there already were variable length link header when entering this |
8891 | | * function but gen_vlan_bpf_extensions() isn't called in that case. |
8892 | | */ |
8893 | 0 | b_tpid = gen_vlan_tpid_test(cstate); |
8894 | 0 | if (has_vlan_tag) |
8895 | 0 | b_vid = gen_vlan_vid_test(cstate, vlan_num); |
8896 | |
|
8897 | 0 | gen_vlan_patch_tpid_test(cstate, b_tpid); |
8898 | 0 | gen_or(b0, b_tpid); |
8899 | 0 | b0 = b_tpid; |
8900 | |
|
8901 | 0 | if (has_vlan_tag) { |
8902 | 0 | gen_vlan_patch_vid_test(cstate, b_vid); |
8903 | 0 | gen_and(b0, b_vid); |
8904 | 0 | b0 = b_vid; |
8905 | 0 | } |
8906 | |
|
8907 | 0 | return b0; |
8908 | 0 | } |
8909 | | #endif |
8910 | | |
8911 | | /* |
8912 | | * support IEEE 802.1Q VLAN trunk over ethernet |
8913 | | */ |
8914 | | struct block * |
8915 | | gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag) |
8916 | 0 | { |
8917 | 0 | struct block *b0; |
8918 | | |
8919 | | /* |
8920 | | * Catch errors reported by us and routines below us, and return NULL |
8921 | | * on an error. |
8922 | | */ |
8923 | 0 | if (setjmp(cstate->top_ctx)) |
8924 | 0 | return (NULL); |
8925 | | |
8926 | | /* can't check for VLAN-encapsulated packets inside MPLS */ |
8927 | 0 | if (cstate->label_stack_depth > 0) |
8928 | 0 | bpf_error(cstate, "no VLAN match after MPLS"); |
8929 | | |
8930 | | /* |
8931 | | * Check for a VLAN packet, and then change the offsets to point |
8932 | | * to the type and data fields within the VLAN packet. Just |
8933 | | * increment the offsets, so that we can support a hierarchy, e.g. |
8934 | | * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within |
8935 | | * VLAN 100. |
8936 | | * |
8937 | | * XXX - this is a bit of a kludge. If we were to split the |
8938 | | * compiler into a parser that parses an expression and |
8939 | | * generates an expression tree, and a code generator that |
8940 | | * takes an expression tree (which could come from our |
8941 | | * parser or from some other parser) and generates BPF code, |
8942 | | * we could perhaps make the offsets parameters of routines |
8943 | | * and, in the handler for an "AND" node, pass to subnodes |
8944 | | * other than the VLAN node the adjusted offsets. |
8945 | | * |
8946 | | * This would mean that "vlan" would, instead of changing the |
8947 | | * behavior of *all* tests after it, change only the behavior |
8948 | | * of tests ANDed with it. That would change the documented |
8949 | | * semantics of "vlan", which might break some expressions. |
8950 | | * However, it would mean that "(vlan and ip) or ip" would check |
8951 | | * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than |
8952 | | * checking only for VLAN-encapsulated IP, so that could still |
8953 | | * be considered worth doing; it wouldn't break expressions |
8954 | | * that are of the form "vlan and ..." or "vlan N and ...", |
8955 | | * which I suspect are the most common expressions involving |
8956 | | * "vlan". "vlan or ..." doesn't necessarily do what the user |
8957 | | * would really want, now, as all the "or ..." tests would |
8958 | | * be done assuming a VLAN, even though the "or" could be viewed |
8959 | | * as meaning "or, if this isn't a VLAN packet...". |
8960 | | */ |
8961 | 0 | switch (cstate->linktype) { |
8962 | | |
8963 | 0 | case DLT_EN10MB: |
8964 | 0 | case DLT_NETANALYZER: |
8965 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
8966 | 0 | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8967 | | /* Verify that this is the outer part of the packet and |
8968 | | * not encapsulated somehow. */ |
8969 | 0 | if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable && |
8970 | 0 | cstate->off_linkhdr.constant_part == |
8971 | 0 | cstate->off_outermostlinkhdr.constant_part) { |
8972 | | /* |
8973 | | * Do we need special VLAN handling? |
8974 | | */ |
8975 | 0 | if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING) |
8976 | 0 | b0 = gen_vlan_bpf_extensions(cstate, vlan_num, |
8977 | 0 | has_vlan_tag); |
8978 | 0 | else |
8979 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, |
8980 | 0 | vlan_num, has_vlan_tag); |
8981 | 0 | } else |
8982 | 0 | #endif |
8983 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, |
8984 | 0 | has_vlan_tag); |
8985 | 0 | break; |
8986 | | |
8987 | 0 | case DLT_IEEE802_11: |
8988 | 0 | case DLT_PRISM_HEADER: |
8989 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8990 | 0 | case DLT_IEEE802_11_RADIO: |
8991 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag); |
8992 | 0 | break; |
8993 | | |
8994 | 0 | default: |
8995 | 0 | bpf_error(cstate, "no VLAN support for %s", |
8996 | 0 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8997 | | /*NOTREACHED*/ |
8998 | 0 | } |
8999 | | |
9000 | 0 | cstate->vlan_stack_depth++; |
9001 | |
|
9002 | 0 | return (b0); |
9003 | 0 | } |
9004 | | |
9005 | | /* |
9006 | | * support for MPLS |
9007 | | * |
9008 | | * The label_num_arg dance is to avoid annoying whining by compilers that |
9009 | | * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
9010 | | * It's not *used* after setjmp returns. |
9011 | | */ |
9012 | | struct block * |
9013 | | gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg, |
9014 | | int has_label_num) |
9015 | 0 | { |
9016 | 0 | volatile bpf_u_int32 label_num = label_num_arg; |
9017 | 0 | struct block *b0, *b1; |
9018 | | |
9019 | | /* |
9020 | | * Catch errors reported by us and routines below us, and return NULL |
9021 | | * on an error. |
9022 | | */ |
9023 | 0 | if (setjmp(cstate->top_ctx)) |
9024 | 0 | return (NULL); |
9025 | | |
9026 | 0 | if (cstate->label_stack_depth > 0) { |
9027 | | /* just match the bottom-of-stack bit clear */ |
9028 | 0 | b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01); |
9029 | 0 | } else { |
9030 | | /* |
9031 | | * We're not in an MPLS stack yet, so check the link-layer |
9032 | | * type against MPLS. |
9033 | | */ |
9034 | 0 | switch (cstate->linktype) { |
9035 | | |
9036 | 0 | case DLT_C_HDLC: /* fall through */ |
9037 | 0 | case DLT_EN10MB: |
9038 | 0 | case DLT_NETANALYZER: |
9039 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
9040 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_MPLS); |
9041 | 0 | break; |
9042 | | |
9043 | 0 | case DLT_PPP: |
9044 | 0 | b0 = gen_linktype(cstate, PPP_MPLS_UCAST); |
9045 | 0 | break; |
9046 | | |
9047 | | /* FIXME add other DLT_s ... |
9048 | | * for Frame-Relay/and ATM this may get messy due to SNAP headers |
9049 | | * leave it for now */ |
9050 | | |
9051 | 0 | default: |
9052 | 0 | bpf_error(cstate, "no MPLS support for %s", |
9053 | 0 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
9054 | | /*NOTREACHED*/ |
9055 | 0 | } |
9056 | 0 | } |
9057 | | |
9058 | | /* If a specific MPLS label is requested, check it */ |
9059 | 0 | if (has_label_num) { |
9060 | 0 | if (label_num > 0xFFFFF) { |
9061 | 0 | bpf_error(cstate, "MPLS label %u greater than maximum %u", |
9062 | 0 | label_num, 0xFFFFF); |
9063 | 0 | } |
9064 | 0 | label_num = label_num << 12; /* label is shifted 12 bits on the wire */ |
9065 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, (bpf_int32)label_num, |
9066 | 0 | 0xfffff000); /* only compare the first 20 bits */ |
9067 | 0 | gen_and(b0, b1); |
9068 | 0 | b0 = b1; |
9069 | 0 | } |
9070 | | |
9071 | | /* |
9072 | | * Change the offsets to point to the type and data fields within |
9073 | | * the MPLS packet. Just increment the offsets, so that we |
9074 | | * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to |
9075 | | * capture packets with an outer label of 100000 and an inner |
9076 | | * label of 1024. |
9077 | | * |
9078 | | * Increment the MPLS stack depth as well; this indicates that |
9079 | | * we're checking MPLS-encapsulated headers, to make sure higher |
9080 | | * level code generators don't try to match against IP-related |
9081 | | * protocols such as Q_ARP, Q_RARP etc. |
9082 | | * |
9083 | | * XXX - this is a bit of a kludge. See comments in gen_vlan(). |
9084 | | */ |
9085 | 0 | cstate->off_nl_nosnap += 4; |
9086 | 0 | cstate->off_nl += 4; |
9087 | 0 | cstate->label_stack_depth++; |
9088 | 0 | return (b0); |
9089 | 0 | } |
9090 | | |
9091 | | /* |
9092 | | * Support PPPOE discovery and session. |
9093 | | */ |
9094 | | struct block * |
9095 | | gen_pppoed(compiler_state_t *cstate) |
9096 | 0 | { |
9097 | | /* |
9098 | | * Catch errors reported by us and routines below us, and return NULL |
9099 | | * on an error. |
9100 | | */ |
9101 | 0 | if (setjmp(cstate->top_ctx)) |
9102 | 0 | return (NULL); |
9103 | | |
9104 | | /* check for PPPoE discovery */ |
9105 | 0 | return gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOED); |
9106 | 0 | } |
9107 | | |
9108 | | struct block * |
9109 | | gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num) |
9110 | 0 | { |
9111 | 0 | struct block *b0, *b1; |
9112 | | |
9113 | | /* |
9114 | | * Catch errors reported by us and routines below us, and return NULL |
9115 | | * on an error. |
9116 | | */ |
9117 | 0 | if (setjmp(cstate->top_ctx)) |
9118 | 0 | return (NULL); |
9119 | | |
9120 | | /* |
9121 | | * Test against the PPPoE session link-layer type. |
9122 | | */ |
9123 | 0 | b0 = gen_linktype(cstate, (bpf_int32)ETHERTYPE_PPPOES); |
9124 | | |
9125 | | /* If a specific session is requested, check PPPoE session id */ |
9126 | 0 | if (has_sess_num) { |
9127 | 0 | if (sess_num > 0x0000ffff) { |
9128 | 0 | bpf_error(cstate, "PPPoE session number %u greater than maximum %u", |
9129 | 0 | sess_num, 0x0000ffff); |
9130 | 0 | } |
9131 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, |
9132 | 0 | (bpf_int32)sess_num, 0x0000ffff); |
9133 | 0 | gen_and(b0, b1); |
9134 | 0 | b0 = b1; |
9135 | 0 | } |
9136 | | |
9137 | | /* |
9138 | | * Change the offsets to point to the type and data fields within |
9139 | | * the PPP packet, and note that this is PPPoE rather than |
9140 | | * raw PPP. |
9141 | | * |
9142 | | * XXX - this is a bit of a kludge. See the comments in |
9143 | | * gen_vlan(). |
9144 | | * |
9145 | | * The "network-layer" protocol is PPPoE, which has a 6-byte |
9146 | | * PPPoE header, followed by a PPP packet. |
9147 | | * |
9148 | | * There is no HDLC encapsulation for the PPP packet (it's |
9149 | | * encapsulated in PPPoES instead), so the link-layer type |
9150 | | * starts at the first byte of the PPP packet. For PPPoE, |
9151 | | * that offset is relative to the beginning of the total |
9152 | | * link-layer payload, including any 802.2 LLC header, so |
9153 | | * it's 6 bytes past cstate->off_nl. |
9154 | | */ |
9155 | 0 | PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable, |
9156 | 0 | cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */ |
9157 | 0 | cstate->off_linkpl.reg); |
9158 | |
|
9159 | 0 | cstate->off_linktype = cstate->off_linkhdr; |
9160 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2; |
9161 | |
|
9162 | 0 | cstate->off_nl = 0; |
9163 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
9164 | |
|
9165 | 0 | return b0; |
9166 | 0 | } |
9167 | | |
9168 | | /* Check that this is Geneve and the VNI is correct if |
9169 | | * specified. Parameterized to handle both IPv4 and IPv6. */ |
9170 | | static struct block * |
9171 | | gen_geneve_check(compiler_state_t *cstate, |
9172 | | struct block *(*gen_portfn)(compiler_state_t *, int, int, int), |
9173 | | enum e_offrel offrel, bpf_u_int32 vni, int has_vni) |
9174 | 0 | { |
9175 | 0 | struct block *b0, *b1; |
9176 | |
|
9177 | 0 | b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST); |
9178 | | |
9179 | | /* Check that we are operating on version 0. Otherwise, we |
9180 | | * can't decode the rest of the fields. The version is 2 bits |
9181 | | * in the first byte of the Geneve header. */ |
9182 | 0 | b1 = gen_mcmp(cstate, offrel, 8, BPF_B, (bpf_int32)0, 0xc0); |
9183 | 0 | gen_and(b0, b1); |
9184 | 0 | b0 = b1; |
9185 | |
|
9186 | 0 | if (has_vni) { |
9187 | 0 | if (vni > 0xffffff) { |
9188 | 0 | bpf_error(cstate, "Geneve VNI %u greater than maximum %u", |
9189 | 0 | vni, 0xffffff); |
9190 | 0 | } |
9191 | 0 | vni <<= 8; /* VNI is in the upper 3 bytes */ |
9192 | 0 | b1 = gen_mcmp(cstate, offrel, 12, BPF_W, (bpf_int32)vni, |
9193 | 0 | 0xffffff00); |
9194 | 0 | gen_and(b0, b1); |
9195 | 0 | b0 = b1; |
9196 | 0 | } |
9197 | | |
9198 | 0 | return b0; |
9199 | 0 | } |
9200 | | |
9201 | | /* The IPv4 and IPv6 Geneve checks need to do two things: |
9202 | | * - Verify that this actually is Geneve with the right VNI. |
9203 | | * - Place the IP header length (plus variable link prefix if |
9204 | | * needed) into register A to be used later to compute |
9205 | | * the inner packet offsets. */ |
9206 | | static struct block * |
9207 | | gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9208 | 0 | { |
9209 | 0 | struct block *b0, *b1; |
9210 | 0 | struct slist *s, *s1; |
9211 | |
|
9212 | 0 | b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); |
9213 | | |
9214 | | /* Load the IP header length into A. */ |
9215 | 0 | s = gen_loadx_iphdrlen(cstate); |
9216 | |
|
9217 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9218 | 0 | sappend(s, s1); |
9219 | | |
9220 | | /* Forcibly append these statements to the true condition |
9221 | | * of the protocol check by creating a new block that is |
9222 | | * always true and ANDing them. */ |
9223 | 0 | b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); |
9224 | 0 | b1->stmts = s; |
9225 | 0 | b1->s.k = 0; |
9226 | |
|
9227 | 0 | gen_and(b0, b1); |
9228 | |
|
9229 | 0 | return b1; |
9230 | 0 | } |
9231 | | |
9232 | | static struct block * |
9233 | | gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9234 | 0 | { |
9235 | 0 | struct block *b0, *b1; |
9236 | 0 | struct slist *s, *s1; |
9237 | |
|
9238 | 0 | b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); |
9239 | | |
9240 | | /* Load the IP header length. We need to account for a |
9241 | | * variable length link prefix if there is one. */ |
9242 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
9243 | 0 | if (s) { |
9244 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IMM); |
9245 | 0 | s1->s.k = 40; |
9246 | 0 | sappend(s, s1); |
9247 | |
|
9248 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9249 | 0 | s1->s.k = 0; |
9250 | 0 | sappend(s, s1); |
9251 | 0 | } else { |
9252 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
9253 | 0 | s->s.k = 40; |
9254 | 0 | } |
9255 | | |
9256 | | /* Forcibly append these statements to the true condition |
9257 | | * of the protocol check by creating a new block that is |
9258 | | * always true and ANDing them. */ |
9259 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9260 | 0 | sappend(s, s1); |
9261 | |
|
9262 | 0 | b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); |
9263 | 0 | b1->stmts = s; |
9264 | 0 | b1->s.k = 0; |
9265 | |
|
9266 | 0 | gen_and(b0, b1); |
9267 | |
|
9268 | 0 | return b1; |
9269 | 0 | } |
9270 | | |
9271 | | /* We need to store three values based on the Geneve header:: |
9272 | | * - The offset of the linktype. |
9273 | | * - The offset of the end of the Geneve header. |
9274 | | * - The offset of the end of the encapsulated MAC header. */ |
9275 | | static struct slist * |
9276 | | gen_geneve_offsets(compiler_state_t *cstate) |
9277 | 0 | { |
9278 | 0 | struct slist *s, *s1, *s_proto; |
9279 | | |
9280 | | /* First we need to calculate the offset of the Geneve header |
9281 | | * itself. This is composed of the IP header previously calculated |
9282 | | * (include any variable link prefix) and stored in A plus the |
9283 | | * fixed sized headers (fixed link prefix, MAC length, and UDP |
9284 | | * header). */ |
9285 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9286 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; |
9287 | | |
9288 | | /* Stash this in X since we'll need it later. */ |
9289 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9290 | 0 | sappend(s, s1); |
9291 | | |
9292 | | /* The EtherType in Geneve is 2 bytes in. Calculate this and |
9293 | | * store it. */ |
9294 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9295 | 0 | s1->s.k = 2; |
9296 | 0 | sappend(s, s1); |
9297 | |
|
9298 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
9299 | 0 | cstate->off_linktype.is_variable = 1; |
9300 | 0 | cstate->off_linktype.constant_part = 0; |
9301 | |
|
9302 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9303 | 0 | s1->s.k = cstate->off_linktype.reg; |
9304 | 0 | sappend(s, s1); |
9305 | | |
9306 | | /* Load the Geneve option length and mask and shift to get the |
9307 | | * number of bytes. It is stored in the first byte of the Geneve |
9308 | | * header. */ |
9309 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
9310 | 0 | s1->s.k = 0; |
9311 | 0 | sappend(s, s1); |
9312 | |
|
9313 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
9314 | 0 | s1->s.k = 0x3f; |
9315 | 0 | sappend(s, s1); |
9316 | |
|
9317 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
9318 | 0 | s1->s.k = 4; |
9319 | 0 | sappend(s, s1); |
9320 | | |
9321 | | /* Add in the rest of the Geneve base header. */ |
9322 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9323 | 0 | s1->s.k = 8; |
9324 | 0 | sappend(s, s1); |
9325 | | |
9326 | | /* Add the Geneve header length to its offset and store. */ |
9327 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9328 | 0 | s1->s.k = 0; |
9329 | 0 | sappend(s, s1); |
9330 | | |
9331 | | /* Set the encapsulated type as Ethernet. Even though we may |
9332 | | * not actually have Ethernet inside there are two reasons this |
9333 | | * is useful: |
9334 | | * - The linktype field is always in EtherType format regardless |
9335 | | * of whether it is in Geneve or an inner Ethernet frame. |
9336 | | * - The only link layer that we have specific support for is |
9337 | | * Ethernet. We will confirm that the packet actually is |
9338 | | * Ethernet at runtime before executing these checks. */ |
9339 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); |
9340 | |
|
9341 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9342 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9343 | 0 | sappend(s, s1); |
9344 | | |
9345 | | /* Calculate whether we have an Ethernet header or just raw IP/ |
9346 | | * MPLS/etc. If we have Ethernet, advance the end of the MAC offset |
9347 | | * and linktype by 14 bytes so that the network header can be found |
9348 | | * seamlessly. Otherwise, keep what we've calculated already. */ |
9349 | | |
9350 | | /* We have a bare jmp so we can't use the optimizer. */ |
9351 | 0 | cstate->no_optimize = 1; |
9352 | | |
9353 | | /* Load the EtherType in the Geneve header, 2 bytes in. */ |
9354 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H); |
9355 | 0 | s1->s.k = 2; |
9356 | 0 | sappend(s, s1); |
9357 | | |
9358 | | /* Load X with the end of the Geneve header. */ |
9359 | 0 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9360 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9361 | 0 | sappend(s, s1); |
9362 | | |
9363 | | /* Check if the EtherType is Transparent Ethernet Bridging. At the |
9364 | | * end of this check, we should have the total length in X. In |
9365 | | * the non-Ethernet case, it's already there. */ |
9366 | 0 | s_proto = new_stmt(cstate, JMP(BPF_JEQ)); |
9367 | 0 | s_proto->s.k = ETHERTYPE_TEB; |
9368 | 0 | sappend(s, s_proto); |
9369 | |
|
9370 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9371 | 0 | sappend(s, s1); |
9372 | 0 | s_proto->s.jt = s1; |
9373 | | |
9374 | | /* Since this is Ethernet, use the EtherType of the payload |
9375 | | * directly as the linktype. Overwrite what we already have. */ |
9376 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9377 | 0 | s1->s.k = 12; |
9378 | 0 | sappend(s, s1); |
9379 | |
|
9380 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9381 | 0 | s1->s.k = cstate->off_linktype.reg; |
9382 | 0 | sappend(s, s1); |
9383 | | |
9384 | | /* Advance two bytes further to get the end of the Ethernet |
9385 | | * header. */ |
9386 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9387 | 0 | s1->s.k = 2; |
9388 | 0 | sappend(s, s1); |
9389 | | |
9390 | | /* Move the result to X. */ |
9391 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9392 | 0 | sappend(s, s1); |
9393 | | |
9394 | | /* Store the final result of our linkpl calculation. */ |
9395 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
9396 | 0 | cstate->off_linkpl.is_variable = 1; |
9397 | 0 | cstate->off_linkpl.constant_part = 0; |
9398 | |
|
9399 | 0 | s1 = new_stmt(cstate, BPF_STX); |
9400 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9401 | 0 | sappend(s, s1); |
9402 | 0 | s_proto->s.jf = s1; |
9403 | |
|
9404 | 0 | cstate->off_nl = 0; |
9405 | |
|
9406 | 0 | return s; |
9407 | 0 | } |
9408 | | |
9409 | | /* Check to see if this is a Geneve packet. */ |
9410 | | struct block * |
9411 | | gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9412 | 0 | { |
9413 | 0 | struct block *b0, *b1; |
9414 | 0 | struct slist *s; |
9415 | | |
9416 | | /* |
9417 | | * Catch errors reported by us and routines below us, and return NULL |
9418 | | * on an error. |
9419 | | */ |
9420 | 0 | if (setjmp(cstate->top_ctx)) |
9421 | 0 | return (NULL); |
9422 | | |
9423 | 0 | b0 = gen_geneve4(cstate, vni, has_vni); |
9424 | 0 | b1 = gen_geneve6(cstate, vni, has_vni); |
9425 | |
|
9426 | 0 | gen_or(b0, b1); |
9427 | 0 | b0 = b1; |
9428 | | |
9429 | | /* Later filters should act on the payload of the Geneve frame, |
9430 | | * update all of the header pointers. Attach this code so that |
9431 | | * it gets executed in the event that the Geneve filter matches. */ |
9432 | 0 | s = gen_geneve_offsets(cstate); |
9433 | |
|
9434 | 0 | b1 = gen_true(cstate); |
9435 | 0 | sappend(s, b1->stmts); |
9436 | 0 | b1->stmts = s; |
9437 | |
|
9438 | 0 | gen_and(b0, b1); |
9439 | |
|
9440 | 0 | cstate->is_geneve = 1; |
9441 | |
|
9442 | 0 | return b1; |
9443 | 0 | } |
9444 | | |
9445 | | /* Check that the encapsulated frame has a link layer header |
9446 | | * for Ethernet filters. */ |
9447 | | static struct block * |
9448 | | gen_geneve_ll_check(compiler_state_t *cstate) |
9449 | 0 | { |
9450 | 0 | struct block *b0; |
9451 | 0 | struct slist *s, *s1; |
9452 | | |
9453 | | /* The easiest way to see if there is a link layer present |
9454 | | * is to check if the link layer header and payload are not |
9455 | | * the same. */ |
9456 | | |
9457 | | /* Geneve always generates pure variable offsets so we can |
9458 | | * compare only the registers. */ |
9459 | 0 | s = new_stmt(cstate, BPF_LD|BPF_MEM); |
9460 | 0 | s->s.k = cstate->off_linkhdr.reg; |
9461 | |
|
9462 | 0 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9463 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9464 | 0 | sappend(s, s1); |
9465 | |
|
9466 | 0 | b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); |
9467 | 0 | b0->stmts = s; |
9468 | 0 | b0->s.k = 0; |
9469 | 0 | gen_not(b0); |
9470 | |
|
9471 | 0 | return b0; |
9472 | 0 | } |
9473 | | |
9474 | | static struct block * |
9475 | | gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield, |
9476 | | bpf_int32 jvalue, bpf_u_int32 jtype, int reverse) |
9477 | 0 | { |
9478 | 0 | struct block *b0; |
9479 | |
|
9480 | 0 | switch (atmfield) { |
9481 | | |
9482 | 0 | case A_VPI: |
9483 | 0 | if (!cstate->is_atm) |
9484 | 0 | bpf_error(cstate, "'vpi' supported only on raw ATM"); |
9485 | 0 | if (cstate->off_vpi == OFFSET_NOT_SET) |
9486 | 0 | abort(); |
9487 | 0 | b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, 0xffffffff, jtype, |
9488 | 0 | reverse, jvalue); |
9489 | 0 | break; |
9490 | | |
9491 | 0 | case A_VCI: |
9492 | 0 | if (!cstate->is_atm) |
9493 | 0 | bpf_error(cstate, "'vci' supported only on raw ATM"); |
9494 | 0 | if (cstate->off_vci == OFFSET_NOT_SET) |
9495 | 0 | abort(); |
9496 | 0 | b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, 0xffffffff, jtype, |
9497 | 0 | reverse, jvalue); |
9498 | 0 | break; |
9499 | | |
9500 | 0 | case A_PROTOTYPE: |
9501 | 0 | if (cstate->off_proto == OFFSET_NOT_SET) |
9502 | 0 | abort(); /* XXX - this isn't on FreeBSD */ |
9503 | 0 | b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0x0f, jtype, |
9504 | 0 | reverse, jvalue); |
9505 | 0 | break; |
9506 | | |
9507 | 0 | case A_MSGTYPE: |
9508 | 0 | if (cstate->off_payload == OFFSET_NOT_SET) |
9509 | 0 | abort(); |
9510 | 0 | b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B, |
9511 | 0 | 0xffffffff, jtype, reverse, jvalue); |
9512 | 0 | break; |
9513 | | |
9514 | 0 | case A_CALLREFTYPE: |
9515 | 0 | if (!cstate->is_atm) |
9516 | 0 | bpf_error(cstate, "'callref' supported only on raw ATM"); |
9517 | 0 | if (cstate->off_proto == OFFSET_NOT_SET) |
9518 | 0 | abort(); |
9519 | 0 | b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 0xffffffff, |
9520 | 0 | jtype, reverse, jvalue); |
9521 | 0 | break; |
9522 | | |
9523 | 0 | default: |
9524 | 0 | abort(); |
9525 | 0 | } |
9526 | 0 | return b0; |
9527 | 0 | } |
9528 | | |
9529 | | static struct block * |
9530 | | gen_atmtype_metac(compiler_state_t *cstate) |
9531 | 0 | { |
9532 | 0 | struct block *b0, *b1; |
9533 | |
|
9534 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9535 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0); |
9536 | 0 | gen_and(b0, b1); |
9537 | 0 | return b1; |
9538 | 0 | } |
9539 | | |
9540 | | static struct block * |
9541 | | gen_atmtype_sc(compiler_state_t *cstate) |
9542 | 0 | { |
9543 | 0 | struct block *b0, *b1; |
9544 | |
|
9545 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9546 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0); |
9547 | 0 | gen_and(b0, b1); |
9548 | 0 | return b1; |
9549 | 0 | } |
9550 | | |
9551 | | static struct block * |
9552 | | gen_atmtype_llc(compiler_state_t *cstate) |
9553 | 0 | { |
9554 | 0 | struct block *b0; |
9555 | |
|
9556 | 0 | b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); |
9557 | 0 | cstate->linktype = cstate->prevlinktype; |
9558 | 0 | return b0; |
9559 | 0 | } |
9560 | | |
9561 | | struct block * |
9562 | | gen_atmfield_code(compiler_state_t *cstate, int atmfield, |
9563 | | bpf_int32 jvalue, bpf_u_int32 jtype, int reverse) |
9564 | 0 | { |
9565 | | /* |
9566 | | * Catch errors reported by us and routines below us, and return NULL |
9567 | | * on an error. |
9568 | | */ |
9569 | 0 | if (setjmp(cstate->top_ctx)) |
9570 | 0 | return (NULL); |
9571 | | |
9572 | 0 | return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype, |
9573 | 0 | reverse); |
9574 | 0 | } |
9575 | | |
9576 | | struct block * |
9577 | | gen_atmtype_abbrev(compiler_state_t *cstate, int type) |
9578 | 0 | { |
9579 | 0 | struct block *b0, *b1; |
9580 | | |
9581 | | /* |
9582 | | * Catch errors reported by us and routines below us, and return NULL |
9583 | | * on an error. |
9584 | | */ |
9585 | 0 | if (setjmp(cstate->top_ctx)) |
9586 | 0 | return (NULL); |
9587 | | |
9588 | 0 | switch (type) { |
9589 | | |
9590 | 0 | case A_METAC: |
9591 | | /* Get all packets in Meta signalling Circuit */ |
9592 | 0 | if (!cstate->is_atm) |
9593 | 0 | bpf_error(cstate, "'metac' supported only on raw ATM"); |
9594 | 0 | b1 = gen_atmtype_metac(cstate); |
9595 | 0 | break; |
9596 | | |
9597 | 0 | case A_BCC: |
9598 | | /* Get all packets in Broadcast Circuit*/ |
9599 | 0 | if (!cstate->is_atm) |
9600 | 0 | bpf_error(cstate, "'bcc' supported only on raw ATM"); |
9601 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9602 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0); |
9603 | 0 | gen_and(b0, b1); |
9604 | 0 | break; |
9605 | | |
9606 | 0 | case A_OAMF4SC: |
9607 | | /* Get all cells in Segment OAM F4 circuit*/ |
9608 | 0 | if (!cstate->is_atm) |
9609 | 0 | bpf_error(cstate, "'oam4sc' supported only on raw ATM"); |
9610 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9611 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); |
9612 | 0 | gen_and(b0, b1); |
9613 | 0 | break; |
9614 | | |
9615 | 0 | case A_OAMF4EC: |
9616 | | /* Get all cells in End-to-End OAM F4 Circuit*/ |
9617 | 0 | if (!cstate->is_atm) |
9618 | 0 | bpf_error(cstate, "'oam4ec' supported only on raw ATM"); |
9619 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9620 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); |
9621 | 0 | gen_and(b0, b1); |
9622 | 0 | break; |
9623 | | |
9624 | 0 | case A_SC: |
9625 | | /* Get all packets in connection Signalling Circuit */ |
9626 | 0 | if (!cstate->is_atm) |
9627 | 0 | bpf_error(cstate, "'sc' supported only on raw ATM"); |
9628 | 0 | b1 = gen_atmtype_sc(cstate); |
9629 | 0 | break; |
9630 | | |
9631 | 0 | case A_ILMIC: |
9632 | | /* Get all packets in ILMI Circuit */ |
9633 | 0 | if (!cstate->is_atm) |
9634 | 0 | bpf_error(cstate, "'ilmic' supported only on raw ATM"); |
9635 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9636 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0); |
9637 | 0 | gen_and(b0, b1); |
9638 | 0 | break; |
9639 | | |
9640 | 0 | case A_LANE: |
9641 | | /* Get all LANE packets */ |
9642 | 0 | if (!cstate->is_atm) |
9643 | 0 | bpf_error(cstate, "'lane' supported only on raw ATM"); |
9644 | 0 | b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0); |
9645 | | |
9646 | | /* |
9647 | | * Arrange that all subsequent tests assume LANE |
9648 | | * rather than LLC-encapsulated packets, and set |
9649 | | * the offsets appropriately for LANE-encapsulated |
9650 | | * Ethernet. |
9651 | | * |
9652 | | * We assume LANE means Ethernet, not Token Ring. |
9653 | | */ |
9654 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 0, |
9655 | 0 | cstate->off_payload + 2, /* Ethernet header */ |
9656 | 0 | -1); |
9657 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
9658 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */ |
9659 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
9660 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
9661 | 0 | break; |
9662 | | |
9663 | 0 | case A_LLC: |
9664 | | /* Get all LLC-encapsulated packets */ |
9665 | 0 | if (!cstate->is_atm) |
9666 | 0 | bpf_error(cstate, "'llc' supported only on raw ATM"); |
9667 | 0 | b1 = gen_atmtype_llc(cstate); |
9668 | 0 | break; |
9669 | | |
9670 | 0 | default: |
9671 | 0 | abort(); |
9672 | 0 | } |
9673 | 0 | return b1; |
9674 | 0 | } |
9675 | | |
9676 | | /* |
9677 | | * Filtering for MTP2 messages based on li value |
9678 | | * FISU, length is null |
9679 | | * LSSU, length is 1 or 2 |
9680 | | * MSU, length is 3 or more |
9681 | | * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits |
9682 | | */ |
9683 | | struct block * |
9684 | | gen_mtp2type_abbrev(compiler_state_t *cstate, int type) |
9685 | 0 | { |
9686 | 0 | struct block *b0, *b1; |
9687 | | |
9688 | | /* |
9689 | | * Catch errors reported by us and routines below us, and return NULL |
9690 | | * on an error. |
9691 | | */ |
9692 | 0 | if (setjmp(cstate->top_ctx)) |
9693 | 0 | return (NULL); |
9694 | | |
9695 | 0 | switch (type) { |
9696 | | |
9697 | 0 | case M_FISU: |
9698 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9699 | 0 | (cstate->linktype != DLT_ERF) && |
9700 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9701 | 0 | bpf_error(cstate, "'fisu' supported only on MTP2"); |
9702 | | /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */ |
9703 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0); |
9704 | 0 | break; |
9705 | | |
9706 | 0 | case M_LSSU: |
9707 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9708 | 0 | (cstate->linktype != DLT_ERF) && |
9709 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9710 | 0 | bpf_error(cstate, "'lssu' supported only on MTP2"); |
9711 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 1, 2); |
9712 | 0 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 0); |
9713 | 0 | gen_and(b1, b0); |
9714 | 0 | break; |
9715 | | |
9716 | 0 | case M_MSU: |
9717 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9718 | 0 | (cstate->linktype != DLT_ERF) && |
9719 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9720 | 0 | bpf_error(cstate, "'msu' supported only on MTP2"); |
9721 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 0x3f, BPF_JGT, 0, 2); |
9722 | 0 | break; |
9723 | | |
9724 | 0 | case MH_FISU: |
9725 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9726 | 0 | (cstate->linktype != DLT_ERF) && |
9727 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9728 | 0 | bpf_error(cstate, "'hfisu' supported only on MTP2_HSL"); |
9729 | | /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */ |
9730 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JEQ, 0, 0); |
9731 | 0 | break; |
9732 | | |
9733 | 0 | case MH_LSSU: |
9734 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9735 | 0 | (cstate->linktype != DLT_ERF) && |
9736 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9737 | 0 | bpf_error(cstate, "'hlssu' supported only on MTP2_HSL"); |
9738 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 1, 0x0100); |
9739 | 0 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0); |
9740 | 0 | gen_and(b1, b0); |
9741 | 0 | break; |
9742 | | |
9743 | 0 | case MH_MSU: |
9744 | 0 | if ( (cstate->linktype != DLT_MTP2) && |
9745 | 0 | (cstate->linktype != DLT_ERF) && |
9746 | 0 | (cstate->linktype != DLT_MTP2_WITH_PHDR) ) |
9747 | 0 | bpf_error(cstate, "'hmsu' supported only on MTP2_HSL"); |
9748 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0x0100); |
9749 | 0 | break; |
9750 | | |
9751 | 0 | default: |
9752 | 0 | abort(); |
9753 | 0 | } |
9754 | 0 | return b0; |
9755 | 0 | } |
9756 | | |
9757 | | /* |
9758 | | * The jvalue_arg dance is to avoid annoying whining by compilers that |
9759 | | * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
9760 | | * It's not *used* after setjmp returns. |
9761 | | */ |
9762 | | struct block * |
9763 | | gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, |
9764 | | bpf_u_int32 jvalue_arg, bpf_u_int32 jtype, int reverse) |
9765 | 0 | { |
9766 | 0 | volatile bpf_u_int32 jvalue = jvalue_arg; |
9767 | 0 | struct block *b0; |
9768 | 0 | bpf_u_int32 val1 , val2 , val3; |
9769 | 0 | u_int newoff_sio; |
9770 | 0 | u_int newoff_opc; |
9771 | 0 | u_int newoff_dpc; |
9772 | 0 | u_int newoff_sls; |
9773 | | |
9774 | | /* |
9775 | | * Catch errors reported by us and routines below us, and return NULL |
9776 | | * on an error. |
9777 | | */ |
9778 | 0 | if (setjmp(cstate->top_ctx)) |
9779 | 0 | return (NULL); |
9780 | | |
9781 | 0 | newoff_sio = cstate->off_sio; |
9782 | 0 | newoff_opc = cstate->off_opc; |
9783 | 0 | newoff_dpc = cstate->off_dpc; |
9784 | 0 | newoff_sls = cstate->off_sls; |
9785 | 0 | switch (mtp3field) { |
9786 | | |
9787 | 0 | case MH_SIO: |
9788 | 0 | newoff_sio += 3; /* offset for MTP2_HSL */ |
9789 | | /* FALLTHROUGH */ |
9790 | |
|
9791 | 0 | case M_SIO: |
9792 | 0 | if (cstate->off_sio == OFFSET_NOT_SET) |
9793 | 0 | bpf_error(cstate, "'sio' supported only on SS7"); |
9794 | | /* sio coded on 1 byte so max value 255 */ |
9795 | 0 | if(jvalue > 255) |
9796 | 0 | bpf_error(cstate, "sio value %u too big; max value = 255", |
9797 | 0 | jvalue); |
9798 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffff, |
9799 | 0 | (u_int)jtype, reverse, (u_int)jvalue); |
9800 | 0 | break; |
9801 | | |
9802 | 0 | case MH_OPC: |
9803 | 0 | newoff_opc += 3; |
9804 | | |
9805 | | /* FALLTHROUGH */ |
9806 | 0 | case M_OPC: |
9807 | 0 | if (cstate->off_opc == OFFSET_NOT_SET) |
9808 | 0 | bpf_error(cstate, "'opc' supported only on SS7"); |
9809 | | /* opc coded on 14 bits so max value 16383 */ |
9810 | 0 | if (jvalue > 16383) |
9811 | 0 | bpf_error(cstate, "opc value %u too big; max value = 16383", |
9812 | 0 | jvalue); |
9813 | | /* the following instructions are made to convert jvalue |
9814 | | * to the form used to write opc in an ss7 message*/ |
9815 | 0 | val1 = jvalue & 0x00003c00; |
9816 | 0 | val1 = val1 >>10; |
9817 | 0 | val2 = jvalue & 0x000003fc; |
9818 | 0 | val2 = val2 <<6; |
9819 | 0 | val3 = jvalue & 0x00000003; |
9820 | 0 | val3 = val3 <<22; |
9821 | 0 | jvalue = val1 + val2 + val3; |
9822 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0f, |
9823 | 0 | (u_int)jtype, reverse, (u_int)jvalue); |
9824 | 0 | break; |
9825 | | |
9826 | 0 | case MH_DPC: |
9827 | 0 | newoff_dpc += 3; |
9828 | | /* FALLTHROUGH */ |
9829 | |
|
9830 | 0 | case M_DPC: |
9831 | 0 | if (cstate->off_dpc == OFFSET_NOT_SET) |
9832 | 0 | bpf_error(cstate, "'dpc' supported only on SS7"); |
9833 | | /* dpc coded on 14 bits so max value 16383 */ |
9834 | 0 | if (jvalue > 16383) |
9835 | 0 | bpf_error(cstate, "dpc value %u too big; max value = 16383", |
9836 | 0 | jvalue); |
9837 | | /* the following instructions are made to convert jvalue |
9838 | | * to the forme used to write dpc in an ss7 message*/ |
9839 | 0 | val1 = jvalue & 0x000000ff; |
9840 | 0 | val1 = val1 << 24; |
9841 | 0 | val2 = jvalue & 0x00003f00; |
9842 | 0 | val2 = val2 << 8; |
9843 | 0 | jvalue = val1 + val2; |
9844 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000, |
9845 | 0 | (u_int)jtype, reverse, (u_int)jvalue); |
9846 | 0 | break; |
9847 | | |
9848 | 0 | case MH_SLS: |
9849 | 0 | newoff_sls += 3; |
9850 | | /* FALLTHROUGH */ |
9851 | |
|
9852 | 0 | case M_SLS: |
9853 | 0 | if (cstate->off_sls == OFFSET_NOT_SET) |
9854 | 0 | bpf_error(cstate, "'sls' supported only on SS7"); |
9855 | | /* sls coded on 4 bits so max value 15 */ |
9856 | 0 | if (jvalue > 15) |
9857 | 0 | bpf_error(cstate, "sls value %u too big; max value = 15", |
9858 | 0 | jvalue); |
9859 | | /* the following instruction is made to convert jvalue |
9860 | | * to the forme used to write sls in an ss7 message*/ |
9861 | 0 | jvalue = jvalue << 4; |
9862 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0, |
9863 | 0 | (u_int)jtype,reverse, (u_int)jvalue); |
9864 | 0 | break; |
9865 | | |
9866 | 0 | default: |
9867 | 0 | abort(); |
9868 | 0 | } |
9869 | 0 | return b0; |
9870 | 0 | } |
9871 | | |
9872 | | static struct block * |
9873 | | gen_msg_abbrev(compiler_state_t *cstate, int type) |
9874 | 0 | { |
9875 | 0 | struct block *b1; |
9876 | | |
9877 | | /* |
9878 | | * Q.2931 signalling protocol messages for handling virtual circuits |
9879 | | * establishment and teardown |
9880 | | */ |
9881 | 0 | switch (type) { |
9882 | | |
9883 | 0 | case A_SETUP: |
9884 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0); |
9885 | 0 | break; |
9886 | | |
9887 | 0 | case A_CALLPROCEED: |
9888 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0); |
9889 | 0 | break; |
9890 | | |
9891 | 0 | case A_CONNECT: |
9892 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0); |
9893 | 0 | break; |
9894 | | |
9895 | 0 | case A_CONNECTACK: |
9896 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0); |
9897 | 0 | break; |
9898 | | |
9899 | 0 | case A_RELEASE: |
9900 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0); |
9901 | 0 | break; |
9902 | | |
9903 | 0 | case A_RELEASE_DONE: |
9904 | 0 | b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0); |
9905 | 0 | break; |
9906 | | |
9907 | 0 | default: |
9908 | 0 | abort(); |
9909 | 0 | } |
9910 | 0 | return b1; |
9911 | 0 | } |
9912 | | |
9913 | | struct block * |
9914 | | gen_atmmulti_abbrev(compiler_state_t *cstate, int type) |
9915 | 0 | { |
9916 | 0 | struct block *b0, *b1; |
9917 | | |
9918 | | /* |
9919 | | * Catch errors reported by us and routines below us, and return NULL |
9920 | | * on an error. |
9921 | | */ |
9922 | 0 | if (setjmp(cstate->top_ctx)) |
9923 | 0 | return (NULL); |
9924 | | |
9925 | 0 | switch (type) { |
9926 | | |
9927 | 0 | case A_OAM: |
9928 | 0 | if (!cstate->is_atm) |
9929 | 0 | bpf_error(cstate, "'oam' supported only on raw ATM"); |
9930 | | /* OAM F4 type */ |
9931 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); |
9932 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); |
9933 | 0 | gen_or(b0, b1); |
9934 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9935 | 0 | gen_and(b0, b1); |
9936 | 0 | break; |
9937 | | |
9938 | 0 | case A_OAMF4: |
9939 | 0 | if (!cstate->is_atm) |
9940 | 0 | bpf_error(cstate, "'oamf4' supported only on raw ATM"); |
9941 | | /* OAM F4 type */ |
9942 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); |
9943 | 0 | b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); |
9944 | 0 | gen_or(b0, b1); |
9945 | 0 | b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); |
9946 | 0 | gen_and(b0, b1); |
9947 | 0 | break; |
9948 | | |
9949 | 0 | case A_CONNECTMSG: |
9950 | | /* |
9951 | | * Get Q.2931 signalling messages for switched |
9952 | | * virtual connection |
9953 | | */ |
9954 | 0 | if (!cstate->is_atm) |
9955 | 0 | bpf_error(cstate, "'connectmsg' supported only on raw ATM"); |
9956 | 0 | b0 = gen_msg_abbrev(cstate, A_SETUP); |
9957 | 0 | b1 = gen_msg_abbrev(cstate, A_CALLPROCEED); |
9958 | 0 | gen_or(b0, b1); |
9959 | 0 | b0 = gen_msg_abbrev(cstate, A_CONNECT); |
9960 | 0 | gen_or(b0, b1); |
9961 | 0 | b0 = gen_msg_abbrev(cstate, A_CONNECTACK); |
9962 | 0 | gen_or(b0, b1); |
9963 | 0 | b0 = gen_msg_abbrev(cstate, A_RELEASE); |
9964 | 0 | gen_or(b0, b1); |
9965 | 0 | b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE); |
9966 | 0 | gen_or(b0, b1); |
9967 | 0 | b0 = gen_atmtype_sc(cstate); |
9968 | 0 | gen_and(b0, b1); |
9969 | 0 | break; |
9970 | | |
9971 | 0 | case A_METACONNECT: |
9972 | 0 | if (!cstate->is_atm) |
9973 | 0 | bpf_error(cstate, "'metaconnect' supported only on raw ATM"); |
9974 | 0 | b0 = gen_msg_abbrev(cstate, A_SETUP); |
9975 | 0 | b1 = gen_msg_abbrev(cstate, A_CALLPROCEED); |
9976 | 0 | gen_or(b0, b1); |
9977 | 0 | b0 = gen_msg_abbrev(cstate, A_CONNECT); |
9978 | 0 | gen_or(b0, b1); |
9979 | 0 | b0 = gen_msg_abbrev(cstate, A_RELEASE); |
9980 | 0 | gen_or(b0, b1); |
9981 | 0 | b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE); |
9982 | 0 | gen_or(b0, b1); |
9983 | 0 | b0 = gen_atmtype_metac(cstate); |
9984 | 0 | gen_and(b0, b1); |
9985 | 0 | break; |
9986 | | |
9987 | 0 | default: |
9988 | 0 | abort(); |
9989 | 0 | } |
9990 | 0 | return b1; |
9991 | 0 | } |