Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 |
3 | | * The Regents of the University of California. All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that: (1) source code distributions |
7 | | * retain the above copyright notice and this paragraph in its entirety, (2) |
8 | | * distributions including binary code include the above copyright notice and |
9 | | * this paragraph in its entirety in the documentation or other materials |
10 | | * provided with the distribution, and (3) all advertising materials mentioning |
11 | | * features or use of this software display the following acknowledgement: |
12 | | * ``This product includes software developed by the University of California, |
13 | | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
14 | | * the University nor the names of its contributors may be used to endorse |
15 | | * or promote products derived from this software without specific prior |
16 | | * written permission. |
17 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
18 | | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
19 | | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
20 | | */ |
21 | | |
22 | | #include <config.h> |
23 | | |
24 | | #ifdef _WIN32 |
25 | | #include <ws2tcpip.h> |
26 | | #else |
27 | | #include <netinet/in.h> |
28 | | #endif /* _WIN32 */ |
29 | | |
30 | | #include <stdlib.h> |
31 | | #include <string.h> |
32 | | #include <memory.h> |
33 | | #include <setjmp.h> |
34 | | #include <stdarg.h> |
35 | | #include <stdio.h> |
36 | | #include <stdint.h> |
37 | | #include <stddef.h> |
38 | | |
39 | | #include "pcap-int.h" |
40 | | #include "thread-local.h" |
41 | | |
42 | | #include "extract.h" |
43 | | |
44 | | #include "ethertype.h" |
45 | | #include "llc.h" |
46 | | #include "gencode.h" |
47 | | #include "ieee80211.h" |
48 | | #include "pflog.h" |
49 | | #include "ppp.h" |
50 | | #include "pcap/sll.h" |
51 | | #include "pcap/ipnet.h" |
52 | | #include "diag-control.h" |
53 | | #include "pcap-util.h" |
54 | | |
55 | | #include "scanner.h" |
56 | | |
57 | | #if defined(__linux__) |
58 | | #include <linux/types.h> |
59 | | #include <linux/if_packet.h> |
60 | | #include <linux/filter.h> |
61 | | #endif |
62 | | |
63 | | #ifdef _WIN32 |
64 | | #ifdef HAVE_NPCAP_BPF_H |
65 | | /* Defines BPF extensions for Npcap */ |
66 | | #include <npcap-bpf.h> |
67 | | #endif |
68 | | #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) |
69 | | /* IPv6 address */ |
70 | | struct in6_addr |
71 | | { |
72 | | union |
73 | | { |
74 | | uint8_t u6_addr8[16]; |
75 | | uint16_t u6_addr16[8]; |
76 | | uint32_t u6_addr32[4]; |
77 | | } in6_u; |
78 | | #define s6_addr in6_u.u6_addr8 |
79 | | #define s6_addr16 in6_u.u6_addr16 |
80 | | #define s6_addr32 in6_u.u6_addr32 |
81 | | #define s6_addr64 in6_u.u6_addr64 |
82 | | }; |
83 | | |
84 | | typedef unsigned short sa_family_t; |
85 | | |
86 | | #define __SOCKADDR_COMMON(sa_prefix) \ |
87 | | sa_family_t sa_prefix##family |
88 | | |
89 | | /* Ditto, for IPv6. */ |
90 | | struct sockaddr_in6 |
91 | | { |
92 | | __SOCKADDR_COMMON (sin6_); |
93 | | uint16_t sin6_port; /* Transport layer port # */ |
94 | | uint32_t sin6_flowinfo; /* IPv6 flow information */ |
95 | | struct in6_addr sin6_addr; /* IPv6 address */ |
96 | | }; |
97 | | |
98 | | #ifndef EAI_ADDRFAMILY |
99 | | struct addrinfo { |
100 | | int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ |
101 | | int ai_family; /* PF_xxx */ |
102 | | int ai_socktype; /* SOCK_xxx */ |
103 | | int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ |
104 | | size_t ai_addrlen; /* length of ai_addr */ |
105 | | char *ai_canonname; /* canonical name for hostname */ |
106 | | struct sockaddr *ai_addr; /* binary address */ |
107 | | struct addrinfo *ai_next; /* next structure in linked list */ |
108 | | }; |
109 | | #endif /* EAI_ADDRFAMILY */ |
110 | | #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */ |
111 | | #else /* _WIN32 */ |
112 | | #include <netdb.h> /* for "struct addrinfo" */ |
113 | | #endif /* _WIN32 */ |
114 | | #include <pcap/namedb.h> |
115 | | |
116 | | #include "nametoaddr.h" |
117 | | |
118 | 0 | #define ETHERMTU 1500 |
119 | | |
120 | | #ifndef IPPROTO_HOPOPTS |
121 | | #define IPPROTO_HOPOPTS 0 |
122 | | #endif |
123 | | #ifndef IPPROTO_ROUTING |
124 | | #define IPPROTO_ROUTING 43 |
125 | | #endif |
126 | | #ifndef IPPROTO_FRAGMENT |
127 | | #define IPPROTO_FRAGMENT 44 |
128 | | #endif |
129 | | #ifndef IPPROTO_DSTOPTS |
130 | | #define IPPROTO_DSTOPTS 60 |
131 | | #endif |
132 | | #ifndef IPPROTO_SCTP |
133 | | #define IPPROTO_SCTP 132 |
134 | | #endif |
135 | | |
136 | 0 | #define GENEVE_PORT 6081 |
137 | 0 | #define VXLAN_PORT 4789 |
138 | | |
139 | | |
140 | | /* |
141 | | * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp |
142 | | */ |
143 | | |
144 | | /* RFC 1051 */ |
145 | | #define ARCTYPE_IP_OLD 240 /* IP protocol */ |
146 | | #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */ |
147 | | |
148 | | /* RFC 1201 */ |
149 | 0 | #define ARCTYPE_IP 212 /* IP protocol */ |
150 | 0 | #define ARCTYPE_ARP 213 /* address resolution protocol */ |
151 | 0 | #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */ |
152 | | |
153 | 0 | #define ARCTYPE_ATALK 221 /* Appletalk */ |
154 | | #define ARCTYPE_BANIAN 247 /* Banyan Vines */ |
155 | | #define ARCTYPE_IPX 250 /* Novell IPX */ |
156 | | |
157 | 0 | #define ARCTYPE_INET6 0xc4 /* IPng */ |
158 | | #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */ |
159 | | |
160 | | |
161 | | /* Based on UNI3.1 standard by ATM Forum */ |
162 | | |
163 | | /* ATM traffic types based on VPI=0 and (the following VCI */ |
164 | | #define VCI_PPC 0x05 /* Point-to-point signal msg */ |
165 | | #define VCI_BCC 0x02 /* Broadcast signal msg */ |
166 | | #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */ |
167 | | #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */ |
168 | | #define VCI_METAC 0x01 /* Meta signal msg */ |
169 | | #define VCI_ILMIC 0x10 /* ILMI msg */ |
170 | | |
171 | | /* Q.2931 signalling messages */ |
172 | 0 | #define CALL_PROCEED 0x02 /* call proceeding */ |
173 | 0 | #define CONNECT 0x07 /* connect */ |
174 | 0 | #define CONNECT_ACK 0x0f /* connect_ack */ |
175 | 0 | #define SETUP 0x05 /* setup */ |
176 | 0 | #define RELEASE 0x4d /* release */ |
177 | 0 | #define RELEASE_DONE 0x5a /* release_done */ |
178 | | #define RESTART 0x46 /* restart */ |
179 | | #define RESTART_ACK 0x4e /* restart ack */ |
180 | | #define STATUS 0x7d /* status */ |
181 | | #define STATUS_ENQ 0x75 /* status ack */ |
182 | | #define ADD_PARTY 0x80 /* add party */ |
183 | | #define ADD_PARTY_ACK 0x81 /* add party ack */ |
184 | | #define ADD_PARTY_REJ 0x82 /* add party rej */ |
185 | | #define DROP_PARTY 0x83 /* drop party */ |
186 | | #define DROP_PARTY_ACK 0x84 /* drop party ack */ |
187 | | |
188 | | /* Information Element Parameters in the signalling messages */ |
189 | | #define CAUSE 0x08 /* cause */ |
190 | | #define ENDPT_REF 0x54 /* endpoint reference */ |
191 | | #define AAL_PARA 0x58 /* ATM adaptation layer parameters */ |
192 | | #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */ |
193 | | #define CONNECT_ID 0x5a /* connection identifier */ |
194 | | #define QOS_PARA 0x5c /* quality of service parameters */ |
195 | | #define B_HIGHER 0x5d /* broadband higher layer information */ |
196 | | #define B_BEARER 0x5e /* broadband bearer capability */ |
197 | | #define B_LOWER 0x5f /* broadband lower information */ |
198 | | #define CALLING_PARTY 0x6c /* calling party number */ |
199 | | #define CALLED_PARTY 0x70 /* called party number */ |
200 | | |
201 | | #define Q2931 0x09 |
202 | | |
203 | | /* Q.2931 signalling general messages format */ |
204 | 0 | #define PROTO_POS 0 /* offset of protocol discriminator */ |
205 | | #define CALL_REF_POS 2 /* offset of call reference value */ |
206 | 0 | #define MSG_TYPE_POS 5 /* offset of message type */ |
207 | | #define MSG_LEN_POS 7 /* offset of message length */ |
208 | | #define IE_BEGIN_POS 9 /* offset of first information element */ |
209 | | |
210 | | /* format of signalling messages */ |
211 | | #define TYPE_POS 0 |
212 | | #define LEN_POS 2 |
213 | | #define FIELD_BEGIN_POS 4 |
214 | | |
215 | | |
216 | | /* SunATM header for ATM packet */ |
217 | | #define SUNATM_DIR_POS 0 |
218 | 0 | #define SUNATM_VPI_POS 1 |
219 | 0 | #define SUNATM_VCI_POS 2 |
220 | 0 | #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */ |
221 | | |
222 | | /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */ |
223 | 0 | #define PT_LANE 0x01 /* LANE */ |
224 | 0 | #define PT_LLC 0x02 /* LLC encapsulation */ |
225 | | #define PT_ILMI 0x05 /* ILMI */ |
226 | | #define PT_QSAAL 0x06 /* Q.SAAL */ |
227 | | |
228 | | |
229 | | /* Types missing from some systems */ |
230 | | |
231 | | /* |
232 | | * Network layer protocol identifiers |
233 | | */ |
234 | | #ifndef ISO8473_CLNP |
235 | 0 | #define ISO8473_CLNP 0x81 |
236 | | #endif |
237 | | #ifndef ISO9542_ESIS |
238 | 0 | #define ISO9542_ESIS 0x82 |
239 | | #endif |
240 | | #ifndef ISO9542X25_ESIS |
241 | | #define ISO9542X25_ESIS 0x8a |
242 | | #endif |
243 | | #ifndef ISO10589_ISIS |
244 | 0 | #define ISO10589_ISIS 0x83 |
245 | | #endif |
246 | | |
247 | 0 | #define ISIS_L1_LAN_IIH 15 |
248 | 0 | #define ISIS_L2_LAN_IIH 16 |
249 | 0 | #define ISIS_PTP_IIH 17 |
250 | 0 | #define ISIS_L1_LSP 18 |
251 | 0 | #define ISIS_L2_LSP 20 |
252 | 0 | #define ISIS_L1_CSNP 24 |
253 | 0 | #define ISIS_L2_CSNP 25 |
254 | 0 | #define ISIS_L1_PSNP 26 |
255 | 0 | #define ISIS_L2_PSNP 27 |
256 | | /* |
257 | | * The maximum possible value can also be used as a bit mask because the |
258 | | * "PDU Type" field comprises the least significant 5 bits of a particular |
259 | | * octet, see sections 9.5~9.13 of ISO/IEC 10589:2002(E). |
260 | | */ |
261 | 0 | #define ISIS_PDU_TYPE_MAX 0x1FU |
262 | | |
263 | | #ifndef ISO8878A_CONS |
264 | | #define ISO8878A_CONS 0x84 |
265 | | #endif |
266 | | #ifndef ISO10747_IDRP |
267 | | #define ISO10747_IDRP 0x85 |
268 | | #endif |
269 | | |
270 | | // Same as in tcpdump/print-sl.c. |
271 | 0 | #define SLIPDIR_IN 0 |
272 | 0 | #define SLIPDIR_OUT 1 |
273 | | |
274 | | #ifdef HAVE_OS_PROTO_H |
275 | | #include "os-proto.h" |
276 | | #endif |
277 | | |
278 | | /* |
279 | | * A valid jump instruction code is a bitwise OR of three values and one of the |
280 | | * values is BPF_JMP. To make sure both of the other two values are always |
281 | | * present, define a macro of two arguments and use it instead of ORing the |
282 | | * values in place. |
283 | | * |
284 | | * Note that "ja L" (documented as "jmp L" in the 1993 BPF paper) does not quite |
285 | | * follow the pattern and there is no "ja x", but internally it works very much |
286 | | * like "ja #k", so JMP(BPF_JA, BPF_K) is appropriate enough. |
287 | | */ |
288 | 0 | #define JMP(jtype, src) (BPF_JMP | (jtype) | (src)) |
289 | | |
290 | | /* |
291 | | * "Push" the current value of the link-layer header type and link-layer |
292 | | * header offset onto a "stack", and set a new value. (It's not a |
293 | | * full-blown stack; we keep only the top two items.) |
294 | | */ |
295 | 0 | #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \ |
296 | 0 | { \ |
297 | 0 | (cs)->prevlinktype = (cs)->linktype; \ |
298 | 0 | (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \ |
299 | 0 | (cs)->linktype = (new_linktype); \ |
300 | 0 | (cs)->off_linkhdr.is_variable = (new_is_variable); \ |
301 | 0 | (cs)->off_linkhdr.constant_part = (new_constant_part); \ |
302 | 0 | (cs)->off_linkhdr.reg = (new_reg); \ |
303 | 0 | (cs)->is_encap = 0; \ |
304 | 0 | } |
305 | | |
306 | | /* |
307 | | * Offset "not set" value. |
308 | | */ |
309 | 0 | #define OFFSET_NOT_SET 0xffffffffU |
310 | | |
311 | | /* |
312 | | * Absolute offsets, which are offsets from the beginning of the raw |
313 | | * packet data, are, in the general case, the sum of a variable value |
314 | | * and a constant value; the variable value may be absent, in which |
315 | | * case the offset is only the constant value, and the constant value |
316 | | * may be zero, in which case the offset is only the variable value. |
317 | | * |
318 | | * bpf_abs_offset is a structure containing all that information: |
319 | | * |
320 | | * is_variable is 1 if there's a variable part. |
321 | | * |
322 | | * constant_part is the constant part of the value, possibly zero; |
323 | | * |
324 | | * if is_variable is 1, reg is the register number for a register |
325 | | * containing the variable value if the register has been assigned, |
326 | | * and -1 otherwise. |
327 | | */ |
328 | | typedef struct { |
329 | | int is_variable; |
330 | | u_int constant_part; |
331 | | int reg; |
332 | | } bpf_abs_offset; |
333 | | |
334 | | /* |
335 | | * Value passed to gen_load_a() to indicate what the offset argument |
336 | | * is relative to the beginning of. |
337 | | */ |
338 | | enum e_offrel { |
339 | | OR_PACKET, /* full packet data */ |
340 | | OR_LINKHDR, /* link-layer header */ |
341 | | OR_PREVLINKHDR, /* previous link-layer header */ |
342 | | OR_LLC, /* 802.2 LLC header */ |
343 | | OR_PREVMPLSHDR, /* previous MPLS header */ |
344 | | OR_LINKTYPE, /* link-layer type */ |
345 | | OR_LINKPL, /* link-layer payload */ |
346 | | OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */ |
347 | | OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */ |
348 | | OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */ |
349 | | }; |
350 | | |
351 | | /* |
352 | | * We divvy out chunks of memory rather than call malloc each time so |
353 | | * we don't have to worry about leaking memory. It's probably |
354 | | * not a big deal if all this memory was wasted but if this ever |
355 | | * goes into a library that would probably not be a good idea. |
356 | | * |
357 | | * XXX - this *is* in a library.... |
358 | | */ |
359 | 0 | #define NCHUNKS 16 |
360 | 0 | #define CHUNK0SIZE 1024 |
361 | | struct chunk { |
362 | | size_t n_left; |
363 | | void *m; |
364 | | }; |
365 | | |
366 | | /* |
367 | | * A chunk can store any of: |
368 | | * - a string (guaranteed alignment 1 but present for completeness) |
369 | | * - a block |
370 | | * - an slist |
371 | | * - an arth |
372 | | * For this simple allocator every allocated chunk gets rounded up to the |
373 | | * alignment needed for any chunk. |
374 | | */ |
375 | | struct chunk_align { |
376 | | char dummy; |
377 | | union { |
378 | | char c; |
379 | | struct block b; |
380 | | struct slist s; |
381 | | struct arth a; |
382 | | } u; |
383 | | }; |
384 | 0 | #define CHUNK_ALIGN (offsetof(struct chunk_align, u)) |
385 | | |
386 | | /* Code generator state */ |
387 | | |
388 | | struct _compiler_state { |
389 | | jmp_buf top_ctx; |
390 | | pcap_t *bpf_pcap; |
391 | | int error_set; |
392 | | |
393 | | struct icode ic; |
394 | | |
395 | | int snaplen; |
396 | | |
397 | | int linktype; |
398 | | int prevlinktype; |
399 | | int outermostlinktype; |
400 | | |
401 | | bpf_u_int32 netmask; |
402 | | int no_optimize; |
403 | | |
404 | | /* Hack for handling VLAN and MPLS stacks. */ |
405 | | u_int label_stack_depth; |
406 | | u_int vlan_stack_depth; |
407 | | |
408 | | /* XXX */ |
409 | | u_int pcap_fddipad; |
410 | | |
411 | | /* |
412 | | * As errors are handled by a longjmp, anything allocated must |
413 | | * be freed in the longjmp handler, so it must be reachable |
414 | | * from that handler. |
415 | | * |
416 | | * One thing that's allocated is the result of pcap_nametoaddrinfo(); |
417 | | * it must be freed with freeaddrinfo(). This variable points to |
418 | | * any addrinfo structure that would need to be freed. |
419 | | */ |
420 | | struct addrinfo *ai; |
421 | | |
422 | | /* |
423 | | * Various code constructs need to know the layout of the packet. |
424 | | * These values give the necessary offsets from the beginning |
425 | | * of the packet data. |
426 | | */ |
427 | | |
428 | | /* |
429 | | * Absolute offset of the beginning of the link-layer header. |
430 | | */ |
431 | | bpf_abs_offset off_linkhdr; |
432 | | |
433 | | /* |
434 | | * If we're checking a link-layer header for a packet encapsulated |
435 | | * in another protocol layer, this is the equivalent information |
436 | | * for the previous layers' link-layer header from the beginning |
437 | | * of the raw packet data. |
438 | | */ |
439 | | bpf_abs_offset off_prevlinkhdr; |
440 | | |
441 | | /* |
442 | | * This is the equivalent information for the outermost layers' |
443 | | * link-layer header. |
444 | | */ |
445 | | bpf_abs_offset off_outermostlinkhdr; |
446 | | |
447 | | /* |
448 | | * Absolute offset of the beginning of the link-layer payload. |
449 | | */ |
450 | | bpf_abs_offset off_linkpl; |
451 | | |
452 | | /* |
453 | | * "off_linktype" is the offset to information in the link-layer |
454 | | * header giving the packet type. This is an absolute offset |
455 | | * from the beginning of the packet. |
456 | | * |
457 | | * For Ethernet, it's the offset of the Ethernet type field; this |
458 | | * means that it must have a value that skips VLAN tags. |
459 | | * |
460 | | * For link-layer types that always use 802.2 headers, it's the |
461 | | * offset of the LLC header; this means that it must have a value |
462 | | * that skips VLAN tags. |
463 | | * |
464 | | * For PPP, it's the offset of the PPP type field. |
465 | | * |
466 | | * For Cisco HDLC, it's the offset of the CHDLC type field. |
467 | | * |
468 | | * For BSD loopback, it's the offset of the AF_ value. |
469 | | * |
470 | | * For Linux cooked sockets, it's the offset of the type field. |
471 | | * |
472 | | * off_linktype.constant_part is set to OFFSET_NOT_SET for no |
473 | | * encapsulation, in which case, IP is assumed. |
474 | | */ |
475 | | bpf_abs_offset off_linktype; |
476 | | |
477 | | /* |
478 | | * TRUE if the link layer includes an ATM pseudo-header. |
479 | | */ |
480 | | int is_atm; |
481 | | |
482 | | /* TRUE if "geneve" or "vxlan" appeared in the filter; it |
483 | | * causes us to generate code that checks for a Geneve or |
484 | | * VXLAN header respectively and assume that later filters |
485 | | * apply to the encapsulated payload. |
486 | | */ |
487 | | int is_encap; |
488 | | |
489 | | /* |
490 | | * TRUE if we need variable length part of VLAN offset |
491 | | */ |
492 | | int is_vlan_vloffset; |
493 | | |
494 | | /* |
495 | | * These are offsets for the ATM pseudo-header. |
496 | | */ |
497 | | u_int off_vpi; |
498 | | u_int off_vci; |
499 | | u_int off_proto; |
500 | | |
501 | | /* |
502 | | * These are offsets for the MTP2 fields. |
503 | | */ |
504 | | u_int off_li; |
505 | | u_int off_li_hsl; |
506 | | |
507 | | /* |
508 | | * These are offsets for the MTP3 fields. |
509 | | */ |
510 | | u_int off_sio; |
511 | | u_int off_opc; |
512 | | u_int off_dpc; |
513 | | u_int off_sls; |
514 | | |
515 | | /* |
516 | | * This is the offset of the first byte after the ATM pseudo_header, |
517 | | * or -1 if there is no ATM pseudo-header. |
518 | | */ |
519 | | u_int off_payload; |
520 | | |
521 | | /* |
522 | | * These are offsets to the beginning of the network-layer header. |
523 | | * They are relative to the beginning of the link-layer payload |
524 | | * (i.e., they don't include off_linkhdr.constant_part or |
525 | | * off_linkpl.constant_part). |
526 | | * |
527 | | * If the link layer never uses 802.2 LLC: |
528 | | * |
529 | | * "off_nl" and "off_nl_nosnap" are the same. |
530 | | * |
531 | | * If the link layer always uses 802.2 LLC: |
532 | | * |
533 | | * "off_nl" is the offset if there's a SNAP header following |
534 | | * the 802.2 header; |
535 | | * |
536 | | * "off_nl_nosnap" is the offset if there's no SNAP header. |
537 | | * |
538 | | * If the link layer is Ethernet: |
539 | | * |
540 | | * "off_nl" is the offset if the packet is an Ethernet II packet |
541 | | * (we assume no 802.3+802.2+SNAP); |
542 | | * |
543 | | * "off_nl_nosnap" is the offset if the packet is an 802.3 packet |
544 | | * with an 802.2 header following it. |
545 | | */ |
546 | | u_int off_nl; |
547 | | u_int off_nl_nosnap; |
548 | | |
549 | | /* |
550 | | * Here we handle simple allocation of the scratch registers. |
551 | | * If too many registers are alloc'd, the allocator punts. |
552 | | */ |
553 | | int regused[BPF_MEMWORDS]; |
554 | | int curreg; |
555 | | |
556 | | /* |
557 | | * Memory chunks. |
558 | | */ |
559 | | struct chunk chunks[NCHUNKS]; |
560 | | int cur_chunk; |
561 | | }; |
562 | | |
563 | | /* |
564 | | * For use by routines outside this file. |
565 | | */ |
566 | | /* VARARGS */ |
567 | | void |
568 | | bpf_set_error(compiler_state_t *cstate, const char *fmt, ...) |
569 | 0 | { |
570 | 0 | va_list ap; |
571 | | |
572 | | /* |
573 | | * If we've already set an error, don't override it. |
574 | | * The lexical analyzer reports some errors by setting |
575 | | * the error and then returning a LEX_ERROR token, which |
576 | | * is not recognized by any grammar rule, and thus forces |
577 | | * the parse to stop. We don't want the error reported |
578 | | * by the lexical analyzer to be overwritten by the syntax |
579 | | * error. |
580 | | */ |
581 | 0 | if (!cstate->error_set) { |
582 | 0 | va_start(ap, fmt); |
583 | 0 | (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
584 | 0 | fmt, ap); |
585 | 0 | va_end(ap); |
586 | 0 | cstate->error_set = 1; |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | | /* |
591 | | * For use *ONLY* in routines in this file. |
592 | | */ |
593 | | static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...) |
594 | | PCAP_PRINTFLIKE(2, 3); |
595 | | |
596 | | /* VARARGS */ |
597 | | static void PCAP_NORETURN |
598 | | bpf_error(compiler_state_t *cstate, const char *fmt, ...) |
599 | 0 | { |
600 | 0 | va_list ap; |
601 | |
|
602 | 0 | va_start(ap, fmt); |
603 | 0 | (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
604 | 0 | fmt, ap); |
605 | 0 | va_end(ap); |
606 | 0 | longjmp(cstate->top_ctx, 1); |
607 | | /*NOTREACHED*/ |
608 | | #ifdef _AIX |
609 | | PCAP_UNREACHABLE |
610 | | #endif /* _AIX */ |
611 | 0 | } |
612 | | |
613 | | static int init_linktype(compiler_state_t *, pcap_t *); |
614 | | |
615 | | static void init_regs(compiler_state_t *); |
616 | | static int alloc_reg(compiler_state_t *); |
617 | | static void free_reg(compiler_state_t *, int); |
618 | | |
619 | | static void initchunks(compiler_state_t *cstate); |
620 | | static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t); |
621 | | static void *newchunk(compiler_state_t *cstate, size_t); |
622 | | static void freechunks(compiler_state_t *cstate); |
623 | | static inline struct block *new_block(compiler_state_t *cstate, int); |
624 | | static inline struct slist *new_stmt(compiler_state_t *cstate, int); |
625 | | static struct block *sprepend_to_block(struct slist *, struct block *); |
626 | | static struct block *gen_retblk(compiler_state_t *cstate, int); |
627 | | static inline void syntax(compiler_state_t *cstate); |
628 | | |
629 | | static void backpatch(struct block *, struct block *); |
630 | | static void merge(struct block *, struct block *); |
631 | | static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int, |
632 | | u_int, bpf_u_int32); |
633 | | static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int, |
634 | | u_int, bpf_u_int32); |
635 | | static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int, |
636 | | u_int, bpf_u_int32); |
637 | | static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int, |
638 | | u_int, bpf_u_int32); |
639 | | static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int, |
640 | | u_int, bpf_u_int32); |
641 | | static struct block *gen_cmp_ne(compiler_state_t *, enum e_offrel, u_int, |
642 | | u_int size, bpf_u_int32); |
643 | | static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int, |
644 | | u_int, bpf_u_int32, bpf_u_int32); |
645 | | static struct block *gen_mcmp_ne(compiler_state_t *, enum e_offrel, u_int, |
646 | | u_int, bpf_u_int32, bpf_u_int32); |
647 | | static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int, |
648 | | u_int, const u_char *); |
649 | | static struct block *gen_jmp_k(compiler_state_t *, const int, |
650 | | const bpf_u_int32, struct slist *); |
651 | | static struct block *gen_jmp_x(compiler_state_t *, const int, struct slist *); |
652 | | static struct block *gen_set(compiler_state_t *, bpf_u_int32, struct slist *); |
653 | | static struct block *gen_unset(compiler_state_t *, bpf_u_int32, struct slist *); |
654 | | static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int, |
655 | | u_int, bpf_u_int32, int, int, bpf_u_int32); |
656 | | static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *, |
657 | | u_int, u_int); |
658 | | static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int, |
659 | | u_int); |
660 | | static struct slist *gen_loadx_iphdrlen(compiler_state_t *); |
661 | | static struct block *gen_uncond(compiler_state_t *, const u_char); |
662 | | static inline struct block *gen_true(compiler_state_t *); |
663 | | static inline struct block *gen_false(compiler_state_t *); |
664 | | static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32); |
665 | | static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32); |
666 | | static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32); |
667 | | static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *); |
668 | | static struct slist *gen_load_prism_llprefixlen(compiler_state_t *); |
669 | | static struct slist *gen_load_avs_llprefixlen(compiler_state_t *); |
670 | | static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *); |
671 | | static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *); |
672 | | static void insert_compute_vloffsets(compiler_state_t *, struct block *); |
673 | | static struct slist *gen_abs_offset_varpart(compiler_state_t *, |
674 | | bpf_abs_offset *); |
675 | | static uint16_t ethertype_to_ppptype(compiler_state_t *, bpf_u_int32); |
676 | | static struct block *gen_linktype(compiler_state_t *, bpf_u_int32); |
677 | | static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32); |
678 | | static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32); |
679 | | static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
680 | | int, u_int, u_int); |
681 | | static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *, |
682 | | struct in6_addr *, int, u_int, u_int); |
683 | | static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int); |
684 | | static unsigned char is_mac48_linktype(const int); |
685 | | static struct block *gen_mac48host(compiler_state_t *, const u_char *, |
686 | | const u_char, const char *); |
687 | | static struct block *gen_mac48host_byname(compiler_state_t *, const char *, |
688 | | const u_char, const char *); |
689 | | static struct block *gen_mac8host(compiler_state_t *, const uint8_t, |
690 | | const u_char, const char *); |
691 | | static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int); |
692 | | static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32); |
693 | | static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
694 | | int, int, int); |
695 | | static struct block *gen_host6(compiler_state_t *, struct in6_addr *, |
696 | | struct in6_addr *, int, int, int); |
697 | | static struct block *gen_host46_byname(compiler_state_t *, const char *, |
698 | | const u_char, const u_char, const u_char); |
699 | | static struct block *gen_gateway(compiler_state_t *, const char *, const u_char); |
700 | | static struct block *gen_ip_proto(compiler_state_t *, const uint8_t); |
701 | | static struct block *gen_ip6_proto(compiler_state_t *, const uint8_t); |
702 | | static struct block *gen_ipfrag(compiler_state_t *); |
703 | | static struct block *gen_portatom(compiler_state_t *, int, uint16_t); |
704 | | static struct block *gen_portrangeatom(compiler_state_t *, u_int, uint16_t, |
705 | | uint16_t); |
706 | | static struct block *gen_portatom6(compiler_state_t *, int, uint16_t); |
707 | | static struct block *gen_portrangeatom6(compiler_state_t *, u_int, uint16_t, |
708 | | uint16_t); |
709 | | static struct block *gen_port(compiler_state_t *, uint16_t, int, int); |
710 | | static struct block *gen_port_common(compiler_state_t *, int, struct block *); |
711 | | static struct block *gen_portrange(compiler_state_t *, uint16_t, uint16_t, |
712 | | int, int); |
713 | | static struct block *gen_port6(compiler_state_t *, uint16_t, int, int); |
714 | | static struct block *gen_port6_common(compiler_state_t *, int, struct block *); |
715 | | static struct block *gen_portrange6(compiler_state_t *, uint16_t, uint16_t, |
716 | | int, int); |
717 | | static int lookup_proto(compiler_state_t *, const char *, const struct qual); |
718 | | #if !defined(NO_PROTOCHAIN) |
719 | | static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int); |
720 | | #endif /* !defined(NO_PROTOCHAIN) */ |
721 | | static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int); |
722 | | static struct slist *xfer_to_x(compiler_state_t *, struct arth *); |
723 | | static struct slist *xfer_to_a(compiler_state_t *, struct arth *); |
724 | | static struct block *gen_mac_multicast(compiler_state_t *, int); |
725 | | static struct block *gen_len(compiler_state_t *, int, int); |
726 | | static struct block *gen_encap_ll_check(compiler_state_t *cstate); |
727 | | |
728 | | static struct block *gen_atmfield_code_internal(compiler_state_t *, int, |
729 | | bpf_u_int32, int, int); |
730 | | static struct block *gen_atmtype_llc(compiler_state_t *); |
731 | | static struct block *gen_msg_abbrev(compiler_state_t *, const uint8_t); |
732 | | static struct block *gen_atm_prototype(compiler_state_t *, const uint8_t); |
733 | | static struct block *gen_atm_vpi(compiler_state_t *, const uint8_t); |
734 | | static struct block *gen_atm_vci(compiler_state_t *, const uint16_t); |
735 | | |
736 | | static void |
737 | | initchunks(compiler_state_t *cstate) |
738 | 0 | { |
739 | 0 | int i; |
740 | |
|
741 | 0 | for (i = 0; i < NCHUNKS; i++) { |
742 | 0 | cstate->chunks[i].n_left = 0; |
743 | 0 | cstate->chunks[i].m = NULL; |
744 | 0 | } |
745 | 0 | cstate->cur_chunk = 0; |
746 | 0 | } |
747 | | |
748 | | static void * |
749 | | newchunk_nolongjmp(compiler_state_t *cstate, size_t n) |
750 | 0 | { |
751 | 0 | struct chunk *cp; |
752 | 0 | int k; |
753 | 0 | size_t size; |
754 | | |
755 | | /* Round up to chunk alignment. */ |
756 | 0 | n = (n + CHUNK_ALIGN - 1) & ~(CHUNK_ALIGN - 1); |
757 | |
|
758 | 0 | cp = &cstate->chunks[cstate->cur_chunk]; |
759 | 0 | if (n > cp->n_left) { |
760 | 0 | ++cp; |
761 | 0 | k = ++cstate->cur_chunk; |
762 | 0 | if (k >= NCHUNKS) { |
763 | 0 | bpf_set_error(cstate, "out of memory"); |
764 | 0 | return (NULL); |
765 | 0 | } |
766 | 0 | size = CHUNK0SIZE << k; |
767 | 0 | cp->m = calloc(1, size); |
768 | 0 | if (cp->m == NULL) { |
769 | 0 | bpf_set_error(cstate, "out of memory"); |
770 | 0 | return (NULL); |
771 | 0 | } |
772 | 0 | cp->n_left = size; |
773 | 0 | if (n > size) { |
774 | 0 | bpf_set_error(cstate, "out of memory"); |
775 | 0 | return (NULL); |
776 | 0 | } |
777 | 0 | } |
778 | 0 | cp->n_left -= n; |
779 | 0 | return (void *)((char *)cp->m + cp->n_left); |
780 | 0 | } |
781 | | |
782 | | static void * |
783 | | newchunk(compiler_state_t *cstate, size_t n) |
784 | 0 | { |
785 | 0 | void *p; |
786 | |
|
787 | 0 | p = newchunk_nolongjmp(cstate, n); |
788 | 0 | if (p == NULL) { |
789 | 0 | longjmp(cstate->top_ctx, 1); |
790 | | /*NOTREACHED*/ |
791 | 0 | } |
792 | 0 | return (p); |
793 | 0 | } |
794 | | |
795 | | static void |
796 | | freechunks(compiler_state_t *cstate) |
797 | 0 | { |
798 | 0 | int i; |
799 | |
|
800 | 0 | for (i = 0; i < NCHUNKS; ++i) |
801 | 0 | if (cstate->chunks[i].m != NULL) |
802 | 0 | free(cstate->chunks[i].m); |
803 | 0 | } |
804 | | |
805 | | /* |
806 | | * A strdup whose allocations are freed after code generation is over. |
807 | | * This is used by the lexical analyzer, so it can't longjmp; it just |
808 | | * returns NULL on an allocation error, and the callers must check |
809 | | * for it. |
810 | | */ |
811 | | char * |
812 | | sdup(compiler_state_t *cstate, const char *s) |
813 | 0 | { |
814 | 0 | size_t n = strlen(s) + 1; |
815 | 0 | char *cp = newchunk_nolongjmp(cstate, n); |
816 | |
|
817 | 0 | if (cp == NULL) |
818 | 0 | return (NULL); |
819 | 0 | pcapint_strlcpy(cp, s, n); |
820 | 0 | return (cp); |
821 | 0 | } |
822 | | |
823 | | static inline struct block * |
824 | | new_block(compiler_state_t *cstate, int code) |
825 | 0 | { |
826 | 0 | struct block *p; |
827 | |
|
828 | 0 | p = (struct block *)newchunk(cstate, sizeof(*p)); |
829 | 0 | p->s.code = code; |
830 | 0 | p->head = p; |
831 | |
|
832 | 0 | return p; |
833 | 0 | } |
834 | | |
835 | | static inline struct slist * |
836 | | new_stmt(compiler_state_t *cstate, int code) |
837 | 0 | { |
838 | 0 | struct slist *p; |
839 | |
|
840 | 0 | p = (struct slist *)newchunk(cstate, sizeof(*p)); |
841 | 0 | p->s.code = code; |
842 | |
|
843 | 0 | return p; |
844 | 0 | } |
845 | | |
846 | | static struct block * |
847 | | gen_retblk_internal(compiler_state_t *cstate, int v) |
848 | 0 | { |
849 | 0 | struct block *b = new_block(cstate, BPF_RET|BPF_K); |
850 | |
|
851 | 0 | b->s.k = v; |
852 | 0 | return b; |
853 | 0 | } |
854 | | |
855 | | static struct block * |
856 | | gen_retblk(compiler_state_t *cstate, int v) |
857 | 0 | { |
858 | 0 | if (setjmp(cstate->top_ctx)) { |
859 | | /* |
860 | | * gen_retblk() only fails because a memory |
861 | | * allocation failed in newchunk(), meaning |
862 | | * that it can't return a pointer. |
863 | | * |
864 | | * Return NULL. |
865 | | */ |
866 | 0 | return NULL; |
867 | 0 | } |
868 | 0 | return gen_retblk_internal(cstate, v); |
869 | 0 | } |
870 | | |
871 | | static inline PCAP_NORETURN_DEF void |
872 | | syntax(compiler_state_t *cstate) |
873 | 0 | { |
874 | 0 | bpf_error(cstate, "syntax error in filter expression"); |
875 | 0 | } |
876 | | |
877 | | /* |
878 | | * For the given integer return a string with the keyword (or the nominal |
879 | | * keyword if there is more than one). This is a simpler version of tok2str() |
880 | | * in tcpdump because in this problem space a valid integer value is not |
881 | | * greater than 71. |
882 | | */ |
883 | | static const char * |
884 | | qual2kw(const char *kind, const unsigned id, const char *tokens[], |
885 | | const size_t size) |
886 | 0 | { |
887 | 0 | static thread_local char buf[4][64]; |
888 | 0 | static thread_local int idx = 0; |
889 | |
|
890 | 0 | if (id < size && tokens[id]) |
891 | 0 | return tokens[id]; |
892 | | |
893 | 0 | char *ret = buf[idx]; |
894 | 0 | idx = (idx + 1) % (sizeof(buf) / sizeof(buf[0])); |
895 | 0 | ret[0] = '\0'; // just in case |
896 | 0 | snprintf(ret, sizeof(buf[0]), "<invalid %s %u>", kind, id); |
897 | 0 | return ret; |
898 | 0 | } |
899 | | |
900 | | // protocol qualifier keywords |
901 | | static const char * |
902 | | pqkw(const unsigned id) |
903 | 0 | { |
904 | 0 | const char * tokens[] = { |
905 | 0 | [Q_LINK] = "link", |
906 | 0 | [Q_IP] = "ip", |
907 | 0 | [Q_ARP] = "arp", |
908 | 0 | [Q_RARP] = "rarp", |
909 | 0 | [Q_SCTP] = "sctp", |
910 | 0 | [Q_TCP] = "tcp", |
911 | 0 | [Q_UDP] = "udp", |
912 | 0 | [Q_ICMP] = "icmp", |
913 | 0 | [Q_IGMP] = "igmp", |
914 | 0 | [Q_IGRP] = "igrp", |
915 | 0 | [Q_ATALK] = "atalk", |
916 | 0 | [Q_DECNET] = "decnet", |
917 | 0 | [Q_LAT] = "lat", |
918 | 0 | [Q_SCA] = "sca", |
919 | 0 | [Q_MOPRC] = "moprc", |
920 | 0 | [Q_MOPDL] = "mopdl", |
921 | 0 | [Q_IPV6] = "ip6", |
922 | 0 | [Q_ICMPV6] = "icmp6", |
923 | 0 | [Q_AH] = "ah", |
924 | 0 | [Q_ESP] = "esp", |
925 | 0 | [Q_PIM] = "pim", |
926 | 0 | [Q_VRRP] = "vrrp", |
927 | 0 | [Q_AARP] = "aarp", |
928 | 0 | [Q_ISO] = "iso", |
929 | 0 | [Q_ESIS] = "esis", |
930 | 0 | [Q_ISIS] = "isis", |
931 | 0 | [Q_CLNP] = "clnp", |
932 | 0 | [Q_STP] = "stp", |
933 | 0 | [Q_IPX] = "ipx", |
934 | 0 | [Q_NETBEUI] = "netbeui", |
935 | 0 | [Q_ISIS_L1] = "l1", |
936 | 0 | [Q_ISIS_L2] = "l2", |
937 | 0 | [Q_ISIS_IIH] = "iih", |
938 | 0 | [Q_ISIS_SNP] = "snp", |
939 | 0 | [Q_ISIS_CSNP] = "csnp", |
940 | 0 | [Q_ISIS_PSNP] = "psnp", |
941 | 0 | [Q_ISIS_LSP] = "lsp", |
942 | 0 | [Q_RADIO] = "radio", |
943 | 0 | [Q_CARP] = "carp", |
944 | 0 | }; |
945 | 0 | return qual2kw("proto", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
946 | 0 | } |
947 | | |
948 | | // direction qualifier keywords |
949 | | static const char * |
950 | | dqkw(const unsigned id) |
951 | 0 | { |
952 | 0 | const char * tokens[] = { |
953 | 0 | [Q_SRC] = "src", |
954 | 0 | [Q_DST] = "dst", |
955 | 0 | [Q_OR] = "src or dst", |
956 | 0 | [Q_AND] = "src and dst", |
957 | 0 | [Q_ADDR1] = "addr1", |
958 | 0 | [Q_ADDR2] = "addr2", |
959 | 0 | [Q_ADDR3] = "addr3", |
960 | 0 | [Q_ADDR4] = "addr4", |
961 | 0 | [Q_RA] = "ra", |
962 | 0 | [Q_TA] = "ta", |
963 | 0 | }; |
964 | 0 | return qual2kw("dir", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
965 | 0 | } |
966 | | |
967 | | // type (in the man page) / address (in the code) qualifier keywords |
968 | | static const char * |
969 | | tqkw(const unsigned id) |
970 | 0 | { |
971 | 0 | const char * tokens[] = { |
972 | 0 | [Q_HOST] = "host", |
973 | 0 | [Q_NET] = "net", |
974 | 0 | [Q_PORT] = "port", |
975 | 0 | [Q_GATEWAY] = "gateway", |
976 | 0 | [Q_PROTO] = "proto", |
977 | 0 | [Q_PROTOCHAIN] = "protochain", |
978 | 0 | [Q_PORTRANGE] = "portrange", |
979 | 0 | }; |
980 | 0 | return qual2kw("type", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
981 | 0 | } |
982 | | |
983 | | // ATM keywords |
984 | | static const char * |
985 | | atmkw(const unsigned id) |
986 | 0 | { |
987 | 0 | const char * tokens[] = { |
988 | 0 | [A_METAC] = "metac", |
989 | 0 | [A_BCC] = "bcc", |
990 | 0 | [A_OAMF4SC] = "oamf4sc", |
991 | 0 | [A_OAMF4EC] = "oamf4ec", |
992 | 0 | [A_SC] = "sc", |
993 | 0 | [A_ILMIC] = "ilmic", |
994 | 0 | [A_OAM] = "oam", |
995 | 0 | [A_OAMF4] = "oamf4", |
996 | 0 | [A_LANE] = "lane", |
997 | 0 | [A_VPI] = "vpi", |
998 | 0 | [A_VCI] = "vci", |
999 | 0 | [A_CONNECTMSG] = "connectmsg", |
1000 | 0 | [A_METACONNECT] = "metaconnect", |
1001 | 0 | }; |
1002 | 0 | return qual2kw("ATM keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
1003 | 0 | } |
1004 | | |
1005 | | // SS7 keywords |
1006 | | static const char * |
1007 | | ss7kw(const unsigned id) |
1008 | 0 | { |
1009 | 0 | const char * tokens[] = { |
1010 | 0 | [M_FISU] = "fisu", |
1011 | 0 | [M_LSSU] = "lssu", |
1012 | 0 | [M_MSU] = "msu", |
1013 | 0 | [MH_FISU] = "hfisu", |
1014 | 0 | [MH_LSSU] = "hlssu", |
1015 | 0 | [MH_MSU] = "hmsu", |
1016 | 0 | [M_SIO] = "sio", |
1017 | 0 | [M_OPC] = "opc", |
1018 | 0 | [M_DPC] = "dpc", |
1019 | 0 | [M_SLS] = "sls", |
1020 | 0 | [MH_SIO] = "hsio", |
1021 | 0 | [MH_OPC] = "hopc", |
1022 | 0 | [MH_DPC] = "hdpc", |
1023 | 0 | [MH_SLS] = "hsls", |
1024 | 0 | }; |
1025 | 0 | return qual2kw("MTP keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
1026 | 0 | } |
1027 | | |
1028 | | // Produce as descriptive an identification string of the DLT as possible. |
1029 | | static const char * |
1030 | | pcapint_datalink_val_to_string(const int dlt) |
1031 | 0 | { |
1032 | 0 | static thread_local char ret[1024]; |
1033 | 0 | const char *name = pcap_datalink_val_to_name(dlt); |
1034 | 0 | const char *descr = pcap_datalink_val_to_description(dlt); |
1035 | | /* |
1036 | | * Belt and braces: if dlt_choices[] continues to be defined the way it is |
1037 | | * defined now and everything goes well, either both pointers are NULL or |
1038 | | * both pointers are not NULL. But let's not rely on that. |
1039 | | */ |
1040 | 0 | if (name) { |
1041 | 0 | if (descr) |
1042 | 0 | snprintf(ret, sizeof(ret), "DLT_%s (%s)", name, descr); |
1043 | 0 | else |
1044 | 0 | snprintf(ret, sizeof(ret), "DLT_%s", name); |
1045 | 0 | return ret; |
1046 | 0 | } |
1047 | | // name == NULL |
1048 | 0 | if (descr) { |
1049 | 0 | snprintf(ret, sizeof(ret), "DLT %d (%s)", dlt, descr); |
1050 | 0 | return ret; |
1051 | 0 | } |
1052 | | // Both are NULL, use a function that always returns a non-NULL. |
1053 | 0 | return pcap_datalink_val_to_description_or_dlt(dlt); |
1054 | 0 | } |
1055 | | |
1056 | | static PCAP_NORETURN_DEF void |
1057 | | fail_kw_on_dlt(compiler_state_t *cstate, const char *keyword) |
1058 | 0 | { |
1059 | 0 | bpf_error(cstate, "'%s' not supported on %s", keyword, |
1060 | 0 | pcapint_datalink_val_to_string(cstate->linktype)); |
1061 | 0 | } |
1062 | | |
1063 | | static void |
1064 | | assert_pflog(compiler_state_t *cstate, const char *kw) |
1065 | 0 | { |
1066 | 0 | if (cstate->linktype != DLT_PFLOG) |
1067 | 0 | bpf_error(cstate, "'%s' supported only on PFLOG linktype", kw); |
1068 | 0 | } |
1069 | | |
1070 | | static void |
1071 | | assert_atm(compiler_state_t *cstate, const char *kw) |
1072 | 0 | { |
1073 | | /* |
1074 | | * Belt and braces: init_linktype() sets either all of these struct |
1075 | | * members (for DLT_SUNATM) or none (otherwise). |
1076 | | */ |
1077 | 0 | if (cstate->linktype != DLT_SUNATM || |
1078 | 0 | ! cstate->is_atm || |
1079 | 0 | cstate->off_vpi == OFFSET_NOT_SET || |
1080 | 0 | cstate->off_vci == OFFSET_NOT_SET || |
1081 | 0 | cstate->off_proto == OFFSET_NOT_SET || |
1082 | 0 | cstate->off_payload == OFFSET_NOT_SET) |
1083 | 0 | bpf_error(cstate, "'%s' supported only on SUNATM", kw); |
1084 | 0 | } |
1085 | | |
1086 | | static void |
1087 | | assert_ss7(compiler_state_t *cstate, const char *kw) |
1088 | 0 | { |
1089 | 0 | switch (cstate->linktype) { |
1090 | 0 | case DLT_MTP2: |
1091 | 0 | case DLT_ERF: |
1092 | 0 | case DLT_MTP2_WITH_PHDR: |
1093 | | // Belt and braces, same as in assert_atm(). |
1094 | 0 | if (cstate->off_sio != OFFSET_NOT_SET && |
1095 | 0 | cstate->off_opc != OFFSET_NOT_SET && |
1096 | 0 | cstate->off_dpc != OFFSET_NOT_SET && |
1097 | 0 | cstate->off_sls != OFFSET_NOT_SET) |
1098 | 0 | return; |
1099 | 0 | } |
1100 | 0 | bpf_error(cstate, "'%s' supported only on SS7", kw); |
1101 | 0 | } |
1102 | | |
1103 | | static void |
1104 | | assert_maxval(compiler_state_t *cstate, const char *name, |
1105 | | const bpf_u_int32 val, const bpf_u_int32 maxval) |
1106 | 0 | { |
1107 | 0 | if (val > maxval) |
1108 | 0 | bpf_error(cstate, "%s %u greater than maximum %u", |
1109 | 0 | name, val, maxval); |
1110 | 0 | } |
1111 | | |
1112 | 0 | #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only" |
1113 | 0 | #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'" |
1114 | 0 | #define ERRSTR_UNKNOWN_MAC48HOST "unknown Ethernet-like host '%s'" |
1115 | 0 | #define ERRSTR_INVALID_IPV4_ADDR "invalid IPv4 address '%s'" |
1116 | 0 | #define ERRSTR_FUNC_VAR_INT "internal error in %s(): %s == %d" |
1117 | | |
1118 | | // Validate a port/portrange proto qualifier and map to an IP protocol number. |
1119 | | static int |
1120 | | port_pq_to_ipproto(compiler_state_t *cstate, const int proto, const char *kw) |
1121 | 0 | { |
1122 | 0 | switch (proto) { |
1123 | 0 | case Q_UDP: |
1124 | 0 | return IPPROTO_UDP; |
1125 | 0 | case Q_TCP: |
1126 | 0 | return IPPROTO_TCP; |
1127 | 0 | case Q_SCTP: |
1128 | 0 | return IPPROTO_SCTP; |
1129 | 0 | case Q_DEFAULT: |
1130 | 0 | return PROTO_UNDEF; |
1131 | 0 | } |
1132 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), kw); |
1133 | 0 | } |
1134 | | |
1135 | | int |
1136 | | pcap_compile(pcap_t *p, struct bpf_program *program, |
1137 | | const char *buf, int optimize, bpf_u_int32 mask) |
1138 | 0 | { |
1139 | | #ifdef _WIN32 |
1140 | | int err; |
1141 | | WSADATA wsaData; |
1142 | | #endif |
1143 | 0 | compiler_state_t cstate; |
1144 | 0 | yyscan_t scanner = NULL; |
1145 | 0 | YY_BUFFER_STATE in_buffer = NULL; |
1146 | 0 | u_int len; |
1147 | 0 | int rc; |
1148 | | |
1149 | | /* |
1150 | | * If this pcap_t hasn't been activated, it doesn't have a |
1151 | | * link-layer type, so we can't use it. |
1152 | | */ |
1153 | 0 | if (!p->activated) { |
1154 | 0 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1155 | 0 | "not-yet-activated pcap_t passed to pcap_compile"); |
1156 | 0 | return (PCAP_ERROR); |
1157 | 0 | } |
1158 | | |
1159 | | #ifdef _WIN32 |
1160 | | /* |
1161 | | * Initialize Winsock, asking for the latest version (2.2), |
1162 | | * as we may be calling Winsock routines to translate |
1163 | | * host names to addresses. |
1164 | | */ |
1165 | | err = WSAStartup(MAKEWORD(2, 2), &wsaData); |
1166 | | if (err != 0) { |
1167 | | pcapint_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE, |
1168 | | err, "Error calling WSAStartup()"); |
1169 | | return (PCAP_ERROR); |
1170 | | } |
1171 | | #endif |
1172 | | |
1173 | | #ifdef ENABLE_REMOTE |
1174 | | /* |
1175 | | * If the device on which we're capturing need to be notified |
1176 | | * that a new filter is being compiled, do so. |
1177 | | * |
1178 | | * This allows them to save a copy of it, in case, for example, |
1179 | | * they're implementing a form of remote packet capture, and |
1180 | | * want the remote machine to filter out the packets in which |
1181 | | * it's sending the packets it's captured. |
1182 | | * |
1183 | | * XXX - the fact that we happen to be compiling a filter |
1184 | | * doesn't necessarily mean we'll be installing it as the |
1185 | | * filter for this pcap_t; we might be running it from userland |
1186 | | * on captured packets to do packet classification. We really |
1187 | | * need a better way of handling this, but this is all that |
1188 | | * the WinPcap remote capture code did. |
1189 | | */ |
1190 | | if (p->save_current_filter_op != NULL) |
1191 | | (p->save_current_filter_op)(p, buf); |
1192 | | #endif |
1193 | | |
1194 | 0 | initchunks(&cstate); |
1195 | 0 | cstate.no_optimize = 0; |
1196 | 0 | cstate.ai = NULL; |
1197 | 0 | cstate.ic.root = NULL; |
1198 | 0 | cstate.ic.cur_mark = 0; |
1199 | 0 | cstate.bpf_pcap = p; |
1200 | 0 | cstate.error_set = 0; |
1201 | 0 | init_regs(&cstate); |
1202 | |
|
1203 | 0 | cstate.netmask = mask; |
1204 | |
|
1205 | 0 | cstate.snaplen = pcap_snapshot(p); |
1206 | 0 | if (cstate.snaplen == 0) { |
1207 | 0 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1208 | 0 | "snaplen of 0 rejects all packets"); |
1209 | 0 | rc = PCAP_ERROR; |
1210 | 0 | goto quit; |
1211 | 0 | } |
1212 | | |
1213 | 0 | if (pcap_lex_init(&scanner) != 0) { |
1214 | 0 | pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
1215 | 0 | errno, "can't initialize scanner"); |
1216 | 0 | rc = PCAP_ERROR; |
1217 | 0 | goto quit; |
1218 | 0 | } |
1219 | 0 | in_buffer = pcap__scan_string(buf ? buf : "", scanner); |
1220 | | |
1221 | | /* |
1222 | | * Associate the compiler state with the lexical analyzer |
1223 | | * state. |
1224 | | */ |
1225 | 0 | pcap_set_extra(&cstate, scanner); |
1226 | |
|
1227 | 0 | if (init_linktype(&cstate, p) == -1) { |
1228 | 0 | rc = PCAP_ERROR; |
1229 | 0 | goto quit; |
1230 | 0 | } |
1231 | 0 | if (pcap_parse(scanner, &cstate) != 0) { |
1232 | 0 | if (cstate.ai != NULL) |
1233 | 0 | freeaddrinfo(cstate.ai); |
1234 | 0 | rc = PCAP_ERROR; |
1235 | 0 | goto quit; |
1236 | 0 | } |
1237 | | |
1238 | 0 | if (cstate.ic.root == NULL) { |
1239 | 0 | cstate.ic.root = gen_retblk(&cstate, cstate.snaplen); |
1240 | | |
1241 | | /* |
1242 | | * Catch errors reported by gen_retblk(). |
1243 | | */ |
1244 | 0 | if (cstate.ic.root== NULL) { |
1245 | 0 | rc = PCAP_ERROR; |
1246 | 0 | goto quit; |
1247 | 0 | } |
1248 | 0 | } |
1249 | | |
1250 | 0 | if (optimize && !cstate.no_optimize) { |
1251 | 0 | if (bpf_optimize(&cstate.ic, p->errbuf) == -1) { |
1252 | | /* Failure */ |
1253 | 0 | rc = PCAP_ERROR; |
1254 | 0 | goto quit; |
1255 | 0 | } |
1256 | 0 | if (cstate.ic.root == NULL || |
1257 | 0 | (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) { |
1258 | 0 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1259 | 0 | "expression rejects all packets"); |
1260 | 0 | rc = PCAP_ERROR; |
1261 | 0 | goto quit; |
1262 | 0 | } |
1263 | 0 | } |
1264 | 0 | program->bf_insns = icode_to_fcode(&cstate.ic, |
1265 | 0 | cstate.ic.root, &len, p->errbuf); |
1266 | 0 | if (program->bf_insns == NULL) { |
1267 | | /* Failure */ |
1268 | 0 | rc = PCAP_ERROR; |
1269 | 0 | goto quit; |
1270 | 0 | } |
1271 | 0 | program->bf_len = len; |
1272 | |
|
1273 | 0 | rc = 0; /* We're all okay */ |
1274 | |
|
1275 | 0 | quit: |
1276 | | /* |
1277 | | * Clean up everything for the lexical analyzer. |
1278 | | */ |
1279 | 0 | if (in_buffer != NULL) |
1280 | 0 | pcap__delete_buffer(in_buffer, scanner); |
1281 | 0 | if (scanner != NULL) |
1282 | 0 | pcap_lex_destroy(scanner); |
1283 | | |
1284 | | /* |
1285 | | * Clean up our own allocated memory. |
1286 | | */ |
1287 | 0 | freechunks(&cstate); |
1288 | |
|
1289 | | #ifdef _WIN32 |
1290 | | WSACleanup(); |
1291 | | #endif |
1292 | |
|
1293 | 0 | return (rc); |
1294 | 0 | } |
1295 | | |
1296 | | /* |
1297 | | * entry point for using the compiler with no pcap open |
1298 | | * pass in all the stuff that is needed explicitly instead. |
1299 | | */ |
1300 | | int |
1301 | | pcap_compile_nopcap(int snaplen_arg, int linktype_arg, |
1302 | | struct bpf_program *program, |
1303 | | const char *buf, int optimize, bpf_u_int32 mask) |
1304 | 0 | { |
1305 | 0 | pcap_t *p; |
1306 | 0 | int ret; |
1307 | |
|
1308 | 0 | p = pcap_open_dead(linktype_arg, snaplen_arg); |
1309 | 0 | if (p == NULL) |
1310 | 0 | return (PCAP_ERROR); |
1311 | 0 | ret = pcap_compile(p, program, buf, optimize, mask); |
1312 | 0 | pcap_close(p); |
1313 | 0 | return (ret); |
1314 | 0 | } |
1315 | | |
1316 | | /* |
1317 | | * Clean up a "struct bpf_program" by freeing all the memory allocated |
1318 | | * in it. |
1319 | | */ |
1320 | | void |
1321 | | pcap_freecode(struct bpf_program *program) |
1322 | 3.67k | { |
1323 | 3.67k | program->bf_len = 0; |
1324 | 3.67k | if (program->bf_insns != NULL) { |
1325 | 0 | free((char *)program->bf_insns); |
1326 | 0 | program->bf_insns = NULL; |
1327 | 0 | } |
1328 | 3.67k | } |
1329 | | |
1330 | | /* |
1331 | | * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates |
1332 | | * which of the jt and jf fields has been resolved and which is a pointer |
1333 | | * back to another unresolved block (or nil). At least one of the fields |
1334 | | * in each block is already resolved. |
1335 | | */ |
1336 | | static void |
1337 | | backpatch(struct block *list, struct block *target) |
1338 | 0 | { |
1339 | 0 | struct block *next; |
1340 | |
|
1341 | 0 | while (list) { |
1342 | 0 | if (!list->sense) { |
1343 | 0 | next = JT(list); |
1344 | 0 | JT(list) = target; |
1345 | 0 | } else { |
1346 | 0 | next = JF(list); |
1347 | 0 | JF(list) = target; |
1348 | 0 | } |
1349 | 0 | list = next; |
1350 | 0 | } |
1351 | 0 | } |
1352 | | |
1353 | | /* |
1354 | | * Merge the lists in b0 and b1, using the 'sense' field to indicate |
1355 | | * which of jt and jf is the link. |
1356 | | */ |
1357 | | static void |
1358 | | merge(struct block *b0, struct block *b1) |
1359 | 0 | { |
1360 | 0 | struct block **p = &b0; |
1361 | | |
1362 | | /* Find end of list. */ |
1363 | 0 | while (*p) |
1364 | 0 | p = !((*p)->sense) ? &JT(*p) : &JF(*p); |
1365 | | |
1366 | | /* Concatenate the lists. */ |
1367 | 0 | *p = b1; |
1368 | 0 | } |
1369 | | |
1370 | | int |
1371 | | finish_parse(compiler_state_t *cstate, struct block *p_arg) |
1372 | 0 | { |
1373 | | /* |
1374 | | * Catch errors reported by us and routines below us, and return -1 |
1375 | | * on an error. |
1376 | | */ |
1377 | 0 | if (setjmp(cstate->top_ctx)) |
1378 | 0 | return (-1); |
1379 | | |
1380 | 0 | struct block *p = p_arg; // "might be clobbered by longjmp()" |
1381 | | |
1382 | | /* |
1383 | | * Insert before the statements of the first (root) block any |
1384 | | * statements needed to load the lengths of any variable-length |
1385 | | * headers into registers. |
1386 | | * |
1387 | | * XXX - a fancier strategy would be to insert those before the |
1388 | | * statements of all blocks that use those lengths and that |
1389 | | * have no predecessors that use them, so that we only compute |
1390 | | * the lengths if we need them. There might be even better |
1391 | | * approaches than that. |
1392 | | * |
1393 | | * However, those strategies would be more complicated, and |
1394 | | * as we don't generate code to compute a length if the |
1395 | | * program has no tests that use the length, and as most |
1396 | | * tests will probably use those lengths, we would just |
1397 | | * postpone computing the lengths so that it's not done |
1398 | | * for tests that fail early, and it's not clear that's |
1399 | | * worth the effort. |
1400 | | */ |
1401 | 0 | insert_compute_vloffsets(cstate, p->head); |
1402 | | |
1403 | | /* |
1404 | | * For DLT_PPI captures, generate a check of the per-packet |
1405 | | * DLT value to make sure it's DLT_IEEE802_11. |
1406 | | * |
1407 | | * XXX - TurboCap cards use DLT_PPI for Ethernet. |
1408 | | * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header |
1409 | | * with appropriate Ethernet information and use that rather |
1410 | | * than using something such as DLT_PPI where you don't know |
1411 | | * the link-layer header type until runtime, which, in the |
1412 | | * general case, would force us to generate both Ethernet *and* |
1413 | | * 802.11 code (*and* anything else for which PPI is used) |
1414 | | * and choose between them early in the BPF program? |
1415 | | */ |
1416 | 0 | if (cstate->linktype == DLT_PPI) { |
1417 | 0 | struct block *ppi_dlt_check = gen_cmp(cstate, OR_PACKET, |
1418 | 0 | 4, BPF_W, SWAPLONG(DLT_IEEE802_11)); |
1419 | 0 | p = gen_and(ppi_dlt_check, p); |
1420 | 0 | } |
1421 | |
|
1422 | 0 | backpatch(p, gen_retblk_internal(cstate, cstate->snaplen)); |
1423 | 0 | p->sense = !p->sense; |
1424 | 0 | backpatch(p, gen_retblk_internal(cstate, 0)); |
1425 | 0 | cstate->ic.root = p->head; |
1426 | 0 | return (0); |
1427 | 0 | } |
1428 | | |
1429 | | struct block * |
1430 | | gen_and(struct block *b0, struct block *b1) |
1431 | 0 | { |
1432 | | // False and X is false. |
1433 | 0 | if (b0->meaning == IS_FALSE) |
1434 | 0 | return b0; |
1435 | | // X and false is false. |
1436 | 0 | if (b1->meaning == IS_FALSE) |
1437 | 0 | return b1; |
1438 | | // True and X is X. |
1439 | 0 | if (b0->meaning == IS_TRUE) |
1440 | 0 | return b1; |
1441 | | // X and true is X. |
1442 | 0 | if (b1->meaning == IS_TRUE) |
1443 | 0 | return b0; |
1444 | | |
1445 | | // b0->meaning == IS_UNCERTAIN && b1->meaning == IS_UNCERTAIN |
1446 | 0 | backpatch(b0, b1->head); |
1447 | 0 | b0->sense = !b0->sense; |
1448 | 0 | b1->sense = !b1->sense; |
1449 | 0 | merge(b1, b0); |
1450 | 0 | b1->sense = !b1->sense; |
1451 | 0 | b1->head = b0->head; |
1452 | 0 | return b1; |
1453 | 0 | } |
1454 | | |
1455 | | struct block * |
1456 | | gen_or(struct block *b0, struct block *b1) |
1457 | 0 | { |
1458 | | // False or X is X. |
1459 | 0 | if (b0->meaning == IS_FALSE) |
1460 | 0 | return b1; |
1461 | | // X or false is X. |
1462 | 0 | if (b1->meaning == IS_FALSE) |
1463 | 0 | return b0; |
1464 | | // True or X is true. |
1465 | 0 | if (b0->meaning == IS_TRUE) |
1466 | 0 | return b0; |
1467 | | // X or true is true. |
1468 | 0 | if (b1->meaning == IS_TRUE) |
1469 | 0 | return b1; |
1470 | | |
1471 | | // b0->meaning == IS_UNCERTAIN && b1->meaning == IS_UNCERTAIN |
1472 | 0 | b0->sense = !b0->sense; |
1473 | 0 | backpatch(b0, b1->head); |
1474 | 0 | b0->sense = !b0->sense; |
1475 | 0 | merge(b1, b0); |
1476 | 0 | b1->head = b0->head; |
1477 | 0 | return b1; |
1478 | 0 | } |
1479 | | |
1480 | | struct block * |
1481 | | gen_not(struct block *b) |
1482 | 0 | { |
1483 | 0 | b->sense = !b->sense; |
1484 | | // A switch on an enum is a source of compiler warnings. |
1485 | 0 | if (b->meaning == IS_TRUE) |
1486 | 0 | b->meaning = IS_FALSE; |
1487 | 0 | else if (b->meaning == IS_FALSE) |
1488 | 0 | b->meaning = IS_TRUE; |
1489 | 0 | return b; |
1490 | 0 | } |
1491 | | |
1492 | | static struct block * |
1493 | | gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1494 | | u_int size, bpf_u_int32 v) |
1495 | 0 | { |
1496 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v); |
1497 | 0 | } |
1498 | | |
1499 | | static struct block * |
1500 | | gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1501 | | u_int size, bpf_u_int32 v) |
1502 | 0 | { |
1503 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v); |
1504 | 0 | } |
1505 | | |
1506 | | static struct block * |
1507 | | gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1508 | | u_int size, bpf_u_int32 v) |
1509 | 0 | { |
1510 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v); |
1511 | 0 | } |
1512 | | |
1513 | | static struct block * |
1514 | | gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1515 | | u_int size, bpf_u_int32 v) |
1516 | 0 | { |
1517 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v); |
1518 | 0 | } |
1519 | | |
1520 | | static struct block * |
1521 | | gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1522 | | u_int size, bpf_u_int32 v) |
1523 | 0 | { |
1524 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v); |
1525 | 0 | } |
1526 | | |
1527 | | static struct block * |
1528 | | gen_cmp_ne(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1529 | | u_int size, bpf_u_int32 v) |
1530 | 0 | { |
1531 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 1, v); |
1532 | 0 | } |
1533 | | |
1534 | | static struct block * |
1535 | | gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1536 | | u_int size, bpf_u_int32 v, bpf_u_int32 mask) |
1537 | 0 | { |
1538 | | /* |
1539 | | * For any A: if mask == 0, it means A & mask == 0, so the result is |
1540 | | * true iff v == 0. In this case ideally the caller should have |
1541 | | * skipped this invocation and have fewer statement blocks to juggle. |
1542 | | * If the caller could have skipped, but has not, produce a block with |
1543 | | * fewer statements. |
1544 | | * |
1545 | | * This could be done in gen_ncmp() in a more generic way, but this |
1546 | | * function is the only code path that can have mask == 0. |
1547 | | */ |
1548 | 0 | if (mask == 0) |
1549 | 0 | return v ? gen_false(cstate) : gen_true(cstate); |
1550 | | |
1551 | 0 | return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v); |
1552 | 0 | } |
1553 | | |
1554 | | static struct block * |
1555 | | gen_mcmp_ne(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1556 | | u_int size, bpf_u_int32 v, bpf_u_int32 mask) |
1557 | 0 | { |
1558 | 0 | return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 1, v); |
1559 | 0 | } |
1560 | | |
1561 | | static struct block * |
1562 | | gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1563 | | u_int size, const u_char *v) |
1564 | 0 | { |
1565 | 0 | struct block *b, *tmp; |
1566 | |
|
1567 | 0 | b = NULL; |
1568 | 0 | while (size >= 4) { |
1569 | 0 | const u_char *p = &v[size - 4]; |
1570 | |
|
1571 | 0 | tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, |
1572 | 0 | EXTRACT_BE_U_4(p)); |
1573 | 0 | if (b != NULL) |
1574 | 0 | tmp = gen_and(b, tmp); |
1575 | 0 | b = tmp; |
1576 | 0 | size -= 4; |
1577 | 0 | } |
1578 | 0 | while (size >= 2) { |
1579 | 0 | const u_char *p = &v[size - 2]; |
1580 | |
|
1581 | 0 | tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, |
1582 | 0 | EXTRACT_BE_U_2(p)); |
1583 | 0 | if (b != NULL) |
1584 | 0 | tmp = gen_and(b, tmp); |
1585 | 0 | b = tmp; |
1586 | 0 | size -= 2; |
1587 | 0 | } |
1588 | 0 | if (size > 0) { |
1589 | 0 | tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]); |
1590 | 0 | if (b != NULL) |
1591 | 0 | tmp = gen_and(b, tmp); |
1592 | 0 | b = tmp; |
1593 | 0 | } |
1594 | 0 | return b; |
1595 | 0 | } |
1596 | | |
1597 | | /* |
1598 | | * Generate an instruction block for one of {"jeq #k", "jgt #k", "jge #k", |
1599 | | * "jset #k", "ja L"}. |
1600 | | */ |
1601 | | static struct block * |
1602 | | gen_jmp_k(compiler_state_t *cstate, const int jtype, const bpf_u_int32 v, |
1603 | | struct slist *stmts) |
1604 | 0 | { |
1605 | 0 | struct block *b = new_block(cstate, JMP(jtype, BPF_K)); |
1606 | 0 | b->s.k = v; |
1607 | 0 | b->stmts = stmts; |
1608 | 0 | return b; |
1609 | 0 | } |
1610 | | |
1611 | | /* |
1612 | | * Generate an instruction block for one of {"jeq x", "jgt x", "jge x", |
1613 | | * "jset x"}. |
1614 | | */ |
1615 | | static struct block * |
1616 | | gen_jmp_x(compiler_state_t *cstate, const int jtype, struct slist *stmts) |
1617 | 0 | { |
1618 | 0 | struct block *b = new_block(cstate, JMP(jtype, BPF_X)); |
1619 | 0 | b->stmts = stmts; |
1620 | 0 | return b; |
1621 | 0 | } |
1622 | | |
1623 | | static struct block * |
1624 | | gen_set(compiler_state_t *cstate, bpf_u_int32 v, struct slist *stmts) |
1625 | 0 | { |
1626 | 0 | return gen_jmp_k(cstate, BPF_JSET, v, stmts); |
1627 | 0 | } |
1628 | | |
1629 | | static struct block * |
1630 | | gen_unset(compiler_state_t *cstate, bpf_u_int32 v, struct slist *stmts) |
1631 | 0 | { |
1632 | 0 | return gen_not(gen_set(cstate, v, stmts)); |
1633 | 0 | } |
1634 | | |
1635 | | /* |
1636 | | * AND the field of size "size" at offset "offset" relative to the header |
1637 | | * specified by "offrel" with "mask", and compare it with the value "v" |
1638 | | * with the test specified by "jtype"; if "reverse" is true, the test |
1639 | | * should test the opposite of "jtype". |
1640 | | */ |
1641 | | static struct block * |
1642 | | gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1643 | | u_int size, bpf_u_int32 mask, int jtype, int reverse, |
1644 | | bpf_u_int32 v) |
1645 | 0 | { |
1646 | 0 | struct slist *s, *s2; |
1647 | 0 | struct block *b; |
1648 | |
|
1649 | 0 | s = gen_load_a(cstate, offrel, offset, size); |
1650 | |
|
1651 | 0 | if (mask != 0xffffffff) { |
1652 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
1653 | 0 | s2->s.k = mask; |
1654 | 0 | sappend(s, s2); |
1655 | 0 | } |
1656 | |
|
1657 | 0 | b = gen_jmp_k(cstate, jtype, v, s); |
1658 | 0 | return reverse ? gen_not(b) : b; |
1659 | 0 | } |
1660 | | |
1661 | | static int |
1662 | | init_linktype(compiler_state_t *cstate, pcap_t *p) |
1663 | 0 | { |
1664 | 0 | cstate->pcap_fddipad = p->fddipad; |
1665 | | |
1666 | | /* |
1667 | | * We start out with only one link-layer header. |
1668 | | */ |
1669 | 0 | cstate->outermostlinktype = pcap_datalink(p); |
1670 | 0 | cstate->off_outermostlinkhdr.constant_part = 0; |
1671 | 0 | cstate->off_outermostlinkhdr.is_variable = 0; |
1672 | 0 | cstate->off_outermostlinkhdr.reg = -1; |
1673 | |
|
1674 | 0 | cstate->prevlinktype = cstate->outermostlinktype; |
1675 | 0 | cstate->off_prevlinkhdr.constant_part = 0; |
1676 | 0 | cstate->off_prevlinkhdr.is_variable = 0; |
1677 | 0 | cstate->off_prevlinkhdr.reg = -1; |
1678 | |
|
1679 | 0 | cstate->linktype = cstate->outermostlinktype; |
1680 | 0 | cstate->off_linkhdr.constant_part = 0; |
1681 | 0 | cstate->off_linkhdr.is_variable = 0; |
1682 | 0 | cstate->off_linkhdr.reg = -1; |
1683 | | |
1684 | | /* |
1685 | | * XXX |
1686 | | */ |
1687 | 0 | cstate->off_linkpl.constant_part = 0; |
1688 | 0 | cstate->off_linkpl.is_variable = 0; |
1689 | 0 | cstate->off_linkpl.reg = -1; |
1690 | |
|
1691 | 0 | cstate->off_linktype.constant_part = 0; |
1692 | 0 | cstate->off_linktype.is_variable = 0; |
1693 | 0 | cstate->off_linktype.reg = -1; |
1694 | | |
1695 | | /* |
1696 | | * Assume it's not raw ATM with a pseudo-header, for now. |
1697 | | */ |
1698 | 0 | cstate->is_atm = 0; |
1699 | 0 | cstate->off_vpi = OFFSET_NOT_SET; |
1700 | 0 | cstate->off_vci = OFFSET_NOT_SET; |
1701 | 0 | cstate->off_proto = OFFSET_NOT_SET; |
1702 | 0 | cstate->off_payload = OFFSET_NOT_SET; |
1703 | | |
1704 | | /* |
1705 | | * And not encapsulated with either Geneve or VXLAN. |
1706 | | */ |
1707 | 0 | cstate->is_encap = 0; |
1708 | | |
1709 | | /* |
1710 | | * No variable length VLAN offset by default |
1711 | | */ |
1712 | 0 | cstate->is_vlan_vloffset = 0; |
1713 | | |
1714 | | /* |
1715 | | * And assume we're not doing SS7. |
1716 | | */ |
1717 | 0 | cstate->off_li = OFFSET_NOT_SET; |
1718 | 0 | cstate->off_li_hsl = OFFSET_NOT_SET; |
1719 | 0 | cstate->off_sio = OFFSET_NOT_SET; |
1720 | 0 | cstate->off_opc = OFFSET_NOT_SET; |
1721 | 0 | cstate->off_dpc = OFFSET_NOT_SET; |
1722 | 0 | cstate->off_sls = OFFSET_NOT_SET; |
1723 | |
|
1724 | 0 | cstate->label_stack_depth = 0; |
1725 | 0 | cstate->vlan_stack_depth = 0; |
1726 | |
|
1727 | 0 | switch (cstate->linktype) { |
1728 | | |
1729 | 0 | case DLT_ARCNET: |
1730 | 0 | cstate->off_linktype.constant_part = 2; |
1731 | 0 | cstate->off_linkpl.constant_part = 6; |
1732 | 0 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1733 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1734 | 0 | break; |
1735 | | |
1736 | 0 | case DLT_ARCNET_LINUX: |
1737 | 0 | cstate->off_linktype.constant_part = 4; |
1738 | 0 | cstate->off_linkpl.constant_part = 8; |
1739 | 0 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1740 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1741 | 0 | break; |
1742 | | |
1743 | 0 | case DLT_EN10MB: |
1744 | 0 | cstate->off_linktype.constant_part = 12; |
1745 | 0 | cstate->off_linkpl.constant_part = 14; /* Ethernet header length */ |
1746 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
1747 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
1748 | 0 | break; |
1749 | | |
1750 | 0 | case DLT_SLIP: |
1751 | | /* |
1752 | | * SLIP doesn't have a link level type. The 16 byte |
1753 | | * header is hacked into our SLIP driver. |
1754 | | */ |
1755 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1756 | 0 | cstate->off_linkpl.constant_part = 16; |
1757 | 0 | cstate->off_nl = 0; |
1758 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1759 | 0 | break; |
1760 | | |
1761 | 0 | case DLT_SLIP_BSDOS: |
1762 | | /* XXX this may be the same as the DLT_PPP_BSDOS case */ |
1763 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1764 | | /* XXX end */ |
1765 | 0 | cstate->off_linkpl.constant_part = 24; |
1766 | 0 | cstate->off_nl = 0; |
1767 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1768 | 0 | break; |
1769 | | |
1770 | 0 | case DLT_NULL: |
1771 | 0 | case DLT_LOOP: |
1772 | 0 | cstate->off_linktype.constant_part = 0; |
1773 | 0 | cstate->off_linkpl.constant_part = 4; |
1774 | 0 | cstate->off_nl = 0; |
1775 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1776 | 0 | break; |
1777 | | |
1778 | 0 | case DLT_ENC: |
1779 | 0 | cstate->off_linktype.constant_part = 0; |
1780 | 0 | cstate->off_linkpl.constant_part = 12; |
1781 | 0 | cstate->off_nl = 0; |
1782 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1783 | 0 | break; |
1784 | | |
1785 | 0 | case DLT_PPP: |
1786 | 0 | case DLT_PPP_PPPD: |
1787 | 0 | case DLT_C_HDLC: /* BSD/OS Cisco HDLC */ |
1788 | 0 | case DLT_HDLC: /* NetBSD (Cisco) HDLC */ |
1789 | 0 | case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */ |
1790 | 0 | cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */ |
1791 | 0 | cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */ |
1792 | 0 | cstate->off_nl = 0; |
1793 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1794 | 0 | break; |
1795 | | |
1796 | 0 | case DLT_PPP_ETHER: |
1797 | | /* |
1798 | | * This does not include the Ethernet header, and |
1799 | | * only covers session state. |
1800 | | */ |
1801 | 0 | cstate->off_linktype.constant_part = 6; |
1802 | 0 | cstate->off_linkpl.constant_part = 8; |
1803 | 0 | cstate->off_nl = 0; |
1804 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1805 | 0 | break; |
1806 | | |
1807 | 0 | case DLT_PPP_BSDOS: |
1808 | 0 | cstate->off_linktype.constant_part = 5; |
1809 | 0 | cstate->off_linkpl.constant_part = 24; |
1810 | 0 | cstate->off_nl = 0; |
1811 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1812 | 0 | break; |
1813 | | |
1814 | 0 | case DLT_FDDI: |
1815 | | /* |
1816 | | * FDDI doesn't really have a link-level type field. |
1817 | | * We set "off_linktype" to the offset of the LLC header. |
1818 | | * |
1819 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1820 | | * is being used and pick out the encapsulated Ethernet type. |
1821 | | * XXX - should we generate code to check for SNAP? |
1822 | | */ |
1823 | 0 | cstate->off_linktype.constant_part = 13; |
1824 | 0 | cstate->off_linktype.constant_part += cstate->pcap_fddipad; |
1825 | 0 | cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */ |
1826 | 0 | cstate->off_linkpl.constant_part += cstate->pcap_fddipad; |
1827 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1828 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1829 | 0 | break; |
1830 | | |
1831 | 0 | case DLT_IEEE802: |
1832 | | /* |
1833 | | * Token Ring doesn't really have a link-level type field. |
1834 | | * We set "off_linktype" to the offset of the LLC header. |
1835 | | * |
1836 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1837 | | * is being used and pick out the encapsulated Ethernet type. |
1838 | | * XXX - should we generate code to check for SNAP? |
1839 | | * |
1840 | | * XXX - the header is actually variable-length. |
1841 | | * Some various Linux patched versions gave 38 |
1842 | | * as "off_linktype" and 40 as "off_nl"; however, |
1843 | | * if a token ring packet has *no* routing |
1844 | | * information, i.e. is not source-routed, the correct |
1845 | | * values are 20 and 22, as they are in the vanilla code. |
1846 | | * |
1847 | | * A packet is source-routed iff the uppermost bit |
1848 | | * of the first byte of the source address, at an |
1849 | | * offset of 8, has the uppermost bit set. If the |
1850 | | * packet is source-routed, the total number of bytes |
1851 | | * of routing information is 2 plus bits 0x1F00 of |
1852 | | * the 16-bit value at an offset of 14 (shifted right |
1853 | | * 8 - figure out which byte that is). |
1854 | | */ |
1855 | 0 | cstate->off_linktype.constant_part = 14; |
1856 | 0 | cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */ |
1857 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1858 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1859 | 0 | break; |
1860 | | |
1861 | 0 | case DLT_PRISM_HEADER: |
1862 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
1863 | 0 | case DLT_IEEE802_11_RADIO: |
1864 | 0 | cstate->off_linkhdr.is_variable = 1; |
1865 | | /* Fall through, 802.11 doesn't have a variable link |
1866 | | * prefix but is otherwise the same. */ |
1867 | | /* FALLTHROUGH */ |
1868 | |
|
1869 | 0 | case DLT_IEEE802_11: |
1870 | | /* |
1871 | | * 802.11 doesn't really have a link-level type field. |
1872 | | * We set "off_linktype.constant_part" to the offset of |
1873 | | * the LLC header. |
1874 | | * |
1875 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1876 | | * is being used and pick out the encapsulated Ethernet type. |
1877 | | * XXX - should we generate code to check for SNAP? |
1878 | | * |
1879 | | * We also handle variable-length radio headers here. |
1880 | | * The Prism header is in theory variable-length, but in |
1881 | | * practice it's always 144 bytes long. However, some |
1882 | | * drivers on Linux use ARPHRD_IEEE80211_PRISM, but |
1883 | | * sometimes or always supply an AVS header, so we |
1884 | | * have to check whether the radio header is a Prism |
1885 | | * header or an AVS header, so, in practice, it's |
1886 | | * variable-length. |
1887 | | */ |
1888 | 0 | cstate->off_linktype.constant_part = 24; |
1889 | 0 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1890 | 0 | cstate->off_linkpl.is_variable = 1; |
1891 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1892 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1893 | 0 | break; |
1894 | | |
1895 | 0 | case DLT_PPI: |
1896 | | /* |
1897 | | * At the moment we treat PPI the same way that we treat |
1898 | | * normal Radiotap encoded packets. The difference is in |
1899 | | * the function that generates the code at the beginning |
1900 | | * to compute the header length. Since this code generator |
1901 | | * of PPI supports bare 802.11 encapsulation only (i.e. |
1902 | | * the encapsulated DLT should be DLT_IEEE802_11) we |
1903 | | * generate code to check for this too. |
1904 | | */ |
1905 | 0 | cstate->off_linktype.constant_part = 24; |
1906 | 0 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1907 | 0 | cstate->off_linkpl.is_variable = 1; |
1908 | 0 | cstate->off_linkhdr.is_variable = 1; |
1909 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1910 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1911 | 0 | break; |
1912 | | |
1913 | 0 | case DLT_ATM_RFC1483: |
1914 | 0 | case DLT_ATM_CLIP: /* Linux ATM defines this */ |
1915 | | /* |
1916 | | * assume routed, non-ISO PDUs |
1917 | | * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) |
1918 | | * |
1919 | | * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS, |
1920 | | * or PPP with the PPP NLPID (e.g., PPPoA)? The |
1921 | | * latter would presumably be treated the way PPPoE |
1922 | | * should be, so you can do "pppoe and udp port 2049" |
1923 | | * or "pppoa and tcp port 80" and have it check for |
1924 | | * PPPo{A,E} and a PPP protocol of IP and.... |
1925 | | */ |
1926 | 0 | cstate->off_linktype.constant_part = 0; |
1927 | 0 | cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */ |
1928 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1929 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1930 | 0 | break; |
1931 | | |
1932 | 0 | case DLT_SUNATM: |
1933 | | /* |
1934 | | * Full Frontal ATM; you get AALn PDUs with an ATM |
1935 | | * pseudo-header. |
1936 | | */ |
1937 | 0 | cstate->is_atm = 1; |
1938 | 0 | cstate->off_vpi = SUNATM_VPI_POS; |
1939 | 0 | cstate->off_vci = SUNATM_VCI_POS; |
1940 | 0 | cstate->off_proto = PROTO_POS; |
1941 | 0 | cstate->off_payload = SUNATM_PKT_BEGIN_POS; |
1942 | 0 | cstate->off_linktype.constant_part = cstate->off_payload; |
1943 | 0 | cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */ |
1944 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1945 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1946 | 0 | break; |
1947 | | |
1948 | 0 | case DLT_RAW: |
1949 | 0 | case DLT_IPV4: |
1950 | 0 | case DLT_IPV6: |
1951 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1952 | 0 | cstate->off_linkpl.constant_part = 0; |
1953 | 0 | cstate->off_nl = 0; |
1954 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1955 | 0 | break; |
1956 | | |
1957 | 0 | case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */ |
1958 | 0 | cstate->off_linktype.constant_part = 14; |
1959 | 0 | cstate->off_linkpl.constant_part = 16; |
1960 | 0 | cstate->off_nl = 0; |
1961 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1962 | 0 | break; |
1963 | | |
1964 | 0 | case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */ |
1965 | 0 | cstate->off_linktype.constant_part = 0; |
1966 | 0 | cstate->off_linkpl.constant_part = 20; |
1967 | 0 | cstate->off_nl = 0; |
1968 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1969 | 0 | break; |
1970 | | |
1971 | 0 | case DLT_LTALK: |
1972 | | /* |
1973 | | * LocalTalk does have a 1-byte type field in the LLAP header, |
1974 | | * but really it just indicates whether there is a "short" or |
1975 | | * "long" DDP packet following. |
1976 | | */ |
1977 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1978 | 0 | cstate->off_linkpl.constant_part = 0; |
1979 | 0 | cstate->off_nl = 0; |
1980 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1981 | 0 | break; |
1982 | | |
1983 | 0 | case DLT_IP_OVER_FC: |
1984 | | /* |
1985 | | * RFC 2625 IP-over-Fibre-Channel doesn't really have a |
1986 | | * link-level type field. We set "off_linktype" to the |
1987 | | * offset of the LLC header. |
1988 | | * |
1989 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1990 | | * is being used and pick out the encapsulated Ethernet type. |
1991 | | * XXX - should we generate code to check for SNAP? RFC |
1992 | | * 2625 says SNAP should be used. |
1993 | | */ |
1994 | 0 | cstate->off_linktype.constant_part = 16; |
1995 | 0 | cstate->off_linkpl.constant_part = 16; |
1996 | 0 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1997 | 0 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1998 | 0 | break; |
1999 | | |
2000 | 0 | case DLT_FRELAY: |
2001 | | /* |
2002 | | * XXX - we should set this to handle SNAP-encapsulated |
2003 | | * frames (NLPID of 0x80). |
2004 | | */ |
2005 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2006 | 0 | cstate->off_linkpl.constant_part = 0; |
2007 | 0 | cstate->off_nl = 0; |
2008 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
2009 | 0 | break; |
2010 | | |
2011 | | /* |
2012 | | * the only BPF-interesting FRF.16 frames are non-control frames; |
2013 | | * Frame Relay has a variable length link-layer |
2014 | | * so lets start with offset 4 for now and increments later on (FIXME); |
2015 | | */ |
2016 | 0 | case DLT_MFR: |
2017 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2018 | 0 | cstate->off_linkpl.constant_part = 0; |
2019 | 0 | cstate->off_nl = 4; |
2020 | 0 | cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */ |
2021 | 0 | break; |
2022 | | |
2023 | 0 | case DLT_APPLE_IP_OVER_IEEE1394: |
2024 | 0 | cstate->off_linktype.constant_part = 16; |
2025 | 0 | cstate->off_linkpl.constant_part = 18; |
2026 | 0 | cstate->off_nl = 0; |
2027 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
2028 | 0 | break; |
2029 | | |
2030 | 0 | case DLT_SYMANTEC_FIREWALL: |
2031 | 0 | cstate->off_linktype.constant_part = 6; |
2032 | 0 | cstate->off_linkpl.constant_part = 44; |
2033 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
2034 | 0 | cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */ |
2035 | 0 | break; |
2036 | | |
2037 | 0 | case DLT_PFLOG: |
2038 | 0 | cstate->off_linktype.constant_part = 0; |
2039 | 0 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
2040 | 0 | cstate->off_linkpl.is_variable = 1; |
2041 | 0 | cstate->off_nl = 0; |
2042 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
2043 | 0 | break; |
2044 | | |
2045 | 0 | case DLT_JUNIPER_MFR: |
2046 | 0 | case DLT_JUNIPER_MLFR: |
2047 | 0 | case DLT_JUNIPER_MLPPP: |
2048 | 0 | case DLT_JUNIPER_PPP: |
2049 | 0 | case DLT_JUNIPER_CHDLC: |
2050 | 0 | case DLT_JUNIPER_FRELAY: |
2051 | 0 | cstate->off_linktype.constant_part = 4; |
2052 | 0 | cstate->off_linkpl.constant_part = 4; |
2053 | 0 | cstate->off_nl = 0; |
2054 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2055 | 0 | break; |
2056 | | |
2057 | 0 | case DLT_JUNIPER_ATM1: |
2058 | 0 | cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */ |
2059 | 0 | cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */ |
2060 | 0 | cstate->off_nl = 0; |
2061 | 0 | cstate->off_nl_nosnap = 10; |
2062 | 0 | break; |
2063 | | |
2064 | 0 | case DLT_JUNIPER_ATM2: |
2065 | 0 | cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */ |
2066 | 0 | cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */ |
2067 | 0 | cstate->off_nl = 0; |
2068 | 0 | cstate->off_nl_nosnap = 10; |
2069 | 0 | break; |
2070 | | |
2071 | | /* frames captured on a Juniper PPPoE service PIC |
2072 | | * contain raw ethernet frames */ |
2073 | 0 | case DLT_JUNIPER_PPPOE: |
2074 | 0 | case DLT_JUNIPER_ETHER: |
2075 | 0 | cstate->off_linkpl.constant_part = 14; |
2076 | 0 | cstate->off_linktype.constant_part = 16; |
2077 | 0 | cstate->off_nl = 18; /* Ethernet II */ |
2078 | 0 | cstate->off_nl_nosnap = 21; /* 802.3+802.2 */ |
2079 | 0 | break; |
2080 | | |
2081 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
2082 | 0 | cstate->off_linktype.constant_part = 4; |
2083 | 0 | cstate->off_linkpl.constant_part = 6; |
2084 | 0 | cstate->off_nl = 0; |
2085 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2086 | 0 | break; |
2087 | | |
2088 | 0 | case DLT_JUNIPER_GGSN: |
2089 | 0 | cstate->off_linktype.constant_part = 6; |
2090 | 0 | cstate->off_linkpl.constant_part = 12; |
2091 | 0 | cstate->off_nl = 0; |
2092 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2093 | 0 | break; |
2094 | | |
2095 | 0 | case DLT_JUNIPER_ES: |
2096 | 0 | cstate->off_linktype.constant_part = 6; |
2097 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
2098 | 0 | cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
2099 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2100 | 0 | break; |
2101 | | |
2102 | 0 | case DLT_JUNIPER_MONITOR: |
2103 | 0 | cstate->off_linktype.constant_part = 12; |
2104 | 0 | cstate->off_linkpl.constant_part = 12; |
2105 | 0 | cstate->off_nl = 0; /* raw IP/IP6 header */ |
2106 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2107 | 0 | break; |
2108 | | |
2109 | 0 | case DLT_JUNIPER_SERVICES: |
2110 | 0 | cstate->off_linktype.constant_part = 12; |
2111 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
2112 | 0 | cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
2113 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2114 | 0 | break; |
2115 | | |
2116 | 0 | case DLT_JUNIPER_VP: |
2117 | 0 | cstate->off_linktype.constant_part = 18; |
2118 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2119 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2120 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2121 | 0 | break; |
2122 | | |
2123 | 0 | case DLT_JUNIPER_ST: |
2124 | 0 | cstate->off_linktype.constant_part = 18; |
2125 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2126 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2127 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2128 | 0 | break; |
2129 | | |
2130 | 0 | case DLT_JUNIPER_ISM: |
2131 | 0 | cstate->off_linktype.constant_part = 8; |
2132 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2133 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2134 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2135 | 0 | break; |
2136 | | |
2137 | 0 | case DLT_JUNIPER_VS: |
2138 | 0 | case DLT_JUNIPER_SRX_E2E: |
2139 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
2140 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
2141 | 0 | cstate->off_linktype.constant_part = 8; |
2142 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2143 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2144 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2145 | 0 | break; |
2146 | | |
2147 | 0 | case DLT_MTP2: |
2148 | 0 | cstate->off_li = 2; |
2149 | 0 | cstate->off_li_hsl = 4; |
2150 | 0 | cstate->off_sio = 3; |
2151 | 0 | cstate->off_opc = 4; |
2152 | 0 | cstate->off_dpc = 4; |
2153 | 0 | cstate->off_sls = 7; |
2154 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2155 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2156 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2157 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2158 | 0 | break; |
2159 | | |
2160 | 0 | case DLT_MTP2_WITH_PHDR: |
2161 | 0 | cstate->off_li = 6; |
2162 | 0 | cstate->off_li_hsl = 8; |
2163 | 0 | cstate->off_sio = 7; |
2164 | 0 | cstate->off_opc = 8; |
2165 | 0 | cstate->off_dpc = 8; |
2166 | 0 | cstate->off_sls = 11; |
2167 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2168 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2169 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2170 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2171 | 0 | break; |
2172 | | |
2173 | 0 | case DLT_ERF: |
2174 | 0 | cstate->off_li = 22; |
2175 | 0 | cstate->off_li_hsl = 24; |
2176 | 0 | cstate->off_sio = 23; |
2177 | 0 | cstate->off_opc = 24; |
2178 | 0 | cstate->off_dpc = 24; |
2179 | 0 | cstate->off_sls = 27; |
2180 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2181 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2182 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2183 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2184 | 0 | break; |
2185 | | |
2186 | 0 | case DLT_PFSYNC: |
2187 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2188 | 0 | cstate->off_linkpl.constant_part = 4; |
2189 | 0 | cstate->off_nl = 0; |
2190 | 0 | cstate->off_nl_nosnap = 0; |
2191 | 0 | break; |
2192 | | |
2193 | 0 | case DLT_IPNET: |
2194 | 0 | cstate->off_linktype.constant_part = 1; |
2195 | 0 | cstate->off_linkpl.constant_part = 24; /* ipnet header length */ |
2196 | 0 | cstate->off_nl = 0; |
2197 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2198 | 0 | break; |
2199 | | |
2200 | 0 | case DLT_NETANALYZER: |
2201 | 0 | cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */ |
2202 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
2203 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */ |
2204 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
2205 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
2206 | 0 | break; |
2207 | | |
2208 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
2209 | 0 | cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */ |
2210 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
2211 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */ |
2212 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
2213 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
2214 | 0 | break; |
2215 | | |
2216 | 0 | case DLT_DSA_TAG_BRCM: |
2217 | 0 | cstate->off_linktype.constant_part = 6 + 6 + 4; // dst, src, DSA tag |
2218 | 0 | cstate->off_linkpl.constant_part = cstate->off_linktype.constant_part + 2; // idem + EtherType |
2219 | 0 | cstate->off_nl = 0; // Ethernet II |
2220 | 0 | cstate->off_nl_nosnap = 3; // 802.3+802.2 |
2221 | 0 | break; |
2222 | | |
2223 | 0 | case DLT_DSA_TAG_DSA: |
2224 | 0 | cstate->off_linktype.constant_part = 6 + 6 + 4; // dst, src, DSA tag |
2225 | 0 | cstate->off_linkpl.constant_part = cstate->off_linktype.constant_part + 2; // idem + EtherType |
2226 | 0 | cstate->off_nl = 0; // Ethernet II |
2227 | 0 | cstate->off_nl_nosnap = 3; // 802.3+802.2 |
2228 | 0 | break; |
2229 | | |
2230 | 0 | case DLT_EN3MB: |
2231 | 0 | case DLT_AX25: |
2232 | 0 | case DLT_PRONET: |
2233 | 0 | case DLT_CHAOS: |
2234 | | #ifdef DLT_HIPPI |
2235 | | case DLT_HIPPI: |
2236 | | #endif |
2237 | 0 | case DLT_REDBACK_SMARTEDGE: |
2238 | 0 | #ifdef DLT_HHDLC |
2239 | 0 | case DLT_HHDLC: |
2240 | 0 | #endif |
2241 | | /* |
2242 | | * Currently, only raw "link[N:M]" filtering is supported. |
2243 | | */ |
2244 | 0 | case DLT_AX25_KISS: |
2245 | | /* |
2246 | | * Idem, plus the initial code for AX.25 KISS commented: |
2247 | | * |
2248 | | * - "variable, min 15, max 71 steps of 7" about off_linktype |
2249 | | * - "variable, min 16, max 71 steps of 7" about off_nl |
2250 | | * |
2251 | | * It is not clear how that relates with the AX.25 and KISS |
2252 | | * specifications, also there is a possibility of Linux kernel |
2253 | | * modifying the packet type and/or structure. So if anybody |
2254 | | * would like to implement a better filtering support for this |
2255 | | * DLT, it would be a good idea to verify and to document all |
2256 | | * particulars of the encoding first. |
2257 | | */ |
2258 | 0 | case DLT_BACNET_MS_TP: |
2259 | | /* |
2260 | | * This DLT supports a few primitives besides "link[N:M]", but |
2261 | | * "link proto", whether explicit or implicit, is not one of |
2262 | | * these. |
2263 | | * |
2264 | | * The third octet of an MS/TP frame is Frame Type, but it is |
2265 | | * the MS/TP frame type [0..7] rather than a network protocol |
2266 | | * type. It can be tested using "link[2]". If in future it |
2267 | | * becomes necessary to have a solution that matches the |
2268 | | * problem space better, it would need to be a new special |
2269 | | * primitive that works on MS/TP DLT(s) only and takes names |
2270 | | * for the types, for example, "ms-tp type token". |
2271 | | */ |
2272 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2273 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2274 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2275 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2276 | 0 | break; |
2277 | | |
2278 | 0 | default: |
2279 | | /* |
2280 | | * For values in the range in which we've assigned new |
2281 | | * DLT_ values, only raw "link[N:M]" filtering is supported. |
2282 | | */ |
2283 | 0 | if (cstate->linktype >= DLT_HIGH_MATCHING_MIN && |
2284 | 0 | cstate->linktype <= DLT_HIGH_MATCHING_MAX) { |
2285 | 0 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2286 | 0 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2287 | 0 | cstate->off_nl = OFFSET_NOT_SET; |
2288 | 0 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2289 | 0 | } else { |
2290 | 0 | bpf_set_error(cstate, "unknown data link type %d", |
2291 | 0 | cstate->linktype); |
2292 | 0 | return (-1); |
2293 | 0 | } |
2294 | 0 | break; |
2295 | 0 | } |
2296 | | |
2297 | 0 | cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr; |
2298 | 0 | return (0); |
2299 | 0 | } |
2300 | | |
2301 | | /* |
2302 | | * Load a value relative to the specified absolute offset. |
2303 | | */ |
2304 | | static struct slist * |
2305 | | gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset, |
2306 | | u_int offset, u_int size) |
2307 | 0 | { |
2308 | 0 | struct slist *s, *s2; |
2309 | |
|
2310 | 0 | s = gen_abs_offset_varpart(cstate, abs_offset); |
2311 | | |
2312 | | /* |
2313 | | * If "s" is non-null, it has code to arrange that the X register |
2314 | | * contains the variable part of the absolute offset, so we |
2315 | | * generate a load relative to that, with an offset of |
2316 | | * abs_offset->constant_part + offset. |
2317 | | * |
2318 | | * Otherwise, we can do an absolute load with an offset of |
2319 | | * abs_offset->constant_part + offset. |
2320 | | */ |
2321 | 0 | if (s != NULL) { |
2322 | | /* |
2323 | | * "s" points to a list of statements that puts the |
2324 | | * variable part of the absolute offset into the X register. |
2325 | | * Do an indirect load, to use the X register as an offset. |
2326 | | */ |
2327 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
2328 | 0 | s2->s.k = abs_offset->constant_part + offset; |
2329 | 0 | sappend(s, s2); |
2330 | 0 | } else { |
2331 | | /* |
2332 | | * There is no variable part of the absolute offset, so |
2333 | | * just do an absolute load. |
2334 | | */ |
2335 | 0 | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
2336 | 0 | s->s.k = abs_offset->constant_part + offset; |
2337 | 0 | } |
2338 | 0 | return s; |
2339 | 0 | } |
2340 | | |
2341 | | /* |
2342 | | * Load a value relative to the beginning of the specified header. |
2343 | | */ |
2344 | | static struct slist * |
2345 | | gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
2346 | | u_int size) |
2347 | 0 | { |
2348 | 0 | struct slist *s, *s2; |
2349 | | |
2350 | | /* |
2351 | | * Squelch warnings from compilers that *don't* assume that |
2352 | | * offrel always has a valid enum value and therefore don't |
2353 | | * assume that we'll always go through one of the case arms. |
2354 | | * |
2355 | | * If we have a default case, compilers that *do* assume that |
2356 | | * will then complain about the default case code being |
2357 | | * unreachable. |
2358 | | * |
2359 | | * Damned if you do, damned if you don't. |
2360 | | */ |
2361 | 0 | s = NULL; |
2362 | |
|
2363 | 0 | switch (offrel) { |
2364 | | |
2365 | 0 | case OR_PACKET: |
2366 | 0 | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
2367 | 0 | s->s.k = offset; |
2368 | 0 | break; |
2369 | | |
2370 | 0 | case OR_LINKHDR: |
2371 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size); |
2372 | 0 | break; |
2373 | | |
2374 | 0 | case OR_PREVLINKHDR: |
2375 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size); |
2376 | 0 | break; |
2377 | | |
2378 | 0 | case OR_LLC: |
2379 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size); |
2380 | 0 | break; |
2381 | | |
2382 | 0 | case OR_PREVMPLSHDR: |
2383 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size); |
2384 | 0 | break; |
2385 | | |
2386 | 0 | case OR_LINKPL: |
2387 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size); |
2388 | 0 | break; |
2389 | | |
2390 | 0 | case OR_LINKPL_NOSNAP: |
2391 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size); |
2392 | 0 | break; |
2393 | | |
2394 | 0 | case OR_LINKTYPE: |
2395 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size); |
2396 | 0 | break; |
2397 | | |
2398 | 0 | case OR_TRAN_IPV4: |
2399 | | /* |
2400 | | * Load the X register with the length of the IPv4 header |
2401 | | * (plus the offset of the link-layer header, if it's |
2402 | | * preceded by a variable-length header such as a radio |
2403 | | * header), in bytes. |
2404 | | */ |
2405 | 0 | s = gen_loadx_iphdrlen(cstate); |
2406 | | |
2407 | | /* |
2408 | | * Load the item at {offset of the link-layer payload} + |
2409 | | * {offset, relative to the start of the link-layer |
2410 | | * payload, of the IPv4 header} + {length of the IPv4 header} + |
2411 | | * {specified offset}. |
2412 | | * |
2413 | | * If the offset of the link-layer payload is variable, |
2414 | | * the variable part of that offset is included in the |
2415 | | * value in the X register, and we include the constant |
2416 | | * part in the offset of the load. |
2417 | | */ |
2418 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
2419 | 0 | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset; |
2420 | 0 | sappend(s, s2); |
2421 | 0 | break; |
2422 | | |
2423 | 0 | case OR_TRAN_IPV6: |
2424 | 0 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size); |
2425 | 0 | break; |
2426 | 0 | } |
2427 | 0 | return s; |
2428 | 0 | } |
2429 | | |
2430 | | /* |
2431 | | * Generate code to load into the X register the sum of the length of |
2432 | | * the IPv4 header and the variable part of the offset of the link-layer |
2433 | | * payload. |
2434 | | */ |
2435 | | static struct slist * |
2436 | | gen_loadx_iphdrlen(compiler_state_t *cstate) |
2437 | 0 | { |
2438 | 0 | struct slist *s, *s2; |
2439 | |
|
2440 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
2441 | 0 | if (s != NULL) { |
2442 | | /* |
2443 | | * The offset of the link-layer payload has a variable |
2444 | | * part. "s" points to a list of statements that put |
2445 | | * the variable part of that offset into the X register. |
2446 | | * |
2447 | | * The 4*([k]&0xf) addressing mode can't be used, as we |
2448 | | * don't have a constant offset, so we have to load the |
2449 | | * value in question into the A register and add to it |
2450 | | * the value from the X register. |
2451 | | */ |
2452 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
2453 | 0 | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
2454 | 0 | sappend(s, s2); |
2455 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2456 | 0 | s2->s.k = 0xf; |
2457 | 0 | sappend(s, s2); |
2458 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
2459 | 0 | s2->s.k = 2; |
2460 | 0 | sappend(s, s2); |
2461 | | |
2462 | | /* |
2463 | | * The A register now contains the length of the IP header. |
2464 | | * We need to add to it the variable part of the offset of |
2465 | | * the link-layer payload, which is still in the X |
2466 | | * register, and move the result into the X register. |
2467 | | */ |
2468 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
2469 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
2470 | 0 | } else { |
2471 | | /* |
2472 | | * The offset of the link-layer payload is a constant, |
2473 | | * so no code was generated to load the (nonexistent) |
2474 | | * variable part of that offset. |
2475 | | * |
2476 | | * This means we can use the 4*([k]&0xf) addressing |
2477 | | * mode. Load the length of the IPv4 header, which |
2478 | | * is at an offset of cstate->off_nl from the beginning of |
2479 | | * the link-layer payload, and thus at an offset of |
2480 | | * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning |
2481 | | * of the raw packet data, using that addressing mode. |
2482 | | */ |
2483 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); |
2484 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
2485 | 0 | } |
2486 | 0 | return s; |
2487 | 0 | } |
2488 | | |
2489 | | /* |
2490 | | * Produce an instruction block with a final branch statement that takes the |
2491 | | * true branch iff rsense is not zero. Since this function detects Boolean |
2492 | | * constants for potential later use, the resulting block must not be modified |
2493 | | * directly afterwards, instead it should be used as an argument to gen_and(), |
2494 | | * gen_or(), gen_not() and sprepend_to_block(). |
2495 | | */ |
2496 | | static struct block * |
2497 | | gen_uncond(compiler_state_t *cstate, const u_char rsense) |
2498 | 0 | { |
2499 | 0 | struct slist *s; |
2500 | |
|
2501 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
2502 | 0 | s->s.k = !rsense; |
2503 | 0 | struct block *ret = gen_jmp_k(cstate, BPF_JEQ, 0, s); |
2504 | 0 | ret->meaning = rsense ? IS_TRUE : IS_FALSE; |
2505 | 0 | return ret; |
2506 | 0 | } |
2507 | | |
2508 | | static inline struct block * |
2509 | | gen_true(compiler_state_t *cstate) |
2510 | 0 | { |
2511 | 0 | return gen_uncond(cstate, 1); |
2512 | 0 | } |
2513 | | |
2514 | | static inline struct block * |
2515 | | gen_false(compiler_state_t *cstate) |
2516 | 0 | { |
2517 | 0 | return gen_uncond(cstate, 0); |
2518 | 0 | } |
2519 | | |
2520 | | /* |
2521 | | * Generate code to match a particular packet type. |
2522 | | * |
2523 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
2524 | | * value, if <= ETHERMTU. We use that to determine whether to |
2525 | | * match the type/length field or to check the type/length field for |
2526 | | * a value <= ETHERMTU to see whether it's a type field and then do |
2527 | | * the appropriate test. |
2528 | | */ |
2529 | | static struct block * |
2530 | | gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2531 | 0 | { |
2532 | 0 | struct block *b0, *b1; |
2533 | |
|
2534 | 0 | switch (ll_proto) { |
2535 | | |
2536 | 0 | case LLCSAP_ISONS: |
2537 | 0 | case LLCSAP_IP: |
2538 | 0 | case LLCSAP_NETBEUI: |
2539 | | /* |
2540 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
2541 | | * so we check the DSAP and SSAP. |
2542 | | * |
2543 | | * LLCSAP_IP checks for IP-over-802.2, rather |
2544 | | * than IP-over-Ethernet or IP-over-SNAP. |
2545 | | * |
2546 | | * XXX - should we check both the DSAP and the |
2547 | | * SSAP, like this, or should we check just the |
2548 | | * DSAP, as we do for other types <= ETHERMTU |
2549 | | * (i.e., other SAP values)? |
2550 | | */ |
2551 | 0 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2552 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); |
2553 | 0 | return gen_and(b0, b1); |
2554 | | |
2555 | 0 | case LLCSAP_IPX: |
2556 | | /* |
2557 | | * Check for; |
2558 | | * |
2559 | | * Ethernet_II frames, which are Ethernet |
2560 | | * frames with a frame type of ETHERTYPE_IPX; |
2561 | | * |
2562 | | * Ethernet_802.3 frames, which are 802.3 |
2563 | | * frames (i.e., the type/length field is |
2564 | | * a length field, <= ETHERMTU, rather than |
2565 | | * a type field) with the first two bytes |
2566 | | * after the Ethernet/802.3 header being |
2567 | | * 0xFFFF; |
2568 | | * |
2569 | | * Ethernet_802.2 frames, which are 802.3 |
2570 | | * frames with an 802.2 LLC header and |
2571 | | * with the IPX LSAP as the DSAP in the LLC |
2572 | | * header; |
2573 | | * |
2574 | | * Ethernet_SNAP frames, which are 802.3 |
2575 | | * frames with an LLC header and a SNAP |
2576 | | * header and with an OUI of 0x000000 |
2577 | | * (encapsulated Ethernet) and a protocol |
2578 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2579 | | * |
2580 | | * XXX - should we generate the same code both |
2581 | | * for tests for LLCSAP_IPX and for ETHERTYPE_IPX? |
2582 | | */ |
2583 | | |
2584 | | /* |
2585 | | * This generates code to check both for the |
2586 | | * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3. |
2587 | | */ |
2588 | 0 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
2589 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF); |
2590 | 0 | b1 = gen_or(b0, b1); |
2591 | | |
2592 | | /* |
2593 | | * Now we add code to check for SNAP frames with |
2594 | | * ETHERTYPE_IPX, i.e. Ethernet_SNAP. |
2595 | | */ |
2596 | 0 | b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2597 | 0 | b1 = gen_or(b0, b1); |
2598 | | |
2599 | | /* |
2600 | | * Now we generate code to check for 802.3 |
2601 | | * frames in general. |
2602 | | */ |
2603 | 0 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2604 | | |
2605 | | /* |
2606 | | * Now add the check for 802.3 frames before the |
2607 | | * check for Ethernet_802.2 and Ethernet_802.3, |
2608 | | * as those checks should only be done on 802.3 |
2609 | | * frames, not on Ethernet frames. |
2610 | | */ |
2611 | 0 | b1 = gen_and(b0, b1); |
2612 | | |
2613 | | /* |
2614 | | * Now add the check for Ethernet_II frames, and |
2615 | | * do that before checking for the other frame |
2616 | | * types. |
2617 | | */ |
2618 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); |
2619 | 0 | return gen_or(b0, b1); |
2620 | | |
2621 | 0 | case ETHERTYPE_ATALK: |
2622 | 0 | case ETHERTYPE_AARP: |
2623 | | /* |
2624 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2625 | | * layer) may use 802.2 encapsulation. |
2626 | | */ |
2627 | | |
2628 | | /* |
2629 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2630 | | * we check for an Ethernet type field less or equal than |
2631 | | * 1500, which means it's an 802.3 length field. |
2632 | | */ |
2633 | 0 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2634 | | |
2635 | | /* |
2636 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2637 | | * SNAP packets with an organization code of |
2638 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2639 | | * type of ETHERTYPE_ATALK (Appletalk). |
2640 | | * |
2641 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2642 | | * SNAP packets with an organization code of |
2643 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2644 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2645 | | */ |
2646 | 0 | if (ll_proto == ETHERTYPE_ATALK) |
2647 | 0 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2648 | 0 | else /* ll_proto == ETHERTYPE_AARP */ |
2649 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2650 | 0 | b1 = gen_and(b0, b1); |
2651 | | |
2652 | | /* |
2653 | | * Check for Ethernet encapsulation (Ethertalk |
2654 | | * phase 1?); we just check for the Ethernet |
2655 | | * protocol type. |
2656 | | */ |
2657 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2658 | |
|
2659 | 0 | return gen_or(b0, b1); |
2660 | | |
2661 | 0 | default: |
2662 | 0 | if (ll_proto <= ETHERMTU) { |
2663 | 0 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
2664 | | /* |
2665 | | * This is an LLC SAP value, so the frames |
2666 | | * that match would be 802.2 frames. |
2667 | | * Check that the frame is an 802.2 frame |
2668 | | * (i.e., that the length/type field is |
2669 | | * a length field, <= ETHERMTU) and |
2670 | | * then check the DSAP. |
2671 | | */ |
2672 | 0 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2673 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto); |
2674 | 0 | return gen_and(b0, b1); |
2675 | 0 | } else { |
2676 | 0 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
2677 | | /* |
2678 | | * This is an Ethernet type, so compare |
2679 | | * the length/type field with it (if |
2680 | | * the frame is an 802.2 frame, the length |
2681 | | * field will be <= ETHERMTU, and, as |
2682 | | * "ll_proto" is > ETHERMTU, this test |
2683 | | * will fail and the frame won't match, |
2684 | | * which is what we want). |
2685 | | */ |
2686 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2687 | 0 | } |
2688 | 0 | } |
2689 | 0 | } |
2690 | | |
2691 | | static struct block * |
2692 | | gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2693 | 0 | { |
2694 | | /* |
2695 | | * For DLT_NULL, the link-layer header is a 32-bit word |
2696 | | * containing an AF_ value in *host* byte order, and for |
2697 | | * DLT_ENC, the link-layer header begins with a 32-bit |
2698 | | * word containing an AF_ value in host byte order. |
2699 | | * |
2700 | | * In addition, if we're reading a saved capture file, |
2701 | | * the host byte order in the capture may not be the |
2702 | | * same as the host byte order on this machine. |
2703 | | * |
2704 | | * For DLT_LOOP, the link-layer header is a 32-bit |
2705 | | * word containing an AF_ value in *network* byte order. |
2706 | | */ |
2707 | 0 | if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) { |
2708 | | /* |
2709 | | * The AF_ value is in host byte order, but the BPF |
2710 | | * interpreter will convert it to network byte order. |
2711 | | * |
2712 | | * If this is a save file, and it's from a machine |
2713 | | * with the opposite byte order to ours, we byte-swap |
2714 | | * the AF_ value. |
2715 | | * |
2716 | | * Then we run it through "htonl()", and generate |
2717 | | * code to compare against the result. |
2718 | | */ |
2719 | 0 | if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped) |
2720 | 0 | ll_proto = SWAPLONG(ll_proto); |
2721 | 0 | ll_proto = htonl(ll_proto); |
2722 | 0 | } |
2723 | 0 | return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto)); |
2724 | 0 | } |
2725 | | |
2726 | | /* |
2727 | | * "proto" is an Ethernet type value and for IPNET, if it is not IPv4 |
2728 | | * or IPv6 then we have an error. |
2729 | | */ |
2730 | | static struct block * |
2731 | | gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2732 | 0 | { |
2733 | 0 | switch (ll_proto) { |
2734 | | |
2735 | 0 | case ETHERTYPE_IP: |
2736 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET); |
2737 | | /*NOTREACHED*/ |
2738 | | |
2739 | 0 | case ETHERTYPE_IPV6: |
2740 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6); |
2741 | | /*NOTREACHED*/ |
2742 | | |
2743 | 0 | default: |
2744 | 0 | break; |
2745 | 0 | } |
2746 | | |
2747 | 0 | return gen_false(cstate); |
2748 | 0 | } |
2749 | | |
2750 | | /* |
2751 | | * Generate code to match a particular packet type. |
2752 | | * |
2753 | | * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
2754 | | * value, if <= ETHERMTU. We use that to determine whether to |
2755 | | * match the type field or to check the type field for the special |
2756 | | * LINUX_SLL_P_802_2 value and then do the appropriate test. |
2757 | | */ |
2758 | | static struct block * |
2759 | | gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2760 | 0 | { |
2761 | 0 | struct block *b0, *b1; |
2762 | |
|
2763 | 0 | switch (ll_proto) { |
2764 | | |
2765 | 0 | case LLCSAP_ISONS: |
2766 | 0 | case LLCSAP_IP: |
2767 | 0 | case LLCSAP_NETBEUI: |
2768 | | /* |
2769 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
2770 | | * so we check the DSAP and SSAP. |
2771 | | * |
2772 | | * LLCSAP_IP checks for IP-over-802.2, rather |
2773 | | * than IP-over-Ethernet or IP-over-SNAP. |
2774 | | * |
2775 | | * XXX - should we check both the DSAP and the |
2776 | | * SSAP, like this, or should we check just the |
2777 | | * DSAP, as we do for other types <= ETHERMTU |
2778 | | * (i.e., other SAP values)? |
2779 | | */ |
2780 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2781 | 0 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); |
2782 | 0 | return gen_and(b0, b1); |
2783 | | |
2784 | 0 | case LLCSAP_IPX: |
2785 | | /* |
2786 | | * Ethernet_II frames, which are Ethernet |
2787 | | * frames with a frame type of ETHERTYPE_IPX; |
2788 | | * |
2789 | | * Ethernet_802.3 frames, which have a frame |
2790 | | * type of LINUX_SLL_P_802_3; |
2791 | | * |
2792 | | * Ethernet_802.2 frames, which are 802.3 |
2793 | | * frames with an 802.2 LLC header (i.e, have |
2794 | | * a frame type of LINUX_SLL_P_802_2) and |
2795 | | * with the IPX LSAP as the DSAP in the LLC |
2796 | | * header; |
2797 | | * |
2798 | | * Ethernet_SNAP frames, which are 802.3 |
2799 | | * frames with an LLC header and a SNAP |
2800 | | * header and with an OUI of 0x000000 |
2801 | | * (encapsulated Ethernet) and a protocol |
2802 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2803 | | * |
2804 | | * First, do the checks on LINUX_SLL_P_802_2 |
2805 | | * frames; generate the check for either |
2806 | | * Ethernet_802.2 or Ethernet_SNAP frames, and |
2807 | | * then put a check for LINUX_SLL_P_802_2 frames |
2808 | | * before it. |
2809 | | */ |
2810 | 0 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
2811 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2812 | 0 | b1 = gen_or(b0, b1); |
2813 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2814 | 0 | b1 = gen_and(b0, b1); |
2815 | | |
2816 | | /* |
2817 | | * Now check for 802.3 frames and OR that with |
2818 | | * the previous test. |
2819 | | */ |
2820 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3); |
2821 | 0 | b1 = gen_or(b0, b1); |
2822 | | |
2823 | | /* |
2824 | | * Now add the check for Ethernet_II frames, and |
2825 | | * do that before checking for the other frame |
2826 | | * types. |
2827 | | */ |
2828 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); |
2829 | 0 | return gen_or(b0, b1); |
2830 | | |
2831 | 0 | case ETHERTYPE_ATALK: |
2832 | 0 | case ETHERTYPE_AARP: |
2833 | | /* |
2834 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2835 | | * layer) may use 802.2 encapsulation. |
2836 | | */ |
2837 | | |
2838 | | /* |
2839 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2840 | | * we check for the 802.2 protocol type in the |
2841 | | * "Ethernet type" field. |
2842 | | */ |
2843 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2844 | | |
2845 | | /* |
2846 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2847 | | * SNAP packets with an organization code of |
2848 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2849 | | * type of ETHERTYPE_ATALK (Appletalk). |
2850 | | * |
2851 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2852 | | * SNAP packets with an organization code of |
2853 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2854 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2855 | | */ |
2856 | 0 | if (ll_proto == ETHERTYPE_ATALK) |
2857 | 0 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2858 | 0 | else /* ll_proto == ETHERTYPE_AARP */ |
2859 | 0 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2860 | 0 | b1 = gen_and(b0, b1); |
2861 | | |
2862 | | /* |
2863 | | * Check for Ethernet encapsulation (Ethertalk |
2864 | | * phase 1?); we just check for the Ethernet |
2865 | | * protocol type. |
2866 | | */ |
2867 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2868 | |
|
2869 | 0 | return gen_or(b0, b1); |
2870 | | |
2871 | 0 | default: |
2872 | 0 | if (ll_proto <= ETHERMTU) { |
2873 | 0 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
2874 | | /* |
2875 | | * This is an LLC SAP value, so the frames |
2876 | | * that match would be 802.2 frames. |
2877 | | * Check for the 802.2 protocol type |
2878 | | * in the "Ethernet type" field, and |
2879 | | * then check the DSAP. |
2880 | | */ |
2881 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2882 | 0 | b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B, |
2883 | 0 | ll_proto); |
2884 | 0 | return gen_and(b0, b1); |
2885 | 0 | } else { |
2886 | 0 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
2887 | | /* |
2888 | | * This is an Ethernet type, so compare |
2889 | | * the length/type field with it (if |
2890 | | * the frame is an 802.2 frame, the length |
2891 | | * field will be <= ETHERMTU, and, as |
2892 | | * "ll_proto" is > ETHERMTU, this test |
2893 | | * will fail and the frame won't match, |
2894 | | * which is what we want). |
2895 | | */ |
2896 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2897 | 0 | } |
2898 | 0 | } |
2899 | 0 | } |
2900 | | |
2901 | | /* |
2902 | | * Load a value relative to the beginning of the link-layer header after the |
2903 | | * pflog header. |
2904 | | */ |
2905 | | static struct slist * |
2906 | | gen_load_pflog_llprefixlen(compiler_state_t *cstate) |
2907 | 0 | { |
2908 | 0 | struct slist *s1, *s2; |
2909 | | |
2910 | | /* |
2911 | | * Generate code to load the length of the pflog header into |
2912 | | * the register assigned to hold that length, if one has been |
2913 | | * assigned. (If one hasn't been assigned, no code we've |
2914 | | * generated uses that prefix, so we don't need to generate any |
2915 | | * code to load it.) |
2916 | | */ |
2917 | 0 | if (cstate->off_linkpl.reg != -1) { |
2918 | | /* |
2919 | | * The length is in the first byte of the header. |
2920 | | */ |
2921 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2922 | 0 | s1->s.k = 0; |
2923 | | |
2924 | | /* |
2925 | | * Round it up to a multiple of 4. |
2926 | | * Add 3, and clear the lower 2 bits. |
2927 | | */ |
2928 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
2929 | 0 | s2->s.k = 3; |
2930 | 0 | sappend(s1, s2); |
2931 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2932 | 0 | s2->s.k = 0xfffffffc; |
2933 | 0 | sappend(s1, s2); |
2934 | | |
2935 | | /* |
2936 | | * Now allocate a register to hold that value and store |
2937 | | * it. |
2938 | | */ |
2939 | 0 | s2 = new_stmt(cstate, BPF_ST); |
2940 | 0 | s2->s.k = cstate->off_linkpl.reg; |
2941 | 0 | sappend(s1, s2); |
2942 | | |
2943 | | /* |
2944 | | * Now move it into the X register. |
2945 | | */ |
2946 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2947 | 0 | sappend(s1, s2); |
2948 | |
|
2949 | 0 | return (s1); |
2950 | 0 | } else |
2951 | 0 | return (NULL); |
2952 | 0 | } |
2953 | | |
2954 | | static struct slist * |
2955 | | gen_load_prism_llprefixlen(compiler_state_t *cstate) |
2956 | 0 | { |
2957 | 0 | struct slist *s1, *s2; |
2958 | 0 | struct slist *sjeq_avs_cookie; |
2959 | 0 | struct slist *sjcommon; |
2960 | | |
2961 | | /* |
2962 | | * This code is not compatible with the optimizer, as |
2963 | | * we are generating jmp instructions within a normal |
2964 | | * slist of instructions |
2965 | | */ |
2966 | 0 | cstate->no_optimize = 1; |
2967 | | |
2968 | | /* |
2969 | | * Generate code to load the length of the radio header into |
2970 | | * the register assigned to hold that length, if one has been |
2971 | | * assigned. (If one hasn't been assigned, no code we've |
2972 | | * generated uses that prefix, so we don't need to generate any |
2973 | | * code to load it.) |
2974 | | * |
2975 | | * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes |
2976 | | * or always use the AVS header rather than the Prism header. |
2977 | | * We load a 4-byte big-endian value at the beginning of the |
2978 | | * raw packet data, and see whether, when masked with 0xFFFFF000, |
2979 | | * it's equal to 0x80211000. If so, that indicates that it's |
2980 | | * an AVS header (the masked-out bits are the version number). |
2981 | | * Otherwise, it's a Prism header. |
2982 | | * |
2983 | | * XXX - the Prism header is also, in theory, variable-length, |
2984 | | * but no known software generates headers that aren't 144 |
2985 | | * bytes long. |
2986 | | */ |
2987 | 0 | if (cstate->off_linkhdr.reg != -1) { |
2988 | | /* |
2989 | | * Load the cookie. |
2990 | | */ |
2991 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2992 | 0 | s1->s.k = 0; |
2993 | | |
2994 | | /* |
2995 | | * AND it with 0xFFFFF000. |
2996 | | */ |
2997 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2998 | 0 | s2->s.k = 0xFFFFF000; |
2999 | 0 | sappend(s1, s2); |
3000 | | |
3001 | | /* |
3002 | | * Compare with 0x80211000. |
3003 | | */ |
3004 | 0 | sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
3005 | 0 | sjeq_avs_cookie->s.k = 0x80211000; |
3006 | 0 | sappend(s1, sjeq_avs_cookie); |
3007 | | |
3008 | | /* |
3009 | | * If it's AVS: |
3010 | | * |
3011 | | * The 4 bytes at an offset of 4 from the beginning of |
3012 | | * the AVS header are the length of the AVS header. |
3013 | | * That field is big-endian. |
3014 | | */ |
3015 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
3016 | 0 | s2->s.k = 4; |
3017 | 0 | sappend(s1, s2); |
3018 | 0 | sjeq_avs_cookie->s.jt = s2; |
3019 | | |
3020 | | /* |
3021 | | * Now jump to the code to allocate a register |
3022 | | * into which to save the header length and |
3023 | | * store the length there. (The "jump always" |
3024 | | * instruction needs to have the k field set; |
3025 | | * it's added to the PC, so, as we're jumping |
3026 | | * over a single instruction, it should be 1.) |
3027 | | */ |
3028 | 0 | sjcommon = new_stmt(cstate, JMP(BPF_JA, BPF_K)); |
3029 | 0 | sjcommon->s.k = 1; |
3030 | 0 | sappend(s1, sjcommon); |
3031 | | |
3032 | | /* |
3033 | | * Now for the code that handles the Prism header. |
3034 | | * Just load the length of the Prism header (144) |
3035 | | * into the A register. Have the test for an AVS |
3036 | | * header branch here if we don't have an AVS header. |
3037 | | */ |
3038 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
3039 | 0 | s2->s.k = 144; |
3040 | 0 | sappend(s1, s2); |
3041 | 0 | sjeq_avs_cookie->s.jf = s2; |
3042 | | |
3043 | | /* |
3044 | | * Now allocate a register to hold that value and store |
3045 | | * it. The code for the AVS header will jump here after |
3046 | | * loading the length of the AVS header. |
3047 | | */ |
3048 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3049 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
3050 | 0 | sappend(s1, s2); |
3051 | 0 | sjcommon->s.jf = s2; |
3052 | | |
3053 | | /* |
3054 | | * Now move it into the X register. |
3055 | | */ |
3056 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3057 | 0 | sappend(s1, s2); |
3058 | |
|
3059 | 0 | return (s1); |
3060 | 0 | } else |
3061 | 0 | return (NULL); |
3062 | 0 | } |
3063 | | |
3064 | | static struct slist * |
3065 | | gen_load_avs_llprefixlen(compiler_state_t *cstate) |
3066 | 0 | { |
3067 | 0 | struct slist *s1, *s2; |
3068 | | |
3069 | | /* |
3070 | | * Generate code to load the length of the AVS header into |
3071 | | * the register assigned to hold that length, if one has been |
3072 | | * assigned. (If one hasn't been assigned, no code we've |
3073 | | * generated uses that prefix, so we don't need to generate any |
3074 | | * code to load it.) |
3075 | | */ |
3076 | 0 | if (cstate->off_linkhdr.reg != -1) { |
3077 | | /* |
3078 | | * The 4 bytes at an offset of 4 from the beginning of |
3079 | | * the AVS header are the length of the AVS header. |
3080 | | * That field is big-endian. |
3081 | | */ |
3082 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
3083 | 0 | s1->s.k = 4; |
3084 | | |
3085 | | /* |
3086 | | * Now allocate a register to hold that value and store |
3087 | | * it. |
3088 | | */ |
3089 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3090 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
3091 | 0 | sappend(s1, s2); |
3092 | | |
3093 | | /* |
3094 | | * Now move it into the X register. |
3095 | | */ |
3096 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3097 | 0 | sappend(s1, s2); |
3098 | |
|
3099 | 0 | return (s1); |
3100 | 0 | } else |
3101 | 0 | return (NULL); |
3102 | 0 | } |
3103 | | |
3104 | | static struct slist * |
3105 | | gen_load_radiotap_llprefixlen(compiler_state_t *cstate) |
3106 | 0 | { |
3107 | 0 | struct slist *s1, *s2; |
3108 | | |
3109 | | /* |
3110 | | * Generate code to load the length of the radiotap header into |
3111 | | * the register assigned to hold that length, if one has been |
3112 | | * assigned. (If one hasn't been assigned, no code we've |
3113 | | * generated uses that prefix, so we don't need to generate any |
3114 | | * code to load it.) |
3115 | | */ |
3116 | 0 | if (cstate->off_linkhdr.reg != -1) { |
3117 | | /* |
3118 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
3119 | | * of the radiotap header are the length of the radiotap |
3120 | | * header; unfortunately, it's little-endian, so we have |
3121 | | * to load it a byte at a time and construct the value. |
3122 | | */ |
3123 | | |
3124 | | /* |
3125 | | * Load the high-order byte, at an offset of 3, shift it |
3126 | | * left a byte, and put the result in the X register. |
3127 | | */ |
3128 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3129 | 0 | s1->s.k = 3; |
3130 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
3131 | 0 | sappend(s1, s2); |
3132 | 0 | s2->s.k = 8; |
3133 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3134 | 0 | sappend(s1, s2); |
3135 | | |
3136 | | /* |
3137 | | * Load the next byte, at an offset of 2, and OR the |
3138 | | * value from the X register into it. |
3139 | | */ |
3140 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3141 | 0 | sappend(s1, s2); |
3142 | 0 | s2->s.k = 2; |
3143 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
3144 | 0 | sappend(s1, s2); |
3145 | | |
3146 | | /* |
3147 | | * Now allocate a register to hold that value and store |
3148 | | * it. |
3149 | | */ |
3150 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3151 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
3152 | 0 | sappend(s1, s2); |
3153 | | |
3154 | | /* |
3155 | | * Now move it into the X register. |
3156 | | */ |
3157 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3158 | 0 | sappend(s1, s2); |
3159 | |
|
3160 | 0 | return (s1); |
3161 | 0 | } else |
3162 | 0 | return (NULL); |
3163 | 0 | } |
3164 | | |
3165 | | /* |
3166 | | * At the moment we treat PPI as normal Radiotap encoded |
3167 | | * packets. The difference is in the function that generates |
3168 | | * the code at the beginning to compute the header length. |
3169 | | * Since this code generator of PPI supports bare 802.11 |
3170 | | * encapsulation only (i.e. the encapsulated DLT should be |
3171 | | * DLT_IEEE802_11) we generate code to check for this too; |
3172 | | * that's done in finish_parse(). |
3173 | | */ |
3174 | | static struct slist * |
3175 | | gen_load_ppi_llprefixlen(compiler_state_t *cstate) |
3176 | 0 | { |
3177 | 0 | struct slist *s1, *s2; |
3178 | | |
3179 | | /* |
3180 | | * Generate code to load the length of the radiotap header |
3181 | | * into the register assigned to hold that length, if one has |
3182 | | * been assigned. |
3183 | | */ |
3184 | 0 | if (cstate->off_linkhdr.reg != -1) { |
3185 | | /* |
3186 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
3187 | | * of the radiotap header are the length of the radiotap |
3188 | | * header; unfortunately, it's little-endian, so we have |
3189 | | * to load it a byte at a time and construct the value. |
3190 | | */ |
3191 | | |
3192 | | /* |
3193 | | * Load the high-order byte, at an offset of 3, shift it |
3194 | | * left a byte, and put the result in the X register. |
3195 | | */ |
3196 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3197 | 0 | s1->s.k = 3; |
3198 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
3199 | 0 | sappend(s1, s2); |
3200 | 0 | s2->s.k = 8; |
3201 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3202 | 0 | sappend(s1, s2); |
3203 | | |
3204 | | /* |
3205 | | * Load the next byte, at an offset of 2, and OR the |
3206 | | * value from the X register into it. |
3207 | | */ |
3208 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3209 | 0 | sappend(s1, s2); |
3210 | 0 | s2->s.k = 2; |
3211 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
3212 | 0 | sappend(s1, s2); |
3213 | | |
3214 | | /* |
3215 | | * Now allocate a register to hold that value and store |
3216 | | * it. |
3217 | | */ |
3218 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3219 | 0 | s2->s.k = cstate->off_linkhdr.reg; |
3220 | 0 | sappend(s1, s2); |
3221 | | |
3222 | | /* |
3223 | | * Now move it into the X register. |
3224 | | */ |
3225 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3226 | 0 | sappend(s1, s2); |
3227 | |
|
3228 | 0 | return (s1); |
3229 | 0 | } else |
3230 | 0 | return (NULL); |
3231 | 0 | } |
3232 | | |
3233 | | /* |
3234 | | * Load a value relative to the beginning of the link-layer header after the 802.11 |
3235 | | * header, i.e. LLC_SNAP. |
3236 | | * The link-layer header doesn't necessarily begin at the beginning |
3237 | | * of the packet data; there might be a variable-length prefix containing |
3238 | | * radio information. |
3239 | | */ |
3240 | | static struct slist * |
3241 | | gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext) |
3242 | 0 | { |
3243 | 0 | struct slist *s2; |
3244 | 0 | struct slist *sjset_data_frame_1; |
3245 | 0 | struct slist *sjset_data_frame_2; |
3246 | 0 | struct slist *sjset_qos; |
3247 | 0 | struct slist *sjset_radiotap_flags_present; |
3248 | 0 | struct slist *sjset_radiotap_ext_present; |
3249 | 0 | struct slist *sjset_radiotap_tsft_present; |
3250 | 0 | struct slist *sjset_tsft_datapad, *sjset_notsft_datapad; |
3251 | 0 | struct slist *s_roundup; |
3252 | |
|
3253 | 0 | if (cstate->off_linkpl.reg == -1) { |
3254 | | /* |
3255 | | * No register has been assigned to the offset of |
3256 | | * the link-layer payload, which means nobody needs |
3257 | | * it; don't bother computing it - just return |
3258 | | * what we already have. |
3259 | | */ |
3260 | 0 | return (s); |
3261 | 0 | } |
3262 | | |
3263 | | /* |
3264 | | * This code is not compatible with the optimizer, as |
3265 | | * we are generating jmp instructions within a normal |
3266 | | * slist of instructions |
3267 | | */ |
3268 | 0 | cstate->no_optimize = 1; |
3269 | | |
3270 | | /* |
3271 | | * If "s" is non-null, it has code to arrange that the X register |
3272 | | * contains the length of the prefix preceding the link-layer |
3273 | | * header. |
3274 | | * |
3275 | | * Otherwise, the length of the prefix preceding the link-layer |
3276 | | * header is "off_outermostlinkhdr.constant_part". |
3277 | | */ |
3278 | 0 | if (s == NULL) { |
3279 | | /* |
3280 | | * There is no variable-length header preceding the |
3281 | | * link-layer header. |
3282 | | * |
3283 | | * Load the length of the fixed-length prefix preceding |
3284 | | * the link-layer header (if any) into the X register, |
3285 | | * and store it in the cstate->off_linkpl.reg register. |
3286 | | * That length is off_outermostlinkhdr.constant_part. |
3287 | | */ |
3288 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_IMM); |
3289 | 0 | s->s.k = cstate->off_outermostlinkhdr.constant_part; |
3290 | 0 | } |
3291 | | |
3292 | | /* |
3293 | | * The X register contains the offset of the beginning of the |
3294 | | * link-layer header; add 24, which is the minimum length |
3295 | | * of the MAC header for a data frame, to that, and store it |
3296 | | * in cstate->off_linkpl.reg, and then load the Frame Control field, |
3297 | | * which is at the offset in the X register, with an indexed load. |
3298 | | */ |
3299 | 0 | s2 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
3300 | 0 | sappend(s, s2); |
3301 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
3302 | 0 | s2->s.k = 24; |
3303 | 0 | sappend(s, s2); |
3304 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3305 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3306 | 0 | sappend(s, s2); |
3307 | |
|
3308 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
3309 | 0 | s2->s.k = 0; |
3310 | 0 | sappend(s, s2); |
3311 | | |
3312 | | /* |
3313 | | * Check the Frame Control field to see if this is a data frame; |
3314 | | * a data frame has the 0x08 bit (b3) in that field set and the |
3315 | | * 0x04 bit (b2) clear. |
3316 | | */ |
3317 | 0 | sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3318 | 0 | sjset_data_frame_1->s.k = IEEE80211_FC0_TYPE_DATA; |
3319 | 0 | sappend(s, sjset_data_frame_1); |
3320 | | |
3321 | | /* |
3322 | | * If b3 is set, test b2, otherwise go to the first statement of |
3323 | | * the rest of the program. |
3324 | | */ |
3325 | 0 | sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3326 | 0 | sjset_data_frame_2->s.k = IEEE80211_FC0_TYPE_CTL; |
3327 | 0 | sappend(s, sjset_data_frame_2); |
3328 | 0 | sjset_data_frame_1->s.jf = snext; |
3329 | | |
3330 | | /* |
3331 | | * If b2 is not set, this is a data frame; test the QoS bit. |
3332 | | * Otherwise, go to the first statement of the rest of the |
3333 | | * program. |
3334 | | */ |
3335 | 0 | sjset_data_frame_2->s.jt = snext; |
3336 | 0 | sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3337 | 0 | sjset_qos->s.k = IEEE80211_FC0_SUBTYPE_QOS; |
3338 | 0 | sappend(s, sjset_qos); |
3339 | | |
3340 | | /* |
3341 | | * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS |
3342 | | * field. |
3343 | | * Otherwise, go to the first statement of the rest of the |
3344 | | * program. |
3345 | | */ |
3346 | 0 | sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
3347 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3348 | 0 | sappend(s, s2); |
3349 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
3350 | 0 | s2->s.k = 2; |
3351 | 0 | sappend(s, s2); |
3352 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3353 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3354 | 0 | sappend(s, s2); |
3355 | | |
3356 | | /* |
3357 | | * If we have a radiotap header, look at it to see whether |
3358 | | * there's Atheros padding between the MAC-layer header |
3359 | | * and the payload. |
3360 | | * |
3361 | | * Note: all of the fields in the radiotap header are |
3362 | | * little-endian, so we byte-swap all of the values |
3363 | | * we test against, as they will be loaded as big-endian |
3364 | | * values. |
3365 | | * |
3366 | | * XXX - in the general case, we would have to scan through |
3367 | | * *all* the presence bits, if there's more than one word of |
3368 | | * presence bits. That would require a loop, meaning that |
3369 | | * we wouldn't be able to run the filter in the kernel. |
3370 | | * |
3371 | | * We assume here that the Atheros adapters that insert the |
3372 | | * annoying padding don't have multiple antennae and therefore |
3373 | | * do not generate radiotap headers with multiple presence words. |
3374 | | */ |
3375 | 0 | if (cstate->linktype == DLT_IEEE802_11_RADIO) { |
3376 | | /* |
3377 | | * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set |
3378 | | * in the first presence flag word? |
3379 | | */ |
3380 | 0 | sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W); |
3381 | 0 | s2->s.k = 4; |
3382 | 0 | sappend(s, s2); |
3383 | |
|
3384 | 0 | sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3385 | 0 | sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002); |
3386 | 0 | sappend(s, sjset_radiotap_flags_present); |
3387 | | |
3388 | | /* |
3389 | | * If not, skip all of this. |
3390 | | */ |
3391 | 0 | sjset_radiotap_flags_present->s.jf = snext; |
3392 | | |
3393 | | /* |
3394 | | * Otherwise, is the "extension" bit set in that word? |
3395 | | */ |
3396 | 0 | sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3397 | 0 | sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000); |
3398 | 0 | sappend(s, sjset_radiotap_ext_present); |
3399 | 0 | sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present; |
3400 | | |
3401 | | /* |
3402 | | * If so, skip all of this. |
3403 | | */ |
3404 | 0 | sjset_radiotap_ext_present->s.jt = snext; |
3405 | | |
3406 | | /* |
3407 | | * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set? |
3408 | | */ |
3409 | 0 | sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3410 | 0 | sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001); |
3411 | 0 | sappend(s, sjset_radiotap_tsft_present); |
3412 | 0 | sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present; |
3413 | | |
3414 | | /* |
3415 | | * If IEEE80211_RADIOTAP_TSFT is set, the flags field is |
3416 | | * at an offset of 16 from the beginning of the raw packet |
3417 | | * data (8 bytes for the radiotap header and 8 bytes for |
3418 | | * the TSFT field). |
3419 | | * |
3420 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
3421 | | * is set. |
3422 | | */ |
3423 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
3424 | 0 | s2->s.k = 16; |
3425 | 0 | sappend(s, s2); |
3426 | 0 | sjset_radiotap_tsft_present->s.jt = s2; |
3427 | |
|
3428 | 0 | sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3429 | 0 | sjset_tsft_datapad->s.k = 0x20; |
3430 | 0 | sappend(s, sjset_tsft_datapad); |
3431 | | |
3432 | | /* |
3433 | | * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is |
3434 | | * at an offset of 8 from the beginning of the raw packet |
3435 | | * data (8 bytes for the radiotap header). |
3436 | | * |
3437 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
3438 | | * is set. |
3439 | | */ |
3440 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
3441 | 0 | s2->s.k = 8; |
3442 | 0 | sappend(s, s2); |
3443 | 0 | sjset_radiotap_tsft_present->s.jf = s2; |
3444 | |
|
3445 | 0 | sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET, BPF_K)); |
3446 | 0 | sjset_notsft_datapad->s.k = 0x20; |
3447 | 0 | sappend(s, sjset_notsft_datapad); |
3448 | | |
3449 | | /* |
3450 | | * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is |
3451 | | * set, round the length of the 802.11 header to |
3452 | | * a multiple of 4. Do that by adding 3 and then |
3453 | | * dividing by and multiplying by 4, which we do by |
3454 | | * ANDing with ~3. |
3455 | | */ |
3456 | 0 | s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM); |
3457 | 0 | s_roundup->s.k = cstate->off_linkpl.reg; |
3458 | 0 | sappend(s, s_roundup); |
3459 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
3460 | 0 | s2->s.k = 3; |
3461 | 0 | sappend(s, s2); |
3462 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM); |
3463 | 0 | s2->s.k = (bpf_u_int32)~3; |
3464 | 0 | sappend(s, s2); |
3465 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3466 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3467 | 0 | sappend(s, s2); |
3468 | |
|
3469 | 0 | sjset_tsft_datapad->s.jt = s_roundup; |
3470 | 0 | sjset_tsft_datapad->s.jf = snext; |
3471 | 0 | sjset_notsft_datapad->s.jt = s_roundup; |
3472 | 0 | sjset_notsft_datapad->s.jf = snext; |
3473 | 0 | } else |
3474 | 0 | sjset_qos->s.jf = snext; |
3475 | |
|
3476 | 0 | return s; |
3477 | 0 | } |
3478 | | |
3479 | | static void |
3480 | | insert_compute_vloffsets(compiler_state_t *cstate, struct block *b) |
3481 | 0 | { |
3482 | 0 | struct slist *s; |
3483 | | |
3484 | | /* There is an implicit dependency between the link |
3485 | | * payload and link header since the payload computation |
3486 | | * includes the variable part of the header. Therefore, |
3487 | | * if nobody else has allocated a register for the link |
3488 | | * header and we need it, do it now. */ |
3489 | 0 | if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable && |
3490 | 0 | cstate->off_linkhdr.reg == -1) |
3491 | 0 | cstate->off_linkhdr.reg = alloc_reg(cstate); |
3492 | | |
3493 | | /* |
3494 | | * For link-layer types that have a variable-length header |
3495 | | * preceding the link-layer header, generate code to load |
3496 | | * the offset of the link-layer header into the register |
3497 | | * assigned to that offset, if any. |
3498 | | * |
3499 | | * XXX - this, and the next switch statement, won't handle |
3500 | | * encapsulation of 802.11 or 802.11+radio information in |
3501 | | * some other protocol stack. That's significantly more |
3502 | | * complicated. |
3503 | | */ |
3504 | 0 | switch (cstate->outermostlinktype) { |
3505 | | |
3506 | 0 | case DLT_PRISM_HEADER: |
3507 | 0 | s = gen_load_prism_llprefixlen(cstate); |
3508 | 0 | break; |
3509 | | |
3510 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
3511 | 0 | s = gen_load_avs_llprefixlen(cstate); |
3512 | 0 | break; |
3513 | | |
3514 | 0 | case DLT_IEEE802_11_RADIO: |
3515 | 0 | s = gen_load_radiotap_llprefixlen(cstate); |
3516 | 0 | break; |
3517 | | |
3518 | 0 | case DLT_PPI: |
3519 | 0 | s = gen_load_ppi_llprefixlen(cstate); |
3520 | 0 | break; |
3521 | | |
3522 | 0 | default: |
3523 | 0 | s = NULL; |
3524 | 0 | break; |
3525 | 0 | } |
3526 | | |
3527 | | /* |
3528 | | * For link-layer types that have a variable-length link-layer |
3529 | | * header, generate code to load the offset of the link-layer |
3530 | | * payload into the register assigned to that offset, if any. |
3531 | | */ |
3532 | 0 | switch (cstate->outermostlinktype) { |
3533 | | |
3534 | 0 | case DLT_IEEE802_11: |
3535 | 0 | case DLT_PRISM_HEADER: |
3536 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
3537 | 0 | case DLT_IEEE802_11_RADIO: |
3538 | 0 | case DLT_PPI: |
3539 | 0 | s = gen_load_802_11_header_len(cstate, s, b->stmts); |
3540 | | /* |
3541 | | * After this call s may have changed, b->stmts has not |
3542 | | * changed, s and b->stmts have not merged into one linked |
3543 | | * list, therefore the meaning of b, whether a Boolean constant |
3544 | | * or not, has not changed. |
3545 | | */ |
3546 | 0 | break; |
3547 | | |
3548 | 0 | case DLT_PFLOG: |
3549 | 0 | s = gen_load_pflog_llprefixlen(cstate); |
3550 | 0 | break; |
3551 | 0 | } |
3552 | | |
3553 | | /* |
3554 | | * If there is no initialization yet and we need variable |
3555 | | * length offsets for VLAN, initialize them to zero |
3556 | | */ |
3557 | 0 | if (s == NULL && cstate->is_vlan_vloffset) { |
3558 | 0 | struct slist *s2; |
3559 | |
|
3560 | 0 | if (cstate->off_linkpl.reg == -1) |
3561 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
3562 | 0 | if (cstate->off_linktype.reg == -1) |
3563 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
3564 | |
|
3565 | 0 | s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
3566 | 0 | s->s.k = 0; |
3567 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3568 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3569 | 0 | sappend(s, s2); |
3570 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3571 | 0 | s2->s.k = cstate->off_linktype.reg; |
3572 | 0 | sappend(s, s2); |
3573 | 0 | } |
3574 | | |
3575 | | /* |
3576 | | * If we have any offset-loading code, append all the |
3577 | | * existing statements in the block to those statements, |
3578 | | * and make the resulting list the list of statements |
3579 | | * for the block. |
3580 | | */ |
3581 | 0 | sprepend_to_block(s, b); |
3582 | 0 | } |
3583 | | |
3584 | | /* |
3585 | | * Take an absolute offset, and: |
3586 | | * |
3587 | | * if it has no variable part, return NULL; |
3588 | | * |
3589 | | * if it has a variable part, generate code to load the register |
3590 | | * containing that variable part into the X register, returning |
3591 | | * a pointer to that code - if no register for that offset has |
3592 | | * been allocated, allocate it first. |
3593 | | * |
3594 | | * (The code to set that register will be generated later, but will |
3595 | | * be placed earlier in the code sequence.) |
3596 | | */ |
3597 | | static struct slist * |
3598 | | gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off) |
3599 | 0 | { |
3600 | 0 | struct slist *s; |
3601 | |
|
3602 | 0 | if (off->is_variable) { |
3603 | 0 | if (off->reg == -1) { |
3604 | | /* |
3605 | | * We haven't yet assigned a register for the |
3606 | | * variable part of the offset of the link-layer |
3607 | | * header; allocate one. |
3608 | | */ |
3609 | 0 | off->reg = alloc_reg(cstate); |
3610 | 0 | } |
3611 | | |
3612 | | /* |
3613 | | * Load the register containing the variable part of the |
3614 | | * offset of the link-layer header into the X register. |
3615 | | */ |
3616 | 0 | s = new_stmt(cstate, BPF_LDX|BPF_MEM); |
3617 | 0 | s->s.k = off->reg; |
3618 | 0 | return s; |
3619 | 0 | } else { |
3620 | | /* |
3621 | | * That offset isn't variable, there's no variable part, |
3622 | | * so we don't need to generate any code. |
3623 | | */ |
3624 | 0 | return NULL; |
3625 | 0 | } |
3626 | 0 | } |
3627 | | |
3628 | | /* |
3629 | | * Map an Ethernet type to the equivalent PPP type. |
3630 | | */ |
3631 | | static uint16_t |
3632 | | ethertype_to_ppptype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
3633 | 0 | { |
3634 | 0 | switch (ll_proto) { |
3635 | | |
3636 | 0 | case ETHERTYPE_IP: |
3637 | 0 | return PPP_IP; |
3638 | | |
3639 | 0 | case ETHERTYPE_IPV6: |
3640 | 0 | return PPP_IPV6; |
3641 | | |
3642 | 0 | case ETHERTYPE_DN: |
3643 | 0 | return PPP_DECNET; |
3644 | | |
3645 | 0 | case ETHERTYPE_ATALK: |
3646 | 0 | return PPP_APPLE; |
3647 | | |
3648 | 0 | case ETHERTYPE_NS: |
3649 | 0 | return PPP_NS; |
3650 | | |
3651 | 0 | case LLCSAP_ISONS: |
3652 | 0 | return PPP_OSI; |
3653 | | |
3654 | 0 | case LLCSAP_8021D: |
3655 | | /* |
3656 | | * I'm assuming the "Bridging PDU"s that go |
3657 | | * over PPP are Spanning Tree Protocol |
3658 | | * Bridging PDUs. |
3659 | | */ |
3660 | 0 | return PPP_BRPDU; |
3661 | | |
3662 | 0 | case LLCSAP_IPX: |
3663 | 0 | return PPP_IPX; |
3664 | 0 | } |
3665 | 0 | assert_maxval(cstate, "PPP protocol", ll_proto, UINT16_MAX); |
3666 | 0 | return (uint16_t)ll_proto; |
3667 | 0 | } |
3668 | | |
3669 | | /* |
3670 | | * Generate any tests that, for encapsulation of a link-layer packet |
3671 | | * inside another protocol stack, need to be done to check for those |
3672 | | * link-layer packets (and that haven't already been done by a check |
3673 | | * for that encapsulation). |
3674 | | */ |
3675 | | static struct block * |
3676 | | gen_prevlinkhdr_check(compiler_state_t *cstate) |
3677 | 0 | { |
3678 | 0 | if (cstate->is_encap) |
3679 | 0 | return gen_encap_ll_check(cstate); |
3680 | | |
3681 | 0 | switch (cstate->prevlinktype) { |
3682 | | |
3683 | 0 | case DLT_SUNATM: |
3684 | | /* |
3685 | | * This is LANE-encapsulated Ethernet; check that the LANE |
3686 | | * packet doesn't begin with an LE Control marker, i.e. |
3687 | | * that it's data, not a control message. |
3688 | | * |
3689 | | * (We've already generated a test for LANE.) |
3690 | | */ |
3691 | 0 | return gen_cmp_ne(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00); |
3692 | | |
3693 | 0 | default: |
3694 | | /* |
3695 | | * No such tests are necessary. |
3696 | | */ |
3697 | 0 | return NULL; |
3698 | 0 | } |
3699 | | /*NOTREACHED*/ |
3700 | 0 | } |
3701 | | |
3702 | | /* |
3703 | | * The three different values we should check for when checking for an |
3704 | | * IPv6 packet with DLT_NULL. |
3705 | | */ |
3706 | 0 | #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */ |
3707 | 0 | #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */ |
3708 | 0 | #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */ |
3709 | | |
3710 | | /* |
3711 | | * Generate code to match a particular packet type by matching the |
3712 | | * link-layer type field or fields in the 802.2 LLC header. |
3713 | | * |
3714 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
3715 | | * value, if <= ETHERMTU. |
3716 | | */ |
3717 | | static struct block * |
3718 | | gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
3719 | 0 | { |
3720 | 0 | struct block *b0, *b1, *b2; |
3721 | | |
3722 | | /* are we checking MPLS-encapsulated packets? */ |
3723 | 0 | if (cstate->label_stack_depth > 0) |
3724 | 0 | return gen_mpls_linktype(cstate, ll_proto); |
3725 | | |
3726 | 0 | switch (cstate->linktype) { |
3727 | | |
3728 | 0 | case DLT_EN10MB: |
3729 | 0 | case DLT_NETANALYZER: |
3730 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
3731 | 0 | case DLT_DSA_TAG_BRCM: |
3732 | 0 | case DLT_DSA_TAG_DSA: |
3733 | | /* Geneve has an EtherType regardless of whether there is an |
3734 | | * L2 header. VXLAN always has an EtherType. */ |
3735 | 0 | if (!cstate->is_encap) |
3736 | 0 | b0 = gen_prevlinkhdr_check(cstate); |
3737 | 0 | else |
3738 | 0 | b0 = NULL; |
3739 | |
|
3740 | 0 | b1 = gen_ether_linktype(cstate, ll_proto); |
3741 | 0 | return b0 ? gen_and(b0, b1) : b1; |
3742 | | /*NOTREACHED*/ |
3743 | | |
3744 | 0 | case DLT_C_HDLC: |
3745 | 0 | case DLT_HDLC: |
3746 | 0 | assert_maxval(cstate, "HDLC protocol", ll_proto, UINT16_MAX); |
3747 | 0 | switch (ll_proto) { |
3748 | | |
3749 | 0 | case LLCSAP_ISONS: |
3750 | 0 | ll_proto = (ll_proto << 8 | LLCSAP_ISONS); |
3751 | | /* fall through */ |
3752 | |
|
3753 | 0 | default: |
3754 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
3755 | | /*NOTREACHED*/ |
3756 | 0 | } |
3757 | | |
3758 | 0 | case DLT_IEEE802_11: |
3759 | 0 | case DLT_PRISM_HEADER: |
3760 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
3761 | 0 | case DLT_IEEE802_11_RADIO: |
3762 | 0 | case DLT_PPI: |
3763 | | /* |
3764 | | * Check that we have a data frame. |
3765 | | */ |
3766 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, |
3767 | 0 | IEEE80211_FC0_TYPE_DATA, |
3768 | 0 | IEEE80211_FC0_TYPE_MASK); |
3769 | | |
3770 | | /* |
3771 | | * Now check for the specified link-layer type. |
3772 | | */ |
3773 | 0 | b1 = gen_llc_linktype(cstate, ll_proto); |
3774 | 0 | return gen_and(b0, b1); |
3775 | | /*NOTREACHED*/ |
3776 | | |
3777 | 0 | case DLT_FDDI: |
3778 | | /* |
3779 | | * XXX - check for LLC frames. |
3780 | | */ |
3781 | 0 | return gen_llc_linktype(cstate, ll_proto); |
3782 | | /*NOTREACHED*/ |
3783 | | |
3784 | 0 | case DLT_IEEE802: |
3785 | | /* |
3786 | | * XXX - check for LLC PDUs, as per IEEE 802.5. |
3787 | | */ |
3788 | 0 | return gen_llc_linktype(cstate, ll_proto); |
3789 | | /*NOTREACHED*/ |
3790 | | |
3791 | 0 | case DLT_ATM_RFC1483: |
3792 | 0 | case DLT_ATM_CLIP: |
3793 | 0 | case DLT_IP_OVER_FC: |
3794 | 0 | return gen_llc_linktype(cstate, ll_proto); |
3795 | | /*NOTREACHED*/ |
3796 | | |
3797 | 0 | case DLT_SUNATM: |
3798 | | /* |
3799 | | * Check for an LLC-encapsulated version of this protocol; |
3800 | | * if we were checking for LANE, linktype would no longer |
3801 | | * be DLT_SUNATM. |
3802 | | * |
3803 | | * Check for LLC encapsulation and then check the protocol. |
3804 | | */ |
3805 | 0 | b0 = gen_atm_prototype(cstate, PT_LLC); |
3806 | 0 | b1 = gen_llc_linktype(cstate, ll_proto); |
3807 | 0 | return gen_and(b0, b1); |
3808 | | /*NOTREACHED*/ |
3809 | | |
3810 | 0 | case DLT_LINUX_SLL: |
3811 | 0 | return gen_linux_sll_linktype(cstate, ll_proto); |
3812 | | /*NOTREACHED*/ |
3813 | | |
3814 | 0 | case DLT_SLIP: |
3815 | 0 | case DLT_SLIP_BSDOS: |
3816 | 0 | case DLT_RAW: |
3817 | | /* |
3818 | | * These types don't provide any type field; packets |
3819 | | * are always IPv4 or IPv6. |
3820 | | * |
3821 | | * XXX - for IPv4, check for a version number of 4, and, |
3822 | | * for IPv6, check for a version number of 6? |
3823 | | */ |
3824 | 0 | switch (ll_proto) { |
3825 | | |
3826 | 0 | case ETHERTYPE_IP: |
3827 | | /* Check for a version number of 4. */ |
3828 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0); |
3829 | | |
3830 | 0 | case ETHERTYPE_IPV6: |
3831 | | /* Check for a version number of 6. */ |
3832 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0); |
3833 | | |
3834 | 0 | default: |
3835 | 0 | return gen_false(cstate); /* always false */ |
3836 | 0 | } |
3837 | | /*NOTREACHED*/ |
3838 | | |
3839 | 0 | case DLT_IPV4: |
3840 | | /* |
3841 | | * Raw IPv4, so no type field. |
3842 | | */ |
3843 | 0 | if (ll_proto == ETHERTYPE_IP) |
3844 | 0 | return gen_true(cstate); /* always true */ |
3845 | | |
3846 | | /* Checking for something other than IPv4; always false */ |
3847 | 0 | return gen_false(cstate); |
3848 | | /*NOTREACHED*/ |
3849 | | |
3850 | 0 | case DLT_IPV6: |
3851 | | /* |
3852 | | * Raw IPv6, so no type field. |
3853 | | */ |
3854 | 0 | if (ll_proto == ETHERTYPE_IPV6) |
3855 | 0 | return gen_true(cstate); /* always true */ |
3856 | | |
3857 | | /* Checking for something other than IPv6; always false */ |
3858 | 0 | return gen_false(cstate); |
3859 | | /*NOTREACHED*/ |
3860 | | |
3861 | 0 | case DLT_PPP: |
3862 | 0 | case DLT_PPP_PPPD: |
3863 | 0 | case DLT_PPP_SERIAL: |
3864 | 0 | case DLT_PPP_ETHER: |
3865 | | /* |
3866 | | * We use Ethernet protocol types inside libpcap; |
3867 | | * map them to the corresponding PPP protocol types. |
3868 | | */ |
3869 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
3870 | 0 | ethertype_to_ppptype(cstate, ll_proto)); |
3871 | | /*NOTREACHED*/ |
3872 | | |
3873 | 0 | case DLT_PPP_BSDOS: |
3874 | | /* |
3875 | | * We use Ethernet protocol types inside libpcap; |
3876 | | * map them to the corresponding PPP protocol types. |
3877 | | */ |
3878 | 0 | switch (ll_proto) { |
3879 | | |
3880 | 0 | case ETHERTYPE_IP: |
3881 | | /* |
3882 | | * Also check for Van Jacobson-compressed IP. |
3883 | | * XXX - do this for other forms of PPP? |
3884 | | */ |
3885 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP); |
3886 | 0 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC); |
3887 | 0 | b1 = gen_or(b0, b1); |
3888 | 0 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC); |
3889 | 0 | return gen_or(b1, b0); |
3890 | | |
3891 | 0 | default: |
3892 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
3893 | 0 | ethertype_to_ppptype(cstate, ll_proto)); |
3894 | 0 | } |
3895 | | /*NOTREACHED*/ |
3896 | | |
3897 | 0 | case DLT_NULL: |
3898 | 0 | case DLT_LOOP: |
3899 | 0 | case DLT_ENC: |
3900 | 0 | switch (ll_proto) { |
3901 | | |
3902 | 0 | case ETHERTYPE_IP: |
3903 | 0 | return (gen_loopback_linktype(cstate, AF_INET)); |
3904 | | |
3905 | 0 | case ETHERTYPE_IPV6: |
3906 | | /* |
3907 | | * AF_ values may, unfortunately, be platform- |
3908 | | * dependent; AF_INET isn't, because everybody |
3909 | | * used 4.2BSD's value, but AF_INET6 is, because |
3910 | | * 4.2BSD didn't have a value for it (given that |
3911 | | * IPv6 didn't exist back in the early 1980's), |
3912 | | * and they all picked their own values. |
3913 | | * |
3914 | | * This means that, if we're reading from a |
3915 | | * savefile, we need to check for all the |
3916 | | * possible values. |
3917 | | * |
3918 | | * If we're doing a live capture, we only need |
3919 | | * to check for this platform's value; however, |
3920 | | * Npcap uses 24, which isn't Windows's AF_INET6 |
3921 | | * value. (Given the multiple different values, |
3922 | | * programs that read pcap files shouldn't be |
3923 | | * checking for their platform's AF_INET6 value |
3924 | | * anyway, they should check for all of the |
3925 | | * possible values. and they might as well do |
3926 | | * that even for live captures.) |
3927 | | */ |
3928 | 0 | if (cstate->bpf_pcap->rfile != NULL) { |
3929 | | /* |
3930 | | * Savefile - check for all three |
3931 | | * possible IPv6 values. |
3932 | | */ |
3933 | 0 | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD); |
3934 | 0 | b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD); |
3935 | 0 | b1 = gen_or(b0, b1); |
3936 | 0 | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN); |
3937 | 0 | return gen_or(b0, b1); |
3938 | 0 | } else { |
3939 | | /* |
3940 | | * Live capture, so we only need to |
3941 | | * check for the value used on this |
3942 | | * platform. |
3943 | | */ |
3944 | | #ifdef _WIN32 |
3945 | | /* |
3946 | | * Npcap doesn't use Windows's AF_INET6, |
3947 | | * as that collides with AF_IPX on |
3948 | | * some BSDs (both have the value 23). |
3949 | | * Instead, it uses 24. |
3950 | | */ |
3951 | | return (gen_loopback_linktype(cstate, 24)); |
3952 | | #else /* _WIN32 */ |
3953 | 0 | #ifdef AF_INET6 |
3954 | 0 | return (gen_loopback_linktype(cstate, AF_INET6)); |
3955 | | #else /* AF_INET6 */ |
3956 | | /* |
3957 | | * I guess this platform doesn't support |
3958 | | * IPv6, so we just reject all packets. |
3959 | | */ |
3960 | | return gen_false(cstate); |
3961 | | #endif /* AF_INET6 */ |
3962 | 0 | #endif /* _WIN32 */ |
3963 | 0 | } |
3964 | | |
3965 | 0 | default: |
3966 | | /* |
3967 | | * Not a type on which we support filtering. |
3968 | | * XXX - support those that have AF_ values |
3969 | | * #defined on this platform, at least? |
3970 | | */ |
3971 | 0 | return gen_false(cstate); |
3972 | 0 | } |
3973 | | |
3974 | 0 | case DLT_PFLOG: |
3975 | | /* |
3976 | | * af field is host byte order in contrast to the rest of |
3977 | | * the packet. |
3978 | | */ |
3979 | 0 | if (ll_proto == ETHERTYPE_IP) |
3980 | 0 | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3981 | 0 | BPF_B, AF_INET)); |
3982 | 0 | else if (ll_proto == ETHERTYPE_IPV6) |
3983 | 0 | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3984 | 0 | BPF_B, AF_INET6)); |
3985 | 0 | else |
3986 | 0 | return gen_false(cstate); |
3987 | | /*NOTREACHED*/ |
3988 | | |
3989 | 0 | case DLT_ARCNET: |
3990 | 0 | case DLT_ARCNET_LINUX: |
3991 | | /* |
3992 | | * In ARCnet header the 8-bit SC (System Code) field identifies |
3993 | | * the higher-level protocol in the INFO (Information) part of |
3994 | | * the packet, same as the 16-bit EtherType > 1500 in Ethernet. |
3995 | | * RFC 1051 (March 1988) allocated ARCTYPE_IP_OLD to IPv4 and |
3996 | | * ARCTYPE_ARP_OLD to ARP, RFC 1201 (February 1991) allocated |
3997 | | * ARCTYPE_IP to IPv4 and ARCTYPE_ARP to ARP. ARCnet header |
3998 | | * encoding and length differ between the two specifications. |
3999 | | * |
4000 | | * This DLT case previously matched IPv4 and ARP by ORing, for |
4001 | | * backward compatibility reasons, respective SCs from RFC 1051 |
4002 | | * and RFC 1201. This worked as expected when a filter program |
4003 | | * tested SC to tell whether a packet is an IPv4/ARP packet, |
4004 | | * but did not access INFO (where the IPv4 or ARP header is). |
4005 | | * |
4006 | | * However, for filter expressions that need to access INFO the |
4007 | | * C code that processes IPv4/ARP header fields generates |
4008 | | * exactly one match and uses the DLT's off_linkpl, which |
4009 | | * init_linktype() initializes to RFC 1201 encoding, so |
4010 | | * combining that with an RFC 1051 SC match produced incorrect |
4011 | | * filter programs. This is why this DLT case in the current |
4012 | | * implementation matches RFC 1201 SCs only. |
4013 | | * |
4014 | | * XXX should we check for first fragment if the protocol |
4015 | | * uses PHDS? |
4016 | | */ |
4017 | 0 | switch (ll_proto) { |
4018 | | |
4019 | 0 | default: |
4020 | 0 | return gen_false(cstate); |
4021 | | |
4022 | 0 | case ETHERTYPE_IPV6: |
4023 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
4024 | 0 | ARCTYPE_INET6)); |
4025 | | |
4026 | 0 | case ETHERTYPE_IP: |
4027 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
4028 | 0 | ARCTYPE_IP); |
4029 | | |
4030 | 0 | case ETHERTYPE_ARP: |
4031 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
4032 | 0 | ARCTYPE_ARP); |
4033 | | |
4034 | 0 | case ETHERTYPE_REVARP: |
4035 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
4036 | 0 | ARCTYPE_REVARP)); |
4037 | | |
4038 | 0 | case ETHERTYPE_ATALK: |
4039 | 0 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
4040 | 0 | ARCTYPE_ATALK)); |
4041 | 0 | } |
4042 | | /*NOTREACHED*/ |
4043 | | |
4044 | 0 | case DLT_LTALK: |
4045 | 0 | switch (ll_proto) { |
4046 | 0 | case ETHERTYPE_ATALK: |
4047 | 0 | return gen_true(cstate); |
4048 | 0 | default: |
4049 | 0 | return gen_false(cstate); |
4050 | 0 | } |
4051 | | /*NOTREACHED*/ |
4052 | | |
4053 | 0 | case DLT_FRELAY: |
4054 | | /* |
4055 | | * XXX - assumes a 2-byte Frame Relay header with |
4056 | | * DLCI and flags. What if the address is longer? |
4057 | | */ |
4058 | 0 | switch (ll_proto) { |
4059 | | |
4060 | 0 | case ETHERTYPE_IP: |
4061 | | /* |
4062 | | * Check for the special NLPID for IP. |
4063 | | */ |
4064 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc); |
4065 | | |
4066 | 0 | case ETHERTYPE_IPV6: |
4067 | | /* |
4068 | | * Check for the special NLPID for IPv6. |
4069 | | */ |
4070 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e); |
4071 | | |
4072 | 0 | case LLCSAP_ISONS: |
4073 | | /* |
4074 | | * Check for several OSI protocols. |
4075 | | * |
4076 | | * Frame Relay packets typically have an OSI |
4077 | | * NLPID at the beginning; we check for each |
4078 | | * of them. |
4079 | | * |
4080 | | * What we check for is the NLPID and a frame |
4081 | | * control field of UI, i.e. 0x03 followed |
4082 | | * by the NLPID. |
4083 | | */ |
4084 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP); |
4085 | 0 | b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS); |
4086 | 0 | b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS); |
4087 | 0 | b2 = gen_or(b1, b2); |
4088 | 0 | return gen_or(b0, b2); |
4089 | | |
4090 | 0 | default: |
4091 | 0 | return gen_false(cstate); |
4092 | 0 | } |
4093 | | /*NOTREACHED*/ |
4094 | | |
4095 | 0 | case DLT_MFR: |
4096 | 0 | break; // not implemented |
4097 | | |
4098 | 0 | case DLT_JUNIPER_MFR: |
4099 | 0 | case DLT_JUNIPER_MLFR: |
4100 | 0 | case DLT_JUNIPER_MLPPP: |
4101 | 0 | case DLT_JUNIPER_ATM1: |
4102 | 0 | case DLT_JUNIPER_ATM2: |
4103 | 0 | case DLT_JUNIPER_PPPOE: |
4104 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
4105 | 0 | case DLT_JUNIPER_GGSN: |
4106 | 0 | case DLT_JUNIPER_ES: |
4107 | 0 | case DLT_JUNIPER_MONITOR: |
4108 | 0 | case DLT_JUNIPER_SERVICES: |
4109 | 0 | case DLT_JUNIPER_ETHER: |
4110 | 0 | case DLT_JUNIPER_PPP: |
4111 | 0 | case DLT_JUNIPER_FRELAY: |
4112 | 0 | case DLT_JUNIPER_CHDLC: |
4113 | 0 | case DLT_JUNIPER_VP: |
4114 | 0 | case DLT_JUNIPER_ST: |
4115 | 0 | case DLT_JUNIPER_ISM: |
4116 | 0 | case DLT_JUNIPER_VS: |
4117 | 0 | case DLT_JUNIPER_SRX_E2E: |
4118 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
4119 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
4120 | | |
4121 | | /* just lets verify the magic number for now - |
4122 | | * on ATM we may have up to 6 different encapsulations on the wire |
4123 | | * and need a lot of heuristics to figure out that the payload |
4124 | | * might be; |
4125 | | * |
4126 | | * FIXME encapsulation specific BPF_ filters |
4127 | | */ |
4128 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */ |
4129 | | |
4130 | 0 | case DLT_IPNET: |
4131 | 0 | return gen_ipnet_linktype(cstate, ll_proto); |
4132 | | |
4133 | 0 | default: |
4134 | | /* |
4135 | | * Does this link-layer header type have a field |
4136 | | * indicating the type of the next protocol? If |
4137 | | * so, off_linktype.constant_part will be the offset of that |
4138 | | * field in the packet; if not, it will be OFFSET_NOT_SET. |
4139 | | */ |
4140 | 0 | if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) { |
4141 | | /* |
4142 | | * Yes; assume it's an Ethernet type. (If |
4143 | | * it's not, it needs to be handled specially |
4144 | | * above.) |
4145 | | */ |
4146 | 0 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
4147 | 0 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
4148 | | /*NOTREACHED */ |
4149 | 0 | } |
4150 | 0 | } |
4151 | | /* |
4152 | | * For example, using the fixed-size NFLOG header it is possible |
4153 | | * to tell only the address family of the packet, other meaningful |
4154 | | * data is either missing or behind TLVs. |
4155 | | */ |
4156 | 0 | bpf_error(cstate, "link-layer type filtering not implemented for %s", |
4157 | 0 | pcapint_datalink_val_to_string(cstate->linktype)); |
4158 | 0 | } |
4159 | | |
4160 | | /* |
4161 | | * Check for an LLC SNAP packet with a given organization code and |
4162 | | * protocol type; we check the entire contents of the 802.2 LLC and |
4163 | | * snap headers, checking for DSAP and SSAP of SNAP and a control |
4164 | | * field of 0x03 in the LLC header, and for the specified organization |
4165 | | * code and protocol type in the SNAP header. |
4166 | | */ |
4167 | | static struct block * |
4168 | | gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype) |
4169 | 0 | { |
4170 | 0 | u_char snapblock[8]; |
4171 | |
|
4172 | 0 | snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */ |
4173 | 0 | snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */ |
4174 | 0 | snapblock[2] = 0x03; /* control = UI */ |
4175 | 0 | snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */ |
4176 | 0 | snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */ |
4177 | 0 | snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */ |
4178 | 0 | snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */ |
4179 | 0 | snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */ |
4180 | 0 | return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock); |
4181 | 0 | } |
4182 | | |
4183 | | /* |
4184 | | * Generate code to match frames with an LLC header. |
4185 | | */ |
4186 | | static struct block * |
4187 | | gen_llc_internal(compiler_state_t *cstate) |
4188 | 0 | { |
4189 | 0 | struct block *b0, *b1; |
4190 | |
|
4191 | 0 | switch (cstate->linktype) { |
4192 | | |
4193 | 0 | case DLT_EN10MB: |
4194 | 0 | case DLT_DSA_TAG_BRCM: |
4195 | 0 | case DLT_DSA_TAG_DSA: |
4196 | | /* |
4197 | | * We check for an Ethernet type field less or equal than |
4198 | | * 1500, which means it's an 802.3 length field. |
4199 | | */ |
4200 | 0 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
4201 | | |
4202 | | /* |
4203 | | * Now check for the purported DSAP and SSAP not being |
4204 | | * 0xFF, to rule out NetWare-over-802.3. |
4205 | | */ |
4206 | 0 | b1 = gen_cmp_ne(cstate, OR_LLC, 0, BPF_H, 0xFFFF); |
4207 | |
|
4208 | 0 | return gen_and(b0, b1); |
4209 | | |
4210 | 0 | case DLT_SUNATM: |
4211 | | /* |
4212 | | * We check for LLC traffic. |
4213 | | */ |
4214 | 0 | return gen_atmtype_llc(cstate); |
4215 | | |
4216 | 0 | case DLT_IEEE802: /* Token Ring */ |
4217 | | /* |
4218 | | * XXX - check for LLC frames. |
4219 | | */ |
4220 | 0 | return gen_true(cstate); |
4221 | | |
4222 | 0 | case DLT_FDDI: |
4223 | | /* |
4224 | | * XXX - check for LLC frames. |
4225 | | */ |
4226 | 0 | return gen_true(cstate); |
4227 | | |
4228 | 0 | case DLT_ATM_RFC1483: |
4229 | | /* |
4230 | | * For LLC encapsulation, these are defined to have an |
4231 | | * 802.2 LLC header. |
4232 | | * |
4233 | | * For VC encapsulation, they don't, but there's no |
4234 | | * way to check for that; the protocol used on the VC |
4235 | | * is negotiated out of band. |
4236 | | */ |
4237 | 0 | return gen_true(cstate); |
4238 | | |
4239 | 0 | case DLT_IEEE802_11: |
4240 | 0 | case DLT_PRISM_HEADER: |
4241 | 0 | case DLT_IEEE802_11_RADIO: |
4242 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
4243 | 0 | case DLT_PPI: |
4244 | | /* |
4245 | | * Check that we have a data frame. |
4246 | | */ |
4247 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, |
4248 | 0 | IEEE80211_FC0_TYPE_DATA, |
4249 | 0 | IEEE80211_FC0_TYPE_MASK); |
4250 | | |
4251 | 0 | default: |
4252 | 0 | fail_kw_on_dlt(cstate, "llc"); |
4253 | | /*NOTREACHED*/ |
4254 | 0 | } |
4255 | 0 | } |
4256 | | |
4257 | | struct block * |
4258 | | gen_llc(compiler_state_t *cstate) |
4259 | 0 | { |
4260 | | /* |
4261 | | * Catch errors reported by us and routines below us, and return NULL |
4262 | | * on an error. |
4263 | | */ |
4264 | 0 | if (setjmp(cstate->top_ctx)) |
4265 | 0 | return (NULL); |
4266 | | |
4267 | 0 | return gen_llc_internal(cstate); |
4268 | 0 | } |
4269 | | |
4270 | | struct block * |
4271 | | gen_llc_i(compiler_state_t *cstate) |
4272 | 0 | { |
4273 | 0 | struct block *b0, *b1; |
4274 | 0 | struct slist *s; |
4275 | | |
4276 | | /* |
4277 | | * Catch errors reported by us and routines below us, and return NULL |
4278 | | * on an error. |
4279 | | */ |
4280 | 0 | if (setjmp(cstate->top_ctx)) |
4281 | 0 | return (NULL); |
4282 | | |
4283 | | /* |
4284 | | * Check whether this is an LLC frame. |
4285 | | */ |
4286 | 0 | b0 = gen_llc_internal(cstate); |
4287 | | |
4288 | | /* |
4289 | | * Load the control byte and test the low-order bit; it must |
4290 | | * be clear for I frames. |
4291 | | */ |
4292 | 0 | s = gen_load_a(cstate, OR_LLC, 2, BPF_B); |
4293 | 0 | b1 = gen_unset(cstate, 0x01, s); |
4294 | |
|
4295 | 0 | return gen_and(b0, b1); |
4296 | 0 | } |
4297 | | |
4298 | | struct block * |
4299 | | gen_llc_s(compiler_state_t *cstate) |
4300 | 0 | { |
4301 | 0 | struct block *b0, *b1; |
4302 | | |
4303 | | /* |
4304 | | * Catch errors reported by us and routines below us, and return NULL |
4305 | | * on an error. |
4306 | | */ |
4307 | 0 | if (setjmp(cstate->top_ctx)) |
4308 | 0 | return (NULL); |
4309 | | |
4310 | | /* |
4311 | | * Check whether this is an LLC frame. |
4312 | | */ |
4313 | 0 | b0 = gen_llc_internal(cstate); |
4314 | | |
4315 | | /* |
4316 | | * Now compare the low-order 2 bit of the control byte against |
4317 | | * the appropriate value for S frames. |
4318 | | */ |
4319 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03); |
4320 | |
|
4321 | 0 | return gen_and(b0, b1); |
4322 | 0 | } |
4323 | | |
4324 | | struct block * |
4325 | | gen_llc_u(compiler_state_t *cstate) |
4326 | 0 | { |
4327 | 0 | struct block *b0, *b1; |
4328 | | |
4329 | | /* |
4330 | | * Catch errors reported by us and routines below us, and return NULL |
4331 | | * on an error. |
4332 | | */ |
4333 | 0 | if (setjmp(cstate->top_ctx)) |
4334 | 0 | return (NULL); |
4335 | | |
4336 | | /* |
4337 | | * Check whether this is an LLC frame. |
4338 | | */ |
4339 | 0 | b0 = gen_llc_internal(cstate); |
4340 | | |
4341 | | /* |
4342 | | * Now compare the low-order 2 bit of the control byte against |
4343 | | * the appropriate value for U frames. |
4344 | | */ |
4345 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03); |
4346 | |
|
4347 | 0 | return gen_and(b0, b1); |
4348 | 0 | } |
4349 | | |
4350 | | struct block * |
4351 | | gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
4352 | 0 | { |
4353 | 0 | struct block *b0, *b1; |
4354 | | |
4355 | | /* |
4356 | | * Catch errors reported by us and routines below us, and return NULL |
4357 | | * on an error. |
4358 | | */ |
4359 | 0 | if (setjmp(cstate->top_ctx)) |
4360 | 0 | return (NULL); |
4361 | | |
4362 | | /* |
4363 | | * Check whether this is an LLC frame. |
4364 | | */ |
4365 | 0 | b0 = gen_llc_internal(cstate); |
4366 | | |
4367 | | /* |
4368 | | * Now check for an S frame with the appropriate type. |
4369 | | */ |
4370 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK); |
4371 | |
|
4372 | 0 | return gen_and(b0, b1); |
4373 | 0 | } |
4374 | | |
4375 | | struct block * |
4376 | | gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
4377 | 0 | { |
4378 | 0 | struct block *b0, *b1; |
4379 | | |
4380 | | /* |
4381 | | * Catch errors reported by us and routines below us, and return NULL |
4382 | | * on an error. |
4383 | | */ |
4384 | 0 | if (setjmp(cstate->top_ctx)) |
4385 | 0 | return (NULL); |
4386 | | |
4387 | | /* |
4388 | | * Check whether this is an LLC frame. |
4389 | | */ |
4390 | 0 | b0 = gen_llc_internal(cstate); |
4391 | | |
4392 | | /* |
4393 | | * Now check for a U frame with the appropriate type. |
4394 | | */ |
4395 | 0 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK); |
4396 | |
|
4397 | 0 | return gen_and(b0, b1); |
4398 | 0 | } |
4399 | | |
4400 | | /* |
4401 | | * Generate code to match a particular packet type, for link-layer types |
4402 | | * using 802.2 LLC headers. |
4403 | | * |
4404 | | * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used |
4405 | | * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues. |
4406 | | * |
4407 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
4408 | | * value, if <= ETHERMTU. We use that to determine whether to |
4409 | | * match the DSAP or both DSAP and LSAP or to check the OUI and |
4410 | | * protocol ID in a SNAP header. |
4411 | | */ |
4412 | | static struct block * |
4413 | | gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
4414 | 0 | { |
4415 | | /* |
4416 | | * XXX - handle token-ring variable-length header. |
4417 | | */ |
4418 | 0 | switch (ll_proto) { |
4419 | | |
4420 | 0 | case LLCSAP_IP: |
4421 | 0 | case LLCSAP_ISONS: |
4422 | 0 | case LLCSAP_NETBEUI: |
4423 | | /* |
4424 | | * XXX - should we check both the DSAP and the |
4425 | | * SSAP, like this, or should we check just the |
4426 | | * DSAP, as we do for other SAP values? |
4427 | | */ |
4428 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32) |
4429 | 0 | ((ll_proto << 8) | ll_proto)); |
4430 | | |
4431 | 0 | case LLCSAP_IPX: |
4432 | | /* |
4433 | | * XXX - are there ever SNAP frames for IPX on |
4434 | | * non-Ethernet 802.x networks? |
4435 | | */ |
4436 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
4437 | | |
4438 | 0 | case ETHERTYPE_ATALK: |
4439 | | /* |
4440 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
4441 | | * SNAP packets with an organization code of |
4442 | | * 0x080007 (Apple, for Appletalk) and a protocol |
4443 | | * type of ETHERTYPE_ATALK (Appletalk). |
4444 | | * |
4445 | | * XXX - check for an organization code of |
4446 | | * encapsulated Ethernet as well? |
4447 | | */ |
4448 | 0 | return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
4449 | | |
4450 | 0 | default: |
4451 | | /* |
4452 | | * XXX - we don't have to check for IPX 802.3 |
4453 | | * here, but should we check for the IPX EtherType? |
4454 | | */ |
4455 | 0 | if (ll_proto <= ETHERMTU) { |
4456 | 0 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
4457 | | /* |
4458 | | * This is an LLC SAP value, so check |
4459 | | * the DSAP. |
4460 | | */ |
4461 | 0 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto); |
4462 | 0 | } else { |
4463 | 0 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
4464 | | /* |
4465 | | * This is an Ethernet type; we assume that it's |
4466 | | * unlikely that it'll appear in the right place |
4467 | | * at random, and therefore check only the |
4468 | | * location that would hold the Ethernet type |
4469 | | * in a SNAP frame with an organization code of |
4470 | | * 0x000000 (encapsulated Ethernet). |
4471 | | * |
4472 | | * XXX - if we were to check for the SNAP DSAP and |
4473 | | * LSAP, as per XXX, and were also to check for an |
4474 | | * organization code of 0x000000 (encapsulated |
4475 | | * Ethernet), we'd do |
4476 | | * |
4477 | | * return gen_snap(cstate, 0x000000, ll_proto); |
4478 | | * |
4479 | | * here; for now, we don't, as per the above. |
4480 | | * I don't know whether it's worth the extra CPU |
4481 | | * time to do the right check or not. |
4482 | | */ |
4483 | 0 | return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto); |
4484 | 0 | } |
4485 | 0 | } |
4486 | 0 | } |
4487 | | |
4488 | | static struct block * |
4489 | | gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
4490 | | int dir, u_int src_off, u_int dst_off) |
4491 | 0 | { |
4492 | 0 | struct block *b0, *b1; |
4493 | 0 | u_int offset; |
4494 | |
|
4495 | 0 | switch (dir) { |
4496 | | |
4497 | 0 | case Q_SRC: |
4498 | 0 | offset = src_off; |
4499 | 0 | break; |
4500 | | |
4501 | 0 | case Q_DST: |
4502 | 0 | offset = dst_off; |
4503 | 0 | break; |
4504 | | |
4505 | 0 | case Q_AND: |
4506 | 0 | b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4507 | 0 | b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off); |
4508 | 0 | return gen_and(b0, b1); |
4509 | | |
4510 | 0 | case Q_DEFAULT: |
4511 | 0 | case Q_OR: |
4512 | 0 | b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4513 | 0 | b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off); |
4514 | 0 | return gen_or(b0, b1); |
4515 | | |
4516 | 0 | default: |
4517 | 0 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
4518 | | /*NOTREACHED*/ |
4519 | 0 | } |
4520 | 0 | return gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask); |
4521 | 0 | } |
4522 | | |
4523 | | static struct block * |
4524 | | gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr, |
4525 | | struct in6_addr *mask, int dir, u_int src_off, u_int dst_off) |
4526 | 0 | { |
4527 | 0 | struct block *b0, *b1; |
4528 | 0 | u_int offset; |
4529 | | /* |
4530 | | * Code below needs to access four separate 32-bit parts of the 128-bit |
4531 | | * IPv6 address and mask. In some OSes this is as simple as using the |
4532 | | * s6_addr32 pseudo-member of struct in6_addr, which contains a union of |
4533 | | * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as |
4534 | | * far as libpcap sees it. Hence copy the data before use to avoid |
4535 | | * potential unaligned memory access and the associated compiler |
4536 | | * warnings (whether genuine or not). |
4537 | | */ |
4538 | 0 | bpf_u_int32 a[4], m[4]; |
4539 | |
|
4540 | 0 | switch (dir) { |
4541 | | |
4542 | 0 | case Q_SRC: |
4543 | 0 | offset = src_off; |
4544 | 0 | break; |
4545 | | |
4546 | 0 | case Q_DST: |
4547 | 0 | offset = dst_off; |
4548 | 0 | break; |
4549 | | |
4550 | 0 | case Q_AND: |
4551 | 0 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4552 | 0 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off); |
4553 | 0 | return gen_and(b0, b1); |
4554 | | |
4555 | 0 | case Q_DEFAULT: |
4556 | 0 | case Q_OR: |
4557 | 0 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4558 | 0 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off); |
4559 | 0 | return gen_or(b0, b1); |
4560 | | |
4561 | 0 | default: |
4562 | 0 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
4563 | | /*NOTREACHED*/ |
4564 | 0 | } |
4565 | | /* this order is important */ |
4566 | 0 | memcpy(a, addr, sizeof(a)); |
4567 | 0 | memcpy(m, mask, sizeof(m)); |
4568 | 0 | b1 = gen_true(cstate); |
4569 | 0 | for (int i = 3; i >= 0; i--) { |
4570 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4 * i, BPF_W, |
4571 | 0 | ntohl(a[i]), ntohl(m[i])); |
4572 | 0 | b1 = gen_and(b0, b1); |
4573 | 0 | } |
4574 | 0 | return b1; |
4575 | 0 | } |
4576 | | |
4577 | | /* |
4578 | | * Like gen_mac48host(), but for DLT_IEEE802_11 (802.11 wireless LAN) and |
4579 | | * various 802.11 + radio headers. |
4580 | | */ |
4581 | | static struct block * |
4582 | | gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4583 | 0 | { |
4584 | 0 | struct block *b0, *b1, *b2; |
4585 | 0 | struct slist *s; |
4586 | |
|
4587 | | #ifdef ENABLE_WLAN_FILTERING_PATCH |
4588 | | /* |
4589 | | * TODO GV 20070613 |
4590 | | * We need to disable the optimizer because the optimizer is buggy |
4591 | | * and wipes out some LD instructions generated by the below |
4592 | | * code to validate the Frame Control bits |
4593 | | */ |
4594 | | cstate->no_optimize = 1; |
4595 | | #endif /* ENABLE_WLAN_FILTERING_PATCH */ |
4596 | |
|
4597 | 0 | switch (dir) { |
4598 | 0 | case Q_SRC: |
4599 | | /* |
4600 | | * Oh, yuk. |
4601 | | * |
4602 | | * For control frames, there is no SA. |
4603 | | * |
4604 | | * For management frames, SA is at an |
4605 | | * offset of 10 from the beginning of |
4606 | | * the packet. |
4607 | | * |
4608 | | * For data frames, SA is at an offset |
4609 | | * of 10 from the beginning of the packet |
4610 | | * if From DS is clear, at an offset of |
4611 | | * 16 from the beginning of the packet |
4612 | | * if From DS is set and To DS is clear, |
4613 | | * and an offset of 24 from the beginning |
4614 | | * of the packet if From DS is set and To DS |
4615 | | * is set. |
4616 | | */ |
4617 | | |
4618 | | /* |
4619 | | * Generate the tests to be done for data frames |
4620 | | * with From DS set. |
4621 | | * |
4622 | | * First, check for To DS set, i.e. check "link[1] & 0x01". |
4623 | | */ |
4624 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4625 | 0 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
4626 | | |
4627 | | /* |
4628 | | * If To DS is set, the SA is at 24. |
4629 | | */ |
4630 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4631 | 0 | b0 = gen_and(b1, b0); |
4632 | | |
4633 | | /* |
4634 | | * Now, check for To DS not set, i.e. check |
4635 | | * "!(link[1] & 0x01)". |
4636 | | */ |
4637 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4638 | 0 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
4639 | | |
4640 | | /* |
4641 | | * If To DS is not set, the SA is at 16. |
4642 | | */ |
4643 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4644 | 0 | b1 = gen_and(b2, b1); |
4645 | | |
4646 | | /* |
4647 | | * Now OR together the last two checks. That gives |
4648 | | * the complete set of checks for data frames with |
4649 | | * From DS set. |
4650 | | */ |
4651 | 0 | b0 = gen_or(b1, b0); |
4652 | | |
4653 | | /* |
4654 | | * Now check for From DS being set, and AND that with |
4655 | | * the ORed-together checks. |
4656 | | */ |
4657 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4658 | 0 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_FROMDS, s); |
4659 | 0 | b0 = gen_and(b1, b0); |
4660 | | |
4661 | | /* |
4662 | | * Now check for data frames with From DS not set. |
4663 | | */ |
4664 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4665 | 0 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_FROMDS, s); |
4666 | | |
4667 | | /* |
4668 | | * If From DS isn't set, the SA is at 10. |
4669 | | */ |
4670 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4671 | 0 | b1 = gen_and(b2, b1); |
4672 | | |
4673 | | /* |
4674 | | * Now OR together the checks for data frames with |
4675 | | * From DS not set and for data frames with From DS |
4676 | | * set; that gives the checks done for data frames. |
4677 | | */ |
4678 | 0 | b0 = gen_or(b1, b0); |
4679 | | |
4680 | | /* |
4681 | | * Now check for a data frame. |
4682 | | * I.e, check "link[0] & 0x08". |
4683 | | */ |
4684 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4685 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4686 | | |
4687 | | /* |
4688 | | * AND that with the checks done for data frames. |
4689 | | */ |
4690 | 0 | b0 = gen_and(b1, b0); |
4691 | | |
4692 | | /* |
4693 | | * If the high-order bit of the type value is 0, this |
4694 | | * is a management frame. |
4695 | | * I.e, check "!(link[0] & 0x08)". |
4696 | | */ |
4697 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4698 | 0 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4699 | | |
4700 | | /* |
4701 | | * For management frames, the SA is at 10. |
4702 | | */ |
4703 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4704 | 0 | b1 = gen_and(b2, b1); |
4705 | | |
4706 | | /* |
4707 | | * OR that with the checks done for data frames. |
4708 | | * That gives the checks done for management and |
4709 | | * data frames. |
4710 | | */ |
4711 | 0 | b0 = gen_or(b1, b0); |
4712 | | |
4713 | | /* |
4714 | | * If the low-order bit of the type value is 1, |
4715 | | * this is either a control frame or a frame |
4716 | | * with a reserved type, and thus not a |
4717 | | * frame with an SA. |
4718 | | * |
4719 | | * I.e., check "!(link[0] & 0x04)". |
4720 | | */ |
4721 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4722 | 0 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
4723 | | |
4724 | | /* |
4725 | | * AND that with the checks for data and management |
4726 | | * frames. |
4727 | | */ |
4728 | 0 | return gen_and(b1, b0); |
4729 | | |
4730 | 0 | case Q_DST: |
4731 | | /* |
4732 | | * Oh, yuk. |
4733 | | * |
4734 | | * For control frames, there is no DA. |
4735 | | * |
4736 | | * For management frames, DA is at an |
4737 | | * offset of 4 from the beginning of |
4738 | | * the packet. |
4739 | | * |
4740 | | * For data frames, DA is at an offset |
4741 | | * of 4 from the beginning of the packet |
4742 | | * if To DS is clear and at an offset of |
4743 | | * 16 from the beginning of the packet |
4744 | | * if To DS is set. |
4745 | | */ |
4746 | | |
4747 | | /* |
4748 | | * Generate the tests to be done for data frames. |
4749 | | * |
4750 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
4751 | | */ |
4752 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4753 | 0 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
4754 | | |
4755 | | /* |
4756 | | * If To DS is set, the DA is at 16. |
4757 | | */ |
4758 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4759 | 0 | b0 = gen_and(b1, b0); |
4760 | | |
4761 | | /* |
4762 | | * Now, check for To DS not set, i.e. check |
4763 | | * "!(link[1] & 0x01)". |
4764 | | */ |
4765 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4766 | 0 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
4767 | | |
4768 | | /* |
4769 | | * If To DS is not set, the DA is at 4. |
4770 | | */ |
4771 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4772 | 0 | b1 = gen_and(b2, b1); |
4773 | | |
4774 | | /* |
4775 | | * Now OR together the last two checks. That gives |
4776 | | * the complete set of checks for data frames. |
4777 | | */ |
4778 | 0 | b0 = gen_or(b1, b0); |
4779 | | |
4780 | | /* |
4781 | | * Now check for a data frame. |
4782 | | * I.e, check "link[0] & 0x08". |
4783 | | */ |
4784 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4785 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4786 | | |
4787 | | /* |
4788 | | * AND that with the checks done for data frames. |
4789 | | */ |
4790 | 0 | b0 = gen_and(b1, b0); |
4791 | | |
4792 | | /* |
4793 | | * If the high-order bit of the type value is 0, this |
4794 | | * is a management frame. |
4795 | | * I.e, check "!(link[0] & 0x08)". |
4796 | | */ |
4797 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4798 | 0 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4799 | | |
4800 | | /* |
4801 | | * For management frames, the DA is at 4. |
4802 | | */ |
4803 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4804 | 0 | b1 = gen_and(b2, b1); |
4805 | | |
4806 | | /* |
4807 | | * OR that with the checks done for data frames. |
4808 | | * That gives the checks done for management and |
4809 | | * data frames. |
4810 | | */ |
4811 | 0 | b0 = gen_or(b1, b0); |
4812 | | |
4813 | | /* |
4814 | | * If the low-order bit of the type value is 1, |
4815 | | * this is either a control frame or a frame |
4816 | | * with a reserved type, and thus not a |
4817 | | * frame with an SA. |
4818 | | * |
4819 | | * I.e., check "!(link[0] & 0x04)". |
4820 | | */ |
4821 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4822 | 0 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
4823 | | |
4824 | | /* |
4825 | | * AND that with the checks for data and management |
4826 | | * frames. |
4827 | | */ |
4828 | 0 | return gen_and(b1, b0); |
4829 | | |
4830 | 0 | case Q_AND: |
4831 | 0 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4832 | 0 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4833 | 0 | return gen_and(b0, b1); |
4834 | | |
4835 | 0 | case Q_DEFAULT: |
4836 | 0 | case Q_OR: |
4837 | 0 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4838 | 0 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4839 | 0 | return gen_or(b0, b1); |
4840 | | |
4841 | | /* |
4842 | | * XXX - add BSSID keyword? |
4843 | | */ |
4844 | 0 | case Q_ADDR1: |
4845 | 0 | return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr)); |
4846 | | |
4847 | 0 | case Q_ADDR2: |
4848 | | /* |
4849 | | * Not present in CTS or ACK control frames. |
4850 | | */ |
4851 | 0 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4852 | 0 | IEEE80211_FC0_TYPE_MASK); |
4853 | 0 | b1 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4854 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4855 | 0 | b2 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4856 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4857 | 0 | b2 = gen_and(b1, b2); |
4858 | 0 | b2 = gen_or(b0, b2); |
4859 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4860 | 0 | return gen_and(b2, b1); |
4861 | | |
4862 | 0 | case Q_ADDR3: |
4863 | | /* |
4864 | | * Not present in control frames. |
4865 | | */ |
4866 | 0 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4867 | 0 | IEEE80211_FC0_TYPE_MASK); |
4868 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4869 | 0 | return gen_and(b0, b1); |
4870 | | |
4871 | 0 | case Q_ADDR4: |
4872 | | /* |
4873 | | * Present only if the direction mask has both "From DS" |
4874 | | * and "To DS" set. Neither control frames nor management |
4875 | | * frames should have both of those set, so we don't |
4876 | | * check the frame type. |
4877 | | */ |
4878 | 0 | b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, |
4879 | 0 | IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK); |
4880 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4881 | 0 | return gen_and(b0, b1); |
4882 | | |
4883 | 0 | case Q_RA: |
4884 | | /* |
4885 | | * Not present in management frames; addr1 in other |
4886 | | * frames. |
4887 | | */ |
4888 | | |
4889 | | /* |
4890 | | * If the high-order bit of the type value is 0, this |
4891 | | * is a management frame. |
4892 | | * I.e, check "(link[0] & 0x08)". |
4893 | | */ |
4894 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4895 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4896 | | |
4897 | | /* |
4898 | | * Check addr1. |
4899 | | */ |
4900 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4901 | | |
4902 | | /* |
4903 | | * AND that with the check of addr1. |
4904 | | */ |
4905 | 0 | return gen_and(b1, b0); |
4906 | | |
4907 | 0 | case Q_TA: |
4908 | | /* |
4909 | | * Not present in management frames; addr2, if present, |
4910 | | * in other frames. |
4911 | | */ |
4912 | | |
4913 | | /* |
4914 | | * Not present in CTS or ACK control frames. |
4915 | | */ |
4916 | 0 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4917 | 0 | IEEE80211_FC0_TYPE_MASK); |
4918 | 0 | b1 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4919 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4920 | 0 | b2 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4921 | 0 | IEEE80211_FC0_SUBTYPE_MASK); |
4922 | 0 | b2 = gen_and(b1, b2); |
4923 | 0 | b2 = gen_or(b0, b2); |
4924 | | |
4925 | | /* |
4926 | | * If the high-order bit of the type value is 0, this |
4927 | | * is a management frame. |
4928 | | * I.e, check "(link[0] & 0x08)". |
4929 | | */ |
4930 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4931 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4932 | | |
4933 | | /* |
4934 | | * AND that with the check for frames other than |
4935 | | * CTS and ACK frames. |
4936 | | */ |
4937 | 0 | b2 = gen_and(b1, b2); |
4938 | | |
4939 | | /* |
4940 | | * Check addr2. |
4941 | | */ |
4942 | 0 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4943 | 0 | return gen_and(b2, b1); |
4944 | 0 | } |
4945 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "dir", dir); |
4946 | | /*NOTREACHED*/ |
4947 | 0 | } |
4948 | | |
4949 | | /* |
4950 | | * This is quite tricky because there may be pad bytes in front of the |
4951 | | * DECNET header, and then there are two possible data packet formats that |
4952 | | * carry both src and dst addresses, plus 5 packet types in a format that |
4953 | | * carries only the src node, plus 2 types that use a different format and |
4954 | | * also carry just the src node. |
4955 | | * |
4956 | | * Yuck. |
4957 | | * |
4958 | | * Instead of doing those all right, we just look for data packets with |
4959 | | * 0 or 1 bytes of padding. If you want to look at other packets, that |
4960 | | * will require a lot more hacking. |
4961 | | * |
4962 | | * To add support for filtering on DECNET "areas" (network numbers) |
4963 | | * one would want to add a "mask" argument to this routine. That would |
4964 | | * make the filter even more inefficient, although one could be clever |
4965 | | * and not generate masking instructions if the mask is 0xFFFF. |
4966 | | */ |
4967 | | static struct block * |
4968 | | gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir) |
4969 | 0 | { |
4970 | 0 | struct block *b0, *b1, *b2, *tmp; |
4971 | 0 | u_int offset_lh; /* offset if long header is received */ |
4972 | 0 | u_int offset_sh; /* offset if short header is received */ |
4973 | |
|
4974 | 0 | switch (dir) { |
4975 | | |
4976 | 0 | case Q_DST: |
4977 | 0 | offset_sh = 1; /* follows flags */ |
4978 | 0 | offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ |
4979 | 0 | break; |
4980 | | |
4981 | 0 | case Q_SRC: |
4982 | 0 | offset_sh = 3; /* follows flags, dstnode */ |
4983 | 0 | offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ |
4984 | 0 | break; |
4985 | | |
4986 | 0 | case Q_AND: |
4987 | | /* Inefficient because we do our Calvinball dance twice */ |
4988 | 0 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4989 | 0 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4990 | 0 | return gen_and(b0, b1); |
4991 | | |
4992 | 0 | case Q_DEFAULT: |
4993 | 0 | case Q_OR: |
4994 | | /* Inefficient because we do our Calvinball dance twice */ |
4995 | 0 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4996 | 0 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4997 | 0 | return gen_or(b0, b1); |
4998 | | |
4999 | 0 | default: |
5000 | 0 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
5001 | | /*NOTREACHED*/ |
5002 | 0 | } |
5003 | | /* |
5004 | | * In a DECnet message inside an Ethernet frame the first two bytes |
5005 | | * immediately after EtherType are the [little-endian] DECnet message |
5006 | | * length, which is irrelevant in this context. |
5007 | | * |
5008 | | * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH |
5009 | | * 8-bit bitmap of the optional padding before the packet route header. |
5010 | | * The bitmap always has bit 7 set to 1 and in this case has bits 0-6 |
5011 | | * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter |
5012 | | * means there aren't any PAD bytes after the bitmap, so the header |
5013 | | * begins at the fourth byte. "pad = 0" means bit 7 of the third byte |
5014 | | * is set to 0, thus the header begins at the third byte. |
5015 | | * |
5016 | | * The header can be in several (as mentioned above) formats, all of |
5017 | | * which begin with the FLAGS 8-bit bitmap, which always has bit 7 |
5018 | | * (PF, "pad field") set to 0 regardless of any padding present before |
5019 | | * the header. "Short header" means bits 0-2 of the bitmap encode the |
5020 | | * integer value 2 (SFDP), and "long header" means value 6 (LFDP). |
5021 | | * |
5022 | | * To test PLENGTH and FLAGS, use multiple-byte constants with the |
5023 | | * values and the masks, this maps to the required single bytes of |
5024 | | * the message correctly on both big-endian and little-endian hosts. |
5025 | | * For the DECnet address use SWAPSHORT(), which always swaps bytes, |
5026 | | * because the wire encoding is little-endian and BPF multiple-byte |
5027 | | * loads are big-endian. When the destination address is near enough |
5028 | | * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two |
5029 | | * smaller ones. |
5030 | | */ |
5031 | | /* Check for pad = 1, long header case */ |
5032 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 0x8106U, 0xFF07U); |
5033 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh, |
5034 | 0 | BPF_H, SWAPSHORT(addr)); |
5035 | 0 | b1 = gen_and(tmp, b1); |
5036 | | /* Check for pad = 0, long header case */ |
5037 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, 0x06U, 0x07U); |
5038 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, |
5039 | 0 | SWAPSHORT(addr)); |
5040 | 0 | b2 = gen_and(tmp, b2); |
5041 | 0 | b1 = gen_or(b2, b1); |
5042 | | /* Check for pad = 1, short header case */ |
5043 | 0 | if (dir == Q_DST) { |
5044 | 0 | b2 = gen_mcmp(cstate, OR_LINKPL, 2, BPF_W, |
5045 | 0 | 0x81020000U | SWAPSHORT(addr), |
5046 | 0 | 0xFF07FFFFU); |
5047 | 0 | } else { |
5048 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 0x8102U, 0xFF07U); |
5049 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, |
5050 | 0 | SWAPSHORT(addr)); |
5051 | 0 | b2 = gen_and(tmp, b2); |
5052 | 0 | } |
5053 | 0 | b1 = gen_or(b2, b1); |
5054 | | /* Check for pad = 0, short header case */ |
5055 | 0 | if (dir == Q_DST) { |
5056 | 0 | b2 = gen_mcmp(cstate, OR_LINKPL, 2, BPF_W, |
5057 | 0 | 0x02000000U | SWAPSHORT(addr) << 8, |
5058 | 0 | 0x07FFFF00U); |
5059 | 0 | } else { |
5060 | 0 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, 0x02U, 0x07U); |
5061 | 0 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, |
5062 | 0 | SWAPSHORT(addr)); |
5063 | 0 | b2 = gen_and(tmp, b2); |
5064 | 0 | } |
5065 | |
|
5066 | 0 | return gen_or(b2, b1); |
5067 | 0 | } |
5068 | | |
5069 | | /* |
5070 | | * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets; |
5071 | | * test the bottom-of-stack bit, and then check the version number |
5072 | | * field in the IP header. |
5073 | | */ |
5074 | | static struct block * |
5075 | | gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
5076 | 0 | { |
5077 | 0 | struct block *b0, *b1; |
5078 | |
|
5079 | 0 | switch (ll_proto) { |
5080 | | |
5081 | 0 | case ETHERTYPE_IP: |
5082 | | /* match the bottom-of-stack bit */ |
5083 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
5084 | | /* match the IPv4 version number */ |
5085 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0); |
5086 | 0 | return gen_and(b0, b1); |
5087 | | |
5088 | 0 | case ETHERTYPE_IPV6: |
5089 | | /* match the bottom-of-stack bit */ |
5090 | 0 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
5091 | | /* match the IPv6 version number */ |
5092 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0); |
5093 | 0 | return gen_and(b0, b1); |
5094 | | |
5095 | 0 | default: |
5096 | | /* FIXME add other L3 proto IDs */ |
5097 | 0 | bpf_error(cstate, "unsupported protocol over mpls"); |
5098 | | /*NOTREACHED*/ |
5099 | 0 | } |
5100 | 0 | } |
5101 | | |
5102 | | static struct block * |
5103 | | gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
5104 | | int proto, int dir, int type) |
5105 | 0 | { |
5106 | 0 | struct block *b0, *b1; |
5107 | |
|
5108 | 0 | switch (proto) { |
5109 | | |
5110 | 0 | case Q_DEFAULT: |
5111 | 0 | b0 = gen_host(cstate, addr, mask, Q_IP, dir, type); |
5112 | | /* |
5113 | | * Only check for non-IPv4 addresses if we're not |
5114 | | * checking MPLS-encapsulated packets. |
5115 | | */ |
5116 | 0 | if (cstate->label_stack_depth == 0) { |
5117 | 0 | b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type); |
5118 | 0 | b1 = gen_or(b0, b1); |
5119 | 0 | b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type); |
5120 | 0 | b0 = gen_or(b1, b0); |
5121 | 0 | } |
5122 | 0 | return b0; |
5123 | | |
5124 | 0 | case Q_IP: |
5125 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
5126 | 0 | b1 = gen_hostop(cstate, addr, mask, dir, 12, 16); |
5127 | 0 | return gen_and(b0, b1); |
5128 | | |
5129 | 0 | case Q_RARP: |
5130 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_REVARP); |
5131 | | /* |
5132 | | * If this DLT does not support RARP, the result of |
5133 | | * gen_linktype() is a Boolean false, then the subsequent |
5134 | | * gen_and() would discard the result of gen_hostop() and |
5135 | | * return the Boolean false. |
5136 | | * |
5137 | | * However, if this DLT also uses a variable-length link-layer |
5138 | | * header (which means DLT_PFLOG only at the time of this |
5139 | | * writing), a side effect of the gen_hostop() invocation |
5140 | | * would be registering a demand for a variable-length offset |
5141 | | * preamble, which a Boolean constant never needs, so in this |
5142 | | * case return early and have one fewer reasons to produce the |
5143 | | * preamble in insert_compute_vloffsets(). |
5144 | | */ |
5145 | 0 | if (b0->meaning == IS_FALSE) |
5146 | 0 | return b0; |
5147 | 0 | b1 = gen_hostop(cstate, addr, mask, dir, 14, 24); |
5148 | 0 | return gen_and(b0, b1); |
5149 | | |
5150 | 0 | case Q_ARP: |
5151 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_ARP); |
5152 | | // same as for Q_RARP |
5153 | 0 | if (b0->meaning == IS_FALSE) |
5154 | 0 | return b0; |
5155 | 0 | b1 = gen_hostop(cstate, addr, mask, dir, 14, 24); |
5156 | 0 | return gen_and(b0, b1); |
5157 | | |
5158 | 0 | case Q_DECNET: |
5159 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_DN); |
5160 | 0 | b1 = gen_dnhostop(cstate, addr, dir); |
5161 | 0 | return gen_and(b0, b1); |
5162 | 0 | } |
5163 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), |
5164 | 0 | type == Q_NET ? "ip net" : "ip host"); |
5165 | | /*NOTREACHED*/ |
5166 | 0 | } |
5167 | | |
5168 | | static struct block * |
5169 | | gen_host6(compiler_state_t *cstate, struct in6_addr *addr, |
5170 | | struct in6_addr *mask, int proto, int dir, int type) |
5171 | 0 | { |
5172 | 0 | struct block *b0, *b1; |
5173 | |
|
5174 | 0 | switch (proto) { |
5175 | | |
5176 | 0 | case Q_DEFAULT: |
5177 | 0 | case Q_IPV6: |
5178 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
5179 | 0 | b1 = gen_hostop6(cstate, addr, mask, dir, 8, 24); |
5180 | 0 | return gen_and(b0, b1); |
5181 | 0 | } |
5182 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), |
5183 | 0 | type == Q_NET ? "ip6 net" : "ip6 host"); |
5184 | | /*NOTREACHED*/ |
5185 | 0 | } |
5186 | | |
5187 | | static struct block * |
5188 | | gen_host46_byname(compiler_state_t *cstate, const char *name, |
5189 | | const u_char proto4, const u_char proto6, const u_char dir) |
5190 | 0 | { |
5191 | 0 | if ((cstate->ai = pcap_nametoaddrinfo(name)) == NULL) |
5192 | 0 | bpf_error(cstate, "unknown host '%s'", name); |
5193 | 0 | struct block *ret = NULL; |
5194 | 0 | struct in6_addr mask128; |
5195 | 0 | memset(&mask128, 0xff, sizeof(mask128)); |
5196 | | |
5197 | | /* |
5198 | | * For a hostname that resolves to both IPv4 and IPv6 addresses the |
5199 | | * AF_INET addresses may come before or after the AF_INET6 addresses |
5200 | | * depending on which getaddrinfo() implementation it is, what the |
5201 | | * resolving host's network configuration is and (on Linux with glibc) |
5202 | | * the contents of gai.conf(5). This is because getaddrinfo() presumes |
5203 | | * a subsequent bind(2) or connect(2) use of the addresses, which is |
5204 | | * not the case here, so there is no sense in preserving the order of |
5205 | | * the AFs in the resolved addresses. However, there is sense in |
5206 | | * hard-coding the order of AFs when generating a match block for more |
5207 | | * than one AF because this way the result reflects fewer external |
5208 | | * effects and is easier to test. |
5209 | | */ |
5210 | | |
5211 | | /* |
5212 | | * Ignore any IPv4 addresses when resolving "ip6 host NAME", validate |
5213 | | * all other proto qualifiers in gen_host(). |
5214 | | */ |
5215 | 0 | if (proto4 != Q_IPV6) { |
5216 | 0 | for (struct addrinfo *ai = cstate->ai; ai; ai = ai->ai_next) { |
5217 | 0 | if (ai->ai_family != AF_INET) |
5218 | 0 | continue; |
5219 | 0 | struct sockaddr_in *sin4 = |
5220 | 0 | (struct sockaddr_in *)ai->ai_addr; |
5221 | 0 | struct block *host4 = gen_host(cstate, ntohl(sin4->sin_addr.s_addr), |
5222 | 0 | 0xffffffff, proto4, dir, Q_HOST); |
5223 | 0 | ret = ret ? gen_or(ret, host4) : host4; |
5224 | 0 | } |
5225 | 0 | } |
5226 | | |
5227 | | /* |
5228 | | * Ignore any IPv6 addresses when resolving "(arp|ip|rarp) host NAME", |
5229 | | * validate all other proto qualifiers in gen_host6(). |
5230 | | */ |
5231 | 0 | if (proto6 != Q_ARP && proto6 != Q_IP && proto6 != Q_RARP) { |
5232 | 0 | for (struct addrinfo *ai = cstate->ai; ai; ai = ai->ai_next) { |
5233 | 0 | if (ai->ai_family != AF_INET6) |
5234 | 0 | continue; |
5235 | 0 | struct sockaddr_in6 *sin6 = |
5236 | 0 | (struct sockaddr_in6 *)ai->ai_addr; |
5237 | 0 | struct block *host6 = gen_host6(cstate, &sin6->sin6_addr, |
5238 | 0 | &mask128, proto6, dir, Q_HOST); |
5239 | 0 | ret = ret ? gen_or(ret, host6) : host6; |
5240 | 0 | } |
5241 | 0 | } |
5242 | |
|
5243 | 0 | freeaddrinfo(cstate->ai); |
5244 | 0 | cstate->ai = NULL; |
5245 | |
|
5246 | 0 | if (! ret) |
5247 | 0 | bpf_error(cstate, "unknown host '%s'%s", name, |
5248 | 0 | proto4 == Q_DEFAULT |
5249 | 0 | ? "" |
5250 | 0 | : " for specified address family"); |
5251 | 0 | return ret; |
5252 | 0 | } |
5253 | | |
5254 | | static unsigned char |
5255 | | is_mac48_linktype(const int linktype) |
5256 | 0 | { |
5257 | 0 | switch (linktype) { |
5258 | 0 | case DLT_EN10MB: |
5259 | 0 | case DLT_FDDI: |
5260 | 0 | case DLT_IEEE802: |
5261 | 0 | case DLT_IEEE802_11: |
5262 | 0 | case DLT_IEEE802_11_RADIO: |
5263 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
5264 | 0 | case DLT_IP_OVER_FC: |
5265 | 0 | case DLT_NETANALYZER: |
5266 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
5267 | 0 | case DLT_DSA_TAG_BRCM: |
5268 | 0 | case DLT_DSA_TAG_DSA: |
5269 | 0 | case DLT_PPI: |
5270 | 0 | case DLT_PRISM_HEADER: |
5271 | 0 | return 1; |
5272 | 0 | default: |
5273 | 0 | return 0; |
5274 | 0 | } |
5275 | 0 | } |
5276 | | |
5277 | | static struct block * |
5278 | | gen_mac48host(compiler_state_t *cstate, const u_char *eaddr, const u_char dir, |
5279 | | const char *keyword) |
5280 | 0 | { |
5281 | 0 | struct block *b1 = NULL; |
5282 | 0 | u_int src_off, dst_off; |
5283 | |
|
5284 | 0 | switch (cstate->linktype) { |
5285 | 0 | case DLT_EN10MB: |
5286 | 0 | case DLT_NETANALYZER: |
5287 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
5288 | 0 | case DLT_DSA_TAG_BRCM: |
5289 | 0 | case DLT_DSA_TAG_DSA: |
5290 | 0 | b1 = gen_prevlinkhdr_check(cstate); |
5291 | 0 | src_off = 6; |
5292 | 0 | dst_off = 0; |
5293 | 0 | break; |
5294 | 0 | case DLT_FDDI: |
5295 | 0 | src_off = 6 + 1 + cstate->pcap_fddipad; |
5296 | 0 | dst_off = 0 + 1 + cstate->pcap_fddipad; |
5297 | 0 | break; |
5298 | 0 | case DLT_IEEE802: |
5299 | 0 | src_off = 8; |
5300 | 0 | dst_off = 2; |
5301 | 0 | break; |
5302 | 0 | case DLT_IEEE802_11: |
5303 | 0 | case DLT_PRISM_HEADER: |
5304 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
5305 | 0 | case DLT_IEEE802_11_RADIO: |
5306 | 0 | case DLT_PPI: |
5307 | 0 | return gen_wlanhostop(cstate, eaddr, dir); |
5308 | 0 | case DLT_IP_OVER_FC: |
5309 | | /* |
5310 | | * Assume that the addresses are IEEE 48-bit MAC addresses, |
5311 | | * as RFC 2625 states. |
5312 | | */ |
5313 | 0 | src_off = 10; |
5314 | 0 | dst_off = 2; |
5315 | 0 | break; |
5316 | 0 | case DLT_SUNATM: |
5317 | | /* |
5318 | | * This is LLC-multiplexed traffic; if it were |
5319 | | * LANE, cstate->linktype would have been set to |
5320 | | * DLT_EN10MB. |
5321 | | */ |
5322 | | /* FALLTHROUGH */ |
5323 | 0 | default: |
5324 | 0 | fail_kw_on_dlt(cstate, keyword); |
5325 | 0 | } |
5326 | | |
5327 | 0 | struct block *b0, *tmp; |
5328 | |
|
5329 | 0 | switch (dir) { |
5330 | 0 | case Q_SRC: |
5331 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5332 | 0 | break; |
5333 | 0 | case Q_DST: |
5334 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5335 | 0 | break; |
5336 | 0 | case Q_AND: |
5337 | 0 | tmp = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5338 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5339 | 0 | b0 = gen_and(tmp, b0); |
5340 | 0 | break; |
5341 | 0 | case Q_DEFAULT: |
5342 | 0 | case Q_OR: |
5343 | 0 | tmp = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5344 | 0 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5345 | 0 | b0 = gen_or(tmp, b0); |
5346 | 0 | break; |
5347 | 0 | default: |
5348 | 0 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
5349 | 0 | } |
5350 | | |
5351 | 0 | return b1 ? gen_and(b1, b0) : b0; |
5352 | 0 | } |
5353 | | |
5354 | | static struct block * |
5355 | | gen_mac48host_byname(compiler_state_t *cstate, const char *name, |
5356 | | const u_char dir, const char *context) |
5357 | 0 | { |
5358 | 0 | if (! is_mac48_linktype(cstate->linktype)) |
5359 | 0 | fail_kw_on_dlt(cstate, context); |
5360 | | |
5361 | 0 | u_char *eaddrp = pcap_ether_hostton(name); |
5362 | 0 | if (eaddrp == NULL) |
5363 | 0 | bpf_error(cstate, ERRSTR_UNKNOWN_MAC48HOST, name); |
5364 | 0 | u_char eaddr[6]; |
5365 | 0 | memcpy(eaddr, eaddrp, sizeof(eaddr)); |
5366 | 0 | free(eaddrp); |
5367 | |
|
5368 | 0 | return gen_mac48host(cstate, eaddr, dir, context); |
5369 | 0 | } |
5370 | | |
5371 | | static struct block * |
5372 | | gen_mac8host(compiler_state_t *cstate, const uint8_t mac8, const u_char dir, |
5373 | | const char *context) |
5374 | 0 | { |
5375 | 0 | u_int src_off, dst_off; |
5376 | |
|
5377 | 0 | switch (cstate->linktype) { |
5378 | 0 | case DLT_ARCNET: |
5379 | 0 | case DLT_ARCNET_LINUX: |
5380 | | /* |
5381 | | * ARCnet is different from Ethernet: the source address comes |
5382 | | * before the destination address, each is one byte long. |
5383 | | * This holds for all three "buffer formats" in RFC 1201 |
5384 | | * Section 2.1, see also page 4-10 in the 1983 edition of the |
5385 | | * "ARCNET Designer's Handbook" published by Datapoint |
5386 | | * (document number 61610-01). |
5387 | | */ |
5388 | 0 | src_off = 0; |
5389 | 0 | dst_off = 1; |
5390 | 0 | break; |
5391 | 0 | case DLT_BACNET_MS_TP: |
5392 | | /* |
5393 | | * MS/TP resembles both Ethernet (in that the destination |
5394 | | * station address precedes the source station address) and |
5395 | | * ARCnet (in that a station address is one byte long). |
5396 | | */ |
5397 | 0 | src_off = 4; |
5398 | 0 | dst_off = 3; |
5399 | 0 | break; |
5400 | 0 | default: |
5401 | 0 | fail_kw_on_dlt(cstate, context); |
5402 | 0 | } |
5403 | | |
5404 | 0 | struct block *src, *dst; |
5405 | |
|
5406 | 0 | switch (dir) { |
5407 | 0 | case Q_SRC: |
5408 | 0 | return gen_cmp(cstate, OR_LINKHDR, src_off, BPF_B, mac8); |
5409 | 0 | case Q_DST: |
5410 | 0 | return gen_cmp(cstate, OR_LINKHDR, dst_off, BPF_B, mac8); |
5411 | 0 | case Q_AND: |
5412 | 0 | src = gen_cmp(cstate, OR_LINKHDR, src_off, BPF_B, mac8); |
5413 | 0 | dst = gen_cmp(cstate, OR_LINKHDR, dst_off, BPF_B, mac8); |
5414 | 0 | return gen_and(src, dst); |
5415 | 0 | case Q_DEFAULT: |
5416 | 0 | case Q_OR: |
5417 | 0 | src = gen_cmp(cstate, OR_LINKHDR, src_off, BPF_B, mac8); |
5418 | 0 | dst = gen_cmp(cstate, OR_LINKHDR, dst_off, BPF_B, mac8); |
5419 | 0 | return gen_or(src, dst); |
5420 | 0 | default: |
5421 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), context); |
5422 | 0 | } |
5423 | 0 | } |
5424 | | |
5425 | | /* |
5426 | | * This primitive is non-directional by design, so the grammar does not allow |
5427 | | * to qualify it with a direction. |
5428 | | */ |
5429 | | static struct block * |
5430 | | gen_gateway(compiler_state_t *cstate, const char *name, const u_char proto) |
5431 | 0 | { |
5432 | 0 | switch (proto) { |
5433 | 0 | case Q_DEFAULT: |
5434 | 0 | case Q_IP: |
5435 | 0 | case Q_ARP: |
5436 | 0 | case Q_RARP: |
5437 | 0 | break; |
5438 | 0 | default: |
5439 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "gateway"); |
5440 | 0 | } |
5441 | | |
5442 | 0 | struct block *b0 = gen_mac48host_byname(cstate, name, Q_OR, "gateway"); |
5443 | | /* |
5444 | | * For "gateway NAME" not qualified with a protocol skip the IPv6 leg |
5445 | | * of the name-to-address translation to match the documented |
5446 | | * IPv4-only behaviour. |
5447 | | */ |
5448 | 0 | struct block *b1 = gen_host46_byname(cstate, name, proto, Q_IP, Q_OR); |
5449 | 0 | return gen_and(b0, gen_not(b1)); |
5450 | 0 | } |
5451 | | |
5452 | | static struct block * |
5453 | | gen_proto_abbrev_internal(compiler_state_t *cstate, int proto) |
5454 | 0 | { |
5455 | 0 | struct block *b0; |
5456 | 0 | struct block *b1; |
5457 | |
|
5458 | 0 | switch (proto) { |
5459 | | |
5460 | 0 | case Q_SCTP: |
5461 | 0 | return gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT); |
5462 | | |
5463 | 0 | case Q_TCP: |
5464 | 0 | return gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT); |
5465 | | |
5466 | 0 | case Q_UDP: |
5467 | 0 | return gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT); |
5468 | | |
5469 | 0 | case Q_ICMP: |
5470 | 0 | return gen_proto(cstate, IPPROTO_ICMP, Q_IP); |
5471 | | |
5472 | | #ifndef IPPROTO_IGMP |
5473 | | #define IPPROTO_IGMP 2 |
5474 | | #endif |
5475 | | |
5476 | 0 | case Q_IGMP: |
5477 | 0 | return gen_proto(cstate, IPPROTO_IGMP, Q_IP); |
5478 | | |
5479 | 0 | #ifndef IPPROTO_IGRP |
5480 | 0 | #define IPPROTO_IGRP 9 |
5481 | 0 | #endif |
5482 | 0 | case Q_IGRP: |
5483 | 0 | return gen_proto(cstate, IPPROTO_IGRP, Q_IP); |
5484 | | |
5485 | | #ifndef IPPROTO_PIM |
5486 | | #define IPPROTO_PIM 103 |
5487 | | #endif |
5488 | | |
5489 | 0 | case Q_PIM: |
5490 | 0 | return gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT); |
5491 | | |
5492 | 0 | #ifndef IPPROTO_VRRP |
5493 | 0 | #define IPPROTO_VRRP 112 |
5494 | 0 | #endif |
5495 | | |
5496 | 0 | case Q_VRRP: |
5497 | 0 | return gen_proto(cstate, IPPROTO_VRRP, Q_IP); |
5498 | | |
5499 | 0 | #ifndef IPPROTO_CARP |
5500 | 0 | #define IPPROTO_CARP 112 |
5501 | 0 | #endif |
5502 | | |
5503 | 0 | case Q_CARP: |
5504 | 0 | return gen_proto(cstate, IPPROTO_CARP, Q_IP); |
5505 | | |
5506 | 0 | case Q_IP: |
5507 | 0 | return gen_linktype(cstate, ETHERTYPE_IP); |
5508 | | |
5509 | 0 | case Q_ARP: |
5510 | 0 | return gen_linktype(cstate, ETHERTYPE_ARP); |
5511 | | |
5512 | 0 | case Q_RARP: |
5513 | 0 | return gen_linktype(cstate, ETHERTYPE_REVARP); |
5514 | | |
5515 | 0 | case Q_ATALK: |
5516 | 0 | return gen_linktype(cstate, ETHERTYPE_ATALK); |
5517 | | |
5518 | 0 | case Q_AARP: |
5519 | 0 | return gen_linktype(cstate, ETHERTYPE_AARP); |
5520 | | |
5521 | 0 | case Q_DECNET: |
5522 | 0 | return gen_linktype(cstate, ETHERTYPE_DN); |
5523 | | |
5524 | 0 | case Q_SCA: |
5525 | 0 | return gen_linktype(cstate, ETHERTYPE_SCA); |
5526 | | |
5527 | 0 | case Q_LAT: |
5528 | 0 | return gen_linktype(cstate, ETHERTYPE_LAT); |
5529 | | |
5530 | 0 | case Q_MOPDL: |
5531 | 0 | return gen_linktype(cstate, ETHERTYPE_MOPDL); |
5532 | | |
5533 | 0 | case Q_MOPRC: |
5534 | 0 | return gen_linktype(cstate, ETHERTYPE_MOPRC); |
5535 | | |
5536 | 0 | case Q_IPV6: |
5537 | 0 | return gen_linktype(cstate, ETHERTYPE_IPV6); |
5538 | | |
5539 | | #ifndef IPPROTO_ICMPV6 |
5540 | | #define IPPROTO_ICMPV6 58 |
5541 | | #endif |
5542 | 0 | case Q_ICMPV6: |
5543 | 0 | return gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6); |
5544 | | |
5545 | | #ifndef IPPROTO_AH |
5546 | | #define IPPROTO_AH 51 |
5547 | | #endif |
5548 | 0 | case Q_AH: |
5549 | 0 | return gen_proto(cstate, IPPROTO_AH, Q_DEFAULT); |
5550 | | |
5551 | | #ifndef IPPROTO_ESP |
5552 | | #define IPPROTO_ESP 50 |
5553 | | #endif |
5554 | 0 | case Q_ESP: |
5555 | 0 | return gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT); |
5556 | | |
5557 | 0 | case Q_ISO: |
5558 | 0 | return gen_linktype(cstate, LLCSAP_ISONS); |
5559 | | |
5560 | 0 | case Q_ESIS: |
5561 | 0 | return gen_proto(cstate, ISO9542_ESIS, Q_ISO); |
5562 | | |
5563 | 0 | case Q_ISIS: |
5564 | 0 | return gen_proto(cstate, ISO10589_ISIS, Q_ISO); |
5565 | | |
5566 | 0 | case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */ |
5567 | 0 | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS); |
5568 | 0 | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); /* FIXME extract the circuit-type bits */ |
5569 | 0 | b1 = gen_or(b0, b1); |
5570 | 0 | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS); |
5571 | 0 | b1 = gen_or(b0, b1); |
5572 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5573 | 0 | b1 = gen_or(b0, b1); |
5574 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5575 | 0 | return gen_or(b0, b1); |
5576 | | |
5577 | 0 | case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */ |
5578 | 0 | b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS); |
5579 | 0 | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); /* FIXME extract the circuit-type bits */ |
5580 | 0 | b1 = gen_or(b0, b1); |
5581 | 0 | b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS); |
5582 | 0 | b1 = gen_or(b0, b1); |
5583 | 0 | b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5584 | 0 | b1 = gen_or(b0, b1); |
5585 | 0 | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5586 | 0 | return gen_or(b0, b1); |
5587 | | |
5588 | 0 | case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */ |
5589 | 0 | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS); |
5590 | 0 | b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS); |
5591 | 0 | b1 = gen_or(b0, b1); |
5592 | 0 | b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); |
5593 | 0 | return gen_or(b0, b1); |
5594 | | |
5595 | 0 | case Q_ISIS_LSP: |
5596 | 0 | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS); |
5597 | 0 | b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS); |
5598 | 0 | return gen_or(b0, b1); |
5599 | | |
5600 | 0 | case Q_ISIS_SNP: |
5601 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5602 | 0 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5603 | 0 | b1 = gen_or(b0, b1); |
5604 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5605 | 0 | b1 = gen_or(b0, b1); |
5606 | 0 | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5607 | 0 | return gen_or(b0, b1); |
5608 | | |
5609 | 0 | case Q_ISIS_CSNP: |
5610 | 0 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5611 | 0 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5612 | 0 | return gen_or(b0, b1); |
5613 | | |
5614 | 0 | case Q_ISIS_PSNP: |
5615 | 0 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5616 | 0 | b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5617 | 0 | return gen_or(b0, b1); |
5618 | | |
5619 | 0 | case Q_CLNP: |
5620 | 0 | return gen_proto(cstate, ISO8473_CLNP, Q_ISO); |
5621 | | |
5622 | 0 | case Q_STP: |
5623 | 0 | return gen_linktype(cstate, LLCSAP_8021D); |
5624 | | |
5625 | 0 | case Q_IPX: |
5626 | 0 | return gen_linktype(cstate, LLCSAP_IPX); |
5627 | | |
5628 | 0 | case Q_NETBEUI: |
5629 | 0 | return gen_linktype(cstate, LLCSAP_NETBEUI); |
5630 | 0 | } |
5631 | 0 | bpf_error(cstate, "'%s' cannot be used as an abbreviation", pqkw(proto)); |
5632 | 0 | } |
5633 | | |
5634 | | struct block * |
5635 | | gen_proto_abbrev(compiler_state_t *cstate, int proto) |
5636 | 0 | { |
5637 | | /* |
5638 | | * Catch errors reported by us and routines below us, and return NULL |
5639 | | * on an error. |
5640 | | */ |
5641 | 0 | if (setjmp(cstate->top_ctx)) |
5642 | 0 | return (NULL); |
5643 | | |
5644 | 0 | return gen_proto_abbrev_internal(cstate, proto); |
5645 | 0 | } |
5646 | | |
5647 | | static struct block * |
5648 | | gen_ip_proto(compiler_state_t *cstate, const uint8_t proto) |
5649 | 0 | { |
5650 | 0 | return gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto); |
5651 | 0 | } |
5652 | | |
5653 | | static struct block * |
5654 | | gen_ip6_proto(compiler_state_t *cstate, const uint8_t proto) |
5655 | 0 | { |
5656 | 0 | return gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto); |
5657 | 0 | } |
5658 | | |
5659 | | static struct block * |
5660 | | gen_ipfrag(compiler_state_t *cstate) |
5661 | 0 | { |
5662 | 0 | struct slist *s; |
5663 | | |
5664 | | /* not IPv4 frag other than the first frag */ |
5665 | 0 | s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H); |
5666 | 0 | return gen_unset(cstate, 0x1fff, s); |
5667 | 0 | } |
5668 | | |
5669 | | /* |
5670 | | * Generate a comparison to a port value in the transport-layer header |
5671 | | * at the specified offset from the beginning of that header. |
5672 | | * |
5673 | | * XXX - this handles a variable-length prefix preceding the link-layer |
5674 | | * header, such as the radiotap or AVS radio prefix, but doesn't handle |
5675 | | * variable-length link-layer headers (such as Token Ring or 802.11 |
5676 | | * headers). |
5677 | | */ |
5678 | | static struct block * |
5679 | | gen_portatom(compiler_state_t *cstate, int off, uint16_t v) |
5680 | 0 | { |
5681 | 0 | return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v); |
5682 | 0 | } |
5683 | | |
5684 | | static struct block * |
5685 | | gen_portatom6(compiler_state_t *cstate, int off, uint16_t v) |
5686 | 0 | { |
5687 | 0 | return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v); |
5688 | 0 | } |
5689 | | |
5690 | | static struct block * |
5691 | | gen_port(compiler_state_t *cstate, uint16_t port, int proto, int dir) |
5692 | 0 | { |
5693 | 0 | struct block *b1, *tmp; |
5694 | |
|
5695 | 0 | switch (dir) { |
5696 | 0 | case Q_SRC: |
5697 | 0 | b1 = gen_portatom(cstate, 0, port); |
5698 | 0 | break; |
5699 | | |
5700 | 0 | case Q_DST: |
5701 | 0 | b1 = gen_portatom(cstate, 2, port); |
5702 | 0 | break; |
5703 | | |
5704 | 0 | case Q_AND: |
5705 | 0 | tmp = gen_portatom(cstate, 0, port); |
5706 | 0 | b1 = gen_portatom(cstate, 2, port); |
5707 | 0 | b1 = gen_and(tmp, b1); |
5708 | 0 | break; |
5709 | | |
5710 | 0 | case Q_DEFAULT: |
5711 | 0 | case Q_OR: |
5712 | 0 | tmp = gen_portatom(cstate, 0, port); |
5713 | 0 | b1 = gen_portatom(cstate, 2, port); |
5714 | 0 | b1 = gen_or(tmp, b1); |
5715 | 0 | break; |
5716 | | |
5717 | 0 | default: |
5718 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "port"); |
5719 | | /*NOTREACHED*/ |
5720 | 0 | } |
5721 | | |
5722 | 0 | return gen_port_common(cstate, proto, b1); |
5723 | 0 | } |
5724 | | |
5725 | | static struct block * |
5726 | | gen_port_common(compiler_state_t *cstate, int proto, struct block *b1) |
5727 | 0 | { |
5728 | 0 | struct block *b0, *tmp; |
5729 | | |
5730 | | /* |
5731 | | * ether proto ip |
5732 | | * |
5733 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
5734 | | * not LLC encapsulation with LLCSAP_IP. |
5735 | | * |
5736 | | * For IEEE 802 networks - which includes 802.5 token ring |
5737 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
5738 | | * says that SNAP encapsulation is used, not LLC encapsulation |
5739 | | * with LLCSAP_IP. |
5740 | | * |
5741 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
5742 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
5743 | | * encapsulation with LLCSAP_IP. |
5744 | | * |
5745 | | * So we always check for ETHERTYPE_IP. |
5746 | | * |
5747 | | * At the time of this writing all three L4 protocols the "port" and |
5748 | | * "portrange" primitives support (TCP, UDP and SCTP) have the source |
5749 | | * and the destination ports identically encoded in the transport |
5750 | | * protocol header. So without a proto qualifier the only difference |
5751 | | * between the implemented cases is the protocol number and all other |
5752 | | * checks need to be made exactly once. |
5753 | | * |
5754 | | * If the expression syntax in future starts to support ports for |
5755 | | * another L4 protocol that has unsigned integer ports encoded using a |
5756 | | * different size and/or offset, this will require a different code. |
5757 | | */ |
5758 | 0 | switch (proto) { |
5759 | 0 | case IPPROTO_UDP: |
5760 | 0 | case IPPROTO_TCP: |
5761 | 0 | case IPPROTO_SCTP: |
5762 | 0 | tmp = gen_ip_proto(cstate, (uint8_t)proto); |
5763 | 0 | break; |
5764 | | |
5765 | 0 | case PROTO_UNDEF: |
5766 | 0 | tmp = gen_ip_proto(cstate, IPPROTO_SCTP); |
5767 | 0 | tmp = gen_or(gen_ip_proto(cstate, IPPROTO_UDP), tmp); |
5768 | 0 | tmp = gen_or(gen_ip_proto(cstate, IPPROTO_TCP), tmp); |
5769 | 0 | break; |
5770 | | |
5771 | 0 | default: |
5772 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "proto", proto); |
5773 | 0 | } |
5774 | | // Not a fragment other than the first fragment. |
5775 | 0 | b0 = gen_ipfrag(cstate); |
5776 | 0 | b0 = gen_and(tmp, b0); |
5777 | 0 | b1 = gen_and(b0, b1); |
5778 | | // "link proto \ip" |
5779 | 0 | return gen_and(gen_linktype(cstate, ETHERTYPE_IP), b1); |
5780 | 0 | } |
5781 | | |
5782 | | static struct block * |
5783 | | gen_port6(compiler_state_t *cstate, uint16_t port, int proto, int dir) |
5784 | 0 | { |
5785 | 0 | struct block *b1, *tmp; |
5786 | |
|
5787 | 0 | switch (dir) { |
5788 | 0 | case Q_SRC: |
5789 | 0 | b1 = gen_portatom6(cstate, 0, port); |
5790 | 0 | break; |
5791 | | |
5792 | 0 | case Q_DST: |
5793 | 0 | b1 = gen_portatom6(cstate, 2, port); |
5794 | 0 | break; |
5795 | | |
5796 | 0 | case Q_AND: |
5797 | 0 | tmp = gen_portatom6(cstate, 0, port); |
5798 | 0 | b1 = gen_portatom6(cstate, 2, port); |
5799 | 0 | b1 = gen_and(tmp, b1); |
5800 | 0 | break; |
5801 | | |
5802 | 0 | case Q_DEFAULT: |
5803 | 0 | case Q_OR: |
5804 | 0 | tmp = gen_portatom6(cstate, 0, port); |
5805 | 0 | b1 = gen_portatom6(cstate, 2, port); |
5806 | 0 | b1 = gen_or(tmp, b1); |
5807 | 0 | break; |
5808 | | |
5809 | 0 | default: |
5810 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "port"); |
5811 | | /*NOTREACHED*/ |
5812 | 0 | } |
5813 | | |
5814 | 0 | return gen_port6_common(cstate, proto, b1); |
5815 | 0 | } |
5816 | | |
5817 | | static struct block * |
5818 | | gen_port6_common(compiler_state_t *cstate, int proto, struct block *b1) |
5819 | 0 | { |
5820 | 0 | struct block *tmp; |
5821 | | |
5822 | | // "ip6 proto 'ip_proto'" |
5823 | 0 | switch (proto) { |
5824 | 0 | case IPPROTO_UDP: |
5825 | 0 | case IPPROTO_TCP: |
5826 | 0 | case IPPROTO_SCTP: |
5827 | 0 | tmp = gen_ip6_proto(cstate, (uint8_t)proto); |
5828 | 0 | break; |
5829 | | |
5830 | 0 | case PROTO_UNDEF: |
5831 | | // Same as in gen_port_common(). |
5832 | 0 | tmp = gen_ip6_proto(cstate, IPPROTO_SCTP); |
5833 | 0 | tmp = gen_or(gen_ip6_proto(cstate, IPPROTO_UDP), tmp); |
5834 | 0 | tmp = gen_or(gen_ip6_proto(cstate, IPPROTO_TCP), tmp); |
5835 | 0 | break; |
5836 | | |
5837 | 0 | default: |
5838 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "proto", proto); |
5839 | 0 | } |
5840 | | // XXX - catch the first fragment of a fragmented packet? |
5841 | 0 | b1 = gen_and(tmp, b1); |
5842 | | // "link proto \ip6" |
5843 | 0 | return gen_and(gen_linktype(cstate, ETHERTYPE_IPV6), b1); |
5844 | 0 | } |
5845 | | |
5846 | | /* gen_portrange code */ |
5847 | | static struct block * |
5848 | | gen_portrangeatom(compiler_state_t *cstate, u_int off, uint16_t v1, |
5849 | | uint16_t v2) |
5850 | 0 | { |
5851 | 0 | if (v1 == v2) |
5852 | 0 | return gen_portatom(cstate, off, v1); |
5853 | | |
5854 | 0 | struct block *b1, *b2; |
5855 | |
|
5856 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, min(v1, v2)); |
5857 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, max(v1, v2)); |
5858 | |
|
5859 | 0 | return gen_and(b1, b2); |
5860 | 0 | } |
5861 | | |
5862 | | static struct block * |
5863 | | gen_portrange(compiler_state_t *cstate, uint16_t port1, uint16_t port2, |
5864 | | int proto, int dir) |
5865 | 0 | { |
5866 | 0 | struct block *b1, *tmp; |
5867 | |
|
5868 | 0 | switch (dir) { |
5869 | 0 | case Q_SRC: |
5870 | 0 | b1 = gen_portrangeatom(cstate, 0, port1, port2); |
5871 | 0 | break; |
5872 | | |
5873 | 0 | case Q_DST: |
5874 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5875 | 0 | break; |
5876 | | |
5877 | 0 | case Q_AND: |
5878 | 0 | tmp = gen_portrangeatom(cstate, 0, port1, port2); |
5879 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5880 | 0 | b1 = gen_and(tmp, b1); |
5881 | 0 | break; |
5882 | | |
5883 | 0 | case Q_DEFAULT: |
5884 | 0 | case Q_OR: |
5885 | 0 | tmp = gen_portrangeatom(cstate, 0, port1, port2); |
5886 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5887 | 0 | b1 = gen_or(tmp, b1); |
5888 | 0 | break; |
5889 | | |
5890 | 0 | default: |
5891 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "portrange"); |
5892 | | /*NOTREACHED*/ |
5893 | 0 | } |
5894 | | |
5895 | 0 | return gen_port_common(cstate, proto, b1); |
5896 | 0 | } |
5897 | | |
5898 | | static struct block * |
5899 | | gen_portrangeatom6(compiler_state_t *cstate, u_int off, uint16_t v1, |
5900 | | uint16_t v2) |
5901 | 0 | { |
5902 | 0 | if (v1 == v2) |
5903 | 0 | return gen_portatom6(cstate, off, v1); |
5904 | | |
5905 | 0 | struct block *b1, *b2; |
5906 | |
|
5907 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, min(v1, v2)); |
5908 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, max(v1, v2)); |
5909 | |
|
5910 | 0 | return gen_and(b1, b2); |
5911 | 0 | } |
5912 | | |
5913 | | static struct block * |
5914 | | gen_portrange6(compiler_state_t *cstate, uint16_t port1, uint16_t port2, |
5915 | | int proto, int dir) |
5916 | 0 | { |
5917 | 0 | struct block *b1, *tmp; |
5918 | |
|
5919 | 0 | switch (dir) { |
5920 | 0 | case Q_SRC: |
5921 | 0 | b1 = gen_portrangeatom6(cstate, 0, port1, port2); |
5922 | 0 | break; |
5923 | | |
5924 | 0 | case Q_DST: |
5925 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5926 | 0 | break; |
5927 | | |
5928 | 0 | case Q_AND: |
5929 | 0 | tmp = gen_portrangeatom6(cstate, 0, port1, port2); |
5930 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5931 | 0 | b1 = gen_and(tmp, b1); |
5932 | 0 | break; |
5933 | | |
5934 | 0 | case Q_DEFAULT: |
5935 | 0 | case Q_OR: |
5936 | 0 | tmp = gen_portrangeatom6(cstate, 0, port1, port2); |
5937 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5938 | 0 | b1 = gen_or(tmp, b1); |
5939 | 0 | break; |
5940 | | |
5941 | 0 | default: |
5942 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "portrange"); |
5943 | | /*NOTREACHED*/ |
5944 | 0 | } |
5945 | | |
5946 | 0 | return gen_port6_common(cstate, proto, b1); |
5947 | 0 | } |
5948 | | |
5949 | | static int |
5950 | | lookup_proto(compiler_state_t *cstate, const char *name, const struct qual q) |
5951 | 0 | { |
5952 | | /* |
5953 | | * Do not check here whether q.proto is valid (e.g. in "udp proto abc" |
5954 | | * fail the "abc", but not the "udp proto"). Likewise, do not check |
5955 | | * here whether the combination of q.proto and q.addr is valid (e.g. |
5956 | | * in "(link|iso|isis) protochain abc" fail the "abc", but not the |
5957 | | * "(link|iso|isis) protochain"). |
5958 | | * |
5959 | | * On the one hand, this avoids a layering violation: gen_proto() and |
5960 | | * gen_protochain() implement the semantic checks. On the other hand, |
5961 | | * the protocol name lookup error arguably is a problem smaller than |
5962 | | * the semantic error, hence the latter ought to be the reported cause |
5963 | | * of failure in both cases. In future this potentially could be made |
5964 | | * more consistent by attempting the lookup after the semantic checks. |
5965 | | */ |
5966 | |
|
5967 | 0 | int v = PROTO_UNDEF; |
5968 | 0 | switch (q.proto) { |
5969 | | |
5970 | 0 | case Q_DEFAULT: |
5971 | 0 | case Q_IP: |
5972 | 0 | case Q_IPV6: |
5973 | 0 | v = pcap_nametoproto(name); |
5974 | 0 | break; |
5975 | | |
5976 | 0 | case Q_LINK: |
5977 | | /* XXX should look up h/w protocol type based on cstate->linktype */ |
5978 | 0 | v = pcap_nametoeproto(name); |
5979 | 0 | if (v == PROTO_UNDEF) |
5980 | 0 | v = pcap_nametollc(name); |
5981 | 0 | break; |
5982 | | |
5983 | 0 | case Q_ISO: |
5984 | 0 | if (strcmp(name, "esis") == 0) |
5985 | 0 | v = ISO9542_ESIS; |
5986 | 0 | else if (strcmp(name, "isis") == 0) |
5987 | 0 | v = ISO10589_ISIS; |
5988 | 0 | else if (strcmp(name, "clnp") == 0) |
5989 | 0 | v = ISO8473_CLNP; |
5990 | 0 | break; |
5991 | | |
5992 | | // "isis proto" is a valid syntax, but it takes only numeric IDs. |
5993 | 0 | } |
5994 | | // In theory, the only possible negative value of v is PROTO_UNDEF. |
5995 | 0 | if (v >= 0) |
5996 | 0 | return v; |
5997 | | |
5998 | 0 | if (q.proto == Q_DEFAULT) |
5999 | 0 | bpf_error(cstate, "unknown '%s' value '%s'", |
6000 | 0 | tqkw(q.addr), name); |
6001 | 0 | bpf_error(cstate, "unknown '%s %s' value '%s'", |
6002 | 0 | pqkw(q.proto), tqkw(q.addr), name); |
6003 | 0 | } |
6004 | | |
6005 | | #if !defined(NO_PROTOCHAIN) |
6006 | | /* |
6007 | | * This primitive is non-directional by design, so the grammar does not allow |
6008 | | * to qualify it with a direction. |
6009 | | */ |
6010 | | static struct block * |
6011 | | gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto) |
6012 | 0 | { |
6013 | 0 | struct block *b0, *b; |
6014 | 0 | struct slist *s[100]; |
6015 | 0 | int fix2, fix3, fix4, fix5; |
6016 | 0 | int ahcheck, again, end; |
6017 | 0 | int i, max; |
6018 | 0 | int reg2 = alloc_reg(cstate); |
6019 | |
|
6020 | 0 | memset(s, 0, sizeof(s)); |
6021 | 0 | fix3 = fix4 = fix5 = 0; |
6022 | |
|
6023 | 0 | switch (proto) { |
6024 | 0 | case Q_IP: |
6025 | 0 | case Q_IPV6: |
6026 | 0 | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6027 | 0 | break; |
6028 | 0 | case Q_DEFAULT: |
6029 | 0 | b0 = gen_protochain(cstate, v, Q_IP); |
6030 | 0 | b = gen_protochain(cstate, v, Q_IPV6); |
6031 | 0 | return gen_or(b0, b); |
6032 | 0 | default: |
6033 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "protochain"); |
6034 | | /*NOTREACHED*/ |
6035 | 0 | } |
6036 | | |
6037 | | /* |
6038 | | * We don't handle variable-length prefixes before the link-layer |
6039 | | * header, or variable-length link-layer headers, here yet. |
6040 | | * We might want to add BPF instructions to do the protochain |
6041 | | * work, to simplify that and, on platforms that have a BPF |
6042 | | * interpreter with the new instructions, let the filtering |
6043 | | * be done in the kernel. (We already require a modified BPF |
6044 | | * engine to do the protochain stuff, to support backward |
6045 | | * branches, and backward branch support is unlikely to appear |
6046 | | * in kernel BPF engines.) |
6047 | | * |
6048 | | * Hence in the current implementation the gen_abs_offset_varpart() |
6049 | | * invocations incurred from gen_load_a() and gen_loadx_iphdrlen() |
6050 | | * below do not affect the offset because off_linkpl.is_variable == 0. |
6051 | | */ |
6052 | 0 | if (cstate->off_linkpl.is_variable) |
6053 | 0 | bpf_error(cstate, "'protochain' not supported with variable length headers"); |
6054 | | |
6055 | | /* |
6056 | | * To quote a comment in optimize.c: |
6057 | | * |
6058 | | * "These data structures are used in a Cocke and Schwartz style |
6059 | | * value numbering scheme. Since the flowgraph is acyclic, |
6060 | | * exit values can be propagated from a node's predecessors |
6061 | | * provided it is uniquely defined." |
6062 | | * |
6063 | | * "Acyclic" means "no backward branches", which means "no |
6064 | | * loops", so we have to turn the optimizer off. |
6065 | | */ |
6066 | 0 | cstate->no_optimize = 1; |
6067 | | |
6068 | | /* |
6069 | | * s[0] is a dummy entry to protect other BPF insn from damage |
6070 | | * by s[fix] = foo with uninitialized variable "fix". It is somewhat |
6071 | | * hard to find interdependency made by jump table fixup. |
6072 | | */ |
6073 | 0 | i = 0; |
6074 | 0 | s[i] = new_stmt(cstate, 0); /*dummy*/ |
6075 | 0 | i++; |
6076 | |
|
6077 | 0 | switch (proto) { |
6078 | 0 | case Q_IP: |
6079 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6080 | | |
6081 | | /* A = ip->ip_p */ |
6082 | 0 | s[i] = gen_load_a(cstate, OR_LINKPL, 9, BPF_B); |
6083 | 0 | i++; |
6084 | | /* X = ip->ip_hl << 2 */ |
6085 | 0 | s[i] = gen_loadx_iphdrlen(cstate); |
6086 | 0 | i++; |
6087 | 0 | break; |
6088 | | |
6089 | 0 | case Q_IPV6: |
6090 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6091 | | |
6092 | | /* A = ip6->ip_nxt */ |
6093 | 0 | s[i] = gen_load_a(cstate, OR_LINKPL, 6, BPF_B); |
6094 | 0 | i++; |
6095 | | /* X = sizeof(struct ip6_hdr) */ |
6096 | 0 | s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM); |
6097 | 0 | s[i]->s.k = 40; |
6098 | 0 | i++; |
6099 | 0 | break; |
6100 | | |
6101 | 0 | default: |
6102 | 0 | bpf_error(cstate, "unsupported proto to gen_protochain"); |
6103 | | /*NOTREACHED*/ |
6104 | 0 | } |
6105 | | |
6106 | | /* again: if (A == v) goto end; else fall through; */ |
6107 | 0 | again = i; |
6108 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6109 | 0 | s[i]->s.k = v; |
6110 | 0 | s[i]->s.jt = NULL; /*later*/ |
6111 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6112 | 0 | fix5 = i; |
6113 | 0 | i++; |
6114 | |
|
6115 | | #ifndef IPPROTO_NONE |
6116 | | #define IPPROTO_NONE 59 |
6117 | | #endif |
6118 | | /* if (A == IPPROTO_NONE) goto end */ |
6119 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6120 | 0 | s[i]->s.jt = NULL; /*later*/ |
6121 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6122 | 0 | s[i]->s.k = IPPROTO_NONE; |
6123 | 0 | s[fix5]->s.jf = s[i]; |
6124 | 0 | fix2 = i; |
6125 | 0 | i++; |
6126 | |
|
6127 | 0 | if (proto == Q_IPV6) { |
6128 | 0 | int v6start, v6end, v6advance, j; |
6129 | |
|
6130 | 0 | v6start = i; |
6131 | | /* if (A == IPPROTO_HOPOPTS) goto v6advance */ |
6132 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6133 | 0 | s[i]->s.jt = NULL; /*later*/ |
6134 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6135 | 0 | s[i]->s.k = IPPROTO_HOPOPTS; |
6136 | 0 | s[fix2]->s.jf = s[i]; |
6137 | 0 | i++; |
6138 | | /* if (A == IPPROTO_DSTOPTS) goto v6advance */ |
6139 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6140 | 0 | s[i]->s.jt = NULL; /*later*/ |
6141 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6142 | 0 | s[i]->s.k = IPPROTO_DSTOPTS; |
6143 | 0 | i++; |
6144 | | /* if (A == IPPROTO_ROUTING) goto v6advance */ |
6145 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6146 | 0 | s[i]->s.jt = NULL; /*later*/ |
6147 | 0 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6148 | 0 | s[i]->s.k = IPPROTO_ROUTING; |
6149 | 0 | i++; |
6150 | | /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ |
6151 | 0 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6152 | 0 | s[i]->s.jt = NULL; /*later*/ |
6153 | 0 | s[i]->s.jf = NULL; /*later*/ |
6154 | 0 | s[i]->s.k = IPPROTO_FRAGMENT; |
6155 | 0 | fix3 = i; |
6156 | 0 | v6end = i; |
6157 | 0 | i++; |
6158 | | |
6159 | | /* v6advance: */ |
6160 | 0 | v6advance = i; |
6161 | | |
6162 | | /* |
6163 | | * in short, |
6164 | | * A = P[X + packet head]; |
6165 | | * X = X + (P[X + packet head + 1] + 1) * 8; |
6166 | | */ |
6167 | | /* A = P[X + packet head] */ |
6168 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6169 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6170 | 0 | i++; |
6171 | | /* MEM[reg2] = A */ |
6172 | 0 | s[i] = new_stmt(cstate, BPF_ST); |
6173 | 0 | s[i]->s.k = reg2; |
6174 | 0 | i++; |
6175 | | /* A = P[X + packet head + 1]; */ |
6176 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6177 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1; |
6178 | 0 | i++; |
6179 | | /* A += 1 */ |
6180 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6181 | 0 | s[i]->s.k = 1; |
6182 | 0 | i++; |
6183 | | /* A *= 8 */ |
6184 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6185 | 0 | s[i]->s.k = 8; |
6186 | 0 | i++; |
6187 | | /* A += X */ |
6188 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
6189 | 0 | s[i]->s.k = 0; |
6190 | 0 | i++; |
6191 | | /* X = A; */ |
6192 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6193 | 0 | i++; |
6194 | | /* A = MEM[reg2] */ |
6195 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6196 | 0 | s[i]->s.k = reg2; |
6197 | 0 | i++; |
6198 | | |
6199 | | /* goto again; (must use BPF_JA for backward jump) */ |
6200 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JA, BPF_K)); |
6201 | 0 | s[i]->s.k = again - i - 1; |
6202 | 0 | s[i - 1]->s.jf = s[i]; |
6203 | 0 | i++; |
6204 | | |
6205 | | /* fixup */ |
6206 | 0 | for (j = v6start; j <= v6end; j++) |
6207 | 0 | s[j]->s.jt = s[v6advance]; |
6208 | 0 | } else { |
6209 | | /* nop */ |
6210 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6211 | 0 | s[i]->s.k = 0; |
6212 | 0 | s[fix2]->s.jf = s[i]; |
6213 | 0 | i++; |
6214 | 0 | } |
6215 | | |
6216 | | /* ahcheck: */ |
6217 | 0 | ahcheck = i; |
6218 | | /* if (A == IPPROTO_AH) then fall through; else goto end; */ |
6219 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
6220 | 0 | s[i]->s.jt = NULL; /*later*/ |
6221 | 0 | s[i]->s.jf = NULL; /*later*/ |
6222 | 0 | s[i]->s.k = IPPROTO_AH; |
6223 | 0 | if (fix3) |
6224 | 0 | s[fix3]->s.jf = s[ahcheck]; |
6225 | 0 | fix4 = i; |
6226 | 0 | i++; |
6227 | | |
6228 | | /* |
6229 | | * in short, |
6230 | | * A = P[X]; |
6231 | | * X = X + (P[X + 1] + 2) * 4; |
6232 | | */ |
6233 | | /* A = P[X + packet head]; */ |
6234 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6235 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6236 | 0 | s[i - 1]->s.jt = s[i]; |
6237 | 0 | i++; |
6238 | | /* MEM[reg2] = A */ |
6239 | 0 | s[i] = new_stmt(cstate, BPF_ST); |
6240 | 0 | s[i]->s.k = reg2; |
6241 | 0 | i++; |
6242 | | /* A = X */ |
6243 | 0 | s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); |
6244 | 0 | i++; |
6245 | | /* A += 1 */ |
6246 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6247 | 0 | s[i]->s.k = 1; |
6248 | 0 | i++; |
6249 | | /* X = A */ |
6250 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6251 | 0 | i++; |
6252 | | /* A = P[X + packet head] */ |
6253 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6254 | 0 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6255 | 0 | i++; |
6256 | | /* A += 2 */ |
6257 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6258 | 0 | s[i]->s.k = 2; |
6259 | 0 | i++; |
6260 | | /* A *= 4 */ |
6261 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6262 | 0 | s[i]->s.k = 4; |
6263 | 0 | i++; |
6264 | | /* X = A; */ |
6265 | 0 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6266 | 0 | i++; |
6267 | | /* A = MEM[reg2] */ |
6268 | 0 | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6269 | 0 | s[i]->s.k = reg2; |
6270 | 0 | i++; |
6271 | | |
6272 | | /* goto again; (must use BPF_JA for backward jump) */ |
6273 | 0 | s[i] = new_stmt(cstate, JMP(BPF_JA, BPF_K)); |
6274 | 0 | s[i]->s.k = again - i - 1; |
6275 | 0 | i++; |
6276 | | |
6277 | | /* end: nop */ |
6278 | 0 | end = i; |
6279 | 0 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6280 | 0 | s[i]->s.k = 0; |
6281 | 0 | s[fix2]->s.jt = s[end]; |
6282 | 0 | s[fix4]->s.jf = s[end]; |
6283 | 0 | s[fix5]->s.jt = s[end]; |
6284 | 0 | i++; |
6285 | | |
6286 | | /* |
6287 | | * make slist chain |
6288 | | */ |
6289 | 0 | max = i; |
6290 | 0 | for (i = 0; i < max - 1; i++) |
6291 | 0 | s[i]->next = s[i + 1]; |
6292 | 0 | s[max - 1]->next = NULL; |
6293 | | |
6294 | | /* |
6295 | | * emit final check |
6296 | | * Remember, s[0] is dummy. |
6297 | | */ |
6298 | 0 | b = gen_jmp_k(cstate, BPF_JEQ, v, s[1]); |
6299 | |
|
6300 | 0 | free_reg(cstate, reg2); |
6301 | |
|
6302 | 0 | return gen_and(b0, b); |
6303 | 0 | } |
6304 | | #endif /* !defined(NO_PROTOCHAIN) */ |
6305 | | |
6306 | | /* |
6307 | | * Generate code that checks whether the packet is a packet for protocol |
6308 | | * <proto> and whether the type field in that protocol's header has |
6309 | | * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an |
6310 | | * IP packet and checks the protocol number in the IP header against <v>. |
6311 | | * |
6312 | | * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks |
6313 | | * against Q_IP and Q_IPV6. |
6314 | | * |
6315 | | * This primitive is non-directional by design, so the grammar does not allow |
6316 | | * to qualify it with a direction. |
6317 | | */ |
6318 | | static struct block * |
6319 | | gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto) |
6320 | 0 | { |
6321 | 0 | struct block *b0, *b1; |
6322 | 0 | struct block *b2; |
6323 | |
|
6324 | 0 | switch (proto) { |
6325 | 0 | case Q_DEFAULT: |
6326 | 0 | b0 = gen_proto(cstate, v, Q_IP); |
6327 | 0 | b1 = gen_proto(cstate, v, Q_IPV6); |
6328 | 0 | return gen_or(b0, b1); |
6329 | | |
6330 | 0 | case Q_LINK: |
6331 | 0 | return gen_linktype(cstate, v); |
6332 | | |
6333 | 0 | case Q_IP: |
6334 | 0 | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6335 | | /* |
6336 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
6337 | | * not LLC encapsulation with LLCSAP_IP. |
6338 | | * |
6339 | | * For IEEE 802 networks - which includes 802.5 token ring |
6340 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
6341 | | * says that SNAP encapsulation is used, not LLC encapsulation |
6342 | | * with LLCSAP_IP. |
6343 | | * |
6344 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
6345 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
6346 | | * encapsulation with LLCSAP_IP. |
6347 | | * |
6348 | | * So we always check for ETHERTYPE_IP. |
6349 | | */ |
6350 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6351 | | // 0 <= v <= UINT8_MAX |
6352 | 0 | b1 = gen_ip_proto(cstate, (uint8_t)v); |
6353 | 0 | return gen_and(b0, b1); |
6354 | | |
6355 | 0 | case Q_IPV6: |
6356 | 0 | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6357 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6358 | | /* |
6359 | | * Also check for a fragment header before the final |
6360 | | * header. |
6361 | | */ |
6362 | 0 | b2 = gen_ip6_proto(cstate, IPPROTO_FRAGMENT); |
6363 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v); |
6364 | 0 | b1 = gen_and(b2, b1); |
6365 | | // 0 <= v <= UINT8_MAX |
6366 | 0 | b2 = gen_ip6_proto(cstate, (uint8_t)v); |
6367 | 0 | b1 = gen_or(b2, b1); |
6368 | 0 | return gen_and(b0, b1); |
6369 | | |
6370 | 0 | case Q_ISO: |
6371 | 0 | assert_maxval(cstate, "ISO protocol", v, UINT8_MAX); |
6372 | 0 | switch (cstate->linktype) { |
6373 | | |
6374 | 0 | case DLT_FRELAY: |
6375 | | /* |
6376 | | * Frame Relay packets typically have an OSI |
6377 | | * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)" |
6378 | | * generates code to check for all the OSI |
6379 | | * NLPIDs, so calling it and then adding a check |
6380 | | * for the particular NLPID for which we're |
6381 | | * looking is bogus, as we can just check for |
6382 | | * the NLPID. |
6383 | | * |
6384 | | * What we check for is the NLPID and a frame |
6385 | | * control field value of UI, i.e. 0x03 followed |
6386 | | * by the NLPID. |
6387 | | * |
6388 | | * XXX - assumes a 2-byte Frame Relay header with |
6389 | | * DLCI and flags. What if the address is longer? |
6390 | | * |
6391 | | * XXX - what about SNAP-encapsulated frames? |
6392 | | */ |
6393 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v); |
6394 | | /*NOTREACHED*/ |
6395 | | |
6396 | 0 | case DLT_C_HDLC: |
6397 | 0 | case DLT_HDLC: |
6398 | | /* |
6399 | | * Cisco uses an EtherType lookalike - for OSI, |
6400 | | * it's 0xfefe. |
6401 | | */ |
6402 | 0 | b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS); |
6403 | | /* OSI in C-HDLC is stuffed with a fudge byte */ |
6404 | 0 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v); |
6405 | 0 | return gen_and(b0, b1); |
6406 | | |
6407 | 0 | default: |
6408 | 0 | b0 = gen_linktype(cstate, LLCSAP_ISONS); |
6409 | 0 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v); |
6410 | 0 | return gen_and(b0, b1); |
6411 | 0 | } |
6412 | | |
6413 | 0 | case Q_ISIS: |
6414 | 0 | assert_maxval(cstate, "IS-IS PDU type", v, ISIS_PDU_TYPE_MAX); |
6415 | 0 | b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO); |
6416 | | /* |
6417 | | * 4 is the offset of the PDU type relative to the IS-IS |
6418 | | * header. |
6419 | | * Except when it is not, see above. |
6420 | | */ |
6421 | 0 | unsigned pdu_type_offset; |
6422 | 0 | switch (cstate->linktype) { |
6423 | 0 | case DLT_C_HDLC: |
6424 | 0 | case DLT_HDLC: |
6425 | 0 | pdu_type_offset = 5; |
6426 | 0 | break; |
6427 | 0 | default: |
6428 | 0 | pdu_type_offset = 4; |
6429 | 0 | } |
6430 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL_NOSNAP, pdu_type_offset, BPF_B, |
6431 | 0 | v, ISIS_PDU_TYPE_MAX); |
6432 | 0 | return gen_and(b0, b1); |
6433 | 0 | } |
6434 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "proto"); |
6435 | | /*NOTREACHED*/ |
6436 | 0 | } |
6437 | | |
6438 | | /* |
6439 | | * Convert a non-numeric name to a port number. |
6440 | | */ |
6441 | | static int |
6442 | | nametoport(compiler_state_t *cstate, const char *name, int ipproto) |
6443 | 0 | { |
6444 | 0 | struct addrinfo hints, *res, *ai; |
6445 | 0 | int error; |
6446 | 0 | struct sockaddr_in *in4; |
6447 | 0 | struct sockaddr_in6 *in6; |
6448 | 0 | int port = -1; |
6449 | | |
6450 | | /* |
6451 | | * We check for both TCP and UDP in case there are |
6452 | | * ambiguous entries. |
6453 | | */ |
6454 | 0 | memset(&hints, 0, sizeof(hints)); |
6455 | 0 | hints.ai_family = PF_UNSPEC; |
6456 | 0 | hints.ai_socktype = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM; |
6457 | 0 | hints.ai_protocol = ipproto; |
6458 | 0 | error = getaddrinfo(NULL, name, &hints, &res); |
6459 | 0 | if (error != 0) { |
6460 | 0 | switch (error) { |
6461 | | |
6462 | 0 | case EAI_NONAME: |
6463 | 0 | case EAI_SERVICE: |
6464 | | /* |
6465 | | * No such port. Just return -1. |
6466 | | */ |
6467 | 0 | break; |
6468 | | |
6469 | 0 | #ifdef EAI_SYSTEM |
6470 | 0 | case EAI_SYSTEM: |
6471 | | /* |
6472 | | * We don't use strerror() because it's not |
6473 | | * guaranteed to be thread-safe on all platforms |
6474 | | * (probably because it might use a non-thread-local |
6475 | | * buffer into which to format an error message |
6476 | | * if the error code isn't one for which it has |
6477 | | * a canned string; three cheers for C string |
6478 | | * handling). |
6479 | | */ |
6480 | 0 | bpf_set_error(cstate, "getaddrinfo(\"%s\" fails with system error: %d", |
6481 | 0 | name, errno); |
6482 | 0 | port = -2; /* a real error */ |
6483 | 0 | break; |
6484 | 0 | #endif |
6485 | | |
6486 | 0 | default: |
6487 | | /* |
6488 | | * This is a real error, not just "there's |
6489 | | * no such service name". |
6490 | | * |
6491 | | * We don't use gai_strerror() because it's not |
6492 | | * guaranteed to be thread-safe on all platforms |
6493 | | * (probably because it might use a non-thread-local |
6494 | | * buffer into which to format an error message |
6495 | | * if the error code isn't one for which it has |
6496 | | * a canned string; three cheers for C string |
6497 | | * handling). |
6498 | | */ |
6499 | 0 | bpf_set_error(cstate, "getaddrinfo(\"%s\") fails with error: %d", |
6500 | 0 | name, error); |
6501 | 0 | port = -2; /* a real error */ |
6502 | 0 | break; |
6503 | 0 | } |
6504 | 0 | } else { |
6505 | | /* |
6506 | | * OK, we found it. Did it find anything? |
6507 | | */ |
6508 | 0 | for (ai = res; ai != NULL; ai = ai->ai_next) { |
6509 | | /* |
6510 | | * Does it have an address? |
6511 | | */ |
6512 | 0 | if (ai->ai_addr != NULL) { |
6513 | | /* |
6514 | | * Yes. Get a port number; we're done. |
6515 | | */ |
6516 | 0 | if (ai->ai_addr->sa_family == AF_INET) { |
6517 | 0 | in4 = (struct sockaddr_in *)ai->ai_addr; |
6518 | 0 | port = ntohs(in4->sin_port); |
6519 | 0 | break; |
6520 | 0 | } |
6521 | 0 | if (ai->ai_addr->sa_family == AF_INET6) { |
6522 | 0 | in6 = (struct sockaddr_in6 *)ai->ai_addr; |
6523 | 0 | port = ntohs(in6->sin6_port); |
6524 | 0 | break; |
6525 | 0 | } |
6526 | 0 | } |
6527 | 0 | } |
6528 | 0 | freeaddrinfo(res); |
6529 | 0 | } |
6530 | 0 | return port; |
6531 | 0 | } |
6532 | | |
6533 | | /* |
6534 | | * Convert a string to a port number. |
6535 | | */ |
6536 | | static bpf_u_int32 |
6537 | | stringtoport(compiler_state_t *cstate, const char *string, size_t string_size, |
6538 | | int *proto) |
6539 | 0 | { |
6540 | 0 | stoulen_ret ret; |
6541 | 0 | char *cpy; |
6542 | 0 | bpf_u_int32 val; |
6543 | 0 | int tcp_port = -1; |
6544 | 0 | int udp_port = -1; |
6545 | | |
6546 | | /* |
6547 | | * See if it's a number. |
6548 | | */ |
6549 | 0 | ret = stoulen(string, string_size, &val, cstate); |
6550 | 0 | switch (ret) { |
6551 | | |
6552 | 0 | case STOULEN_OK: |
6553 | | /* Unknown port type - it's just a number. */ |
6554 | 0 | *proto = PROTO_UNDEF; |
6555 | 0 | break; |
6556 | | |
6557 | 0 | case STOULEN_NOT_OCTAL_NUMBER: |
6558 | 0 | case STOULEN_NOT_HEX_NUMBER: |
6559 | 0 | case STOULEN_NOT_DECIMAL_NUMBER: |
6560 | | /* |
6561 | | * Not a valid number; try looking it up as a port. |
6562 | | */ |
6563 | 0 | cpy = malloc(string_size + 1); /* +1 for terminating '\0' */ |
6564 | 0 | memcpy(cpy, string, string_size); |
6565 | 0 | cpy[string_size] = '\0'; |
6566 | 0 | tcp_port = nametoport(cstate, cpy, IPPROTO_TCP); |
6567 | 0 | if (tcp_port == -2) { |
6568 | | /* |
6569 | | * We got a hard error; the error string has |
6570 | | * already been set. |
6571 | | */ |
6572 | 0 | free(cpy); |
6573 | 0 | longjmp(cstate->top_ctx, 1); |
6574 | | /*NOTREACHED*/ |
6575 | 0 | } |
6576 | 0 | udp_port = nametoport(cstate, cpy, IPPROTO_UDP); |
6577 | 0 | if (udp_port == -2) { |
6578 | | /* |
6579 | | * We got a hard error; the error string has |
6580 | | * already been set. |
6581 | | */ |
6582 | 0 | free(cpy); |
6583 | 0 | longjmp(cstate->top_ctx, 1); |
6584 | | /*NOTREACHED*/ |
6585 | 0 | } |
6586 | | |
6587 | | /* |
6588 | | * We need to check /etc/services for ambiguous entries. |
6589 | | * If we find an ambiguous entry, and it has the |
6590 | | * same port number, change the proto to PROTO_UNDEF |
6591 | | * so both TCP and UDP will be checked. |
6592 | | */ |
6593 | 0 | if (tcp_port >= 0) { |
6594 | 0 | val = (bpf_u_int32)tcp_port; |
6595 | 0 | *proto = IPPROTO_TCP; |
6596 | 0 | if (udp_port >= 0) { |
6597 | 0 | if (udp_port == tcp_port) |
6598 | 0 | *proto = PROTO_UNDEF; |
6599 | | #ifdef notdef |
6600 | | else |
6601 | | /* Can't handle ambiguous names that refer |
6602 | | to different port numbers. */ |
6603 | | warning("ambiguous port %s in /etc/services", |
6604 | | cpy); |
6605 | | #endif |
6606 | 0 | } |
6607 | 0 | free(cpy); |
6608 | 0 | break; |
6609 | 0 | } |
6610 | 0 | if (udp_port >= 0) { |
6611 | 0 | val = (bpf_u_int32)udp_port; |
6612 | 0 | *proto = IPPROTO_UDP; |
6613 | 0 | free(cpy); |
6614 | 0 | break; |
6615 | 0 | } |
6616 | 0 | bpf_set_error(cstate, "'%s' is not a valid port", cpy); |
6617 | 0 | free(cpy); |
6618 | 0 | longjmp(cstate->top_ctx, 1); |
6619 | | /*NOTREACHED*/ |
6620 | | #ifdef _AIX |
6621 | | PCAP_UNREACHABLE |
6622 | | #endif /* _AIX */ |
6623 | | |
6624 | 0 | case STOULEN_ERROR: |
6625 | | /* Error already set. */ |
6626 | 0 | longjmp(cstate->top_ctx, 1); |
6627 | | /*NOTREACHED*/ |
6628 | | #ifdef _AIX |
6629 | | PCAP_UNREACHABLE |
6630 | | #endif /* _AIX */ |
6631 | | |
6632 | 0 | default: |
6633 | | /* Should not happen */ |
6634 | 0 | bpf_set_error(cstate, "stoulen returned %d - this should not happen", ret); |
6635 | 0 | longjmp(cstate->top_ctx, 1); |
6636 | | /*NOTREACHED*/ |
6637 | 0 | } |
6638 | 0 | return (val); |
6639 | 0 | } |
6640 | | |
6641 | | /* |
6642 | | * Convert a string in the form PPP-PPP, which correspond to ports, to |
6643 | | * a starting and ending port in a port range. |
6644 | | */ |
6645 | | static void |
6646 | | stringtoportrange(compiler_state_t *cstate, const char *string, |
6647 | | bpf_u_int32 *port1, bpf_u_int32 *port2, int *proto) |
6648 | 0 | { |
6649 | 0 | char *hyphen_off; |
6650 | 0 | const char *first, *second; |
6651 | 0 | size_t first_size, second_size; |
6652 | 0 | int save_proto; |
6653 | |
|
6654 | 0 | if ((hyphen_off = strchr(string, '-')) == NULL) |
6655 | 0 | bpf_error(cstate, "port range '%s' contains no hyphen", string); |
6656 | | |
6657 | | /* |
6658 | | * Make sure there are no other hyphens. |
6659 | | * |
6660 | | * XXX - we support named ports, but there are some port names |
6661 | | * in /etc/services that include hyphens, so this would rule |
6662 | | * that out. |
6663 | | */ |
6664 | 0 | if (strchr(hyphen_off + 1, '-') != NULL) |
6665 | 0 | bpf_error(cstate, "port range '%s' contains more than one hyphen", |
6666 | 0 | string); |
6667 | | |
6668 | | /* |
6669 | | * Get the length of the first port. |
6670 | | */ |
6671 | 0 | first = string; |
6672 | 0 | first_size = hyphen_off - string; |
6673 | 0 | if (first_size == 0) { |
6674 | | /* Range of "-port", which we don't support. */ |
6675 | 0 | bpf_error(cstate, "port range '%s' has no starting port", string); |
6676 | 0 | } |
6677 | | |
6678 | | /* |
6679 | | * Try to convert it to a port. |
6680 | | */ |
6681 | 0 | *port1 = stringtoport(cstate, first, first_size, proto); |
6682 | 0 | save_proto = *proto; |
6683 | | |
6684 | | /* |
6685 | | * Get the length of the second port. |
6686 | | */ |
6687 | 0 | second = hyphen_off + 1; |
6688 | 0 | second_size = strlen(second); |
6689 | 0 | if (second_size == 0) { |
6690 | | /* Range of "port-", which we don't support. */ |
6691 | 0 | bpf_error(cstate, "port range '%s' has no ending port", string); |
6692 | 0 | } |
6693 | | |
6694 | | /* |
6695 | | * Try to convert it to a port. |
6696 | | */ |
6697 | 0 | *port2 = stringtoport(cstate, second, second_size, proto); |
6698 | 0 | if (*proto != save_proto) |
6699 | 0 | *proto = PROTO_UNDEF; |
6700 | 0 | } |
6701 | | |
6702 | | struct block * |
6703 | | gen_scode(compiler_state_t *cstate, const char *name, struct qual q) |
6704 | 0 | { |
6705 | 0 | int proto = q.proto; |
6706 | 0 | int dir = q.dir; |
6707 | 0 | bpf_u_int32 mask, addr; |
6708 | 0 | int port, real_proto; |
6709 | 0 | bpf_u_int32 port1, port2; |
6710 | | |
6711 | | /* |
6712 | | * Catch errors reported by us and routines below us, and return NULL |
6713 | | * on an error. |
6714 | | */ |
6715 | 0 | if (setjmp(cstate->top_ctx)) |
6716 | 0 | return (NULL); |
6717 | | |
6718 | 0 | if (q.proto == Q_DECNET) { |
6719 | | /* |
6720 | | * A long time ago on Ultrix libpcap supported translation of |
6721 | | * DECnet host names into DECnet addresses, but this feature |
6722 | | * is history now. The current implementation does not define |
6723 | | * any primitives that have "decnet" as the protocol qualifier |
6724 | | * and a name as the ID. |
6725 | | */ |
6726 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, "decnet", |
6727 | 0 | tqkw(q.addr == Q_DEFAULT ? Q_HOST : q.addr)); |
6728 | 0 | } |
6729 | | |
6730 | 0 | struct block *b, *b6; |
6731 | 0 | switch (q.addr) { |
6732 | | |
6733 | 0 | case Q_NET: |
6734 | 0 | addr = pcap_nametonetaddr(name); |
6735 | 0 | if (addr == 0) |
6736 | 0 | bpf_error(cstate, "unknown network '%s'", name); |
6737 | | /* Left justify network addr and calculate its network mask */ |
6738 | 0 | mask = 0xffffffff; |
6739 | 0 | while (addr && (addr & 0xff000000) == 0) { |
6740 | 0 | addr <<= 8; |
6741 | 0 | mask <<= 8; |
6742 | 0 | } |
6743 | 0 | return gen_host(cstate, addr, mask, proto, dir, q.addr); |
6744 | | |
6745 | 0 | case Q_DEFAULT: |
6746 | 0 | case Q_HOST: |
6747 | 0 | if (proto == Q_LINK) { |
6748 | 0 | return gen_mac48host_byname(cstate, name, q.dir, "link host NAME"); |
6749 | 0 | } else { |
6750 | 0 | return gen_host46_byname(cstate, name, q.proto, |
6751 | 0 | q.proto, q.dir); |
6752 | 0 | } |
6753 | | |
6754 | 0 | case Q_PORT: |
6755 | 0 | (void)port_pq_to_ipproto(cstate, proto, "port"); // validate only |
6756 | 0 | if (pcap_nametoport(name, &port, &real_proto) == 0) |
6757 | 0 | bpf_error(cstate, "unknown port '%s'", name); |
6758 | 0 | if (proto == Q_UDP) { |
6759 | 0 | if (real_proto == IPPROTO_TCP) |
6760 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6761 | 0 | else if (real_proto == IPPROTO_SCTP) |
6762 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6763 | 0 | else |
6764 | | /* override PROTO_UNDEF */ |
6765 | 0 | real_proto = IPPROTO_UDP; |
6766 | 0 | } |
6767 | 0 | if (proto == Q_TCP) { |
6768 | 0 | if (real_proto == IPPROTO_UDP) |
6769 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6770 | | |
6771 | 0 | else if (real_proto == IPPROTO_SCTP) |
6772 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6773 | 0 | else |
6774 | | /* override PROTO_UNDEF */ |
6775 | 0 | real_proto = IPPROTO_TCP; |
6776 | 0 | } |
6777 | 0 | if (proto == Q_SCTP) { |
6778 | 0 | if (real_proto == IPPROTO_UDP) |
6779 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6780 | | |
6781 | 0 | else if (real_proto == IPPROTO_TCP) |
6782 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6783 | 0 | else |
6784 | | /* override PROTO_UNDEF */ |
6785 | 0 | real_proto = IPPROTO_SCTP; |
6786 | 0 | } |
6787 | | |
6788 | | /* |
6789 | | * These two checks are redundant at this point: here name is |
6790 | | * a string that the lexer does not recognize as a number |
6791 | | * hence did not attempt stoulen(), pcap_nametoport() does not |
6792 | | * use stoulen() and has successfully translated the string to |
6793 | | * an uint16_t value using getaddrinfo(). |
6794 | | */ |
6795 | 0 | if (port < 0) |
6796 | 0 | bpf_error(cstate, "illegal port number %d < 0", port); |
6797 | 0 | if (port > 65535) |
6798 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port); |
6799 | | |
6800 | | // real_proto can be PROTO_UNDEF |
6801 | 0 | b = gen_port(cstate, (uint16_t)port, real_proto, dir); |
6802 | 0 | b6 = gen_port6(cstate, (uint16_t)port, real_proto, dir); |
6803 | 0 | return gen_or(b6, b); |
6804 | | |
6805 | 0 | case Q_PORTRANGE: |
6806 | 0 | (void)port_pq_to_ipproto(cstate, proto, "portrange"); // validate only |
6807 | 0 | stringtoportrange(cstate, name, &port1, &port2, &real_proto); |
6808 | 0 | if (proto == Q_UDP) { |
6809 | 0 | if (real_proto == IPPROTO_TCP) |
6810 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6811 | 0 | else if (real_proto == IPPROTO_SCTP) |
6812 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6813 | 0 | else |
6814 | | /* override PROTO_UNDEF */ |
6815 | 0 | real_proto = IPPROTO_UDP; |
6816 | 0 | } |
6817 | 0 | if (proto == Q_TCP) { |
6818 | 0 | if (real_proto == IPPROTO_UDP) |
6819 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6820 | 0 | else if (real_proto == IPPROTO_SCTP) |
6821 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6822 | 0 | else |
6823 | | /* override PROTO_UNDEF */ |
6824 | 0 | real_proto = IPPROTO_TCP; |
6825 | 0 | } |
6826 | 0 | if (proto == Q_SCTP) { |
6827 | 0 | if (real_proto == IPPROTO_UDP) |
6828 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6829 | 0 | else if (real_proto == IPPROTO_TCP) |
6830 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6831 | 0 | else |
6832 | | /* override PROTO_UNDEF */ |
6833 | 0 | real_proto = IPPROTO_SCTP; |
6834 | 0 | } |
6835 | | |
6836 | | /* |
6837 | | * When name is a string of the form "str1-str2", these two |
6838 | | * checks are redundant at this point: in both stringtoport() |
6839 | | * invocations stoulen() has rejected the argument and |
6840 | | * getaddrinfo() has successfully translated it to an uint16_t |
6841 | | * value. |
6842 | | * |
6843 | | * When name is a string of the form "num1-num2", "num-str" or |
6844 | | * "str-num", these two checks are necessary: in at least one |
6845 | | * stringtoport() invocation stoulen() can return any uint32_t |
6846 | | * value if it has accepted the argument. |
6847 | | */ |
6848 | 0 | if (port1 > 65535) |
6849 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port1); |
6850 | 0 | if (port2 > 65535) |
6851 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port2); |
6852 | | |
6853 | | // real_proto can be PROTO_UNDEF |
6854 | 0 | b = gen_portrange(cstate, (uint16_t)port1, (uint16_t)port2, |
6855 | 0 | real_proto, dir); |
6856 | 0 | b6 = gen_portrange6(cstate, (uint16_t)port1, (uint16_t)port2, |
6857 | 0 | real_proto, dir); |
6858 | 0 | return gen_or(b6, b); |
6859 | | |
6860 | 0 | case Q_GATEWAY: |
6861 | 0 | return gen_gateway(cstate, name, q.proto); |
6862 | | |
6863 | 0 | case Q_PROTO: |
6864 | 0 | return gen_proto(cstate, lookup_proto(cstate, name, q), proto); |
6865 | | |
6866 | 0 | #if !defined(NO_PROTOCHAIN) |
6867 | 0 | case Q_PROTOCHAIN: |
6868 | 0 | return gen_protochain(cstate, lookup_proto(cstate, name, q), proto); |
6869 | 0 | #endif /* !defined(NO_PROTOCHAIN) */ |
6870 | | |
6871 | 0 | case Q_UNDEF: |
6872 | 0 | syntax(cstate); |
6873 | | /*NOTREACHED*/ |
6874 | 0 | } |
6875 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "q.addr", q.addr); |
6876 | | /*NOTREACHED*/ |
6877 | 0 | } |
6878 | | |
6879 | | struct block * |
6880 | | gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2, |
6881 | | bpf_u_int32 masklen, struct qual q) |
6882 | 0 | { |
6883 | 0 | int nlen, mlen; |
6884 | 0 | bpf_u_int32 n, m; |
6885 | 0 | uint64_t m64; |
6886 | | |
6887 | | /* |
6888 | | * Catch errors reported by us and routines below us, and return NULL |
6889 | | * on an error. |
6890 | | */ |
6891 | 0 | if (setjmp(cstate->top_ctx)) |
6892 | 0 | return (NULL); |
6893 | | |
6894 | 0 | if (q.proto == Q_DECNET) { |
6895 | | /* |
6896 | | * libpcap has never defined any primitives that have "decnet" |
6897 | | * as the protocol qualifier and an IPv4 network with a |
6898 | | * netmask as the ID. |
6899 | | */ |
6900 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, "decnet", |
6901 | 0 | tqkw(q.addr == Q_DEFAULT ? Q_HOST : q.addr)); |
6902 | 0 | } |
6903 | | |
6904 | 0 | nlen = pcapint_atoin(s1, &n); |
6905 | 0 | if (nlen < 0) |
6906 | 0 | bpf_error(cstate, ERRSTR_INVALID_IPV4_ADDR, s1); |
6907 | | /* Promote short ipaddr */ |
6908 | 0 | n <<= 32 - nlen; |
6909 | |
|
6910 | 0 | if (s2 != NULL) { |
6911 | 0 | mlen = pcapint_atoin(s2, &m); |
6912 | 0 | if (mlen < 0) |
6913 | 0 | bpf_error(cstate, ERRSTR_INVALID_IPV4_ADDR, s2); |
6914 | | /* Promote short ipaddr */ |
6915 | 0 | m <<= 32 - mlen; |
6916 | 0 | if ((n & ~m) != 0) |
6917 | 0 | bpf_error(cstate, "non-network bits set in \"%s mask %s\"", |
6918 | 0 | s1, s2); |
6919 | 0 | } else { |
6920 | | /* Convert mask len to mask */ |
6921 | 0 | if (masklen > 32) |
6922 | 0 | bpf_error(cstate, "mask length must be <= 32"); |
6923 | 0 | m64 = UINT64_C(0xffffffff) << (32 - masklen); |
6924 | 0 | m = (bpf_u_int32)m64; |
6925 | 0 | if ((n & ~m) != 0) |
6926 | 0 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", |
6927 | 0 | s1, masklen); |
6928 | 0 | } |
6929 | | |
6930 | 0 | switch (q.addr) { |
6931 | | |
6932 | 0 | case Q_NET: |
6933 | 0 | return gen_host(cstate, n, m, q.proto, q.dir, q.addr); |
6934 | | |
6935 | 0 | default: |
6936 | | // Q_HOST and Q_GATEWAY only (see the grammar) |
6937 | 0 | bpf_error(cstate, "Mask syntax for networks only"); |
6938 | | /*NOTREACHED*/ |
6939 | 0 | } |
6940 | | /*NOTREACHED*/ |
6941 | 0 | } |
6942 | | |
6943 | | struct block * |
6944 | | gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q) |
6945 | 0 | { |
6946 | 0 | bpf_u_int32 mask; |
6947 | 0 | int proto; |
6948 | 0 | int dir; |
6949 | 0 | int vlen; |
6950 | | |
6951 | | /* |
6952 | | * Catch errors reported by us and routines below us, and return NULL |
6953 | | * on an error. |
6954 | | */ |
6955 | 0 | if (setjmp(cstate->top_ctx)) |
6956 | 0 | return (NULL); |
6957 | | |
6958 | 0 | if (q.proto == Q_DECNET) { |
6959 | | /* |
6960 | | * libpcap defines exactly one primitive that has "decnet" as |
6961 | | * the protocol qualifier: "decnet host AREANUMBER.NODENUMBER". |
6962 | | */ |
6963 | 0 | if (q.addr != Q_DEFAULT && q.addr != Q_HOST) |
6964 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, "decnet", |
6965 | 0 | tqkw(q.addr)); |
6966 | | |
6967 | 0 | if (s == NULL) { |
6968 | | /* |
6969 | | * v contains a 32-bit unsigned parsed from a string |
6970 | | * of the form {N}, which could be decimal, hexadecimal |
6971 | | * or octal. Although it would be possible to use the |
6972 | | * value as a raw 16-bit DECnet address when the value |
6973 | | * fits into 16 bits, this would be a questionable |
6974 | | * feature: DECnet address wire encoding is |
6975 | | * little-endian, so this would not work as intuitively |
6976 | | * as the same works for [big-endian] IPv4 addresses |
6977 | | * (0x01020304 means 1.2.3.4). |
6978 | | */ |
6979 | 0 | bpf_error(cstate, "invalid DECnet address '%u'", v); |
6980 | 0 | } |
6981 | | |
6982 | | /* |
6983 | | * s points to a string of the form {N}.{N}, {N}.{N}.{N} or |
6984 | | * {N}.{N}.{N}.{N}, of which only the first potentially stands |
6985 | | * for a valid DECnet address. |
6986 | | */ |
6987 | 0 | vlen = pcapint_atodn(s, &v); |
6988 | 0 | if (vlen == 0) |
6989 | 0 | bpf_error(cstate, "invalid DECnet address '%s'", s); |
6990 | | |
6991 | 0 | return gen_host(cstate, v, 0, q.proto, q.dir, q.addr); |
6992 | 0 | } |
6993 | | |
6994 | 0 | proto = q.proto; |
6995 | 0 | dir = q.dir; |
6996 | 0 | if (s == NULL) { |
6997 | | /* |
6998 | | * v contains a 32-bit unsigned parsed from a string of the |
6999 | | * form {N}, which could be decimal, hexadecimal or octal. |
7000 | | * This is a valid IPv4 address, in the sense of inet_aton(3). |
7001 | | */ |
7002 | 0 | vlen = 32; |
7003 | 0 | } else { |
7004 | | /* |
7005 | | * s points to a string of the form {N}.{N}, {N}.{N}.{N} or |
7006 | | * {N}.{N}.{N}.{N}, all of which potentially stand for a valid |
7007 | | * IPv4 address, in the sense of inet_aton(3). |
7008 | | */ |
7009 | 0 | vlen = pcapint_atoin(s, &v); |
7010 | 0 | if (vlen < 0) |
7011 | 0 | bpf_error(cstate, ERRSTR_INVALID_IPV4_ADDR, s); |
7012 | 0 | } |
7013 | | |
7014 | 0 | struct block *b, *b6; |
7015 | 0 | switch (q.addr) { |
7016 | | |
7017 | 0 | case Q_DEFAULT: |
7018 | 0 | case Q_HOST: |
7019 | 0 | case Q_NET: |
7020 | 0 | if (proto == Q_LINK) { |
7021 | 0 | if (s) |
7022 | | // "link (host|net) IPV4ADDR" and variations thereof |
7023 | 0 | bpf_error(cstate, "illegal link-layer address '%s'", s); |
7024 | 0 | else |
7025 | | // link host NUMBER |
7026 | 0 | bpf_error(cstate, "illegal link-layer address '%u'", v); |
7027 | 0 | } else { |
7028 | 0 | mask = 0xffffffff; |
7029 | 0 | if (s == NULL && q.addr == Q_NET) { |
7030 | | /* Promote short net number */ |
7031 | 0 | while (v && (v & 0xff000000) == 0) { |
7032 | 0 | v <<= 8; |
7033 | 0 | mask <<= 8; |
7034 | 0 | } |
7035 | 0 | } else { |
7036 | | /* Promote short ipaddr */ |
7037 | 0 | v <<= 32 - vlen; |
7038 | 0 | mask <<= 32 - vlen ; |
7039 | 0 | } |
7040 | 0 | return gen_host(cstate, v, mask, proto, dir, q.addr); |
7041 | 0 | } |
7042 | | |
7043 | 0 | case Q_PORT: |
7044 | 0 | proto = port_pq_to_ipproto(cstate, proto, "port"); |
7045 | | |
7046 | | // This check is necessary: v can hold any uint32_t value. |
7047 | 0 | if (v > 65535) |
7048 | 0 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7049 | | |
7050 | | // proto can be PROTO_UNDEF |
7051 | 0 | b = gen_port(cstate, (uint16_t)v, proto, dir); |
7052 | 0 | b6 = gen_port6(cstate, (uint16_t)v, proto, dir); |
7053 | 0 | return gen_or(b6, b); |
7054 | | |
7055 | 0 | case Q_PORTRANGE: |
7056 | 0 | proto = port_pq_to_ipproto(cstate, proto, "portrange"); |
7057 | | |
7058 | | // This check is necessary: v can hold any uint32_t value. |
7059 | 0 | if (v > 65535) |
7060 | 0 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7061 | | |
7062 | | // proto can be PROTO_UNDEF |
7063 | 0 | b = gen_portrange(cstate, (uint16_t)v, (uint16_t)v, |
7064 | 0 | proto, dir); |
7065 | 0 | b6 = gen_portrange6(cstate, (uint16_t)v, (uint16_t)v, |
7066 | 0 | proto, dir); |
7067 | 0 | return gen_or(b6, b); |
7068 | | |
7069 | 0 | case Q_GATEWAY: |
7070 | 0 | bpf_error(cstate, "'gateway' requires a name"); |
7071 | | /*NOTREACHED*/ |
7072 | | |
7073 | 0 | case Q_PROTO: |
7074 | 0 | return gen_proto(cstate, v, proto); |
7075 | | |
7076 | 0 | #if !defined(NO_PROTOCHAIN) |
7077 | 0 | case Q_PROTOCHAIN: |
7078 | 0 | return gen_protochain(cstate, v, proto); |
7079 | 0 | #endif |
7080 | | |
7081 | 0 | case Q_UNDEF: |
7082 | 0 | syntax(cstate); |
7083 | | /*NOTREACHED*/ |
7084 | | |
7085 | 0 | default: |
7086 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "q.addr", q.addr); |
7087 | | /*NOTREACHED*/ |
7088 | 0 | } |
7089 | | /*NOTREACHED*/ |
7090 | 0 | } |
7091 | | |
7092 | | struct block * |
7093 | | gen_mcode6(compiler_state_t *cstate, const char *s, bpf_u_int32 masklen, |
7094 | | struct qual q) |
7095 | 0 | { |
7096 | 0 | struct in6_addr addr; |
7097 | 0 | struct in6_addr mask; |
7098 | 0 | bpf_u_int32 a[4], m[4]; /* Same as in gen_hostop6(). */ |
7099 | | |
7100 | | /* |
7101 | | * Catch errors reported by us and routines below us, and return NULL |
7102 | | * on an error. |
7103 | | */ |
7104 | 0 | if (setjmp(cstate->top_ctx)) |
7105 | 0 | return (NULL); |
7106 | | |
7107 | | /* |
7108 | | * If everything works correctly, this call never fails: a string that |
7109 | | * is valid for HID6 and the associated validating inet_pton() in the |
7110 | | * lexer is valid for inet_pton() here. |
7111 | | */ |
7112 | 0 | if (1 != inet_pton(AF_INET6, s, &addr)) |
7113 | 0 | bpf_error(cstate, "'%s' is not a valid IPv6 address", s); |
7114 | | |
7115 | 0 | if (masklen > sizeof(mask.s6_addr) * 8) |
7116 | 0 | bpf_error(cstate, "mask length must be <= %zu", sizeof(mask.s6_addr) * 8); |
7117 | 0 | memset(&mask, 0, sizeof(mask)); |
7118 | 0 | memset(&mask.s6_addr, 0xff, masklen / 8); |
7119 | 0 | if (masklen % 8) { |
7120 | 0 | mask.s6_addr[masklen / 8] = |
7121 | 0 | (0xff << (8 - masklen % 8)) & 0xff; |
7122 | 0 | } |
7123 | |
|
7124 | 0 | memcpy(a, &addr, sizeof(a)); |
7125 | 0 | memcpy(m, &mask, sizeof(m)); |
7126 | 0 | if ((a[0] & ~m[0]) || (a[1] & ~m[1]) |
7127 | 0 | || (a[2] & ~m[2]) || (a[3] & ~m[3])) { |
7128 | 0 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", s, masklen); |
7129 | 0 | } |
7130 | | |
7131 | 0 | char buf[INET6_ADDRSTRLEN + sizeof("/128")]; |
7132 | 0 | switch (q.addr) { |
7133 | | |
7134 | 0 | case Q_DEFAULT: |
7135 | 0 | case Q_HOST: |
7136 | 0 | if (masklen != 128) { |
7137 | 0 | snprintf(buf, sizeof(buf), "%s/%u", s, masklen); |
7138 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, "host", buf); |
7139 | 0 | } |
7140 | | /* FALLTHROUGH */ |
7141 | | |
7142 | 0 | case Q_NET: |
7143 | 0 | return gen_host6(cstate, &addr, &mask, q.proto, q.dir, q.addr); |
7144 | | |
7145 | 0 | default: |
7146 | | // Q_GATEWAY only (see the grammar) |
7147 | 0 | if (masklen == 128) |
7148 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, tqkw(q.addr), s); |
7149 | 0 | else { |
7150 | 0 | snprintf(buf, sizeof(buf), "%s/%u", s, masklen); |
7151 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, tqkw(q.addr), buf); |
7152 | 0 | } |
7153 | | /*NOTREACHED*/ |
7154 | 0 | } |
7155 | 0 | } |
7156 | | |
7157 | | struct block * |
7158 | | gen_ecode(compiler_state_t *cstate, const char *s, struct qual q) |
7159 | 0 | { |
7160 | | /* |
7161 | | * Catch errors reported by us and routines below us, and return NULL |
7162 | | * on an error. |
7163 | | */ |
7164 | 0 | if (setjmp(cstate->top_ctx)) |
7165 | 0 | return (NULL); |
7166 | | |
7167 | 0 | const char *context = "link host XX:XX:XX:XX:XX:XX"; |
7168 | |
|
7169 | 0 | if (! ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK)) |
7170 | 0 | bpf_error(cstate, "Ethernet address used in non-ether expression"); |
7171 | 0 | if (! is_mac48_linktype(cstate->linktype)) |
7172 | 0 | fail_kw_on_dlt(cstate, context); |
7173 | | |
7174 | 0 | u_char eaddr[6]; |
7175 | | /* |
7176 | | * Belt and braces: so long as the lexer regexp guards MAC-48 syntax, |
7177 | | * here the attempt to parse it will always succeed. |
7178 | | */ |
7179 | 0 | if (! pcapint_atomac48(s, eaddr)) |
7180 | 0 | bpf_error(cstate, "invalid MAC-48 address '%s'", s); |
7181 | | |
7182 | 0 | return gen_mac48host(cstate, eaddr, q.dir, context); |
7183 | 0 | } |
7184 | | |
7185 | | // Process a regular primitive, the ID is a MAC-8 address string. |
7186 | | struct block * |
7187 | | gen_acode(compiler_state_t *cstate, const char *s, struct qual q) |
7188 | 0 | { |
7189 | | /* |
7190 | | * Catch errors reported by us and routines below us, and return NULL |
7191 | | * on an error. |
7192 | | */ |
7193 | 0 | if (setjmp(cstate->top_ctx)) |
7194 | 0 | return (NULL); |
7195 | | |
7196 | 0 | if (q.addr != Q_HOST && q.addr != Q_DEFAULT) |
7197 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, tqkw(q.addr), "$XX"); |
7198 | 0 | if (q.proto != Q_LINK) |
7199 | 0 | bpf_error(cstate, "'link' is the only valid proto qualifier for 'host $XX'"); |
7200 | | |
7201 | 0 | uint8_t addr; |
7202 | | /* |
7203 | | * The lexer currently defines the address format in a way that makes |
7204 | | * this error condition never true. Let's check it anyway in case this |
7205 | | * part of the lexer changes in future. |
7206 | | */ |
7207 | 0 | if (! pcapint_atoan(s, &addr)) |
7208 | 0 | bpf_error(cstate, "invalid MAC-8 address '%s'", s); |
7209 | | |
7210 | 0 | return gen_mac8host(cstate, addr, q.dir, "link host $XX"); |
7211 | 0 | } |
7212 | | |
7213 | | void |
7214 | | sappend(struct slist *s0, struct slist *s1) |
7215 | 0 | { |
7216 | | /* |
7217 | | * This is definitely not the best way to do this, but the |
7218 | | * lists will rarely get long. |
7219 | | */ |
7220 | 0 | while (s0->next) |
7221 | 0 | s0 = s0->next; |
7222 | 0 | s0->next = s1; |
7223 | 0 | } |
7224 | | |
7225 | | /* |
7226 | | * Prepend the given list of statements to the list of side effect statements |
7227 | | * of the block. Either of the lists may be NULL to mean the valid edge case |
7228 | | * of an empty list. |
7229 | | */ |
7230 | | static struct block * |
7231 | | sprepend_to_block(struct slist *s, struct block *b) |
7232 | 0 | { |
7233 | 0 | if (s) { |
7234 | 0 | if (b->stmts) |
7235 | 0 | sappend(s, b->stmts); |
7236 | 0 | b->stmts = s; |
7237 | | /* |
7238 | | * The block has changed. It could have been a Boolean |
7239 | | * constant before. |
7240 | | */ |
7241 | 0 | b->meaning = IS_UNCERTAIN; |
7242 | 0 | } |
7243 | 0 | return b; |
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, |
7275 | | bpf_u_int32 size) |
7276 | 0 | { |
7277 | 0 | int size_code; |
7278 | 0 | struct slist *s, *tmp; |
7279 | 0 | struct block *b; |
7280 | 0 | int regno = alloc_reg(cstate); |
7281 | |
|
7282 | 0 | free_reg(cstate, inst->regno); |
7283 | 0 | switch (size) { |
7284 | | |
7285 | 0 | default: |
7286 | 0 | bpf_error(cstate, "data size must be 1, 2, or 4"); |
7287 | | /*NOTREACHED*/ |
7288 | | |
7289 | 0 | case 1: |
7290 | 0 | size_code = BPF_B; |
7291 | 0 | break; |
7292 | | |
7293 | 0 | case 2: |
7294 | 0 | size_code = BPF_H; |
7295 | 0 | break; |
7296 | | |
7297 | 0 | case 4: |
7298 | 0 | size_code = BPF_W; |
7299 | 0 | break; |
7300 | 0 | } |
7301 | 0 | switch (proto) { |
7302 | 0 | default: |
7303 | 0 | bpf_error(cstate, "'%s' does not support the index operation", pqkw(proto)); |
7304 | | |
7305 | 0 | case Q_RADIO: |
7306 | | /* |
7307 | | * The offset is relative to the beginning of the packet |
7308 | | * data, if we have a radio header. (If we don't, this |
7309 | | * is an error.) |
7310 | | */ |
7311 | 0 | if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS && |
7312 | 0 | cstate->linktype != DLT_IEEE802_11_RADIO && |
7313 | 0 | cstate->linktype != DLT_PRISM_HEADER) |
7314 | 0 | bpf_error(cstate, "radio information not present in capture"); |
7315 | | |
7316 | | /* |
7317 | | * Load into the X register the offset computed into the |
7318 | | * register specified by "index". |
7319 | | */ |
7320 | 0 | s = xfer_to_x(cstate, inst); |
7321 | | |
7322 | | /* |
7323 | | * Load the item at that offset. |
7324 | | */ |
7325 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7326 | 0 | sappend(s, tmp); |
7327 | 0 | sappend(inst->s, s); |
7328 | 0 | break; |
7329 | | |
7330 | 0 | case Q_LINK: |
7331 | | /* |
7332 | | * The offset is relative to the beginning of |
7333 | | * the link-layer header. |
7334 | | * |
7335 | | * XXX - what about ATM LANE? Should the index be |
7336 | | * relative to the beginning of the AAL5 frame, so |
7337 | | * that 0 refers to the beginning of the LE Control |
7338 | | * field, or relative to the beginning of the LAN |
7339 | | * frame, so that 0 refers, for Ethernet LANE, to |
7340 | | * the beginning of the destination address? |
7341 | | */ |
7342 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr); |
7343 | | |
7344 | | /* |
7345 | | * If "s" is non-null, it has code to arrange that the |
7346 | | * X register contains the length of the prefix preceding |
7347 | | * the link-layer header. Add to it the offset computed |
7348 | | * into the register specified by "index", and move that |
7349 | | * into the X register. Otherwise, just load into the X |
7350 | | * register the offset computed into the register specified |
7351 | | * by "index". |
7352 | | */ |
7353 | 0 | if (s != NULL) { |
7354 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7355 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7356 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7357 | 0 | } else |
7358 | 0 | s = xfer_to_x(cstate, inst); |
7359 | | |
7360 | | /* |
7361 | | * Load the item at the sum of the offset we've put in the |
7362 | | * X register and the offset of the start of the link |
7363 | | * layer header (which is 0 if the radio header is |
7364 | | * variable-length; that header length is what we put |
7365 | | * into the X register and then added to the index). |
7366 | | */ |
7367 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7368 | 0 | tmp->s.k = cstate->off_linkhdr.constant_part; |
7369 | 0 | sappend(s, tmp); |
7370 | 0 | sappend(inst->s, s); |
7371 | 0 | break; |
7372 | | |
7373 | 0 | case Q_IP: |
7374 | 0 | case Q_ARP: |
7375 | 0 | case Q_RARP: |
7376 | 0 | case Q_ATALK: |
7377 | 0 | case Q_DECNET: |
7378 | 0 | case Q_SCA: |
7379 | 0 | case Q_LAT: |
7380 | 0 | case Q_MOPRC: |
7381 | 0 | case Q_MOPDL: |
7382 | 0 | case Q_IPV6: |
7383 | | /* |
7384 | | * The offset is relative to the beginning of |
7385 | | * the network-layer header. |
7386 | | * XXX - are there any cases where we want |
7387 | | * cstate->off_nl_nosnap? |
7388 | | */ |
7389 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7390 | | |
7391 | | /* |
7392 | | * If "s" is non-null, it has code to arrange that the |
7393 | | * X register contains the variable part of the offset |
7394 | | * of the link-layer payload. Add to it the offset |
7395 | | * computed into the register specified by "index", |
7396 | | * and move that into the X register. Otherwise, just |
7397 | | * load into the X register the offset computed into |
7398 | | * the register specified by "index". |
7399 | | */ |
7400 | 0 | if (s != NULL) { |
7401 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7402 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7403 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7404 | 0 | } else |
7405 | 0 | s = xfer_to_x(cstate, inst); |
7406 | | |
7407 | | /* |
7408 | | * Load the item at the sum of the offset we've put in the |
7409 | | * X register, the offset of the start of the network |
7410 | | * layer header from the beginning of the link-layer |
7411 | | * payload, and the constant part of the offset of the |
7412 | | * start of the link-layer payload. |
7413 | | */ |
7414 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7415 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7416 | 0 | sappend(s, tmp); |
7417 | 0 | sappend(inst->s, s); |
7418 | | |
7419 | | /* |
7420 | | * Do the computation only if the packet contains |
7421 | | * the protocol in question. |
7422 | | */ |
7423 | 0 | b = gen_proto_abbrev_internal(cstate, proto); |
7424 | 0 | if (inst->b) |
7425 | 0 | b = gen_and(inst->b, b); |
7426 | 0 | inst->b = b; |
7427 | 0 | break; |
7428 | | |
7429 | 0 | case Q_SCTP: |
7430 | 0 | case Q_TCP: |
7431 | 0 | case Q_UDP: |
7432 | 0 | case Q_ICMP: |
7433 | 0 | case Q_IGMP: |
7434 | 0 | case Q_IGRP: |
7435 | 0 | case Q_PIM: |
7436 | 0 | case Q_VRRP: |
7437 | 0 | case Q_CARP: |
7438 | | /* |
7439 | | * The offset is relative to the beginning of |
7440 | | * the transport-layer header. |
7441 | | * |
7442 | | * Load the X register with the length of the IPv4 header |
7443 | | * (plus the offset of the link-layer header, if it's |
7444 | | * a variable-length header), in bytes. |
7445 | | * |
7446 | | * XXX - are there any cases where we want |
7447 | | * cstate->off_nl_nosnap? |
7448 | | * XXX - we should, if we're built with |
7449 | | * IPv6 support, generate code to load either |
7450 | | * IPv4, IPv6, or both, as appropriate. |
7451 | | */ |
7452 | 0 | s = gen_loadx_iphdrlen(cstate); |
7453 | | |
7454 | | /* |
7455 | | * The X register now contains the sum of the variable |
7456 | | * part of the offset of the link-layer payload and the |
7457 | | * length of the network-layer header. |
7458 | | * |
7459 | | * Load into the A register the offset relative to |
7460 | | * the beginning of the transport layer header, |
7461 | | * add the X register to that, move that to the |
7462 | | * X register, and load with an offset from the |
7463 | | * X register equal to the sum of the constant part of |
7464 | | * the offset of the link-layer payload and the offset, |
7465 | | * relative to the beginning of the link-layer payload, |
7466 | | * of the network-layer header. |
7467 | | */ |
7468 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7469 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7470 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7471 | 0 | sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code)); |
7472 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7473 | 0 | sappend(inst->s, s); |
7474 | | |
7475 | | /* |
7476 | | * Do the computation only if the packet contains |
7477 | | * the protocol in question - which is true only |
7478 | | * if this is an IP datagram and is the first or |
7479 | | * only fragment of that datagram. |
7480 | | */ |
7481 | 0 | b = gen_and(gen_proto_abbrev_internal(cstate, proto), gen_ipfrag(cstate)); |
7482 | 0 | if (inst->b) |
7483 | 0 | b = gen_and(inst->b, b); |
7484 | 0 | b = gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b); |
7485 | 0 | inst->b = b; |
7486 | 0 | break; |
7487 | 0 | case Q_ICMPV6: |
7488 | | /* |
7489 | | * Do the computation only if the packet contains |
7490 | | * the protocol in question. |
7491 | | */ |
7492 | 0 | b = gen_proto_abbrev_internal(cstate, Q_IPV6); |
7493 | 0 | inst->b = inst->b ? gen_and(inst->b, b) : b; |
7494 | | |
7495 | | /* |
7496 | | * Check if we have an icmp6 next header |
7497 | | */ |
7498 | 0 | b = gen_ip6_proto(cstate, 58); |
7499 | 0 | inst->b = inst->b ? gen_and(inst->b, b) : b; |
7500 | |
|
7501 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7502 | | /* |
7503 | | * If "s" is non-null, it has code to arrange that the |
7504 | | * X register contains the variable part of the offset |
7505 | | * of the link-layer payload. Add to it the offset |
7506 | | * computed into the register specified by "index", |
7507 | | * and move that into the X register. Otherwise, just |
7508 | | * load into the X register the offset computed into |
7509 | | * the register specified by "index". |
7510 | | */ |
7511 | 0 | if (s != NULL) { |
7512 | 0 | sappend(s, xfer_to_a(cstate, inst)); |
7513 | 0 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7514 | 0 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7515 | 0 | } else |
7516 | 0 | s = xfer_to_x(cstate, inst); |
7517 | | |
7518 | | /* |
7519 | | * Load the item at the sum of the offset we've put in the |
7520 | | * X register, the offset of the start of the network |
7521 | | * layer header from the beginning of the link-layer |
7522 | | * payload, and the constant part of the offset of the |
7523 | | * start of the link-layer payload. |
7524 | | */ |
7525 | 0 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7526 | 0 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40; |
7527 | |
|
7528 | 0 | sappend(s, tmp); |
7529 | 0 | sappend(inst->s, s); |
7530 | |
|
7531 | 0 | break; |
7532 | 0 | } |
7533 | 0 | inst->regno = regno; |
7534 | 0 | s = new_stmt(cstate, BPF_ST); |
7535 | 0 | s->s.k = regno; |
7536 | 0 | sappend(inst->s, s); |
7537 | |
|
7538 | 0 | return inst; |
7539 | 0 | } |
7540 | | |
7541 | | struct arth * |
7542 | | gen_load(compiler_state_t *cstate, int proto, struct arth *inst, |
7543 | | bpf_u_int32 size) |
7544 | 0 | { |
7545 | | /* |
7546 | | * Catch errors reported by us and routines below us, and return NULL |
7547 | | * on an error. |
7548 | | */ |
7549 | 0 | if (setjmp(cstate->top_ctx)) |
7550 | 0 | return (NULL); |
7551 | | |
7552 | 0 | return gen_load_internal(cstate, proto, inst, size); |
7553 | 0 | } |
7554 | | |
7555 | | static struct block * |
7556 | | gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0, |
7557 | | struct arth *a1, int reversed) |
7558 | 0 | { |
7559 | 0 | struct slist *s0, *s1; |
7560 | 0 | struct block *b, *tmp; |
7561 | |
|
7562 | 0 | s0 = xfer_to_x(cstate, a1); |
7563 | 0 | s1 = xfer_to_a(cstate, a0); |
7564 | 0 | sappend(s0, s1); |
7565 | 0 | sappend(a1->s, s0); |
7566 | 0 | sappend(a0->s, a1->s); |
7567 | |
|
7568 | 0 | b = gen_jmp_x(cstate, code, a0->s); |
7569 | 0 | if (reversed) |
7570 | 0 | gen_not(b); |
7571 | |
|
7572 | 0 | free_reg(cstate, a0->regno); |
7573 | 0 | free_reg(cstate, a1->regno); |
7574 | | |
7575 | | /* 'and' together protocol checks */ |
7576 | 0 | if (a0->b) { |
7577 | 0 | tmp = a1->b ? gen_and(a0->b, a1->b) : a0->b; |
7578 | 0 | } else |
7579 | 0 | tmp = a1->b; |
7580 | |
|
7581 | 0 | return tmp ? gen_and(tmp, b) : b; |
7582 | 0 | } |
7583 | | |
7584 | | struct block * |
7585 | | gen_relation(compiler_state_t *cstate, int code, struct arth *a0, |
7586 | | struct arth *a1, int reversed) |
7587 | 0 | { |
7588 | | /* |
7589 | | * Catch errors reported by us and routines below us, and return NULL |
7590 | | * on an error. |
7591 | | */ |
7592 | 0 | if (setjmp(cstate->top_ctx)) |
7593 | 0 | return (NULL); |
7594 | | |
7595 | 0 | return gen_relation_internal(cstate, code, a0, a1, reversed); |
7596 | 0 | } |
7597 | | |
7598 | | struct arth * |
7599 | | gen_loadlen(compiler_state_t *cstate) |
7600 | 0 | { |
7601 | 0 | int regno; |
7602 | 0 | struct arth *a; |
7603 | 0 | struct slist *s; |
7604 | | |
7605 | | /* |
7606 | | * Catch errors reported by us and routines below us, and return NULL |
7607 | | * on an error. |
7608 | | */ |
7609 | 0 | if (setjmp(cstate->top_ctx)) |
7610 | 0 | return (NULL); |
7611 | | |
7612 | 0 | regno = alloc_reg(cstate); |
7613 | 0 | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7614 | 0 | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7615 | 0 | s->next = new_stmt(cstate, BPF_ST); |
7616 | 0 | s->next->s.k = regno; |
7617 | 0 | a->s = s; |
7618 | 0 | a->regno = regno; |
7619 | |
|
7620 | 0 | return a; |
7621 | 0 | } |
7622 | | |
7623 | | static struct arth * |
7624 | | gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val) |
7625 | 0 | { |
7626 | 0 | struct arth *a; |
7627 | 0 | struct slist *s; |
7628 | 0 | int reg; |
7629 | |
|
7630 | 0 | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7631 | |
|
7632 | 0 | reg = alloc_reg(cstate); |
7633 | |
|
7634 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
7635 | 0 | s->s.k = val; |
7636 | 0 | s->next = new_stmt(cstate, BPF_ST); |
7637 | 0 | s->next->s.k = reg; |
7638 | 0 | a->s = s; |
7639 | 0 | a->regno = reg; |
7640 | |
|
7641 | 0 | return a; |
7642 | 0 | } |
7643 | | |
7644 | | struct arth * |
7645 | | gen_loadi(compiler_state_t *cstate, bpf_u_int32 val) |
7646 | 0 | { |
7647 | | /* |
7648 | | * Catch errors reported by us and routines below us, and return NULL |
7649 | | * on an error. |
7650 | | */ |
7651 | 0 | if (setjmp(cstate->top_ctx)) |
7652 | 0 | return (NULL); |
7653 | | |
7654 | 0 | return gen_loadi_internal(cstate, val); |
7655 | 0 | } |
7656 | | |
7657 | | /* |
7658 | | * The a_arg dance is to avoid annoying whining by compilers that |
7659 | | * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7660 | | * It's not *used* after setjmp returns. |
7661 | | */ |
7662 | | struct arth * |
7663 | | gen_neg(compiler_state_t *cstate, struct arth *a_arg) |
7664 | 0 | { |
7665 | 0 | struct arth *a = a_arg; |
7666 | 0 | struct slist *s; |
7667 | | |
7668 | | /* |
7669 | | * Catch errors reported by us and routines below us, and return NULL |
7670 | | * on an error. |
7671 | | */ |
7672 | 0 | if (setjmp(cstate->top_ctx)) |
7673 | 0 | return (NULL); |
7674 | | |
7675 | 0 | s = xfer_to_a(cstate, a); |
7676 | 0 | sappend(a->s, s); |
7677 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_NEG); |
7678 | 0 | s->s.k = 0; |
7679 | 0 | sappend(a->s, s); |
7680 | 0 | s = new_stmt(cstate, BPF_ST); |
7681 | 0 | s->s.k = a->regno; |
7682 | 0 | sappend(a->s, s); |
7683 | |
|
7684 | 0 | return a; |
7685 | 0 | } |
7686 | | |
7687 | | /* |
7688 | | * The a0_arg dance is to avoid annoying whining by compilers that |
7689 | | * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7690 | | * It's not *used* after setjmp returns. |
7691 | | */ |
7692 | | struct arth * |
7693 | | gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg, |
7694 | | struct arth *a1) |
7695 | 0 | { |
7696 | 0 | struct arth *a0 = a0_arg; |
7697 | 0 | struct slist *s0, *s1, *s2; |
7698 | | |
7699 | | /* |
7700 | | * Catch errors reported by us and routines below us, and return NULL |
7701 | | * on an error. |
7702 | | */ |
7703 | 0 | if (setjmp(cstate->top_ctx)) |
7704 | 0 | return (NULL); |
7705 | | |
7706 | | /* |
7707 | | * Disallow division by, or modulus by, zero; we do this here |
7708 | | * so that it gets done even if the optimizer is disabled. |
7709 | | * |
7710 | | * Also disallow shifts by a value greater than 31; we do this |
7711 | | * here, for the same reason. |
7712 | | */ |
7713 | 0 | if (code == BPF_DIV) { |
7714 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7715 | 0 | bpf_error(cstate, "division by zero"); |
7716 | 0 | } else if (code == BPF_MOD) { |
7717 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7718 | 0 | bpf_error(cstate, "modulus by zero"); |
7719 | 0 | } else if (code == BPF_LSH || code == BPF_RSH) { |
7720 | 0 | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31) |
7721 | 0 | bpf_error(cstate, "shift by more than 31 bits"); |
7722 | 0 | } |
7723 | 0 | s0 = xfer_to_x(cstate, a1); |
7724 | 0 | s1 = xfer_to_a(cstate, a0); |
7725 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_X|code); |
7726 | |
|
7727 | 0 | sappend(s1, s2); |
7728 | 0 | sappend(s0, s1); |
7729 | 0 | sappend(a1->s, s0); |
7730 | 0 | sappend(a0->s, a1->s); |
7731 | |
|
7732 | 0 | free_reg(cstate, a0->regno); |
7733 | 0 | free_reg(cstate, a1->regno); |
7734 | |
|
7735 | 0 | s0 = new_stmt(cstate, BPF_ST); |
7736 | 0 | a0->regno = s0->s.k = alloc_reg(cstate); |
7737 | 0 | sappend(a0->s, s0); |
7738 | |
|
7739 | 0 | return a0; |
7740 | 0 | } |
7741 | | |
7742 | | /* |
7743 | | * Initialize the table of used registers and the current register. |
7744 | | */ |
7745 | | static void |
7746 | | init_regs(compiler_state_t *cstate) |
7747 | 0 | { |
7748 | 0 | cstate->curreg = 0; |
7749 | 0 | memset(cstate->regused, 0, sizeof cstate->regused); |
7750 | 0 | } |
7751 | | |
7752 | | /* |
7753 | | * Return the next free register. |
7754 | | */ |
7755 | | static int |
7756 | | alloc_reg(compiler_state_t *cstate) |
7757 | 0 | { |
7758 | 0 | int n = BPF_MEMWORDS; |
7759 | |
|
7760 | 0 | while (--n >= 0) { |
7761 | 0 | if (cstate->regused[cstate->curreg]) |
7762 | 0 | cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS; |
7763 | 0 | else { |
7764 | 0 | cstate->regused[cstate->curreg] = 1; |
7765 | 0 | return cstate->curreg; |
7766 | 0 | } |
7767 | 0 | } |
7768 | 0 | bpf_error(cstate, "too many registers needed to evaluate expression"); |
7769 | | /*NOTREACHED*/ |
7770 | 0 | } |
7771 | | |
7772 | | /* |
7773 | | * Return a register to the table so it can |
7774 | | * be used later. |
7775 | | */ |
7776 | | static void |
7777 | | free_reg(compiler_state_t *cstate, int n) |
7778 | 0 | { |
7779 | 0 | cstate->regused[n] = 0; |
7780 | 0 | } |
7781 | | |
7782 | | static struct block * |
7783 | | gen_len(compiler_state_t *cstate, int jmp, int n) |
7784 | 0 | { |
7785 | 0 | struct slist *s; |
7786 | |
|
7787 | 0 | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7788 | 0 | return gen_jmp_k(cstate, jmp, n, s); |
7789 | 0 | } |
7790 | | |
7791 | | struct block * |
7792 | | gen_greater(compiler_state_t *cstate, int n) |
7793 | 0 | { |
7794 | | /* |
7795 | | * Catch errors reported by us and routines below us, and return NULL |
7796 | | * on an error. |
7797 | | */ |
7798 | 0 | if (setjmp(cstate->top_ctx)) |
7799 | 0 | return (NULL); |
7800 | | |
7801 | 0 | return gen_len(cstate, BPF_JGE, n); |
7802 | 0 | } |
7803 | | |
7804 | | /* |
7805 | | * Actually, this is less than or equal. |
7806 | | */ |
7807 | | struct block * |
7808 | | gen_less(compiler_state_t *cstate, int n) |
7809 | 0 | { |
7810 | | /* |
7811 | | * Catch errors reported by us and routines below us, and return NULL |
7812 | | * on an error. |
7813 | | */ |
7814 | 0 | if (setjmp(cstate->top_ctx)) |
7815 | 0 | return (NULL); |
7816 | | |
7817 | 0 | return gen_not(gen_len(cstate, BPF_JGT, n)); |
7818 | 0 | } |
7819 | | |
7820 | | /* |
7821 | | * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to |
7822 | | * the beginning of the link-layer header. |
7823 | | * XXX - that means you can't test values in the radiotap header, but |
7824 | | * as that header is difficult if not impossible to parse generally |
7825 | | * without a loop, that might not be a severe problem. A new keyword |
7826 | | * "radio" could be added for that, although what you'd really want |
7827 | | * would be a way of testing particular radio header values, which |
7828 | | * would generate code appropriate to the radio header in question. |
7829 | | */ |
7830 | | struct block * |
7831 | | gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val) |
7832 | 0 | { |
7833 | 0 | struct block *b; |
7834 | 0 | struct slist *s; |
7835 | | |
7836 | | /* |
7837 | | * Catch errors reported by us and routines below us, and return NULL |
7838 | | * on an error. |
7839 | | */ |
7840 | 0 | if (setjmp(cstate->top_ctx)) |
7841 | 0 | return (NULL); |
7842 | | |
7843 | 0 | assert_maxval(cstate, "byte argument", val, UINT8_MAX); |
7844 | |
|
7845 | 0 | switch (op) { |
7846 | 0 | default: |
7847 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "op", op); |
7848 | | |
7849 | 0 | case '=': |
7850 | 0 | return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7851 | | |
7852 | 0 | case '<': |
7853 | 0 | return gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7854 | | |
7855 | 0 | case '>': |
7856 | 0 | return gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7857 | | |
7858 | 0 | case '|': |
7859 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K); |
7860 | 0 | break; |
7861 | | |
7862 | 0 | case '&': |
7863 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
7864 | 0 | break; |
7865 | 0 | } |
7866 | 0 | s->s.k = val; |
7867 | | // Load the required byte first. |
7868 | 0 | struct slist *s0 = gen_load_a(cstate, OR_LINKHDR, idx, BPF_B); |
7869 | 0 | sappend(s0, s); |
7870 | 0 | b = gen_jmp_k(cstate, BPF_JEQ, 0, s0); |
7871 | |
|
7872 | 0 | return gen_not(b); |
7873 | 0 | } |
7874 | | |
7875 | | struct block * |
7876 | | gen_broadcast(compiler_state_t *cstate, int proto) |
7877 | 0 | { |
7878 | 0 | bpf_u_int32 hostmask; |
7879 | 0 | struct block *b0, *b1, *b2; |
7880 | 0 | static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
7881 | | |
7882 | | /* |
7883 | | * Catch errors reported by us and routines below us, and return NULL |
7884 | | * on an error. |
7885 | | */ |
7886 | 0 | if (setjmp(cstate->top_ctx)) |
7887 | 0 | return (NULL); |
7888 | | |
7889 | 0 | switch (proto) { |
7890 | | |
7891 | 0 | case Q_DEFAULT: |
7892 | 0 | case Q_LINK: |
7893 | 0 | switch (cstate->linktype) { |
7894 | 0 | case DLT_ARCNET: |
7895 | 0 | case DLT_ARCNET_LINUX: |
7896 | | // ARCnet broadcast is [8-bit] destination address 0. |
7897 | 0 | return gen_mac8host(cstate, 0, Q_DST, "broadcast"); |
7898 | 0 | case DLT_BACNET_MS_TP: |
7899 | | // MS/TP broadcast is [8-bit] destination address 0xFF. |
7900 | 0 | return gen_mac8host(cstate, 0xFF, Q_DST, "broadcast"); |
7901 | 0 | } |
7902 | 0 | return gen_mac48host(cstate, ebroadcast, Q_DST, "broadcast"); |
7903 | | /*NOTREACHED*/ |
7904 | | |
7905 | 0 | case Q_IP: |
7906 | | /* |
7907 | | * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff) |
7908 | | * as an indication that we don't know the netmask, and fail |
7909 | | * in that case. |
7910 | | */ |
7911 | 0 | if (cstate->netmask == PCAP_NETMASK_UNKNOWN) |
7912 | 0 | bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported"); |
7913 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
7914 | 0 | hostmask = ~cstate->netmask; |
7915 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask); |
7916 | 0 | b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, hostmask, hostmask); |
7917 | 0 | return gen_and(b0, gen_or(b1, b2)); |
7918 | 0 | } |
7919 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "broadcast"); |
7920 | | /*NOTREACHED*/ |
7921 | 0 | } |
7922 | | |
7923 | | /* |
7924 | | * Generate code to test the low-order bit of a MAC address (that's |
7925 | | * the bottom bit of the *first* byte). |
7926 | | */ |
7927 | | static struct block * |
7928 | | gen_mac_multicast(compiler_state_t *cstate, int offset) |
7929 | 0 | { |
7930 | 0 | struct slist *s; |
7931 | | |
7932 | | /* link[offset] & 1 != 0 */ |
7933 | 0 | s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B); |
7934 | 0 | return gen_set(cstate, 1, s); |
7935 | 0 | } |
7936 | | |
7937 | | struct block * |
7938 | | gen_multicast(compiler_state_t *cstate, int proto) |
7939 | 0 | { |
7940 | 0 | struct block *b0, *b1, *b2; |
7941 | 0 | struct slist *s; |
7942 | | |
7943 | | /* |
7944 | | * Catch errors reported by us and routines below us, and return NULL |
7945 | | * on an error. |
7946 | | */ |
7947 | 0 | if (setjmp(cstate->top_ctx)) |
7948 | 0 | return (NULL); |
7949 | | |
7950 | 0 | switch (proto) { |
7951 | | |
7952 | 0 | case Q_DEFAULT: |
7953 | 0 | case Q_LINK: |
7954 | 0 | switch (cstate->linktype) { |
7955 | 0 | case DLT_ARCNET: |
7956 | 0 | case DLT_ARCNET_LINUX: |
7957 | | // ARCnet multicast is the same as broadcast. |
7958 | 0 | return gen_mac8host(cstate, 0, Q_DST, "multicast"); |
7959 | 0 | case DLT_EN10MB: |
7960 | 0 | case DLT_NETANALYZER: |
7961 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
7962 | 0 | case DLT_DSA_TAG_BRCM: |
7963 | 0 | case DLT_DSA_TAG_DSA: |
7964 | 0 | b1 = gen_prevlinkhdr_check(cstate); |
7965 | | /* ether[0] & 1 != 0 */ |
7966 | 0 | b0 = gen_mac_multicast(cstate, 0); |
7967 | 0 | return b1 ? gen_and(b1, b0) : b0; |
7968 | 0 | case DLT_FDDI: |
7969 | | /* |
7970 | | * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX |
7971 | | * |
7972 | | * XXX - was that referring to bit-order issues? |
7973 | | */ |
7974 | | /* fddi[1] & 1 != 0 */ |
7975 | 0 | return gen_mac_multicast(cstate, 1); |
7976 | 0 | case DLT_IEEE802: |
7977 | | /* tr[2] & 1 != 0 */ |
7978 | 0 | return gen_mac_multicast(cstate, 2); |
7979 | 0 | case DLT_IEEE802_11: |
7980 | 0 | case DLT_PRISM_HEADER: |
7981 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
7982 | 0 | case DLT_IEEE802_11_RADIO: |
7983 | 0 | case DLT_PPI: |
7984 | | /* |
7985 | | * Oh, yuk. |
7986 | | * |
7987 | | * For control frames, there is no DA. |
7988 | | * |
7989 | | * For management frames, DA is at an |
7990 | | * offset of 4 from the beginning of |
7991 | | * the packet. |
7992 | | * |
7993 | | * For data frames, DA is at an offset |
7994 | | * of 4 from the beginning of the packet |
7995 | | * if To DS is clear and at an offset of |
7996 | | * 16 from the beginning of the packet |
7997 | | * if To DS is set. |
7998 | | */ |
7999 | | |
8000 | | /* |
8001 | | * Generate the tests to be done for data frames. |
8002 | | * |
8003 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
8004 | | */ |
8005 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8006 | 0 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
8007 | | |
8008 | | /* |
8009 | | * If To DS is set, the DA is at 16. |
8010 | | */ |
8011 | 0 | b0 = gen_mac_multicast(cstate, 16); |
8012 | 0 | b0 = gen_and(b1, b0); |
8013 | | |
8014 | | /* |
8015 | | * Now, check for To DS not set, i.e. check |
8016 | | * "!(link[1] & 0x01)". |
8017 | | */ |
8018 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8019 | 0 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
8020 | | |
8021 | | /* |
8022 | | * If To DS is not set, the DA is at 4. |
8023 | | */ |
8024 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8025 | 0 | b1 = gen_and(b2, b1); |
8026 | | |
8027 | | /* |
8028 | | * Now OR together the last two checks. That gives |
8029 | | * the complete set of checks for data frames. |
8030 | | */ |
8031 | 0 | b0 = gen_or(b1, b0); |
8032 | | |
8033 | | /* |
8034 | | * Now check for a data frame. |
8035 | | * I.e, check "link[0] & 0x08". |
8036 | | */ |
8037 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8038 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
8039 | | |
8040 | | /* |
8041 | | * AND that with the checks done for data frames. |
8042 | | */ |
8043 | 0 | b0 = gen_and(b1, b0); |
8044 | | |
8045 | | /* |
8046 | | * If the high-order bit of the type value is 0, this |
8047 | | * is a management frame. |
8048 | | * I.e, check "!(link[0] & 0x08)". |
8049 | | */ |
8050 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8051 | 0 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
8052 | | |
8053 | | /* |
8054 | | * For management frames, the DA is at 4. |
8055 | | */ |
8056 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8057 | 0 | b1 = gen_and(b2, b1); |
8058 | | |
8059 | | /* |
8060 | | * OR that with the checks done for data frames. |
8061 | | * That gives the checks done for management and |
8062 | | * data frames. |
8063 | | */ |
8064 | 0 | b0 = gen_or(b1, b0); |
8065 | | |
8066 | | /* |
8067 | | * If the low-order bit of the type value is 1, |
8068 | | * this is either a control frame or a frame |
8069 | | * with a reserved type, and thus not a |
8070 | | * frame with an SA. |
8071 | | * |
8072 | | * I.e., check "!(link[0] & 0x04)". |
8073 | | */ |
8074 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8075 | 0 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
8076 | | |
8077 | | /* |
8078 | | * AND that with the checks for data and management |
8079 | | * frames. |
8080 | | */ |
8081 | 0 | return gen_and(b1, b0); |
8082 | 0 | case DLT_IP_OVER_FC: |
8083 | 0 | return gen_mac_multicast(cstate, 2); |
8084 | 0 | default: |
8085 | 0 | break; |
8086 | 0 | } |
8087 | 0 | fail_kw_on_dlt(cstate, "multicast"); |
8088 | | /*NOTREACHED*/ |
8089 | | |
8090 | 0 | case Q_IP: |
8091 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
8092 | | |
8093 | | /* |
8094 | | * Compare address with 224.0.0.0/4 |
8095 | | */ |
8096 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_B, 0xe0, 0xf0); |
8097 | |
|
8098 | 0 | return gen_and(b0, b1); |
8099 | | |
8100 | 0 | case Q_IPV6: |
8101 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
8102 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255); |
8103 | 0 | return gen_and(b0, b1); |
8104 | 0 | } |
8105 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "multicast"); |
8106 | | /*NOTREACHED*/ |
8107 | 0 | } |
8108 | | |
8109 | | #ifdef __linux__ |
8110 | | /* |
8111 | | * This is Linux; we require PF_PACKET support. If this is a *live* capture, |
8112 | | * we can look at special meta-data in the filter expression; otherwise we |
8113 | | * can't because it is either a savefile (rfile != NULL) or a pcap_t created |
8114 | | * using pcap_open_dead() (rfile == NULL). Thus check for a flag that |
8115 | | * pcap_activate() conditionally sets. |
8116 | | */ |
8117 | | static void |
8118 | | require_basic_bpf_extensions(compiler_state_t *cstate, const char *keyword) |
8119 | 0 | { |
8120 | 0 | if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_BASIC_HANDLING) |
8121 | 0 | return; |
8122 | 0 | bpf_error(cstate, "not a live capture, '%s' not supported on %s", |
8123 | 0 | keyword, |
8124 | 0 | pcapint_datalink_val_to_string(cstate->linktype)); |
8125 | 0 | } |
8126 | | #endif // __linux__ |
8127 | | |
8128 | | struct block * |
8129 | | gen_ifindex(compiler_state_t *cstate, int ifindex) |
8130 | 0 | { |
8131 | | /* |
8132 | | * Catch errors reported by us and routines below us, and return NULL |
8133 | | * on an error. |
8134 | | */ |
8135 | 0 | if (setjmp(cstate->top_ctx)) |
8136 | 0 | return (NULL); |
8137 | | |
8138 | | /* |
8139 | | * Only some data link types support ifindex qualifiers. |
8140 | | */ |
8141 | 0 | switch (cstate->linktype) { |
8142 | 0 | case DLT_LINUX_SLL2: |
8143 | | /* match packets on this interface */ |
8144 | 0 | return gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex); |
8145 | 0 | default: |
8146 | 0 | #if defined(__linux__) |
8147 | 0 | require_basic_bpf_extensions(cstate, "ifindex"); |
8148 | | /* match ifindex */ |
8149 | 0 | return gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W, |
8150 | 0 | ifindex); |
8151 | | #else /* defined(__linux__) */ |
8152 | | fail_kw_on_dlt(cstate, "ifindex"); |
8153 | | /*NOTREACHED*/ |
8154 | | #endif /* defined(__linux__) */ |
8155 | 0 | } |
8156 | 0 | } |
8157 | | |
8158 | | /* |
8159 | | * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic. |
8160 | | * Outbound traffic is sent by this machine, while inbound traffic is |
8161 | | * sent by a remote machine (and may include packets destined for a |
8162 | | * unicast or multicast link-layer address we are not subscribing to). |
8163 | | * These are the same definitions implemented by pcap_setdirection(). |
8164 | | * Capturing only unicast traffic destined for this host is probably |
8165 | | * better accomplished using a higher-layer filter. |
8166 | | */ |
8167 | | struct block * |
8168 | | gen_inbound_outbound(compiler_state_t *cstate, const int outbound) |
8169 | 0 | { |
8170 | 0 | struct block *b0; |
8171 | | |
8172 | | /* |
8173 | | * Catch errors reported by us and routines below us, and return NULL |
8174 | | * on an error. |
8175 | | */ |
8176 | 0 | if (setjmp(cstate->top_ctx)) |
8177 | 0 | return (NULL); |
8178 | | |
8179 | | /* |
8180 | | * Only some data link types support inbound/outbound qualifiers. |
8181 | | */ |
8182 | 0 | switch (cstate->linktype) { |
8183 | 0 | case DLT_SLIP: |
8184 | 0 | return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, |
8185 | 0 | outbound ? SLIPDIR_OUT : SLIPDIR_IN); |
8186 | | |
8187 | 0 | case DLT_IPNET: |
8188 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, |
8189 | 0 | outbound ? IPNET_OUTBOUND : IPNET_INBOUND); |
8190 | | |
8191 | 0 | case DLT_LINUX_SLL: |
8192 | | /* match outgoing packets */ |
8193 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING); |
8194 | | // To filter on inbound traffic, invert the match. |
8195 | 0 | return outbound ? b0 : gen_not(b0); |
8196 | | |
8197 | 0 | case DLT_LINUX_SLL2: |
8198 | | /* match outgoing packets */ |
8199 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING); |
8200 | | // To filter on inbound traffic, invert the match. |
8201 | 0 | return outbound ? b0 : gen_not(b0); |
8202 | | |
8203 | 0 | case DLT_PFLOG: |
8204 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B, |
8205 | 0 | outbound ? PF_OUT : PF_IN); |
8206 | | |
8207 | 0 | case DLT_PPP_PPPD: |
8208 | 0 | return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, outbound ? PPP_PPPD_OUT : PPP_PPPD_IN); |
8209 | | |
8210 | 0 | case DLT_JUNIPER_MFR: |
8211 | 0 | case DLT_JUNIPER_MLFR: |
8212 | 0 | case DLT_JUNIPER_MLPPP: |
8213 | 0 | case DLT_JUNIPER_ATM1: |
8214 | 0 | case DLT_JUNIPER_ATM2: |
8215 | 0 | case DLT_JUNIPER_PPPOE: |
8216 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
8217 | 0 | case DLT_JUNIPER_GGSN: |
8218 | 0 | case DLT_JUNIPER_ES: |
8219 | 0 | case DLT_JUNIPER_MONITOR: |
8220 | 0 | case DLT_JUNIPER_SERVICES: |
8221 | 0 | case DLT_JUNIPER_ETHER: |
8222 | 0 | case DLT_JUNIPER_PPP: |
8223 | 0 | case DLT_JUNIPER_FRELAY: |
8224 | 0 | case DLT_JUNIPER_CHDLC: |
8225 | 0 | case DLT_JUNIPER_VP: |
8226 | 0 | case DLT_JUNIPER_ST: |
8227 | 0 | case DLT_JUNIPER_ISM: |
8228 | 0 | case DLT_JUNIPER_VS: |
8229 | 0 | case DLT_JUNIPER_SRX_E2E: |
8230 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
8231 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
8232 | | /* juniper flags (including direction) are stored |
8233 | | * the byte after the 3-byte magic number */ |
8234 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, outbound ? 0 : 1, 0x01); |
8235 | | |
8236 | 0 | case DLT_DSA_TAG_BRCM: |
8237 | | /* |
8238 | | * This DSA tag encodes the frame direction in the three most |
8239 | | * significant bits of its first octet: 0b000***** ("egress", |
8240 | | * switch -> CPU) means "inbound" in libpcap terms and |
8241 | | * 0b001***** ("ingress", CPU -> switch) means "outbound". |
8242 | | */ |
8243 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 6 + 6, BPF_B, |
8244 | 0 | outbound ? 0x20 : 0x00, 0xe0); |
8245 | | |
8246 | 0 | case DLT_DSA_TAG_DSA: |
8247 | | /* |
8248 | | * This DSA tag does not encode the frame direction, but it |
8249 | | * encodes the frame mode, and some modes imply exactly one |
8250 | | * direction. The mode is the two most significant bits of the |
8251 | | * first octet. 0b00****** ("To_CPU ingress") and 0b10****** |
8252 | | * ("To_Sniffer ingress") mean "inbound" in libpcap terms and |
8253 | | * 0b01****** ("From_CPU egress") means "outbound". 0x11****** |
8254 | | * ("Forward") can mean either direction, so cannot be used for |
8255 | | * this purpose. |
8256 | | * |
8257 | | * So match 0b01****** for outbound and 0b*0****** otherwise. |
8258 | | */ |
8259 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 6 + 6, BPF_B, |
8260 | 0 | outbound ? 0x40 : 0x00, |
8261 | 0 | outbound ? 0xc0 : 0x40); |
8262 | | |
8263 | 0 | default: |
8264 | | /* |
8265 | | * If we have packet meta-data indicating a direction, |
8266 | | * and that metadata can be checked by BPF code, check |
8267 | | * it. Otherwise, give up, as this link-layer type has |
8268 | | * nothing in the packet data. |
8269 | | * |
8270 | | * Currently, the only platform where a BPF filter can |
8271 | | * check that metadata is Linux with the in-kernel |
8272 | | * BPF interpreter. If other packet capture mechanisms |
8273 | | * and BPF filters also supported this, it would be |
8274 | | * nice. It would be even better if they made that |
8275 | | * metadata available so that we could provide it |
8276 | | * with newer capture APIs, allowing it to be saved |
8277 | | * in pcapng files. |
8278 | | */ |
8279 | 0 | #if defined(__linux__) |
8280 | 0 | require_basic_bpf_extensions(cstate, outbound ? "outbound" : "inbound"); |
8281 | | /* match outgoing packets */ |
8282 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H, |
8283 | 0 | PACKET_OUTGOING); |
8284 | | // To filter on inbound traffic, invert the match. |
8285 | 0 | return outbound ? b0 : gen_not(b0); |
8286 | | #else /* defined(__linux__) */ |
8287 | | fail_kw_on_dlt(cstate, outbound ? "outbound" : "inbound"); |
8288 | | /*NOTREACHED*/ |
8289 | | #endif /* defined(__linux__) */ |
8290 | 0 | } |
8291 | 0 | } |
8292 | | |
8293 | | /* PF firewall log matched interface */ |
8294 | | struct block * |
8295 | | gen_pf_ifname(compiler_state_t *cstate, const char *ifname) |
8296 | 0 | { |
8297 | 0 | u_int len, off; |
8298 | | |
8299 | | /* |
8300 | | * Catch errors reported by us and routines below us, and return NULL |
8301 | | * on an error. |
8302 | | */ |
8303 | 0 | if (setjmp(cstate->top_ctx)) |
8304 | 0 | return (NULL); |
8305 | | |
8306 | 0 | assert_pflog(cstate, "ifname"); |
8307 | |
|
8308 | 0 | len = sizeof(((struct pfloghdr *)0)->ifname); |
8309 | 0 | off = offsetof(struct pfloghdr, ifname); |
8310 | 0 | if (strlen(ifname) >= len) { |
8311 | 0 | bpf_error(cstate, "ifname interface names can only be %d characters", |
8312 | 0 | len-1); |
8313 | | /*NOTREACHED*/ |
8314 | 0 | } |
8315 | 0 | return gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname), |
8316 | 0 | (const u_char *)ifname); |
8317 | 0 | } |
8318 | | |
8319 | | /* PF firewall log ruleset name */ |
8320 | | struct block * |
8321 | | gen_pf_ruleset(compiler_state_t *cstate, char *ruleset) |
8322 | 0 | { |
8323 | | /* |
8324 | | * Catch errors reported by us and routines below us, and return NULL |
8325 | | * on an error. |
8326 | | */ |
8327 | 0 | if (setjmp(cstate->top_ctx)) |
8328 | 0 | return (NULL); |
8329 | | |
8330 | 0 | assert_pflog(cstate, "ruleset"); |
8331 | |
|
8332 | 0 | if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { |
8333 | 0 | bpf_error(cstate, "ruleset names can only be %ld characters", |
8334 | 0 | (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1)); |
8335 | | /*NOTREACHED*/ |
8336 | 0 | } |
8337 | | |
8338 | 0 | return gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset), |
8339 | 0 | (u_int)strlen(ruleset), (const u_char *)ruleset); |
8340 | 0 | } |
8341 | | |
8342 | | /* PF firewall log rule number */ |
8343 | | struct block * |
8344 | | gen_pf_rnr(compiler_state_t *cstate, int rnr) |
8345 | 0 | { |
8346 | | /* |
8347 | | * Catch errors reported by us and routines below us, and return NULL |
8348 | | * on an error. |
8349 | | */ |
8350 | 0 | if (setjmp(cstate->top_ctx)) |
8351 | 0 | return (NULL); |
8352 | | |
8353 | 0 | assert_pflog(cstate, "rnr"); |
8354 | |
|
8355 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W, |
8356 | 0 | (bpf_u_int32)rnr); |
8357 | 0 | } |
8358 | | |
8359 | | /* PF firewall log sub-rule number */ |
8360 | | struct block * |
8361 | | gen_pf_srnr(compiler_state_t *cstate, int srnr) |
8362 | 0 | { |
8363 | | /* |
8364 | | * Catch errors reported by us and routines below us, and return NULL |
8365 | | * on an error. |
8366 | | */ |
8367 | 0 | if (setjmp(cstate->top_ctx)) |
8368 | 0 | return (NULL); |
8369 | | |
8370 | 0 | assert_pflog(cstate, "srnr"); |
8371 | |
|
8372 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W, |
8373 | 0 | (bpf_u_int32)srnr); |
8374 | 0 | } |
8375 | | |
8376 | | /* PF firewall log reason code */ |
8377 | | struct block * |
8378 | | gen_pf_reason(compiler_state_t *cstate, int reason) |
8379 | 0 | { |
8380 | | /* |
8381 | | * Catch errors reported by us and routines below us, and return NULL |
8382 | | * on an error. |
8383 | | */ |
8384 | 0 | if (setjmp(cstate->top_ctx)) |
8385 | 0 | return (NULL); |
8386 | | |
8387 | 0 | assert_pflog(cstate, "reason"); |
8388 | |
|
8389 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B, |
8390 | 0 | (bpf_u_int32)reason); |
8391 | 0 | } |
8392 | | |
8393 | | /* PF firewall log action */ |
8394 | | struct block * |
8395 | | gen_pf_action(compiler_state_t *cstate, int action) |
8396 | 0 | { |
8397 | | /* |
8398 | | * Catch errors reported by us and routines below us, and return NULL |
8399 | | * on an error. |
8400 | | */ |
8401 | 0 | if (setjmp(cstate->top_ctx)) |
8402 | 0 | return (NULL); |
8403 | | |
8404 | 0 | assert_pflog(cstate, "action"); |
8405 | |
|
8406 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B, |
8407 | 0 | (bpf_u_int32)action); |
8408 | 0 | } |
8409 | | |
8410 | | /* IEEE 802.11 wireless header */ |
8411 | | struct block * |
8412 | | gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask) |
8413 | 0 | { |
8414 | | /* |
8415 | | * Catch errors reported by us and routines below us, and return NULL |
8416 | | * on an error. |
8417 | | */ |
8418 | 0 | if (setjmp(cstate->top_ctx)) |
8419 | 0 | return (NULL); |
8420 | | |
8421 | 0 | switch (cstate->linktype) { |
8422 | | |
8423 | 0 | case DLT_IEEE802_11: |
8424 | 0 | case DLT_PRISM_HEADER: |
8425 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8426 | 0 | case DLT_IEEE802_11_RADIO: |
8427 | 0 | case DLT_PPI: |
8428 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask); |
8429 | | |
8430 | 0 | default: |
8431 | 0 | fail_kw_on_dlt(cstate, "type/subtype"); |
8432 | | /*NOTREACHED*/ |
8433 | 0 | } |
8434 | 0 | } |
8435 | | |
8436 | | struct block * |
8437 | | gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir) |
8438 | 0 | { |
8439 | | /* |
8440 | | * Catch errors reported by us and routines below us, and return NULL |
8441 | | * on an error. |
8442 | | */ |
8443 | 0 | if (setjmp(cstate->top_ctx)) |
8444 | 0 | return (NULL); |
8445 | | |
8446 | 0 | switch (cstate->linktype) { |
8447 | | |
8448 | 0 | case DLT_IEEE802_11: |
8449 | 0 | case DLT_PRISM_HEADER: |
8450 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8451 | 0 | case DLT_IEEE802_11_RADIO: |
8452 | 0 | case DLT_PPI: |
8453 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir, |
8454 | 0 | IEEE80211_FC1_DIR_MASK); |
8455 | | |
8456 | 0 | default: |
8457 | 0 | fail_kw_on_dlt(cstate, "dir"); |
8458 | | /*NOTREACHED*/ |
8459 | 0 | } |
8460 | 0 | } |
8461 | | |
8462 | | static struct block * |
8463 | | gen_vlan_tpid_test(compiler_state_t *cstate) |
8464 | 0 | { |
8465 | 0 | struct block *b0, *b1; |
8466 | | |
8467 | | /* check for VLAN, including 802.1ad and QinQ */ |
8468 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_8021Q); |
8469 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_8021AD); |
8470 | 0 | b0 = gen_or(b0, b1); |
8471 | 0 | b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ); |
8472 | |
|
8473 | 0 | return gen_or(b0, b1); |
8474 | 0 | } |
8475 | | |
8476 | | static struct block * |
8477 | | gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num) |
8478 | 0 | { |
8479 | 0 | assert_maxval(cstate, "VLAN tag", vlan_num, 0x0fff); |
8480 | 0 | return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff); |
8481 | 0 | } |
8482 | | |
8483 | | static struct block * |
8484 | | gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8485 | | int has_vlan_tag) |
8486 | 0 | { |
8487 | 0 | struct block *b0, *b1; |
8488 | |
|
8489 | 0 | b0 = gen_vlan_tpid_test(cstate); |
8490 | |
|
8491 | 0 | if (has_vlan_tag) { |
8492 | 0 | b1 = gen_vlan_vid_test(cstate, vlan_num); |
8493 | 0 | b0 = gen_and(b0, b1); |
8494 | 0 | } |
8495 | | |
8496 | | /* |
8497 | | * Both payload and link header type follow the VLAN tags so that |
8498 | | * both need to be updated. |
8499 | | */ |
8500 | 0 | cstate->off_linkpl.constant_part += 4; |
8501 | 0 | cstate->off_linktype.constant_part += 4; |
8502 | |
|
8503 | 0 | return b0; |
8504 | 0 | } |
8505 | | |
8506 | | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8507 | | /* add v to variable part of off */ |
8508 | | static void |
8509 | | gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, |
8510 | | bpf_u_int32 v, struct slist *s) |
8511 | 0 | { |
8512 | 0 | struct slist *s2; |
8513 | |
|
8514 | 0 | if (!off->is_variable) |
8515 | 0 | off->is_variable = 1; |
8516 | 0 | if (off->reg == -1) |
8517 | 0 | off->reg = alloc_reg(cstate); |
8518 | |
|
8519 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
8520 | 0 | s2->s.k = off->reg; |
8521 | 0 | sappend(s, s2); |
8522 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
8523 | 0 | s2->s.k = v; |
8524 | 0 | sappend(s, s2); |
8525 | 0 | s2 = new_stmt(cstate, BPF_ST); |
8526 | 0 | s2->s.k = off->reg; |
8527 | 0 | sappend(s, s2); |
8528 | 0 | } |
8529 | | |
8530 | | /* |
8531 | | * patch block b_tpid (VLAN TPID test) to update variable parts of link payload |
8532 | | * and link type offsets first |
8533 | | */ |
8534 | | static void |
8535 | | gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid) |
8536 | 0 | { |
8537 | 0 | struct slist s; |
8538 | | |
8539 | | /* offset determined at run time, shift variable part */ |
8540 | 0 | s.next = NULL; |
8541 | 0 | cstate->is_vlan_vloffset = 1; |
8542 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s); |
8543 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s); |
8544 | | |
8545 | | /* we get a pointer to a chain of or-ed blocks, patch first of them */ |
8546 | 0 | sprepend_to_block(s.next, b_tpid->head); |
8547 | 0 | } |
8548 | | |
8549 | | /* |
8550 | | * patch block b_vid (VLAN id test) to load VID value either from packet |
8551 | | * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true |
8552 | | */ |
8553 | | static void |
8554 | | gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid) |
8555 | 0 | { |
8556 | 0 | struct slist *s, *s2, *sjeq; |
8557 | 0 | unsigned cnt; |
8558 | |
|
8559 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8560 | 0 | s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); |
8561 | | |
8562 | | /* true -> next instructions, false -> beginning of b_vid */ |
8563 | 0 | sjeq = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
8564 | 0 | sjeq->s.k = 1; |
8565 | 0 | sjeq->s.jf = b_vid->stmts; |
8566 | 0 | sappend(s, sjeq); |
8567 | |
|
8568 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_H|BPF_ABS); |
8569 | 0 | s2->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG); |
8570 | 0 | sappend(s, s2); |
8571 | 0 | sjeq->s.jt = s2; |
8572 | | |
8573 | | /* Jump to the test in b_vid. We need to jump one instruction before |
8574 | | * the end of the b_vid block so that we only skip loading the TCI |
8575 | | * from packet data and not the 'and' instruction extracting VID. |
8576 | | */ |
8577 | 0 | cnt = 0; |
8578 | 0 | for (s2 = b_vid->stmts; s2; s2 = s2->next) |
8579 | 0 | cnt++; |
8580 | 0 | s2 = new_stmt(cstate, JMP(BPF_JA, BPF_K)); |
8581 | 0 | s2->s.k = cnt - 1; |
8582 | 0 | sappend(s, s2); |
8583 | | |
8584 | | /* insert our statements at the beginning of b_vid */ |
8585 | 0 | sprepend_to_block(s, b_vid); |
8586 | 0 | } |
8587 | | |
8588 | | /* |
8589 | | * Generate check for "vlan" or "vlan <id>" on systems with support for BPF |
8590 | | * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN |
8591 | | * tag can be either in metadata or in packet data; therefore if the |
8592 | | * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link |
8593 | | * header for VLAN tag. As the decision is done at run time, we need |
8594 | | * update variable part of the offsets |
8595 | | */ |
8596 | | static struct block * |
8597 | | gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8598 | | int has_vlan_tag) |
8599 | 0 | { |
8600 | 0 | struct block *b0, *b_tpid, *b_vid = NULL; |
8601 | 0 | struct slist *s; |
8602 | | |
8603 | | /* generate new filter code based on extracting packet |
8604 | | * metadata */ |
8605 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8606 | 0 | s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); |
8607 | |
|
8608 | 0 | b0 = gen_jmp_k(cstate, BPF_JEQ, 1, s); |
8609 | | |
8610 | | /* |
8611 | | * This is tricky. We need to insert the statements updating variable |
8612 | | * parts of offsets before the traditional TPID and VID tests so |
8613 | | * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but |
8614 | | * we do not want this update to affect those checks. That's why we |
8615 | | * generate both test blocks first and insert the statements updating |
8616 | | * variable parts of both offsets after that. This wouldn't work if |
8617 | | * there already were variable length link header when entering this |
8618 | | * function but gen_vlan_bpf_extensions() isn't called in that case. |
8619 | | */ |
8620 | 0 | b_tpid = gen_vlan_tpid_test(cstate); |
8621 | 0 | if (has_vlan_tag) |
8622 | 0 | b_vid = gen_vlan_vid_test(cstate, vlan_num); |
8623 | |
|
8624 | 0 | gen_vlan_patch_tpid_test(cstate, b_tpid); |
8625 | 0 | b0 = gen_or(b0, b_tpid); |
8626 | |
|
8627 | 0 | if (has_vlan_tag) { |
8628 | 0 | gen_vlan_patch_vid_test(cstate, b_vid); |
8629 | 0 | b0 = gen_and(b0, b_vid); |
8630 | 0 | } |
8631 | |
|
8632 | 0 | return b0; |
8633 | 0 | } |
8634 | | #endif |
8635 | | |
8636 | | /* |
8637 | | * support IEEE 802.1Q VLAN trunk over ethernet |
8638 | | */ |
8639 | | struct block * |
8640 | | gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag) |
8641 | 0 | { |
8642 | 0 | struct block *b0; |
8643 | | |
8644 | | /* |
8645 | | * Catch errors reported by us and routines below us, and return NULL |
8646 | | * on an error. |
8647 | | */ |
8648 | 0 | if (setjmp(cstate->top_ctx)) |
8649 | 0 | return (NULL); |
8650 | | |
8651 | | /* can't check for VLAN-encapsulated packets inside MPLS */ |
8652 | 0 | if (cstate->label_stack_depth > 0) |
8653 | 0 | bpf_error(cstate, "no VLAN match after MPLS"); |
8654 | | |
8655 | | /* |
8656 | | * Check for a VLAN packet, and then change the offsets to point |
8657 | | * to the type and data fields within the VLAN packet. Just |
8658 | | * increment the offsets, so that we can support a hierarchy, e.g. |
8659 | | * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within |
8660 | | * VLAN 100. |
8661 | | * |
8662 | | * XXX - this is a bit of a kludge. If we were to split the |
8663 | | * compiler into a parser that parses an expression and |
8664 | | * generates an expression tree, and a code generator that |
8665 | | * takes an expression tree (which could come from our |
8666 | | * parser or from some other parser) and generates BPF code, |
8667 | | * we could perhaps make the offsets parameters of routines |
8668 | | * and, in the handler for an "AND" node, pass to subnodes |
8669 | | * other than the VLAN node the adjusted offsets. |
8670 | | * |
8671 | | * This would mean that "vlan" would, instead of changing the |
8672 | | * behavior of *all* tests after it, change only the behavior |
8673 | | * of tests ANDed with it. That would change the documented |
8674 | | * semantics of "vlan", which might break some expressions. |
8675 | | * However, it would mean that "(vlan and ip) or ip" would check |
8676 | | * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than |
8677 | | * checking only for VLAN-encapsulated IP, so that could still |
8678 | | * be considered worth doing; it wouldn't break expressions |
8679 | | * that are of the form "vlan and ..." or "vlan N and ...", |
8680 | | * which I suspect are the most common expressions involving |
8681 | | * "vlan". "vlan or ..." doesn't necessarily do what the user |
8682 | | * would really want, now, as all the "or ..." tests would |
8683 | | * be done assuming a VLAN, even though the "or" could be viewed |
8684 | | * as meaning "or, if this isn't a VLAN packet...". |
8685 | | */ |
8686 | 0 | switch (cstate->linktype) { |
8687 | | |
8688 | 0 | case DLT_EN10MB: |
8689 | | /* |
8690 | | * Newer version of the Linux kernel pass around |
8691 | | * packets in which the VLAN tag has been removed |
8692 | | * from the packet data and put into metadata. |
8693 | | * |
8694 | | * This requires special treatment. |
8695 | | */ |
8696 | 0 | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8697 | | /* Verify that this is the outer part of the packet and |
8698 | | * not encapsulated somehow. */ |
8699 | 0 | if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable && |
8700 | 0 | cstate->off_linkhdr.constant_part == |
8701 | 0 | cstate->off_outermostlinkhdr.constant_part) { |
8702 | | /* |
8703 | | * Do we need special VLAN handling? |
8704 | | */ |
8705 | 0 | if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING) |
8706 | 0 | b0 = gen_vlan_bpf_extensions(cstate, vlan_num, |
8707 | 0 | has_vlan_tag); |
8708 | 0 | else |
8709 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, |
8710 | 0 | vlan_num, has_vlan_tag); |
8711 | 0 | } else |
8712 | 0 | #endif |
8713 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, |
8714 | 0 | has_vlan_tag); |
8715 | 0 | break; |
8716 | | |
8717 | 0 | case DLT_NETANALYZER: |
8718 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
8719 | 0 | case DLT_DSA_TAG_BRCM: |
8720 | 0 | case DLT_DSA_TAG_DSA: |
8721 | 0 | case DLT_IEEE802_11: |
8722 | 0 | case DLT_PRISM_HEADER: |
8723 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8724 | 0 | case DLT_IEEE802_11_RADIO: |
8725 | | /* |
8726 | | * These are either Ethernet packets with an additional |
8727 | | * metadata header (the NetAnalyzer types), or 802.11 |
8728 | | * packets, possibly with an additional metadata header. |
8729 | | * |
8730 | | * For the first of those, the VLAN tag is in the normal |
8731 | | * place, so the special-case handling above isn't |
8732 | | * necessary. |
8733 | | * |
8734 | | * For the second of those, we don't do the special-case |
8735 | | * handling for now. |
8736 | | */ |
8737 | 0 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag); |
8738 | 0 | break; |
8739 | | |
8740 | 0 | default: |
8741 | 0 | fail_kw_on_dlt(cstate, "vlan"); |
8742 | | /*NOTREACHED*/ |
8743 | 0 | } |
8744 | | |
8745 | 0 | cstate->vlan_stack_depth++; |
8746 | |
|
8747 | 0 | return (b0); |
8748 | 0 | } |
8749 | | |
8750 | | /* |
8751 | | * support for MPLS |
8752 | | * |
8753 | | * The label_num_arg dance is to avoid annoying whining by compilers that |
8754 | | * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
8755 | | * It's not *used* after setjmp returns. |
8756 | | */ |
8757 | | static struct block * |
8758 | | gen_mpls_internal(compiler_state_t *cstate, bpf_u_int32 label_num, |
8759 | | int has_label_num) |
8760 | 0 | { |
8761 | 0 | struct block *b0, *b1; |
8762 | |
|
8763 | 0 | if (cstate->label_stack_depth > 0) { |
8764 | | /* just match the bottom-of-stack bit clear */ |
8765 | 0 | b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01); |
8766 | 0 | } else { |
8767 | | /* |
8768 | | * We're not in an MPLS stack yet, so check the link-layer |
8769 | | * type against MPLS. |
8770 | | */ |
8771 | 0 | switch (cstate->linktype) { |
8772 | | |
8773 | 0 | case DLT_C_HDLC: /* fall through */ |
8774 | 0 | case DLT_HDLC: |
8775 | 0 | case DLT_EN10MB: |
8776 | 0 | case DLT_NETANALYZER: |
8777 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
8778 | 0 | case DLT_DSA_TAG_BRCM: |
8779 | 0 | case DLT_DSA_TAG_DSA: |
8780 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_MPLS); |
8781 | 0 | break; |
8782 | | |
8783 | 0 | case DLT_PPP: |
8784 | 0 | b0 = gen_linktype(cstate, PPP_MPLS_UCAST); |
8785 | 0 | break; |
8786 | | |
8787 | | /* FIXME add other DLT_s ... |
8788 | | * for Frame-Relay/and ATM this may get messy due to SNAP headers |
8789 | | * leave it for now */ |
8790 | | |
8791 | 0 | default: |
8792 | 0 | fail_kw_on_dlt(cstate, "mpls"); |
8793 | | /*NOTREACHED*/ |
8794 | 0 | } |
8795 | 0 | } |
8796 | | |
8797 | | /* If a specific MPLS label is requested, check it */ |
8798 | 0 | if (has_label_num) { |
8799 | 0 | assert_maxval(cstate, "MPLS label", label_num, 0xFFFFF); |
8800 | 0 | label_num = label_num << 12; /* label is shifted 12 bits on the wire */ |
8801 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num, |
8802 | 0 | 0xfffff000); /* only compare the first 20 bits */ |
8803 | 0 | b0 = gen_and(b0, b1); |
8804 | 0 | } |
8805 | | |
8806 | | /* |
8807 | | * Change the offsets to point to the type and data fields within |
8808 | | * the MPLS packet. Just increment the offsets, so that we |
8809 | | * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to |
8810 | | * capture packets with an outer label of 100000 and an inner |
8811 | | * label of 1024. |
8812 | | * |
8813 | | * Increment the MPLS stack depth as well; this indicates that |
8814 | | * we're checking MPLS-encapsulated headers, to make sure higher |
8815 | | * level code generators don't try to match against IP-related |
8816 | | * protocols such as Q_ARP, Q_RARP etc. |
8817 | | * |
8818 | | * XXX - this is a bit of a kludge. See comments in gen_vlan(). |
8819 | | */ |
8820 | 0 | cstate->off_nl_nosnap += 4; |
8821 | 0 | cstate->off_nl += 4; |
8822 | 0 | cstate->label_stack_depth++; |
8823 | 0 | return (b0); |
8824 | 0 | } |
8825 | | |
8826 | | struct block * |
8827 | | gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num, int has_label_num) |
8828 | 0 | { |
8829 | | /* |
8830 | | * Catch errors reported by us and routines below us, and return NULL |
8831 | | * on an error. |
8832 | | */ |
8833 | 0 | if (setjmp(cstate->top_ctx)) |
8834 | 0 | return (NULL); |
8835 | | |
8836 | 0 | return gen_mpls_internal(cstate, label_num, has_label_num); |
8837 | 0 | } |
8838 | | |
8839 | | /* |
8840 | | * Support PPPOE discovery and session. |
8841 | | */ |
8842 | | struct block * |
8843 | | gen_pppoed(compiler_state_t *cstate) |
8844 | 0 | { |
8845 | | /* |
8846 | | * Catch errors reported by us and routines below us, and return NULL |
8847 | | * on an error. |
8848 | | */ |
8849 | 0 | if (setjmp(cstate->top_ctx)) |
8850 | 0 | return (NULL); |
8851 | | |
8852 | | /* check for PPPoE discovery */ |
8853 | 0 | return gen_linktype(cstate, ETHERTYPE_PPPOED); |
8854 | 0 | } |
8855 | | |
8856 | | /* |
8857 | | * RFC 2516 Section 4: |
8858 | | * |
8859 | | * The Ethernet payload for PPPoE is as follows: |
8860 | | * |
8861 | | * 1 2 3 |
8862 | | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
8863 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
8864 | | * | VER | TYPE | CODE | SESSION_ID | |
8865 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
8866 | | * | LENGTH | payload ~ |
8867 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
8868 | | */ |
8869 | | struct block * |
8870 | | gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num) |
8871 | 0 | { |
8872 | 0 | struct block *b0, *b1; |
8873 | | |
8874 | | /* |
8875 | | * Catch errors reported by us and routines below us, and return NULL |
8876 | | * on an error. |
8877 | | */ |
8878 | 0 | if (setjmp(cstate->top_ctx)) |
8879 | 0 | return (NULL); |
8880 | | |
8881 | | /* |
8882 | | * Test against the PPPoE session link-layer type. |
8883 | | */ |
8884 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_PPPOES); |
8885 | | |
8886 | | /* If a specific session is requested, check PPPoE session id */ |
8887 | 0 | if (has_sess_num) { |
8888 | 0 | assert_maxval(cstate, "PPPoE session number", sess_num, UINT16_MAX); |
8889 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 2, BPF_H, sess_num); |
8890 | 0 | b0 = gen_and(b0, b1); |
8891 | 0 | } |
8892 | | |
8893 | | /* |
8894 | | * Change the offsets to point to the type and data fields within |
8895 | | * the PPP packet, and note that this is PPPoE rather than |
8896 | | * raw PPP. |
8897 | | * |
8898 | | * XXX - this is a bit of a kludge. See the comments in |
8899 | | * gen_vlan(). |
8900 | | * |
8901 | | * The "network-layer" protocol is PPPoE, which has a 6-byte |
8902 | | * PPPoE header, followed by a PPP packet. |
8903 | | * |
8904 | | * There is no HDLC encapsulation for the PPP packet (it's |
8905 | | * encapsulated in PPPoES instead), so the link-layer type |
8906 | | * starts at the first byte of the PPP packet. For PPPoE, |
8907 | | * that offset is relative to the beginning of the total |
8908 | | * link-layer payload, including any 802.2 LLC header, so |
8909 | | * it's 6 bytes past cstate->off_nl. |
8910 | | */ |
8911 | 0 | PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable, |
8912 | 0 | cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */ |
8913 | 0 | cstate->off_linkpl.reg); |
8914 | |
|
8915 | 0 | cstate->off_linktype = cstate->off_linkhdr; |
8916 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2; |
8917 | |
|
8918 | 0 | cstate->off_nl = 0; |
8919 | 0 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
8920 | |
|
8921 | 0 | return b0; |
8922 | 0 | } |
8923 | | |
8924 | | /* Check that this is Geneve and the VNI is correct if |
8925 | | * specified. Parameterized to handle both IPv4 and IPv6. */ |
8926 | | static struct block * |
8927 | | gen_geneve_check(compiler_state_t *cstate, |
8928 | | struct block *(*gen_portfn)(compiler_state_t *, uint16_t, int, int), |
8929 | | enum e_offrel offrel, bpf_u_int32 vni, int has_vni) |
8930 | 0 | { |
8931 | 0 | struct block *b0, *b1; |
8932 | |
|
8933 | 0 | b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST); |
8934 | | |
8935 | | /* Check that we are operating on version 0. Otherwise, we |
8936 | | * can't decode the rest of the fields. The version is 2 bits |
8937 | | * in the first byte of the Geneve header. */ |
8938 | 0 | b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0); |
8939 | 0 | b0 = gen_and(b0, b1); |
8940 | |
|
8941 | 0 | if (has_vni) { |
8942 | 0 | assert_maxval(cstate, "Geneve VNI", vni, 0xffffff); |
8943 | 0 | vni <<= 8; /* VNI is in the upper 3 bytes */ |
8944 | 0 | b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00); |
8945 | 0 | b0 = gen_and(b0, b1); |
8946 | 0 | } |
8947 | |
|
8948 | 0 | return b0; |
8949 | 0 | } |
8950 | | |
8951 | | /* The IPv4 and IPv6 Geneve checks need to do two things: |
8952 | | * - Verify that this actually is Geneve with the right VNI. |
8953 | | * - Place the IP header length (plus variable link prefix if |
8954 | | * needed) into register A to be used later to compute |
8955 | | * the inner packet offsets. */ |
8956 | | static struct block * |
8957 | | gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
8958 | 0 | { |
8959 | 0 | struct block *b0, *b1; |
8960 | 0 | struct slist *s, *s1; |
8961 | |
|
8962 | 0 | b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); |
8963 | | |
8964 | | /* Load the IP header length into A. */ |
8965 | 0 | s = gen_loadx_iphdrlen(cstate); |
8966 | |
|
8967 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
8968 | 0 | sappend(s, s1); |
8969 | | |
8970 | | /* Forcibly append these statements to the true condition |
8971 | | * of the protocol check by creating a new block that is |
8972 | | * always true and ANDing them. */ |
8973 | 0 | b1 = gen_jmp_x(cstate, BPF_JEQ, s); |
8974 | |
|
8975 | 0 | return gen_and(b0, b1); |
8976 | 0 | } |
8977 | | |
8978 | | static struct block * |
8979 | | gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
8980 | 0 | { |
8981 | 0 | struct block *b0, *b1; |
8982 | 0 | struct slist *s, *s1; |
8983 | |
|
8984 | 0 | b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); |
8985 | | |
8986 | | /* Load the IP header length. We need to account for a |
8987 | | * variable length link prefix if there is one. */ |
8988 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
8989 | 0 | if (s) { |
8990 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IMM); |
8991 | 0 | s1->s.k = 40; |
8992 | 0 | sappend(s, s1); |
8993 | |
|
8994 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
8995 | 0 | s1->s.k = 0; |
8996 | 0 | sappend(s, s1); |
8997 | 0 | } else { |
8998 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
8999 | 0 | s->s.k = 40; |
9000 | 0 | } |
9001 | | |
9002 | | /* Forcibly append these statements to the true condition |
9003 | | * of the protocol check by creating a new block that is |
9004 | | * always true and ANDing them. */ |
9005 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9006 | 0 | sappend(s, s1); |
9007 | |
|
9008 | 0 | b1 = gen_jmp_x(cstate, BPF_JEQ, s); |
9009 | |
|
9010 | 0 | return gen_and(b0, b1); |
9011 | 0 | } |
9012 | | |
9013 | | /* We need to store three values based on the Geneve header:: |
9014 | | * - The offset of the linktype. |
9015 | | * - The offset of the end of the Geneve header. |
9016 | | * - The offset of the end of the encapsulated MAC header. */ |
9017 | | static struct slist * |
9018 | | gen_geneve_offsets(compiler_state_t *cstate) |
9019 | 0 | { |
9020 | 0 | struct slist *s, *s1, *s_proto; |
9021 | | |
9022 | | /* First we need to calculate the offset of the Geneve header |
9023 | | * itself. This is composed of the IP header previously calculated |
9024 | | * (include any variable link prefix) and stored in A plus the |
9025 | | * fixed sized headers (fixed link prefix, MAC length, and UDP |
9026 | | * header). */ |
9027 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9028 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; |
9029 | | |
9030 | | /* Stash this in X since we'll need it later. */ |
9031 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9032 | 0 | sappend(s, s1); |
9033 | | |
9034 | | /* The EtherType in Geneve is 2 bytes in. Calculate this and |
9035 | | * store it. */ |
9036 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9037 | 0 | s1->s.k = 2; |
9038 | 0 | sappend(s, s1); |
9039 | |
|
9040 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
9041 | 0 | cstate->off_linktype.is_variable = 1; |
9042 | 0 | cstate->off_linktype.constant_part = 0; |
9043 | |
|
9044 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9045 | 0 | s1->s.k = cstate->off_linktype.reg; |
9046 | 0 | sappend(s, s1); |
9047 | | |
9048 | | /* Load the Geneve option length and mask and shift to get the |
9049 | | * number of bytes. It is stored in the first byte of the Geneve |
9050 | | * header. */ |
9051 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
9052 | 0 | s1->s.k = 0; |
9053 | 0 | sappend(s, s1); |
9054 | |
|
9055 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
9056 | 0 | s1->s.k = 0x3f; |
9057 | 0 | sappend(s, s1); |
9058 | |
|
9059 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
9060 | 0 | s1->s.k = 4; |
9061 | 0 | sappend(s, s1); |
9062 | | |
9063 | | /* Add in the rest of the Geneve base header. */ |
9064 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9065 | 0 | s1->s.k = 8; |
9066 | 0 | sappend(s, s1); |
9067 | | |
9068 | | /* Add the Geneve header length to its offset and store. */ |
9069 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9070 | 0 | s1->s.k = 0; |
9071 | 0 | sappend(s, s1); |
9072 | | |
9073 | | /* Set the encapsulated type as Ethernet. Even though we may |
9074 | | * not actually have Ethernet inside there are two reasons this |
9075 | | * is useful: |
9076 | | * - The linktype field is always in EtherType format regardless |
9077 | | * of whether it is in Geneve or an inner Ethernet frame. |
9078 | | * - The only link layer that we have specific support for is |
9079 | | * Ethernet. We will confirm that the packet actually is |
9080 | | * Ethernet at runtime before executing these checks. */ |
9081 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); |
9082 | |
|
9083 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9084 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9085 | 0 | sappend(s, s1); |
9086 | | |
9087 | | /* Calculate whether we have an Ethernet header or just raw IP/ |
9088 | | * MPLS/etc. If we have Ethernet, advance the end of the MAC offset |
9089 | | * and linktype by 14 bytes so that the network header can be found |
9090 | | * seamlessly. Otherwise, keep what we've calculated already. */ |
9091 | | |
9092 | | /* We have a bare jmp so we can't use the optimizer. */ |
9093 | 0 | cstate->no_optimize = 1; |
9094 | | |
9095 | | /* Load the EtherType in the Geneve header, 2 bytes in. */ |
9096 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H); |
9097 | 0 | s1->s.k = 2; |
9098 | 0 | sappend(s, s1); |
9099 | | |
9100 | | /* Load X with the end of the Geneve header. */ |
9101 | 0 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9102 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9103 | 0 | sappend(s, s1); |
9104 | | |
9105 | | /* Check if the EtherType is Transparent Ethernet Bridging. At the |
9106 | | * end of this check, we should have the total length in X. In |
9107 | | * the non-Ethernet case, it's already there. */ |
9108 | 0 | s_proto = new_stmt(cstate, JMP(BPF_JEQ, BPF_K)); |
9109 | 0 | s_proto->s.k = ETHERTYPE_TEB; |
9110 | 0 | sappend(s, s_proto); |
9111 | |
|
9112 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9113 | 0 | sappend(s, s1); |
9114 | 0 | s_proto->s.jt = s1; |
9115 | | |
9116 | | /* Since this is Ethernet, use the EtherType of the payload |
9117 | | * directly as the linktype. Overwrite what we already have. */ |
9118 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9119 | 0 | s1->s.k = 12; |
9120 | 0 | sappend(s, s1); |
9121 | |
|
9122 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9123 | 0 | s1->s.k = cstate->off_linktype.reg; |
9124 | 0 | sappend(s, s1); |
9125 | | |
9126 | | /* Advance two bytes further to get the end of the Ethernet |
9127 | | * header. */ |
9128 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9129 | 0 | s1->s.k = 2; |
9130 | 0 | sappend(s, s1); |
9131 | | |
9132 | | /* Move the result to X. */ |
9133 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9134 | 0 | sappend(s, s1); |
9135 | | |
9136 | | /* Store the final result of our linkpl calculation. */ |
9137 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
9138 | 0 | cstate->off_linkpl.is_variable = 1; |
9139 | 0 | cstate->off_linkpl.constant_part = 0; |
9140 | |
|
9141 | 0 | s1 = new_stmt(cstate, BPF_STX); |
9142 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9143 | 0 | sappend(s, s1); |
9144 | 0 | s_proto->s.jf = s1; |
9145 | |
|
9146 | 0 | cstate->off_nl = 0; |
9147 | |
|
9148 | 0 | return s; |
9149 | 0 | } |
9150 | | |
9151 | | /* Check to see if this is a Geneve packet. */ |
9152 | | struct block * |
9153 | | gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9154 | 0 | { |
9155 | 0 | struct block *b0, *b1; |
9156 | | |
9157 | | /* |
9158 | | * Catch errors reported by us and routines below us, and return NULL |
9159 | | * on an error. |
9160 | | */ |
9161 | 0 | if (setjmp(cstate->top_ctx)) |
9162 | 0 | return (NULL); |
9163 | | |
9164 | 0 | b0 = gen_geneve4(cstate, vni, has_vni); |
9165 | 0 | b1 = gen_geneve6(cstate, vni, has_vni); |
9166 | | |
9167 | | /* Later filters should act on the payload of the Geneve frame, |
9168 | | * update all of the header pointers. Attach this code so that |
9169 | | * it gets executed in the event that the Geneve filter matches. */ |
9170 | 0 | struct block *offsets = |
9171 | 0 | sprepend_to_block(gen_geneve_offsets(cstate),gen_true(cstate)); |
9172 | |
|
9173 | 0 | cstate->is_encap = 1; |
9174 | |
|
9175 | 0 | return gen_and(gen_or(b0, b1), offsets); |
9176 | 0 | } |
9177 | | |
9178 | | /* Check that this is VXLAN and the VNI is correct if |
9179 | | * specified. Parameterized to handle both IPv4 and IPv6. */ |
9180 | | static struct block * |
9181 | | gen_vxlan_check(compiler_state_t *cstate, |
9182 | | struct block *(*gen_portfn)(compiler_state_t *, uint16_t, int, int), |
9183 | | enum e_offrel offrel, bpf_u_int32 vni, int has_vni) |
9184 | 0 | { |
9185 | 0 | struct block *b0, *b1; |
9186 | |
|
9187 | 0 | b0 = gen_portfn(cstate, VXLAN_PORT, IPPROTO_UDP, Q_DST); |
9188 | | |
9189 | | /* Check that the VXLAN header has the flag bits set |
9190 | | * correctly. */ |
9191 | 0 | b1 = gen_cmp(cstate, offrel, 8, BPF_B, 0x08); |
9192 | 0 | b0 = gen_and(b0, b1); |
9193 | |
|
9194 | 0 | if (has_vni) { |
9195 | 0 | assert_maxval(cstate, "VXLAN VNI", vni, 0xffffff); |
9196 | 0 | vni <<= 8; /* VNI is in the upper 3 bytes */ |
9197 | 0 | b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00); |
9198 | 0 | b0 = gen_and(b0, b1); |
9199 | 0 | } |
9200 | |
|
9201 | 0 | return b0; |
9202 | 0 | } |
9203 | | |
9204 | | /* The IPv4 and IPv6 VXLAN checks need to do two things: |
9205 | | * - Verify that this actually is VXLAN with the right VNI. |
9206 | | * - Place the IP header length (plus variable link prefix if |
9207 | | * needed) into register A to be used later to compute |
9208 | | * the inner packet offsets. */ |
9209 | | static struct block * |
9210 | | gen_vxlan4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9211 | 0 | { |
9212 | 0 | struct block *b0, *b1; |
9213 | 0 | struct slist *s, *s1; |
9214 | |
|
9215 | 0 | b0 = gen_vxlan_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); |
9216 | | |
9217 | | /* Load the IP header length into A. */ |
9218 | 0 | s = gen_loadx_iphdrlen(cstate); |
9219 | |
|
9220 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9221 | 0 | sappend(s, s1); |
9222 | | |
9223 | | /* Forcibly append these statements to the true condition |
9224 | | * of the protocol check by creating a new block that is |
9225 | | * always true and ANDing them. */ |
9226 | 0 | b1 = gen_jmp_x(cstate, BPF_JEQ, s); |
9227 | |
|
9228 | 0 | return gen_and(b0, b1); |
9229 | 0 | } |
9230 | | |
9231 | | static struct block * |
9232 | | gen_vxlan6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9233 | 0 | { |
9234 | 0 | struct block *b0, *b1; |
9235 | 0 | struct slist *s, *s1; |
9236 | |
|
9237 | 0 | b0 = gen_vxlan_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); |
9238 | | |
9239 | | /* Load the IP header length. We need to account for a |
9240 | | * variable length link prefix if there is one. */ |
9241 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
9242 | 0 | if (s) { |
9243 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IMM); |
9244 | 0 | s1->s.k = 40; |
9245 | 0 | sappend(s, s1); |
9246 | |
|
9247 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9248 | 0 | s1->s.k = 0; |
9249 | 0 | sappend(s, s1); |
9250 | 0 | } else { |
9251 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
9252 | 0 | s->s.k = 40; |
9253 | 0 | } |
9254 | | |
9255 | | /* Forcibly append these statements to the true condition |
9256 | | * of the protocol check by creating a new block that is |
9257 | | * always true and ANDing them. */ |
9258 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9259 | 0 | sappend(s, s1); |
9260 | |
|
9261 | 0 | b1 = gen_jmp_x(cstate, BPF_JEQ, s); |
9262 | |
|
9263 | 0 | return gen_and(b0, b1); |
9264 | 0 | } |
9265 | | |
9266 | | /* We need to store three values based on the VXLAN header: |
9267 | | * - The offset of the linktype. |
9268 | | * - The offset of the end of the VXLAN header. |
9269 | | * - The offset of the end of the encapsulated MAC header. */ |
9270 | | static struct slist * |
9271 | | gen_vxlan_offsets(compiler_state_t *cstate) |
9272 | 0 | { |
9273 | 0 | struct slist *s, *s1; |
9274 | | |
9275 | | /* Calculate the offset of the VXLAN header itself. This |
9276 | | * includes the IP header computed previously (including any |
9277 | | * variable link prefix) and stored in A plus the fixed size |
9278 | | * headers (fixed link prefix, MAC length, UDP header). */ |
9279 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9280 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; |
9281 | | |
9282 | | /* Add the VXLAN header length to its offset and store */ |
9283 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9284 | 0 | s1->s.k = 8; |
9285 | 0 | sappend(s, s1); |
9286 | | |
9287 | | /* Push the link header. VXLAN packets always contain Ethernet |
9288 | | * frames. */ |
9289 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); |
9290 | |
|
9291 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9292 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9293 | 0 | sappend(s, s1); |
9294 | | |
9295 | | /* As the payload is an Ethernet packet, we can use the |
9296 | | * EtherType of the payload directly as the linktype. */ |
9297 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9298 | 0 | s1->s.k = 12; |
9299 | 0 | sappend(s, s1); |
9300 | |
|
9301 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
9302 | 0 | cstate->off_linktype.is_variable = 1; |
9303 | 0 | cstate->off_linktype.constant_part = 0; |
9304 | |
|
9305 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9306 | 0 | s1->s.k = cstate->off_linktype.reg; |
9307 | 0 | sappend(s, s1); |
9308 | | |
9309 | | /* Two bytes further is the end of the Ethernet header and the |
9310 | | * start of the payload. */ |
9311 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9312 | 0 | s1->s.k = 2; |
9313 | 0 | sappend(s, s1); |
9314 | | |
9315 | | /* Move the result to X. */ |
9316 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9317 | 0 | sappend(s, s1); |
9318 | | |
9319 | | /* Store the final result of our linkpl calculation. */ |
9320 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
9321 | 0 | cstate->off_linkpl.is_variable = 1; |
9322 | 0 | cstate->off_linkpl.constant_part = 0; |
9323 | |
|
9324 | 0 | s1 = new_stmt(cstate, BPF_STX); |
9325 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9326 | 0 | sappend(s, s1); |
9327 | |
|
9328 | 0 | cstate->off_nl = 0; |
9329 | |
|
9330 | 0 | return s; |
9331 | 0 | } |
9332 | | |
9333 | | /* Check to see if this is a VXLAN packet. */ |
9334 | | struct block * |
9335 | | gen_vxlan(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9336 | 0 | { |
9337 | 0 | struct block *b0, *b1; |
9338 | | |
9339 | | /* |
9340 | | * Catch errors reported by us and routines below us, and return NULL |
9341 | | * on an error. |
9342 | | */ |
9343 | 0 | if (setjmp(cstate->top_ctx)) |
9344 | 0 | return (NULL); |
9345 | | |
9346 | 0 | b0 = gen_vxlan4(cstate, vni, has_vni); |
9347 | 0 | b1 = gen_vxlan6(cstate, vni, has_vni); |
9348 | | |
9349 | | /* Later filters should act on the payload of the VXLAN frame, |
9350 | | * update all of the header pointers. Attach this code so that |
9351 | | * it gets executed in the event that the VXLAN filter matches. */ |
9352 | 0 | struct block *offsets = |
9353 | 0 | sprepend_to_block(gen_vxlan_offsets(cstate), gen_true(cstate)); |
9354 | |
|
9355 | 0 | cstate->is_encap = 1; |
9356 | |
|
9357 | 0 | return gen_and(gen_or(b0, b1), offsets); |
9358 | 0 | } |
9359 | | |
9360 | | /* Check that the encapsulated frame has a link layer header |
9361 | | * for Ethernet filters. */ |
9362 | | static struct block * |
9363 | | gen_encap_ll_check(compiler_state_t *cstate) |
9364 | 0 | { |
9365 | 0 | struct block *b0; |
9366 | 0 | struct slist *s, *s1; |
9367 | | |
9368 | | /* The easiest way to see if there is a link layer present |
9369 | | * is to check if the link layer header and payload are not |
9370 | | * the same. */ |
9371 | | |
9372 | | /* Geneve always generates pure variable offsets so we can |
9373 | | * compare only the registers. */ |
9374 | 0 | s = new_stmt(cstate, BPF_LD|BPF_MEM); |
9375 | 0 | s->s.k = cstate->off_linkhdr.reg; |
9376 | |
|
9377 | 0 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9378 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9379 | 0 | sappend(s, s1); |
9380 | |
|
9381 | 0 | b0 = gen_jmp_x(cstate, BPF_JEQ, s); |
9382 | |
|
9383 | 0 | return gen_not(b0); |
9384 | 0 | } |
9385 | | |
9386 | | static struct block * |
9387 | | gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield, |
9388 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9389 | 0 | { |
9390 | 0 | assert_atm(cstate, atmkw(atmfield)); |
9391 | |
|
9392 | 0 | switch (atmfield) { |
9393 | | |
9394 | 0 | case A_VPI: |
9395 | 0 | assert_maxval(cstate, "VPI", jvalue, UINT8_MAX); |
9396 | 0 | return gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, |
9397 | 0 | 0xffffffffU, jtype, reverse, jvalue); |
9398 | | |
9399 | 0 | case A_VCI: |
9400 | 0 | assert_maxval(cstate, "VCI", jvalue, UINT16_MAX); |
9401 | 0 | return gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, |
9402 | 0 | 0xffffffffU, jtype, reverse, jvalue); |
9403 | | |
9404 | 0 | default: |
9405 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "atmfield", atmfield); |
9406 | 0 | } |
9407 | 0 | } |
9408 | | |
9409 | | static struct block * |
9410 | | gen_atm_vpi(compiler_state_t *cstate, const uint8_t v) |
9411 | 0 | { |
9412 | 0 | return gen_atmfield_code_internal(cstate, A_VPI, v, BPF_JEQ, 0); |
9413 | 0 | } |
9414 | | |
9415 | | static struct block * |
9416 | | gen_atm_vci(compiler_state_t *cstate, const uint16_t v) |
9417 | 0 | { |
9418 | 0 | return gen_atmfield_code_internal(cstate, A_VCI, v, BPF_JEQ, 0); |
9419 | 0 | } |
9420 | | |
9421 | | static struct block * |
9422 | | gen_atm_prototype(compiler_state_t *cstate, const uint8_t v) |
9423 | 0 | { |
9424 | 0 | return gen_mcmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, v, 0x0fU); |
9425 | 0 | } |
9426 | | |
9427 | | static struct block * |
9428 | | gen_atmtype_llc(compiler_state_t *cstate) |
9429 | 0 | { |
9430 | 0 | struct block *b0; |
9431 | |
|
9432 | 0 | b0 = gen_atm_prototype(cstate, PT_LLC); |
9433 | 0 | cstate->linktype = cstate->prevlinktype; |
9434 | 0 | return b0; |
9435 | 0 | } |
9436 | | |
9437 | | struct block * |
9438 | | gen_atmfield_code(compiler_state_t *cstate, int atmfield, |
9439 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9440 | 0 | { |
9441 | | /* |
9442 | | * Catch errors reported by us and routines below us, and return NULL |
9443 | | * on an error. |
9444 | | */ |
9445 | 0 | if (setjmp(cstate->top_ctx)) |
9446 | 0 | return (NULL); |
9447 | | |
9448 | 0 | return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype, |
9449 | 0 | reverse); |
9450 | 0 | } |
9451 | | |
9452 | | struct block * |
9453 | | gen_atmtype_abbrev(compiler_state_t *cstate, int type) |
9454 | 0 | { |
9455 | 0 | struct block *b0, *b1; |
9456 | | |
9457 | | /* |
9458 | | * Catch errors reported by us and routines below us, and return NULL |
9459 | | * on an error. |
9460 | | */ |
9461 | 0 | if (setjmp(cstate->top_ctx)) |
9462 | 0 | return (NULL); |
9463 | | |
9464 | 0 | assert_atm(cstate, atmkw(type)); |
9465 | |
|
9466 | 0 | switch (type) { |
9467 | | |
9468 | 0 | case A_METAC: |
9469 | | /* Get all packets in Meta signalling Circuit */ |
9470 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9471 | 0 | b1 = gen_atm_vci(cstate, 1); |
9472 | 0 | return gen_and(b0, b1); |
9473 | | |
9474 | 0 | case A_BCC: |
9475 | | /* Get all packets in Broadcast Circuit*/ |
9476 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9477 | 0 | b1 = gen_atm_vci(cstate, 2); |
9478 | 0 | return gen_and(b0, b1); |
9479 | | |
9480 | 0 | case A_OAMF4SC: |
9481 | | /* Get all cells in Segment OAM F4 circuit*/ |
9482 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9483 | 0 | b1 = gen_atm_vci(cstate, 3); |
9484 | 0 | return gen_and(b0, b1); |
9485 | | |
9486 | 0 | case A_OAMF4EC: |
9487 | | /* Get all cells in End-to-End OAM F4 Circuit*/ |
9488 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9489 | 0 | b1 = gen_atm_vci(cstate, 4); |
9490 | 0 | return gen_and(b0, b1); |
9491 | | |
9492 | 0 | case A_SC: |
9493 | | /* Get all packets in connection Signalling Circuit */ |
9494 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9495 | 0 | b1 = gen_atm_vci(cstate, 5); |
9496 | 0 | return gen_and(b0, b1); |
9497 | | |
9498 | 0 | case A_ILMIC: |
9499 | | /* Get all packets in ILMI Circuit */ |
9500 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9501 | 0 | b1 = gen_atm_vci(cstate, 16); |
9502 | 0 | return gen_and(b0, b1); |
9503 | | |
9504 | 0 | case A_LANE: |
9505 | | /* Get all LANE packets */ |
9506 | 0 | b1 = gen_atm_prototype(cstate, PT_LANE); |
9507 | | |
9508 | | /* |
9509 | | * Arrange that all subsequent tests assume LANE |
9510 | | * rather than LLC-encapsulated packets, and set |
9511 | | * the offsets appropriately for LANE-encapsulated |
9512 | | * Ethernet. |
9513 | | * |
9514 | | * We assume LANE means Ethernet, not Token Ring. |
9515 | | */ |
9516 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 0, |
9517 | 0 | cstate->off_payload + 2, /* Ethernet header */ |
9518 | 0 | -1); |
9519 | 0 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
9520 | 0 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */ |
9521 | 0 | cstate->off_nl = 0; /* Ethernet II */ |
9522 | 0 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
9523 | 0 | return b1; |
9524 | | |
9525 | 0 | default: |
9526 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "type", type); |
9527 | 0 | } |
9528 | 0 | } |
9529 | | |
9530 | | /* |
9531 | | * Filtering for MTP2 messages based on li value |
9532 | | * FISU, length is null |
9533 | | * LSSU, length is 1 or 2 |
9534 | | * MSU, length is 3 or more |
9535 | | * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits |
9536 | | */ |
9537 | | struct block * |
9538 | | gen_mtp2type_abbrev(compiler_state_t *cstate, int type) |
9539 | 0 | { |
9540 | 0 | struct block *b0, *b1; |
9541 | | |
9542 | | /* |
9543 | | * Catch errors reported by us and routines below us, and return NULL |
9544 | | * on an error. |
9545 | | */ |
9546 | 0 | if (setjmp(cstate->top_ctx)) |
9547 | 0 | return (NULL); |
9548 | | |
9549 | 0 | assert_ss7(cstate, ss7kw(type)); |
9550 | |
|
9551 | 0 | switch (type) { |
9552 | | |
9553 | 0 | case M_FISU: |
9554 | 0 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9555 | 0 | 0x3fU, BPF_JEQ, 0, 0U); |
9556 | | |
9557 | 0 | case M_LSSU: |
9558 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9559 | 0 | 0x3fU, BPF_JGT, 1, 2U); |
9560 | 0 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9561 | 0 | 0x3fU, BPF_JGT, 0, 0U); |
9562 | 0 | return gen_and(b1, b0); |
9563 | | |
9564 | 0 | case M_MSU: |
9565 | 0 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9566 | 0 | 0x3fU, BPF_JGT, 0, 2U); |
9567 | | |
9568 | 0 | case MH_FISU: |
9569 | 0 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9570 | 0 | 0xff80U, BPF_JEQ, 0, 0U); |
9571 | | |
9572 | 0 | case MH_LSSU: |
9573 | 0 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9574 | 0 | 0xff80U, BPF_JGT, 1, 0x0100U); |
9575 | 0 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9576 | 0 | 0xff80U, BPF_JGT, 0, 0U); |
9577 | 0 | return gen_and(b1, b0); |
9578 | | |
9579 | 0 | case MH_MSU: |
9580 | 0 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9581 | 0 | 0xff80U, BPF_JGT, 0, 0x0100U); |
9582 | | |
9583 | 0 | default: |
9584 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "type", type); |
9585 | 0 | } |
9586 | 0 | } |
9587 | | |
9588 | | /* |
9589 | | * These maximum valid values are all-ones, so they double as the bitmasks |
9590 | | * before any bitwise shifting. |
9591 | | */ |
9592 | 0 | #define MTP2_SIO_MAXVAL UINT8_MAX |
9593 | 0 | #define MTP3_PC_MAXVAL 0x3fffU |
9594 | 0 | #define MTP3_SLS_MAXVAL 0xfU |
9595 | | |
9596 | | static struct block * |
9597 | | gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field, |
9598 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9599 | 0 | { |
9600 | 0 | u_int newoff_sio; |
9601 | 0 | u_int newoff_opc; |
9602 | 0 | u_int newoff_dpc; |
9603 | 0 | u_int newoff_sls; |
9604 | |
|
9605 | 0 | newoff_sio = cstate->off_sio; |
9606 | 0 | newoff_opc = cstate->off_opc; |
9607 | 0 | newoff_dpc = cstate->off_dpc; |
9608 | 0 | newoff_sls = cstate->off_sls; |
9609 | |
|
9610 | 0 | assert_ss7(cstate, ss7kw(mtp3field)); |
9611 | |
|
9612 | 0 | switch (mtp3field) { |
9613 | | |
9614 | | /* |
9615 | | * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703. |
9616 | | * |
9617 | | * SIO is the simplest field: the size is one byte and the offset is a |
9618 | | * multiple of bytes, so the only detail to get right is the value of |
9619 | | * the [right-to-left] field offset. |
9620 | | */ |
9621 | 0 | case MH_SIO: |
9622 | 0 | newoff_sio += 3; /* offset for MTP2_HSL */ |
9623 | | /* FALLTHROUGH */ |
9624 | |
|
9625 | 0 | case M_SIO: |
9626 | 0 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP2_SIO_MAXVAL); |
9627 | | // Here the bitmask means "do not apply a bitmask". |
9628 | 0 | return gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, UINT32_MAX, |
9629 | 0 | jtype, reverse, jvalue); |
9630 | | |
9631 | | /* |
9632 | | * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704. |
9633 | | * |
9634 | | * SLS, OPC and DPC are more complicated: none of these is sized in a |
9635 | | * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet |
9636 | | * diagrams are meant to be read right-to-left. This means in the |
9637 | | * diagrams within individual fields and concatenations thereof |
9638 | | * bitwise shifts and masks can be noted in the common left-to-right |
9639 | | * manner until each final value is ready to be byte-swapped and |
9640 | | * handed to gen_ncmp(). See also gen_dnhostop(), which solves a |
9641 | | * similar problem in a similar way. |
9642 | | * |
9643 | | * Offsets of fields within the packet header always have the |
9644 | | * right-to-left meaning. Note that in DLT_MTP2 and possibly other |
9645 | | * DLTs the offset does not include the F (Flag) field at the |
9646 | | * beginning of each message. |
9647 | | * |
9648 | | * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the |
9649 | | * 32-bit standard routing header has a 4 byte [RTL] offset and could |
9650 | | * be tested entirely using a single BPF_W comparison. In this case |
9651 | | * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC |
9652 | | * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field |
9653 | | * [LTR] bitmask would be (0xF << 28), all of which conveniently |
9654 | | * correlates with the [RTL] packet diagram until the byte-swapping is |
9655 | | * done before use. |
9656 | | * |
9657 | | * The code below uses this approach for OPC, which spans 3 bytes. |
9658 | | * DPC and SLS use shorter loads, SLS also uses a different offset. |
9659 | | */ |
9660 | 0 | case MH_OPC: |
9661 | 0 | newoff_opc += 3; |
9662 | | |
9663 | | /* FALLTHROUGH */ |
9664 | 0 | case M_OPC: |
9665 | 0 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_PC_MAXVAL); |
9666 | 0 | return gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, |
9667 | 0 | SWAPLONG(MTP3_PC_MAXVAL << 14), jtype, reverse, |
9668 | 0 | SWAPLONG(jvalue << 14)); |
9669 | | |
9670 | 0 | case MH_DPC: |
9671 | 0 | newoff_dpc += 3; |
9672 | | /* FALLTHROUGH */ |
9673 | |
|
9674 | 0 | case M_DPC: |
9675 | 0 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_PC_MAXVAL); |
9676 | 0 | return gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_H, |
9677 | 0 | SWAPSHORT(MTP3_PC_MAXVAL), jtype, reverse, |
9678 | 0 | SWAPSHORT(jvalue)); |
9679 | | |
9680 | 0 | case MH_SLS: |
9681 | 0 | newoff_sls += 3; |
9682 | | /* FALLTHROUGH */ |
9683 | |
|
9684 | 0 | case M_SLS: |
9685 | 0 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_SLS_MAXVAL); |
9686 | 0 | return gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, |
9687 | 0 | MTP3_SLS_MAXVAL << 4, jtype, reverse, |
9688 | 0 | jvalue << 4); |
9689 | | |
9690 | 0 | default: |
9691 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "mtp3field", mtp3field); |
9692 | 0 | } |
9693 | 0 | } |
9694 | | |
9695 | | struct block * |
9696 | | gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, |
9697 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9698 | 0 | { |
9699 | | /* |
9700 | | * Catch errors reported by us and routines below us, and return NULL |
9701 | | * on an error. |
9702 | | */ |
9703 | 0 | if (setjmp(cstate->top_ctx)) |
9704 | 0 | return (NULL); |
9705 | | |
9706 | 0 | return gen_mtp3field_code_internal(cstate, mtp3field, jvalue, jtype, |
9707 | 0 | reverse); |
9708 | 0 | } |
9709 | | |
9710 | | static struct block * |
9711 | | gen_msg_abbrev(compiler_state_t *cstate, const uint8_t type) |
9712 | 0 | { |
9713 | | /* |
9714 | | * Q.2931 signalling protocol messages for handling virtual circuits |
9715 | | * establishment and teardown |
9716 | | */ |
9717 | 0 | return gen_cmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, |
9718 | 0 | BPF_B, type); |
9719 | 0 | } |
9720 | | |
9721 | | struct block * |
9722 | | gen_atmmulti_abbrev(compiler_state_t *cstate, int type) |
9723 | 0 | { |
9724 | 0 | struct block *b0, *b1; |
9725 | | |
9726 | | /* |
9727 | | * Catch errors reported by us and routines below us, and return NULL |
9728 | | * on an error. |
9729 | | */ |
9730 | 0 | if (setjmp(cstate->top_ctx)) |
9731 | 0 | return (NULL); |
9732 | | |
9733 | 0 | assert_atm(cstate, atmkw(type)); |
9734 | |
|
9735 | 0 | switch (type) { |
9736 | | |
9737 | 0 | case A_OAM: |
9738 | | /* OAM F4 type */ |
9739 | 0 | b0 = gen_atm_vci(cstate, 3); |
9740 | 0 | b1 = gen_atm_vci(cstate, 4); |
9741 | 0 | b1 = gen_or(b0, b1); |
9742 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9743 | 0 | return gen_and(b0, b1); |
9744 | | |
9745 | 0 | case A_OAMF4: |
9746 | | /* OAM F4 type */ |
9747 | 0 | b0 = gen_atm_vci(cstate, 3); |
9748 | 0 | b1 = gen_atm_vci(cstate, 4); |
9749 | 0 | b1 = gen_or(b0, b1); |
9750 | 0 | b0 = gen_atm_vpi(cstate, 0); |
9751 | 0 | return gen_and(b0, b1); |
9752 | | |
9753 | 0 | case A_CONNECTMSG: |
9754 | | /* |
9755 | | * Get Q.2931 signalling messages for switched |
9756 | | * virtual connection |
9757 | | */ |
9758 | 0 | b0 = gen_msg_abbrev(cstate, SETUP); |
9759 | 0 | b1 = gen_msg_abbrev(cstate, CALL_PROCEED); |
9760 | 0 | b1 = gen_or(b0, b1); |
9761 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT); |
9762 | 0 | b1 = gen_or(b0, b1); |
9763 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT_ACK); |
9764 | 0 | b1 = gen_or(b0, b1); |
9765 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE); |
9766 | 0 | b1 = gen_or(b0, b1); |
9767 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE_DONE); |
9768 | 0 | b1 = gen_or(b0, b1); |
9769 | 0 | b0 = gen_atmtype_abbrev(cstate, A_SC); |
9770 | 0 | return gen_and(b0, b1); |
9771 | | |
9772 | 0 | case A_METACONNECT: |
9773 | 0 | b0 = gen_msg_abbrev(cstate, SETUP); |
9774 | 0 | b1 = gen_msg_abbrev(cstate, CALL_PROCEED); |
9775 | 0 | b1 = gen_or(b0, b1); |
9776 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT); |
9777 | 0 | b1 = gen_or(b0, b1); |
9778 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE); |
9779 | 0 | b1 = gen_or(b0, b1); |
9780 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE_DONE); |
9781 | 0 | b1 = gen_or(b0, b1); |
9782 | 0 | b0 = gen_atmtype_abbrev(cstate, A_METAC); |
9783 | 0 | return gen_and(b0, b1); |
9784 | | |
9785 | 0 | default: |
9786 | 0 | bpf_error(cstate, ERRSTR_FUNC_VAR_INT, __func__, "type", type); |
9787 | 0 | } |
9788 | 0 | } |