Line | Count | Source (jump to first uncovered line) |
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 | | |
41 | | #include "extract.h" |
42 | | |
43 | | #include "ethertype.h" |
44 | | #include "llc.h" |
45 | | #include "gencode.h" |
46 | | #include "ieee80211.h" |
47 | | #include "pflog.h" |
48 | | #include "ppp.h" |
49 | | #include "pcap/sll.h" |
50 | | #include "pcap/ipnet.h" |
51 | | #include "diag-control.h" |
52 | | #include "pcap-util.h" |
53 | | |
54 | | #include "scanner.h" |
55 | | |
56 | | #if defined(__linux__) |
57 | | #include <linux/types.h> |
58 | | #include <linux/if_packet.h> |
59 | | #include <linux/filter.h> |
60 | | #endif |
61 | | |
62 | | #ifdef _WIN32 |
63 | | #ifdef HAVE_NPCAP_BPF_H |
64 | | /* Defines BPF extensions for Npcap */ |
65 | | #include <npcap-bpf.h> |
66 | | #endif |
67 | | #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) |
68 | | /* IPv6 address */ |
69 | | struct in6_addr |
70 | | { |
71 | | union |
72 | | { |
73 | | uint8_t u6_addr8[16]; |
74 | | uint16_t u6_addr16[8]; |
75 | | uint32_t u6_addr32[4]; |
76 | | } in6_u; |
77 | | #define s6_addr in6_u.u6_addr8 |
78 | | #define s6_addr16 in6_u.u6_addr16 |
79 | | #define s6_addr32 in6_u.u6_addr32 |
80 | | #define s6_addr64 in6_u.u6_addr64 |
81 | | }; |
82 | | |
83 | | typedef unsigned short sa_family_t; |
84 | | |
85 | | #define __SOCKADDR_COMMON(sa_prefix) \ |
86 | | sa_family_t sa_prefix##family |
87 | | |
88 | | /* Ditto, for IPv6. */ |
89 | | struct sockaddr_in6 |
90 | | { |
91 | | __SOCKADDR_COMMON (sin6_); |
92 | | uint16_t sin6_port; /* Transport layer port # */ |
93 | | uint32_t sin6_flowinfo; /* IPv6 flow information */ |
94 | | struct in6_addr sin6_addr; /* IPv6 address */ |
95 | | }; |
96 | | |
97 | | #ifndef EAI_ADDRFAMILY |
98 | | struct addrinfo { |
99 | | int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ |
100 | | int ai_family; /* PF_xxx */ |
101 | | int ai_socktype; /* SOCK_xxx */ |
102 | | int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ |
103 | | size_t ai_addrlen; /* length of ai_addr */ |
104 | | char *ai_canonname; /* canonical name for hostname */ |
105 | | struct sockaddr *ai_addr; /* binary address */ |
106 | | struct addrinfo *ai_next; /* next structure in linked list */ |
107 | | }; |
108 | | #endif /* EAI_ADDRFAMILY */ |
109 | | #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */ |
110 | | #else /* _WIN32 */ |
111 | | #include <netdb.h> /* for "struct addrinfo" */ |
112 | | #endif /* _WIN32 */ |
113 | | #include <pcap/namedb.h> |
114 | | |
115 | | #include "nametoaddr.h" |
116 | | |
117 | 26.6k | #define ETHERMTU 1500 |
118 | | |
119 | | #ifndef IPPROTO_HOPOPTS |
120 | | #define IPPROTO_HOPOPTS 0 |
121 | | #endif |
122 | | #ifndef IPPROTO_ROUTING |
123 | | #define IPPROTO_ROUTING 43 |
124 | | #endif |
125 | | #ifndef IPPROTO_FRAGMENT |
126 | | #define IPPROTO_FRAGMENT 44 |
127 | | #endif |
128 | | #ifndef IPPROTO_DSTOPTS |
129 | | #define IPPROTO_DSTOPTS 60 |
130 | | #endif |
131 | | #ifndef IPPROTO_SCTP |
132 | | #define IPPROTO_SCTP 132 |
133 | | #endif |
134 | | |
135 | 0 | #define GENEVE_PORT 6081 |
136 | 1.38k | #define VXLAN_PORT 4789 |
137 | | |
138 | | |
139 | | /* |
140 | | * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp |
141 | | */ |
142 | | |
143 | | /* RFC 1051 */ |
144 | 617 | #define ARCTYPE_IP_OLD 240 /* IP protocol */ |
145 | 255 | #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */ |
146 | | |
147 | | /* RFC 1201 */ |
148 | 617 | #define ARCTYPE_IP 212 /* IP protocol */ |
149 | 255 | #define ARCTYPE_ARP 213 /* address resolution protocol */ |
150 | 226 | #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */ |
151 | | |
152 | 35 | #define ARCTYPE_ATALK 221 /* Appletalk */ |
153 | | #define ARCTYPE_BANIAN 247 /* Banyan Vines */ |
154 | | #define ARCTYPE_IPX 250 /* Novell IPX */ |
155 | | |
156 | 307 | #define ARCTYPE_INET6 0xc4 /* IPng */ |
157 | | #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */ |
158 | | |
159 | | |
160 | | /* Based on UNI3.1 standard by ATM Forum */ |
161 | | |
162 | | /* ATM traffic types based on VPI=0 and (the following VCI */ |
163 | | #define VCI_PPC 0x05 /* Point-to-point signal msg */ |
164 | | #define VCI_BCC 0x02 /* Broadcast signal msg */ |
165 | | #define VCI_OAMF4SC 0x03 /* Segment OAM F4 flow cell */ |
166 | | #define VCI_OAMF4EC 0x04 /* End-to-end OAM F4 flow cell */ |
167 | | #define VCI_METAC 0x01 /* Meta signal msg */ |
168 | | #define VCI_ILMIC 0x10 /* ILMI msg */ |
169 | | |
170 | | /* Q.2931 signalling messages */ |
171 | 0 | #define CALL_PROCEED 0x02 /* call proceeding */ |
172 | 0 | #define CONNECT 0x07 /* connect */ |
173 | 0 | #define CONNECT_ACK 0x0f /* connect_ack */ |
174 | 0 | #define SETUP 0x05 /* setup */ |
175 | 0 | #define RELEASE 0x4d /* release */ |
176 | 0 | #define RELEASE_DONE 0x5a /* release_done */ |
177 | | #define RESTART 0x46 /* restart */ |
178 | | #define RESTART_ACK 0x4e /* restart ack */ |
179 | | #define STATUS 0x7d /* status */ |
180 | | #define STATUS_ENQ 0x75 /* status ack */ |
181 | | #define ADD_PARTY 0x80 /* add party */ |
182 | | #define ADD_PARTY_ACK 0x81 /* add party ack */ |
183 | | #define ADD_PARTY_REJ 0x82 /* add party rej */ |
184 | | #define DROP_PARTY 0x83 /* drop party */ |
185 | | #define DROP_PARTY_ACK 0x84 /* drop party ack */ |
186 | | |
187 | | /* Information Element Parameters in the signalling messages */ |
188 | | #define CAUSE 0x08 /* cause */ |
189 | | #define ENDPT_REF 0x54 /* endpoint reference */ |
190 | | #define AAL_PARA 0x58 /* ATM adaptation layer parameters */ |
191 | | #define TRAFF_DESCRIP 0x59 /* atm traffic descriptors */ |
192 | | #define CONNECT_ID 0x5a /* connection identifier */ |
193 | | #define QOS_PARA 0x5c /* quality of service parameters */ |
194 | | #define B_HIGHER 0x5d /* broadband higher layer information */ |
195 | | #define B_BEARER 0x5e /* broadband bearer capability */ |
196 | | #define B_LOWER 0x5f /* broadband lower information */ |
197 | | #define CALLING_PARTY 0x6c /* calling party number */ |
198 | | #define CALLED_PARTY 0x70 /* called party number */ |
199 | | |
200 | | #define Q2931 0x09 |
201 | | |
202 | | /* Q.2931 signalling general messages format */ |
203 | 227 | #define PROTO_POS 0 /* offset of protocol discriminator */ |
204 | | #define CALL_REF_POS 2 /* offset of call reference value */ |
205 | 0 | #define MSG_TYPE_POS 5 /* offset of message type */ |
206 | | #define MSG_LEN_POS 7 /* offset of message length */ |
207 | | #define IE_BEGIN_POS 9 /* offset of first information element */ |
208 | | |
209 | | /* format of signalling messages */ |
210 | | #define TYPE_POS 0 |
211 | | #define LEN_POS 2 |
212 | | #define FIELD_BEGIN_POS 4 |
213 | | |
214 | | |
215 | | /* SunATM header for ATM packet */ |
216 | | #define SUNATM_DIR_POS 0 |
217 | 227 | #define SUNATM_VPI_POS 1 |
218 | 227 | #define SUNATM_VCI_POS 2 |
219 | 2.19k | #define SUNATM_PKT_BEGIN_POS 4 /* Start of ATM packet */ |
220 | | |
221 | | /* Protocol type values in the bottom for bits of the byte at SUNATM_DIR_POS. */ |
222 | 61 | #define PT_LANE 0x01 /* LANE */ |
223 | 793 | #define PT_LLC 0x02 /* LLC encapsulation */ |
224 | | #define PT_ILMI 0x05 /* ILMI */ |
225 | | #define PT_QSAAL 0x06 /* Q.SAAL */ |
226 | | |
227 | | |
228 | | /* Types missing from some systems */ |
229 | | |
230 | | /* |
231 | | * Network layer protocol identifiers |
232 | | */ |
233 | | #ifndef ISO8473_CLNP |
234 | 64 | #define ISO8473_CLNP 0x81 |
235 | | #endif |
236 | | #ifndef ISO9542_ESIS |
237 | 77 | #define ISO9542_ESIS 0x82 |
238 | | #endif |
239 | | #ifndef ISO9542X25_ESIS |
240 | | #define ISO9542X25_ESIS 0x8a |
241 | | #endif |
242 | | #ifndef ISO10589_ISIS |
243 | 12.9k | #define ISO10589_ISIS 0x83 |
244 | | #endif |
245 | | |
246 | 1.37k | #define ISIS_L1_LAN_IIH 15 |
247 | 1.17k | #define ISIS_L2_LAN_IIH 16 |
248 | 2.44k | #define ISIS_PTP_IIH 17 |
249 | 1.33k | #define ISIS_L1_LSP 18 |
250 | 1.12k | #define ISIS_L2_LSP 20 |
251 | 1.42k | #define ISIS_L1_CSNP 24 |
252 | 1.21k | #define ISIS_L2_CSNP 25 |
253 | 1.44k | #define ISIS_L1_PSNP 26 |
254 | 1.24k | #define ISIS_L2_PSNP 27 |
255 | | /* |
256 | | * The maximum possible value can also be used as a bit mask because the |
257 | | * "PDU Type" field comprises the least significant 5 bits of a particular |
258 | | * octet, see sections 9.5~9.13 of ISO/IEC 10589:2002(E). |
259 | | */ |
260 | 25.5k | #define ISIS_PDU_TYPE_MAX 0x1FU |
261 | | |
262 | | #ifndef ISO8878A_CONS |
263 | | #define ISO8878A_CONS 0x84 |
264 | | #endif |
265 | | #ifndef ISO10747_IDRP |
266 | | #define ISO10747_IDRP 0x85 |
267 | | #endif |
268 | | |
269 | | // Same as in tcpdump/print-sl.c. |
270 | 0 | #define SLIPDIR_IN 0 |
271 | 0 | #define SLIPDIR_OUT 1 |
272 | | |
273 | | #ifdef HAVE_OS_PROTO_H |
274 | | #include "os-proto.h" |
275 | | #endif |
276 | | |
277 | 264k | #define JMP(c) ((c)|BPF_JMP|BPF_K) |
278 | | |
279 | | /* |
280 | | * "Push" the current value of the link-layer header type and link-layer |
281 | | * header offset onto a "stack", and set a new value. (It's not a |
282 | | * full-blown stack; we keep only the top two items.) |
283 | | */ |
284 | 931 | #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \ |
285 | 931 | { \ |
286 | 931 | (cs)->prevlinktype = (cs)->linktype; \ |
287 | 931 | (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \ |
288 | 931 | (cs)->linktype = (new_linktype); \ |
289 | 931 | (cs)->off_linkhdr.is_variable = (new_is_variable); \ |
290 | 931 | (cs)->off_linkhdr.constant_part = (new_constant_part); \ |
291 | 931 | (cs)->off_linkhdr.reg = (new_reg); \ |
292 | 931 | (cs)->is_encap = 0; \ |
293 | 931 | } |
294 | | |
295 | | /* |
296 | | * Offset "not set" value. |
297 | | */ |
298 | 120k | #define OFFSET_NOT_SET 0xffffffffU |
299 | | |
300 | | /* |
301 | | * Absolute offsets, which are offsets from the beginning of the raw |
302 | | * packet data, are, in the general case, the sum of a variable value |
303 | | * and a constant value; the variable value may be absent, in which |
304 | | * case the offset is only the constant value, and the constant value |
305 | | * may be zero, in which case the offset is only the variable value. |
306 | | * |
307 | | * bpf_abs_offset is a structure containing all that information: |
308 | | * |
309 | | * is_variable is 1 if there's a variable part. |
310 | | * |
311 | | * constant_part is the constant part of the value, possibly zero; |
312 | | * |
313 | | * if is_variable is 1, reg is the register number for a register |
314 | | * containing the variable value if the register has been assigned, |
315 | | * and -1 otherwise. |
316 | | */ |
317 | | typedef struct { |
318 | | int is_variable; |
319 | | u_int constant_part; |
320 | | int reg; |
321 | | } bpf_abs_offset; |
322 | | |
323 | | /* |
324 | | * Value passed to gen_load_a() to indicate what the offset argument |
325 | | * is relative to the beginning of. |
326 | | */ |
327 | | enum e_offrel { |
328 | | OR_PACKET, /* full packet data */ |
329 | | OR_LINKHDR, /* link-layer header */ |
330 | | OR_PREVLINKHDR, /* previous link-layer header */ |
331 | | OR_LLC, /* 802.2 LLC header */ |
332 | | OR_PREVMPLSHDR, /* previous MPLS header */ |
333 | | OR_LINKTYPE, /* link-layer type */ |
334 | | OR_LINKPL, /* link-layer payload */ |
335 | | OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */ |
336 | | OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */ |
337 | | OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */ |
338 | | }; |
339 | | |
340 | | /* |
341 | | * We divvy out chunks of memory rather than call malloc each time so |
342 | | * we don't have to worry about leaking memory. It's probably |
343 | | * not a big deal if all this memory was wasted but if this ever |
344 | | * goes into a library that would probably not be a good idea. |
345 | | * |
346 | | * XXX - this *is* in a library.... |
347 | | */ |
348 | 360k | #define NCHUNKS 16 |
349 | 18.6k | #define CHUNK0SIZE 1024 |
350 | | struct chunk { |
351 | | size_t n_left; |
352 | | void *m; |
353 | | }; |
354 | | |
355 | | /* |
356 | | * A chunk can store any of: |
357 | | * - a string (guaranteed alignment 1 but present for completeness) |
358 | | * - a block |
359 | | * - an slist |
360 | | * - an arth |
361 | | * For this simple allocator every allocated chunk gets rounded up to the |
362 | | * alignment needed for any chunk. |
363 | | */ |
364 | | struct chunk_align { |
365 | | char dummy; |
366 | | union { |
367 | | char c; |
368 | | struct block b; |
369 | | struct slist s; |
370 | | struct arth a; |
371 | | } u; |
372 | | }; |
373 | 2.02M | #define CHUNK_ALIGN (offsetof(struct chunk_align, u)) |
374 | | |
375 | | /* Code generator state */ |
376 | | |
377 | | struct _compiler_state { |
378 | | jmp_buf top_ctx; |
379 | | pcap_t *bpf_pcap; |
380 | | int error_set; |
381 | | |
382 | | struct icode ic; |
383 | | |
384 | | int snaplen; |
385 | | |
386 | | int linktype; |
387 | | int prevlinktype; |
388 | | int outermostlinktype; |
389 | | |
390 | | bpf_u_int32 netmask; |
391 | | int no_optimize; |
392 | | |
393 | | /* Hack for handling VLAN and MPLS stacks. */ |
394 | | u_int label_stack_depth; |
395 | | u_int vlan_stack_depth; |
396 | | |
397 | | /* XXX */ |
398 | | u_int pcap_fddipad; |
399 | | |
400 | | /* |
401 | | * As errors are handled by a longjmp, anything allocated must |
402 | | * be freed in the longjmp handler, so it must be reachable |
403 | | * from that handler. |
404 | | * |
405 | | * One thing that's allocated is the result of pcap_nametoaddrinfo(); |
406 | | * it must be freed with freeaddrinfo(). This variable points to |
407 | | * any addrinfo structure that would need to be freed. |
408 | | */ |
409 | | struct addrinfo *ai; |
410 | | |
411 | | /* |
412 | | * Various code constructs need to know the layout of the packet. |
413 | | * These values give the necessary offsets from the beginning |
414 | | * of the packet data. |
415 | | */ |
416 | | |
417 | | /* |
418 | | * Absolute offset of the beginning of the link-layer header. |
419 | | */ |
420 | | bpf_abs_offset off_linkhdr; |
421 | | |
422 | | /* |
423 | | * If we're checking a link-layer header for a packet encapsulated |
424 | | * in another protocol layer, this is the equivalent information |
425 | | * for the previous layers' link-layer header from the beginning |
426 | | * of the raw packet data. |
427 | | */ |
428 | | bpf_abs_offset off_prevlinkhdr; |
429 | | |
430 | | /* |
431 | | * This is the equivalent information for the outermost layers' |
432 | | * link-layer header. |
433 | | */ |
434 | | bpf_abs_offset off_outermostlinkhdr; |
435 | | |
436 | | /* |
437 | | * Absolute offset of the beginning of the link-layer payload. |
438 | | */ |
439 | | bpf_abs_offset off_linkpl; |
440 | | |
441 | | /* |
442 | | * "off_linktype" is the offset to information in the link-layer |
443 | | * header giving the packet type. This is an absolute offset |
444 | | * from the beginning of the packet. |
445 | | * |
446 | | * For Ethernet, it's the offset of the Ethernet type field; this |
447 | | * means that it must have a value that skips VLAN tags. |
448 | | * |
449 | | * For link-layer types that always use 802.2 headers, it's the |
450 | | * offset of the LLC header; this means that it must have a value |
451 | | * that skips VLAN tags. |
452 | | * |
453 | | * For PPP, it's the offset of the PPP type field. |
454 | | * |
455 | | * For Cisco HDLC, it's the offset of the CHDLC type field. |
456 | | * |
457 | | * For BSD loopback, it's the offset of the AF_ value. |
458 | | * |
459 | | * For Linux cooked sockets, it's the offset of the type field. |
460 | | * |
461 | | * off_linktype.constant_part is set to OFFSET_NOT_SET for no |
462 | | * encapsulation, in which case, IP is assumed. |
463 | | */ |
464 | | bpf_abs_offset off_linktype; |
465 | | |
466 | | /* |
467 | | * TRUE if the link layer includes an ATM pseudo-header. |
468 | | */ |
469 | | int is_atm; |
470 | | |
471 | | /* TRUE if "geneve" or "vxlan" appeared in the filter; it |
472 | | * causes us to generate code that checks for a Geneve or |
473 | | * VXLAN header respectively and assume that later filters |
474 | | * apply to the encapsulated payload. |
475 | | */ |
476 | | int is_encap; |
477 | | |
478 | | /* |
479 | | * TRUE if we need variable length part of VLAN offset |
480 | | */ |
481 | | int is_vlan_vloffset; |
482 | | |
483 | | /* |
484 | | * These are offsets for the ATM pseudo-header. |
485 | | */ |
486 | | u_int off_vpi; |
487 | | u_int off_vci; |
488 | | u_int off_proto; |
489 | | |
490 | | /* |
491 | | * These are offsets for the MTP2 fields. |
492 | | */ |
493 | | u_int off_li; |
494 | | u_int off_li_hsl; |
495 | | |
496 | | /* |
497 | | * These are offsets for the MTP3 fields. |
498 | | */ |
499 | | u_int off_sio; |
500 | | u_int off_opc; |
501 | | u_int off_dpc; |
502 | | u_int off_sls; |
503 | | |
504 | | /* |
505 | | * This is the offset of the first byte after the ATM pseudo_header, |
506 | | * or -1 if there is no ATM pseudo-header. |
507 | | */ |
508 | | u_int off_payload; |
509 | | |
510 | | /* |
511 | | * These are offsets to the beginning of the network-layer header. |
512 | | * They are relative to the beginning of the link-layer payload |
513 | | * (i.e., they don't include off_linkhdr.constant_part or |
514 | | * off_linkpl.constant_part). |
515 | | * |
516 | | * If the link layer never uses 802.2 LLC: |
517 | | * |
518 | | * "off_nl" and "off_nl_nosnap" are the same. |
519 | | * |
520 | | * If the link layer always uses 802.2 LLC: |
521 | | * |
522 | | * "off_nl" is the offset if there's a SNAP header following |
523 | | * the 802.2 header; |
524 | | * |
525 | | * "off_nl_nosnap" is the offset if there's no SNAP header. |
526 | | * |
527 | | * If the link layer is Ethernet: |
528 | | * |
529 | | * "off_nl" is the offset if the packet is an Ethernet II packet |
530 | | * (we assume no 802.3+802.2+SNAP); |
531 | | * |
532 | | * "off_nl_nosnap" is the offset if the packet is an 802.3 packet |
533 | | * with an 802.2 header following it. |
534 | | */ |
535 | | u_int off_nl; |
536 | | u_int off_nl_nosnap; |
537 | | |
538 | | /* |
539 | | * Here we handle simple allocation of the scratch registers. |
540 | | * If too many registers are alloc'd, the allocator punts. |
541 | | */ |
542 | | int regused[BPF_MEMWORDS]; |
543 | | int curreg; |
544 | | |
545 | | /* |
546 | | * Memory chunks. |
547 | | */ |
548 | | struct chunk chunks[NCHUNKS]; |
549 | | int cur_chunk; |
550 | | }; |
551 | | |
552 | | /* |
553 | | * For use by routines outside this file. |
554 | | */ |
555 | | /* VARARGS */ |
556 | | void |
557 | | bpf_set_error(compiler_state_t *cstate, const char *fmt, ...) |
558 | 1.76k | { |
559 | 1.76k | va_list ap; |
560 | | |
561 | | /* |
562 | | * If we've already set an error, don't override it. |
563 | | * The lexical analyzer reports some errors by setting |
564 | | * the error and then returning a LEX_ERROR token, which |
565 | | * is not recognized by any grammar rule, and thus forces |
566 | | * the parse to stop. We don't want the error reported |
567 | | * by the lexical analyzer to be overwritten by the syntax |
568 | | * error. |
569 | | */ |
570 | 1.76k | if (!cstate->error_set) { |
571 | 1.73k | va_start(ap, fmt); |
572 | 1.73k | (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
573 | 1.73k | fmt, ap); |
574 | 1.73k | va_end(ap); |
575 | 1.73k | cstate->error_set = 1; |
576 | 1.73k | } |
577 | 1.76k | } |
578 | | |
579 | | /* |
580 | | * For use *ONLY* in routines in this file. |
581 | | */ |
582 | | static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...) |
583 | | PCAP_PRINTFLIKE(2, 3); |
584 | | |
585 | | /* VARARGS */ |
586 | | static void PCAP_NORETURN |
587 | | bpf_error(compiler_state_t *cstate, const char *fmt, ...) |
588 | 2.00k | { |
589 | 2.00k | va_list ap; |
590 | | |
591 | 2.00k | va_start(ap, fmt); |
592 | 2.00k | (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, |
593 | 2.00k | fmt, ap); |
594 | 2.00k | va_end(ap); |
595 | 2.00k | longjmp(cstate->top_ctx, 1); |
596 | | /*NOTREACHED*/ |
597 | | #ifdef _AIX |
598 | | PCAP_UNREACHABLE |
599 | | #endif /* _AIX */ |
600 | 2.00k | } |
601 | | |
602 | | static int init_linktype(compiler_state_t *, pcap_t *); |
603 | | |
604 | | static void init_regs(compiler_state_t *); |
605 | | static int alloc_reg(compiler_state_t *); |
606 | | static void free_reg(compiler_state_t *, int); |
607 | | |
608 | | static void initchunks(compiler_state_t *cstate); |
609 | | static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t); |
610 | | static void *newchunk(compiler_state_t *cstate, size_t); |
611 | | static void freechunks(compiler_state_t *cstate); |
612 | | static inline struct block *new_block(compiler_state_t *cstate, int); |
613 | | static inline struct slist *new_stmt(compiler_state_t *cstate, int); |
614 | | static struct block *gen_retblk(compiler_state_t *cstate, int); |
615 | | static inline void syntax(compiler_state_t *cstate); |
616 | | |
617 | | static void backpatch(struct block *, struct block *); |
618 | | static void merge(struct block *, struct block *); |
619 | | static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int, |
620 | | u_int, bpf_u_int32); |
621 | | static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int, |
622 | | u_int, bpf_u_int32); |
623 | | static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int, |
624 | | u_int, bpf_u_int32); |
625 | | static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int, |
626 | | u_int, bpf_u_int32); |
627 | | static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int, |
628 | | u_int, bpf_u_int32); |
629 | | static struct block *gen_cmp_ne(compiler_state_t *, enum e_offrel, u_int, |
630 | | u_int size, bpf_u_int32); |
631 | | static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int, |
632 | | u_int, bpf_u_int32, bpf_u_int32); |
633 | | static struct block *gen_mcmp_ne(compiler_state_t *, enum e_offrel, u_int, |
634 | | u_int, bpf_u_int32, bpf_u_int32); |
635 | | static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int, |
636 | | u_int, const u_char *); |
637 | | static struct block *gen_jmp(compiler_state_t *, int, bpf_u_int32, |
638 | | struct slist *); |
639 | | static struct block *gen_set(compiler_state_t *, bpf_u_int32, struct slist *); |
640 | | static struct block *gen_unset(compiler_state_t *, bpf_u_int32, struct slist *); |
641 | | static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int, |
642 | | u_int, bpf_u_int32, int, int, bpf_u_int32); |
643 | | static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *, |
644 | | u_int, u_int); |
645 | | static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int, |
646 | | u_int); |
647 | | static struct slist *gen_loadx_iphdrlen(compiler_state_t *); |
648 | | static struct block *gen_uncond(compiler_state_t *, int); |
649 | | static inline struct block *gen_true(compiler_state_t *); |
650 | | static inline struct block *gen_false(compiler_state_t *); |
651 | | static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32); |
652 | | static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32); |
653 | | static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32); |
654 | | static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *); |
655 | | static struct slist *gen_load_prism_llprefixlen(compiler_state_t *); |
656 | | static struct slist *gen_load_avs_llprefixlen(compiler_state_t *); |
657 | | static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *); |
658 | | static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *); |
659 | | static void insert_compute_vloffsets(compiler_state_t *, struct block *); |
660 | | static struct slist *gen_abs_offset_varpart(compiler_state_t *, |
661 | | bpf_abs_offset *); |
662 | | static uint16_t ethertype_to_ppptype(compiler_state_t *, bpf_u_int32); |
663 | | static struct block *gen_linktype(compiler_state_t *, bpf_u_int32); |
664 | | static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32); |
665 | | static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32); |
666 | | static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
667 | | int, u_int, u_int); |
668 | | static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *, |
669 | | struct in6_addr *, int, u_int, u_int); |
670 | | static struct block *gen_ahostop(compiler_state_t *, const uint8_t, int); |
671 | | static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int); |
672 | | static unsigned char is_mac48_linktype(const int); |
673 | | static struct block *gen_mac48host(compiler_state_t *, const u_char *, |
674 | | const u_char, const char *); |
675 | | static struct block *gen_mac48host_byname(compiler_state_t *, const char *, |
676 | | const u_char, const char *); |
677 | | static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int); |
678 | | static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32); |
679 | | static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32, |
680 | | int, int, int); |
681 | | static struct block *gen_host6(compiler_state_t *, struct in6_addr *, |
682 | | struct in6_addr *, int, int, int); |
683 | | static struct block *gen_gateway(compiler_state_t *, const char *, |
684 | | struct addrinfo *, int); |
685 | | static struct block *gen_ip_proto(compiler_state_t *, const uint8_t); |
686 | | static struct block *gen_ip6_proto(compiler_state_t *, const uint8_t); |
687 | | static struct block *gen_ipfrag(compiler_state_t *); |
688 | | static struct block *gen_portatom(compiler_state_t *, int, uint16_t); |
689 | | static struct block *gen_portrangeatom(compiler_state_t *, u_int, uint16_t, |
690 | | uint16_t); |
691 | | static struct block *gen_portatom6(compiler_state_t *, int, uint16_t); |
692 | | static struct block *gen_portrangeatom6(compiler_state_t *, u_int, uint16_t, |
693 | | uint16_t); |
694 | | static struct block *gen_port(compiler_state_t *, uint16_t, int, int); |
695 | | static struct block *gen_port_common(compiler_state_t *, int, struct block *); |
696 | | static struct block *gen_portrange(compiler_state_t *, uint16_t, uint16_t, |
697 | | int, int); |
698 | | static struct block *gen_port6(compiler_state_t *, uint16_t, int, int); |
699 | | static struct block *gen_port6_common(compiler_state_t *, int, struct block *); |
700 | | static struct block *gen_portrange6(compiler_state_t *, uint16_t, uint16_t, |
701 | | int, int); |
702 | | static int lookup_proto(compiler_state_t *, const char *, const struct qual); |
703 | | #if !defined(NO_PROTOCHAIN) |
704 | | static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int); |
705 | | #endif /* !defined(NO_PROTOCHAIN) */ |
706 | | static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int); |
707 | | static struct slist *xfer_to_x(compiler_state_t *, struct arth *); |
708 | | static struct slist *xfer_to_a(compiler_state_t *, struct arth *); |
709 | | static struct block *gen_mac_multicast(compiler_state_t *, int); |
710 | | static struct block *gen_len(compiler_state_t *, int, int); |
711 | | static struct block *gen_encap_ll_check(compiler_state_t *cstate); |
712 | | |
713 | | static struct block *gen_atmfield_code_internal(compiler_state_t *, int, |
714 | | bpf_u_int32, int, int); |
715 | | static struct block *gen_atmtype_llc(compiler_state_t *); |
716 | | static struct block *gen_msg_abbrev(compiler_state_t *, const uint8_t); |
717 | | static struct block *gen_atm_prototype(compiler_state_t *, const uint8_t); |
718 | | static struct block *gen_atm_vpi(compiler_state_t *, const uint8_t); |
719 | | static struct block *gen_atm_vci(compiler_state_t *, const uint16_t); |
720 | | |
721 | | static void |
722 | | initchunks(compiler_state_t *cstate) |
723 | 10.0k | { |
724 | 10.0k | int i; |
725 | | |
726 | 171k | for (i = 0; i < NCHUNKS; i++) { |
727 | 160k | cstate->chunks[i].n_left = 0; |
728 | 160k | cstate->chunks[i].m = NULL; |
729 | 160k | } |
730 | 10.0k | cstate->cur_chunk = 0; |
731 | 10.0k | } |
732 | | |
733 | | static void * |
734 | | newchunk_nolongjmp(compiler_state_t *cstate, size_t n) |
735 | 1.01M | { |
736 | 1.01M | struct chunk *cp; |
737 | 1.01M | int k; |
738 | 1.01M | size_t size; |
739 | | |
740 | | /* Round up to chunk alignment. */ |
741 | 1.01M | n = (n + CHUNK_ALIGN - 1) & ~(CHUNK_ALIGN - 1); |
742 | | |
743 | 1.01M | cp = &cstate->chunks[cstate->cur_chunk]; |
744 | 1.01M | if (n > cp->n_left) { |
745 | 18.6k | ++cp; |
746 | 18.6k | k = ++cstate->cur_chunk; |
747 | 18.6k | if (k >= NCHUNKS) { |
748 | 0 | bpf_set_error(cstate, "out of memory"); |
749 | 0 | return (NULL); |
750 | 0 | } |
751 | 18.6k | size = CHUNK0SIZE << k; |
752 | 18.6k | cp->m = (void *)malloc(size); |
753 | 18.6k | if (cp->m == NULL) { |
754 | 0 | bpf_set_error(cstate, "out of memory"); |
755 | 0 | return (NULL); |
756 | 0 | } |
757 | 18.6k | memset((char *)cp->m, 0, size); |
758 | 18.6k | cp->n_left = size; |
759 | 18.6k | if (n > size) { |
760 | 0 | bpf_set_error(cstate, "out of memory"); |
761 | 0 | return (NULL); |
762 | 0 | } |
763 | 18.6k | } |
764 | 1.01M | cp->n_left -= n; |
765 | 1.01M | return (void *)((char *)cp->m + cp->n_left); |
766 | 1.01M | } |
767 | | |
768 | | static void * |
769 | | newchunk(compiler_state_t *cstate, size_t n) |
770 | 1.00M | { |
771 | 1.00M | void *p; |
772 | | |
773 | 1.00M | p = newchunk_nolongjmp(cstate, n); |
774 | 1.00M | if (p == NULL) { |
775 | 0 | longjmp(cstate->top_ctx, 1); |
776 | | /*NOTREACHED*/ |
777 | 0 | } |
778 | 1.00M | return (p); |
779 | 1.00M | } |
780 | | |
781 | | static void |
782 | | freechunks(compiler_state_t *cstate) |
783 | 10.0k | { |
784 | 10.0k | int i; |
785 | | |
786 | 171k | for (i = 0; i < NCHUNKS; ++i) |
787 | 160k | if (cstate->chunks[i].m != NULL) |
788 | 18.6k | free(cstate->chunks[i].m); |
789 | 10.0k | } |
790 | | |
791 | | /* |
792 | | * A strdup whose allocations are freed after code generation is over. |
793 | | * This is used by the lexical analyzer, so it can't longjmp; it just |
794 | | * returns NULL on an allocation error, and the callers must check |
795 | | * for it. |
796 | | */ |
797 | | char * |
798 | | sdup(compiler_state_t *cstate, const char *s) |
799 | 4.52k | { |
800 | 4.52k | size_t n = strlen(s) + 1; |
801 | 4.52k | char *cp = newchunk_nolongjmp(cstate, n); |
802 | | |
803 | 4.52k | if (cp == NULL) |
804 | 0 | return (NULL); |
805 | 4.52k | pcapint_strlcpy(cp, s, n); |
806 | 4.52k | return (cp); |
807 | 4.52k | } |
808 | | |
809 | | static inline struct block * |
810 | | new_block(compiler_state_t *cstate, int code) |
811 | 276k | { |
812 | 276k | struct block *p; |
813 | | |
814 | 276k | p = (struct block *)newchunk(cstate, sizeof(*p)); |
815 | 276k | p->s.code = code; |
816 | 276k | p->head = p; |
817 | | |
818 | 276k | return p; |
819 | 276k | } |
820 | | |
821 | | static inline struct slist * |
822 | | new_stmt(compiler_state_t *cstate, int code) |
823 | 703k | { |
824 | 703k | struct slist *p; |
825 | | |
826 | 703k | p = (struct slist *)newchunk(cstate, sizeof(*p)); |
827 | 703k | p->s.code = code; |
828 | | |
829 | 703k | return p; |
830 | 703k | } |
831 | | |
832 | | static struct block * |
833 | | gen_retblk_internal(compiler_state_t *cstate, int v) |
834 | 11.9k | { |
835 | 11.9k | struct block *b = new_block(cstate, BPF_RET|BPF_K); |
836 | | |
837 | 11.9k | b->s.k = v; |
838 | 11.9k | return b; |
839 | 11.9k | } |
840 | | |
841 | | static struct block * |
842 | | gen_retblk(compiler_state_t *cstate, int v) |
843 | 1.64k | { |
844 | 1.64k | if (setjmp(cstate->top_ctx)) { |
845 | | /* |
846 | | * gen_retblk() only fails because a memory |
847 | | * allocation failed in newchunk(), meaning |
848 | | * that it can't return a pointer. |
849 | | * |
850 | | * Return NULL. |
851 | | */ |
852 | 0 | return NULL; |
853 | 0 | } |
854 | 1.64k | return gen_retblk_internal(cstate, v); |
855 | 1.64k | } |
856 | | |
857 | | static inline PCAP_NORETURN_DEF void |
858 | | syntax(compiler_state_t *cstate) |
859 | 143 | { |
860 | 143 | bpf_error(cstate, "syntax error in filter expression"); |
861 | 143 | } |
862 | | |
863 | | /* |
864 | | * For the given integer return a string with the keyword (or the nominal |
865 | | * keyword if there is more than one). This is a simpler version of tok2str() |
866 | | * in tcpdump because in this problem space a valid integer value is not |
867 | | * greater than 71. |
868 | | */ |
869 | | static const char * |
870 | | qual2kw(const char *kind, const unsigned id, const char *tokens[], |
871 | | const size_t size) |
872 | 2.86k | { |
873 | 2.86k | static char buf[4][64]; |
874 | 2.86k | static int idx = 0; |
875 | | |
876 | 2.86k | if (id < size && tokens[id]) |
877 | 2.86k | return tokens[id]; |
878 | | |
879 | 0 | char *ret = buf[idx]; |
880 | 0 | idx = (idx + 1) % (sizeof(buf) / sizeof(buf[0])); |
881 | 0 | ret[0] = '\0'; // just in case |
882 | 0 | snprintf(ret, sizeof(buf[0]), "<invalid %s %u>", kind, id); |
883 | 0 | return ret; |
884 | 2.86k | } |
885 | | |
886 | | // protocol qualifier keywords |
887 | | static const char * |
888 | | pqkw(const unsigned id) |
889 | 279 | { |
890 | 279 | const char * tokens[] = { |
891 | 279 | [Q_LINK] = "link", |
892 | 279 | [Q_IP] = "ip", |
893 | 279 | [Q_ARP] = "arp", |
894 | 279 | [Q_RARP] = "rarp", |
895 | 279 | [Q_SCTP] = "sctp", |
896 | 279 | [Q_TCP] = "tcp", |
897 | 279 | [Q_UDP] = "udp", |
898 | 279 | [Q_ICMP] = "icmp", |
899 | 279 | [Q_IGMP] = "igmp", |
900 | 279 | [Q_IGRP] = "igrp", |
901 | 279 | [Q_ATALK] = "atalk", |
902 | 279 | [Q_DECNET] = "decnet", |
903 | 279 | [Q_LAT] = "lat", |
904 | 279 | [Q_SCA] = "sca", |
905 | 279 | [Q_MOPRC] = "moprc", |
906 | 279 | [Q_MOPDL] = "mopdl", |
907 | 279 | [Q_IPV6] = "ip6", |
908 | 279 | [Q_ICMPV6] = "icmp6", |
909 | 279 | [Q_AH] = "ah", |
910 | 279 | [Q_ESP] = "esp", |
911 | 279 | [Q_PIM] = "pim", |
912 | 279 | [Q_VRRP] = "vrrp", |
913 | 279 | [Q_AARP] = "aarp", |
914 | 279 | [Q_ISO] = "iso", |
915 | 279 | [Q_ESIS] = "esis", |
916 | 279 | [Q_ISIS] = "isis", |
917 | 279 | [Q_CLNP] = "clnp", |
918 | 279 | [Q_STP] = "stp", |
919 | 279 | [Q_IPX] = "ipx", |
920 | 279 | [Q_NETBEUI] = "netbeui", |
921 | 279 | [Q_ISIS_L1] = "l1", |
922 | 279 | [Q_ISIS_L2] = "l2", |
923 | 279 | [Q_ISIS_IIH] = "iih", |
924 | 279 | [Q_ISIS_SNP] = "snp", |
925 | 279 | [Q_ISIS_CSNP] = "csnp", |
926 | 279 | [Q_ISIS_PSNP] = "psnp", |
927 | 279 | [Q_ISIS_LSP] = "lsp", |
928 | 279 | [Q_RADIO] = "radio", |
929 | 279 | [Q_CARP] = "carp", |
930 | 279 | }; |
931 | 279 | return qual2kw("proto", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
932 | 279 | } |
933 | | |
934 | | // direction qualifier keywords |
935 | | static const char * |
936 | | dqkw(const unsigned id) |
937 | 59 | { |
938 | 59 | const char * tokens[] = { |
939 | 59 | [Q_SRC] = "src", |
940 | 59 | [Q_DST] = "dst", |
941 | 59 | [Q_OR] = "src or dst", |
942 | 59 | [Q_AND] = "src and dst", |
943 | 59 | [Q_ADDR1] = "addr1", |
944 | 59 | [Q_ADDR2] = "addr2", |
945 | 59 | [Q_ADDR3] = "addr3", |
946 | 59 | [Q_ADDR4] = "addr4", |
947 | 59 | [Q_RA] = "ra", |
948 | 59 | [Q_TA] = "ta", |
949 | 59 | }; |
950 | 59 | return qual2kw("dir", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
951 | 59 | } |
952 | | |
953 | | // type (in the man page) / address (in the code) qualifier keywords |
954 | | static const char * |
955 | | tqkw(const unsigned id) |
956 | 424 | { |
957 | 424 | const char * tokens[] = { |
958 | 424 | [Q_HOST] = "host", |
959 | 424 | [Q_NET] = "net", |
960 | 424 | [Q_PORT] = "port", |
961 | 424 | [Q_GATEWAY] = "gateway", |
962 | 424 | [Q_PROTO] = "proto", |
963 | 424 | [Q_PROTOCHAIN] = "protochain", |
964 | 424 | [Q_PORTRANGE] = "portrange", |
965 | 424 | }; |
966 | 424 | return qual2kw("type", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
967 | 424 | } |
968 | | |
969 | | // ATM keywords |
970 | | static const char * |
971 | | atmkw(const unsigned id) |
972 | 677 | { |
973 | 677 | const char * tokens[] = { |
974 | 677 | [A_METAC] = "metac", |
975 | 677 | [A_BCC] = "bcc", |
976 | 677 | [A_OAMF4SC] = "oamf4sc", |
977 | 677 | [A_OAMF4EC] = "oamf4ec", |
978 | 677 | [A_SC] = "sc", |
979 | 677 | [A_ILMIC] = "ilmic", |
980 | 677 | [A_OAM] = "oam", |
981 | 677 | [A_OAMF4] = "oamf4", |
982 | 677 | [A_LANE] = "lane", |
983 | 677 | [A_VPI] = "vpi", |
984 | 677 | [A_VCI] = "vci", |
985 | 677 | [A_CONNECTMSG] = "connectmsg", |
986 | 677 | [A_METACONNECT] = "metaconnect", |
987 | 677 | }; |
988 | 677 | return qual2kw("ATM keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
989 | 677 | } |
990 | | |
991 | | // SS7 keywords |
992 | | static const char * |
993 | | ss7kw(const unsigned id) |
994 | 1.42k | { |
995 | 1.42k | const char * tokens[] = { |
996 | 1.42k | [M_FISU] = "fisu", |
997 | 1.42k | [M_LSSU] = "lssu", |
998 | 1.42k | [M_MSU] = "msu", |
999 | 1.42k | [MH_FISU] = "hfisu", |
1000 | 1.42k | [MH_LSSU] = "hlssu", |
1001 | 1.42k | [MH_MSU] = "hmsu", |
1002 | 1.42k | [M_SIO] = "sio", |
1003 | 1.42k | [M_OPC] = "opc", |
1004 | 1.42k | [M_DPC] = "dpc", |
1005 | 1.42k | [M_SLS] = "sls", |
1006 | 1.42k | [MH_SIO] = "hsio", |
1007 | 1.42k | [MH_OPC] = "hopc", |
1008 | 1.42k | [MH_DPC] = "hdpc", |
1009 | 1.42k | [MH_SLS] = "hsls", |
1010 | 1.42k | }; |
1011 | 1.42k | return qual2kw("MTP keyword", id, tokens, sizeof(tokens) / sizeof(tokens[0])); |
1012 | 1.42k | } |
1013 | | |
1014 | | static PCAP_NORETURN_DEF void |
1015 | | fail_kw_on_dlt(compiler_state_t *cstate, const char *keyword) |
1016 | 19 | { |
1017 | 19 | bpf_error(cstate, "'%s' not supported on %s", keyword, |
1018 | 19 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
1019 | 19 | } |
1020 | | |
1021 | | static void |
1022 | | assert_pflog(compiler_state_t *cstate, const char *kw) |
1023 | 109 | { |
1024 | 109 | if (cstate->linktype != DLT_PFLOG) |
1025 | 38 | bpf_error(cstate, "'%s' supported only on PFLOG linktype", kw); |
1026 | 109 | } |
1027 | | |
1028 | | static void |
1029 | | assert_atm(compiler_state_t *cstate, const char *kw) |
1030 | 677 | { |
1031 | | /* |
1032 | | * Belt and braces: init_linktype() sets either all of these struct |
1033 | | * members (for DLT_SUNATM) or none (otherwise). |
1034 | | */ |
1035 | 677 | if (cstate->linktype != DLT_SUNATM || |
1036 | 677 | ! cstate->is_atm || |
1037 | 677 | cstate->off_vpi == OFFSET_NOT_SET || |
1038 | 677 | cstate->off_vci == OFFSET_NOT_SET || |
1039 | 677 | cstate->off_proto == OFFSET_NOT_SET || |
1040 | 677 | cstate->off_payload == OFFSET_NOT_SET) |
1041 | 25 | bpf_error(cstate, "'%s' supported only on SUNATM", kw); |
1042 | 677 | } |
1043 | | |
1044 | | static void |
1045 | | assert_ss7(compiler_state_t *cstate, const char *kw) |
1046 | 834 | { |
1047 | 834 | switch (cstate->linktype) { |
1048 | 247 | case DLT_MTP2: |
1049 | 453 | case DLT_ERF: |
1050 | 828 | case DLT_MTP2_WITH_PHDR: |
1051 | | // Belt and braces, same as in assert_atm(). |
1052 | 828 | if (cstate->off_sio != OFFSET_NOT_SET && |
1053 | 828 | cstate->off_opc != OFFSET_NOT_SET && |
1054 | 828 | cstate->off_dpc != OFFSET_NOT_SET && |
1055 | 828 | cstate->off_sls != OFFSET_NOT_SET) |
1056 | 828 | return; |
1057 | 834 | } |
1058 | 6 | bpf_error(cstate, "'%s' supported only on SS7", kw); |
1059 | 834 | } |
1060 | | |
1061 | | static void |
1062 | | assert_maxval(compiler_state_t *cstate, const char *name, |
1063 | | const bpf_u_int32 val, const bpf_u_int32 maxval) |
1064 | 60.5k | { |
1065 | 60.5k | if (val > maxval) |
1066 | 581 | bpf_error(cstate, "%s %u greater than maximum %u", |
1067 | 581 | name, val, maxval); |
1068 | 60.5k | } |
1069 | | |
1070 | 53 | #define ERRSTR_802_11_ONLY_KW "'%s' is valid for 802.11 syntax only" |
1071 | 112 | #define ERRSTR_INVALID_QUAL "'%s' is not a valid qualifier for '%s'" |
1072 | 32 | #define ERRSTR_UNKNOWN_MAC48HOST "unknown Ethernet-like host '%s'" |
1073 | | |
1074 | | // Validate a port/portrange proto qualifier and map to an IP protocol number. |
1075 | | static int |
1076 | | port_pq_to_ipproto(compiler_state_t *cstate, const int proto, const char *kw) |
1077 | 5.16k | { |
1078 | 5.16k | switch (proto) { |
1079 | 91 | case Q_UDP: |
1080 | 91 | return IPPROTO_UDP; |
1081 | 138 | case Q_TCP: |
1082 | 138 | return IPPROTO_TCP; |
1083 | 80 | case Q_SCTP: |
1084 | 80 | return IPPROTO_SCTP; |
1085 | 4.85k | case Q_DEFAULT: |
1086 | 4.85k | return PROTO_UNDEF; |
1087 | 5.16k | } |
1088 | 1 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), kw); |
1089 | 5.16k | } |
1090 | | |
1091 | | int |
1092 | | pcap_compile(pcap_t *p, struct bpf_program *program, |
1093 | | const char *buf, int optimize, bpf_u_int32 mask) |
1094 | 10.0k | { |
1095 | | #ifdef _WIN32 |
1096 | | int err; |
1097 | | WSADATA wsaData; |
1098 | | #endif |
1099 | 10.0k | compiler_state_t cstate; |
1100 | 10.0k | yyscan_t scanner = NULL; |
1101 | 10.0k | YY_BUFFER_STATE in_buffer = NULL; |
1102 | 10.0k | u_int len; |
1103 | 10.0k | int rc; |
1104 | | |
1105 | | /* |
1106 | | * If this pcap_t hasn't been activated, it doesn't have a |
1107 | | * link-layer type, so we can't use it. |
1108 | | */ |
1109 | 10.0k | if (!p->activated) { |
1110 | 0 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1111 | 0 | "not-yet-activated pcap_t passed to pcap_compile"); |
1112 | 0 | return (PCAP_ERROR); |
1113 | 0 | } |
1114 | | |
1115 | | #ifdef _WIN32 |
1116 | | /* |
1117 | | * Initialize Winsock, asking for the latest version (2.2), |
1118 | | * as we may be calling Winsock routines to translate |
1119 | | * host names to addresses. |
1120 | | */ |
1121 | | err = WSAStartup(MAKEWORD(2, 2), &wsaData); |
1122 | | if (err != 0) { |
1123 | | pcapint_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE, |
1124 | | err, "Error calling WSAStartup()"); |
1125 | | return (PCAP_ERROR); |
1126 | | } |
1127 | | #endif |
1128 | | |
1129 | | #ifdef ENABLE_REMOTE |
1130 | | /* |
1131 | | * If the device on which we're capturing need to be notified |
1132 | | * that a new filter is being compiled, do so. |
1133 | | * |
1134 | | * This allows them to save a copy of it, in case, for example, |
1135 | | * they're implementing a form of remote packet capture, and |
1136 | | * want the remote machine to filter out the packets in which |
1137 | | * it's sending the packets it's captured. |
1138 | | * |
1139 | | * XXX - the fact that we happen to be compiling a filter |
1140 | | * doesn't necessarily mean we'll be installing it as the |
1141 | | * filter for this pcap_t; we might be running it from userland |
1142 | | * on captured packets to do packet classification. We really |
1143 | | * need a better way of handling this, but this is all that |
1144 | | * the WinPcap remote capture code did. |
1145 | | */ |
1146 | | if (p->save_current_filter_op != NULL) |
1147 | | (p->save_current_filter_op)(p, buf); |
1148 | | #endif |
1149 | | |
1150 | 10.0k | initchunks(&cstate); |
1151 | 10.0k | cstate.no_optimize = 0; |
1152 | 10.0k | cstate.ai = NULL; |
1153 | 10.0k | cstate.ic.root = NULL; |
1154 | 10.0k | cstate.ic.cur_mark = 0; |
1155 | 10.0k | cstate.bpf_pcap = p; |
1156 | 10.0k | cstate.error_set = 0; |
1157 | 10.0k | init_regs(&cstate); |
1158 | | |
1159 | 10.0k | cstate.netmask = mask; |
1160 | | |
1161 | 10.0k | cstate.snaplen = pcap_snapshot(p); |
1162 | 10.0k | if (cstate.snaplen == 0) { |
1163 | 0 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1164 | 0 | "snaplen of 0 rejects all packets"); |
1165 | 0 | rc = PCAP_ERROR; |
1166 | 0 | goto quit; |
1167 | 0 | } |
1168 | | |
1169 | 10.0k | if (pcap_lex_init(&scanner) != 0) { |
1170 | 0 | pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
1171 | 0 | errno, "can't initialize scanner"); |
1172 | 0 | rc = PCAP_ERROR; |
1173 | 0 | goto quit; |
1174 | 0 | } |
1175 | 10.0k | in_buffer = pcap__scan_string(buf ? buf : "", scanner); |
1176 | | |
1177 | | /* |
1178 | | * Associate the compiler state with the lexical analyzer |
1179 | | * state. |
1180 | | */ |
1181 | 10.0k | pcap_set_extra(&cstate, scanner); |
1182 | | |
1183 | 10.0k | if (init_linktype(&cstate, p) == -1) { |
1184 | 35 | rc = PCAP_ERROR; |
1185 | 35 | goto quit; |
1186 | 35 | } |
1187 | 10.0k | if (pcap_parse(scanner, &cstate) != 0) { |
1188 | 3.70k | if (cstate.ai != NULL) |
1189 | 11 | freeaddrinfo(cstate.ai); |
1190 | 3.70k | rc = PCAP_ERROR; |
1191 | 3.70k | goto quit; |
1192 | 3.70k | } |
1193 | | |
1194 | 6.32k | if (cstate.ic.root == NULL) { |
1195 | 1.64k | cstate.ic.root = gen_retblk(&cstate, cstate.snaplen); |
1196 | | |
1197 | | /* |
1198 | | * Catch errors reported by gen_retblk(). |
1199 | | */ |
1200 | 1.64k | if (cstate.ic.root== NULL) { |
1201 | 0 | rc = PCAP_ERROR; |
1202 | 0 | goto quit; |
1203 | 0 | } |
1204 | 1.64k | } |
1205 | | |
1206 | 6.32k | if (optimize && !cstate.no_optimize) { |
1207 | 5.67k | if (bpf_optimize(&cstate.ic, p->errbuf) == -1) { |
1208 | | /* Failure */ |
1209 | 51 | rc = PCAP_ERROR; |
1210 | 51 | goto quit; |
1211 | 51 | } |
1212 | 5.62k | if (cstate.ic.root == NULL || |
1213 | 5.62k | (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) { |
1214 | 627 | (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
1215 | 627 | "expression rejects all packets"); |
1216 | 627 | rc = PCAP_ERROR; |
1217 | 627 | goto quit; |
1218 | 627 | } |
1219 | 5.62k | } |
1220 | 5.64k | program->bf_insns = icode_to_fcode(&cstate.ic, |
1221 | 5.64k | cstate.ic.root, &len, p->errbuf); |
1222 | 5.64k | if (program->bf_insns == NULL) { |
1223 | | /* Failure */ |
1224 | 0 | rc = PCAP_ERROR; |
1225 | 0 | goto quit; |
1226 | 0 | } |
1227 | 5.64k | program->bf_len = len; |
1228 | | |
1229 | 5.64k | rc = 0; /* We're all okay */ |
1230 | | |
1231 | 10.0k | quit: |
1232 | | /* |
1233 | | * Clean up everything for the lexical analyzer. |
1234 | | */ |
1235 | 10.0k | if (in_buffer != NULL) |
1236 | 10.0k | pcap__delete_buffer(in_buffer, scanner); |
1237 | 10.0k | if (scanner != NULL) |
1238 | 10.0k | pcap_lex_destroy(scanner); |
1239 | | |
1240 | | /* |
1241 | | * Clean up our own allocated memory. |
1242 | | */ |
1243 | 10.0k | freechunks(&cstate); |
1244 | | |
1245 | | #ifdef _WIN32 |
1246 | | WSACleanup(); |
1247 | | #endif |
1248 | | |
1249 | 10.0k | return (rc); |
1250 | 5.64k | } |
1251 | | |
1252 | | /* |
1253 | | * entry point for using the compiler with no pcap open |
1254 | | * pass in all the stuff that is needed explicitly instead. |
1255 | | */ |
1256 | | int |
1257 | | pcap_compile_nopcap(int snaplen_arg, int linktype_arg, |
1258 | | struct bpf_program *program, |
1259 | | const char *buf, int optimize, bpf_u_int32 mask) |
1260 | 0 | { |
1261 | 0 | pcap_t *p; |
1262 | 0 | int ret; |
1263 | |
|
1264 | 0 | p = pcap_open_dead(linktype_arg, snaplen_arg); |
1265 | 0 | if (p == NULL) |
1266 | 0 | return (PCAP_ERROR); |
1267 | 0 | ret = pcap_compile(p, program, buf, optimize, mask); |
1268 | 0 | pcap_close(p); |
1269 | 0 | return (ret); |
1270 | 0 | } |
1271 | | |
1272 | | /* |
1273 | | * Clean up a "struct bpf_program" by freeing all the memory allocated |
1274 | | * in it. |
1275 | | */ |
1276 | | void |
1277 | | pcap_freecode(struct bpf_program *program) |
1278 | 15.7k | { |
1279 | 15.7k | program->bf_len = 0; |
1280 | 15.7k | if (program->bf_insns != NULL) { |
1281 | 5.64k | free((char *)program->bf_insns); |
1282 | 5.64k | program->bf_insns = NULL; |
1283 | 5.64k | } |
1284 | 15.7k | } |
1285 | | |
1286 | | /* |
1287 | | * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates |
1288 | | * which of the jt and jf fields has been resolved and which is a pointer |
1289 | | * back to another unresolved block (or nil). At least one of the fields |
1290 | | * in each block is already resolved. |
1291 | | */ |
1292 | | static void |
1293 | | backpatch(struct block *list, struct block *target) |
1294 | 267k | { |
1295 | 267k | struct block *next; |
1296 | | |
1297 | 770k | while (list) { |
1298 | 503k | if (!list->sense) { |
1299 | 251k | next = JT(list); |
1300 | 251k | JT(list) = target; |
1301 | 251k | } else { |
1302 | 251k | next = JF(list); |
1303 | 251k | JF(list) = target; |
1304 | 251k | } |
1305 | 503k | list = next; |
1306 | 503k | } |
1307 | 267k | } |
1308 | | |
1309 | | /* |
1310 | | * Merge the lists in b0 and b1, using the 'sense' field to indicate |
1311 | | * which of jt and jf is the link. |
1312 | | */ |
1313 | | static void |
1314 | | merge(struct block *b0, struct block *b1) |
1315 | 257k | { |
1316 | 257k | register struct block **p = &b0; |
1317 | | |
1318 | | /* Find end of list. */ |
1319 | 669k | while (*p) |
1320 | 412k | p = !((*p)->sense) ? &JT(*p) : &JF(*p); |
1321 | | |
1322 | | /* Concatenate the lists. */ |
1323 | 257k | *p = b1; |
1324 | 257k | } |
1325 | | |
1326 | | int |
1327 | | finish_parse(compiler_state_t *cstate, struct block *p) |
1328 | 5.17k | { |
1329 | | /* |
1330 | | * Catch errors reported by us and routines below us, and return -1 |
1331 | | * on an error. |
1332 | | */ |
1333 | 5.17k | if (setjmp(cstate->top_ctx)) |
1334 | 0 | return (-1); |
1335 | | |
1336 | | /* |
1337 | | * Insert before the statements of the first (root) block any |
1338 | | * statements needed to load the lengths of any variable-length |
1339 | | * headers into registers. |
1340 | | * |
1341 | | * XXX - a fancier strategy would be to insert those before the |
1342 | | * statements of all blocks that use those lengths and that |
1343 | | * have no predecessors that use them, so that we only compute |
1344 | | * the lengths if we need them. There might be even better |
1345 | | * approaches than that. |
1346 | | * |
1347 | | * However, those strategies would be more complicated, and |
1348 | | * as we don't generate code to compute a length if the |
1349 | | * program has no tests that use the length, and as most |
1350 | | * tests will probably use those lengths, we would just |
1351 | | * postpone computing the lengths so that it's not done |
1352 | | * for tests that fail early, and it's not clear that's |
1353 | | * worth the effort. |
1354 | | */ |
1355 | 5.17k | insert_compute_vloffsets(cstate, p->head); |
1356 | | |
1357 | | /* |
1358 | | * For DLT_PPI captures, generate a check of the per-packet |
1359 | | * DLT value to make sure it's DLT_IEEE802_11. |
1360 | | * |
1361 | | * XXX - TurboCap cards use DLT_PPI for Ethernet. |
1362 | | * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header |
1363 | | * with appropriate Ethernet information and use that rather |
1364 | | * than using something such as DLT_PPI where you don't know |
1365 | | * the link-layer header type until runtime, which, in the |
1366 | | * general case, would force us to generate both Ethernet *and* |
1367 | | * 802.11 code (*and* anything else for which PPI is used) |
1368 | | * and choose between them early in the BPF program? |
1369 | | */ |
1370 | 5.17k | if (cstate->linktype == DLT_PPI) { |
1371 | 183 | struct block *ppi_dlt_check = gen_cmp(cstate, OR_PACKET, |
1372 | 183 | 4, BPF_W, SWAPLONG(DLT_IEEE802_11)); |
1373 | 183 | gen_and(ppi_dlt_check, p); |
1374 | 183 | } |
1375 | | |
1376 | 5.17k | backpatch(p, gen_retblk_internal(cstate, cstate->snaplen)); |
1377 | 5.17k | p->sense = !p->sense; |
1378 | 5.17k | backpatch(p, gen_retblk_internal(cstate, 0)); |
1379 | 5.17k | cstate->ic.root = p->head; |
1380 | 5.17k | return (0); |
1381 | 5.17k | } |
1382 | | |
1383 | | void |
1384 | | gen_and(struct block *b0, struct block *b1) |
1385 | 141k | { |
1386 | 141k | backpatch(b0, b1->head); |
1387 | 141k | b0->sense = !b0->sense; |
1388 | 141k | b1->sense = !b1->sense; |
1389 | 141k | merge(b1, b0); |
1390 | 141k | b1->sense = !b1->sense; |
1391 | 141k | b1->head = b0->head; |
1392 | 141k | } |
1393 | | |
1394 | | void |
1395 | | gen_or(struct block *b0, struct block *b1) |
1396 | 115k | { |
1397 | 115k | b0->sense = !b0->sense; |
1398 | 115k | backpatch(b0, b1->head); |
1399 | 115k | b0->sense = !b0->sense; |
1400 | 115k | merge(b1, b0); |
1401 | 115k | b1->head = b0->head; |
1402 | 115k | } |
1403 | | |
1404 | | void |
1405 | | gen_not(struct block *b) |
1406 | 18.5k | { |
1407 | 18.5k | b->sense = !b->sense; |
1408 | 18.5k | } |
1409 | | |
1410 | | static struct block * |
1411 | | gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1412 | | u_int size, bpf_u_int32 v) |
1413 | 137k | { |
1414 | 137k | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v); |
1415 | 137k | } |
1416 | | |
1417 | | static struct block * |
1418 | | gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1419 | | u_int size, bpf_u_int32 v) |
1420 | 39 | { |
1421 | 39 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v); |
1422 | 39 | } |
1423 | | |
1424 | | static struct block * |
1425 | | gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1426 | | u_int size, bpf_u_int32 v) |
1427 | 0 | { |
1428 | 0 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v); |
1429 | 0 | } |
1430 | | |
1431 | | static struct block * |
1432 | | gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1433 | | u_int size, bpf_u_int32 v) |
1434 | 16 | { |
1435 | 16 | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v); |
1436 | 16 | } |
1437 | | |
1438 | | static struct block * |
1439 | | gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1440 | | u_int size, bpf_u_int32 v) |
1441 | 2.79k | { |
1442 | 2.79k | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v); |
1443 | 2.79k | } |
1444 | | |
1445 | | static struct block * |
1446 | | gen_cmp_ne(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1447 | | u_int size, bpf_u_int32 v) |
1448 | 2.15k | { |
1449 | 2.15k | return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 1, v); |
1450 | 2.15k | } |
1451 | | |
1452 | | static struct block * |
1453 | | gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1454 | | u_int size, bpf_u_int32 v, bpf_u_int32 mask) |
1455 | 95.0k | { |
1456 | | /* |
1457 | | * For any A: if mask == 0, it means A & mask == 0, so the result is |
1458 | | * true iff v == 0. In this case ideally the caller should have |
1459 | | * skipped this invocation and have fewer statement blocks to juggle. |
1460 | | * If the caller could have skipped, but has not, produce a block with |
1461 | | * fewer statements. |
1462 | | * |
1463 | | * This could be done in gen_ncmp() in a more generic way, but this |
1464 | | * function is the only code path that can have mask == 0. |
1465 | | */ |
1466 | 95.0k | if (mask == 0) |
1467 | 0 | return v ? gen_false(cstate) : gen_true(cstate); |
1468 | | |
1469 | 95.0k | return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v); |
1470 | 95.0k | } |
1471 | | |
1472 | | static struct block * |
1473 | | gen_mcmp_ne(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1474 | | u_int size, bpf_u_int32 v, bpf_u_int32 mask) |
1475 | 246 | { |
1476 | 246 | return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 1, v); |
1477 | 246 | } |
1478 | | |
1479 | | static struct block * |
1480 | | gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1481 | | u_int size, const u_char *v) |
1482 | 2.45k | { |
1483 | 2.45k | register struct block *b, *tmp; |
1484 | | |
1485 | 2.45k | b = NULL; |
1486 | 5.16k | while (size >= 4) { |
1487 | 2.71k | register const u_char *p = &v[size - 4]; |
1488 | | |
1489 | 2.71k | tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, |
1490 | 2.71k | EXTRACT_BE_U_4(p)); |
1491 | 2.71k | if (b != NULL) |
1492 | 296 | gen_and(b, tmp); |
1493 | 2.71k | b = tmp; |
1494 | 2.71k | size -= 4; |
1495 | 2.71k | } |
1496 | 4.59k | while (size >= 2) { |
1497 | 2.14k | register const u_char *p = &v[size - 2]; |
1498 | | |
1499 | 2.14k | tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, |
1500 | 2.14k | EXTRACT_BE_U_2(p)); |
1501 | 2.14k | if (b != NULL) |
1502 | 2.12k | gen_and(b, tmp); |
1503 | 2.14k | b = tmp; |
1504 | 2.14k | size -= 2; |
1505 | 2.14k | } |
1506 | 2.45k | if (size > 0) { |
1507 | 31 | tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]); |
1508 | 31 | if (b != NULL) |
1509 | 20 | gen_and(b, tmp); |
1510 | 31 | b = tmp; |
1511 | 31 | } |
1512 | 2.45k | return b; |
1513 | 2.45k | } |
1514 | | |
1515 | | static struct block * |
1516 | | gen_jmp(compiler_state_t *cstate, int jtype, bpf_u_int32 v, struct slist *stmts) |
1517 | 260k | { |
1518 | 260k | struct block *b = new_block(cstate, JMP(jtype)); |
1519 | 260k | b->s.k = v; |
1520 | 260k | b->stmts = stmts; |
1521 | 260k | return b; |
1522 | 260k | } |
1523 | | |
1524 | | static struct block * |
1525 | | gen_set(compiler_state_t *cstate, bpf_u_int32 v, struct slist *stmts) |
1526 | 9.58k | { |
1527 | 9.58k | return gen_jmp(cstate, BPF_JSET, v, stmts); |
1528 | 9.58k | } |
1529 | | |
1530 | | static struct block * |
1531 | | gen_unset(compiler_state_t *cstate, bpf_u_int32 v, struct slist *stmts) |
1532 | 8.49k | { |
1533 | 8.49k | struct block *b = gen_set(cstate, v, stmts); |
1534 | 8.49k | gen_not(b); |
1535 | 8.49k | return b; |
1536 | 8.49k | } |
1537 | | |
1538 | | /* |
1539 | | * AND the field of size "size" at offset "offset" relative to the header |
1540 | | * specified by "offrel" with "mask", and compare it with the value "v" |
1541 | | * with the test specified by "jtype"; if "reverse" is true, the test |
1542 | | * should test the opposite of "jtype". |
1543 | | */ |
1544 | | static struct block * |
1545 | | gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
1546 | | u_int size, bpf_u_int32 mask, int jtype, int reverse, |
1547 | | bpf_u_int32 v) |
1548 | 239k | { |
1549 | 239k | struct slist *s, *s2; |
1550 | 239k | struct block *b; |
1551 | | |
1552 | 239k | s = gen_load_a(cstate, offrel, offset, size); |
1553 | | |
1554 | 239k | if (mask != 0xffffffff) { |
1555 | 72.9k | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
1556 | 72.9k | s2->s.k = mask; |
1557 | 72.9k | sappend(s, s2); |
1558 | 72.9k | } |
1559 | | |
1560 | 239k | b = gen_jmp(cstate, jtype, v, s); |
1561 | 239k | if (reverse) |
1562 | 5.46k | gen_not(b); |
1563 | 239k | return b; |
1564 | 239k | } |
1565 | | |
1566 | | static int |
1567 | | init_linktype(compiler_state_t *cstate, pcap_t *p) |
1568 | 10.0k | { |
1569 | 10.0k | cstate->pcap_fddipad = p->fddipad; |
1570 | | |
1571 | | /* |
1572 | | * We start out with only one link-layer header. |
1573 | | */ |
1574 | 10.0k | cstate->outermostlinktype = pcap_datalink(p); |
1575 | 10.0k | cstate->off_outermostlinkhdr.constant_part = 0; |
1576 | 10.0k | cstate->off_outermostlinkhdr.is_variable = 0; |
1577 | 10.0k | cstate->off_outermostlinkhdr.reg = -1; |
1578 | | |
1579 | 10.0k | cstate->prevlinktype = cstate->outermostlinktype; |
1580 | 10.0k | cstate->off_prevlinkhdr.constant_part = 0; |
1581 | 10.0k | cstate->off_prevlinkhdr.is_variable = 0; |
1582 | 10.0k | cstate->off_prevlinkhdr.reg = -1; |
1583 | | |
1584 | 10.0k | cstate->linktype = cstate->outermostlinktype; |
1585 | 10.0k | cstate->off_linkhdr.constant_part = 0; |
1586 | 10.0k | cstate->off_linkhdr.is_variable = 0; |
1587 | 10.0k | cstate->off_linkhdr.reg = -1; |
1588 | | |
1589 | | /* |
1590 | | * XXX |
1591 | | */ |
1592 | 10.0k | cstate->off_linkpl.constant_part = 0; |
1593 | 10.0k | cstate->off_linkpl.is_variable = 0; |
1594 | 10.0k | cstate->off_linkpl.reg = -1; |
1595 | | |
1596 | 10.0k | cstate->off_linktype.constant_part = 0; |
1597 | 10.0k | cstate->off_linktype.is_variable = 0; |
1598 | 10.0k | cstate->off_linktype.reg = -1; |
1599 | | |
1600 | | /* |
1601 | | * Assume it's not raw ATM with a pseudo-header, for now. |
1602 | | */ |
1603 | 10.0k | cstate->is_atm = 0; |
1604 | 10.0k | cstate->off_vpi = OFFSET_NOT_SET; |
1605 | 10.0k | cstate->off_vci = OFFSET_NOT_SET; |
1606 | 10.0k | cstate->off_proto = OFFSET_NOT_SET; |
1607 | 10.0k | cstate->off_payload = OFFSET_NOT_SET; |
1608 | | |
1609 | | /* |
1610 | | * And not encapsulated with either Geneve or VXLAN. |
1611 | | */ |
1612 | 10.0k | cstate->is_encap = 0; |
1613 | | |
1614 | | /* |
1615 | | * No variable length VLAN offset by default |
1616 | | */ |
1617 | 10.0k | cstate->is_vlan_vloffset = 0; |
1618 | | |
1619 | | /* |
1620 | | * And assume we're not doing SS7. |
1621 | | */ |
1622 | 10.0k | cstate->off_li = OFFSET_NOT_SET; |
1623 | 10.0k | cstate->off_li_hsl = OFFSET_NOT_SET; |
1624 | 10.0k | cstate->off_sio = OFFSET_NOT_SET; |
1625 | 10.0k | cstate->off_opc = OFFSET_NOT_SET; |
1626 | 10.0k | cstate->off_dpc = OFFSET_NOT_SET; |
1627 | 10.0k | cstate->off_sls = OFFSET_NOT_SET; |
1628 | | |
1629 | 10.0k | cstate->label_stack_depth = 0; |
1630 | 10.0k | cstate->vlan_stack_depth = 0; |
1631 | | |
1632 | 10.0k | switch (cstate->linktype) { |
1633 | | |
1634 | 207 | case DLT_ARCNET: |
1635 | 207 | cstate->off_linktype.constant_part = 2; |
1636 | 207 | cstate->off_linkpl.constant_part = 6; |
1637 | 207 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1638 | 207 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1639 | 207 | break; |
1640 | | |
1641 | 94 | case DLT_ARCNET_LINUX: |
1642 | 94 | cstate->off_linktype.constant_part = 4; |
1643 | 94 | cstate->off_linkpl.constant_part = 8; |
1644 | 94 | cstate->off_nl = 0; /* XXX in reality, variable! */ |
1645 | 94 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1646 | 94 | break; |
1647 | | |
1648 | 421 | case DLT_EN10MB: |
1649 | 421 | cstate->off_linktype.constant_part = 12; |
1650 | 421 | cstate->off_linkpl.constant_part = 14; /* Ethernet header length */ |
1651 | 421 | cstate->off_nl = 0; /* Ethernet II */ |
1652 | 421 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
1653 | 421 | break; |
1654 | | |
1655 | 92 | case DLT_SLIP: |
1656 | | /* |
1657 | | * SLIP doesn't have a link level type. The 16 byte |
1658 | | * header is hacked into our SLIP driver. |
1659 | | */ |
1660 | 92 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1661 | 92 | cstate->off_linkpl.constant_part = 16; |
1662 | 92 | cstate->off_nl = 0; |
1663 | 92 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1664 | 92 | break; |
1665 | | |
1666 | 62 | case DLT_SLIP_BSDOS: |
1667 | | /* XXX this may be the same as the DLT_PPP_BSDOS case */ |
1668 | 62 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1669 | | /* XXX end */ |
1670 | 62 | cstate->off_linkpl.constant_part = 24; |
1671 | 62 | cstate->off_nl = 0; |
1672 | 62 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1673 | 62 | break; |
1674 | | |
1675 | 2.53k | case DLT_NULL: |
1676 | 2.56k | case DLT_LOOP: |
1677 | 2.56k | cstate->off_linktype.constant_part = 0; |
1678 | 2.56k | cstate->off_linkpl.constant_part = 4; |
1679 | 2.56k | cstate->off_nl = 0; |
1680 | 2.56k | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1681 | 2.56k | break; |
1682 | | |
1683 | 34 | case DLT_ENC: |
1684 | 34 | cstate->off_linktype.constant_part = 0; |
1685 | 34 | cstate->off_linkpl.constant_part = 12; |
1686 | 34 | cstate->off_nl = 0; |
1687 | 34 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1688 | 34 | break; |
1689 | | |
1690 | 137 | case DLT_PPP: |
1691 | 166 | case DLT_PPP_PPPD: |
1692 | 192 | case DLT_C_HDLC: /* BSD/OS Cisco HDLC */ |
1693 | 264 | case DLT_HDLC: /* NetBSD (Cisco) HDLC */ |
1694 | 379 | case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */ |
1695 | 379 | cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */ |
1696 | 379 | cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */ |
1697 | 379 | cstate->off_nl = 0; |
1698 | 379 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1699 | 379 | break; |
1700 | | |
1701 | 60 | case DLT_PPP_ETHER: |
1702 | | /* |
1703 | | * This does not include the Ethernet header, and |
1704 | | * only covers session state. |
1705 | | */ |
1706 | 60 | cstate->off_linktype.constant_part = 6; |
1707 | 60 | cstate->off_linkpl.constant_part = 8; |
1708 | 60 | cstate->off_nl = 0; |
1709 | 60 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1710 | 60 | break; |
1711 | | |
1712 | 386 | case DLT_PPP_BSDOS: |
1713 | 386 | cstate->off_linktype.constant_part = 5; |
1714 | 386 | cstate->off_linkpl.constant_part = 24; |
1715 | 386 | cstate->off_nl = 0; |
1716 | 386 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1717 | 386 | break; |
1718 | | |
1719 | 75 | case DLT_FDDI: |
1720 | | /* |
1721 | | * FDDI doesn't really have a link-level type field. |
1722 | | * We set "off_linktype" to the offset of the LLC header. |
1723 | | * |
1724 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1725 | | * is being used and pick out the encapsulated Ethernet type. |
1726 | | * XXX - should we generate code to check for SNAP? |
1727 | | */ |
1728 | 75 | cstate->off_linktype.constant_part = 13; |
1729 | 75 | cstate->off_linktype.constant_part += cstate->pcap_fddipad; |
1730 | 75 | cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */ |
1731 | 75 | cstate->off_linkpl.constant_part += cstate->pcap_fddipad; |
1732 | 75 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1733 | 75 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1734 | 75 | break; |
1735 | | |
1736 | 70 | case DLT_IEEE802: |
1737 | | /* |
1738 | | * Token Ring doesn't really have a link-level type field. |
1739 | | * We set "off_linktype" to the offset of the LLC header. |
1740 | | * |
1741 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1742 | | * is being used and pick out the encapsulated Ethernet type. |
1743 | | * XXX - should we generate code to check for SNAP? |
1744 | | * |
1745 | | * XXX - the header is actually variable-length. |
1746 | | * Some various Linux patched versions gave 38 |
1747 | | * as "off_linktype" and 40 as "off_nl"; however, |
1748 | | * if a token ring packet has *no* routing |
1749 | | * information, i.e. is not source-routed, the correct |
1750 | | * values are 20 and 22, as they are in the vanilla code. |
1751 | | * |
1752 | | * A packet is source-routed iff the uppermost bit |
1753 | | * of the first byte of the source address, at an |
1754 | | * offset of 8, has the uppermost bit set. If the |
1755 | | * packet is source-routed, the total number of bytes |
1756 | | * of routing information is 2 plus bits 0x1F00 of |
1757 | | * the 16-bit value at an offset of 14 (shifted right |
1758 | | * 8 - figure out which byte that is). |
1759 | | */ |
1760 | 70 | cstate->off_linktype.constant_part = 14; |
1761 | 70 | cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */ |
1762 | 70 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1763 | 70 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1764 | 70 | break; |
1765 | | |
1766 | 324 | case DLT_PRISM_HEADER: |
1767 | 483 | case DLT_IEEE802_11_RADIO_AVS: |
1768 | 756 | case DLT_IEEE802_11_RADIO: |
1769 | 756 | cstate->off_linkhdr.is_variable = 1; |
1770 | | /* Fall through, 802.11 doesn't have a variable link |
1771 | | * prefix but is otherwise the same. */ |
1772 | | /* FALLTHROUGH */ |
1773 | | |
1774 | 965 | case DLT_IEEE802_11: |
1775 | | /* |
1776 | | * 802.11 doesn't really have a link-level type field. |
1777 | | * We set "off_linktype.constant_part" to the offset of |
1778 | | * the LLC header. |
1779 | | * |
1780 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1781 | | * is being used and pick out the encapsulated Ethernet type. |
1782 | | * XXX - should we generate code to check for SNAP? |
1783 | | * |
1784 | | * We also handle variable-length radio headers here. |
1785 | | * The Prism header is in theory variable-length, but in |
1786 | | * practice it's always 144 bytes long. However, some |
1787 | | * drivers on Linux use ARPHRD_IEEE80211_PRISM, but |
1788 | | * sometimes or always supply an AVS header, so we |
1789 | | * have to check whether the radio header is a Prism |
1790 | | * header or an AVS header, so, in practice, it's |
1791 | | * variable-length. |
1792 | | */ |
1793 | 965 | cstate->off_linktype.constant_part = 24; |
1794 | 965 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1795 | 965 | cstate->off_linkpl.is_variable = 1; |
1796 | 965 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1797 | 965 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1798 | 965 | break; |
1799 | | |
1800 | 230 | case DLT_PPI: |
1801 | | /* |
1802 | | * At the moment we treat PPI the same way that we treat |
1803 | | * normal Radiotap encoded packets. The difference is in |
1804 | | * the function that generates the code at the beginning |
1805 | | * to compute the header length. Since this code generator |
1806 | | * of PPI supports bare 802.11 encapsulation only (i.e. |
1807 | | * the encapsulated DLT should be DLT_IEEE802_11) we |
1808 | | * generate code to check for this too. |
1809 | | */ |
1810 | 230 | cstate->off_linktype.constant_part = 24; |
1811 | 230 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1812 | 230 | cstate->off_linkpl.is_variable = 1; |
1813 | 230 | cstate->off_linkhdr.is_variable = 1; |
1814 | 230 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1815 | 230 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1816 | 230 | break; |
1817 | | |
1818 | 58 | case DLT_ATM_RFC1483: |
1819 | 111 | case DLT_ATM_CLIP: /* Linux ATM defines this */ |
1820 | | /* |
1821 | | * assume routed, non-ISO PDUs |
1822 | | * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) |
1823 | | * |
1824 | | * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS, |
1825 | | * or PPP with the PPP NLPID (e.g., PPPoA)? The |
1826 | | * latter would presumably be treated the way PPPoE |
1827 | | * should be, so you can do "pppoe and udp port 2049" |
1828 | | * or "pppoa and tcp port 80" and have it check for |
1829 | | * PPPo{A,E} and a PPP protocol of IP and.... |
1830 | | */ |
1831 | 111 | cstate->off_linktype.constant_part = 0; |
1832 | 111 | cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */ |
1833 | 111 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1834 | 111 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1835 | 111 | break; |
1836 | | |
1837 | 227 | case DLT_SUNATM: |
1838 | | /* |
1839 | | * Full Frontal ATM; you get AALn PDUs with an ATM |
1840 | | * pseudo-header. |
1841 | | */ |
1842 | 227 | cstate->is_atm = 1; |
1843 | 227 | cstate->off_vpi = SUNATM_VPI_POS; |
1844 | 227 | cstate->off_vci = SUNATM_VCI_POS; |
1845 | 227 | cstate->off_proto = PROTO_POS; |
1846 | 227 | cstate->off_payload = SUNATM_PKT_BEGIN_POS; |
1847 | 227 | cstate->off_linktype.constant_part = cstate->off_payload; |
1848 | 227 | cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */ |
1849 | 227 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1850 | 227 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1851 | 227 | break; |
1852 | | |
1853 | 102 | case DLT_RAW: |
1854 | 173 | case DLT_IPV4: |
1855 | 256 | case DLT_IPV6: |
1856 | 256 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1857 | 256 | cstate->off_linkpl.constant_part = 0; |
1858 | 256 | cstate->off_nl = 0; |
1859 | 256 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1860 | 256 | break; |
1861 | | |
1862 | 381 | case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */ |
1863 | 381 | cstate->off_linktype.constant_part = 14; |
1864 | 381 | cstate->off_linkpl.constant_part = 16; |
1865 | 381 | cstate->off_nl = 0; |
1866 | 381 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1867 | 381 | break; |
1868 | | |
1869 | 167 | case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */ |
1870 | 167 | cstate->off_linktype.constant_part = 0; |
1871 | 167 | cstate->off_linkpl.constant_part = 20; |
1872 | 167 | cstate->off_nl = 0; |
1873 | 167 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1874 | 167 | break; |
1875 | | |
1876 | 53 | case DLT_LTALK: |
1877 | | /* |
1878 | | * LocalTalk does have a 1-byte type field in the LLAP header, |
1879 | | * but really it just indicates whether there is a "short" or |
1880 | | * "long" DDP packet following. |
1881 | | */ |
1882 | 53 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1883 | 53 | cstate->off_linkpl.constant_part = 0; |
1884 | 53 | cstate->off_nl = 0; |
1885 | 53 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1886 | 53 | break; |
1887 | | |
1888 | 58 | case DLT_IP_OVER_FC: |
1889 | | /* |
1890 | | * RFC 2625 IP-over-Fibre-Channel doesn't really have a |
1891 | | * link-level type field. We set "off_linktype" to the |
1892 | | * offset of the LLC header. |
1893 | | * |
1894 | | * To check for Ethernet types, we assume that SSAP = SNAP |
1895 | | * is being used and pick out the encapsulated Ethernet type. |
1896 | | * XXX - should we generate code to check for SNAP? RFC |
1897 | | * 2625 says SNAP should be used. |
1898 | | */ |
1899 | 58 | cstate->off_linktype.constant_part = 16; |
1900 | 58 | cstate->off_linkpl.constant_part = 16; |
1901 | 58 | cstate->off_nl = 8; /* 802.2+SNAP */ |
1902 | 58 | cstate->off_nl_nosnap = 3; /* 802.2 */ |
1903 | 58 | break; |
1904 | | |
1905 | 125 | case DLT_FRELAY: |
1906 | | /* |
1907 | | * XXX - we should set this to handle SNAP-encapsulated |
1908 | | * frames (NLPID of 0x80). |
1909 | | */ |
1910 | 125 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1911 | 125 | cstate->off_linkpl.constant_part = 0; |
1912 | 125 | cstate->off_nl = 0; |
1913 | 125 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1914 | 125 | break; |
1915 | | |
1916 | | /* |
1917 | | * the only BPF-interesting FRF.16 frames are non-control frames; |
1918 | | * Frame Relay has a variable length link-layer |
1919 | | * so lets start with offset 4 for now and increments later on (FIXME); |
1920 | | */ |
1921 | 8 | case DLT_MFR: |
1922 | 8 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
1923 | 8 | cstate->off_linkpl.constant_part = 0; |
1924 | 8 | cstate->off_nl = 4; |
1925 | 8 | cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */ |
1926 | 8 | break; |
1927 | | |
1928 | 22 | case DLT_APPLE_IP_OVER_IEEE1394: |
1929 | 22 | cstate->off_linktype.constant_part = 16; |
1930 | 22 | cstate->off_linkpl.constant_part = 18; |
1931 | 22 | cstate->off_nl = 0; |
1932 | 22 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1933 | 22 | break; |
1934 | | |
1935 | 42 | case DLT_SYMANTEC_FIREWALL: |
1936 | 42 | cstate->off_linktype.constant_part = 6; |
1937 | 42 | cstate->off_linkpl.constant_part = 44; |
1938 | 42 | cstate->off_nl = 0; /* Ethernet II */ |
1939 | 42 | cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */ |
1940 | 42 | break; |
1941 | | |
1942 | 553 | case DLT_PFLOG: |
1943 | 553 | cstate->off_linktype.constant_part = 0; |
1944 | 553 | cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ |
1945 | 553 | cstate->off_linkpl.is_variable = 1; |
1946 | 553 | cstate->off_nl = 0; |
1947 | 553 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
1948 | 553 | break; |
1949 | | |
1950 | 18 | case DLT_JUNIPER_MFR: |
1951 | 41 | case DLT_JUNIPER_MLFR: |
1952 | 63 | case DLT_JUNIPER_MLPPP: |
1953 | 76 | case DLT_JUNIPER_PPP: |
1954 | 98 | case DLT_JUNIPER_CHDLC: |
1955 | 112 | case DLT_JUNIPER_FRELAY: |
1956 | 112 | cstate->off_linktype.constant_part = 4; |
1957 | 112 | cstate->off_linkpl.constant_part = 4; |
1958 | 112 | cstate->off_nl = 0; |
1959 | 112 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1960 | 112 | break; |
1961 | | |
1962 | 18 | case DLT_JUNIPER_ATM1: |
1963 | 18 | cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */ |
1964 | 18 | cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */ |
1965 | 18 | cstate->off_nl = 0; |
1966 | 18 | cstate->off_nl_nosnap = 10; |
1967 | 18 | break; |
1968 | | |
1969 | 27 | case DLT_JUNIPER_ATM2: |
1970 | 27 | cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */ |
1971 | 27 | cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */ |
1972 | 27 | cstate->off_nl = 0; |
1973 | 27 | cstate->off_nl_nosnap = 10; |
1974 | 27 | break; |
1975 | | |
1976 | | /* frames captured on a Juniper PPPoE service PIC |
1977 | | * contain raw ethernet frames */ |
1978 | 26 | case DLT_JUNIPER_PPPOE: |
1979 | 56 | case DLT_JUNIPER_ETHER: |
1980 | 56 | cstate->off_linkpl.constant_part = 14; |
1981 | 56 | cstate->off_linktype.constant_part = 16; |
1982 | 56 | cstate->off_nl = 18; /* Ethernet II */ |
1983 | 56 | cstate->off_nl_nosnap = 21; /* 802.3+802.2 */ |
1984 | 56 | break; |
1985 | | |
1986 | 16 | case DLT_JUNIPER_PPPOE_ATM: |
1987 | 16 | cstate->off_linktype.constant_part = 4; |
1988 | 16 | cstate->off_linkpl.constant_part = 6; |
1989 | 16 | cstate->off_nl = 0; |
1990 | 16 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1991 | 16 | break; |
1992 | | |
1993 | 20 | case DLT_JUNIPER_GGSN: |
1994 | 20 | cstate->off_linktype.constant_part = 6; |
1995 | 20 | cstate->off_linkpl.constant_part = 12; |
1996 | 20 | cstate->off_nl = 0; |
1997 | 20 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
1998 | 20 | break; |
1999 | | |
2000 | 27 | case DLT_JUNIPER_ES: |
2001 | 27 | cstate->off_linktype.constant_part = 6; |
2002 | 27 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
2003 | 27 | cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ |
2004 | 27 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2005 | 27 | break; |
2006 | | |
2007 | 25 | case DLT_JUNIPER_MONITOR: |
2008 | 25 | cstate->off_linktype.constant_part = 12; |
2009 | 25 | cstate->off_linkpl.constant_part = 12; |
2010 | 25 | cstate->off_nl = 0; /* raw IP/IP6 header */ |
2011 | 25 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2012 | 25 | break; |
2013 | | |
2014 | 16 | case DLT_BACNET_MS_TP: |
2015 | 16 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2016 | 16 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2017 | 16 | cstate->off_nl = OFFSET_NOT_SET; |
2018 | 16 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2019 | 16 | break; |
2020 | | |
2021 | 19 | case DLT_JUNIPER_SERVICES: |
2022 | 19 | cstate->off_linktype.constant_part = 12; |
2023 | 19 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
2024 | 19 | cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ |
2025 | 19 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2026 | 19 | break; |
2027 | | |
2028 | 11 | case DLT_JUNIPER_VP: |
2029 | 11 | cstate->off_linktype.constant_part = 18; |
2030 | 11 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2031 | 11 | cstate->off_nl = OFFSET_NOT_SET; |
2032 | 11 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2033 | 11 | break; |
2034 | | |
2035 | 16 | case DLT_JUNIPER_ST: |
2036 | 16 | cstate->off_linktype.constant_part = 18; |
2037 | 16 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2038 | 16 | cstate->off_nl = OFFSET_NOT_SET; |
2039 | 16 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2040 | 16 | break; |
2041 | | |
2042 | 23 | case DLT_JUNIPER_ISM: |
2043 | 23 | cstate->off_linktype.constant_part = 8; |
2044 | 23 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2045 | 23 | cstate->off_nl = OFFSET_NOT_SET; |
2046 | 23 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2047 | 23 | break; |
2048 | | |
2049 | 7 | case DLT_JUNIPER_VS: |
2050 | 28 | case DLT_JUNIPER_SRX_E2E: |
2051 | 47 | case DLT_JUNIPER_FIBRECHANNEL: |
2052 | 73 | case DLT_JUNIPER_ATM_CEMIC: |
2053 | 73 | cstate->off_linktype.constant_part = 8; |
2054 | 73 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2055 | 73 | cstate->off_nl = OFFSET_NOT_SET; |
2056 | 73 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2057 | 73 | break; |
2058 | | |
2059 | 82 | case DLT_MTP2: |
2060 | 82 | cstate->off_li = 2; |
2061 | 82 | cstate->off_li_hsl = 4; |
2062 | 82 | cstate->off_sio = 3; |
2063 | 82 | cstate->off_opc = 4; |
2064 | 82 | cstate->off_dpc = 4; |
2065 | 82 | cstate->off_sls = 7; |
2066 | 82 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2067 | 82 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2068 | 82 | cstate->off_nl = OFFSET_NOT_SET; |
2069 | 82 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2070 | 82 | break; |
2071 | | |
2072 | 59 | case DLT_MTP2_WITH_PHDR: |
2073 | 59 | cstate->off_li = 6; |
2074 | 59 | cstate->off_li_hsl = 8; |
2075 | 59 | cstate->off_sio = 7; |
2076 | 59 | cstate->off_opc = 8; |
2077 | 59 | cstate->off_dpc = 8; |
2078 | 59 | cstate->off_sls = 11; |
2079 | 59 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2080 | 59 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2081 | 59 | cstate->off_nl = OFFSET_NOT_SET; |
2082 | 59 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2083 | 59 | break; |
2084 | | |
2085 | 78 | case DLT_ERF: |
2086 | 78 | cstate->off_li = 22; |
2087 | 78 | cstate->off_li_hsl = 24; |
2088 | 78 | cstate->off_sio = 23; |
2089 | 78 | cstate->off_opc = 24; |
2090 | 78 | cstate->off_dpc = 24; |
2091 | 78 | cstate->off_sls = 27; |
2092 | 78 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2093 | 78 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2094 | 78 | cstate->off_nl = OFFSET_NOT_SET; |
2095 | 78 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2096 | 78 | break; |
2097 | | |
2098 | 11 | case DLT_PFSYNC: |
2099 | 11 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2100 | 11 | cstate->off_linkpl.constant_part = 4; |
2101 | 11 | cstate->off_nl = 0; |
2102 | 11 | cstate->off_nl_nosnap = 0; |
2103 | 11 | break; |
2104 | | |
2105 | 10 | case DLT_AX25_KISS: |
2106 | | /* |
2107 | | * Currently, only raw "link[N:M]" filtering is supported. |
2108 | | */ |
2109 | 10 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2110 | 10 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2111 | 10 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2112 | 10 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2113 | 10 | break; |
2114 | | |
2115 | 82 | case DLT_IPNET: |
2116 | 82 | cstate->off_linktype.constant_part = 1; |
2117 | 82 | cstate->off_linkpl.constant_part = 24; /* ipnet header length */ |
2118 | 82 | cstate->off_nl = 0; |
2119 | 82 | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2120 | 82 | break; |
2121 | | |
2122 | 73 | case DLT_NETANALYZER: |
2123 | 73 | cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */ |
2124 | 73 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
2125 | 73 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */ |
2126 | 73 | cstate->off_nl = 0; /* Ethernet II */ |
2127 | 73 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
2128 | 73 | break; |
2129 | | |
2130 | 65 | case DLT_NETANALYZER_TRANSPARENT: |
2131 | 65 | cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */ |
2132 | 65 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
2133 | 65 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */ |
2134 | 65 | cstate->off_nl = 0; /* Ethernet II */ |
2135 | 65 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
2136 | 65 | break; |
2137 | | |
2138 | 19 | case DLT_EN3MB: |
2139 | | /* |
2140 | | * Currently, only raw "link[N:M]" filtering is supported. |
2141 | | */ |
2142 | 19 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2143 | 19 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2144 | 19 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2145 | 19 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2146 | 19 | break; |
2147 | | |
2148 | 11 | case DLT_AX25: |
2149 | | /* |
2150 | | * Currently, only raw "link[N:M]" filtering is supported. |
2151 | | */ |
2152 | 11 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2153 | 11 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2154 | 11 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2155 | 11 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2156 | 11 | break; |
2157 | | |
2158 | 21 | case DLT_PRONET: |
2159 | | /* |
2160 | | * Currently, only raw "link[N:M]" filtering is supported. |
2161 | | */ |
2162 | 21 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2163 | 21 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2164 | 21 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2165 | 21 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2166 | 21 | break; |
2167 | | |
2168 | 5 | case DLT_CHAOS: |
2169 | | /* |
2170 | | * Currently, only raw "link[N:M]" filtering is supported. |
2171 | | */ |
2172 | 5 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2173 | 5 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2174 | 5 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2175 | 5 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2176 | 5 | break; |
2177 | | |
2178 | | #ifdef DLT_HIPPI |
2179 | | case DLT_HIPPI: |
2180 | | /* |
2181 | | * Currently, only raw "link[N:M]" filtering is supported. |
2182 | | */ |
2183 | | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2184 | | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2185 | | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2186 | | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2187 | | break; |
2188 | | |
2189 | | #endif |
2190 | | |
2191 | 14 | case DLT_REDBACK_SMARTEDGE: |
2192 | | /* |
2193 | | * Currently, only raw "link[N:M]" filtering is supported. |
2194 | | */ |
2195 | 14 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2196 | 14 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2197 | 14 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2198 | 14 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2199 | 14 | break; |
2200 | | |
2201 | | |
2202 | 0 | #ifdef DLT_HHDLC |
2203 | 4 | case DLT_HHDLC: |
2204 | | /* |
2205 | | * Currently, only raw "link[N:M]" filtering is supported. |
2206 | | */ |
2207 | 4 | cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ |
2208 | 4 | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2209 | 4 | cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ |
2210 | 4 | cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ |
2211 | 4 | break; |
2212 | | |
2213 | 0 | #endif |
2214 | | |
2215 | 1.41k | default: |
2216 | | /* |
2217 | | * For values in the range in which we've assigned new |
2218 | | * DLT_ values, only raw "link[N:M]" filtering is supported. |
2219 | | */ |
2220 | 1.41k | if (cstate->linktype >= DLT_HIGH_MATCHING_MIN && |
2221 | 1.41k | cstate->linktype <= DLT_HIGH_MATCHING_MAX) { |
2222 | 1.38k | cstate->off_linktype.constant_part = OFFSET_NOT_SET; |
2223 | 1.38k | cstate->off_linkpl.constant_part = OFFSET_NOT_SET; |
2224 | 1.38k | cstate->off_nl = OFFSET_NOT_SET; |
2225 | 1.38k | cstate->off_nl_nosnap = OFFSET_NOT_SET; |
2226 | 1.38k | } else { |
2227 | 35 | bpf_set_error(cstate, "unknown data link type %d", |
2228 | 35 | cstate->linktype); |
2229 | 35 | return (-1); |
2230 | 35 | } |
2231 | 1.38k | break; |
2232 | 10.0k | } |
2233 | | |
2234 | 10.0k | cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr; |
2235 | 10.0k | return (0); |
2236 | 10.0k | } |
2237 | | |
2238 | | /* |
2239 | | * Load a value relative to the specified absolute offset. |
2240 | | */ |
2241 | | static struct slist * |
2242 | | gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset, |
2243 | | u_int offset, u_int size) |
2244 | 236k | { |
2245 | 236k | struct slist *s, *s2; |
2246 | | |
2247 | 236k | s = gen_abs_offset_varpart(cstate, abs_offset); |
2248 | | |
2249 | | /* |
2250 | | * If "s" is non-null, it has code to arrange that the X register |
2251 | | * contains the variable part of the absolute offset, so we |
2252 | | * generate a load relative to that, with an offset of |
2253 | | * abs_offset->constant_part + offset. |
2254 | | * |
2255 | | * Otherwise, we can do an absolute load with an offset of |
2256 | | * abs_offset->constant_part + offset. |
2257 | | */ |
2258 | 236k | if (s != NULL) { |
2259 | | /* |
2260 | | * "s" points to a list of statements that puts the |
2261 | | * variable part of the absolute offset into the X register. |
2262 | | * Do an indirect load, to use the X register as an offset. |
2263 | | */ |
2264 | 94.0k | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
2265 | 94.0k | s2->s.k = abs_offset->constant_part + offset; |
2266 | 94.0k | sappend(s, s2); |
2267 | 142k | } else { |
2268 | | /* |
2269 | | * There is no variable part of the absolute offset, so |
2270 | | * just do an absolute load. |
2271 | | */ |
2272 | 142k | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
2273 | 142k | s->s.k = abs_offset->constant_part + offset; |
2274 | 142k | } |
2275 | 236k | return s; |
2276 | 236k | } |
2277 | | |
2278 | | /* |
2279 | | * Load a value relative to the beginning of the specified header. |
2280 | | */ |
2281 | | static struct slist * |
2282 | | gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, |
2283 | | u_int size) |
2284 | 249k | { |
2285 | 249k | struct slist *s, *s2; |
2286 | | |
2287 | | /* |
2288 | | * Squelch warnings from compilers that *don't* assume that |
2289 | | * offrel always has a valid enum value and therefore don't |
2290 | | * assume that we'll always go through one of the case arms. |
2291 | | * |
2292 | | * If we have a default case, compilers that *do* assume that |
2293 | | * will then complain about the default case code being |
2294 | | * unreachable. |
2295 | | * |
2296 | | * Damned if you do, damned if you don't. |
2297 | | */ |
2298 | 249k | s = NULL; |
2299 | | |
2300 | 249k | switch (offrel) { |
2301 | | |
2302 | 1.09k | case OR_PACKET: |
2303 | 1.09k | s = new_stmt(cstate, BPF_LD|BPF_ABS|size); |
2304 | 1.09k | s->s.k = offset; |
2305 | 1.09k | break; |
2306 | | |
2307 | 48.8k | case OR_LINKHDR: |
2308 | 48.8k | s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size); |
2309 | 48.8k | break; |
2310 | | |
2311 | 1.96k | case OR_PREVLINKHDR: |
2312 | 1.96k | s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size); |
2313 | 1.96k | break; |
2314 | | |
2315 | 25.3k | case OR_LLC: |
2316 | 25.3k | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size); |
2317 | 25.3k | break; |
2318 | | |
2319 | 77 | case OR_PREVMPLSHDR: |
2320 | 77 | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size); |
2321 | 77 | break; |
2322 | | |
2323 | 100k | case OR_LINKPL: |
2324 | 100k | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size); |
2325 | 100k | break; |
2326 | | |
2327 | 25.5k | case OR_LINKPL_NOSNAP: |
2328 | 25.5k | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size); |
2329 | 25.5k | break; |
2330 | | |
2331 | 22.9k | case OR_LINKTYPE: |
2332 | 22.9k | s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size); |
2333 | 22.9k | break; |
2334 | | |
2335 | 11.2k | case OR_TRAN_IPV4: |
2336 | | /* |
2337 | | * Load the X register with the length of the IPv4 header |
2338 | | * (plus the offset of the link-layer header, if it's |
2339 | | * preceded by a variable-length header such as a radio |
2340 | | * header), in bytes. |
2341 | | */ |
2342 | 11.2k | s = gen_loadx_iphdrlen(cstate); |
2343 | | |
2344 | | /* |
2345 | | * Load the item at {offset of the link-layer payload} + |
2346 | | * {offset, relative to the start of the link-layer |
2347 | | * payload, of the IPv4 header} + {length of the IPv4 header} + |
2348 | | * {specified offset}. |
2349 | | * |
2350 | | * If the offset of the link-layer payload is variable, |
2351 | | * the variable part of that offset is included in the |
2352 | | * value in the X register, and we include the constant |
2353 | | * part in the offset of the load. |
2354 | | */ |
2355 | 11.2k | s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); |
2356 | 11.2k | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset; |
2357 | 11.2k | sappend(s, s2); |
2358 | 11.2k | break; |
2359 | | |
2360 | 11.2k | case OR_TRAN_IPV6: |
2361 | 11.2k | s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size); |
2362 | 11.2k | break; |
2363 | 249k | } |
2364 | 249k | return s; |
2365 | 249k | } |
2366 | | |
2367 | | /* |
2368 | | * Generate code to load into the X register the sum of the length of |
2369 | | * the IPv4 header and the variable part of the offset of the link-layer |
2370 | | * payload. |
2371 | | */ |
2372 | | static struct slist * |
2373 | | gen_loadx_iphdrlen(compiler_state_t *cstate) |
2374 | 13.3k | { |
2375 | 13.3k | struct slist *s, *s2; |
2376 | | |
2377 | 13.3k | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
2378 | 13.3k | if (s != NULL) { |
2379 | | /* |
2380 | | * The offset of the link-layer payload has a variable |
2381 | | * part. "s" points to a list of statements that put |
2382 | | * the variable part of that offset into the X register. |
2383 | | * |
2384 | | * The 4*([k]&0xf) addressing mode can't be used, as we |
2385 | | * don't have a constant offset, so we have to load the |
2386 | | * value in question into the A register and add to it |
2387 | | * the value from the X register. |
2388 | | */ |
2389 | 5.30k | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
2390 | 5.30k | s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
2391 | 5.30k | sappend(s, s2); |
2392 | 5.30k | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2393 | 5.30k | s2->s.k = 0xf; |
2394 | 5.30k | sappend(s, s2); |
2395 | 5.30k | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
2396 | 5.30k | s2->s.k = 2; |
2397 | 5.30k | sappend(s, s2); |
2398 | | |
2399 | | /* |
2400 | | * The A register now contains the length of the IP header. |
2401 | | * We need to add to it the variable part of the offset of |
2402 | | * the link-layer payload, which is still in the X |
2403 | | * register, and move the result into the X register. |
2404 | | */ |
2405 | 5.30k | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
2406 | 5.30k | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
2407 | 8.00k | } else { |
2408 | | /* |
2409 | | * The offset of the link-layer payload is a constant, |
2410 | | * so no code was generated to load the (nonexistent) |
2411 | | * variable part of that offset. |
2412 | | * |
2413 | | * This means we can use the 4*([k]&0xf) addressing |
2414 | | * mode. Load the length of the IPv4 header, which |
2415 | | * is at an offset of cstate->off_nl from the beginning of |
2416 | | * the link-layer payload, and thus at an offset of |
2417 | | * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning |
2418 | | * of the raw packet data, using that addressing mode. |
2419 | | */ |
2420 | 8.00k | s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); |
2421 | 8.00k | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
2422 | 8.00k | } |
2423 | 13.3k | return s; |
2424 | 13.3k | } |
2425 | | |
2426 | | |
2427 | | static struct block * |
2428 | | gen_uncond(compiler_state_t *cstate, int rsense) |
2429 | 8.32k | { |
2430 | 8.32k | struct slist *s; |
2431 | | |
2432 | 8.32k | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
2433 | 8.32k | s->s.k = !rsense; |
2434 | 8.32k | return gen_jmp(cstate, BPF_JEQ, 0, s); |
2435 | 8.32k | } |
2436 | | |
2437 | | static inline struct block * |
2438 | | gen_true(compiler_state_t *cstate) |
2439 | 1.35k | { |
2440 | 1.35k | return gen_uncond(cstate, 1); |
2441 | 1.35k | } |
2442 | | |
2443 | | static inline struct block * |
2444 | | gen_false(compiler_state_t *cstate) |
2445 | 6.97k | { |
2446 | 6.97k | return gen_uncond(cstate, 0); |
2447 | 6.97k | } |
2448 | | |
2449 | | /* |
2450 | | * Generate code to match a particular packet type. |
2451 | | * |
2452 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
2453 | | * value, if <= ETHERMTU. We use that to determine whether to |
2454 | | * match the type/length field or to check the type/length field for |
2455 | | * a value <= ETHERMTU to see whether it's a type field and then do |
2456 | | * the appropriate test. |
2457 | | */ |
2458 | | static struct block * |
2459 | | gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2460 | 7.71k | { |
2461 | 7.71k | struct block *b0, *b1; |
2462 | | |
2463 | 7.71k | switch (ll_proto) { |
2464 | | |
2465 | 2.25k | case LLCSAP_ISONS: |
2466 | 2.33k | case LLCSAP_IP: |
2467 | 2.36k | case LLCSAP_NETBEUI: |
2468 | | /* |
2469 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
2470 | | * so we check the DSAP and SSAP. |
2471 | | * |
2472 | | * LLCSAP_IP checks for IP-over-802.2, rather |
2473 | | * than IP-over-Ethernet or IP-over-SNAP. |
2474 | | * |
2475 | | * XXX - should we check both the DSAP and the |
2476 | | * SSAP, like this, or should we check just the |
2477 | | * DSAP, as we do for other types <= ETHERMTU |
2478 | | * (i.e., other SAP values)? |
2479 | | */ |
2480 | 2.36k | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2481 | 2.36k | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); |
2482 | 2.36k | gen_and(b0, b1); |
2483 | 2.36k | return b1; |
2484 | | |
2485 | 39 | case LLCSAP_IPX: |
2486 | | /* |
2487 | | * Check for; |
2488 | | * |
2489 | | * Ethernet_II frames, which are Ethernet |
2490 | | * frames with a frame type of ETHERTYPE_IPX; |
2491 | | * |
2492 | | * Ethernet_802.3 frames, which are 802.3 |
2493 | | * frames (i.e., the type/length field is |
2494 | | * a length field, <= ETHERMTU, rather than |
2495 | | * a type field) with the first two bytes |
2496 | | * after the Ethernet/802.3 header being |
2497 | | * 0xFFFF; |
2498 | | * |
2499 | | * Ethernet_802.2 frames, which are 802.3 |
2500 | | * frames with an 802.2 LLC header and |
2501 | | * with the IPX LSAP as the DSAP in the LLC |
2502 | | * header; |
2503 | | * |
2504 | | * Ethernet_SNAP frames, which are 802.3 |
2505 | | * frames with an LLC header and a SNAP |
2506 | | * header and with an OUI of 0x000000 |
2507 | | * (encapsulated Ethernet) and a protocol |
2508 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2509 | | * |
2510 | | * XXX - should we generate the same code both |
2511 | | * for tests for LLCSAP_IPX and for ETHERTYPE_IPX? |
2512 | | */ |
2513 | | |
2514 | | /* |
2515 | | * This generates code to check both for the |
2516 | | * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3. |
2517 | | */ |
2518 | 39 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
2519 | 39 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF); |
2520 | 39 | gen_or(b0, b1); |
2521 | | |
2522 | | /* |
2523 | | * Now we add code to check for SNAP frames with |
2524 | | * ETHERTYPE_IPX, i.e. Ethernet_SNAP. |
2525 | | */ |
2526 | 39 | b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2527 | 39 | gen_or(b0, b1); |
2528 | | |
2529 | | /* |
2530 | | * Now we generate code to check for 802.3 |
2531 | | * frames in general. |
2532 | | */ |
2533 | 39 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2534 | | |
2535 | | /* |
2536 | | * Now add the check for 802.3 frames before the |
2537 | | * check for Ethernet_802.2 and Ethernet_802.3, |
2538 | | * as those checks should only be done on 802.3 |
2539 | | * frames, not on Ethernet frames. |
2540 | | */ |
2541 | 39 | gen_and(b0, b1); |
2542 | | |
2543 | | /* |
2544 | | * Now add the check for Ethernet_II frames, and |
2545 | | * do that before checking for the other frame |
2546 | | * types. |
2547 | | */ |
2548 | 39 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); |
2549 | 39 | gen_or(b0, b1); |
2550 | 39 | return b1; |
2551 | | |
2552 | 46 | case ETHERTYPE_ATALK: |
2553 | 64 | case ETHERTYPE_AARP: |
2554 | | /* |
2555 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2556 | | * layer) may use 802.2 encapsulation. |
2557 | | */ |
2558 | | |
2559 | | /* |
2560 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2561 | | * we check for an Ethernet type field less or equal than |
2562 | | * 1500, which means it's an 802.3 length field. |
2563 | | */ |
2564 | 64 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2565 | | |
2566 | | /* |
2567 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2568 | | * SNAP packets with an organization code of |
2569 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2570 | | * type of ETHERTYPE_ATALK (Appletalk). |
2571 | | * |
2572 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2573 | | * SNAP packets with an organization code of |
2574 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2575 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2576 | | */ |
2577 | 64 | if (ll_proto == ETHERTYPE_ATALK) |
2578 | 46 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2579 | 18 | else /* ll_proto == ETHERTYPE_AARP */ |
2580 | 18 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2581 | 64 | gen_and(b0, b1); |
2582 | | |
2583 | | /* |
2584 | | * Check for Ethernet encapsulation (Ethertalk |
2585 | | * phase 1?); we just check for the Ethernet |
2586 | | * protocol type. |
2587 | | */ |
2588 | 64 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2589 | | |
2590 | 64 | gen_or(b0, b1); |
2591 | 64 | return b1; |
2592 | | |
2593 | 5.24k | default: |
2594 | 5.24k | if (ll_proto <= ETHERMTU) { |
2595 | 150 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
2596 | | /* |
2597 | | * This is an LLC SAP value, so the frames |
2598 | | * that match would be 802.2 frames. |
2599 | | * Check that the frame is an 802.2 frame |
2600 | | * (i.e., that the length/type field is |
2601 | | * a length field, <= ETHERMTU) and |
2602 | | * then check the DSAP. |
2603 | | */ |
2604 | 150 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
2605 | 150 | b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto); |
2606 | 150 | gen_and(b0, b1); |
2607 | 150 | return b1; |
2608 | 5.09k | } else { |
2609 | 5.09k | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
2610 | | /* |
2611 | | * This is an Ethernet type, so compare |
2612 | | * the length/type field with it (if |
2613 | | * the frame is an 802.2 frame, the length |
2614 | | * field will be <= ETHERMTU, and, as |
2615 | | * "ll_proto" is > ETHERMTU, this test |
2616 | | * will fail and the frame won't match, |
2617 | | * which is what we want). |
2618 | | */ |
2619 | 5.09k | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2620 | 5.09k | } |
2621 | 7.71k | } |
2622 | 7.71k | } |
2623 | | |
2624 | | static struct block * |
2625 | | gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2626 | 9.78k | { |
2627 | | /* |
2628 | | * For DLT_NULL, the link-layer header is a 32-bit word |
2629 | | * containing an AF_ value in *host* byte order, and for |
2630 | | * DLT_ENC, the link-layer header begins with a 32-bit |
2631 | | * word containing an AF_ value in host byte order. |
2632 | | * |
2633 | | * In addition, if we're reading a saved capture file, |
2634 | | * the host byte order in the capture may not be the |
2635 | | * same as the host byte order on this machine. |
2636 | | * |
2637 | | * For DLT_LOOP, the link-layer header is a 32-bit |
2638 | | * word containing an AF_ value in *network* byte order. |
2639 | | */ |
2640 | 9.78k | if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) { |
2641 | | /* |
2642 | | * The AF_ value is in host byte order, but the BPF |
2643 | | * interpreter will convert it to network byte order. |
2644 | | * |
2645 | | * If this is a save file, and it's from a machine |
2646 | | * with the opposite byte order to ours, we byte-swap |
2647 | | * the AF_ value. |
2648 | | * |
2649 | | * Then we run it through "htonl()", and generate |
2650 | | * code to compare against the result. |
2651 | | */ |
2652 | 9.51k | if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped) |
2653 | 2.24k | ll_proto = SWAPLONG(ll_proto); |
2654 | 9.51k | ll_proto = htonl(ll_proto); |
2655 | 9.51k | } |
2656 | 9.78k | return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto)); |
2657 | 9.78k | } |
2658 | | |
2659 | | /* |
2660 | | * "proto" is an Ethernet type value and for IPNET, if it is not IPv4 |
2661 | | * or IPv6 then we have an error. |
2662 | | */ |
2663 | | static struct block * |
2664 | | gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2665 | 505 | { |
2666 | 505 | switch (ll_proto) { |
2667 | | |
2668 | 111 | case ETHERTYPE_IP: |
2669 | 111 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET); |
2670 | | /*NOTREACHED*/ |
2671 | | |
2672 | 92 | case ETHERTYPE_IPV6: |
2673 | 92 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6); |
2674 | | /*NOTREACHED*/ |
2675 | | |
2676 | 302 | default: |
2677 | 302 | break; |
2678 | 505 | } |
2679 | | |
2680 | 302 | return gen_false(cstate); |
2681 | 505 | } |
2682 | | |
2683 | | /* |
2684 | | * Generate code to match a particular packet type. |
2685 | | * |
2686 | | * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
2687 | | * value, if <= ETHERMTU. We use that to determine whether to |
2688 | | * match the type field or to check the type field for the special |
2689 | | * LINUX_SLL_P_802_2 value and then do the appropriate test. |
2690 | | */ |
2691 | | static struct block * |
2692 | | gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
2693 | 861 | { |
2694 | 861 | struct block *b0, *b1; |
2695 | | |
2696 | 861 | switch (ll_proto) { |
2697 | | |
2698 | 224 | case LLCSAP_ISONS: |
2699 | 310 | case LLCSAP_IP: |
2700 | 333 | case LLCSAP_NETBEUI: |
2701 | | /* |
2702 | | * OSI protocols and NetBEUI always use 802.2 encapsulation, |
2703 | | * so we check the DSAP and SSAP. |
2704 | | * |
2705 | | * LLCSAP_IP checks for IP-over-802.2, rather |
2706 | | * than IP-over-Ethernet or IP-over-SNAP. |
2707 | | * |
2708 | | * XXX - should we check both the DSAP and the |
2709 | | * SSAP, like this, or should we check just the |
2710 | | * DSAP, as we do for other types <= ETHERMTU |
2711 | | * (i.e., other SAP values)? |
2712 | | */ |
2713 | 333 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2714 | 333 | b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); |
2715 | 333 | gen_and(b0, b1); |
2716 | 333 | return b1; |
2717 | | |
2718 | 68 | case LLCSAP_IPX: |
2719 | | /* |
2720 | | * Ethernet_II frames, which are Ethernet |
2721 | | * frames with a frame type of ETHERTYPE_IPX; |
2722 | | * |
2723 | | * Ethernet_802.3 frames, which have a frame |
2724 | | * type of LINUX_SLL_P_802_3; |
2725 | | * |
2726 | | * Ethernet_802.2 frames, which are 802.3 |
2727 | | * frames with an 802.2 LLC header (i.e, have |
2728 | | * a frame type of LINUX_SLL_P_802_2) and |
2729 | | * with the IPX LSAP as the DSAP in the LLC |
2730 | | * header; |
2731 | | * |
2732 | | * Ethernet_SNAP frames, which are 802.3 |
2733 | | * frames with an LLC header and a SNAP |
2734 | | * header and with an OUI of 0x000000 |
2735 | | * (encapsulated Ethernet) and a protocol |
2736 | | * ID of ETHERTYPE_IPX in the SNAP header. |
2737 | | * |
2738 | | * First, do the checks on LINUX_SLL_P_802_2 |
2739 | | * frames; generate the check for either |
2740 | | * Ethernet_802.2 or Ethernet_SNAP frames, and |
2741 | | * then put a check for LINUX_SLL_P_802_2 frames |
2742 | | * before it. |
2743 | | */ |
2744 | 68 | b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
2745 | 68 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); |
2746 | 68 | gen_or(b0, b1); |
2747 | 68 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2748 | 68 | gen_and(b0, b1); |
2749 | | |
2750 | | /* |
2751 | | * Now check for 802.3 frames and OR that with |
2752 | | * the previous test. |
2753 | | */ |
2754 | 68 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3); |
2755 | 68 | gen_or(b0, b1); |
2756 | | |
2757 | | /* |
2758 | | * Now add the check for Ethernet_II frames, and |
2759 | | * do that before checking for the other frame |
2760 | | * types. |
2761 | | */ |
2762 | 68 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); |
2763 | 68 | gen_or(b0, b1); |
2764 | 68 | return b1; |
2765 | | |
2766 | 35 | case ETHERTYPE_ATALK: |
2767 | 52 | case ETHERTYPE_AARP: |
2768 | | /* |
2769 | | * EtherTalk (AppleTalk protocols on Ethernet link |
2770 | | * layer) may use 802.2 encapsulation. |
2771 | | */ |
2772 | | |
2773 | | /* |
2774 | | * Check for 802.2 encapsulation (EtherTalk phase 2?); |
2775 | | * we check for the 802.2 protocol type in the |
2776 | | * "Ethernet type" field. |
2777 | | */ |
2778 | 52 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2779 | | |
2780 | | /* |
2781 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
2782 | | * SNAP packets with an organization code of |
2783 | | * 0x080007 (Apple, for Appletalk) and a protocol |
2784 | | * type of ETHERTYPE_ATALK (Appletalk). |
2785 | | * |
2786 | | * 802.2-encapsulated ETHERTYPE_AARP packets are |
2787 | | * SNAP packets with an organization code of |
2788 | | * 0x000000 (encapsulated Ethernet) and a protocol |
2789 | | * type of ETHERTYPE_AARP (Appletalk ARP). |
2790 | | */ |
2791 | 52 | if (ll_proto == ETHERTYPE_ATALK) |
2792 | 35 | b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
2793 | 17 | else /* ll_proto == ETHERTYPE_AARP */ |
2794 | 17 | b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); |
2795 | 52 | gen_and(b0, b1); |
2796 | | |
2797 | | /* |
2798 | | * Check for Ethernet encapsulation (Ethertalk |
2799 | | * phase 1?); we just check for the Ethernet |
2800 | | * protocol type. |
2801 | | */ |
2802 | 52 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2803 | | |
2804 | 52 | gen_or(b0, b1); |
2805 | 52 | return b1; |
2806 | | |
2807 | 408 | default: |
2808 | 408 | if (ll_proto <= ETHERMTU) { |
2809 | 100 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
2810 | | /* |
2811 | | * This is an LLC SAP value, so the frames |
2812 | | * that match would be 802.2 frames. |
2813 | | * Check for the 802.2 protocol type |
2814 | | * in the "Ethernet type" field, and |
2815 | | * then check the DSAP. |
2816 | | */ |
2817 | 100 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); |
2818 | 100 | b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B, |
2819 | 100 | ll_proto); |
2820 | 100 | gen_and(b0, b1); |
2821 | 100 | return b1; |
2822 | 308 | } else { |
2823 | 308 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
2824 | | /* |
2825 | | * This is an Ethernet type, so compare |
2826 | | * the length/type field with it (if |
2827 | | * the frame is an 802.2 frame, the length |
2828 | | * field will be <= ETHERMTU, and, as |
2829 | | * "ll_proto" is > ETHERMTU, this test |
2830 | | * will fail and the frame won't match, |
2831 | | * which is what we want). |
2832 | | */ |
2833 | 308 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
2834 | 308 | } |
2835 | 861 | } |
2836 | 861 | } |
2837 | | |
2838 | | /* |
2839 | | * Load a value relative to the beginning of the link-layer header after the |
2840 | | * pflog header. |
2841 | | */ |
2842 | | static struct slist * |
2843 | | gen_load_pflog_llprefixlen(compiler_state_t *cstate) |
2844 | 337 | { |
2845 | 337 | struct slist *s1, *s2; |
2846 | | |
2847 | | /* |
2848 | | * Generate code to load the length of the pflog header into |
2849 | | * the register assigned to hold that length, if one has been |
2850 | | * assigned. (If one hasn't been assigned, no code we've |
2851 | | * generated uses that prefix, so we don't need to generate any |
2852 | | * code to load it.) |
2853 | | */ |
2854 | 337 | if (cstate->off_linkpl.reg != -1) { |
2855 | | /* |
2856 | | * The length is in the first byte of the header. |
2857 | | */ |
2858 | 260 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
2859 | 260 | s1->s.k = 0; |
2860 | | |
2861 | | /* |
2862 | | * Round it up to a multiple of 4. |
2863 | | * Add 3, and clear the lower 2 bits. |
2864 | | */ |
2865 | 260 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
2866 | 260 | s2->s.k = 3; |
2867 | 260 | sappend(s1, s2); |
2868 | 260 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2869 | 260 | s2->s.k = 0xfffffffc; |
2870 | 260 | sappend(s1, s2); |
2871 | | |
2872 | | /* |
2873 | | * Now allocate a register to hold that value and store |
2874 | | * it. |
2875 | | */ |
2876 | 260 | s2 = new_stmt(cstate, BPF_ST); |
2877 | 260 | s2->s.k = cstate->off_linkpl.reg; |
2878 | 260 | sappend(s1, s2); |
2879 | | |
2880 | | /* |
2881 | | * Now move it into the X register. |
2882 | | */ |
2883 | 260 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2884 | 260 | sappend(s1, s2); |
2885 | | |
2886 | 260 | return (s1); |
2887 | 260 | } else |
2888 | 77 | return (NULL); |
2889 | 337 | } |
2890 | | |
2891 | | static struct slist * |
2892 | | gen_load_prism_llprefixlen(compiler_state_t *cstate) |
2893 | 271 | { |
2894 | 271 | struct slist *s1, *s2; |
2895 | 271 | struct slist *sjeq_avs_cookie; |
2896 | 271 | struct slist *sjcommon; |
2897 | | |
2898 | | /* |
2899 | | * This code is not compatible with the optimizer, as |
2900 | | * we are generating jmp instructions within a normal |
2901 | | * slist of instructions |
2902 | | */ |
2903 | 271 | cstate->no_optimize = 1; |
2904 | | |
2905 | | /* |
2906 | | * Generate code to load the length of the radio header into |
2907 | | * the register assigned to hold that length, if one has been |
2908 | | * assigned. (If one hasn't been assigned, no code we've |
2909 | | * generated uses that prefix, so we don't need to generate any |
2910 | | * code to load it.) |
2911 | | * |
2912 | | * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes |
2913 | | * or always use the AVS header rather than the Prism header. |
2914 | | * We load a 4-byte big-endian value at the beginning of the |
2915 | | * raw packet data, and see whether, when masked with 0xFFFFF000, |
2916 | | * it's equal to 0x80211000. If so, that indicates that it's |
2917 | | * an AVS header (the masked-out bits are the version number). |
2918 | | * Otherwise, it's a Prism header. |
2919 | | * |
2920 | | * XXX - the Prism header is also, in theory, variable-length, |
2921 | | * but no known software generates headers that aren't 144 |
2922 | | * bytes long. |
2923 | | */ |
2924 | 271 | if (cstate->off_linkhdr.reg != -1) { |
2925 | | /* |
2926 | | * Load the cookie. |
2927 | | */ |
2928 | 122 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2929 | 122 | s1->s.k = 0; |
2930 | | |
2931 | | /* |
2932 | | * AND it with 0xFFFFF000. |
2933 | | */ |
2934 | 122 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
2935 | 122 | s2->s.k = 0xFFFFF000; |
2936 | 122 | sappend(s1, s2); |
2937 | | |
2938 | | /* |
2939 | | * Compare with 0x80211000. |
2940 | | */ |
2941 | 122 | sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ)); |
2942 | 122 | sjeq_avs_cookie->s.k = 0x80211000; |
2943 | 122 | sappend(s1, sjeq_avs_cookie); |
2944 | | |
2945 | | /* |
2946 | | * If it's AVS: |
2947 | | * |
2948 | | * The 4 bytes at an offset of 4 from the beginning of |
2949 | | * the AVS header are the length of the AVS header. |
2950 | | * That field is big-endian. |
2951 | | */ |
2952 | 122 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
2953 | 122 | s2->s.k = 4; |
2954 | 122 | sappend(s1, s2); |
2955 | 122 | sjeq_avs_cookie->s.jt = s2; |
2956 | | |
2957 | | /* |
2958 | | * Now jump to the code to allocate a register |
2959 | | * into which to save the header length and |
2960 | | * store the length there. (The "jump always" |
2961 | | * instruction needs to have the k field set; |
2962 | | * it's added to the PC, so, as we're jumping |
2963 | | * over a single instruction, it should be 1.) |
2964 | | */ |
2965 | 122 | sjcommon = new_stmt(cstate, JMP(BPF_JA)); |
2966 | 122 | sjcommon->s.k = 1; |
2967 | 122 | sappend(s1, sjcommon); |
2968 | | |
2969 | | /* |
2970 | | * Now for the code that handles the Prism header. |
2971 | | * Just load the length of the Prism header (144) |
2972 | | * into the A register. Have the test for an AVS |
2973 | | * header branch here if we don't have an AVS header. |
2974 | | */ |
2975 | 122 | s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
2976 | 122 | s2->s.k = 144; |
2977 | 122 | sappend(s1, s2); |
2978 | 122 | sjeq_avs_cookie->s.jf = s2; |
2979 | | |
2980 | | /* |
2981 | | * Now allocate a register to hold that value and store |
2982 | | * it. The code for the AVS header will jump here after |
2983 | | * loading the length of the AVS header. |
2984 | | */ |
2985 | 122 | s2 = new_stmt(cstate, BPF_ST); |
2986 | 122 | s2->s.k = cstate->off_linkhdr.reg; |
2987 | 122 | sappend(s1, s2); |
2988 | 122 | sjcommon->s.jf = s2; |
2989 | | |
2990 | | /* |
2991 | | * Now move it into the X register. |
2992 | | */ |
2993 | 122 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
2994 | 122 | sappend(s1, s2); |
2995 | | |
2996 | 122 | return (s1); |
2997 | 122 | } else |
2998 | 149 | return (NULL); |
2999 | 271 | } |
3000 | | |
3001 | | static struct slist * |
3002 | | gen_load_avs_llprefixlen(compiler_state_t *cstate) |
3003 | 109 | { |
3004 | 109 | struct slist *s1, *s2; |
3005 | | |
3006 | | /* |
3007 | | * Generate code to load the length of the AVS header into |
3008 | | * the register assigned to hold that length, if one has been |
3009 | | * assigned. (If one hasn't been assigned, no code we've |
3010 | | * generated uses that prefix, so we don't need to generate any |
3011 | | * code to load it.) |
3012 | | */ |
3013 | 109 | if (cstate->off_linkhdr.reg != -1) { |
3014 | | /* |
3015 | | * The 4 bytes at an offset of 4 from the beginning of |
3016 | | * the AVS header are the length of the AVS header. |
3017 | | * That field is big-endian. |
3018 | | */ |
3019 | 105 | s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); |
3020 | 105 | s1->s.k = 4; |
3021 | | |
3022 | | /* |
3023 | | * Now allocate a register to hold that value and store |
3024 | | * it. |
3025 | | */ |
3026 | 105 | s2 = new_stmt(cstate, BPF_ST); |
3027 | 105 | s2->s.k = cstate->off_linkhdr.reg; |
3028 | 105 | sappend(s1, s2); |
3029 | | |
3030 | | /* |
3031 | | * Now move it into the X register. |
3032 | | */ |
3033 | 105 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3034 | 105 | sappend(s1, s2); |
3035 | | |
3036 | 105 | return (s1); |
3037 | 105 | } else |
3038 | 4 | return (NULL); |
3039 | 109 | } |
3040 | | |
3041 | | static struct slist * |
3042 | | gen_load_radiotap_llprefixlen(compiler_state_t *cstate) |
3043 | 221 | { |
3044 | 221 | struct slist *s1, *s2; |
3045 | | |
3046 | | /* |
3047 | | * Generate code to load the length of the radiotap header into |
3048 | | * the register assigned to hold that length, if one has been |
3049 | | * assigned. (If one hasn't been assigned, no code we've |
3050 | | * generated uses that prefix, so we don't need to generate any |
3051 | | * code to load it.) |
3052 | | */ |
3053 | 221 | if (cstate->off_linkhdr.reg != -1) { |
3054 | | /* |
3055 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
3056 | | * of the radiotap header are the length of the radiotap |
3057 | | * header; unfortunately, it's little-endian, so we have |
3058 | | * to load it a byte at a time and construct the value. |
3059 | | */ |
3060 | | |
3061 | | /* |
3062 | | * Load the high-order byte, at an offset of 3, shift it |
3063 | | * left a byte, and put the result in the X register. |
3064 | | */ |
3065 | 203 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3066 | 203 | s1->s.k = 3; |
3067 | 203 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
3068 | 203 | sappend(s1, s2); |
3069 | 203 | s2->s.k = 8; |
3070 | 203 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3071 | 203 | sappend(s1, s2); |
3072 | | |
3073 | | /* |
3074 | | * Load the next byte, at an offset of 2, and OR the |
3075 | | * value from the X register into it. |
3076 | | */ |
3077 | 203 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3078 | 203 | sappend(s1, s2); |
3079 | 203 | s2->s.k = 2; |
3080 | 203 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
3081 | 203 | sappend(s1, s2); |
3082 | | |
3083 | | /* |
3084 | | * Now allocate a register to hold that value and store |
3085 | | * it. |
3086 | | */ |
3087 | 203 | s2 = new_stmt(cstate, BPF_ST); |
3088 | 203 | s2->s.k = cstate->off_linkhdr.reg; |
3089 | 203 | sappend(s1, s2); |
3090 | | |
3091 | | /* |
3092 | | * Now move it into the X register. |
3093 | | */ |
3094 | 203 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3095 | 203 | sappend(s1, s2); |
3096 | | |
3097 | 203 | return (s1); |
3098 | 203 | } else |
3099 | 18 | return (NULL); |
3100 | 221 | } |
3101 | | |
3102 | | /* |
3103 | | * At the moment we treat PPI as normal Radiotap encoded |
3104 | | * packets. The difference is in the function that generates |
3105 | | * the code at the beginning to compute the header length. |
3106 | | * Since this code generator of PPI supports bare 802.11 |
3107 | | * encapsulation only (i.e. the encapsulated DLT should be |
3108 | | * DLT_IEEE802_11) we generate code to check for this too; |
3109 | | * that's done in finish_parse(). |
3110 | | */ |
3111 | | static struct slist * |
3112 | | gen_load_ppi_llprefixlen(compiler_state_t *cstate) |
3113 | 187 | { |
3114 | 187 | struct slist *s1, *s2; |
3115 | | |
3116 | | /* |
3117 | | * Generate code to load the length of the radiotap header |
3118 | | * into the register assigned to hold that length, if one has |
3119 | | * been assigned. |
3120 | | */ |
3121 | 187 | if (cstate->off_linkhdr.reg != -1) { |
3122 | | /* |
3123 | | * The 2 bytes at offsets of 2 and 3 from the beginning |
3124 | | * of the radiotap header are the length of the radiotap |
3125 | | * header; unfortunately, it's little-endian, so we have |
3126 | | * to load it a byte at a time and construct the value. |
3127 | | */ |
3128 | | |
3129 | | /* |
3130 | | * Load the high-order byte, at an offset of 3, shift it |
3131 | | * left a byte, and put the result in the X register. |
3132 | | */ |
3133 | 139 | s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3134 | 139 | s1->s.k = 3; |
3135 | 139 | s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); |
3136 | 139 | sappend(s1, s2); |
3137 | 139 | s2->s.k = 8; |
3138 | 139 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3139 | 139 | sappend(s1, s2); |
3140 | | |
3141 | | /* |
3142 | | * Load the next byte, at an offset of 2, and OR the |
3143 | | * value from the X register into it. |
3144 | | */ |
3145 | 139 | s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
3146 | 139 | sappend(s1, s2); |
3147 | 139 | s2->s.k = 2; |
3148 | 139 | s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); |
3149 | 139 | sappend(s1, s2); |
3150 | | |
3151 | | /* |
3152 | | * Now allocate a register to hold that value and store |
3153 | | * it. |
3154 | | */ |
3155 | 139 | s2 = new_stmt(cstate, BPF_ST); |
3156 | 139 | s2->s.k = cstate->off_linkhdr.reg; |
3157 | 139 | sappend(s1, s2); |
3158 | | |
3159 | | /* |
3160 | | * Now move it into the X register. |
3161 | | */ |
3162 | 139 | s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
3163 | 139 | sappend(s1, s2); |
3164 | | |
3165 | 139 | return (s1); |
3166 | 139 | } else |
3167 | 48 | return (NULL); |
3168 | 187 | } |
3169 | | |
3170 | | /* |
3171 | | * Load a value relative to the beginning of the link-layer header after the 802.11 |
3172 | | * header, i.e. LLC_SNAP. |
3173 | | * The link-layer header doesn't necessarily begin at the beginning |
3174 | | * of the packet data; there might be a variable-length prefix containing |
3175 | | * radio information. |
3176 | | */ |
3177 | | static struct slist * |
3178 | | gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext) |
3179 | 920 | { |
3180 | 920 | struct slist *s2; |
3181 | 920 | struct slist *sjset_data_frame_1; |
3182 | 920 | struct slist *sjset_data_frame_2; |
3183 | 920 | struct slist *sjset_qos; |
3184 | 920 | struct slist *sjset_radiotap_flags_present; |
3185 | 920 | struct slist *sjset_radiotap_ext_present; |
3186 | 920 | struct slist *sjset_radiotap_tsft_present; |
3187 | 920 | struct slist *sjset_tsft_datapad, *sjset_notsft_datapad; |
3188 | 920 | struct slist *s_roundup; |
3189 | | |
3190 | 920 | if (cstate->off_linkpl.reg == -1) { |
3191 | | /* |
3192 | | * No register has been assigned to the offset of |
3193 | | * the link-layer payload, which means nobody needs |
3194 | | * it; don't bother computing it - just return |
3195 | | * what we already have. |
3196 | | */ |
3197 | 392 | return (s); |
3198 | 392 | } |
3199 | | |
3200 | | /* |
3201 | | * This code is not compatible with the optimizer, as |
3202 | | * we are generating jmp instructions within a normal |
3203 | | * slist of instructions |
3204 | | */ |
3205 | 528 | cstate->no_optimize = 1; |
3206 | | |
3207 | | /* |
3208 | | * If "s" is non-null, it has code to arrange that the X register |
3209 | | * contains the length of the prefix preceding the link-layer |
3210 | | * header. |
3211 | | * |
3212 | | * Otherwise, the length of the prefix preceding the link-layer |
3213 | | * header is "off_outermostlinkhdr.constant_part". |
3214 | | */ |
3215 | 528 | if (s == NULL) { |
3216 | | /* |
3217 | | * There is no variable-length header preceding the |
3218 | | * link-layer header. |
3219 | | * |
3220 | | * Load the length of the fixed-length prefix preceding |
3221 | | * the link-layer header (if any) into the X register, |
3222 | | * and store it in the cstate->off_linkpl.reg register. |
3223 | | * That length is off_outermostlinkhdr.constant_part. |
3224 | | */ |
3225 | 108 | s = new_stmt(cstate, BPF_LDX|BPF_IMM); |
3226 | 108 | s->s.k = cstate->off_outermostlinkhdr.constant_part; |
3227 | 108 | } |
3228 | | |
3229 | | /* |
3230 | | * The X register contains the offset of the beginning of the |
3231 | | * link-layer header; add 24, which is the minimum length |
3232 | | * of the MAC header for a data frame, to that, and store it |
3233 | | * in cstate->off_linkpl.reg, and then load the Frame Control field, |
3234 | | * which is at the offset in the X register, with an indexed load. |
3235 | | */ |
3236 | 528 | s2 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
3237 | 528 | sappend(s, s2); |
3238 | 528 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
3239 | 528 | s2->s.k = 24; |
3240 | 528 | sappend(s, s2); |
3241 | 528 | s2 = new_stmt(cstate, BPF_ST); |
3242 | 528 | s2->s.k = cstate->off_linkpl.reg; |
3243 | 528 | sappend(s, s2); |
3244 | | |
3245 | 528 | s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
3246 | 528 | s2->s.k = 0; |
3247 | 528 | sappend(s, s2); |
3248 | | |
3249 | | /* |
3250 | | * Check the Frame Control field to see if this is a data frame; |
3251 | | * a data frame has the 0x08 bit (b3) in that field set and the |
3252 | | * 0x04 bit (b2) clear. |
3253 | | */ |
3254 | 528 | sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET)); |
3255 | 528 | sjset_data_frame_1->s.k = IEEE80211_FC0_TYPE_DATA; |
3256 | 528 | sappend(s, sjset_data_frame_1); |
3257 | | |
3258 | | /* |
3259 | | * If b3 is set, test b2, otherwise go to the first statement of |
3260 | | * the rest of the program. |
3261 | | */ |
3262 | 528 | sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET)); |
3263 | 528 | sjset_data_frame_2->s.k = IEEE80211_FC0_TYPE_CTL; |
3264 | 528 | sappend(s, sjset_data_frame_2); |
3265 | 528 | sjset_data_frame_1->s.jf = snext; |
3266 | | |
3267 | | /* |
3268 | | * If b2 is not set, this is a data frame; test the QoS bit. |
3269 | | * Otherwise, go to the first statement of the rest of the |
3270 | | * program. |
3271 | | */ |
3272 | 528 | sjset_data_frame_2->s.jt = snext; |
3273 | 528 | sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET)); |
3274 | 528 | sjset_qos->s.k = IEEE80211_FC0_SUBTYPE_QOS; |
3275 | 528 | sappend(s, sjset_qos); |
3276 | | |
3277 | | /* |
3278 | | * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS |
3279 | | * field. |
3280 | | * Otherwise, go to the first statement of the rest of the |
3281 | | * program. |
3282 | | */ |
3283 | 528 | sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
3284 | 528 | s2->s.k = cstate->off_linkpl.reg; |
3285 | 528 | sappend(s, s2); |
3286 | 528 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
3287 | 528 | s2->s.k = 2; |
3288 | 528 | sappend(s, s2); |
3289 | 528 | s2 = new_stmt(cstate, BPF_ST); |
3290 | 528 | s2->s.k = cstate->off_linkpl.reg; |
3291 | 528 | sappend(s, s2); |
3292 | | |
3293 | | /* |
3294 | | * If we have a radiotap header, look at it to see whether |
3295 | | * there's Atheros padding between the MAC-layer header |
3296 | | * and the payload. |
3297 | | * |
3298 | | * Note: all of the fields in the radiotap header are |
3299 | | * little-endian, so we byte-swap all of the values |
3300 | | * we test against, as they will be loaded as big-endian |
3301 | | * values. |
3302 | | * |
3303 | | * XXX - in the general case, we would have to scan through |
3304 | | * *all* the presence bits, if there's more than one word of |
3305 | | * presence bits. That would require a loop, meaning that |
3306 | | * we wouldn't be able to run the filter in the kernel. |
3307 | | * |
3308 | | * We assume here that the Atheros adapters that insert the |
3309 | | * annoying padding don't have multiple antennae and therefore |
3310 | | * do not generate radiotap headers with multiple presence words. |
3311 | | */ |
3312 | 528 | if (cstate->linktype == DLT_IEEE802_11_RADIO) { |
3313 | | /* |
3314 | | * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set |
3315 | | * in the first presence flag word? |
3316 | | */ |
3317 | 151 | sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W); |
3318 | 151 | s2->s.k = 4; |
3319 | 151 | sappend(s, s2); |
3320 | | |
3321 | 151 | sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET)); |
3322 | 151 | sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002); |
3323 | 151 | sappend(s, sjset_radiotap_flags_present); |
3324 | | |
3325 | | /* |
3326 | | * If not, skip all of this. |
3327 | | */ |
3328 | 151 | sjset_radiotap_flags_present->s.jf = snext; |
3329 | | |
3330 | | /* |
3331 | | * Otherwise, is the "extension" bit set in that word? |
3332 | | */ |
3333 | 151 | sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET)); |
3334 | 151 | sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000); |
3335 | 151 | sappend(s, sjset_radiotap_ext_present); |
3336 | 151 | sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present; |
3337 | | |
3338 | | /* |
3339 | | * If so, skip all of this. |
3340 | | */ |
3341 | 151 | sjset_radiotap_ext_present->s.jt = snext; |
3342 | | |
3343 | | /* |
3344 | | * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set? |
3345 | | */ |
3346 | 151 | sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET)); |
3347 | 151 | sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001); |
3348 | 151 | sappend(s, sjset_radiotap_tsft_present); |
3349 | 151 | sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present; |
3350 | | |
3351 | | /* |
3352 | | * If IEEE80211_RADIOTAP_TSFT is set, the flags field is |
3353 | | * at an offset of 16 from the beginning of the raw packet |
3354 | | * data (8 bytes for the radiotap header and 8 bytes for |
3355 | | * the TSFT field). |
3356 | | * |
3357 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
3358 | | * is set. |
3359 | | */ |
3360 | 151 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
3361 | 151 | s2->s.k = 16; |
3362 | 151 | sappend(s, s2); |
3363 | 151 | sjset_radiotap_tsft_present->s.jt = s2; |
3364 | | |
3365 | 151 | sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); |
3366 | 151 | sjset_tsft_datapad->s.k = 0x20; |
3367 | 151 | sappend(s, sjset_tsft_datapad); |
3368 | | |
3369 | | /* |
3370 | | * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is |
3371 | | * at an offset of 8 from the beginning of the raw packet |
3372 | | * data (8 bytes for the radiotap header). |
3373 | | * |
3374 | | * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) |
3375 | | * is set. |
3376 | | */ |
3377 | 151 | s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
3378 | 151 | s2->s.k = 8; |
3379 | 151 | sappend(s, s2); |
3380 | 151 | sjset_radiotap_tsft_present->s.jf = s2; |
3381 | | |
3382 | 151 | sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); |
3383 | 151 | sjset_notsft_datapad->s.k = 0x20; |
3384 | 151 | sappend(s, sjset_notsft_datapad); |
3385 | | |
3386 | | /* |
3387 | | * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is |
3388 | | * set, round the length of the 802.11 header to |
3389 | | * a multiple of 4. Do that by adding 3 and then |
3390 | | * dividing by and multiplying by 4, which we do by |
3391 | | * ANDing with ~3. |
3392 | | */ |
3393 | 151 | s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM); |
3394 | 151 | s_roundup->s.k = cstate->off_linkpl.reg; |
3395 | 151 | sappend(s, s_roundup); |
3396 | 151 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
3397 | 151 | s2->s.k = 3; |
3398 | 151 | sappend(s, s2); |
3399 | 151 | s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM); |
3400 | 151 | s2->s.k = (bpf_u_int32)~3; |
3401 | 151 | sappend(s, s2); |
3402 | 151 | s2 = new_stmt(cstate, BPF_ST); |
3403 | 151 | s2->s.k = cstate->off_linkpl.reg; |
3404 | 151 | sappend(s, s2); |
3405 | | |
3406 | 151 | sjset_tsft_datapad->s.jt = s_roundup; |
3407 | 151 | sjset_tsft_datapad->s.jf = snext; |
3408 | 151 | sjset_notsft_datapad->s.jt = s_roundup; |
3409 | 151 | sjset_notsft_datapad->s.jf = snext; |
3410 | 151 | } else |
3411 | 377 | sjset_qos->s.jf = snext; |
3412 | | |
3413 | 528 | return s; |
3414 | 920 | } |
3415 | | |
3416 | | static void |
3417 | | insert_compute_vloffsets(compiler_state_t *cstate, struct block *b) |
3418 | 5.17k | { |
3419 | 5.17k | struct slist *s; |
3420 | | |
3421 | | /* There is an implicit dependency between the link |
3422 | | * payload and link header since the payload computation |
3423 | | * includes the variable part of the header. Therefore, |
3424 | | * if nobody else has allocated a register for the link |
3425 | | * header and we need it, do it now. */ |
3426 | 5.17k | if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable && |
3427 | 5.17k | cstate->off_linkhdr.reg == -1) |
3428 | 3 | cstate->off_linkhdr.reg = alloc_reg(cstate); |
3429 | | |
3430 | | /* |
3431 | | * For link-layer types that have a variable-length header |
3432 | | * preceding the link-layer header, generate code to load |
3433 | | * the offset of the link-layer header into the register |
3434 | | * assigned to that offset, if any. |
3435 | | * |
3436 | | * XXX - this, and the next switch statement, won't handle |
3437 | | * encapsulation of 802.11 or 802.11+radio information in |
3438 | | * some other protocol stack. That's significantly more |
3439 | | * complicated. |
3440 | | */ |
3441 | 5.17k | switch (cstate->outermostlinktype) { |
3442 | | |
3443 | 271 | case DLT_PRISM_HEADER: |
3444 | 271 | s = gen_load_prism_llprefixlen(cstate); |
3445 | 271 | break; |
3446 | | |
3447 | 109 | case DLT_IEEE802_11_RADIO_AVS: |
3448 | 109 | s = gen_load_avs_llprefixlen(cstate); |
3449 | 109 | break; |
3450 | | |
3451 | 221 | case DLT_IEEE802_11_RADIO: |
3452 | 221 | s = gen_load_radiotap_llprefixlen(cstate); |
3453 | 221 | break; |
3454 | | |
3455 | 187 | case DLT_PPI: |
3456 | 187 | s = gen_load_ppi_llprefixlen(cstate); |
3457 | 187 | break; |
3458 | | |
3459 | 4.38k | default: |
3460 | 4.38k | s = NULL; |
3461 | 4.38k | break; |
3462 | 5.17k | } |
3463 | | |
3464 | | /* |
3465 | | * For link-layer types that have a variable-length link-layer |
3466 | | * header, generate code to load the offset of the link-layer |
3467 | | * payload into the register assigned to that offset, if any. |
3468 | | */ |
3469 | 5.17k | switch (cstate->outermostlinktype) { |
3470 | | |
3471 | 132 | case DLT_IEEE802_11: |
3472 | 403 | case DLT_PRISM_HEADER: |
3473 | 512 | case DLT_IEEE802_11_RADIO_AVS: |
3474 | 733 | case DLT_IEEE802_11_RADIO: |
3475 | 920 | case DLT_PPI: |
3476 | 920 | s = gen_load_802_11_header_len(cstate, s, b->stmts); |
3477 | 920 | break; |
3478 | | |
3479 | 337 | case DLT_PFLOG: |
3480 | 337 | s = gen_load_pflog_llprefixlen(cstate); |
3481 | 337 | break; |
3482 | 5.17k | } |
3483 | | |
3484 | | /* |
3485 | | * If there is no initialization yet and we need variable |
3486 | | * length offsets for VLAN, initialize them to zero |
3487 | | */ |
3488 | 5.17k | if (s == NULL && cstate->is_vlan_vloffset) { |
3489 | 0 | struct slist *s2; |
3490 | |
|
3491 | 0 | if (cstate->off_linkpl.reg == -1) |
3492 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
3493 | 0 | if (cstate->off_linktype.reg == -1) |
3494 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
3495 | |
|
3496 | 0 | s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); |
3497 | 0 | s->s.k = 0; |
3498 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3499 | 0 | s2->s.k = cstate->off_linkpl.reg; |
3500 | 0 | sappend(s, s2); |
3501 | 0 | s2 = new_stmt(cstate, BPF_ST); |
3502 | 0 | s2->s.k = cstate->off_linktype.reg; |
3503 | 0 | sappend(s, s2); |
3504 | 0 | } |
3505 | | |
3506 | | /* |
3507 | | * If we have any offset-loading code, append all the |
3508 | | * existing statements in the block to those statements, |
3509 | | * and make the resulting list the list of statements |
3510 | | * for the block. |
3511 | | */ |
3512 | 5.17k | if (s != NULL) { |
3513 | 937 | sappend(s, b->stmts); |
3514 | 937 | b->stmts = s; |
3515 | 937 | } |
3516 | 5.17k | } |
3517 | | |
3518 | | /* |
3519 | | * Take an absolute offset, and: |
3520 | | * |
3521 | | * if it has no variable part, return NULL; |
3522 | | * |
3523 | | * if it has a variable part, generate code to load the register |
3524 | | * containing that variable part into the X register, returning |
3525 | | * a pointer to that code - if no register for that offset has |
3526 | | * been allocated, allocate it first. |
3527 | | * |
3528 | | * (The code to set that register will be generated later, but will |
3529 | | * be placed earlier in the code sequence.) |
3530 | | */ |
3531 | | static struct slist * |
3532 | | gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off) |
3533 | 252k | { |
3534 | 252k | struct slist *s; |
3535 | | |
3536 | 252k | if (off->is_variable) { |
3537 | 100k | if (off->reg == -1) { |
3538 | | /* |
3539 | | * We haven't yet assigned a register for the |
3540 | | * variable part of the offset of the link-layer |
3541 | | * header; allocate one. |
3542 | | */ |
3543 | 1.86k | off->reg = alloc_reg(cstate); |
3544 | 1.86k | } |
3545 | | |
3546 | | /* |
3547 | | * Load the register containing the variable part of the |
3548 | | * offset of the link-layer header into the X register. |
3549 | | */ |
3550 | 100k | s = new_stmt(cstate, BPF_LDX|BPF_MEM); |
3551 | 100k | s->s.k = off->reg; |
3552 | 100k | return s; |
3553 | 152k | } else { |
3554 | | /* |
3555 | | * That offset isn't variable, there's no variable part, |
3556 | | * so we don't need to generate any code. |
3557 | | */ |
3558 | 152k | return NULL; |
3559 | 152k | } |
3560 | 252k | } |
3561 | | |
3562 | | /* |
3563 | | * Map an Ethernet type to the equivalent PPP type. |
3564 | | */ |
3565 | | static uint16_t |
3566 | | ethertype_to_ppptype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
3567 | 5.18k | { |
3568 | 5.18k | switch (ll_proto) { |
3569 | | |
3570 | 825 | case ETHERTYPE_IP: |
3571 | 825 | return PPP_IP; |
3572 | | |
3573 | 1.01k | case ETHERTYPE_IPV6: |
3574 | 1.01k | return PPP_IPV6; |
3575 | | |
3576 | 123 | case ETHERTYPE_DN: |
3577 | 123 | return PPP_DECNET; |
3578 | | |
3579 | 70 | case ETHERTYPE_ATALK: |
3580 | 70 | return PPP_APPLE; |
3581 | | |
3582 | 20 | case ETHERTYPE_NS: |
3583 | 20 | return PPP_NS; |
3584 | | |
3585 | 669 | case LLCSAP_ISONS: |
3586 | 669 | return PPP_OSI; |
3587 | | |
3588 | 155 | case LLCSAP_8021D: |
3589 | | /* |
3590 | | * I'm assuming the "Bridging PDU"s that go |
3591 | | * over PPP are Spanning Tree Protocol |
3592 | | * Bridging PDUs. |
3593 | | */ |
3594 | 155 | return PPP_BRPDU; |
3595 | | |
3596 | 139 | case LLCSAP_IPX: |
3597 | 139 | return PPP_IPX; |
3598 | 5.18k | } |
3599 | 2.16k | assert_maxval(cstate, "PPP protocol", ll_proto, UINT16_MAX); |
3600 | 2.16k | return (uint16_t)ll_proto; |
3601 | 5.18k | } |
3602 | | |
3603 | | /* |
3604 | | * Generate any tests that, for encapsulation of a link-layer packet |
3605 | | * inside another protocol stack, need to be done to check for those |
3606 | | * link-layer packets (and that haven't already been done by a check |
3607 | | * for that encapsulation). |
3608 | | */ |
3609 | | static struct block * |
3610 | | gen_prevlinkhdr_check(compiler_state_t *cstate) |
3611 | 5.74k | { |
3612 | 5.74k | if (cstate->is_encap) |
3613 | 44 | return gen_encap_ll_check(cstate); |
3614 | | |
3615 | 5.70k | switch (cstate->prevlinktype) { |
3616 | | |
3617 | 1.96k | case DLT_SUNATM: |
3618 | | /* |
3619 | | * This is LANE-encapsulated Ethernet; check that the LANE |
3620 | | * packet doesn't begin with an LE Control marker, i.e. |
3621 | | * that it's data, not a control message. |
3622 | | * |
3623 | | * (We've already generated a test for LANE.) |
3624 | | */ |
3625 | 1.96k | return gen_cmp_ne(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00); |
3626 | | |
3627 | 3.73k | default: |
3628 | | /* |
3629 | | * No such tests are necessary. |
3630 | | */ |
3631 | 3.73k | return NULL; |
3632 | 5.70k | } |
3633 | | /*NOTREACHED*/ |
3634 | 5.70k | } |
3635 | | |
3636 | | /* |
3637 | | * The three different values we should check for when checking for an |
3638 | | * IPv6 packet with DLT_NULL. |
3639 | | */ |
3640 | 2.16k | #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */ |
3641 | 2.16k | #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */ |
3642 | 2.16k | #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */ |
3643 | | |
3644 | | /* |
3645 | | * Generate code to match a particular packet type by matching the |
3646 | | * link-layer type field or fields in the 802.2 LLC header. |
3647 | | * |
3648 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
3649 | | * value, if <= ETHERMTU. |
3650 | | */ |
3651 | | static struct block * |
3652 | | gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
3653 | 63.1k | { |
3654 | 63.1k | struct block *b0, *b1, *b2; |
3655 | | |
3656 | | /* are we checking MPLS-encapsulated packets? */ |
3657 | 63.1k | if (cstate->label_stack_depth > 0) |
3658 | 138 | return gen_mpls_linktype(cstate, ll_proto); |
3659 | | |
3660 | 62.9k | switch (cstate->linktype) { |
3661 | | |
3662 | 6.57k | case DLT_EN10MB: |
3663 | 7.08k | case DLT_NETANALYZER: |
3664 | 7.71k | case DLT_NETANALYZER_TRANSPARENT: |
3665 | | /* Geneve has an EtherType regardless of whether there is an |
3666 | | * L2 header. VXLAN always has an EtherType. */ |
3667 | 7.71k | if (!cstate->is_encap) |
3668 | 5.57k | b0 = gen_prevlinkhdr_check(cstate); |
3669 | 2.13k | else |
3670 | 2.13k | b0 = NULL; |
3671 | | |
3672 | 7.71k | b1 = gen_ether_linktype(cstate, ll_proto); |
3673 | 7.71k | if (b0 != NULL) |
3674 | 1.95k | gen_and(b0, b1); |
3675 | 7.71k | return b1; |
3676 | | /*NOTREACHED*/ |
3677 | | |
3678 | 290 | case DLT_C_HDLC: |
3679 | 745 | case DLT_HDLC: |
3680 | 745 | assert_maxval(cstate, "HDLC protocol", ll_proto, UINT16_MAX); |
3681 | 745 | switch (ll_proto) { |
3682 | | |
3683 | 10 | case LLCSAP_ISONS: |
3684 | 10 | ll_proto = (ll_proto << 8 | LLCSAP_ISONS); |
3685 | | /* fall through */ |
3686 | | |
3687 | 729 | default: |
3688 | 729 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
3689 | | /*NOTREACHED*/ |
3690 | 745 | } |
3691 | | |
3692 | 4.66k | case DLT_IEEE802_11: |
3693 | 9.26k | case DLT_PRISM_HEADER: |
3694 | 10.4k | case DLT_IEEE802_11_RADIO_AVS: |
3695 | 16.3k | case DLT_IEEE802_11_RADIO: |
3696 | 18.7k | case DLT_PPI: |
3697 | | /* |
3698 | | * Check that we have a data frame. |
3699 | | */ |
3700 | 18.7k | b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, |
3701 | 18.7k | IEEE80211_FC0_TYPE_DATA, |
3702 | 18.7k | IEEE80211_FC0_TYPE_MASK); |
3703 | | |
3704 | | /* |
3705 | | * Now check for the specified link-layer type. |
3706 | | */ |
3707 | 18.7k | b1 = gen_llc_linktype(cstate, ll_proto); |
3708 | 18.7k | gen_and(b0, b1); |
3709 | 18.7k | return b1; |
3710 | | /*NOTREACHED*/ |
3711 | | |
3712 | 685 | case DLT_FDDI: |
3713 | | /* |
3714 | | * XXX - check for LLC frames. |
3715 | | */ |
3716 | 685 | return gen_llc_linktype(cstate, ll_proto); |
3717 | | /*NOTREACHED*/ |
3718 | | |
3719 | 376 | case DLT_IEEE802: |
3720 | | /* |
3721 | | * XXX - check for LLC PDUs, as per IEEE 802.5. |
3722 | | */ |
3723 | 376 | return gen_llc_linktype(cstate, ll_proto); |
3724 | | /*NOTREACHED*/ |
3725 | | |
3726 | 389 | case DLT_ATM_RFC1483: |
3727 | 582 | case DLT_ATM_CLIP: |
3728 | 1.10k | case DLT_IP_OVER_FC: |
3729 | 1.10k | return gen_llc_linktype(cstate, ll_proto); |
3730 | | /*NOTREACHED*/ |
3731 | | |
3732 | 721 | case DLT_SUNATM: |
3733 | | /* |
3734 | | * Check for an LLC-encapsulated version of this protocol; |
3735 | | * if we were checking for LANE, linktype would no longer |
3736 | | * be DLT_SUNATM. |
3737 | | * |
3738 | | * Check for LLC encapsulation and then check the protocol. |
3739 | | */ |
3740 | 721 | b0 = gen_atm_prototype(cstate, PT_LLC); |
3741 | 721 | b1 = gen_llc_linktype(cstate, ll_proto); |
3742 | 721 | gen_and(b0, b1); |
3743 | 721 | return b1; |
3744 | | /*NOTREACHED*/ |
3745 | | |
3746 | 861 | case DLT_LINUX_SLL: |
3747 | 861 | return gen_linux_sll_linktype(cstate, ll_proto); |
3748 | | /*NOTREACHED*/ |
3749 | | |
3750 | 690 | case DLT_SLIP: |
3751 | 1.49k | case DLT_SLIP_BSDOS: |
3752 | 2.30k | case DLT_RAW: |
3753 | | /* |
3754 | | * These types don't provide any type field; packets |
3755 | | * are always IPv4 or IPv6. |
3756 | | * |
3757 | | * XXX - for IPv4, check for a version number of 4, and, |
3758 | | * for IPv6, check for a version number of 6? |
3759 | | */ |
3760 | 2.30k | switch (ll_proto) { |
3761 | | |
3762 | 839 | case ETHERTYPE_IP: |
3763 | | /* Check for a version number of 4. */ |
3764 | 839 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0); |
3765 | | |
3766 | 368 | case ETHERTYPE_IPV6: |
3767 | | /* Check for a version number of 6. */ |
3768 | 368 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0); |
3769 | | |
3770 | 1.09k | default: |
3771 | 1.09k | return gen_false(cstate); /* always false */ |
3772 | 2.30k | } |
3773 | | /*NOTREACHED*/ |
3774 | | |
3775 | 664 | case DLT_IPV4: |
3776 | | /* |
3777 | | * Raw IPv4, so no type field. |
3778 | | */ |
3779 | 664 | if (ll_proto == ETHERTYPE_IP) |
3780 | 256 | return gen_true(cstate); /* always true */ |
3781 | | |
3782 | | /* Checking for something other than IPv4; always false */ |
3783 | 408 | return gen_false(cstate); |
3784 | | /*NOTREACHED*/ |
3785 | | |
3786 | 499 | case DLT_IPV6: |
3787 | | /* |
3788 | | * Raw IPv6, so no type field. |
3789 | | */ |
3790 | 499 | if (ll_proto == ETHERTYPE_IPV6) |
3791 | 168 | return gen_true(cstate); /* always true */ |
3792 | | |
3793 | | /* Checking for something other than IPv6; always false */ |
3794 | 331 | return gen_false(cstate); |
3795 | | /*NOTREACHED*/ |
3796 | | |
3797 | 1.21k | case DLT_PPP: |
3798 | 1.52k | case DLT_PPP_PPPD: |
3799 | 2.39k | case DLT_PPP_SERIAL: |
3800 | 2.96k | case DLT_PPP_ETHER: |
3801 | | /* |
3802 | | * We use Ethernet protocol types inside libpcap; |
3803 | | * map them to the corresponding PPP protocol types. |
3804 | | */ |
3805 | 2.96k | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
3806 | 2.96k | ethertype_to_ppptype(cstate, ll_proto)); |
3807 | | /*NOTREACHED*/ |
3808 | | |
3809 | 3.85k | case DLT_PPP_BSDOS: |
3810 | | /* |
3811 | | * We use Ethernet protocol types inside libpcap; |
3812 | | * map them to the corresponding PPP protocol types. |
3813 | | */ |
3814 | 3.85k | switch (ll_proto) { |
3815 | | |
3816 | 1.64k | case ETHERTYPE_IP: |
3817 | | /* |
3818 | | * Also check for Van Jacobson-compressed IP. |
3819 | | * XXX - do this for other forms of PPP? |
3820 | | */ |
3821 | 1.64k | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP); |
3822 | 1.64k | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC); |
3823 | 1.64k | gen_or(b0, b1); |
3824 | 1.64k | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC); |
3825 | 1.64k | gen_or(b1, b0); |
3826 | 1.64k | return b0; |
3827 | | |
3828 | 2.21k | default: |
3829 | 2.21k | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, |
3830 | 2.21k | ethertype_to_ppptype(cstate, ll_proto)); |
3831 | 3.85k | } |
3832 | | /*NOTREACHED*/ |
3833 | | |
3834 | 8.54k | case DLT_NULL: |
3835 | 8.75k | case DLT_LOOP: |
3836 | 9.00k | case DLT_ENC: |
3837 | 9.00k | switch (ll_proto) { |
3838 | | |
3839 | 3.27k | case ETHERTYPE_IP: |
3840 | 3.27k | return (gen_loopback_linktype(cstate, AF_INET)); |
3841 | | |
3842 | 2.16k | case ETHERTYPE_IPV6: |
3843 | | /* |
3844 | | * AF_ values may, unfortunately, be platform- |
3845 | | * dependent; AF_INET isn't, because everybody |
3846 | | * used 4.2BSD's value, but AF_INET6 is, because |
3847 | | * 4.2BSD didn't have a value for it (given that |
3848 | | * IPv6 didn't exist back in the early 1980's), |
3849 | | * and they all picked their own values. |
3850 | | * |
3851 | | * This means that, if we're reading from a |
3852 | | * savefile, we need to check for all the |
3853 | | * possible values. |
3854 | | * |
3855 | | * If we're doing a live capture, we only need |
3856 | | * to check for this platform's value; however, |
3857 | | * Npcap uses 24, which isn't Windows's AF_INET6 |
3858 | | * value. (Given the multiple different values, |
3859 | | * programs that read pcap files shouldn't be |
3860 | | * checking for their platform's AF_INET6 value |
3861 | | * anyway, they should check for all of the |
3862 | | * possible values. and they might as well do |
3863 | | * that even for live captures.) |
3864 | | */ |
3865 | 2.16k | if (cstate->bpf_pcap->rfile != NULL) { |
3866 | | /* |
3867 | | * Savefile - check for all three |
3868 | | * possible IPv6 values. |
3869 | | */ |
3870 | 2.16k | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD); |
3871 | 2.16k | b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD); |
3872 | 2.16k | gen_or(b0, b1); |
3873 | 2.16k | b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN); |
3874 | 2.16k | gen_or(b0, b1); |
3875 | 2.16k | return (b1); |
3876 | 2.16k | } else { |
3877 | | /* |
3878 | | * Live capture, so we only need to |
3879 | | * check for the value used on this |
3880 | | * platform. |
3881 | | */ |
3882 | | #ifdef _WIN32 |
3883 | | /* |
3884 | | * Npcap doesn't use Windows's AF_INET6, |
3885 | | * as that collides with AF_IPX on |
3886 | | * some BSDs (both have the value 23). |
3887 | | * Instead, it uses 24. |
3888 | | */ |
3889 | | return (gen_loopback_linktype(cstate, 24)); |
3890 | | #else /* _WIN32 */ |
3891 | 0 | #ifdef AF_INET6 |
3892 | 0 | return (gen_loopback_linktype(cstate, AF_INET6)); |
3893 | | #else /* AF_INET6 */ |
3894 | | /* |
3895 | | * I guess this platform doesn't support |
3896 | | * IPv6, so we just reject all packets. |
3897 | | */ |
3898 | | return gen_false(cstate); |
3899 | | #endif /* AF_INET6 */ |
3900 | 0 | #endif /* _WIN32 */ |
3901 | 0 | } |
3902 | | |
3903 | 3.55k | default: |
3904 | | /* |
3905 | | * Not a type on which we support filtering. |
3906 | | * XXX - support those that have AF_ values |
3907 | | * #defined on this platform, at least? |
3908 | | */ |
3909 | 3.55k | return gen_false(cstate); |
3910 | 9.00k | } |
3911 | | |
3912 | 3.61k | case DLT_PFLOG: |
3913 | | /* |
3914 | | * af field is host byte order in contrast to the rest of |
3915 | | * the packet. |
3916 | | */ |
3917 | 3.61k | if (ll_proto == ETHERTYPE_IP) |
3918 | 1.68k | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3919 | 1.68k | BPF_B, AF_INET)); |
3920 | 1.93k | else if (ll_proto == ETHERTYPE_IPV6) |
3921 | 1.18k | return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), |
3922 | 1.18k | BPF_B, AF_INET6)); |
3923 | 753 | else |
3924 | 753 | return gen_false(cstate); |
3925 | | /*NOTREACHED*/ |
3926 | | |
3927 | 1.23k | case DLT_ARCNET: |
3928 | 1.77k | case DLT_ARCNET_LINUX: |
3929 | | /* |
3930 | | * XXX should we check for first fragment if the protocol |
3931 | | * uses PHDS? |
3932 | | */ |
3933 | 1.77k | switch (ll_proto) { |
3934 | | |
3935 | 330 | default: |
3936 | 330 | return gen_false(cstate); |
3937 | | |
3938 | 307 | case ETHERTYPE_IPV6: |
3939 | 307 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3940 | 307 | ARCTYPE_INET6)); |
3941 | | |
3942 | 617 | case ETHERTYPE_IP: |
3943 | 617 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3944 | 617 | ARCTYPE_IP); |
3945 | 617 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3946 | 617 | ARCTYPE_IP_OLD); |
3947 | 617 | gen_or(b0, b1); |
3948 | 617 | return (b1); |
3949 | | |
3950 | 255 | case ETHERTYPE_ARP: |
3951 | 255 | b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3952 | 255 | ARCTYPE_ARP); |
3953 | 255 | b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3954 | 255 | ARCTYPE_ARP_OLD); |
3955 | 255 | gen_or(b0, b1); |
3956 | 255 | return (b1); |
3957 | | |
3958 | 226 | case ETHERTYPE_REVARP: |
3959 | 226 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3960 | 226 | ARCTYPE_REVARP)); |
3961 | | |
3962 | 35 | case ETHERTYPE_ATALK: |
3963 | 35 | return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, |
3964 | 35 | ARCTYPE_ATALK)); |
3965 | 1.77k | } |
3966 | | /*NOTREACHED*/ |
3967 | | |
3968 | 98 | case DLT_LTALK: |
3969 | 98 | switch (ll_proto) { |
3970 | 34 | case ETHERTYPE_ATALK: |
3971 | 34 | return gen_true(cstate); |
3972 | 64 | default: |
3973 | 64 | return gen_false(cstate); |
3974 | 98 | } |
3975 | | /*NOTREACHED*/ |
3976 | | |
3977 | 603 | case DLT_FRELAY: |
3978 | | /* |
3979 | | * XXX - assumes a 2-byte Frame Relay header with |
3980 | | * DLCI and flags. What if the address is longer? |
3981 | | */ |
3982 | 603 | switch (ll_proto) { |
3983 | | |
3984 | 223 | case ETHERTYPE_IP: |
3985 | | /* |
3986 | | * Check for the special NLPID for IP. |
3987 | | */ |
3988 | 223 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc); |
3989 | | |
3990 | 231 | case ETHERTYPE_IPV6: |
3991 | | /* |
3992 | | * Check for the special NLPID for IPv6. |
3993 | | */ |
3994 | 231 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e); |
3995 | | |
3996 | 19 | case LLCSAP_ISONS: |
3997 | | /* |
3998 | | * Check for several OSI protocols. |
3999 | | * |
4000 | | * Frame Relay packets typically have an OSI |
4001 | | * NLPID at the beginning; we check for each |
4002 | | * of them. |
4003 | | * |
4004 | | * What we check for is the NLPID and a frame |
4005 | | * control field of UI, i.e. 0x03 followed |
4006 | | * by the NLPID. |
4007 | | */ |
4008 | 19 | b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP); |
4009 | 19 | b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS); |
4010 | 19 | b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS); |
4011 | 19 | gen_or(b1, b2); |
4012 | 19 | gen_or(b0, b2); |
4013 | 19 | return b2; |
4014 | | |
4015 | 130 | default: |
4016 | 130 | return gen_false(cstate); |
4017 | 603 | } |
4018 | | /*NOTREACHED*/ |
4019 | | |
4020 | 1 | case DLT_MFR: |
4021 | 1 | break; // not implemented |
4022 | | |
4023 | 78 | case DLT_JUNIPER_MFR: |
4024 | 394 | case DLT_JUNIPER_MLFR: |
4025 | 576 | case DLT_JUNIPER_MLPPP: |
4026 | 854 | case DLT_JUNIPER_ATM1: |
4027 | 1.19k | case DLT_JUNIPER_ATM2: |
4028 | 1.42k | case DLT_JUNIPER_PPPOE: |
4029 | 1.64k | case DLT_JUNIPER_PPPOE_ATM: |
4030 | 1.83k | case DLT_JUNIPER_GGSN: |
4031 | 2.11k | case DLT_JUNIPER_ES: |
4032 | 2.82k | case DLT_JUNIPER_MONITOR: |
4033 | 3.39k | case DLT_JUNIPER_SERVICES: |
4034 | 3.69k | case DLT_JUNIPER_ETHER: |
4035 | 3.94k | case DLT_JUNIPER_PPP: |
4036 | 4.04k | case DLT_JUNIPER_FRELAY: |
4037 | 4.24k | case DLT_JUNIPER_CHDLC: |
4038 | 4.32k | case DLT_JUNIPER_VP: |
4039 | 4.61k | case DLT_JUNIPER_ST: |
4040 | 4.80k | case DLT_JUNIPER_ISM: |
4041 | 4.87k | case DLT_JUNIPER_VS: |
4042 | 5.08k | case DLT_JUNIPER_SRX_E2E: |
4043 | 5.16k | case DLT_JUNIPER_FIBRECHANNEL: |
4044 | 5.38k | case DLT_JUNIPER_ATM_CEMIC: |
4045 | | |
4046 | | /* just lets verify the magic number for now - |
4047 | | * on ATM we may have up to 6 different encapsulations on the wire |
4048 | | * and need a lot of heuristics to figure out that the payload |
4049 | | * might be; |
4050 | | * |
4051 | | * FIXME encapsulation specific BPF_ filters |
4052 | | */ |
4053 | 5.38k | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */ |
4054 | | |
4055 | 176 | case DLT_BACNET_MS_TP: |
4056 | 176 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000); |
4057 | | |
4058 | 505 | case DLT_IPNET: |
4059 | 505 | return gen_ipnet_linktype(cstate, ll_proto); |
4060 | | |
4061 | 1 | case DLT_LINUX_IRDA: |
4062 | 2 | case DLT_DOCSIS: |
4063 | 8 | case DLT_MTP2: |
4064 | 8 | case DLT_MTP2_WITH_PHDR: |
4065 | 9 | case DLT_ERF: |
4066 | 11 | case DLT_PFSYNC: |
4067 | 12 | case DLT_LINUX_LAPD: |
4068 | 16 | case DLT_USB_FREEBSD: |
4069 | 17 | case DLT_USB_LINUX: |
4070 | 21 | case DLT_USB_LINUX_MMAPPED: |
4071 | 22 | case DLT_USBPCAP: |
4072 | 22 | case DLT_BLUETOOTH_HCI_H4: |
4073 | 23 | case DLT_BLUETOOTH_HCI_H4_WITH_PHDR: |
4074 | 25 | case DLT_CAN20B: |
4075 | 26 | case DLT_CAN_SOCKETCAN: |
4076 | 27 | case DLT_IEEE802_15_4: |
4077 | 27 | case DLT_IEEE802_15_4_LINUX: |
4078 | 28 | case DLT_IEEE802_15_4_NONASK_PHY: |
4079 | 28 | case DLT_IEEE802_15_4_NOFCS: |
4080 | 29 | case DLT_IEEE802_15_4_TAP: |
4081 | 30 | case DLT_IEEE802_16_MAC_CPS_RADIO: |
4082 | 31 | case DLT_SITA: |
4083 | 32 | case DLT_RAIF1: |
4084 | 33 | case DLT_IPMB_KONTRON: |
4085 | 34 | case DLT_I2C_LINUX: |
4086 | 35 | case DLT_AX25_KISS: |
4087 | 37 | case DLT_NFLOG: |
4088 | | /* Using the fixed-size NFLOG header it is possible to tell only |
4089 | | * the address family of the packet, other meaningful data is |
4090 | | * either missing or behind TLVs. |
4091 | | */ |
4092 | 37 | break; // not implemented |
4093 | | |
4094 | 595 | default: |
4095 | | /* |
4096 | | * Does this link-layer header type have a field |
4097 | | * indicating the type of the next protocol? If |
4098 | | * so, off_linktype.constant_part will be the offset of that |
4099 | | * field in the packet; if not, it will be OFFSET_NOT_SET. |
4100 | | */ |
4101 | 595 | if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) { |
4102 | | /* |
4103 | | * Yes; assume it's an Ethernet type. (If |
4104 | | * it's not, it needs to be handled specially |
4105 | | * above.) |
4106 | | */ |
4107 | 567 | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
4108 | 567 | return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); |
4109 | | /*NOTREACHED */ |
4110 | 567 | } |
4111 | 62.9k | } |
4112 | 66 | bpf_error(cstate, "link-layer type filtering not implemented for %s", |
4113 | 66 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
4114 | 62.9k | } |
4115 | | |
4116 | | /* |
4117 | | * Check for an LLC SNAP packet with a given organization code and |
4118 | | * protocol type; we check the entire contents of the 802.2 LLC and |
4119 | | * snap headers, checking for DSAP and SSAP of SNAP and a control |
4120 | | * field of 0x03 in the LLC header, and for the specified organization |
4121 | | * code and protocol type in the SNAP header. |
4122 | | */ |
4123 | | static struct block * |
4124 | | gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype) |
4125 | 294 | { |
4126 | 294 | u_char snapblock[8]; |
4127 | | |
4128 | 294 | snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */ |
4129 | 294 | snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */ |
4130 | 294 | snapblock[2] = 0x03; /* control = UI */ |
4131 | 294 | snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */ |
4132 | 294 | snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */ |
4133 | 294 | snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */ |
4134 | 294 | snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */ |
4135 | 294 | snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */ |
4136 | 294 | return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock); |
4137 | 294 | } |
4138 | | |
4139 | | /* |
4140 | | * Generate code to match frames with an LLC header. |
4141 | | */ |
4142 | | static struct block * |
4143 | | gen_llc_internal(compiler_state_t *cstate) |
4144 | 837 | { |
4145 | 837 | struct block *b0, *b1; |
4146 | | |
4147 | 837 | switch (cstate->linktype) { |
4148 | | |
4149 | 189 | case DLT_EN10MB: |
4150 | | /* |
4151 | | * We check for an Ethernet type field less or equal than |
4152 | | * 1500, which means it's an 802.3 length field. |
4153 | | */ |
4154 | 189 | b0 = gen_cmp_le(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); |
4155 | | |
4156 | | /* |
4157 | | * Now check for the purported DSAP and SSAP not being |
4158 | | * 0xFF, to rule out NetWare-over-802.3. |
4159 | | */ |
4160 | 189 | b1 = gen_cmp_ne(cstate, OR_LLC, 0, BPF_H, 0xFFFF); |
4161 | 189 | gen_and(b0, b1); |
4162 | 189 | return b1; |
4163 | | |
4164 | 72 | case DLT_SUNATM: |
4165 | | /* |
4166 | | * We check for LLC traffic. |
4167 | | */ |
4168 | 72 | return gen_atmtype_llc(cstate); |
4169 | | |
4170 | 104 | case DLT_IEEE802: /* Token Ring */ |
4171 | | /* |
4172 | | * XXX - check for LLC frames. |
4173 | | */ |
4174 | 104 | return gen_true(cstate); |
4175 | | |
4176 | 52 | case DLT_FDDI: |
4177 | | /* |
4178 | | * XXX - check for LLC frames. |
4179 | | */ |
4180 | 52 | return gen_true(cstate); |
4181 | | |
4182 | 61 | case DLT_ATM_RFC1483: |
4183 | | /* |
4184 | | * For LLC encapsulation, these are defined to have an |
4185 | | * 802.2 LLC header. |
4186 | | * |
4187 | | * For VC encapsulation, they don't, but there's no |
4188 | | * way to check for that; the protocol used on the VC |
4189 | | * is negotiated out of band. |
4190 | | */ |
4191 | 61 | return gen_true(cstate); |
4192 | | |
4193 | 69 | case DLT_IEEE802_11: |
4194 | 133 | case DLT_PRISM_HEADER: |
4195 | 189 | case DLT_IEEE802_11_RADIO: |
4196 | 261 | case DLT_IEEE802_11_RADIO_AVS: |
4197 | 351 | case DLT_PPI: |
4198 | | /* |
4199 | | * Check that we have a data frame. |
4200 | | */ |
4201 | 351 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, |
4202 | 351 | IEEE80211_FC0_TYPE_DATA, |
4203 | 351 | IEEE80211_FC0_TYPE_MASK); |
4204 | | |
4205 | 8 | default: |
4206 | 8 | fail_kw_on_dlt(cstate, "llc"); |
4207 | | /*NOTREACHED*/ |
4208 | 837 | } |
4209 | 837 | } |
4210 | | |
4211 | | struct block * |
4212 | | gen_llc(compiler_state_t *cstate) |
4213 | 538 | { |
4214 | | /* |
4215 | | * Catch errors reported by us and routines below us, and return NULL |
4216 | | * on an error. |
4217 | | */ |
4218 | 538 | if (setjmp(cstate->top_ctx)) |
4219 | 3 | return (NULL); |
4220 | | |
4221 | 535 | return gen_llc_internal(cstate); |
4222 | 538 | } |
4223 | | |
4224 | | struct block * |
4225 | | gen_llc_i(compiler_state_t *cstate) |
4226 | 61 | { |
4227 | 61 | struct block *b0, *b1; |
4228 | 61 | struct slist *s; |
4229 | | |
4230 | | /* |
4231 | | * Catch errors reported by us and routines below us, and return NULL |
4232 | | * on an error. |
4233 | | */ |
4234 | 61 | if (setjmp(cstate->top_ctx)) |
4235 | 1 | return (NULL); |
4236 | | |
4237 | | /* |
4238 | | * Check whether this is an LLC frame. |
4239 | | */ |
4240 | 60 | b0 = gen_llc_internal(cstate); |
4241 | | |
4242 | | /* |
4243 | | * Load the control byte and test the low-order bit; it must |
4244 | | * be clear for I frames. |
4245 | | */ |
4246 | 60 | s = gen_load_a(cstate, OR_LLC, 2, BPF_B); |
4247 | 60 | b1 = gen_unset(cstate, 0x01, s); |
4248 | | |
4249 | 60 | gen_and(b0, b1); |
4250 | 60 | return b1; |
4251 | 61 | } |
4252 | | |
4253 | | struct block * |
4254 | | gen_llc_s(compiler_state_t *cstate) |
4255 | 41 | { |
4256 | 41 | struct block *b0, *b1; |
4257 | | |
4258 | | /* |
4259 | | * Catch errors reported by us and routines below us, and return NULL |
4260 | | * on an error. |
4261 | | */ |
4262 | 41 | if (setjmp(cstate->top_ctx)) |
4263 | 1 | return (NULL); |
4264 | | |
4265 | | /* |
4266 | | * Check whether this is an LLC frame. |
4267 | | */ |
4268 | 40 | b0 = gen_llc_internal(cstate); |
4269 | | |
4270 | | /* |
4271 | | * Now compare the low-order 2 bit of the control byte against |
4272 | | * the appropriate value for S frames. |
4273 | | */ |
4274 | 40 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03); |
4275 | 40 | gen_and(b0, b1); |
4276 | 40 | return b1; |
4277 | 41 | } |
4278 | | |
4279 | | struct block * |
4280 | | gen_llc_u(compiler_state_t *cstate) |
4281 | 75 | { |
4282 | 75 | struct block *b0, *b1; |
4283 | | |
4284 | | /* |
4285 | | * Catch errors reported by us and routines below us, and return NULL |
4286 | | * on an error. |
4287 | | */ |
4288 | 75 | if (setjmp(cstate->top_ctx)) |
4289 | 0 | return (NULL); |
4290 | | |
4291 | | /* |
4292 | | * Check whether this is an LLC frame. |
4293 | | */ |
4294 | 75 | b0 = gen_llc_internal(cstate); |
4295 | | |
4296 | | /* |
4297 | | * Now compare the low-order 2 bit of the control byte against |
4298 | | * the appropriate value for U frames. |
4299 | | */ |
4300 | 75 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03); |
4301 | 75 | gen_and(b0, b1); |
4302 | 75 | return b1; |
4303 | 75 | } |
4304 | | |
4305 | | struct block * |
4306 | | gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
4307 | 64 | { |
4308 | 64 | struct block *b0, *b1; |
4309 | | |
4310 | | /* |
4311 | | * Catch errors reported by us and routines below us, and return NULL |
4312 | | * on an error. |
4313 | | */ |
4314 | 64 | if (setjmp(cstate->top_ctx)) |
4315 | 2 | return (NULL); |
4316 | | |
4317 | | /* |
4318 | | * Check whether this is an LLC frame. |
4319 | | */ |
4320 | 62 | b0 = gen_llc_internal(cstate); |
4321 | | |
4322 | | /* |
4323 | | * Now check for an S frame with the appropriate type. |
4324 | | */ |
4325 | 62 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK); |
4326 | 62 | gen_and(b0, b1); |
4327 | 62 | return b1; |
4328 | 64 | } |
4329 | | |
4330 | | struct block * |
4331 | | gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) |
4332 | 58 | { |
4333 | 58 | struct block *b0, *b1; |
4334 | | |
4335 | | /* |
4336 | | * Catch errors reported by us and routines below us, and return NULL |
4337 | | * on an error. |
4338 | | */ |
4339 | 58 | if (setjmp(cstate->top_ctx)) |
4340 | 1 | return (NULL); |
4341 | | |
4342 | | /* |
4343 | | * Check whether this is an LLC frame. |
4344 | | */ |
4345 | 57 | b0 = gen_llc_internal(cstate); |
4346 | | |
4347 | | /* |
4348 | | * Now check for a U frame with the appropriate type. |
4349 | | */ |
4350 | 57 | b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK); |
4351 | 57 | gen_and(b0, b1); |
4352 | 57 | return b1; |
4353 | 58 | } |
4354 | | |
4355 | | /* |
4356 | | * Generate code to match a particular packet type, for link-layer types |
4357 | | * using 802.2 LLC headers. |
4358 | | * |
4359 | | * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used |
4360 | | * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues. |
4361 | | * |
4362 | | * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP |
4363 | | * value, if <= ETHERMTU. We use that to determine whether to |
4364 | | * match the DSAP or both DSAP and LSAP or to check the OUI and |
4365 | | * protocol ID in a SNAP header. |
4366 | | */ |
4367 | | static struct block * |
4368 | | gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
4369 | 21.6k | { |
4370 | | /* |
4371 | | * XXX - handle token-ring variable-length header. |
4372 | | */ |
4373 | 21.6k | switch (ll_proto) { |
4374 | | |
4375 | 52 | case LLCSAP_IP: |
4376 | 3.21k | case LLCSAP_ISONS: |
4377 | 3.25k | case LLCSAP_NETBEUI: |
4378 | | /* |
4379 | | * XXX - should we check both the DSAP and the |
4380 | | * SSAP, like this, or should we check just the |
4381 | | * DSAP, as we do for other SAP values? |
4382 | | */ |
4383 | 3.25k | return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32) |
4384 | 3.25k | ((ll_proto << 8) | ll_proto)); |
4385 | | |
4386 | 80 | case LLCSAP_IPX: |
4387 | | /* |
4388 | | * XXX - are there ever SNAP frames for IPX on |
4389 | | * non-Ethernet 802.x networks? |
4390 | | */ |
4391 | 80 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); |
4392 | | |
4393 | 71 | case ETHERTYPE_ATALK: |
4394 | | /* |
4395 | | * 802.2-encapsulated ETHERTYPE_ATALK packets are |
4396 | | * SNAP packets with an organization code of |
4397 | | * 0x080007 (Apple, for Appletalk) and a protocol |
4398 | | * type of ETHERTYPE_ATALK (Appletalk). |
4399 | | * |
4400 | | * XXX - check for an organization code of |
4401 | | * encapsulated Ethernet as well? |
4402 | | */ |
4403 | 71 | return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); |
4404 | | |
4405 | 18.1k | default: |
4406 | | /* |
4407 | | * XXX - we don't have to check for IPX 802.3 |
4408 | | * here, but should we check for the IPX Ethertype? |
4409 | | */ |
4410 | 18.1k | if (ll_proto <= ETHERMTU) { |
4411 | 127 | assert_maxval(cstate, "LLC DSAP", ll_proto, UINT8_MAX); |
4412 | | /* |
4413 | | * This is an LLC SAP value, so check |
4414 | | * the DSAP. |
4415 | | */ |
4416 | 127 | return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto); |
4417 | 18.0k | } else { |
4418 | 18.0k | assert_maxval(cstate, "EtherType", ll_proto, UINT16_MAX); |
4419 | | /* |
4420 | | * This is an Ethernet type; we assume that it's |
4421 | | * unlikely that it'll appear in the right place |
4422 | | * at random, and therefore check only the |
4423 | | * location that would hold the Ethernet type |
4424 | | * in a SNAP frame with an organization code of |
4425 | | * 0x000000 (encapsulated Ethernet). |
4426 | | * |
4427 | | * XXX - if we were to check for the SNAP DSAP and |
4428 | | * LSAP, as per XXX, and were also to check for an |
4429 | | * organization code of 0x000000 (encapsulated |
4430 | | * Ethernet), we'd do |
4431 | | * |
4432 | | * return gen_snap(cstate, 0x000000, ll_proto); |
4433 | | * |
4434 | | * here; for now, we don't, as per the above. |
4435 | | * I don't know whether it's worth the extra CPU |
4436 | | * time to do the right check or not. |
4437 | | */ |
4438 | 18.0k | return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto); |
4439 | 18.0k | } |
4440 | 21.6k | } |
4441 | 21.6k | } |
4442 | | |
4443 | | static struct block * |
4444 | | gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
4445 | | int dir, u_int src_off, u_int dst_off) |
4446 | 73.1k | { |
4447 | 73.1k | struct block *b0, *b1; |
4448 | 73.1k | u_int offset; |
4449 | | |
4450 | 73.1k | switch (dir) { |
4451 | | |
4452 | 24.3k | case Q_SRC: |
4453 | 24.3k | offset = src_off; |
4454 | 24.3k | break; |
4455 | | |
4456 | 24.9k | case Q_DST: |
4457 | 24.9k | offset = dst_off; |
4458 | 24.9k | break; |
4459 | | |
4460 | 1.10k | case Q_AND: |
4461 | 1.10k | b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4462 | 1.10k | b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off); |
4463 | 1.10k | gen_and(b0, b1); |
4464 | 1.10k | return b1; |
4465 | | |
4466 | 22.3k | case Q_DEFAULT: |
4467 | 22.8k | case Q_OR: |
4468 | 22.8k | b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4469 | 22.8k | b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off); |
4470 | 22.8k | gen_or(b0, b1); |
4471 | 22.8k | return b1; |
4472 | | |
4473 | 1 | case Q_ADDR1: |
4474 | 3 | case Q_ADDR2: |
4475 | 4 | case Q_ADDR3: |
4476 | 5 | case Q_ADDR4: |
4477 | 7 | case Q_RA: |
4478 | 15 | case Q_TA: |
4479 | 15 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
4480 | | /*NOTREACHED*/ |
4481 | | |
4482 | 0 | default: |
4483 | 0 | abort(); |
4484 | | /*NOTREACHED*/ |
4485 | 73.1k | } |
4486 | 49.2k | return gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask); |
4487 | 73.1k | } |
4488 | | |
4489 | | static struct block * |
4490 | | gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr, |
4491 | | struct in6_addr *mask, int dir, u_int src_off, u_int dst_off) |
4492 | 1.51k | { |
4493 | 1.51k | struct block *b0, *b1; |
4494 | 1.51k | u_int offset; |
4495 | | /* |
4496 | | * Code below needs to access four separate 32-bit parts of the 128-bit |
4497 | | * IPv6 address and mask. In some OSes this is as simple as using the |
4498 | | * s6_addr32 pseudo-member of struct in6_addr, which contains a union of |
4499 | | * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as |
4500 | | * far as libpcap sees it. Hence copy the data before use to avoid |
4501 | | * potential unaligned memory access and the associated compiler |
4502 | | * warnings (whether genuine or not). |
4503 | | */ |
4504 | 1.51k | bpf_u_int32 a[4], m[4]; |
4505 | | |
4506 | 1.51k | switch (dir) { |
4507 | | |
4508 | 536 | case Q_SRC: |
4509 | 536 | offset = src_off; |
4510 | 536 | break; |
4511 | | |
4512 | 514 | case Q_DST: |
4513 | 514 | offset = dst_off; |
4514 | 514 | break; |
4515 | | |
4516 | 52 | case Q_AND: |
4517 | 52 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4518 | 52 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off); |
4519 | 52 | gen_and(b0, b1); |
4520 | 52 | return b1; |
4521 | | |
4522 | 330 | case Q_DEFAULT: |
4523 | 410 | case Q_OR: |
4524 | 410 | b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off); |
4525 | 410 | b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off); |
4526 | 410 | gen_or(b0, b1); |
4527 | 410 | return b1; |
4528 | | |
4529 | 1 | case Q_ADDR1: |
4530 | 1 | case Q_ADDR2: |
4531 | 2 | case Q_ADDR3: |
4532 | 3 | case Q_ADDR4: |
4533 | 3 | case Q_RA: |
4534 | 3 | case Q_TA: |
4535 | 3 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
4536 | | /*NOTREACHED*/ |
4537 | | |
4538 | 0 | default: |
4539 | 0 | abort(); |
4540 | | /*NOTREACHED*/ |
4541 | 1.51k | } |
4542 | | /* this order is important */ |
4543 | 1.05k | memcpy(a, addr, sizeof(a)); |
4544 | 1.05k | memcpy(m, mask, sizeof(m)); |
4545 | 1.05k | b1 = NULL; |
4546 | 5.25k | for (int i = 3; i >= 0; i--) { |
4547 | | // Same as the Q_IP case in gen_host(). |
4548 | 4.20k | if (m[i] == 0 && a[i] == 0) |
4549 | 216 | continue; |
4550 | 3.98k | b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4 * i, BPF_W, |
4551 | 3.98k | ntohl(a[i]), ntohl(m[i])); |
4552 | 3.98k | if (b1) |
4553 | 2.93k | gen_and(b0, b1); |
4554 | 1.05k | else |
4555 | 1.05k | b1 = b0; |
4556 | 3.98k | } |
4557 | 1.05k | return b1 ? b1 : gen_true(cstate); |
4558 | 1.51k | } |
4559 | | |
4560 | | /* |
4561 | | * Like gen_mac48host(), but for DLT_IEEE802_11 (802.11 wireless LAN) and |
4562 | | * various 802.11 + radio headers. |
4563 | | */ |
4564 | | static struct block * |
4565 | | gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) |
4566 | 804 | { |
4567 | 804 | register struct block *b0, *b1, *b2; |
4568 | 804 | register struct slist *s; |
4569 | | |
4570 | | #ifdef ENABLE_WLAN_FILTERING_PATCH |
4571 | | /* |
4572 | | * TODO GV 20070613 |
4573 | | * We need to disable the optimizer because the optimizer is buggy |
4574 | | * and wipes out some LD instructions generated by the below |
4575 | | * code to validate the Frame Control bits |
4576 | | */ |
4577 | | cstate->no_optimize = 1; |
4578 | | #endif /* ENABLE_WLAN_FILTERING_PATCH */ |
4579 | | |
4580 | 804 | switch (dir) { |
4581 | 199 | case Q_SRC: |
4582 | | /* |
4583 | | * Oh, yuk. |
4584 | | * |
4585 | | * For control frames, there is no SA. |
4586 | | * |
4587 | | * For management frames, SA is at an |
4588 | | * offset of 10 from the beginning of |
4589 | | * the packet. |
4590 | | * |
4591 | | * For data frames, SA is at an offset |
4592 | | * of 10 from the beginning of the packet |
4593 | | * if From DS is clear, at an offset of |
4594 | | * 16 from the beginning of the packet |
4595 | | * if From DS is set and To DS is clear, |
4596 | | * and an offset of 24 from the beginning |
4597 | | * of the packet if From DS is set and To DS |
4598 | | * is set. |
4599 | | */ |
4600 | | |
4601 | | /* |
4602 | | * Generate the tests to be done for data frames |
4603 | | * with From DS set. |
4604 | | * |
4605 | | * First, check for To DS set, i.e. check "link[1] & 0x01". |
4606 | | */ |
4607 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4608 | 199 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
4609 | | |
4610 | | /* |
4611 | | * If To DS is set, the SA is at 24. |
4612 | | */ |
4613 | 199 | b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4614 | 199 | gen_and(b1, b0); |
4615 | | |
4616 | | /* |
4617 | | * Now, check for To DS not set, i.e. check |
4618 | | * "!(link[1] & 0x01)". |
4619 | | */ |
4620 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4621 | 199 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
4622 | | |
4623 | | /* |
4624 | | * If To DS is not set, the SA is at 16. |
4625 | | */ |
4626 | 199 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4627 | 199 | gen_and(b2, b1); |
4628 | | |
4629 | | /* |
4630 | | * Now OR together the last two checks. That gives |
4631 | | * the complete set of checks for data frames with |
4632 | | * From DS set. |
4633 | | */ |
4634 | 199 | gen_or(b1, b0); |
4635 | | |
4636 | | /* |
4637 | | * Now check for From DS being set, and AND that with |
4638 | | * the ORed-together checks. |
4639 | | */ |
4640 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4641 | 199 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_FROMDS, s); |
4642 | 199 | gen_and(b1, b0); |
4643 | | |
4644 | | /* |
4645 | | * Now check for data frames with From DS not set. |
4646 | | */ |
4647 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4648 | 199 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_FROMDS, s); |
4649 | | |
4650 | | /* |
4651 | | * If From DS isn't set, the SA is at 10. |
4652 | | */ |
4653 | 199 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4654 | 199 | gen_and(b2, b1); |
4655 | | |
4656 | | /* |
4657 | | * Now OR together the checks for data frames with |
4658 | | * From DS not set and for data frames with From DS |
4659 | | * set; that gives the checks done for data frames. |
4660 | | */ |
4661 | 199 | gen_or(b1, b0); |
4662 | | |
4663 | | /* |
4664 | | * Now check for a data frame. |
4665 | | * I.e, check "link[0] & 0x08". |
4666 | | */ |
4667 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4668 | 199 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4669 | | |
4670 | | /* |
4671 | | * AND that with the checks done for data frames. |
4672 | | */ |
4673 | 199 | gen_and(b1, b0); |
4674 | | |
4675 | | /* |
4676 | | * If the high-order bit of the type value is 0, this |
4677 | | * is a management frame. |
4678 | | * I.e, check "!(link[0] & 0x08)". |
4679 | | */ |
4680 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4681 | 199 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4682 | | |
4683 | | /* |
4684 | | * For management frames, the SA is at 10. |
4685 | | */ |
4686 | 199 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4687 | 199 | gen_and(b2, b1); |
4688 | | |
4689 | | /* |
4690 | | * OR that with the checks done for data frames. |
4691 | | * That gives the checks done for management and |
4692 | | * data frames. |
4693 | | */ |
4694 | 199 | gen_or(b1, b0); |
4695 | | |
4696 | | /* |
4697 | | * If the low-order bit of the type value is 1, |
4698 | | * this is either a control frame or a frame |
4699 | | * with a reserved type, and thus not a |
4700 | | * frame with an SA. |
4701 | | * |
4702 | | * I.e., check "!(link[0] & 0x04)". |
4703 | | */ |
4704 | 199 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4705 | 199 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
4706 | | |
4707 | | /* |
4708 | | * AND that with the checks for data and management |
4709 | | * frames. |
4710 | | */ |
4711 | 199 | gen_and(b1, b0); |
4712 | 199 | return b0; |
4713 | | |
4714 | 206 | case Q_DST: |
4715 | | /* |
4716 | | * Oh, yuk. |
4717 | | * |
4718 | | * For control frames, there is no DA. |
4719 | | * |
4720 | | * For management frames, DA is at an |
4721 | | * offset of 4 from the beginning of |
4722 | | * the packet. |
4723 | | * |
4724 | | * For data frames, DA is at an offset |
4725 | | * of 4 from the beginning of the packet |
4726 | | * if To DS is clear and at an offset of |
4727 | | * 16 from the beginning of the packet |
4728 | | * if To DS is set. |
4729 | | */ |
4730 | | |
4731 | | /* |
4732 | | * Generate the tests to be done for data frames. |
4733 | | * |
4734 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
4735 | | */ |
4736 | 206 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4737 | 206 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
4738 | | |
4739 | | /* |
4740 | | * If To DS is set, the DA is at 16. |
4741 | | */ |
4742 | 206 | b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4743 | 206 | gen_and(b1, b0); |
4744 | | |
4745 | | /* |
4746 | | * Now, check for To DS not set, i.e. check |
4747 | | * "!(link[1] & 0x01)". |
4748 | | */ |
4749 | 206 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
4750 | 206 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
4751 | | |
4752 | | /* |
4753 | | * If To DS is not set, the DA is at 4. |
4754 | | */ |
4755 | 206 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4756 | 206 | gen_and(b2, b1); |
4757 | | |
4758 | | /* |
4759 | | * Now OR together the last two checks. That gives |
4760 | | * the complete set of checks for data frames. |
4761 | | */ |
4762 | 206 | gen_or(b1, b0); |
4763 | | |
4764 | | /* |
4765 | | * Now check for a data frame. |
4766 | | * I.e, check "link[0] & 0x08". |
4767 | | */ |
4768 | 206 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4769 | 206 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4770 | | |
4771 | | /* |
4772 | | * AND that with the checks done for data frames. |
4773 | | */ |
4774 | 206 | gen_and(b1, b0); |
4775 | | |
4776 | | /* |
4777 | | * If the high-order bit of the type value is 0, this |
4778 | | * is a management frame. |
4779 | | * I.e, check "!(link[0] & 0x08)". |
4780 | | */ |
4781 | 206 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4782 | 206 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4783 | | |
4784 | | /* |
4785 | | * For management frames, the DA is at 4. |
4786 | | */ |
4787 | 206 | b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4788 | 206 | gen_and(b2, b1); |
4789 | | |
4790 | | /* |
4791 | | * OR that with the checks done for data frames. |
4792 | | * That gives the checks done for management and |
4793 | | * data frames. |
4794 | | */ |
4795 | 206 | gen_or(b1, b0); |
4796 | | |
4797 | | /* |
4798 | | * If the low-order bit of the type value is 1, |
4799 | | * this is either a control frame or a frame |
4800 | | * with a reserved type, and thus not a |
4801 | | * frame with an SA. |
4802 | | * |
4803 | | * I.e., check "!(link[0] & 0x04)". |
4804 | | */ |
4805 | 206 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4806 | 206 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
4807 | | |
4808 | | /* |
4809 | | * AND that with the checks for data and management |
4810 | | * frames. |
4811 | | */ |
4812 | 206 | gen_and(b1, b0); |
4813 | 206 | return b0; |
4814 | | |
4815 | 74 | case Q_AND: |
4816 | 74 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4817 | 74 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4818 | 74 | gen_and(b0, b1); |
4819 | 74 | return b1; |
4820 | | |
4821 | 70 | case Q_DEFAULT: |
4822 | 125 | case Q_OR: |
4823 | 125 | b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); |
4824 | 125 | b1 = gen_wlanhostop(cstate, eaddr, Q_DST); |
4825 | 125 | gen_or(b0, b1); |
4826 | 125 | return b1; |
4827 | | |
4828 | | /* |
4829 | | * XXX - add BSSID keyword? |
4830 | | */ |
4831 | 34 | case Q_ADDR1: |
4832 | 34 | return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr)); |
4833 | | |
4834 | 37 | case Q_ADDR2: |
4835 | | /* |
4836 | | * Not present in CTS or ACK control frames. |
4837 | | */ |
4838 | 37 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4839 | 37 | IEEE80211_FC0_TYPE_MASK); |
4840 | 37 | b1 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4841 | 37 | IEEE80211_FC0_SUBTYPE_MASK); |
4842 | 37 | b2 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4843 | 37 | IEEE80211_FC0_SUBTYPE_MASK); |
4844 | 37 | gen_and(b1, b2); |
4845 | 37 | gen_or(b0, b2); |
4846 | 37 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4847 | 37 | gen_and(b2, b1); |
4848 | 37 | return b1; |
4849 | | |
4850 | 24 | case Q_ADDR3: |
4851 | | /* |
4852 | | * Not present in control frames. |
4853 | | */ |
4854 | 24 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4855 | 24 | IEEE80211_FC0_TYPE_MASK); |
4856 | 24 | b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); |
4857 | 24 | gen_and(b0, b1); |
4858 | 24 | return b1; |
4859 | | |
4860 | 22 | case Q_ADDR4: |
4861 | | /* |
4862 | | * Present only if the direction mask has both "From DS" |
4863 | | * and "To DS" set. Neither control frames nor management |
4864 | | * frames should have both of those set, so we don't |
4865 | | * check the frame type. |
4866 | | */ |
4867 | 22 | b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, |
4868 | 22 | IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK); |
4869 | 22 | b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); |
4870 | 22 | gen_and(b0, b1); |
4871 | 22 | return b1; |
4872 | | |
4873 | 46 | case Q_RA: |
4874 | | /* |
4875 | | * Not present in management frames; addr1 in other |
4876 | | * frames. |
4877 | | */ |
4878 | | |
4879 | | /* |
4880 | | * If the high-order bit of the type value is 0, this |
4881 | | * is a management frame. |
4882 | | * I.e, check "(link[0] & 0x08)". |
4883 | | */ |
4884 | 46 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4885 | 46 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4886 | | |
4887 | | /* |
4888 | | * Check addr1. |
4889 | | */ |
4890 | 46 | b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); |
4891 | | |
4892 | | /* |
4893 | | * AND that with the check of addr1. |
4894 | | */ |
4895 | 46 | gen_and(b1, b0); |
4896 | 46 | return (b0); |
4897 | | |
4898 | 37 | case Q_TA: |
4899 | | /* |
4900 | | * Not present in management frames; addr2, if present, |
4901 | | * in other frames. |
4902 | | */ |
4903 | | |
4904 | | /* |
4905 | | * Not present in CTS or ACK control frames. |
4906 | | */ |
4907 | 37 | b0 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, |
4908 | 37 | IEEE80211_FC0_TYPE_MASK); |
4909 | 37 | b1 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, |
4910 | 37 | IEEE80211_FC0_SUBTYPE_MASK); |
4911 | 37 | b2 = gen_mcmp_ne(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, |
4912 | 37 | IEEE80211_FC0_SUBTYPE_MASK); |
4913 | 37 | gen_and(b1, b2); |
4914 | 37 | gen_or(b0, b2); |
4915 | | |
4916 | | /* |
4917 | | * If the high-order bit of the type value is 0, this |
4918 | | * is a management frame. |
4919 | | * I.e, check "(link[0] & 0x08)". |
4920 | | */ |
4921 | 37 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
4922 | 37 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
4923 | | |
4924 | | /* |
4925 | | * AND that with the check for frames other than |
4926 | | * CTS and ACK frames. |
4927 | | */ |
4928 | 37 | gen_and(b1, b2); |
4929 | | |
4930 | | /* |
4931 | | * Check addr2. |
4932 | | */ |
4933 | 37 | b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); |
4934 | 37 | gen_and(b2, b1); |
4935 | 37 | return b1; |
4936 | 804 | } |
4937 | 0 | abort(); |
4938 | | /*NOTREACHED*/ |
4939 | 804 | } |
4940 | | |
4941 | | /* |
4942 | | * This is quite tricky because there may be pad bytes in front of the |
4943 | | * DECNET header, and then there are two possible data packet formats that |
4944 | | * carry both src and dst addresses, plus 5 packet types in a format that |
4945 | | * carries only the src node, plus 2 types that use a different format and |
4946 | | * also carry just the src node. |
4947 | | * |
4948 | | * Yuck. |
4949 | | * |
4950 | | * Instead of doing those all right, we just look for data packets with |
4951 | | * 0 or 1 bytes of padding. If you want to look at other packets, that |
4952 | | * will require a lot more hacking. |
4953 | | * |
4954 | | * To add support for filtering on DECNET "areas" (network numbers) |
4955 | | * one would want to add a "mask" argument to this routine. That would |
4956 | | * make the filter even more inefficient, although one could be clever |
4957 | | * and not generate masking instructions if the mask is 0xFFFF. |
4958 | | */ |
4959 | | static struct block * |
4960 | | gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir) |
4961 | 447 | { |
4962 | 447 | struct block *b0, *b1, *b2, *tmp; |
4963 | 447 | u_int offset_lh; /* offset if long header is received */ |
4964 | 447 | u_int offset_sh; /* offset if short header is received */ |
4965 | | |
4966 | 447 | switch (dir) { |
4967 | | |
4968 | 145 | case Q_DST: |
4969 | 145 | offset_sh = 1; /* follows flags */ |
4970 | 145 | offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ |
4971 | 145 | break; |
4972 | | |
4973 | 148 | case Q_SRC: |
4974 | 148 | offset_sh = 3; /* follows flags, dstnode */ |
4975 | 148 | offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ |
4976 | 148 | break; |
4977 | | |
4978 | 22 | case Q_AND: |
4979 | | /* Inefficient because we do our Calvinball dance twice */ |
4980 | 22 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4981 | 22 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4982 | 22 | gen_and(b0, b1); |
4983 | 22 | return b1; |
4984 | | |
4985 | 42 | case Q_DEFAULT: |
4986 | 119 | case Q_OR: |
4987 | | /* Inefficient because we do our Calvinball dance twice */ |
4988 | 119 | b0 = gen_dnhostop(cstate, addr, Q_SRC); |
4989 | 119 | b1 = gen_dnhostop(cstate, addr, Q_DST); |
4990 | 119 | gen_or(b0, b1); |
4991 | 119 | return b1; |
4992 | | |
4993 | 0 | case Q_ADDR1: |
4994 | 1 | case Q_ADDR2: |
4995 | 1 | case Q_ADDR3: |
4996 | 2 | case Q_ADDR4: |
4997 | 5 | case Q_RA: |
4998 | 13 | case Q_TA: |
4999 | 13 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
5000 | | /*NOTREACHED*/ |
5001 | | |
5002 | 0 | default: |
5003 | 0 | abort(); |
5004 | | /*NOTREACHED*/ |
5005 | 447 | } |
5006 | | /* |
5007 | | * In a DECnet message inside an Ethernet frame the first two bytes |
5008 | | * immediately after EtherType are the [little-endian] DECnet message |
5009 | | * length, which is irrelevant in this context. |
5010 | | * |
5011 | | * "pad = 1" means the third byte equals 0x81, thus it is the PLENGTH |
5012 | | * 8-bit bitmap of the optional padding before the packet route header. |
5013 | | * The bitmap always has bit 7 set to 1 and in this case has bits 0-6 |
5014 | | * (TOTAL-PAD-SEQUENCE-LENGTH) set to integer value 1. The latter |
5015 | | * means there aren't any PAD bytes after the bitmap, so the header |
5016 | | * begins at the fourth byte. "pad = 0" means bit 7 of the third byte |
5017 | | * is set to 0, thus the header begins at the third byte. |
5018 | | * |
5019 | | * The header can be in several (as mentioned above) formats, all of |
5020 | | * which begin with the FLAGS 8-bit bitmap, which always has bit 7 |
5021 | | * (PF, "pad field") set to 0 regardless of any padding present before |
5022 | | * the header. "Short header" means bits 0-2 of the bitmap encode the |
5023 | | * integer value 2 (SFDP), and "long header" means value 6 (LFDP). |
5024 | | * |
5025 | | * To test PLENGTH and FLAGS, use multiple-byte constants with the |
5026 | | * values and the masks, this maps to the required single bytes of |
5027 | | * the message correctly on both big-endian and little-endian hosts. |
5028 | | * For the DECnet address use SWAPSHORT(), which always swaps bytes, |
5029 | | * because the wire encoding is little-endian and BPF multiple-byte |
5030 | | * loads are big-endian. When the destination address is near enough |
5031 | | * to PLENGTH and FLAGS, generate one 32-bit comparison instead of two |
5032 | | * smaller ones. |
5033 | | */ |
5034 | | /* Check for pad = 1, long header case */ |
5035 | 293 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 0x8106U, 0xFF07U); |
5036 | 293 | b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh, |
5037 | 293 | BPF_H, SWAPSHORT(addr)); |
5038 | 293 | gen_and(tmp, b1); |
5039 | | /* Check for pad = 0, long header case */ |
5040 | 293 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, 0x06U, 0x07U); |
5041 | 293 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, |
5042 | 293 | SWAPSHORT(addr)); |
5043 | 293 | gen_and(tmp, b2); |
5044 | 293 | gen_or(b2, b1); |
5045 | | /* Check for pad = 1, short header case */ |
5046 | 293 | if (dir == Q_DST) { |
5047 | 145 | b2 = gen_mcmp(cstate, OR_LINKPL, 2, BPF_W, |
5048 | 145 | 0x81020000U | SWAPSHORT(addr), |
5049 | 145 | 0xFF07FFFFU); |
5050 | 148 | } else { |
5051 | 148 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 0x8102U, 0xFF07U); |
5052 | 148 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, |
5053 | 148 | SWAPSHORT(addr)); |
5054 | 148 | gen_and(tmp, b2); |
5055 | 148 | } |
5056 | 293 | gen_or(b2, b1); |
5057 | | /* Check for pad = 0, short header case */ |
5058 | 293 | if (dir == Q_DST) { |
5059 | 145 | b2 = gen_mcmp(cstate, OR_LINKPL, 2, BPF_W, |
5060 | 145 | 0x02000000U | SWAPSHORT(addr) << 8, |
5061 | 145 | 0x07FFFF00U); |
5062 | 148 | } else { |
5063 | 148 | tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, 0x02U, 0x07U); |
5064 | 148 | b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, |
5065 | 148 | SWAPSHORT(addr)); |
5066 | 148 | gen_and(tmp, b2); |
5067 | 148 | } |
5068 | 293 | gen_or(b2, b1); |
5069 | | |
5070 | 293 | return b1; |
5071 | 447 | } |
5072 | | |
5073 | | /* |
5074 | | * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets; |
5075 | | * test the bottom-of-stack bit, and then check the version number |
5076 | | * field in the IP header. |
5077 | | */ |
5078 | | static struct block * |
5079 | | gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) |
5080 | 138 | { |
5081 | 138 | struct block *b0, *b1; |
5082 | | |
5083 | 138 | switch (ll_proto) { |
5084 | | |
5085 | 70 | case ETHERTYPE_IP: |
5086 | | /* match the bottom-of-stack bit */ |
5087 | 70 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
5088 | | /* match the IPv4 version number */ |
5089 | 70 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0); |
5090 | 70 | gen_and(b0, b1); |
5091 | 70 | return b1; |
5092 | | |
5093 | 43 | case ETHERTYPE_IPV6: |
5094 | | /* match the bottom-of-stack bit */ |
5095 | 43 | b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); |
5096 | | /* match the IPv6 version number */ |
5097 | 43 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0); |
5098 | 43 | gen_and(b0, b1); |
5099 | 43 | return b1; |
5100 | | |
5101 | 25 | default: |
5102 | | /* FIXME add other L3 proto IDs */ |
5103 | 25 | bpf_error(cstate, "unsupported protocol over mpls"); |
5104 | | /*NOTREACHED*/ |
5105 | 138 | } |
5106 | 138 | } |
5107 | | |
5108 | | static struct block * |
5109 | | gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, |
5110 | | int proto, int dir, int type) |
5111 | 34.0k | { |
5112 | 34.0k | struct block *b0, *b1; |
5113 | | |
5114 | 34.0k | switch (proto) { |
5115 | | |
5116 | 8.31k | case Q_DEFAULT: |
5117 | 8.31k | b0 = gen_host(cstate, addr, mask, Q_IP, dir, type); |
5118 | | /* |
5119 | | * Only check for non-IPv4 addresses if we're not |
5120 | | * checking MPLS-encapsulated packets. |
5121 | | */ |
5122 | 8.31k | if (cstate->label_stack_depth == 0) { |
5123 | 8.26k | b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type); |
5124 | 8.26k | gen_or(b0, b1); |
5125 | 8.26k | b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type); |
5126 | 8.26k | gen_or(b1, b0); |
5127 | 8.26k | } |
5128 | 8.31k | return b0; |
5129 | | |
5130 | 1 | case Q_LINK: |
5131 | | // "link net NETNAME" and variations thereof |
5132 | 1 | break; // invalid qualifier |
5133 | | |
5134 | 8.65k | case Q_IP: |
5135 | 8.65k | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
5136 | | /* |
5137 | | * Belt and braces: if other code works correctly, any host |
5138 | | * bits are clear and mask == 0 means addr == 0. In this case |
5139 | | * the call to gen_hostop() would produce an "always true" |
5140 | | * instruction block and ANDing it with the link type check |
5141 | | * would be a no-op. |
5142 | | */ |
5143 | 8.65k | if (mask == 0 && addr == 0) |
5144 | 80 | return b0; |
5145 | 8.57k | b1 = gen_hostop(cstate, addr, mask, dir, 12, 16); |
5146 | 8.57k | gen_and(b0, b1); |
5147 | 8.57k | return b1; |
5148 | | |
5149 | 8.37k | case Q_RARP: |
5150 | 8.37k | b0 = gen_linktype(cstate, ETHERTYPE_REVARP); |
5151 | | // Same as for Q_IP above. |
5152 | 8.37k | if (mask == 0 && addr == 0) |
5153 | 73 | return b0; |
5154 | 8.30k | b1 = gen_hostop(cstate, addr, mask, dir, 14, 24); |
5155 | 8.30k | gen_and(b0, b1); |
5156 | 8.30k | return b1; |
5157 | | |
5158 | 8.50k | case Q_ARP: |
5159 | 8.50k | b0 = gen_linktype(cstate, ETHERTYPE_ARP); |
5160 | | // Same as for Q_IP above. |
5161 | 8.50k | if (mask == 0 && addr == 0) |
5162 | 67 | return b0; |
5163 | 8.44k | b1 = gen_hostop(cstate, addr, mask, dir, 14, 24); |
5164 | 8.44k | gen_and(b0, b1); |
5165 | 8.44k | return b1; |
5166 | | |
5167 | 1 | case Q_SCTP: |
5168 | 2 | case Q_TCP: |
5169 | 4 | case Q_UDP: |
5170 | 5 | case Q_ICMP: |
5171 | 6 | case Q_IGMP: |
5172 | 7 | case Q_IGRP: |
5173 | 8 | case Q_ATALK: |
5174 | 8 | break; // invalid qualifier |
5175 | | |
5176 | 168 | case Q_DECNET: |
5177 | 168 | b0 = gen_linktype(cstate, ETHERTYPE_DN); |
5178 | 168 | b1 = gen_dnhostop(cstate, addr, dir); |
5179 | 168 | gen_and(b0, b1); |
5180 | 168 | return b1; |
5181 | | |
5182 | 2 | case Q_LAT: |
5183 | 3 | case Q_SCA: |
5184 | 4 | case Q_MOPRC: |
5185 | 5 | case Q_MOPDL: |
5186 | 6 | case Q_IPV6: |
5187 | 7 | case Q_ICMPV6: |
5188 | 9 | case Q_AH: |
5189 | 10 | case Q_ESP: |
5190 | 11 | case Q_PIM: |
5191 | 12 | case Q_VRRP: |
5192 | 13 | case Q_AARP: |
5193 | 14 | case Q_ISO: |
5194 | 15 | case Q_ESIS: |
5195 | 16 | case Q_ISIS: |
5196 | 17 | case Q_CLNP: |
5197 | 19 | case Q_STP: |
5198 | 20 | case Q_IPX: |
5199 | 21 | case Q_NETBEUI: |
5200 | 21 | case Q_ISIS_L1: |
5201 | 22 | case Q_ISIS_L2: |
5202 | 23 | case Q_ISIS_IIH: |
5203 | 24 | case Q_ISIS_SNP: |
5204 | 25 | case Q_ISIS_CSNP: |
5205 | 25 | case Q_ISIS_PSNP: |
5206 | 26 | case Q_ISIS_LSP: |
5207 | 27 | case Q_RADIO: |
5208 | 28 | case Q_CARP: |
5209 | 28 | break; // invalid qualifier |
5210 | | |
5211 | 0 | default: |
5212 | 0 | abort(); |
5213 | 34.0k | } |
5214 | 37 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), |
5215 | 37 | type == Q_NET ? "ip net" : "ip host"); |
5216 | | /*NOTREACHED*/ |
5217 | 34.0k | } |
5218 | | |
5219 | | static struct block * |
5220 | | gen_host6(compiler_state_t *cstate, struct in6_addr *addr, |
5221 | | struct in6_addr *mask, int proto, int dir, int type) |
5222 | 631 | { |
5223 | 631 | struct block *b0, *b1; |
5224 | | |
5225 | 631 | switch (proto) { |
5226 | | |
5227 | 514 | case Q_DEFAULT: |
5228 | 596 | case Q_IPV6: |
5229 | 596 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
5230 | | // Same as the Q_IP case in gen_host(). |
5231 | 596 | if ( |
5232 | 596 | ! memcmp(mask, &in6addr_any, sizeof(struct in6_addr)) && |
5233 | 596 | ! memcmp(addr, &in6addr_any, sizeof(struct in6_addr)) |
5234 | 596 | ) |
5235 | 3 | return b0; |
5236 | 593 | b1 = gen_hostop6(cstate, addr, mask, dir, 8, 24); |
5237 | 593 | gen_and(b0, b1); |
5238 | 593 | return b1; |
5239 | | |
5240 | 2 | case Q_LINK: |
5241 | 2 | case Q_IP: |
5242 | 3 | case Q_RARP: |
5243 | 3 | case Q_ARP: |
5244 | 4 | case Q_SCTP: |
5245 | 5 | case Q_TCP: |
5246 | 6 | case Q_UDP: |
5247 | 7 | case Q_ICMP: |
5248 | 8 | case Q_IGMP: |
5249 | 9 | case Q_IGRP: |
5250 | 10 | case Q_ATALK: |
5251 | 11 | case Q_DECNET: |
5252 | 12 | case Q_LAT: |
5253 | 13 | case Q_SCA: |
5254 | 14 | case Q_MOPRC: |
5255 | 15 | case Q_MOPDL: |
5256 | 16 | case Q_ICMPV6: |
5257 | 17 | case Q_AH: |
5258 | 17 | case Q_ESP: |
5259 | 18 | case Q_PIM: |
5260 | 19 | case Q_VRRP: |
5261 | 20 | case Q_AARP: |
5262 | 21 | case Q_ISO: |
5263 | 22 | case Q_ESIS: |
5264 | 23 | case Q_ISIS: |
5265 | 24 | case Q_CLNP: |
5266 | 25 | case Q_STP: |
5267 | 26 | case Q_IPX: |
5268 | 26 | case Q_NETBEUI: |
5269 | 27 | case Q_ISIS_L1: |
5270 | 28 | case Q_ISIS_L2: |
5271 | 29 | case Q_ISIS_IIH: |
5272 | 30 | case Q_ISIS_SNP: |
5273 | 31 | case Q_ISIS_CSNP: |
5274 | 32 | case Q_ISIS_PSNP: |
5275 | 33 | case Q_ISIS_LSP: |
5276 | 34 | case Q_RADIO: |
5277 | 35 | case Q_CARP: |
5278 | 35 | break; // invalid qualifier |
5279 | | |
5280 | 0 | default: |
5281 | 0 | abort(); |
5282 | 631 | } |
5283 | 35 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), |
5284 | 35 | type == Q_NET ? "ip6 net" : "ip6 host"); |
5285 | | /*NOTREACHED*/ |
5286 | 631 | } |
5287 | | |
5288 | | static unsigned char |
5289 | | is_mac48_linktype(const int linktype) |
5290 | 744 | { |
5291 | 744 | switch (linktype) { |
5292 | 106 | case DLT_EN10MB: |
5293 | 136 | case DLT_FDDI: |
5294 | 187 | case DLT_IEEE802: |
5295 | 300 | case DLT_IEEE802_11: |
5296 | 372 | case DLT_IEEE802_11_RADIO: |
5297 | 417 | case DLT_IEEE802_11_RADIO_AVS: |
5298 | 471 | case DLT_IP_OVER_FC: |
5299 | 501 | case DLT_NETANALYZER: |
5300 | 545 | case DLT_NETANALYZER_TRANSPARENT: |
5301 | 689 | case DLT_PPI: |
5302 | 739 | case DLT_PRISM_HEADER: |
5303 | 739 | return 1; |
5304 | 5 | default: |
5305 | 5 | return 0; |
5306 | 744 | } |
5307 | 744 | } |
5308 | | |
5309 | | static struct block * |
5310 | | gen_mac48host(compiler_state_t *cstate, const u_char *eaddr, const u_char dir, |
5311 | | const char *keyword) |
5312 | 707 | { |
5313 | 707 | struct block *b1 = NULL; |
5314 | 707 | u_int src_off, dst_off; |
5315 | | |
5316 | 707 | switch (cstate->linktype) { |
5317 | 101 | case DLT_EN10MB: |
5318 | 129 | case DLT_NETANALYZER: |
5319 | 171 | case DLT_NETANALYZER_TRANSPARENT: |
5320 | 171 | b1 = gen_prevlinkhdr_check(cstate); |
5321 | 171 | src_off = 6; |
5322 | 171 | dst_off = 0; |
5323 | 171 | break; |
5324 | 29 | case DLT_FDDI: |
5325 | 29 | src_off = 6 + 1 + cstate->pcap_fddipad; |
5326 | 29 | dst_off = 0 + 1 + cstate->pcap_fddipad; |
5327 | 29 | break; |
5328 | 49 | case DLT_IEEE802: |
5329 | 49 | src_off = 8; |
5330 | 49 | dst_off = 2; |
5331 | 49 | break; |
5332 | 107 | case DLT_IEEE802_11: |
5333 | 153 | case DLT_PRISM_HEADER: |
5334 | 196 | case DLT_IEEE802_11_RADIO_AVS: |
5335 | 267 | case DLT_IEEE802_11_RADIO: |
5336 | 406 | case DLT_PPI: |
5337 | 406 | return gen_wlanhostop(cstate, eaddr, dir); |
5338 | 52 | case DLT_IP_OVER_FC: |
5339 | | /* |
5340 | | * Assume that the addresses are IEEE 48-bit MAC addresses, |
5341 | | * as RFC 2625 states. |
5342 | | */ |
5343 | 52 | src_off = 10; |
5344 | 52 | dst_off = 2; |
5345 | 52 | break; |
5346 | 0 | case DLT_SUNATM: |
5347 | | /* |
5348 | | * This is LLC-multiplexed traffic; if it were |
5349 | | * LANE, cstate->linktype would have been set to |
5350 | | * DLT_EN10MB. |
5351 | | */ |
5352 | | /* FALLTHROUGH */ |
5353 | 0 | default: |
5354 | 0 | fail_kw_on_dlt(cstate, keyword); |
5355 | 707 | } |
5356 | | |
5357 | 301 | struct block *b0, *tmp; |
5358 | | |
5359 | 301 | switch (dir) { |
5360 | 51 | case Q_SRC: |
5361 | 51 | b0 = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5362 | 51 | break; |
5363 | 25 | case Q_DST: |
5364 | 25 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5365 | 25 | break; |
5366 | 70 | case Q_AND: |
5367 | 70 | tmp = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5368 | 70 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5369 | 70 | gen_and(tmp, b0); |
5370 | 70 | break; |
5371 | 63 | case Q_DEFAULT: |
5372 | 146 | case Q_OR: |
5373 | 146 | tmp = gen_bcmp(cstate, OR_LINKHDR, src_off, 6, eaddr); |
5374 | 146 | b0 = gen_bcmp(cstate, OR_LINKHDR, dst_off, 6, eaddr); |
5375 | 146 | gen_or(tmp, b0); |
5376 | 146 | break; |
5377 | 9 | default: |
5378 | 9 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
5379 | 301 | } |
5380 | | |
5381 | 292 | if (b1 != NULL) |
5382 | 58 | gen_and(b1, b0); |
5383 | 292 | return b0; |
5384 | 301 | } |
5385 | | |
5386 | | static struct block * |
5387 | | gen_mac48host_byname(compiler_state_t *cstate, const char *name, |
5388 | | const u_char dir, const char *context) |
5389 | 35 | { |
5390 | 35 | if (! is_mac48_linktype(cstate->linktype)) |
5391 | 3 | fail_kw_on_dlt(cstate, context); |
5392 | | |
5393 | 32 | u_char *eaddrp = pcap_ether_hostton(name); |
5394 | 32 | if (eaddrp == NULL) |
5395 | 32 | bpf_error(cstate, ERRSTR_UNKNOWN_MAC48HOST, name); |
5396 | 0 | u_char eaddr[6]; |
5397 | 0 | memcpy(eaddr, eaddrp, sizeof(eaddr)); |
5398 | 0 | free(eaddrp); |
5399 | |
|
5400 | 0 | return gen_mac48host(cstate, eaddr, dir, context); |
5401 | 32 | } |
5402 | | |
5403 | | /* |
5404 | | * This primitive is non-directional by design, so the grammar does not allow |
5405 | | * to qualify it with a direction. |
5406 | | */ |
5407 | | static struct block * |
5408 | | gen_gateway(compiler_state_t *cstate, const char *name, |
5409 | | struct addrinfo *alist, int proto) |
5410 | 0 | { |
5411 | 0 | struct block *b0, *b1, *tmp; |
5412 | 0 | struct addrinfo *ai; |
5413 | 0 | struct sockaddr_in *sin; |
5414 | |
|
5415 | 0 | switch (proto) { |
5416 | 0 | case Q_DEFAULT: |
5417 | 0 | case Q_IP: |
5418 | 0 | case Q_ARP: |
5419 | 0 | case Q_RARP: |
5420 | 0 | b0 = gen_mac48host_byname(cstate, name, Q_OR, "gateway"); |
5421 | 0 | b1 = NULL; |
5422 | 0 | for (ai = alist; ai != NULL; ai = ai->ai_next) { |
5423 | | /* |
5424 | | * Does it have an address? |
5425 | | */ |
5426 | 0 | if (ai->ai_addr != NULL) { |
5427 | | /* |
5428 | | * Yes. Is it an IPv4 address? |
5429 | | */ |
5430 | 0 | if (ai->ai_addr->sa_family == AF_INET) { |
5431 | | /* |
5432 | | * Generate an entry for it. |
5433 | | */ |
5434 | 0 | sin = (struct sockaddr_in *)ai->ai_addr; |
5435 | 0 | tmp = gen_host(cstate, |
5436 | 0 | ntohl(sin->sin_addr.s_addr), |
5437 | 0 | 0xffffffff, proto, Q_OR, Q_HOST); |
5438 | | /* |
5439 | | * Is it the *first* IPv4 address? |
5440 | | */ |
5441 | 0 | if (b1 == NULL) { |
5442 | | /* |
5443 | | * Yes, so start with it. |
5444 | | */ |
5445 | 0 | b1 = tmp; |
5446 | 0 | } else { |
5447 | | /* |
5448 | | * No, so OR it into the |
5449 | | * existing set of |
5450 | | * addresses. |
5451 | | */ |
5452 | 0 | gen_or(b1, tmp); |
5453 | 0 | b1 = tmp; |
5454 | 0 | } |
5455 | 0 | } |
5456 | 0 | } |
5457 | 0 | } |
5458 | 0 | if (b1 == NULL) { |
5459 | | /* |
5460 | | * No IPv4 addresses found. |
5461 | | */ |
5462 | 0 | return (NULL); |
5463 | 0 | } |
5464 | 0 | gen_not(b1); |
5465 | 0 | gen_and(b0, b1); |
5466 | 0 | return b1; |
5467 | 0 | } |
5468 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "gateway"); |
5469 | | /*NOTREACHED*/ |
5470 | 0 | } |
5471 | | |
5472 | | static struct block * |
5473 | | gen_proto_abbrev_internal(compiler_state_t *cstate, int proto) |
5474 | 8.17k | { |
5475 | 8.17k | struct block *b0; |
5476 | 8.17k | struct block *b1; |
5477 | | |
5478 | 8.17k | switch (proto) { |
5479 | | |
5480 | 129 | case Q_SCTP: |
5481 | 129 | return gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT); |
5482 | | |
5483 | 356 | case Q_TCP: |
5484 | 356 | return gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT); |
5485 | | |
5486 | 481 | case Q_UDP: |
5487 | 481 | return gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT); |
5488 | | |
5489 | 76 | case Q_ICMP: |
5490 | 76 | return gen_proto(cstate, IPPROTO_ICMP, Q_IP); |
5491 | | |
5492 | | #ifndef IPPROTO_IGMP |
5493 | | #define IPPROTO_IGMP 2 |
5494 | | #endif |
5495 | | |
5496 | 43 | case Q_IGMP: |
5497 | 43 | return gen_proto(cstate, IPPROTO_IGMP, Q_IP); |
5498 | | |
5499 | 0 | #ifndef IPPROTO_IGRP |
5500 | 62 | #define IPPROTO_IGRP 9 |
5501 | 0 | #endif |
5502 | 62 | case Q_IGRP: |
5503 | 62 | return gen_proto(cstate, IPPROTO_IGRP, Q_IP); |
5504 | | |
5505 | | #ifndef IPPROTO_PIM |
5506 | | #define IPPROTO_PIM 103 |
5507 | | #endif |
5508 | | |
5509 | 189 | case Q_PIM: |
5510 | 189 | return gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT); |
5511 | | |
5512 | 0 | #ifndef IPPROTO_VRRP |
5513 | 51 | #define IPPROTO_VRRP 112 |
5514 | 0 | #endif |
5515 | | |
5516 | 51 | case Q_VRRP: |
5517 | 51 | return gen_proto(cstate, IPPROTO_VRRP, Q_IP); |
5518 | | |
5519 | 0 | #ifndef IPPROTO_CARP |
5520 | 36 | #define IPPROTO_CARP 112 |
5521 | 0 | #endif |
5522 | | |
5523 | 36 | case Q_CARP: |
5524 | 36 | return gen_proto(cstate, IPPROTO_CARP, Q_IP); |
5525 | | |
5526 | 2.03k | case Q_IP: |
5527 | 2.03k | return gen_linktype(cstate, ETHERTYPE_IP); |
5528 | | |
5529 | 29 | case Q_ARP: |
5530 | 29 | return gen_linktype(cstate, ETHERTYPE_ARP); |
5531 | | |
5532 | 30 | case Q_RARP: |
5533 | 30 | return gen_linktype(cstate, ETHERTYPE_REVARP); |
5534 | | |
5535 | 1 | case Q_LINK: |
5536 | 1 | break; // invalid syntax |
5537 | | |
5538 | 315 | case Q_ATALK: |
5539 | 315 | return gen_linktype(cstate, ETHERTYPE_ATALK); |
5540 | | |
5541 | 45 | case Q_AARP: |
5542 | 45 | return gen_linktype(cstate, ETHERTYPE_AARP); |
5543 | | |
5544 | 50 | case Q_DECNET: |
5545 | 50 | return gen_linktype(cstate, ETHERTYPE_DN); |
5546 | | |
5547 | 36 | case Q_SCA: |
5548 | 36 | return gen_linktype(cstate, ETHERTYPE_SCA); |
5549 | | |
5550 | 58 | case Q_LAT: |
5551 | 58 | return gen_linktype(cstate, ETHERTYPE_LAT); |
5552 | | |
5553 | 25 | case Q_MOPDL: |
5554 | 25 | return gen_linktype(cstate, ETHERTYPE_MOPDL); |
5555 | | |
5556 | 63 | case Q_MOPRC: |
5557 | 63 | return gen_linktype(cstate, ETHERTYPE_MOPRC); |
5558 | | |
5559 | 399 | case Q_IPV6: |
5560 | 399 | return gen_linktype(cstate, ETHERTYPE_IPV6); |
5561 | | |
5562 | | #ifndef IPPROTO_ICMPV6 |
5563 | | #define IPPROTO_ICMPV6 58 |
5564 | | #endif |
5565 | 9 | case Q_ICMPV6: |
5566 | 9 | return gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6); |
5567 | | |
5568 | | #ifndef IPPROTO_AH |
5569 | | #define IPPROTO_AH 51 |
5570 | | #endif |
5571 | 181 | case Q_AH: |
5572 | 181 | return gen_proto(cstate, IPPROTO_AH, Q_DEFAULT); |
5573 | | |
5574 | | #ifndef IPPROTO_ESP |
5575 | | #define IPPROTO_ESP 50 |
5576 | | #endif |
5577 | 51 | case Q_ESP: |
5578 | 51 | return gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT); |
5579 | | |
5580 | 35 | case Q_ISO: |
5581 | 35 | return gen_linktype(cstate, LLCSAP_ISONS); |
5582 | | |
5583 | 47 | case Q_ESIS: |
5584 | 47 | return gen_proto(cstate, ISO9542_ESIS, Q_ISO); |
5585 | | |
5586 | 102 | case Q_ISIS: |
5587 | 102 | return gen_proto(cstate, ISO10589_ISIS, Q_ISO); |
5588 | | |
5589 | 1.27k | case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */ |
5590 | 1.27k | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS); |
5591 | 1.27k | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); /* FIXME extract the circuit-type bits */ |
5592 | 1.27k | gen_or(b0, b1); |
5593 | 1.27k | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS); |
5594 | 1.27k | gen_or(b0, b1); |
5595 | 1.27k | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5596 | 1.27k | gen_or(b0, b1); |
5597 | 1.27k | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5598 | 1.27k | gen_or(b0, b1); |
5599 | 1.27k | return b1; |
5600 | | |
5601 | 1.06k | case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */ |
5602 | 1.06k | b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS); |
5603 | 1.06k | b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); /* FIXME extract the circuit-type bits */ |
5604 | 1.06k | gen_or(b0, b1); |
5605 | 1.06k | b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS); |
5606 | 1.06k | gen_or(b0, b1); |
5607 | 1.06k | b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5608 | 1.06k | gen_or(b0, b1); |
5609 | 1.06k | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5610 | 1.06k | gen_or(b0, b1); |
5611 | 1.06k | return b1; |
5612 | | |
5613 | 101 | case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */ |
5614 | 101 | b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS); |
5615 | 101 | b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS); |
5616 | 101 | gen_or(b0, b1); |
5617 | 101 | b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS); |
5618 | 101 | gen_or(b0, b1); |
5619 | 101 | return b1; |
5620 | | |
5621 | 55 | case Q_ISIS_LSP: |
5622 | 55 | b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS); |
5623 | 55 | b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS); |
5624 | 55 | gen_or(b0, b1); |
5625 | 55 | return b1; |
5626 | | |
5627 | 124 | case Q_ISIS_SNP: |
5628 | 124 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5629 | 124 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5630 | 124 | gen_or(b0, b1); |
5631 | 124 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5632 | 124 | gen_or(b0, b1); |
5633 | 124 | b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5634 | 124 | gen_or(b0, b1); |
5635 | 124 | return b1; |
5636 | | |
5637 | 20 | case Q_ISIS_CSNP: |
5638 | 20 | b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS); |
5639 | 20 | b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS); |
5640 | 20 | gen_or(b0, b1); |
5641 | 20 | return b1; |
5642 | | |
5643 | 48 | case Q_ISIS_PSNP: |
5644 | 48 | b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS); |
5645 | 48 | b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS); |
5646 | 48 | gen_or(b0, b1); |
5647 | 48 | return b1; |
5648 | | |
5649 | 20 | case Q_CLNP: |
5650 | 20 | return gen_proto(cstate, ISO8473_CLNP, Q_ISO); |
5651 | | |
5652 | 199 | case Q_STP: |
5653 | 199 | return gen_linktype(cstate, LLCSAP_8021D); |
5654 | | |
5655 | 298 | case Q_IPX: |
5656 | 298 | return gen_linktype(cstate, LLCSAP_IPX); |
5657 | | |
5658 | 33 | case Q_NETBEUI: |
5659 | 33 | return gen_linktype(cstate, LLCSAP_NETBEUI); |
5660 | | |
5661 | 1 | case Q_RADIO: |
5662 | 1 | break; // invalid syntax |
5663 | | |
5664 | 0 | default: |
5665 | 0 | abort(); |
5666 | 8.17k | } |
5667 | 2 | bpf_error(cstate, "'%s' cannot be used as an abbreviation", pqkw(proto)); |
5668 | 8.17k | } |
5669 | | |
5670 | | struct block * |
5671 | | gen_proto_abbrev(compiler_state_t *cstate, int proto) |
5672 | 4.28k | { |
5673 | | /* |
5674 | | * Catch errors reported by us and routines below us, and return NULL |
5675 | | * on an error. |
5676 | | */ |
5677 | 4.28k | if (setjmp(cstate->top_ctx)) |
5678 | 33 | return (NULL); |
5679 | | |
5680 | 4.25k | return gen_proto_abbrev_internal(cstate, proto); |
5681 | 4.28k | } |
5682 | | |
5683 | | static struct block * |
5684 | | gen_ip_proto(compiler_state_t *cstate, const uint8_t proto) |
5685 | 17.2k | { |
5686 | 17.2k | return gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto); |
5687 | 17.2k | } |
5688 | | |
5689 | | static struct block * |
5690 | | gen_ip6_proto(compiler_state_t *cstate, const uint8_t proto) |
5691 | 19.1k | { |
5692 | 19.1k | return gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto); |
5693 | 19.1k | } |
5694 | | |
5695 | | static struct block * |
5696 | | gen_ipfrag(compiler_state_t *cstate) |
5697 | 7.01k | { |
5698 | 7.01k | struct slist *s; |
5699 | | |
5700 | | /* not IPv4 frag other than the first frag */ |
5701 | 7.01k | s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H); |
5702 | 7.01k | return gen_unset(cstate, 0x1fff, s); |
5703 | 7.01k | } |
5704 | | |
5705 | | /* |
5706 | | * Generate a comparison to a port value in the transport-layer header |
5707 | | * at the specified offset from the beginning of that header. |
5708 | | * |
5709 | | * XXX - this handles a variable-length prefix preceding the link-layer |
5710 | | * header, such as the radiotap or AVS radio prefix, but doesn't handle |
5711 | | * variable-length link-layer headers (such as Token Ring or 802.11 |
5712 | | * headers). |
5713 | | */ |
5714 | | static struct block * |
5715 | | gen_portatom(compiler_state_t *cstate, int off, uint16_t v) |
5716 | 10.4k | { |
5717 | 10.4k | return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v); |
5718 | 10.4k | } |
5719 | | |
5720 | | static struct block * |
5721 | | gen_portatom6(compiler_state_t *cstate, int off, uint16_t v) |
5722 | 10.4k | { |
5723 | 10.4k | return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v); |
5724 | 10.4k | } |
5725 | | |
5726 | | static struct block * |
5727 | | gen_port(compiler_state_t *cstate, uint16_t port, int proto, int dir) |
5728 | 5.67k | { |
5729 | 5.67k | struct block *b1, *tmp; |
5730 | | |
5731 | 5.67k | switch (dir) { |
5732 | 37 | case Q_SRC: |
5733 | 37 | b1 = gen_portatom(cstate, 0, port); |
5734 | 37 | break; |
5735 | | |
5736 | 815 | case Q_DST: |
5737 | 815 | b1 = gen_portatom(cstate, 2, port); |
5738 | 815 | break; |
5739 | | |
5740 | 131 | case Q_AND: |
5741 | 131 | tmp = gen_portatom(cstate, 0, port); |
5742 | 131 | b1 = gen_portatom(cstate, 2, port); |
5743 | 131 | gen_and(tmp, b1); |
5744 | 131 | break; |
5745 | | |
5746 | 4.63k | case Q_DEFAULT: |
5747 | 4.68k | case Q_OR: |
5748 | 4.68k | tmp = gen_portatom(cstate, 0, port); |
5749 | 4.68k | b1 = gen_portatom(cstate, 2, port); |
5750 | 4.68k | gen_or(tmp, b1); |
5751 | 4.68k | break; |
5752 | | |
5753 | 1 | case Q_ADDR1: |
5754 | 2 | case Q_ADDR2: |
5755 | 3 | case Q_ADDR3: |
5756 | 4 | case Q_ADDR4: |
5757 | 5 | case Q_RA: |
5758 | 6 | case Q_TA: |
5759 | 6 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "port"); |
5760 | | /*NOTREACHED*/ |
5761 | | |
5762 | 0 | default: |
5763 | 0 | abort(); |
5764 | | /*NOTREACHED*/ |
5765 | 5.67k | } |
5766 | | |
5767 | 5.66k | return gen_port_common(cstate, proto, b1); |
5768 | 5.67k | } |
5769 | | |
5770 | | static struct block * |
5771 | | gen_port_common(compiler_state_t *cstate, int proto, struct block *b1) |
5772 | 5.66k | { |
5773 | 5.66k | struct block *b0, *tmp; |
5774 | | |
5775 | | /* |
5776 | | * ether proto ip |
5777 | | * |
5778 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
5779 | | * not LLC encapsulation with LLCSAP_IP. |
5780 | | * |
5781 | | * For IEEE 802 networks - which includes 802.5 token ring |
5782 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
5783 | | * says that SNAP encapsulation is used, not LLC encapsulation |
5784 | | * with LLCSAP_IP. |
5785 | | * |
5786 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
5787 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
5788 | | * encapsulation with LLCSAP_IP. |
5789 | | * |
5790 | | * So we always check for ETHERTYPE_IP. |
5791 | | * |
5792 | | * At the time of this writing all three L4 protocols the "port" and |
5793 | | * "portrange" primitives support (TCP, UDP and SCTP) have the source |
5794 | | * and the destination ports identically encoded in the transport |
5795 | | * protocol header. So without a proto qualifier the only difference |
5796 | | * between the implemented cases is the protocol number and all other |
5797 | | * checks need to be made exactly once. |
5798 | | * |
5799 | | * If the expression syntax in future starts to support ports for |
5800 | | * another L4 protocol that has unsigned integer ports encoded using a |
5801 | | * different size and/or offset, this will require a different code. |
5802 | | */ |
5803 | 5.66k | switch (proto) { |
5804 | 786 | case IPPROTO_UDP: |
5805 | 920 | case IPPROTO_TCP: |
5806 | 990 | case IPPROTO_SCTP: |
5807 | 990 | tmp = gen_ip_proto(cstate, (uint8_t)proto); |
5808 | 990 | break; |
5809 | | |
5810 | 4.67k | case PROTO_UNDEF: |
5811 | 4.67k | tmp = gen_ip_proto(cstate, IPPROTO_UDP); |
5812 | 4.67k | gen_or(gen_ip_proto(cstate, IPPROTO_TCP), tmp); |
5813 | 4.67k | gen_or(gen_ip_proto(cstate, IPPROTO_SCTP), tmp); |
5814 | 4.67k | break; |
5815 | | |
5816 | 0 | default: |
5817 | 0 | abort(); |
5818 | 5.66k | } |
5819 | | // Not a fragment other than the first fragment. |
5820 | 5.66k | b0 = gen_ipfrag(cstate); |
5821 | 5.66k | gen_and(tmp, b0); |
5822 | 5.66k | gen_and(b0, b1); |
5823 | | // "link proto \ip" |
5824 | 5.66k | gen_and(gen_linktype(cstate, ETHERTYPE_IP), b1); |
5825 | 5.66k | return b1; |
5826 | 5.66k | } |
5827 | | |
5828 | | static struct block * |
5829 | | gen_port6(compiler_state_t *cstate, uint16_t port, int proto, int dir) |
5830 | 5.64k | { |
5831 | 5.64k | struct block *b1, *tmp; |
5832 | | |
5833 | 5.64k | switch (dir) { |
5834 | 37 | case Q_SRC: |
5835 | 37 | b1 = gen_portatom6(cstate, 0, port); |
5836 | 37 | break; |
5837 | | |
5838 | 805 | case Q_DST: |
5839 | 805 | b1 = gen_portatom6(cstate, 2, port); |
5840 | 805 | break; |
5841 | | |
5842 | 131 | case Q_AND: |
5843 | 131 | tmp = gen_portatom6(cstate, 0, port); |
5844 | 131 | b1 = gen_portatom6(cstate, 2, port); |
5845 | 131 | gen_and(tmp, b1); |
5846 | 131 | break; |
5847 | | |
5848 | 4.62k | case Q_DEFAULT: |
5849 | 4.67k | case Q_OR: |
5850 | 4.67k | tmp = gen_portatom6(cstate, 0, port); |
5851 | 4.67k | b1 = gen_portatom6(cstate, 2, port); |
5852 | 4.67k | gen_or(tmp, b1); |
5853 | 4.67k | break; |
5854 | | |
5855 | 0 | default: |
5856 | 0 | abort(); |
5857 | 5.64k | } |
5858 | | |
5859 | 5.64k | return gen_port6_common(cstate, proto, b1); |
5860 | 5.64k | } |
5861 | | |
5862 | | static struct block * |
5863 | | gen_port6_common(compiler_state_t *cstate, int proto, struct block *b1) |
5864 | 5.64k | { |
5865 | 5.64k | struct block *tmp; |
5866 | | |
5867 | | // "ip6 proto 'ip_proto'" |
5868 | 5.64k | switch (proto) { |
5869 | 776 | case IPPROTO_UDP: |
5870 | 910 | case IPPROTO_TCP: |
5871 | 980 | case IPPROTO_SCTP: |
5872 | 980 | tmp = gen_ip6_proto(cstate, (uint8_t)proto); |
5873 | 980 | break; |
5874 | | |
5875 | 4.66k | case PROTO_UNDEF: |
5876 | | // Same as in gen_port_common(). |
5877 | 4.66k | tmp = gen_ip6_proto(cstate, IPPROTO_UDP); |
5878 | 4.66k | gen_or(gen_ip6_proto(cstate, IPPROTO_TCP), tmp); |
5879 | 4.66k | gen_or(gen_ip6_proto(cstate, IPPROTO_SCTP), tmp); |
5880 | 4.66k | break; |
5881 | | |
5882 | 0 | default: |
5883 | 0 | abort(); |
5884 | 5.64k | } |
5885 | | // XXX - catch the first fragment of a fragmented packet? |
5886 | 5.64k | gen_and(tmp, b1); |
5887 | | // "link proto \ip6" |
5888 | 5.64k | gen_and(gen_linktype(cstate, ETHERTYPE_IPV6), b1); |
5889 | 5.64k | return b1; |
5890 | 5.64k | } |
5891 | | |
5892 | | /* gen_portrange code */ |
5893 | | static struct block * |
5894 | | gen_portrangeatom(compiler_state_t *cstate, u_int off, uint16_t v1, |
5895 | | uint16_t v2) |
5896 | 0 | { |
5897 | 0 | if (v1 == v2) |
5898 | 0 | return gen_portatom(cstate, off, v1); |
5899 | | |
5900 | 0 | struct block *b1, *b2; |
5901 | |
|
5902 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, min(v1, v2)); |
5903 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, max(v1, v2)); |
5904 | |
|
5905 | 0 | gen_and(b1, b2); |
5906 | |
|
5907 | 0 | return b2; |
5908 | 0 | } |
5909 | | |
5910 | | static struct block * |
5911 | | gen_portrange(compiler_state_t *cstate, uint16_t port1, uint16_t port2, |
5912 | | int proto, int dir) |
5913 | 0 | { |
5914 | 0 | struct block *b1, *tmp; |
5915 | |
|
5916 | 0 | switch (dir) { |
5917 | 0 | case Q_SRC: |
5918 | 0 | b1 = gen_portrangeatom(cstate, 0, port1, port2); |
5919 | 0 | break; |
5920 | | |
5921 | 0 | case Q_DST: |
5922 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5923 | 0 | break; |
5924 | | |
5925 | 0 | case Q_AND: |
5926 | 0 | tmp = gen_portrangeatom(cstate, 0, port1, port2); |
5927 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5928 | 0 | gen_and(tmp, b1); |
5929 | 0 | break; |
5930 | | |
5931 | 0 | case Q_DEFAULT: |
5932 | 0 | case Q_OR: |
5933 | 0 | tmp = gen_portrangeatom(cstate, 0, port1, port2); |
5934 | 0 | b1 = gen_portrangeatom(cstate, 2, port1, port2); |
5935 | 0 | gen_or(tmp, b1); |
5936 | 0 | break; |
5937 | | |
5938 | 0 | case Q_ADDR1: |
5939 | 0 | case Q_ADDR2: |
5940 | 0 | case Q_ADDR3: |
5941 | 0 | case Q_ADDR4: |
5942 | 0 | case Q_RA: |
5943 | 0 | case Q_TA: |
5944 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, dqkw(dir), "portrange"); |
5945 | | /*NOTREACHED*/ |
5946 | | |
5947 | 0 | default: |
5948 | 0 | abort(); |
5949 | | /*NOTREACHED*/ |
5950 | 0 | } |
5951 | | |
5952 | 0 | return gen_port_common(cstate, proto, b1); |
5953 | 0 | } |
5954 | | |
5955 | | static struct block * |
5956 | | gen_portrangeatom6(compiler_state_t *cstate, u_int off, uint16_t v1, |
5957 | | uint16_t v2) |
5958 | 0 | { |
5959 | 0 | if (v1 == v2) |
5960 | 0 | return gen_portatom6(cstate, off, v1); |
5961 | | |
5962 | 0 | struct block *b1, *b2; |
5963 | |
|
5964 | 0 | b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, min(v1, v2)); |
5965 | 0 | b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, max(v1, v2)); |
5966 | |
|
5967 | 0 | gen_and(b1, b2); |
5968 | |
|
5969 | 0 | return b2; |
5970 | 0 | } |
5971 | | |
5972 | | static struct block * |
5973 | | gen_portrange6(compiler_state_t *cstate, uint16_t port1, uint16_t port2, |
5974 | | int proto, int dir) |
5975 | 0 | { |
5976 | 0 | struct block *b1, *tmp; |
5977 | |
|
5978 | 0 | switch (dir) { |
5979 | 0 | case Q_SRC: |
5980 | 0 | b1 = gen_portrangeatom6(cstate, 0, port1, port2); |
5981 | 0 | break; |
5982 | | |
5983 | 0 | case Q_DST: |
5984 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5985 | 0 | break; |
5986 | | |
5987 | 0 | case Q_AND: |
5988 | 0 | tmp = gen_portrangeatom6(cstate, 0, port1, port2); |
5989 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5990 | 0 | gen_and(tmp, b1); |
5991 | 0 | break; |
5992 | | |
5993 | 0 | case Q_DEFAULT: |
5994 | 0 | case Q_OR: |
5995 | 0 | tmp = gen_portrangeatom6(cstate, 0, port1, port2); |
5996 | 0 | b1 = gen_portrangeatom6(cstate, 2, port1, port2); |
5997 | 0 | gen_or(tmp, b1); |
5998 | 0 | break; |
5999 | | |
6000 | 0 | default: |
6001 | 0 | abort(); |
6002 | 0 | } |
6003 | | |
6004 | 0 | return gen_port6_common(cstate, proto, b1); |
6005 | 0 | } |
6006 | | |
6007 | | static int |
6008 | | lookup_proto(compiler_state_t *cstate, const char *name, const struct qual q) |
6009 | 579 | { |
6010 | | /* |
6011 | | * Do not check here whether q.proto is valid (e.g. in "udp proto abc" |
6012 | | * fail the "abc", but not the "udp proto"). Likewise, do not check |
6013 | | * here whether the combination of q.proto and q.addr is valid (e.g. |
6014 | | * in "(link|iso|isis) protochain abc" fail the "abc", but not the |
6015 | | * "(link|iso|isis) protochain"). |
6016 | | * |
6017 | | * On the one hand, this avoids a layering violation: gen_proto() and |
6018 | | * gen_protochain() implement the semantic checks. On the other hand, |
6019 | | * the protocol name lookup error arguably is a problem smaller than |
6020 | | * the semantic error, hence the latter ought to be the reported cause |
6021 | | * of failure in both cases. In future this potentially could be made |
6022 | | * more consistent by attempting the lookup after the semantic checks. |
6023 | | */ |
6024 | | |
6025 | 579 | int v = PROTO_UNDEF; |
6026 | 579 | switch (q.proto) { |
6027 | | |
6028 | 255 | case Q_DEFAULT: |
6029 | 267 | case Q_IP: |
6030 | 278 | case Q_IPV6: |
6031 | 278 | v = pcap_nametoproto(name); |
6032 | 278 | break; |
6033 | | |
6034 | 153 | case Q_LINK: |
6035 | | /* XXX should look up h/w protocol type based on cstate->linktype */ |
6036 | 153 | v = pcap_nametoeproto(name); |
6037 | 153 | if (v == PROTO_UNDEF) |
6038 | 118 | v = pcap_nametollc(name); |
6039 | 153 | break; |
6040 | | |
6041 | 147 | case Q_ISO: |
6042 | 147 | if (strcmp(name, "esis") == 0) |
6043 | 11 | v = ISO9542_ESIS; |
6044 | 136 | else if (strcmp(name, "isis") == 0) |
6045 | 35 | v = ISO10589_ISIS; |
6046 | 101 | else if (strcmp(name, "clnp") == 0) |
6047 | 25 | v = ISO8473_CLNP; |
6048 | 147 | break; |
6049 | | |
6050 | | // "isis proto" is a valid syntax, but it takes only numeric IDs. |
6051 | 579 | } |
6052 | | // In theory, the only possible negative value of v is PROTO_UNDEF. |
6053 | 579 | if (v >= 0) |
6054 | 155 | return v; |
6055 | | |
6056 | 424 | if (q.proto == Q_DEFAULT) |
6057 | 255 | bpf_error(cstate, "unknown '%s' value '%s'", |
6058 | 255 | tqkw(q.addr), name); |
6059 | 169 | bpf_error(cstate, "unknown '%s %s' value '%s'", |
6060 | 169 | pqkw(q.proto), tqkw(q.addr), name); |
6061 | 424 | } |
6062 | | |
6063 | | #if !defined(NO_PROTOCHAIN) |
6064 | | /* |
6065 | | * This primitive is non-directional by design, so the grammar does not allow |
6066 | | * to qualify it with a direction. |
6067 | | */ |
6068 | | static struct block * |
6069 | | gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto) |
6070 | 2.18k | { |
6071 | 2.18k | struct block *b0, *b; |
6072 | 2.18k | struct slist *s[100]; |
6073 | 2.18k | int fix2, fix3, fix4, fix5; |
6074 | 2.18k | int ahcheck, again, end; |
6075 | 2.18k | int i, max; |
6076 | 2.18k | int reg2 = alloc_reg(cstate); |
6077 | | |
6078 | 2.18k | memset(s, 0, sizeof(s)); |
6079 | 2.18k | fix3 = fix4 = fix5 = 0; |
6080 | | |
6081 | 2.18k | switch (proto) { |
6082 | 755 | case Q_IP: |
6083 | 1.49k | case Q_IPV6: |
6084 | 1.49k | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6085 | 1.49k | break; |
6086 | 688 | case Q_DEFAULT: |
6087 | 688 | b0 = gen_protochain(cstate, v, Q_IP); |
6088 | 688 | b = gen_protochain(cstate, v, Q_IPV6); |
6089 | 688 | gen_or(b0, b); |
6090 | 688 | return b; |
6091 | 1 | default: |
6092 | 1 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "protochain"); |
6093 | | /*NOTREACHED*/ |
6094 | 2.18k | } |
6095 | | |
6096 | | /* |
6097 | | * We don't handle variable-length prefixes before the link-layer |
6098 | | * header, or variable-length link-layer headers, here yet. |
6099 | | * We might want to add BPF instructions to do the protochain |
6100 | | * work, to simplify that and, on platforms that have a BPF |
6101 | | * interpreter with the new instructions, let the filtering |
6102 | | * be done in the kernel. (We already require a modified BPF |
6103 | | * engine to do the protochain stuff, to support backward |
6104 | | * branches, and backward branch support is unlikely to appear |
6105 | | * in kernel BPF engines.) |
6106 | | */ |
6107 | 1.46k | if (cstate->off_linkpl.is_variable) |
6108 | 1 | bpf_error(cstate, "'protochain' not supported with variable length headers"); |
6109 | | |
6110 | | /* |
6111 | | * To quote a comment in optimize.c: |
6112 | | * |
6113 | | * "These data structures are used in a Cocke and Schwartz style |
6114 | | * value numbering scheme. Since the flowgraph is acyclic, |
6115 | | * exit values can be propagated from a node's predecessors |
6116 | | * provided it is uniquely defined." |
6117 | | * |
6118 | | * "Acyclic" means "no backward branches", which means "no |
6119 | | * loops", so we have to turn the optimizer off. |
6120 | | */ |
6121 | 1.46k | cstate->no_optimize = 1; |
6122 | | |
6123 | | /* |
6124 | | * s[0] is a dummy entry to protect other BPF insn from damage |
6125 | | * by s[fix] = foo with uninitialized variable "fix". It is somewhat |
6126 | | * hard to find interdependency made by jump table fixup. |
6127 | | */ |
6128 | 1.46k | i = 0; |
6129 | 1.46k | s[i] = new_stmt(cstate, 0); /*dummy*/ |
6130 | 1.46k | i++; |
6131 | | |
6132 | 1.46k | switch (proto) { |
6133 | 726 | case Q_IP: |
6134 | 726 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6135 | | |
6136 | | /* A = ip->ip_p */ |
6137 | 726 | s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
6138 | 726 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9; |
6139 | 726 | i++; |
6140 | | /* X = ip->ip_hl << 2 */ |
6141 | 726 | s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); |
6142 | 726 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6143 | 726 | i++; |
6144 | 726 | break; |
6145 | | |
6146 | 738 | case Q_IPV6: |
6147 | 738 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6148 | | |
6149 | | /* A = ip6->ip_nxt */ |
6150 | 738 | s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); |
6151 | 738 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6; |
6152 | 738 | i++; |
6153 | | /* X = sizeof(struct ip6_hdr) */ |
6154 | 738 | s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM); |
6155 | 738 | s[i]->s.k = 40; |
6156 | 738 | i++; |
6157 | 738 | break; |
6158 | | |
6159 | 0 | default: |
6160 | 0 | bpf_error(cstate, "unsupported proto to gen_protochain"); |
6161 | | /*NOTREACHED*/ |
6162 | 1.46k | } |
6163 | | |
6164 | | /* again: if (A == v) goto end; else fall through; */ |
6165 | 1.46k | again = i; |
6166 | 1.46k | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6167 | 1.46k | s[i]->s.k = v; |
6168 | 1.46k | s[i]->s.jt = NULL; /*later*/ |
6169 | 1.46k | s[i]->s.jf = NULL; /*update in next stmt*/ |
6170 | 1.46k | fix5 = i; |
6171 | 1.46k | i++; |
6172 | | |
6173 | | #ifndef IPPROTO_NONE |
6174 | | #define IPPROTO_NONE 59 |
6175 | | #endif |
6176 | | /* if (A == IPPROTO_NONE) goto end */ |
6177 | 1.46k | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6178 | 1.46k | s[i]->s.jt = NULL; /*later*/ |
6179 | 1.46k | s[i]->s.jf = NULL; /*update in next stmt*/ |
6180 | 1.46k | s[i]->s.k = IPPROTO_NONE; |
6181 | 1.46k | s[fix5]->s.jf = s[i]; |
6182 | 1.46k | fix2 = i; |
6183 | 1.46k | i++; |
6184 | | |
6185 | 1.46k | if (proto == Q_IPV6) { |
6186 | 738 | int v6start, v6end, v6advance, j; |
6187 | | |
6188 | 738 | v6start = i; |
6189 | | /* if (A == IPPROTO_HOPOPTS) goto v6advance */ |
6190 | 738 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6191 | 738 | s[i]->s.jt = NULL; /*later*/ |
6192 | 738 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6193 | 738 | s[i]->s.k = IPPROTO_HOPOPTS; |
6194 | 738 | s[fix2]->s.jf = s[i]; |
6195 | 738 | i++; |
6196 | | /* if (A == IPPROTO_DSTOPTS) goto v6advance */ |
6197 | 738 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6198 | 738 | s[i]->s.jt = NULL; /*later*/ |
6199 | 738 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6200 | 738 | s[i]->s.k = IPPROTO_DSTOPTS; |
6201 | 738 | i++; |
6202 | | /* if (A == IPPROTO_ROUTING) goto v6advance */ |
6203 | 738 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6204 | 738 | s[i]->s.jt = NULL; /*later*/ |
6205 | 738 | s[i]->s.jf = NULL; /*update in next stmt*/ |
6206 | 738 | s[i]->s.k = IPPROTO_ROUTING; |
6207 | 738 | i++; |
6208 | | /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ |
6209 | 738 | s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6210 | 738 | s[i]->s.jt = NULL; /*later*/ |
6211 | 738 | s[i]->s.jf = NULL; /*later*/ |
6212 | 738 | s[i]->s.k = IPPROTO_FRAGMENT; |
6213 | 738 | fix3 = i; |
6214 | 738 | v6end = i; |
6215 | 738 | i++; |
6216 | | |
6217 | | /* v6advance: */ |
6218 | 738 | v6advance = i; |
6219 | | |
6220 | | /* |
6221 | | * in short, |
6222 | | * A = P[X + packet head]; |
6223 | | * X = X + (P[X + packet head + 1] + 1) * 8; |
6224 | | */ |
6225 | | /* A = P[X + packet head] */ |
6226 | 738 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6227 | 738 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6228 | 738 | i++; |
6229 | | /* MEM[reg2] = A */ |
6230 | 738 | s[i] = new_stmt(cstate, BPF_ST); |
6231 | 738 | s[i]->s.k = reg2; |
6232 | 738 | i++; |
6233 | | /* A = P[X + packet head + 1]; */ |
6234 | 738 | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6235 | 738 | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1; |
6236 | 738 | i++; |
6237 | | /* A += 1 */ |
6238 | 738 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6239 | 738 | s[i]->s.k = 1; |
6240 | 738 | i++; |
6241 | | /* A *= 8 */ |
6242 | 738 | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6243 | 738 | s[i]->s.k = 8; |
6244 | 738 | i++; |
6245 | | /* A += X */ |
6246 | 738 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
6247 | 738 | s[i]->s.k = 0; |
6248 | 738 | i++; |
6249 | | /* X = A; */ |
6250 | 738 | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6251 | 738 | i++; |
6252 | | /* A = MEM[reg2] */ |
6253 | 738 | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6254 | 738 | s[i]->s.k = reg2; |
6255 | 738 | i++; |
6256 | | |
6257 | | /* goto again; (must use BPF_JA for backward jump) */ |
6258 | 738 | s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); |
6259 | 738 | s[i]->s.k = again - i - 1; |
6260 | 738 | s[i - 1]->s.jf = s[i]; |
6261 | 738 | i++; |
6262 | | |
6263 | | /* fixup */ |
6264 | 3.69k | for (j = v6start; j <= v6end; j++) |
6265 | 2.95k | s[j]->s.jt = s[v6advance]; |
6266 | 738 | } else { |
6267 | | /* nop */ |
6268 | 724 | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6269 | 724 | s[i]->s.k = 0; |
6270 | 724 | s[fix2]->s.jf = s[i]; |
6271 | 724 | i++; |
6272 | 724 | } |
6273 | | |
6274 | | /* ahcheck: */ |
6275 | 1.46k | ahcheck = i; |
6276 | | /* if (A == IPPROTO_AH) then fall through; else goto end; */ |
6277 | 1.46k | s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); |
6278 | 1.46k | s[i]->s.jt = NULL; /*later*/ |
6279 | 1.46k | s[i]->s.jf = NULL; /*later*/ |
6280 | 1.46k | s[i]->s.k = IPPROTO_AH; |
6281 | 1.46k | if (fix3) |
6282 | 738 | s[fix3]->s.jf = s[ahcheck]; |
6283 | 1.46k | fix4 = i; |
6284 | 1.46k | i++; |
6285 | | |
6286 | | /* |
6287 | | * in short, |
6288 | | * A = P[X]; |
6289 | | * X = X + (P[X + 1] + 2) * 4; |
6290 | | */ |
6291 | | /* A = P[X + packet head]; */ |
6292 | 1.46k | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6293 | 1.46k | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6294 | 1.46k | s[i - 1]->s.jt = s[i]; |
6295 | 1.46k | i++; |
6296 | | /* MEM[reg2] = A */ |
6297 | 1.46k | s[i] = new_stmt(cstate, BPF_ST); |
6298 | 1.46k | s[i]->s.k = reg2; |
6299 | 1.46k | i++; |
6300 | | /* A = X */ |
6301 | 1.46k | s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); |
6302 | 1.46k | i++; |
6303 | | /* A += 1 */ |
6304 | 1.46k | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6305 | 1.46k | s[i]->s.k = 1; |
6306 | 1.46k | i++; |
6307 | | /* X = A */ |
6308 | 1.46k | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6309 | 1.46k | i++; |
6310 | | /* A = P[X + packet head] */ |
6311 | 1.46k | s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
6312 | 1.46k | s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
6313 | 1.46k | i++; |
6314 | | /* A += 2 */ |
6315 | 1.46k | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6316 | 1.46k | s[i]->s.k = 2; |
6317 | 1.46k | i++; |
6318 | | /* A *= 4 */ |
6319 | 1.46k | s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
6320 | 1.46k | s[i]->s.k = 4; |
6321 | 1.46k | i++; |
6322 | | /* X = A; */ |
6323 | 1.46k | s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); |
6324 | 1.46k | i++; |
6325 | | /* A = MEM[reg2] */ |
6326 | 1.46k | s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); |
6327 | 1.46k | s[i]->s.k = reg2; |
6328 | 1.46k | i++; |
6329 | | |
6330 | | /* goto again; (must use BPF_JA for backward jump) */ |
6331 | 1.46k | s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); |
6332 | 1.46k | s[i]->s.k = again - i - 1; |
6333 | 1.46k | i++; |
6334 | | |
6335 | | /* end: nop */ |
6336 | 1.46k | end = i; |
6337 | 1.46k | s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
6338 | 1.46k | s[i]->s.k = 0; |
6339 | 1.46k | s[fix2]->s.jt = s[end]; |
6340 | 1.46k | s[fix4]->s.jf = s[end]; |
6341 | 1.46k | s[fix5]->s.jt = s[end]; |
6342 | 1.46k | i++; |
6343 | | |
6344 | | /* |
6345 | | * make slist chain |
6346 | | */ |
6347 | 1.46k | max = i; |
6348 | 36.6k | for (i = 0; i < max - 1; i++) |
6349 | 35.1k | s[i]->next = s[i + 1]; |
6350 | 1.46k | s[max - 1]->next = NULL; |
6351 | | |
6352 | | /* |
6353 | | * emit final check |
6354 | | * Remember, s[0] is dummy. |
6355 | | */ |
6356 | 1.46k | b = gen_jmp(cstate, BPF_JEQ, v, s[1]); |
6357 | | |
6358 | 1.46k | free_reg(cstate, reg2); |
6359 | | |
6360 | 1.46k | gen_and(b0, b); |
6361 | 1.46k | return b; |
6362 | 1.46k | } |
6363 | | #endif /* !defined(NO_PROTOCHAIN) */ |
6364 | | |
6365 | | /* |
6366 | | * Generate code that checks whether the packet is a packet for protocol |
6367 | | * <proto> and whether the type field in that protocol's header has |
6368 | | * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an |
6369 | | * IP packet and checks the protocol number in the IP header against <v>. |
6370 | | * |
6371 | | * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks |
6372 | | * against Q_IP and Q_IPV6. |
6373 | | * |
6374 | | * This primitive is non-directional by design, so the grammar does not allow |
6375 | | * to qualify it with a direction. |
6376 | | */ |
6377 | | static struct block * |
6378 | | gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto) |
6379 | 33.5k | { |
6380 | 33.5k | struct block *b0, *b1; |
6381 | 33.5k | struct block *b2; |
6382 | | |
6383 | 33.5k | switch (proto) { |
6384 | 1.85k | case Q_DEFAULT: |
6385 | 1.85k | b0 = gen_proto(cstate, v, Q_IP); |
6386 | 1.85k | b1 = gen_proto(cstate, v, Q_IPV6); |
6387 | 1.85k | gen_or(b0, b1); |
6388 | 1.85k | return b1; |
6389 | | |
6390 | 1.55k | case Q_LINK: |
6391 | 1.55k | return gen_linktype(cstate, v); |
6392 | | |
6393 | 2.29k | case Q_IP: |
6394 | 2.29k | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6395 | | /* |
6396 | | * For FDDI, RFC 1188 says that SNAP encapsulation is used, |
6397 | | * not LLC encapsulation with LLCSAP_IP. |
6398 | | * |
6399 | | * For IEEE 802 networks - which includes 802.5 token ring |
6400 | | * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 |
6401 | | * says that SNAP encapsulation is used, not LLC encapsulation |
6402 | | * with LLCSAP_IP. |
6403 | | * |
6404 | | * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and |
6405 | | * RFC 2225 say that SNAP encapsulation is used, not LLC |
6406 | | * encapsulation with LLCSAP_IP. |
6407 | | * |
6408 | | * So we always check for ETHERTYPE_IP. |
6409 | | */ |
6410 | 2.29k | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
6411 | | // 0 <= v <= UINT8_MAX |
6412 | 2.29k | b1 = gen_ip_proto(cstate, (uint8_t)v); |
6413 | 2.29k | gen_and(b0, b1); |
6414 | 2.29k | return b1; |
6415 | | |
6416 | 0 | case Q_ARP: |
6417 | 1 | case Q_RARP: |
6418 | 2 | case Q_SCTP: |
6419 | 3 | case Q_TCP: |
6420 | 4 | case Q_UDP: |
6421 | 5 | case Q_ICMP: |
6422 | 6 | case Q_IGMP: |
6423 | 7 | case Q_IGRP: |
6424 | 8 | case Q_ATALK: |
6425 | 8 | case Q_DECNET: |
6426 | 9 | case Q_LAT: |
6427 | 10 | case Q_SCA: |
6428 | 11 | case Q_MOPRC: |
6429 | 12 | case Q_MOPDL: |
6430 | 12 | break; // invalid qualifier |
6431 | | |
6432 | 1.98k | case Q_IPV6: |
6433 | 1.98k | assert_maxval(cstate, "protocol number", v, UINT8_MAX); |
6434 | 1.98k | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
6435 | | /* |
6436 | | * Also check for a fragment header before the final |
6437 | | * header. |
6438 | | */ |
6439 | 1.98k | b2 = gen_ip6_proto(cstate, IPPROTO_FRAGMENT); |
6440 | 1.98k | b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v); |
6441 | 1.98k | gen_and(b2, b1); |
6442 | | // 0 <= v <= UINT8_MAX |
6443 | 1.98k | b2 = gen_ip6_proto(cstate, (uint8_t)v); |
6444 | 1.98k | gen_or(b2, b1); |
6445 | 1.98k | gen_and(b0, b1); |
6446 | 1.98k | return b1; |
6447 | | |
6448 | 1 | case Q_ICMPV6: |
6449 | 2 | case Q_AH: |
6450 | 3 | case Q_ESP: |
6451 | 4 | case Q_PIM: |
6452 | 5 | case Q_VRRP: |
6453 | 6 | case Q_AARP: |
6454 | 6 | break; // invalid qualifier |
6455 | | |
6456 | 13.0k | case Q_ISO: |
6457 | 13.0k | assert_maxval(cstate, "ISO protocol", v, UINT8_MAX); |
6458 | 13.0k | switch (cstate->linktype) { |
6459 | | |
6460 | 211 | case DLT_FRELAY: |
6461 | | /* |
6462 | | * Frame Relay packets typically have an OSI |
6463 | | * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)" |
6464 | | * generates code to check for all the OSI |
6465 | | * NLPIDs, so calling it and then adding a check |
6466 | | * for the particular NLPID for which we're |
6467 | | * looking is bogus, as we can just check for |
6468 | | * the NLPID. |
6469 | | * |
6470 | | * What we check for is the NLPID and a frame |
6471 | | * control field value of UI, i.e. 0x03 followed |
6472 | | * by the NLPID. |
6473 | | * |
6474 | | * XXX - assumes a 2-byte Frame Relay header with |
6475 | | * DLCI and flags. What if the address is longer? |
6476 | | * |
6477 | | * XXX - what about SNAP-encapsulated frames? |
6478 | | */ |
6479 | 211 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v); |
6480 | | /*NOTREACHED*/ |
6481 | | |
6482 | 238 | case DLT_C_HDLC: |
6483 | 431 | case DLT_HDLC: |
6484 | | /* |
6485 | | * Cisco uses an Ethertype lookalike - for OSI, |
6486 | | * it's 0xfefe. |
6487 | | */ |
6488 | 431 | b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS); |
6489 | | /* OSI in C-HDLC is stuffed with a fudge byte */ |
6490 | 431 | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v); |
6491 | 431 | gen_and(b0, b1); |
6492 | 431 | return b1; |
6493 | | |
6494 | 12.3k | default: |
6495 | 12.3k | b0 = gen_linktype(cstate, LLCSAP_ISONS); |
6496 | 12.3k | b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v); |
6497 | 12.3k | gen_and(b0, b1); |
6498 | 12.3k | return b1; |
6499 | 13.0k | } |
6500 | | |
6501 | 1 | case Q_ESIS: |
6502 | 1 | break; // invalid qualifier |
6503 | | |
6504 | 12.7k | case Q_ISIS: |
6505 | 12.7k | assert_maxval(cstate, "IS-IS PDU type", v, ISIS_PDU_TYPE_MAX); |
6506 | 12.7k | b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO); |
6507 | | /* |
6508 | | * 4 is the offset of the PDU type relative to the IS-IS |
6509 | | * header. |
6510 | | * Except when it is not, see above. |
6511 | | */ |
6512 | 12.7k | unsigned pdu_type_offset; |
6513 | 12.7k | switch (cstate->linktype) { |
6514 | 227 | case DLT_C_HDLC: |
6515 | 419 | case DLT_HDLC: |
6516 | 419 | pdu_type_offset = 5; |
6517 | 419 | break; |
6518 | 12.3k | default: |
6519 | 12.3k | pdu_type_offset = 4; |
6520 | 12.7k | } |
6521 | 12.7k | b1 = gen_mcmp(cstate, OR_LINKPL_NOSNAP, pdu_type_offset, BPF_B, |
6522 | 12.7k | v, ISIS_PDU_TYPE_MAX); |
6523 | 12.7k | gen_and(b0, b1); |
6524 | 12.7k | return b1; |
6525 | | |
6526 | 1 | case Q_CLNP: |
6527 | 1 | case Q_STP: |
6528 | 2 | case Q_IPX: |
6529 | 3 | case Q_NETBEUI: |
6530 | 4 | case Q_ISIS_L1: |
6531 | 5 | case Q_ISIS_L2: |
6532 | 7 | case Q_ISIS_IIH: |
6533 | 8 | case Q_ISIS_SNP: |
6534 | 9 | case Q_ISIS_CSNP: |
6535 | 10 | case Q_ISIS_PSNP: |
6536 | 11 | case Q_ISIS_LSP: |
6537 | 12 | case Q_RADIO: |
6538 | 13 | case Q_CARP: |
6539 | 13 | break; // invalid qualifier |
6540 | | |
6541 | 0 | default: |
6542 | 0 | abort(); |
6543 | | /*NOTREACHED*/ |
6544 | 33.5k | } |
6545 | 32 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "proto"); |
6546 | | /*NOTREACHED*/ |
6547 | 33.5k | } |
6548 | | |
6549 | | /* |
6550 | | * Convert a non-numeric name to a port number. |
6551 | | */ |
6552 | | static int |
6553 | | nametoport(compiler_state_t *cstate, const char *name, int ipproto) |
6554 | 0 | { |
6555 | 0 | struct addrinfo hints, *res, *ai; |
6556 | 0 | int error; |
6557 | 0 | struct sockaddr_in *in4; |
6558 | 0 | struct sockaddr_in6 *in6; |
6559 | 0 | int port = -1; |
6560 | | |
6561 | | /* |
6562 | | * We check for both TCP and UDP in case there are |
6563 | | * ambiguous entries. |
6564 | | */ |
6565 | 0 | memset(&hints, 0, sizeof(hints)); |
6566 | 0 | hints.ai_family = PF_UNSPEC; |
6567 | 0 | hints.ai_socktype = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM; |
6568 | 0 | hints.ai_protocol = ipproto; |
6569 | 0 | error = getaddrinfo(NULL, name, &hints, &res); |
6570 | 0 | if (error != 0) { |
6571 | 0 | switch (error) { |
6572 | | |
6573 | 0 | case EAI_NONAME: |
6574 | 0 | case EAI_SERVICE: |
6575 | | /* |
6576 | | * No such port. Just return -1. |
6577 | | */ |
6578 | 0 | break; |
6579 | | |
6580 | 0 | #ifdef EAI_SYSTEM |
6581 | 0 | case EAI_SYSTEM: |
6582 | | /* |
6583 | | * We don't use strerror() because it's not |
6584 | | * guaranteed to be thread-safe on all platforms |
6585 | | * (probably because it might use a non-thread-local |
6586 | | * buffer into which to format an error message |
6587 | | * if the error code isn't one for which it has |
6588 | | * a canned string; three cheers for C string |
6589 | | * handling). |
6590 | | */ |
6591 | 0 | bpf_set_error(cstate, "getaddrinfo(\"%s\" fails with system error: %d", |
6592 | 0 | name, errno); |
6593 | 0 | port = -2; /* a real error */ |
6594 | 0 | break; |
6595 | 0 | #endif |
6596 | | |
6597 | 0 | default: |
6598 | | /* |
6599 | | * This is a real error, not just "there's |
6600 | | * no such service name". |
6601 | | * |
6602 | | * We don't use gai_strerror() because it's not |
6603 | | * guaranteed to be thread-safe on all platforms |
6604 | | * (probably because it might use a non-thread-local |
6605 | | * buffer into which to format an error message |
6606 | | * if the error code isn't one for which it has |
6607 | | * a canned string; three cheers for C string |
6608 | | * handling). |
6609 | | */ |
6610 | 0 | bpf_set_error(cstate, "getaddrinfo(\"%s\") fails with error: %d", |
6611 | 0 | name, error); |
6612 | 0 | port = -2; /* a real error */ |
6613 | 0 | break; |
6614 | 0 | } |
6615 | 0 | } else { |
6616 | | /* |
6617 | | * OK, we found it. Did it find anything? |
6618 | | */ |
6619 | 0 | for (ai = res; ai != NULL; ai = ai->ai_next) { |
6620 | | /* |
6621 | | * Does it have an address? |
6622 | | */ |
6623 | 0 | if (ai->ai_addr != NULL) { |
6624 | | /* |
6625 | | * Yes. Get a port number; we're done. |
6626 | | */ |
6627 | 0 | if (ai->ai_addr->sa_family == AF_INET) { |
6628 | 0 | in4 = (struct sockaddr_in *)ai->ai_addr; |
6629 | 0 | port = ntohs(in4->sin_port); |
6630 | 0 | break; |
6631 | 0 | } |
6632 | 0 | if (ai->ai_addr->sa_family == AF_INET6) { |
6633 | 0 | in6 = (struct sockaddr_in6 *)ai->ai_addr; |
6634 | 0 | port = ntohs(in6->sin6_port); |
6635 | 0 | break; |
6636 | 0 | } |
6637 | 0 | } |
6638 | 0 | } |
6639 | 0 | freeaddrinfo(res); |
6640 | 0 | } |
6641 | 0 | return port; |
6642 | 0 | } |
6643 | | |
6644 | | /* |
6645 | | * Convert a string to a port number. |
6646 | | */ |
6647 | | static bpf_u_int32 |
6648 | | stringtoport(compiler_state_t *cstate, const char *string, size_t string_size, |
6649 | | int *proto) |
6650 | 0 | { |
6651 | 0 | stoulen_ret ret; |
6652 | 0 | char *cpy; |
6653 | 0 | bpf_u_int32 val; |
6654 | 0 | int tcp_port = -1; |
6655 | 0 | int udp_port = -1; |
6656 | | |
6657 | | /* |
6658 | | * See if it's a number. |
6659 | | */ |
6660 | 0 | ret = stoulen(string, string_size, &val, cstate); |
6661 | 0 | switch (ret) { |
6662 | | |
6663 | 0 | case STOULEN_OK: |
6664 | | /* Unknown port type - it's just a number. */ |
6665 | 0 | *proto = PROTO_UNDEF; |
6666 | 0 | break; |
6667 | | |
6668 | 0 | case STOULEN_NOT_OCTAL_NUMBER: |
6669 | 0 | case STOULEN_NOT_HEX_NUMBER: |
6670 | 0 | case STOULEN_NOT_DECIMAL_NUMBER: |
6671 | | /* |
6672 | | * Not a valid number; try looking it up as a port. |
6673 | | */ |
6674 | 0 | cpy = malloc(string_size + 1); /* +1 for terminating '\0' */ |
6675 | 0 | memcpy(cpy, string, string_size); |
6676 | 0 | cpy[string_size] = '\0'; |
6677 | 0 | tcp_port = nametoport(cstate, cpy, IPPROTO_TCP); |
6678 | 0 | if (tcp_port == -2) { |
6679 | | /* |
6680 | | * We got a hard error; the error string has |
6681 | | * already been set. |
6682 | | */ |
6683 | 0 | free(cpy); |
6684 | 0 | longjmp(cstate->top_ctx, 1); |
6685 | | /*NOTREACHED*/ |
6686 | 0 | } |
6687 | 0 | udp_port = nametoport(cstate, cpy, IPPROTO_UDP); |
6688 | 0 | if (udp_port == -2) { |
6689 | | /* |
6690 | | * We got a hard error; the error string has |
6691 | | * already been set. |
6692 | | */ |
6693 | 0 | free(cpy); |
6694 | 0 | longjmp(cstate->top_ctx, 1); |
6695 | | /*NOTREACHED*/ |
6696 | 0 | } |
6697 | | |
6698 | | /* |
6699 | | * We need to check /etc/services for ambiguous entries. |
6700 | | * If we find an ambiguous entry, and it has the |
6701 | | * same port number, change the proto to PROTO_UNDEF |
6702 | | * so both TCP and UDP will be checked. |
6703 | | */ |
6704 | 0 | if (tcp_port >= 0) { |
6705 | 0 | val = (bpf_u_int32)tcp_port; |
6706 | 0 | *proto = IPPROTO_TCP; |
6707 | 0 | if (udp_port >= 0) { |
6708 | 0 | if (udp_port == tcp_port) |
6709 | 0 | *proto = PROTO_UNDEF; |
6710 | | #ifdef notdef |
6711 | | else |
6712 | | /* Can't handle ambiguous names that refer |
6713 | | to different port numbers. */ |
6714 | | warning("ambiguous port %s in /etc/services", |
6715 | | cpy); |
6716 | | #endif |
6717 | 0 | } |
6718 | 0 | free(cpy); |
6719 | 0 | break; |
6720 | 0 | } |
6721 | 0 | if (udp_port >= 0) { |
6722 | 0 | val = (bpf_u_int32)udp_port; |
6723 | 0 | *proto = IPPROTO_UDP; |
6724 | 0 | free(cpy); |
6725 | 0 | break; |
6726 | 0 | } |
6727 | 0 | bpf_set_error(cstate, "'%s' is not a valid port", cpy); |
6728 | 0 | free(cpy); |
6729 | 0 | longjmp(cstate->top_ctx, 1); |
6730 | | /*NOTREACHED*/ |
6731 | | #ifdef _AIX |
6732 | | PCAP_UNREACHABLE |
6733 | | #endif /* _AIX */ |
6734 | | |
6735 | 0 | case STOULEN_ERROR: |
6736 | | /* Error already set. */ |
6737 | 0 | longjmp(cstate->top_ctx, 1); |
6738 | | /*NOTREACHED*/ |
6739 | | #ifdef _AIX |
6740 | | PCAP_UNREACHABLE |
6741 | | #endif /* _AIX */ |
6742 | | |
6743 | 0 | default: |
6744 | | /* Should not happen */ |
6745 | 0 | bpf_set_error(cstate, "stoulen returned %d - this should not happen", ret); |
6746 | 0 | longjmp(cstate->top_ctx, 1); |
6747 | | /*NOTREACHED*/ |
6748 | 0 | } |
6749 | 0 | return (val); |
6750 | 0 | } |
6751 | | |
6752 | | /* |
6753 | | * Convert a string in the form PPP-PPP, which correspond to ports, to |
6754 | | * a starting and ending port in a port range. |
6755 | | */ |
6756 | | static void |
6757 | | stringtoportrange(compiler_state_t *cstate, const char *string, |
6758 | | bpf_u_int32 *port1, bpf_u_int32 *port2, int *proto) |
6759 | 0 | { |
6760 | 0 | char *hyphen_off; |
6761 | 0 | const char *first, *second; |
6762 | 0 | size_t first_size, second_size; |
6763 | 0 | int save_proto; |
6764 | |
|
6765 | 0 | if ((hyphen_off = strchr(string, '-')) == NULL) |
6766 | 0 | bpf_error(cstate, "port range '%s' contains no hyphen", string); |
6767 | | |
6768 | | /* |
6769 | | * Make sure there are no other hyphens. |
6770 | | * |
6771 | | * XXX - we support named ports, but there are some port names |
6772 | | * in /etc/services that include hyphens, so this would rule |
6773 | | * that out. |
6774 | | */ |
6775 | 0 | if (strchr(hyphen_off + 1, '-') != NULL) |
6776 | 0 | bpf_error(cstate, "port range '%s' contains more than one hyphen", |
6777 | 0 | string); |
6778 | | |
6779 | | /* |
6780 | | * Get the length of the first port. |
6781 | | */ |
6782 | 0 | first = string; |
6783 | 0 | first_size = hyphen_off - string; |
6784 | 0 | if (first_size == 0) { |
6785 | | /* Range of "-port", which we don't support. */ |
6786 | 0 | bpf_error(cstate, "port range '%s' has no starting port", string); |
6787 | 0 | } |
6788 | | |
6789 | | /* |
6790 | | * Try to convert it to a port. |
6791 | | */ |
6792 | 0 | *port1 = stringtoport(cstate, first, first_size, proto); |
6793 | 0 | save_proto = *proto; |
6794 | | |
6795 | | /* |
6796 | | * Get the length of the second port. |
6797 | | */ |
6798 | 0 | second = hyphen_off + 1; |
6799 | 0 | second_size = strlen(second); |
6800 | 0 | if (second_size == 0) { |
6801 | | /* Range of "port-", which we don't support. */ |
6802 | 0 | bpf_error(cstate, "port range '%s' has no ending port", string); |
6803 | 0 | } |
6804 | | |
6805 | | /* |
6806 | | * Try to convert it to a port. |
6807 | | */ |
6808 | 0 | *port2 = stringtoport(cstate, second, second_size, proto); |
6809 | 0 | if (*proto != save_proto) |
6810 | 0 | *proto = PROTO_UNDEF; |
6811 | 0 | } |
6812 | | |
6813 | | struct block * |
6814 | | gen_scode(compiler_state_t *cstate, const char *name, struct qual q) |
6815 | 1.32k | { |
6816 | 1.32k | int proto = q.proto; |
6817 | 1.32k | int dir = q.dir; |
6818 | 1.32k | int tproto; |
6819 | 1.32k | bpf_u_int32 mask, addr; |
6820 | 1.32k | struct addrinfo *res, *res0; |
6821 | 1.32k | struct sockaddr_in *sin4; |
6822 | 1.32k | int tproto6; |
6823 | 1.32k | struct sockaddr_in6 *sin6; |
6824 | 1.32k | struct in6_addr mask128; |
6825 | 1.32k | struct block *b, *tmp; |
6826 | 1.32k | int port, real_proto; |
6827 | 1.32k | bpf_u_int32 port1, port2; |
6828 | | |
6829 | | /* |
6830 | | * Catch errors reported by us and routines below us, and return NULL |
6831 | | * on an error. |
6832 | | */ |
6833 | 1.32k | if (setjmp(cstate->top_ctx)) |
6834 | 797 | return (NULL); |
6835 | | |
6836 | 529 | switch (q.addr) { |
6837 | | |
6838 | 3 | case Q_NET: |
6839 | 3 | addr = pcap_nametonetaddr(name); |
6840 | 3 | if (addr == 0) |
6841 | 3 | bpf_error(cstate, "unknown network '%s'", name); |
6842 | | /* Left justify network addr and calculate its network mask */ |
6843 | 0 | mask = 0xffffffff; |
6844 | 0 | while (addr && (addr & 0xff000000) == 0) { |
6845 | 0 | addr <<= 8; |
6846 | 0 | mask <<= 8; |
6847 | 0 | } |
6848 | 0 | return gen_host(cstate, addr, mask, proto, dir, q.addr); |
6849 | | |
6850 | 190 | case Q_DEFAULT: |
6851 | 262 | case Q_HOST: |
6852 | 262 | if (proto == Q_LINK) { |
6853 | 35 | return gen_mac48host_byname(cstate, name, q.dir, "link host NAME"); |
6854 | 227 | } else if (proto == Q_DECNET) { |
6855 | | /* |
6856 | | * A long time ago on Ultrix libpcap supported |
6857 | | * translation of DECnet host names into DECnet |
6858 | | * addresses, but this feature is history now. |
6859 | | */ |
6860 | 1 | bpf_error(cstate, "invalid DECnet address '%s'", name); |
6861 | 226 | } else { |
6862 | 226 | memset(&mask128, 0xff, sizeof(mask128)); |
6863 | 226 | res0 = res = pcap_nametoaddrinfo(name); |
6864 | 226 | if (res == NULL) |
6865 | 19 | bpf_error(cstate, "unknown host '%s'", name); |
6866 | 207 | cstate->ai = res; |
6867 | 207 | b = tmp = NULL; |
6868 | 207 | tproto = proto; |
6869 | 207 | tproto6 = proto; |
6870 | 207 | if (cstate->off_linktype.constant_part == OFFSET_NOT_SET && |
6871 | 207 | tproto == Q_DEFAULT) { |
6872 | 70 | tproto = Q_IP; |
6873 | 70 | tproto6 = Q_IPV6; |
6874 | 70 | } |
6875 | 403 | for (res = res0; res; res = res->ai_next) { |
6876 | 207 | switch (res->ai_family) { |
6877 | 185 | case AF_INET: |
6878 | 185 | if (tproto == Q_IPV6) |
6879 | 1 | continue; |
6880 | | |
6881 | 184 | sin4 = (struct sockaddr_in *) |
6882 | 184 | res->ai_addr; |
6883 | 184 | tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr), |
6884 | 184 | 0xffffffff, tproto, dir, q.addr); |
6885 | 184 | break; |
6886 | 22 | case AF_INET6: |
6887 | 22 | if (tproto6 == Q_IP) |
6888 | 1 | continue; |
6889 | | |
6890 | 21 | sin6 = (struct sockaddr_in6 *) |
6891 | 21 | res->ai_addr; |
6892 | 21 | tmp = gen_host6(cstate, &sin6->sin6_addr, |
6893 | 21 | &mask128, tproto6, dir, q.addr); |
6894 | 21 | break; |
6895 | 0 | default: |
6896 | 0 | continue; |
6897 | 207 | } |
6898 | 194 | if (b) |
6899 | 0 | gen_or(b, tmp); |
6900 | 194 | b = tmp; |
6901 | 194 | } |
6902 | 196 | cstate->ai = NULL; |
6903 | 196 | freeaddrinfo(res0); |
6904 | 196 | if (b == NULL) { |
6905 | 2 | bpf_error(cstate, "unknown host '%s'%s", name, |
6906 | 2 | (proto == Q_DEFAULT) |
6907 | 2 | ? "" |
6908 | 2 | : " for specified address family"); |
6909 | 2 | } |
6910 | 194 | return b; |
6911 | 196 | } |
6912 | | |
6913 | 355 | case Q_PORT: |
6914 | 355 | (void)port_pq_to_ipproto(cstate, proto, "port"); // validate only |
6915 | 355 | if (pcap_nametoport(name, &port, &real_proto) == 0) |
6916 | 171 | bpf_error(cstate, "unknown port '%s'", name); |
6917 | 184 | if (proto == Q_UDP) { |
6918 | 27 | if (real_proto == IPPROTO_TCP) |
6919 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6920 | 27 | else if (real_proto == IPPROTO_SCTP) |
6921 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6922 | 27 | else |
6923 | | /* override PROTO_UNDEF */ |
6924 | 27 | real_proto = IPPROTO_UDP; |
6925 | 27 | } |
6926 | 184 | if (proto == Q_TCP) { |
6927 | 58 | if (real_proto == IPPROTO_UDP) |
6928 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6929 | | |
6930 | 58 | else if (real_proto == IPPROTO_SCTP) |
6931 | 0 | bpf_error(cstate, "port '%s' is sctp", name); |
6932 | 58 | else |
6933 | | /* override PROTO_UNDEF */ |
6934 | 58 | real_proto = IPPROTO_TCP; |
6935 | 58 | } |
6936 | 184 | if (proto == Q_SCTP) { |
6937 | 4 | if (real_proto == IPPROTO_UDP) |
6938 | 0 | bpf_error(cstate, "port '%s' is udp", name); |
6939 | | |
6940 | 4 | else if (real_proto == IPPROTO_TCP) |
6941 | 0 | bpf_error(cstate, "port '%s' is tcp", name); |
6942 | 4 | else |
6943 | | /* override PROTO_UNDEF */ |
6944 | 4 | real_proto = IPPROTO_SCTP; |
6945 | 4 | } |
6946 | 184 | if (port < 0) |
6947 | 0 | bpf_error(cstate, "illegal port number %d < 0", port); |
6948 | 184 | if (port > 65535) |
6949 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port); |
6950 | | // real_proto can be PROTO_UNDEF |
6951 | 184 | b = gen_port(cstate, (uint16_t)port, real_proto, dir); |
6952 | 184 | gen_or(gen_port6(cstate, (uint16_t)port, real_proto, dir), b); |
6953 | 184 | return b; |
6954 | | |
6955 | 0 | case Q_PORTRANGE: |
6956 | 0 | (void)port_pq_to_ipproto(cstate, proto, "portrange"); // validate only |
6957 | 0 | stringtoportrange(cstate, name, &port1, &port2, &real_proto); |
6958 | 0 | if (proto == Q_UDP) { |
6959 | 0 | if (real_proto == IPPROTO_TCP) |
6960 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6961 | 0 | else if (real_proto == IPPROTO_SCTP) |
6962 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6963 | 0 | else |
6964 | | /* override PROTO_UNDEF */ |
6965 | 0 | real_proto = IPPROTO_UDP; |
6966 | 0 | } |
6967 | 0 | if (proto == Q_TCP) { |
6968 | 0 | if (real_proto == IPPROTO_UDP) |
6969 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6970 | 0 | else if (real_proto == IPPROTO_SCTP) |
6971 | 0 | bpf_error(cstate, "port in range '%s' is sctp", name); |
6972 | 0 | else |
6973 | | /* override PROTO_UNDEF */ |
6974 | 0 | real_proto = IPPROTO_TCP; |
6975 | 0 | } |
6976 | 0 | if (proto == Q_SCTP) { |
6977 | 0 | if (real_proto == IPPROTO_UDP) |
6978 | 0 | bpf_error(cstate, "port in range '%s' is udp", name); |
6979 | 0 | else if (real_proto == IPPROTO_TCP) |
6980 | 0 | bpf_error(cstate, "port in range '%s' is tcp", name); |
6981 | 0 | else |
6982 | | /* override PROTO_UNDEF */ |
6983 | 0 | real_proto = IPPROTO_SCTP; |
6984 | 0 | } |
6985 | 0 | if (port1 > 65535) |
6986 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port1); |
6987 | 0 | if (port2 > 65535) |
6988 | 0 | bpf_error(cstate, "illegal port number %d > 65535", port2); |
6989 | | |
6990 | | // real_proto can be PROTO_UNDEF |
6991 | 0 | b = gen_portrange(cstate, (uint16_t)port1, (uint16_t)port2, |
6992 | 0 | real_proto, dir); |
6993 | 0 | gen_or(gen_portrange6(cstate, (uint16_t)port1, (uint16_t)port2, |
6994 | 0 | real_proto, dir), b); |
6995 | 0 | return b; |
6996 | | |
6997 | 0 | case Q_GATEWAY: |
6998 | 0 | res = pcap_nametoaddrinfo(name); |
6999 | 0 | cstate->ai = res; |
7000 | 0 | if (res == NULL) |
7001 | 0 | bpf_error(cstate, "unknown host '%s'", name); |
7002 | 0 | b = gen_gateway(cstate, name, res, proto); |
7003 | 0 | cstate->ai = NULL; |
7004 | 0 | freeaddrinfo(res); |
7005 | 0 | if (b == NULL) |
7006 | 0 | bpf_error(cstate, "unknown host '%s'", name); |
7007 | 0 | return b; |
7008 | | |
7009 | 463 | case Q_PROTO: |
7010 | 463 | return gen_proto(cstate, lookup_proto(cstate, name, q), proto); |
7011 | | |
7012 | 0 | #if !defined(NO_PROTOCHAIN) |
7013 | 116 | case Q_PROTOCHAIN: |
7014 | 116 | return gen_protochain(cstate, lookup_proto(cstate, name, q), proto); |
7015 | 0 | #endif /* !defined(NO_PROTOCHAIN) */ |
7016 | | |
7017 | 127 | case Q_UNDEF: |
7018 | 127 | syntax(cstate); |
7019 | | /*NOTREACHED*/ |
7020 | 529 | } |
7021 | 0 | abort(); |
7022 | | /*NOTREACHED*/ |
7023 | 529 | } |
7024 | | |
7025 | | struct block * |
7026 | | gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2, |
7027 | | bpf_u_int32 masklen, struct qual q) |
7028 | 323 | { |
7029 | 323 | register int nlen, mlen; |
7030 | 323 | bpf_u_int32 n, m; |
7031 | 323 | uint64_t m64; |
7032 | | |
7033 | | /* |
7034 | | * Catch errors reported by us and routines below us, and return NULL |
7035 | | * on an error. |
7036 | | */ |
7037 | 323 | if (setjmp(cstate->top_ctx)) |
7038 | 73 | return (NULL); |
7039 | | |
7040 | 250 | nlen = pcapint_atoin(s1, &n); |
7041 | 250 | if (nlen < 0) |
7042 | 1 | bpf_error(cstate, "invalid IPv4 address '%s'", s1); |
7043 | | /* Promote short ipaddr */ |
7044 | 249 | n <<= 32 - nlen; |
7045 | | |
7046 | 249 | if (s2 != NULL) { |
7047 | 0 | mlen = pcapint_atoin(s2, &m); |
7048 | 0 | if (mlen < 0) |
7049 | 0 | bpf_error(cstate, "invalid IPv4 address '%s'", s2); |
7050 | | /* Promote short ipaddr */ |
7051 | 0 | m <<= 32 - mlen; |
7052 | 0 | if ((n & ~m) != 0) |
7053 | 0 | bpf_error(cstate, "non-network bits set in \"%s mask %s\"", |
7054 | 0 | s1, s2); |
7055 | 249 | } else { |
7056 | | /* Convert mask len to mask */ |
7057 | 249 | if (masklen > 32) |
7058 | 29 | bpf_error(cstate, "mask length must be <= 32"); |
7059 | 220 | m64 = UINT64_C(0xffffffff) << (32 - masklen); |
7060 | 220 | m = (bpf_u_int32)m64; |
7061 | 220 | if ((n & ~m) != 0) |
7062 | 34 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", |
7063 | 34 | s1, masklen); |
7064 | 220 | } |
7065 | | |
7066 | 186 | switch (q.addr) { |
7067 | | |
7068 | 252 | case Q_NET: |
7069 | 252 | return gen_host(cstate, n, m, q.proto, q.dir, q.addr); |
7070 | | |
7071 | 7 | default: |
7072 | | // Q_HOST and Q_GATEWAY only (see the grammar) |
7073 | 7 | bpf_error(cstate, "Mask syntax for networks only"); |
7074 | | /*NOTREACHED*/ |
7075 | 186 | } |
7076 | | /*NOTREACHED*/ |
7077 | 186 | } |
7078 | | |
7079 | | struct block * |
7080 | | gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q) |
7081 | 16.8k | { |
7082 | 16.8k | bpf_u_int32 mask; |
7083 | 16.8k | int proto; |
7084 | 16.8k | int dir; |
7085 | 16.8k | register int vlen; |
7086 | | |
7087 | | /* |
7088 | | * Catch errors reported by us and routines below us, and return NULL |
7089 | | * on an error. |
7090 | | */ |
7091 | 16.8k | if (setjmp(cstate->top_ctx)) |
7092 | 629 | return (NULL); |
7093 | | |
7094 | 16.2k | proto = q.proto; |
7095 | 16.2k | dir = q.dir; |
7096 | 16.5k | if (s == NULL) { |
7097 | | /* |
7098 | | * v contains a 32-bit unsigned parsed from a string of the |
7099 | | * form {N}, which could be decimal, hexadecimal or octal. |
7100 | | * Although it would be possible to use the value as a raw |
7101 | | * 16-bit DECnet address when the value fits into 16 bits, this |
7102 | | * would be a questionable feature: DECnet address wire |
7103 | | * encoding is little-endian, so this would not work as |
7104 | | * intuitively as the same works for [big-endian] IPv4 |
7105 | | * addresses (0x01020304 means 1.2.3.4). |
7106 | | */ |
7107 | 16.5k | if (proto == Q_DECNET) |
7108 | 4 | bpf_error(cstate, "invalid DECnet address '%u'", v); |
7109 | 16.5k | vlen = 32; |
7110 | 18.4E | } else if (proto == Q_DECNET) { |
7111 | | /* |
7112 | | * s points to a string of the form {N}.{N}, {N}.{N}.{N} or |
7113 | | * {N}.{N}.{N}.{N}, of which only the first potentially stands |
7114 | | * for a valid DECnet address. |
7115 | | */ |
7116 | 178 | vlen = pcapint_atodn(s, &v); |
7117 | 178 | if (vlen == 0) |
7118 | 11 | bpf_error(cstate, "invalid DECnet address '%s'", s); |
7119 | 18.4E | } else { |
7120 | | /* |
7121 | | * s points to a string of the form {N}.{N}, {N}.{N}.{N} or |
7122 | | * {N}.{N}.{N}.{N}, all of which potentially stand for a valid |
7123 | | * IPv4 address. |
7124 | | */ |
7125 | 18.4E | vlen = pcapint_atoin(s, &v); |
7126 | 18.4E | if (vlen < 0) |
7127 | 3 | bpf_error(cstate, "invalid IPv4 address '%s'", s); |
7128 | 18.4E | } |
7129 | | |
7130 | 16.2k | switch (q.addr) { |
7131 | | |
7132 | 1.08k | case Q_DEFAULT: |
7133 | 1.59k | case Q_HOST: |
7134 | 8.77k | case Q_NET: |
7135 | 8.77k | if (proto == Q_DECNET) |
7136 | 167 | return gen_host(cstate, v, 0, proto, dir, q.addr); |
7137 | 8.60k | else if (proto == Q_LINK) { |
7138 | | // "link (host|net) IPV4ADDR" and variations thereof |
7139 | 5 | bpf_error(cstate, "illegal link layer address"); |
7140 | 8.59k | } else { |
7141 | 8.59k | mask = 0xffffffff; |
7142 | 8.59k | if (s == NULL && q.addr == Q_NET) { |
7143 | | /* Promote short net number */ |
7144 | 21.7k | while (v && (v & 0xff000000) == 0) { |
7145 | 14.7k | v <<= 8; |
7146 | 14.7k | mask <<= 8; |
7147 | 14.7k | } |
7148 | 7.04k | } else { |
7149 | | /* Promote short ipaddr */ |
7150 | 1.55k | v <<= 32 - vlen; |
7151 | 1.55k | mask <<= 32 - vlen ; |
7152 | 1.55k | } |
7153 | 8.59k | return gen_host(cstate, v, mask, proto, dir, q.addr); |
7154 | 8.59k | } |
7155 | | |
7156 | 4.80k | case Q_PORT: |
7157 | 4.80k | proto = port_pq_to_ipproto(cstate, proto, "port"); |
7158 | | |
7159 | 4.80k | if (v > 65535) |
7160 | 17 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7161 | | |
7162 | | // proto can be PROTO_UNDEF |
7163 | 4.78k | { |
7164 | 4.78k | struct block *b; |
7165 | 4.78k | b = gen_port(cstate, (uint16_t)v, proto, dir); |
7166 | 4.78k | gen_or(gen_port6(cstate, (uint16_t)v, proto, dir), b); |
7167 | 4.78k | return b; |
7168 | 4.80k | } |
7169 | | |
7170 | 0 | case Q_PORTRANGE: |
7171 | 0 | proto = port_pq_to_ipproto(cstate, proto, "portrange"); |
7172 | |
|
7173 | 0 | if (v > 65535) |
7174 | 0 | bpf_error(cstate, "illegal port number %u > 65535", v); |
7175 | | |
7176 | | // proto can be PROTO_UNDEF |
7177 | 0 | { |
7178 | 0 | struct block *b; |
7179 | 0 | b = gen_portrange(cstate, (uint16_t)v, (uint16_t)v, |
7180 | 0 | proto, dir); |
7181 | 0 | gen_or(gen_portrange6(cstate, (uint16_t)v, (uint16_t)v, |
7182 | 0 | proto, dir), b); |
7183 | 0 | return b; |
7184 | 0 | } |
7185 | | |
7186 | 0 | case Q_GATEWAY: |
7187 | 0 | bpf_error(cstate, "'gateway' requires a name"); |
7188 | | /*NOTREACHED*/ |
7189 | | |
7190 | 2.39k | case Q_PROTO: |
7191 | 2.39k | return gen_proto(cstate, v, proto); |
7192 | | |
7193 | 0 | #if !defined(NO_PROTOCHAIN) |
7194 | 847 | case Q_PROTOCHAIN: |
7195 | 847 | return gen_protochain(cstate, v, proto); |
7196 | 0 | #endif |
7197 | | |
7198 | 16 | case Q_UNDEF: |
7199 | 16 | syntax(cstate); |
7200 | | /*NOTREACHED*/ |
7201 | | |
7202 | 0 | default: |
7203 | 0 | abort(); |
7204 | | /*NOTREACHED*/ |
7205 | 16.2k | } |
7206 | | /*NOTREACHED*/ |
7207 | 16.2k | } |
7208 | | |
7209 | | struct block * |
7210 | | gen_mcode6(compiler_state_t *cstate, const char *s, bpf_u_int32 masklen, |
7211 | | struct qual q) |
7212 | 689 | { |
7213 | 689 | struct in6_addr addr; |
7214 | 689 | struct in6_addr mask; |
7215 | 689 | bpf_u_int32 a[4], m[4]; /* Same as in gen_hostop6(). */ |
7216 | | |
7217 | | /* |
7218 | | * Catch errors reported by us and routines below us, and return NULL |
7219 | | * on an error. |
7220 | | */ |
7221 | 689 | if (setjmp(cstate->top_ctx)) |
7222 | 118 | return (NULL); |
7223 | | |
7224 | | /* |
7225 | | * If everything works correctly, this call never fails: a string that |
7226 | | * is valid for HID6 in the lexer is valid for inet_pton(). |
7227 | | */ |
7228 | 571 | if (1 != inet_pton(AF_INET6, s, &addr)) |
7229 | 0 | bpf_error(cstate, "'%s' is not a valid IPv6 address", s); |
7230 | | |
7231 | 571 | if (masklen > sizeof(mask.s6_addr) * 8) |
7232 | 17 | bpf_error(cstate, "mask length must be <= %zu", sizeof(mask.s6_addr) * 8); |
7233 | 554 | memset(&mask, 0, sizeof(mask)); |
7234 | 554 | memset(&mask.s6_addr, 0xff, masklen / 8); |
7235 | 554 | if (masklen % 8) { |
7236 | 70 | mask.s6_addr[masklen / 8] = |
7237 | 70 | (0xff << (8 - masklen % 8)) & 0xff; |
7238 | 70 | } |
7239 | | |
7240 | 554 | memcpy(a, &addr, sizeof(a)); |
7241 | 554 | memcpy(m, &mask, sizeof(m)); |
7242 | 666 | if ((a[0] & ~m[0]) || (a[1] & ~m[1]) |
7243 | 635 | || (a[2] & ~m[2]) || (a[3] & ~m[3])) { |
7244 | 54 | bpf_error(cstate, "non-network bits set in \"%s/%d\"", s, masklen); |
7245 | 54 | } |
7246 | | |
7247 | 500 | switch (q.addr) { |
7248 | | |
7249 | 260 | case Q_DEFAULT: |
7250 | 379 | case Q_HOST: |
7251 | 379 | if (masklen != 128) |
7252 | 5 | bpf_error(cstate, "Mask syntax for networks only"); |
7253 | | /* FALLTHROUGH */ |
7254 | | |
7255 | 610 | case Q_NET: |
7256 | 610 | return gen_host6(cstate, &addr, &mask, q.proto, q.dir, q.addr); |
7257 | | |
7258 | 3 | default: |
7259 | | // Q_GATEWAY only (see the grammar) |
7260 | 3 | bpf_error(cstate, "invalid qualifier against IPv6 address"); |
7261 | | /*NOTREACHED*/ |
7262 | 500 | } |
7263 | 500 | } |
7264 | | |
7265 | | struct block * |
7266 | | gen_ecode(compiler_state_t *cstate, const char *s, struct qual q) |
7267 | 711 | { |
7268 | | /* |
7269 | | * Catch errors reported by us and routines below us, and return NULL |
7270 | | * on an error. |
7271 | | */ |
7272 | 711 | if (setjmp(cstate->top_ctx)) |
7273 | 13 | return (NULL); |
7274 | | |
7275 | 698 | const char *context = "link host XX:XX:XX:XX:XX:XX"; |
7276 | | |
7277 | 710 | if (! ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK)) |
7278 | 2 | bpf_error(cstate, "ethernet address used in non-ether expression"); |
7279 | 696 | if (! is_mac48_linktype(cstate->linktype)) |
7280 | 2 | fail_kw_on_dlt(cstate, context); |
7281 | | |
7282 | 694 | u_char *eaddrp = pcap_ether_aton(s); |
7283 | 694 | if (eaddrp == NULL) |
7284 | 0 | bpf_error(cstate, "malloc"); |
7285 | 694 | u_char eaddr[6]; |
7286 | 694 | memcpy(eaddr, eaddrp, sizeof(eaddr)); |
7287 | 694 | free(eaddrp); |
7288 | | |
7289 | 694 | return gen_mac48host(cstate, eaddr, q.dir, context); |
7290 | 694 | } |
7291 | | |
7292 | | void |
7293 | | sappend(struct slist *s0, struct slist *s1) |
7294 | 376k | { |
7295 | | /* |
7296 | | * This is definitely not the best way to do this, but the |
7297 | | * lists will rarely get long. |
7298 | | */ |
7299 | 5.91M | while (s0->next) |
7300 | 5.54M | s0 = s0->next; |
7301 | 376k | s0->next = s1; |
7302 | 376k | } |
7303 | | |
7304 | | static struct slist * |
7305 | | xfer_to_x(compiler_state_t *cstate, struct arth *a) |
7306 | 21.5k | { |
7307 | 21.5k | struct slist *s; |
7308 | | |
7309 | 21.5k | s = new_stmt(cstate, BPF_LDX|BPF_MEM); |
7310 | 21.5k | s->s.k = a->regno; |
7311 | 21.5k | return s; |
7312 | 21.5k | } |
7313 | | |
7314 | | static struct slist * |
7315 | | xfer_to_a(compiler_state_t *cstate, struct arth *a) |
7316 | 36.0k | { |
7317 | 36.0k | struct slist *s; |
7318 | | |
7319 | 36.0k | s = new_stmt(cstate, BPF_LD|BPF_MEM); |
7320 | 36.0k | s->s.k = a->regno; |
7321 | 36.0k | return s; |
7322 | 36.0k | } |
7323 | | |
7324 | | /* |
7325 | | * Modify "index" to use the value stored into its register as an |
7326 | | * offset relative to the beginning of the header for the protocol |
7327 | | * "proto", and allocate a register and put an item "size" bytes long |
7328 | | * (1, 2, or 4) at that offset into that register, making it the register |
7329 | | * for "index". |
7330 | | */ |
7331 | | static struct arth * |
7332 | | gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst, |
7333 | | bpf_u_int32 size) |
7334 | 3.39k | { |
7335 | 3.39k | int size_code; |
7336 | 3.39k | struct slist *s, *tmp; |
7337 | 3.39k | struct block *b; |
7338 | 3.39k | int regno = alloc_reg(cstate); |
7339 | | |
7340 | 3.39k | free_reg(cstate, inst->regno); |
7341 | 3.39k | switch (size) { |
7342 | | |
7343 | 0 | default: |
7344 | 0 | bpf_error(cstate, "data size must be 1, 2, or 4"); |
7345 | | /*NOTREACHED*/ |
7346 | | |
7347 | 3.07k | case 1: |
7348 | 3.07k | size_code = BPF_B; |
7349 | 3.07k | break; |
7350 | | |
7351 | 138 | case 2: |
7352 | 138 | size_code = BPF_H; |
7353 | 138 | break; |
7354 | | |
7355 | 175 | case 4: |
7356 | 175 | size_code = BPF_W; |
7357 | 175 | break; |
7358 | 3.39k | } |
7359 | 3.38k | switch (proto) { |
7360 | 2 | default: |
7361 | 2 | bpf_error(cstate, "'%s' does not support the index operation", pqkw(proto)); |
7362 | | |
7363 | 94 | case Q_RADIO: |
7364 | | /* |
7365 | | * The offset is relative to the beginning of the packet |
7366 | | * data, if we have a radio header. (If we don't, this |
7367 | | * is an error.) |
7368 | | */ |
7369 | 94 | if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS && |
7370 | 94 | cstate->linktype != DLT_IEEE802_11_RADIO && |
7371 | 94 | cstate->linktype != DLT_PRISM_HEADER) |
7372 | 8 | bpf_error(cstate, "radio information not present in capture"); |
7373 | | |
7374 | | /* |
7375 | | * Load into the X register the offset computed into the |
7376 | | * register specified by "index". |
7377 | | */ |
7378 | 86 | s = xfer_to_x(cstate, inst); |
7379 | | |
7380 | | /* |
7381 | | * Load the item at that offset. |
7382 | | */ |
7383 | 86 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7384 | 86 | sappend(s, tmp); |
7385 | 86 | sappend(inst->s, s); |
7386 | 86 | break; |
7387 | | |
7388 | 754 | case Q_LINK: |
7389 | | /* |
7390 | | * The offset is relative to the beginning of |
7391 | | * the link-layer header. |
7392 | | * |
7393 | | * XXX - what about ATM LANE? Should the index be |
7394 | | * relative to the beginning of the AAL5 frame, so |
7395 | | * that 0 refers to the beginning of the LE Control |
7396 | | * field, or relative to the beginning of the LAN |
7397 | | * frame, so that 0 refers, for Ethernet LANE, to |
7398 | | * the beginning of the destination address? |
7399 | | */ |
7400 | 754 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr); |
7401 | | |
7402 | | /* |
7403 | | * If "s" is non-null, it has code to arrange that the |
7404 | | * X register contains the length of the prefix preceding |
7405 | | * the link-layer header. Add to it the offset computed |
7406 | | * into the register specified by "index", and move that |
7407 | | * into the X register. Otherwise, just load into the X |
7408 | | * register the offset computed into the register specified |
7409 | | * by "index". |
7410 | | */ |
7411 | 754 | if (s != NULL) { |
7412 | 302 | sappend(s, xfer_to_a(cstate, inst)); |
7413 | 302 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7414 | 302 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7415 | 302 | } else |
7416 | 452 | s = xfer_to_x(cstate, inst); |
7417 | | |
7418 | | /* |
7419 | | * Load the item at the sum of the offset we've put in the |
7420 | | * X register and the offset of the start of the link |
7421 | | * layer header (which is 0 if the radio header is |
7422 | | * variable-length; that header length is what we put |
7423 | | * into the X register and then added to the index). |
7424 | | */ |
7425 | 754 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7426 | 754 | tmp->s.k = cstate->off_linkhdr.constant_part; |
7427 | 754 | sappend(s, tmp); |
7428 | 754 | sappend(inst->s, s); |
7429 | 754 | break; |
7430 | | |
7431 | 563 | case Q_IP: |
7432 | 590 | case Q_ARP: |
7433 | 602 | case Q_RARP: |
7434 | 673 | case Q_ATALK: |
7435 | 714 | case Q_DECNET: |
7436 | 740 | case Q_SCA: |
7437 | 774 | case Q_LAT: |
7438 | 790 | case Q_MOPRC: |
7439 | 808 | case Q_MOPDL: |
7440 | 942 | case Q_IPV6: |
7441 | | /* |
7442 | | * The offset is relative to the beginning of |
7443 | | * the network-layer header. |
7444 | | * XXX - are there any cases where we want |
7445 | | * cstate->off_nl_nosnap? |
7446 | | */ |
7447 | 942 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7448 | | |
7449 | | /* |
7450 | | * If "s" is non-null, it has code to arrange that the |
7451 | | * X register contains the variable part of the offset |
7452 | | * of the link-layer payload. Add to it the offset |
7453 | | * computed into the register specified by "index", |
7454 | | * and move that into the X register. Otherwise, just |
7455 | | * load into the X register the offset computed into |
7456 | | * the register specified by "index". |
7457 | | */ |
7458 | 942 | if (s != NULL) { |
7459 | 315 | sappend(s, xfer_to_a(cstate, inst)); |
7460 | 315 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7461 | 315 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7462 | 315 | } else |
7463 | 627 | s = xfer_to_x(cstate, inst); |
7464 | | |
7465 | | /* |
7466 | | * Load the item at the sum of the offset we've put in the |
7467 | | * X register, the offset of the start of the network |
7468 | | * layer header from the beginning of the link-layer |
7469 | | * payload, and the constant part of the offset of the |
7470 | | * start of the link-layer payload. |
7471 | | */ |
7472 | 942 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7473 | 942 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7474 | 942 | sappend(s, tmp); |
7475 | 942 | sappend(inst->s, s); |
7476 | | |
7477 | | /* |
7478 | | * Do the computation only if the packet contains |
7479 | | * the protocol in question. |
7480 | | */ |
7481 | 942 | b = gen_proto_abbrev_internal(cstate, proto); |
7482 | 942 | if (inst->b) |
7483 | 373 | gen_and(inst->b, b); |
7484 | 942 | inst->b = b; |
7485 | 942 | break; |
7486 | | |
7487 | 128 | case Q_SCTP: |
7488 | 463 | case Q_TCP: |
7489 | 920 | case Q_UDP: |
7490 | 995 | case Q_ICMP: |
7491 | 1.03k | case Q_IGMP: |
7492 | 1.09k | case Q_IGRP: |
7493 | 1.28k | case Q_PIM: |
7494 | 1.33k | case Q_VRRP: |
7495 | 1.35k | case Q_CARP: |
7496 | | /* |
7497 | | * The offset is relative to the beginning of |
7498 | | * the transport-layer header. |
7499 | | * |
7500 | | * Load the X register with the length of the IPv4 header |
7501 | | * (plus the offset of the link-layer header, if it's |
7502 | | * a variable-length header), in bytes. |
7503 | | * |
7504 | | * XXX - are there any cases where we want |
7505 | | * cstate->off_nl_nosnap? |
7506 | | * XXX - we should, if we're built with |
7507 | | * IPv6 support, generate code to load either |
7508 | | * IPv4, IPv6, or both, as appropriate. |
7509 | | */ |
7510 | 1.35k | s = gen_loadx_iphdrlen(cstate); |
7511 | | |
7512 | | /* |
7513 | | * The X register now contains the sum of the variable |
7514 | | * part of the offset of the link-layer payload and the |
7515 | | * length of the network-layer header. |
7516 | | * |
7517 | | * Load into the A register the offset relative to |
7518 | | * the beginning of the transport layer header, |
7519 | | * add the X register to that, move that to the |
7520 | | * X register, and load with an offset from the |
7521 | | * X register equal to the sum of the constant part of |
7522 | | * the offset of the link-layer payload and the offset, |
7523 | | * relative to the beginning of the link-layer payload, |
7524 | | * of the network-layer header. |
7525 | | */ |
7526 | 1.35k | sappend(s, xfer_to_a(cstate, inst)); |
7527 | 1.35k | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7528 | 1.35k | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7529 | 1.35k | sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code)); |
7530 | 1.35k | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; |
7531 | 1.35k | sappend(inst->s, s); |
7532 | | |
7533 | | /* |
7534 | | * Do the computation only if the packet contains |
7535 | | * the protocol in question - which is true only |
7536 | | * if this is an IP datagram and is the first or |
7537 | | * only fragment of that datagram. |
7538 | | */ |
7539 | 1.35k | gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate)); |
7540 | 1.35k | if (inst->b) |
7541 | 374 | gen_and(inst->b, b); |
7542 | 1.35k | gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b); |
7543 | 1.35k | inst->b = b; |
7544 | 1.35k | break; |
7545 | 243 | case Q_ICMPV6: |
7546 | | /* |
7547 | | * Do the computation only if the packet contains |
7548 | | * the protocol in question. |
7549 | | */ |
7550 | 243 | b = gen_proto_abbrev_internal(cstate, Q_IPV6); |
7551 | 243 | if (inst->b) |
7552 | 145 | gen_and(inst->b, b); |
7553 | 243 | inst->b = b; |
7554 | | |
7555 | | /* |
7556 | | * Check if we have an icmp6 next header |
7557 | | */ |
7558 | 243 | b = gen_ip6_proto(cstate, 58); |
7559 | 243 | if (inst->b) |
7560 | 243 | gen_and(inst->b, b); |
7561 | 243 | inst->b = b; |
7562 | | |
7563 | 243 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
7564 | | /* |
7565 | | * If "s" is non-null, it has code to arrange that the |
7566 | | * X register contains the variable part of the offset |
7567 | | * of the link-layer payload. Add to it the offset |
7568 | | * computed into the register specified by "index", |
7569 | | * and move that into the X register. Otherwise, just |
7570 | | * load into the X register the offset computed into |
7571 | | * the register specified by "index". |
7572 | | */ |
7573 | 243 | if (s != NULL) { |
7574 | 124 | sappend(s, xfer_to_a(cstate, inst)); |
7575 | 124 | sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); |
7576 | 124 | sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); |
7577 | 124 | } else |
7578 | 119 | s = xfer_to_x(cstate, inst); |
7579 | | |
7580 | | /* |
7581 | | * Load the item at the sum of the offset we've put in the |
7582 | | * X register, the offset of the start of the network |
7583 | | * layer header from the beginning of the link-layer |
7584 | | * payload, and the constant part of the offset of the |
7585 | | * start of the link-layer payload. |
7586 | | */ |
7587 | 243 | tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); |
7588 | 243 | tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40; |
7589 | | |
7590 | 243 | sappend(s, tmp); |
7591 | 243 | sappend(inst->s, s); |
7592 | | |
7593 | 243 | break; |
7594 | 3.38k | } |
7595 | 3.37k | inst->regno = regno; |
7596 | 3.37k | s = new_stmt(cstate, BPF_ST); |
7597 | 3.37k | s->s.k = regno; |
7598 | 3.37k | sappend(inst->s, s); |
7599 | | |
7600 | 3.37k | return inst; |
7601 | 3.38k | } |
7602 | | |
7603 | | struct arth * |
7604 | | gen_load(compiler_state_t *cstate, int proto, struct arth *inst, |
7605 | | bpf_u_int32 size) |
7606 | 3.39k | { |
7607 | | /* |
7608 | | * Catch errors reported by us and routines below us, and return NULL |
7609 | | * on an error. |
7610 | | */ |
7611 | 3.39k | if (setjmp(cstate->top_ctx)) |
7612 | 18 | return (NULL); |
7613 | | |
7614 | 3.37k | return gen_load_internal(cstate, proto, inst, size); |
7615 | 3.39k | } |
7616 | | |
7617 | | static struct block * |
7618 | | gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0, |
7619 | | struct arth *a1, int reversed) |
7620 | 4.38k | { |
7621 | 4.38k | struct slist *s0, *s1, *s2; |
7622 | 4.38k | struct block *b, *tmp; |
7623 | | |
7624 | 4.38k | s0 = xfer_to_x(cstate, a1); |
7625 | 4.38k | s1 = xfer_to_a(cstate, a0); |
7626 | 4.38k | if (code == BPF_JEQ) { |
7627 | 1.65k | s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X); |
7628 | 1.65k | b = new_block(cstate, JMP(code)); |
7629 | 1.65k | sappend(s1, s2); |
7630 | 1.65k | } |
7631 | 2.73k | else |
7632 | 2.73k | b = new_block(cstate, BPF_JMP|code|BPF_X); |
7633 | 4.38k | if (reversed) |
7634 | 1.61k | gen_not(b); |
7635 | | |
7636 | 4.38k | sappend(s0, s1); |
7637 | 4.38k | sappend(a1->s, s0); |
7638 | 4.38k | sappend(a0->s, a1->s); |
7639 | | |
7640 | 4.38k | b->stmts = a0->s; |
7641 | | |
7642 | 4.38k | free_reg(cstate, a0->regno); |
7643 | 4.38k | free_reg(cstate, a1->regno); |
7644 | | |
7645 | | /* 'and' together protocol checks */ |
7646 | 4.38k | if (a0->b) { |
7647 | 327 | if (a1->b) { |
7648 | 105 | gen_and(a0->b, tmp = a1->b); |
7649 | 105 | } |
7650 | 222 | else |
7651 | 222 | tmp = a0->b; |
7652 | 327 | } else |
7653 | 4.05k | tmp = a1->b; |
7654 | | |
7655 | 4.38k | if (tmp) |
7656 | 500 | gen_and(tmp, b); |
7657 | | |
7658 | 4.38k | return b; |
7659 | 4.38k | } |
7660 | | |
7661 | | struct block * |
7662 | | gen_relation(compiler_state_t *cstate, int code, struct arth *a0, |
7663 | | struct arth *a1, int reversed) |
7664 | 4.38k | { |
7665 | | /* |
7666 | | * Catch errors reported by us and routines below us, and return NULL |
7667 | | * on an error. |
7668 | | */ |
7669 | 4.38k | if (setjmp(cstate->top_ctx)) |
7670 | 0 | return (NULL); |
7671 | | |
7672 | 4.38k | return gen_relation_internal(cstate, code, a0, a1, reversed); |
7673 | 4.38k | } |
7674 | | |
7675 | | struct arth * |
7676 | | gen_loadlen(compiler_state_t *cstate) |
7677 | 2.48k | { |
7678 | 2.48k | int regno; |
7679 | 2.48k | struct arth *a; |
7680 | 2.48k | struct slist *s; |
7681 | | |
7682 | | /* |
7683 | | * Catch errors reported by us and routines below us, and return NULL |
7684 | | * on an error. |
7685 | | */ |
7686 | 2.48k | if (setjmp(cstate->top_ctx)) |
7687 | 4 | return (NULL); |
7688 | | |
7689 | 2.47k | regno = alloc_reg(cstate); |
7690 | 2.47k | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7691 | 2.47k | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7692 | 2.47k | s->next = new_stmt(cstate, BPF_ST); |
7693 | 2.47k | s->next->s.k = regno; |
7694 | 2.47k | a->s = s; |
7695 | 2.47k | a->regno = regno; |
7696 | | |
7697 | 2.47k | return a; |
7698 | 2.48k | } |
7699 | | |
7700 | | static struct arth * |
7701 | | gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val) |
7702 | 23.2k | { |
7703 | 23.2k | struct arth *a; |
7704 | 23.2k | struct slist *s; |
7705 | 23.2k | int reg; |
7706 | | |
7707 | 23.2k | a = (struct arth *)newchunk(cstate, sizeof(*a)); |
7708 | | |
7709 | 23.2k | reg = alloc_reg(cstate); |
7710 | | |
7711 | 23.2k | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
7712 | 23.2k | s->s.k = val; |
7713 | 23.2k | s->next = new_stmt(cstate, BPF_ST); |
7714 | 23.2k | s->next->s.k = reg; |
7715 | 23.2k | a->s = s; |
7716 | 23.2k | a->regno = reg; |
7717 | | |
7718 | 23.2k | return a; |
7719 | 23.2k | } |
7720 | | |
7721 | | struct arth * |
7722 | | gen_loadi(compiler_state_t *cstate, bpf_u_int32 val) |
7723 | 23.2k | { |
7724 | | /* |
7725 | | * Catch errors reported by us and routines below us, and return NULL |
7726 | | * on an error. |
7727 | | */ |
7728 | 23.2k | if (setjmp(cstate->top_ctx)) |
7729 | 4 | return (NULL); |
7730 | | |
7731 | 23.2k | return gen_loadi_internal(cstate, val); |
7732 | 23.2k | } |
7733 | | |
7734 | | /* |
7735 | | * The a_arg dance is to avoid annoying whining by compilers that |
7736 | | * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7737 | | * It's not *used* after setjmp returns. |
7738 | | */ |
7739 | | struct arth * |
7740 | | gen_neg(compiler_state_t *cstate, struct arth *a_arg) |
7741 | 13.6k | { |
7742 | 13.6k | struct arth *a = a_arg; |
7743 | 13.6k | struct slist *s; |
7744 | | |
7745 | | /* |
7746 | | * Catch errors reported by us and routines below us, and return NULL |
7747 | | * on an error. |
7748 | | */ |
7749 | 13.6k | if (setjmp(cstate->top_ctx)) |
7750 | 0 | return (NULL); |
7751 | | |
7752 | 13.6k | s = xfer_to_a(cstate, a); |
7753 | 13.6k | sappend(a->s, s); |
7754 | 13.6k | s = new_stmt(cstate, BPF_ALU|BPF_NEG); |
7755 | 13.6k | s->s.k = 0; |
7756 | 13.6k | sappend(a->s, s); |
7757 | 13.6k | s = new_stmt(cstate, BPF_ST); |
7758 | 13.6k | s->s.k = a->regno; |
7759 | 13.6k | sappend(a->s, s); |
7760 | | |
7761 | 13.6k | return a; |
7762 | 13.6k | } |
7763 | | |
7764 | | /* |
7765 | | * The a0_arg dance is to avoid annoying whining by compilers that |
7766 | | * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
7767 | | * It's not *used* after setjmp returns. |
7768 | | */ |
7769 | | struct arth * |
7770 | | gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg, |
7771 | | struct arth *a1) |
7772 | 15.9k | { |
7773 | 15.9k | struct arth *a0 = a0_arg; |
7774 | 15.9k | struct slist *s0, *s1, *s2; |
7775 | | |
7776 | | /* |
7777 | | * Catch errors reported by us and routines below us, and return NULL |
7778 | | * on an error. |
7779 | | */ |
7780 | 15.9k | if (setjmp(cstate->top_ctx)) |
7781 | 44 | return (NULL); |
7782 | | |
7783 | | /* |
7784 | | * Disallow division by, or modulus by, zero; we do this here |
7785 | | * so that it gets done even if the optimizer is disabled. |
7786 | | * |
7787 | | * Also disallow shifts by a value greater than 31; we do this |
7788 | | * here, for the same reason. |
7789 | | */ |
7790 | 15.8k | if (code == BPF_DIV) { |
7791 | 1.50k | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7792 | 5 | bpf_error(cstate, "division by zero"); |
7793 | 14.3k | } else if (code == BPF_MOD) { |
7794 | 1.31k | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) |
7795 | 7 | bpf_error(cstate, "modulus by zero"); |
7796 | 13.0k | } else if (code == BPF_LSH || code == BPF_RSH) { |
7797 | 1.20k | if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31) |
7798 | 32 | bpf_error(cstate, "shift by more than 31 bits"); |
7799 | 1.20k | } |
7800 | 15.8k | s0 = xfer_to_x(cstate, a1); |
7801 | 15.8k | s1 = xfer_to_a(cstate, a0); |
7802 | 15.8k | s2 = new_stmt(cstate, BPF_ALU|BPF_X|code); |
7803 | | |
7804 | 15.8k | sappend(s1, s2); |
7805 | 15.8k | sappend(s0, s1); |
7806 | 15.8k | sappend(a1->s, s0); |
7807 | 15.8k | sappend(a0->s, a1->s); |
7808 | | |
7809 | 15.8k | free_reg(cstate, a0->regno); |
7810 | 15.8k | free_reg(cstate, a1->regno); |
7811 | | |
7812 | 15.8k | s0 = new_stmt(cstate, BPF_ST); |
7813 | 15.8k | a0->regno = s0->s.k = alloc_reg(cstate); |
7814 | 15.8k | sappend(a0->s, s0); |
7815 | | |
7816 | 15.8k | return a0; |
7817 | 15.8k | } |
7818 | | |
7819 | | /* |
7820 | | * Initialize the table of used registers and the current register. |
7821 | | */ |
7822 | | static void |
7823 | | init_regs(compiler_state_t *cstate) |
7824 | 10.0k | { |
7825 | 10.0k | cstate->curreg = 0; |
7826 | 10.0k | memset(cstate->regused, 0, sizeof cstate->regused); |
7827 | 10.0k | } |
7828 | | |
7829 | | /* |
7830 | | * Return the next free register. |
7831 | | */ |
7832 | | static int |
7833 | | alloc_reg(compiler_state_t *cstate) |
7834 | 51.0k | { |
7835 | 51.0k | int n = BPF_MEMWORDS; |
7836 | | |
7837 | 81.6k | while (--n >= 0) { |
7838 | 81.6k | if (cstate->regused[cstate->curreg]) |
7839 | 30.5k | cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS; |
7840 | 51.0k | else { |
7841 | 51.0k | cstate->regused[cstate->curreg] = 1; |
7842 | 51.0k | return cstate->curreg; |
7843 | 51.0k | } |
7844 | 81.6k | } |
7845 | 25 | bpf_error(cstate, "too many registers needed to evaluate expression"); |
7846 | | /*NOTREACHED*/ |
7847 | 51.0k | } |
7848 | | |
7849 | | /* |
7850 | | * Return a register to the table so it can |
7851 | | * be used later. |
7852 | | */ |
7853 | | static void |
7854 | | free_reg(compiler_state_t *cstate, int n) |
7855 | 45.3k | { |
7856 | 45.3k | cstate->regused[n] = 0; |
7857 | 45.3k | } |
7858 | | |
7859 | | static struct block * |
7860 | | gen_len(compiler_state_t *cstate, int jmp, int n) |
7861 | 51 | { |
7862 | 51 | struct slist *s; |
7863 | | |
7864 | 51 | s = new_stmt(cstate, BPF_LD|BPF_LEN); |
7865 | 51 | return gen_jmp(cstate, jmp, n, s); |
7866 | 51 | } |
7867 | | |
7868 | | struct block * |
7869 | | gen_greater(compiler_state_t *cstate, int n) |
7870 | 0 | { |
7871 | | /* |
7872 | | * Catch errors reported by us and routines below us, and return NULL |
7873 | | * on an error. |
7874 | | */ |
7875 | 0 | if (setjmp(cstate->top_ctx)) |
7876 | 0 | return (NULL); |
7877 | | |
7878 | 0 | return gen_len(cstate, BPF_JGE, n); |
7879 | 0 | } |
7880 | | |
7881 | | /* |
7882 | | * Actually, this is less than or equal. |
7883 | | */ |
7884 | | struct block * |
7885 | | gen_less(compiler_state_t *cstate, int n) |
7886 | 51 | { |
7887 | 51 | struct block *b; |
7888 | | |
7889 | | /* |
7890 | | * Catch errors reported by us and routines below us, and return NULL |
7891 | | * on an error. |
7892 | | */ |
7893 | 51 | if (setjmp(cstate->top_ctx)) |
7894 | 0 | return (NULL); |
7895 | | |
7896 | 51 | b = gen_len(cstate, BPF_JGT, n); |
7897 | 51 | gen_not(b); |
7898 | | |
7899 | 51 | return b; |
7900 | 51 | } |
7901 | | |
7902 | | /* |
7903 | | * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to |
7904 | | * the beginning of the link-layer header. |
7905 | | * XXX - that means you can't test values in the radiotap header, but |
7906 | | * as that header is difficult if not impossible to parse generally |
7907 | | * without a loop, that might not be a severe problem. A new keyword |
7908 | | * "radio" could be added for that, although what you'd really want |
7909 | | * would be a way of testing particular radio header values, which |
7910 | | * would generate code appropriate to the radio header in question. |
7911 | | */ |
7912 | | struct block * |
7913 | | gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val) |
7914 | 180 | { |
7915 | 180 | struct block *b; |
7916 | 180 | struct slist *s; |
7917 | | |
7918 | | /* |
7919 | | * Catch errors reported by us and routines below us, and return NULL |
7920 | | * on an error. |
7921 | | */ |
7922 | 180 | if (setjmp(cstate->top_ctx)) |
7923 | 16 | return (NULL); |
7924 | | |
7925 | 164 | assert_maxval(cstate, "byte argument", val, UINT8_MAX); |
7926 | | |
7927 | 164 | switch (op) { |
7928 | 0 | default: |
7929 | 0 | abort(); |
7930 | | |
7931 | 15 | case '=': |
7932 | 15 | return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7933 | | |
7934 | 16 | case '<': |
7935 | 16 | return gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7936 | | |
7937 | 39 | case '>': |
7938 | 39 | return gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); |
7939 | | |
7940 | 45 | case '|': |
7941 | 45 | s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K); |
7942 | 45 | break; |
7943 | | |
7944 | 49 | case '&': |
7945 | 49 | s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
7946 | 49 | break; |
7947 | 164 | } |
7948 | 94 | s->s.k = val; |
7949 | | // Load the required byte first. |
7950 | 94 | struct slist *s0 = gen_load_a(cstate, OR_LINKHDR, idx, BPF_B); |
7951 | 94 | sappend(s0, s); |
7952 | 94 | b = gen_jmp(cstate, BPF_JEQ, 0, s0); |
7953 | 94 | gen_not(b); |
7954 | | |
7955 | 94 | return b; |
7956 | 164 | } |
7957 | | |
7958 | | struct block * |
7959 | | gen_broadcast(compiler_state_t *cstate, int proto) |
7960 | 0 | { |
7961 | 0 | bpf_u_int32 hostmask; |
7962 | 0 | struct block *b0, *b1, *b2; |
7963 | 0 | static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
7964 | | |
7965 | | /* |
7966 | | * Catch errors reported by us and routines below us, and return NULL |
7967 | | * on an error. |
7968 | | */ |
7969 | 0 | if (setjmp(cstate->top_ctx)) |
7970 | 0 | return (NULL); |
7971 | | |
7972 | 0 | switch (proto) { |
7973 | | |
7974 | 0 | case Q_DEFAULT: |
7975 | 0 | case Q_LINK: |
7976 | 0 | switch (cstate->linktype) { |
7977 | 0 | case DLT_ARCNET: |
7978 | 0 | case DLT_ARCNET_LINUX: |
7979 | | // ARCnet broadcast is [8-bit] destination address 0. |
7980 | 0 | return gen_ahostop(cstate, 0, Q_DST); |
7981 | 0 | } |
7982 | 0 | return gen_mac48host(cstate, ebroadcast, Q_DST, "broadcast"); |
7983 | | /*NOTREACHED*/ |
7984 | | |
7985 | 0 | case Q_IP: |
7986 | | /* |
7987 | | * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff) |
7988 | | * as an indication that we don't know the netmask, and fail |
7989 | | * in that case. |
7990 | | */ |
7991 | 0 | if (cstate->netmask == PCAP_NETMASK_UNKNOWN) |
7992 | 0 | bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported"); |
7993 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
7994 | 0 | hostmask = ~cstate->netmask; |
7995 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask); |
7996 | 0 | b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, hostmask, hostmask); |
7997 | 0 | gen_or(b1, b2); |
7998 | 0 | gen_and(b0, b2); |
7999 | 0 | return b2; |
8000 | 0 | } |
8001 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "broadcast"); |
8002 | | /*NOTREACHED*/ |
8003 | 0 | } |
8004 | | |
8005 | | /* |
8006 | | * Generate code to test the low-order bit of a MAC address (that's |
8007 | | * the bottom bit of the *first* byte). |
8008 | | */ |
8009 | | static struct block * |
8010 | | gen_mac_multicast(compiler_state_t *cstate, int offset) |
8011 | 0 | { |
8012 | 0 | register struct slist *s; |
8013 | | |
8014 | | /* link[offset] & 1 != 0 */ |
8015 | 0 | s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B); |
8016 | 0 | return gen_set(cstate, 1, s); |
8017 | 0 | } |
8018 | | |
8019 | | struct block * |
8020 | | gen_multicast(compiler_state_t *cstate, int proto) |
8021 | 0 | { |
8022 | 0 | register struct block *b0, *b1, *b2; |
8023 | 0 | register struct slist *s; |
8024 | | |
8025 | | /* |
8026 | | * Catch errors reported by us and routines below us, and return NULL |
8027 | | * on an error. |
8028 | | */ |
8029 | 0 | if (setjmp(cstate->top_ctx)) |
8030 | 0 | return (NULL); |
8031 | | |
8032 | 0 | switch (proto) { |
8033 | | |
8034 | 0 | case Q_DEFAULT: |
8035 | 0 | case Q_LINK: |
8036 | 0 | switch (cstate->linktype) { |
8037 | 0 | case DLT_ARCNET: |
8038 | 0 | case DLT_ARCNET_LINUX: |
8039 | | // ARCnet multicast is the same as broadcast. |
8040 | 0 | return gen_ahostop(cstate, 0, Q_DST); |
8041 | 0 | case DLT_EN10MB: |
8042 | 0 | case DLT_NETANALYZER: |
8043 | 0 | case DLT_NETANALYZER_TRANSPARENT: |
8044 | 0 | b1 = gen_prevlinkhdr_check(cstate); |
8045 | | /* ether[0] & 1 != 0 */ |
8046 | 0 | b0 = gen_mac_multicast(cstate, 0); |
8047 | 0 | if (b1 != NULL) |
8048 | 0 | gen_and(b1, b0); |
8049 | 0 | return b0; |
8050 | 0 | case DLT_FDDI: |
8051 | | /* |
8052 | | * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX |
8053 | | * |
8054 | | * XXX - was that referring to bit-order issues? |
8055 | | */ |
8056 | | /* fddi[1] & 1 != 0 */ |
8057 | 0 | return gen_mac_multicast(cstate, 1); |
8058 | 0 | case DLT_IEEE802: |
8059 | | /* tr[2] & 1 != 0 */ |
8060 | 0 | return gen_mac_multicast(cstate, 2); |
8061 | 0 | case DLT_IEEE802_11: |
8062 | 0 | case DLT_PRISM_HEADER: |
8063 | 0 | case DLT_IEEE802_11_RADIO_AVS: |
8064 | 0 | case DLT_IEEE802_11_RADIO: |
8065 | 0 | case DLT_PPI: |
8066 | | /* |
8067 | | * Oh, yuk. |
8068 | | * |
8069 | | * For control frames, there is no DA. |
8070 | | * |
8071 | | * For management frames, DA is at an |
8072 | | * offset of 4 from the beginning of |
8073 | | * the packet. |
8074 | | * |
8075 | | * For data frames, DA is at an offset |
8076 | | * of 4 from the beginning of the packet |
8077 | | * if To DS is clear and at an offset of |
8078 | | * 16 from the beginning of the packet |
8079 | | * if To DS is set. |
8080 | | */ |
8081 | | |
8082 | | /* |
8083 | | * Generate the tests to be done for data frames. |
8084 | | * |
8085 | | * First, check for To DS set, i.e. "link[1] & 0x01". |
8086 | | */ |
8087 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8088 | 0 | b1 = gen_set(cstate, IEEE80211_FC1_DIR_TODS, s); |
8089 | | |
8090 | | /* |
8091 | | * If To DS is set, the DA is at 16. |
8092 | | */ |
8093 | 0 | b0 = gen_mac_multicast(cstate, 16); |
8094 | 0 | gen_and(b1, b0); |
8095 | | |
8096 | | /* |
8097 | | * Now, check for To DS not set, i.e. check |
8098 | | * "!(link[1] & 0x01)". |
8099 | | */ |
8100 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); |
8101 | 0 | b2 = gen_unset(cstate, IEEE80211_FC1_DIR_TODS, s); |
8102 | | |
8103 | | /* |
8104 | | * If To DS is not set, the DA is at 4. |
8105 | | */ |
8106 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8107 | 0 | gen_and(b2, b1); |
8108 | | |
8109 | | /* |
8110 | | * Now OR together the last two checks. That gives |
8111 | | * the complete set of checks for data frames. |
8112 | | */ |
8113 | 0 | gen_or(b1, b0); |
8114 | | |
8115 | | /* |
8116 | | * Now check for a data frame. |
8117 | | * I.e, check "link[0] & 0x08". |
8118 | | */ |
8119 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8120 | 0 | b1 = gen_set(cstate, IEEE80211_FC0_TYPE_DATA, s); |
8121 | | |
8122 | | /* |
8123 | | * AND that with the checks done for data frames. |
8124 | | */ |
8125 | 0 | gen_and(b1, b0); |
8126 | | |
8127 | | /* |
8128 | | * If the high-order bit of the type value is 0, this |
8129 | | * is a management frame. |
8130 | | * I.e, check "!(link[0] & 0x08)". |
8131 | | */ |
8132 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8133 | 0 | b2 = gen_unset(cstate, IEEE80211_FC0_TYPE_DATA, s); |
8134 | | |
8135 | | /* |
8136 | | * For management frames, the DA is at 4. |
8137 | | */ |
8138 | 0 | b1 = gen_mac_multicast(cstate, 4); |
8139 | 0 | gen_and(b2, b1); |
8140 | | |
8141 | | /* |
8142 | | * OR that with the checks done for data frames. |
8143 | | * That gives the checks done for management and |
8144 | | * data frames. |
8145 | | */ |
8146 | 0 | gen_or(b1, b0); |
8147 | | |
8148 | | /* |
8149 | | * If the low-order bit of the type value is 1, |
8150 | | * this is either a control frame or a frame |
8151 | | * with a reserved type, and thus not a |
8152 | | * frame with an SA. |
8153 | | * |
8154 | | * I.e., check "!(link[0] & 0x04)". |
8155 | | */ |
8156 | 0 | s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); |
8157 | 0 | b1 = gen_unset(cstate, IEEE80211_FC0_TYPE_CTL, s); |
8158 | | |
8159 | | /* |
8160 | | * AND that with the checks for data and management |
8161 | | * frames. |
8162 | | */ |
8163 | 0 | gen_and(b1, b0); |
8164 | 0 | return b0; |
8165 | 0 | case DLT_IP_OVER_FC: |
8166 | 0 | return gen_mac_multicast(cstate, 2); |
8167 | 0 | default: |
8168 | 0 | break; |
8169 | 0 | } |
8170 | 0 | fail_kw_on_dlt(cstate, "multicast"); |
8171 | | /*NOTREACHED*/ |
8172 | | |
8173 | 0 | case Q_IP: |
8174 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IP); |
8175 | | |
8176 | | /* |
8177 | | * Compare address with 224.0.0.0/4 |
8178 | | */ |
8179 | 0 | b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_B, 0xe0, 0xf0); |
8180 | |
|
8181 | 0 | gen_and(b0, b1); |
8182 | 0 | return b1; |
8183 | | |
8184 | 0 | case Q_IPV6: |
8185 | 0 | b0 = gen_linktype(cstate, ETHERTYPE_IPV6); |
8186 | 0 | b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255); |
8187 | 0 | gen_and(b0, b1); |
8188 | 0 | return b1; |
8189 | 0 | } |
8190 | 0 | bpf_error(cstate, ERRSTR_INVALID_QUAL, pqkw(proto), "multicast"); |
8191 | | /*NOTREACHED*/ |
8192 | 0 | } |
8193 | | |
8194 | | #ifdef __linux__ |
8195 | | /* |
8196 | | * This is Linux; we require PF_PACKET support. If this is a *live* capture, |
8197 | | * we can look at special meta-data in the filter expression; otherwise we |
8198 | | * can't because it is either a savefile (rfile != NULL) or a pcap_t created |
8199 | | * using pcap_open_dead() (rfile == NULL). Thus check for a flag that |
8200 | | * pcap_activate() conditionally sets. |
8201 | | */ |
8202 | | static void |
8203 | | require_basic_bpf_extensions(compiler_state_t *cstate, const char *keyword) |
8204 | 0 | { |
8205 | 0 | if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_BASIC_HANDLING) |
8206 | 0 | return; |
8207 | 0 | bpf_error(cstate, "%s not supported on %s (not a live capture)", |
8208 | 0 | keyword, |
8209 | 0 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8210 | 0 | } |
8211 | | #endif // __linux__ |
8212 | | |
8213 | | struct block * |
8214 | | gen_ifindex(compiler_state_t *cstate, int ifindex) |
8215 | 0 | { |
8216 | | /* |
8217 | | * Catch errors reported by us and routines below us, and return NULL |
8218 | | * on an error. |
8219 | | */ |
8220 | 0 | if (setjmp(cstate->top_ctx)) |
8221 | 0 | return (NULL); |
8222 | | |
8223 | | /* |
8224 | | * Only some data link types support ifindex qualifiers. |
8225 | | */ |
8226 | 0 | switch (cstate->linktype) { |
8227 | 0 | case DLT_LINUX_SLL2: |
8228 | | /* match packets on this interface */ |
8229 | 0 | return gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex); |
8230 | 0 | default: |
8231 | 0 | #if defined(__linux__) |
8232 | 0 | require_basic_bpf_extensions(cstate, "ifindex"); |
8233 | | /* match ifindex */ |
8234 | 0 | return gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W, |
8235 | 0 | ifindex); |
8236 | | #else /* defined(__linux__) */ |
8237 | | fail_kw_on_dlt(cstate, "ifindex"); |
8238 | | /*NOTREACHED*/ |
8239 | | #endif /* defined(__linux__) */ |
8240 | 0 | } |
8241 | 0 | } |
8242 | | |
8243 | | /* |
8244 | | * Filter on inbound (outbound == 0) or outbound (outbound == 1) traffic. |
8245 | | * Outbound traffic is sent by this machine, while inbound traffic is |
8246 | | * sent by a remote machine (and may include packets destined for a |
8247 | | * unicast or multicast link-layer address we are not subscribing to). |
8248 | | * These are the same definitions implemented by pcap_setdirection(). |
8249 | | * Capturing only unicast traffic destined for this host is probably |
8250 | | * better accomplished using a higher-layer filter. |
8251 | | */ |
8252 | | struct block * |
8253 | | gen_inbound_outbound(compiler_state_t *cstate, const int outbound) |
8254 | 0 | { |
8255 | 0 | register struct block *b0; |
8256 | | |
8257 | | /* |
8258 | | * Catch errors reported by us and routines below us, and return NULL |
8259 | | * on an error. |
8260 | | */ |
8261 | 0 | if (setjmp(cstate->top_ctx)) |
8262 | 0 | return (NULL); |
8263 | | |
8264 | | /* |
8265 | | * Only some data link types support inbound/outbound qualifiers. |
8266 | | */ |
8267 | 0 | switch (cstate->linktype) { |
8268 | 0 | case DLT_SLIP: |
8269 | 0 | return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, |
8270 | 0 | outbound ? SLIPDIR_OUT : SLIPDIR_IN); |
8271 | | |
8272 | 0 | case DLT_IPNET: |
8273 | 0 | return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, |
8274 | 0 | outbound ? IPNET_OUTBOUND : IPNET_INBOUND); |
8275 | | |
8276 | 0 | case DLT_LINUX_SLL: |
8277 | | /* match outgoing packets */ |
8278 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING); |
8279 | 0 | if (! outbound) { |
8280 | | /* to filter on inbound traffic, invert the match */ |
8281 | 0 | gen_not(b0); |
8282 | 0 | } |
8283 | 0 | return b0; |
8284 | | |
8285 | 0 | case DLT_LINUX_SLL2: |
8286 | | /* match outgoing packets */ |
8287 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING); |
8288 | 0 | if (! outbound) { |
8289 | | /* to filter on inbound traffic, invert the match */ |
8290 | 0 | gen_not(b0); |
8291 | 0 | } |
8292 | 0 | return b0; |
8293 | | |
8294 | 0 | case DLT_PFLOG: |
8295 | 0 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B, |
8296 | 0 | outbound ? PF_OUT : PF_IN); |
8297 | | |
8298 | 0 | case DLT_PPP_PPPD: |
8299 | 0 | return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, outbound ? PPP_PPPD_OUT : PPP_PPPD_IN); |
8300 | | |
8301 | 0 | case DLT_JUNIPER_MFR: |
8302 | 0 | case DLT_JUNIPER_MLFR: |
8303 | 0 | case DLT_JUNIPER_MLPPP: |
8304 | 0 | case DLT_JUNIPER_ATM1: |
8305 | 0 | case DLT_JUNIPER_ATM2: |
8306 | 0 | case DLT_JUNIPER_PPPOE: |
8307 | 0 | case DLT_JUNIPER_PPPOE_ATM: |
8308 | 0 | case DLT_JUNIPER_GGSN: |
8309 | 0 | case DLT_JUNIPER_ES: |
8310 | 0 | case DLT_JUNIPER_MONITOR: |
8311 | 0 | case DLT_JUNIPER_SERVICES: |
8312 | 0 | case DLT_JUNIPER_ETHER: |
8313 | 0 | case DLT_JUNIPER_PPP: |
8314 | 0 | case DLT_JUNIPER_FRELAY: |
8315 | 0 | case DLT_JUNIPER_CHDLC: |
8316 | 0 | case DLT_JUNIPER_VP: |
8317 | 0 | case DLT_JUNIPER_ST: |
8318 | 0 | case DLT_JUNIPER_ISM: |
8319 | 0 | case DLT_JUNIPER_VS: |
8320 | 0 | case DLT_JUNIPER_SRX_E2E: |
8321 | 0 | case DLT_JUNIPER_FIBRECHANNEL: |
8322 | 0 | case DLT_JUNIPER_ATM_CEMIC: |
8323 | | /* juniper flags (including direction) are stored |
8324 | | * the byte after the 3-byte magic number */ |
8325 | 0 | return gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, outbound ? 0 : 1, 0x01); |
8326 | | |
8327 | 0 | default: |
8328 | | /* |
8329 | | * If we have packet meta-data indicating a direction, |
8330 | | * and that metadata can be checked by BPF code, check |
8331 | | * it. Otherwise, give up, as this link-layer type has |
8332 | | * nothing in the packet data. |
8333 | | * |
8334 | | * Currently, the only platform where a BPF filter can |
8335 | | * check that metadata is Linux with the in-kernel |
8336 | | * BPF interpreter. If other packet capture mechanisms |
8337 | | * and BPF filters also supported this, it would be |
8338 | | * nice. It would be even better if they made that |
8339 | | * metadata available so that we could provide it |
8340 | | * with newer capture APIs, allowing it to be saved |
8341 | | * in pcapng files. |
8342 | | */ |
8343 | 0 | #if defined(__linux__) |
8344 | 0 | require_basic_bpf_extensions(cstate, outbound ? "outbound" : "inbound"); |
8345 | | /* match outgoing packets */ |
8346 | 0 | b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H, |
8347 | 0 | PACKET_OUTGOING); |
8348 | 0 | if (! outbound) { |
8349 | | /* to filter on inbound traffic, invert the match */ |
8350 | 0 | gen_not(b0); |
8351 | 0 | } |
8352 | 0 | return b0; |
8353 | | #else /* defined(__linux__) */ |
8354 | | fail_kw_on_dlt(cstate, outbound ? "outbound" : "inbound"); |
8355 | | /*NOTREACHED*/ |
8356 | | #endif /* defined(__linux__) */ |
8357 | 0 | } |
8358 | 0 | } |
8359 | | |
8360 | | /* PF firewall log matched interface */ |
8361 | | struct block * |
8362 | | gen_pf_ifname(compiler_state_t *cstate, const char *ifname) |
8363 | 35 | { |
8364 | 35 | u_int len, off; |
8365 | | |
8366 | | /* |
8367 | | * Catch errors reported by us and routines below us, and return NULL |
8368 | | * on an error. |
8369 | | */ |
8370 | 35 | if (setjmp(cstate->top_ctx)) |
8371 | 8 | return (NULL); |
8372 | | |
8373 | 27 | assert_pflog(cstate, "ifname"); |
8374 | | |
8375 | 27 | len = sizeof(((struct pfloghdr *)0)->ifname); |
8376 | 27 | off = offsetof(struct pfloghdr, ifname); |
8377 | 27 | if (strlen(ifname) >= len) { |
8378 | 1 | bpf_error(cstate, "ifname interface names can only be %d characters", |
8379 | 1 | len-1); |
8380 | | /*NOTREACHED*/ |
8381 | 1 | } |
8382 | 26 | return gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname), |
8383 | 26 | (const u_char *)ifname); |
8384 | 27 | } |
8385 | | |
8386 | | /* PF firewall log ruleset name */ |
8387 | | struct block * |
8388 | | gen_pf_ruleset(compiler_state_t *cstate, char *ruleset) |
8389 | 17 | { |
8390 | | /* |
8391 | | * Catch errors reported by us and routines below us, and return NULL |
8392 | | * on an error. |
8393 | | */ |
8394 | 17 | if (setjmp(cstate->top_ctx)) |
8395 | 8 | return (NULL); |
8396 | | |
8397 | 9 | assert_pflog(cstate, "ruleset"); |
8398 | | |
8399 | 9 | if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { |
8400 | 1 | bpf_error(cstate, "ruleset names can only be %ld characters", |
8401 | 1 | (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1)); |
8402 | | /*NOTREACHED*/ |
8403 | 1 | } |
8404 | | |
8405 | 8 | return gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset), |
8406 | 8 | (u_int)strlen(ruleset), (const u_char *)ruleset); |
8407 | 9 | } |
8408 | | |
8409 | | /* PF firewall log rule number */ |
8410 | | struct block * |
8411 | | gen_pf_rnr(compiler_state_t *cstate, int rnr) |
8412 | 14 | { |
8413 | | /* |
8414 | | * Catch errors reported by us and routines below us, and return NULL |
8415 | | * on an error. |
8416 | | */ |
8417 | 14 | if (setjmp(cstate->top_ctx)) |
8418 | 6 | return (NULL); |
8419 | | |
8420 | 8 | assert_pflog(cstate, "rnr"); |
8421 | | |
8422 | 8 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W, |
8423 | 8 | (bpf_u_int32)rnr); |
8424 | 14 | } |
8425 | | |
8426 | | /* PF firewall log sub-rule number */ |
8427 | | struct block * |
8428 | | gen_pf_srnr(compiler_state_t *cstate, int srnr) |
8429 | 18 | { |
8430 | | /* |
8431 | | * Catch errors reported by us and routines below us, and return NULL |
8432 | | * on an error. |
8433 | | */ |
8434 | 18 | if (setjmp(cstate->top_ctx)) |
8435 | 8 | return (NULL); |
8436 | | |
8437 | 10 | assert_pflog(cstate, "srnr"); |
8438 | | |
8439 | 10 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W, |
8440 | 10 | (bpf_u_int32)srnr); |
8441 | 18 | } |
8442 | | |
8443 | | /* PF firewall log reason code */ |
8444 | | struct block * |
8445 | | gen_pf_reason(compiler_state_t *cstate, int reason) |
8446 | 19 | { |
8447 | | /* |
8448 | | * Catch errors reported by us and routines below us, and return NULL |
8449 | | * on an error. |
8450 | | */ |
8451 | 19 | if (setjmp(cstate->top_ctx)) |
8452 | 5 | return (NULL); |
8453 | | |
8454 | 14 | assert_pflog(cstate, "reason"); |
8455 | | |
8456 | 14 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B, |
8457 | 14 | (bpf_u_int32)reason); |
8458 | 19 | } |
8459 | | |
8460 | | /* PF firewall log action */ |
8461 | | struct block * |
8462 | | gen_pf_action(compiler_state_t *cstate, int action) |
8463 | 6 | { |
8464 | | /* |
8465 | | * Catch errors reported by us and routines below us, and return NULL |
8466 | | * on an error. |
8467 | | */ |
8468 | 6 | if (setjmp(cstate->top_ctx)) |
8469 | 5 | return (NULL); |
8470 | | |
8471 | 1 | assert_pflog(cstate, "action"); |
8472 | | |
8473 | 1 | return gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B, |
8474 | 1 | (bpf_u_int32)action); |
8475 | 6 | } |
8476 | | |
8477 | | /* IEEE 802.11 wireless header */ |
8478 | | struct block * |
8479 | | gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask) |
8480 | 186 | { |
8481 | | /* |
8482 | | * Catch errors reported by us and routines below us, and return NULL |
8483 | | * on an error. |
8484 | | */ |
8485 | 186 | if (setjmp(cstate->top_ctx)) |
8486 | 5 | return (NULL); |
8487 | | |
8488 | 181 | switch (cstate->linktype) { |
8489 | | |
8490 | 41 | case DLT_IEEE802_11: |
8491 | 80 | case DLT_PRISM_HEADER: |
8492 | 114 | case DLT_IEEE802_11_RADIO_AVS: |
8493 | 151 | case DLT_IEEE802_11_RADIO: |
8494 | 181 | case DLT_PPI: |
8495 | 181 | return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask); |
8496 | | |
8497 | 5 | default: |
8498 | 5 | fail_kw_on_dlt(cstate, "type/subtype"); |
8499 | | /*NOTREACHED*/ |
8500 | 181 | } |
8501 | 181 | } |
8502 | | |
8503 | | struct block * |
8504 | | gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir) |
8505 | 201 | { |
8506 | | /* |
8507 | | * Catch errors reported by us and routines below us, and return NULL |
8508 | | * on an error. |
8509 | | */ |
8510 | 201 | if (setjmp(cstate->top_ctx)) |
8511 | 1 | return (NULL); |
8512 | | |
8513 | 200 | switch (cstate->linktype) { |
8514 | | |
8515 | 49 | case DLT_IEEE802_11: |
8516 | 83 | case DLT_PRISM_HEADER: |
8517 | 118 | case DLT_IEEE802_11_RADIO_AVS: |
8518 | 151 | case DLT_IEEE802_11_RADIO: |
8519 | 200 | case DLT_PPI: |
8520 | 200 | return gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir, |
8521 | 200 | IEEE80211_FC1_DIR_MASK); |
8522 | | |
8523 | 1 | default: |
8524 | 1 | fail_kw_on_dlt(cstate, "dir"); |
8525 | | /*NOTREACHED*/ |
8526 | 200 | } |
8527 | 200 | } |
8528 | | |
8529 | | // Process an ARCnet host address string. |
8530 | | struct block * |
8531 | | gen_acode(compiler_state_t *cstate, const char *s, struct qual q) |
8532 | 479 | { |
8533 | | /* |
8534 | | * Catch errors reported by us and routines below us, and return NULL |
8535 | | * on an error. |
8536 | | */ |
8537 | 479 | if (setjmp(cstate->top_ctx)) |
8538 | 20 | return (NULL); |
8539 | | |
8540 | 459 | switch (cstate->linktype) { |
8541 | | |
8542 | 337 | case DLT_ARCNET: |
8543 | 478 | case DLT_ARCNET_LINUX: |
8544 | 478 | if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && |
8545 | 478 | q.proto == Q_LINK) { |
8546 | 472 | uint8_t addr; |
8547 | | /* |
8548 | | * The lexer currently defines the address format in a |
8549 | | * way that makes this error condition never true. |
8550 | | * Let's check it anyway in case this part of the lexer |
8551 | | * changes in future. |
8552 | | */ |
8553 | 472 | if (! pcapint_atoan(s, &addr)) |
8554 | 0 | bpf_error(cstate, "invalid ARCnet address '%s'", s); |
8555 | 472 | return gen_ahostop(cstate, addr, (int)q.dir); |
8556 | 472 | } else |
8557 | 6 | bpf_error(cstate, "ARCnet address used in non-arc expression"); |
8558 | | /*NOTREACHED*/ |
8559 | | |
8560 | 1 | default: |
8561 | 1 | bpf_error(cstate, "aid supported only on ARCnet"); |
8562 | | /*NOTREACHED*/ |
8563 | 459 | } |
8564 | 459 | } |
8565 | | |
8566 | | // Compare an ARCnet host address with the given value. |
8567 | | static struct block * |
8568 | | gen_ahostop(compiler_state_t *cstate, const uint8_t eaddr, int dir) |
8569 | 684 | { |
8570 | 684 | register struct block *b0, *b1; |
8571 | | |
8572 | 684 | switch (dir) { |
8573 | | /* |
8574 | | * ARCnet is different from Ethernet: the source address comes before |
8575 | | * the destination address, each is one byte long. This holds for all |
8576 | | * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10 |
8577 | | * in the 1983 edition of the "ARCNET Designer's Handbook" published |
8578 | | * by Datapoint (document number 61610-01). |
8579 | | */ |
8580 | 209 | case Q_SRC: |
8581 | 209 | return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, eaddr); |
8582 | | |
8583 | 356 | case Q_DST: |
8584 | 356 | return gen_cmp(cstate, OR_LINKHDR, 1, BPF_B, eaddr); |
8585 | | |
8586 | 7 | case Q_AND: |
8587 | 7 | b0 = gen_ahostop(cstate, eaddr, Q_SRC); |
8588 | 7 | b1 = gen_ahostop(cstate, eaddr, Q_DST); |
8589 | 7 | gen_and(b0, b1); |
8590 | 7 | return b1; |
8591 | | |
8592 | 93 | case Q_DEFAULT: |
8593 | 99 | case Q_OR: |
8594 | 99 | b0 = gen_ahostop(cstate, eaddr, Q_SRC); |
8595 | 99 | b1 = gen_ahostop(cstate, eaddr, Q_DST); |
8596 | 99 | gen_or(b0, b1); |
8597 | 99 | return b1; |
8598 | | |
8599 | 0 | case Q_ADDR1: |
8600 | 1 | case Q_ADDR2: |
8601 | 2 | case Q_ADDR3: |
8602 | 3 | case Q_ADDR4: |
8603 | 6 | case Q_RA: |
8604 | 13 | case Q_TA: |
8605 | 13 | bpf_error(cstate, ERRSTR_802_11_ONLY_KW, dqkw(dir)); |
8606 | | /*NOTREACHED*/ |
8607 | 684 | } |
8608 | 0 | abort(); |
8609 | | /*NOTREACHED*/ |
8610 | 684 | } |
8611 | | |
8612 | | static struct block * |
8613 | | gen_vlan_tpid_test(compiler_state_t *cstate) |
8614 | 515 | { |
8615 | 515 | struct block *b0, *b1; |
8616 | | |
8617 | | /* check for VLAN, including 802.1ad and QinQ */ |
8618 | 515 | b0 = gen_linktype(cstate, ETHERTYPE_8021Q); |
8619 | 515 | b1 = gen_linktype(cstate, ETHERTYPE_8021AD); |
8620 | 515 | gen_or(b0,b1); |
8621 | 515 | b0 = b1; |
8622 | 515 | b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ); |
8623 | 515 | gen_or(b0,b1); |
8624 | | |
8625 | 515 | return b1; |
8626 | 515 | } |
8627 | | |
8628 | | static struct block * |
8629 | | gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num) |
8630 | 65 | { |
8631 | 65 | assert_maxval(cstate, "VLAN tag", vlan_num, 0x0fff); |
8632 | 65 | return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff); |
8633 | 65 | } |
8634 | | |
8635 | | static struct block * |
8636 | | gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8637 | | int has_vlan_tag) |
8638 | 515 | { |
8639 | 515 | struct block *b0, *b1; |
8640 | | |
8641 | 515 | b0 = gen_vlan_tpid_test(cstate); |
8642 | | |
8643 | 515 | if (has_vlan_tag) { |
8644 | 65 | b1 = gen_vlan_vid_test(cstate, vlan_num); |
8645 | 65 | gen_and(b0, b1); |
8646 | 65 | b0 = b1; |
8647 | 65 | } |
8648 | | |
8649 | | /* |
8650 | | * Both payload and link header type follow the VLAN tags so that |
8651 | | * both need to be updated. |
8652 | | */ |
8653 | 515 | cstate->off_linkpl.constant_part += 4; |
8654 | 515 | cstate->off_linktype.constant_part += 4; |
8655 | | |
8656 | 515 | return b0; |
8657 | 515 | } |
8658 | | |
8659 | | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8660 | | /* add v to variable part of off */ |
8661 | | static void |
8662 | | gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, |
8663 | | bpf_u_int32 v, struct slist *s) |
8664 | 0 | { |
8665 | 0 | struct slist *s2; |
8666 | |
|
8667 | 0 | if (!off->is_variable) |
8668 | 0 | off->is_variable = 1; |
8669 | 0 | if (off->reg == -1) |
8670 | 0 | off->reg = alloc_reg(cstate); |
8671 | |
|
8672 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_MEM); |
8673 | 0 | s2->s.k = off->reg; |
8674 | 0 | sappend(s, s2); |
8675 | 0 | s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); |
8676 | 0 | s2->s.k = v; |
8677 | 0 | sappend(s, s2); |
8678 | 0 | s2 = new_stmt(cstate, BPF_ST); |
8679 | 0 | s2->s.k = off->reg; |
8680 | 0 | sappend(s, s2); |
8681 | 0 | } |
8682 | | |
8683 | | /* |
8684 | | * patch block b_tpid (VLAN TPID test) to update variable parts of link payload |
8685 | | * and link type offsets first |
8686 | | */ |
8687 | | static void |
8688 | | gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid) |
8689 | 0 | { |
8690 | 0 | struct slist s; |
8691 | | |
8692 | | /* offset determined at run time, shift variable part */ |
8693 | 0 | s.next = NULL; |
8694 | 0 | cstate->is_vlan_vloffset = 1; |
8695 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s); |
8696 | 0 | gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s); |
8697 | | |
8698 | | /* we get a pointer to a chain of or-ed blocks, patch first of them */ |
8699 | 0 | sappend(s.next, b_tpid->head->stmts); |
8700 | 0 | b_tpid->head->stmts = s.next; |
8701 | 0 | } |
8702 | | |
8703 | | /* |
8704 | | * patch block b_vid (VLAN id test) to load VID value either from packet |
8705 | | * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true |
8706 | | */ |
8707 | | static void |
8708 | | gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid) |
8709 | 0 | { |
8710 | 0 | struct slist *s, *s2, *sjeq; |
8711 | 0 | unsigned cnt; |
8712 | |
|
8713 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8714 | 0 | s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); |
8715 | | |
8716 | | /* true -> next instructions, false -> beginning of b_vid */ |
8717 | 0 | sjeq = new_stmt(cstate, JMP(BPF_JEQ)); |
8718 | 0 | sjeq->s.k = 1; |
8719 | 0 | sjeq->s.jf = b_vid->stmts; |
8720 | 0 | sappend(s, sjeq); |
8721 | |
|
8722 | 0 | s2 = new_stmt(cstate, BPF_LD|BPF_H|BPF_ABS); |
8723 | 0 | s2->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG); |
8724 | 0 | sappend(s, s2); |
8725 | 0 | sjeq->s.jt = s2; |
8726 | | |
8727 | | /* Jump to the test in b_vid. We need to jump one instruction before |
8728 | | * the end of the b_vid block so that we only skip loading the TCI |
8729 | | * from packet data and not the 'and' instruction extracting VID. |
8730 | | */ |
8731 | 0 | cnt = 0; |
8732 | 0 | for (s2 = b_vid->stmts; s2; s2 = s2->next) |
8733 | 0 | cnt++; |
8734 | 0 | s2 = new_stmt(cstate, JMP(BPF_JA)); |
8735 | 0 | s2->s.k = cnt - 1; |
8736 | 0 | sappend(s, s2); |
8737 | | |
8738 | | /* insert our statements at the beginning of b_vid */ |
8739 | 0 | sappend(s, b_vid->stmts); |
8740 | 0 | b_vid->stmts = s; |
8741 | 0 | } |
8742 | | |
8743 | | /* |
8744 | | * Generate check for "vlan" or "vlan <id>" on systems with support for BPF |
8745 | | * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN |
8746 | | * tag can be either in metadata or in packet data; therefore if the |
8747 | | * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link |
8748 | | * header for VLAN tag. As the decision is done at run time, we need |
8749 | | * update variable part of the offsets |
8750 | | */ |
8751 | | static struct block * |
8752 | | gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, |
8753 | | int has_vlan_tag) |
8754 | 0 | { |
8755 | 0 | struct block *b0, *b_tpid, *b_vid = NULL; |
8756 | 0 | struct slist *s; |
8757 | | |
8758 | | /* generate new filter code based on extracting packet |
8759 | | * metadata */ |
8760 | 0 | s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); |
8761 | 0 | s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); |
8762 | |
|
8763 | 0 | b0 = gen_jmp(cstate, BPF_JEQ, 1, s); |
8764 | | |
8765 | | /* |
8766 | | * This is tricky. We need to insert the statements updating variable |
8767 | | * parts of offsets before the traditional TPID and VID tests so |
8768 | | * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but |
8769 | | * we do not want this update to affect those checks. That's why we |
8770 | | * generate both test blocks first and insert the statements updating |
8771 | | * variable parts of both offsets after that. This wouldn't work if |
8772 | | * there already were variable length link header when entering this |
8773 | | * function but gen_vlan_bpf_extensions() isn't called in that case. |
8774 | | */ |
8775 | 0 | b_tpid = gen_vlan_tpid_test(cstate); |
8776 | 0 | if (has_vlan_tag) |
8777 | 0 | b_vid = gen_vlan_vid_test(cstate, vlan_num); |
8778 | |
|
8779 | 0 | gen_vlan_patch_tpid_test(cstate, b_tpid); |
8780 | 0 | gen_or(b0, b_tpid); |
8781 | 0 | b0 = b_tpid; |
8782 | |
|
8783 | 0 | if (has_vlan_tag) { |
8784 | 0 | gen_vlan_patch_vid_test(cstate, b_vid); |
8785 | 0 | gen_and(b0, b_vid); |
8786 | 0 | b0 = b_vid; |
8787 | 0 | } |
8788 | |
|
8789 | 0 | return b0; |
8790 | 0 | } |
8791 | | #endif |
8792 | | |
8793 | | /* |
8794 | | * support IEEE 802.1Q VLAN trunk over ethernet |
8795 | | */ |
8796 | | struct block * |
8797 | | gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag) |
8798 | 517 | { |
8799 | 517 | struct block *b0; |
8800 | | |
8801 | | /* |
8802 | | * Catch errors reported by us and routines below us, and return NULL |
8803 | | * on an error. |
8804 | | */ |
8805 | 517 | if (setjmp(cstate->top_ctx)) |
8806 | 21 | return (NULL); |
8807 | | |
8808 | | /* can't check for VLAN-encapsulated packets inside MPLS */ |
8809 | 496 | if (cstate->label_stack_depth > 0) |
8810 | 1 | bpf_error(cstate, "no VLAN match after MPLS"); |
8811 | | |
8812 | | /* |
8813 | | * Check for a VLAN packet, and then change the offsets to point |
8814 | | * to the type and data fields within the VLAN packet. Just |
8815 | | * increment the offsets, so that we can support a hierarchy, e.g. |
8816 | | * "vlan 100 && vlan 200" to capture VLAN 200 encapsulated within |
8817 | | * VLAN 100. |
8818 | | * |
8819 | | * XXX - this is a bit of a kludge. If we were to split the |
8820 | | * compiler into a parser that parses an expression and |
8821 | | * generates an expression tree, and a code generator that |
8822 | | * takes an expression tree (which could come from our |
8823 | | * parser or from some other parser) and generates BPF code, |
8824 | | * we could perhaps make the offsets parameters of routines |
8825 | | * and, in the handler for an "AND" node, pass to subnodes |
8826 | | * other than the VLAN node the adjusted offsets. |
8827 | | * |
8828 | | * This would mean that "vlan" would, instead of changing the |
8829 | | * behavior of *all* tests after it, change only the behavior |
8830 | | * of tests ANDed with it. That would change the documented |
8831 | | * semantics of "vlan", which might break some expressions. |
8832 | | * However, it would mean that "(vlan and ip) or ip" would check |
8833 | | * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than |
8834 | | * checking only for VLAN-encapsulated IP, so that could still |
8835 | | * be considered worth doing; it wouldn't break expressions |
8836 | | * that are of the form "vlan and ..." or "vlan N and ...", |
8837 | | * which I suspect are the most common expressions involving |
8838 | | * "vlan". "vlan or ..." doesn't necessarily do what the user |
8839 | | * would really want, now, as all the "or ..." tests would |
8840 | | * be done assuming a VLAN, even though the "or" could be viewed |
8841 | | * as meaning "or, if this isn't a VLAN packet...". |
8842 | | */ |
8843 | 495 | switch (cstate->linktype) { |
8844 | | |
8845 | 74 | case DLT_EN10MB: |
8846 | | /* |
8847 | | * Newer version of the Linux kernel pass around |
8848 | | * packets in which the VLAN tag has been removed |
8849 | | * from the packet data and put into metadata. |
8850 | | * |
8851 | | * This requires special treatment. |
8852 | | */ |
8853 | 74 | #if defined(SKF_AD_VLAN_TAG_PRESENT) |
8854 | | /* Verify that this is the outer part of the packet and |
8855 | | * not encapsulated somehow. */ |
8856 | 74 | if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable && |
8857 | 74 | cstate->off_linkhdr.constant_part == |
8858 | 16 | cstate->off_outermostlinkhdr.constant_part) { |
8859 | | /* |
8860 | | * Do we need special VLAN handling? |
8861 | | */ |
8862 | 9 | if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING) |
8863 | 0 | b0 = gen_vlan_bpf_extensions(cstate, vlan_num, |
8864 | 0 | has_vlan_tag); |
8865 | 9 | else |
8866 | 9 | b0 = gen_vlan_no_bpf_extensions(cstate, |
8867 | 9 | vlan_num, has_vlan_tag); |
8868 | 9 | } else |
8869 | 65 | #endif |
8870 | 65 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, |
8871 | 65 | has_vlan_tag); |
8872 | 74 | break; |
8873 | | |
8874 | 69 | case DLT_NETANALYZER: |
8875 | 137 | case DLT_NETANALYZER_TRANSPARENT: |
8876 | 222 | case DLT_IEEE802_11: |
8877 | 280 | case DLT_PRISM_HEADER: |
8878 | 352 | case DLT_IEEE802_11_RADIO_AVS: |
8879 | 441 | case DLT_IEEE802_11_RADIO: |
8880 | | /* |
8881 | | * These are either Ethernet packets with an additional |
8882 | | * metadata header (the NetAnalyzer types), or 802.11 |
8883 | | * packets, possibly with an additional metadata header. |
8884 | | * |
8885 | | * For the first of those, the VLAN tag is in the normal |
8886 | | * place, so the special-case handling above isn't |
8887 | | * necessary. |
8888 | | * |
8889 | | * For the second of those, we don't do the special-case |
8890 | | * handling for now. |
8891 | | */ |
8892 | 441 | b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag); |
8893 | 441 | break; |
8894 | | |
8895 | 1 | default: |
8896 | 1 | bpf_error(cstate, "no VLAN support for %s", |
8897 | 1 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8898 | | /*NOTREACHED*/ |
8899 | 495 | } |
8900 | | |
8901 | 496 | cstate->vlan_stack_depth++; |
8902 | | |
8903 | 496 | return (b0); |
8904 | 495 | } |
8905 | | |
8906 | | /* |
8907 | | * support for MPLS |
8908 | | * |
8909 | | * The label_num_arg dance is to avoid annoying whining by compilers that |
8910 | | * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*? |
8911 | | * It's not *used* after setjmp returns. |
8912 | | */ |
8913 | | static struct block * |
8914 | | gen_mpls_internal(compiler_state_t *cstate, bpf_u_int32 label_num, |
8915 | | int has_label_num) |
8916 | 159 | { |
8917 | 159 | struct block *b0, *b1; |
8918 | | |
8919 | 159 | if (cstate->label_stack_depth > 0) { |
8920 | | /* just match the bottom-of-stack bit clear */ |
8921 | 77 | b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01); |
8922 | 82 | } else { |
8923 | | /* |
8924 | | * We're not in an MPLS stack yet, so check the link-layer |
8925 | | * type against MPLS. |
8926 | | */ |
8927 | 82 | switch (cstate->linktype) { |
8928 | | |
8929 | 3 | case DLT_C_HDLC: /* fall through */ |
8930 | 12 | case DLT_HDLC: |
8931 | 38 | case DLT_EN10MB: |
8932 | 57 | case DLT_NETANALYZER: |
8933 | 63 | case DLT_NETANALYZER_TRANSPARENT: |
8934 | 63 | b0 = gen_linktype(cstate, ETHERTYPE_MPLS); |
8935 | 63 | break; |
8936 | | |
8937 | 17 | case DLT_PPP: |
8938 | 17 | b0 = gen_linktype(cstate, PPP_MPLS_UCAST); |
8939 | 17 | break; |
8940 | | |
8941 | | /* FIXME add other DLT_s ... |
8942 | | * for Frame-Relay/and ATM this may get messy due to SNAP headers |
8943 | | * leave it for now */ |
8944 | | |
8945 | 2 | default: |
8946 | 2 | bpf_error(cstate, "no MPLS support for %s", |
8947 | 2 | pcap_datalink_val_to_description_or_dlt(cstate->linktype)); |
8948 | | /*NOTREACHED*/ |
8949 | 82 | } |
8950 | 82 | } |
8951 | | |
8952 | | /* If a specific MPLS label is requested, check it */ |
8953 | 157 | if (has_label_num) { |
8954 | 87 | assert_maxval(cstate, "MPLS label", label_num, 0xFFFFF); |
8955 | 87 | label_num = label_num << 12; /* label is shifted 12 bits on the wire */ |
8956 | 87 | b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num, |
8957 | 87 | 0xfffff000); /* only compare the first 20 bits */ |
8958 | 87 | gen_and(b0, b1); |
8959 | 87 | b0 = b1; |
8960 | 87 | } |
8961 | | |
8962 | | /* |
8963 | | * Change the offsets to point to the type and data fields within |
8964 | | * the MPLS packet. Just increment the offsets, so that we |
8965 | | * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to |
8966 | | * capture packets with an outer label of 100000 and an inner |
8967 | | * label of 1024. |
8968 | | * |
8969 | | * Increment the MPLS stack depth as well; this indicates that |
8970 | | * we're checking MPLS-encapsulated headers, to make sure higher |
8971 | | * level code generators don't try to match against IP-related |
8972 | | * protocols such as Q_ARP, Q_RARP etc. |
8973 | | * |
8974 | | * XXX - this is a bit of a kludge. See comments in gen_vlan(). |
8975 | | */ |
8976 | 157 | cstate->off_nl_nosnap += 4; |
8977 | 157 | cstate->off_nl += 4; |
8978 | 157 | cstate->label_stack_depth++; |
8979 | 157 | return (b0); |
8980 | 159 | } |
8981 | | |
8982 | | struct block * |
8983 | | gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num, int has_label_num) |
8984 | 159 | { |
8985 | | /* |
8986 | | * Catch errors reported by us and routines below us, and return NULL |
8987 | | * on an error. |
8988 | | */ |
8989 | 159 | if (setjmp(cstate->top_ctx)) |
8990 | 16 | return (NULL); |
8991 | | |
8992 | 143 | return gen_mpls_internal(cstate, label_num, has_label_num); |
8993 | 159 | } |
8994 | | |
8995 | | /* |
8996 | | * Support PPPOE discovery and session. |
8997 | | */ |
8998 | | struct block * |
8999 | | gen_pppoed(compiler_state_t *cstate) |
9000 | 18 | { |
9001 | | /* |
9002 | | * Catch errors reported by us and routines below us, and return NULL |
9003 | | * on an error. |
9004 | | */ |
9005 | 18 | if (setjmp(cstate->top_ctx)) |
9006 | 1 | return (NULL); |
9007 | | |
9008 | | /* check for PPPoE discovery */ |
9009 | 17 | return gen_linktype(cstate, ETHERTYPE_PPPOED); |
9010 | 18 | } |
9011 | | |
9012 | | /* |
9013 | | * RFC 2516 Section 4: |
9014 | | * |
9015 | | * The Ethernet payload for PPPoE is as follows: |
9016 | | * |
9017 | | * 1 2 3 |
9018 | | * 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 |
9019 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
9020 | | * | VER | TYPE | CODE | SESSION_ID | |
9021 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
9022 | | * | LENGTH | payload ~ |
9023 | | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
9024 | | */ |
9025 | | struct block * |
9026 | | gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num) |
9027 | 196 | { |
9028 | 196 | struct block *b0, *b1; |
9029 | | |
9030 | | /* |
9031 | | * Catch errors reported by us and routines below us, and return NULL |
9032 | | * on an error. |
9033 | | */ |
9034 | 196 | if (setjmp(cstate->top_ctx)) |
9035 | 14 | return (NULL); |
9036 | | |
9037 | | /* |
9038 | | * Test against the PPPoE session link-layer type. |
9039 | | */ |
9040 | 182 | b0 = gen_linktype(cstate, ETHERTYPE_PPPOES); |
9041 | | |
9042 | | /* If a specific session is requested, check PPPoE session id */ |
9043 | 182 | if (has_sess_num) { |
9044 | 37 | assert_maxval(cstate, "PPPoE session number", sess_num, UINT16_MAX); |
9045 | 37 | b1 = gen_cmp(cstate, OR_LINKPL, 2, BPF_H, sess_num); |
9046 | 37 | gen_and(b0, b1); |
9047 | 37 | b0 = b1; |
9048 | 37 | } |
9049 | | |
9050 | | /* |
9051 | | * Change the offsets to point to the type and data fields within |
9052 | | * the PPP packet, and note that this is PPPoE rather than |
9053 | | * raw PPP. |
9054 | | * |
9055 | | * XXX - this is a bit of a kludge. See the comments in |
9056 | | * gen_vlan(). |
9057 | | * |
9058 | | * The "network-layer" protocol is PPPoE, which has a 6-byte |
9059 | | * PPPoE header, followed by a PPP packet. |
9060 | | * |
9061 | | * There is no HDLC encapsulation for the PPP packet (it's |
9062 | | * encapsulated in PPPoES instead), so the link-layer type |
9063 | | * starts at the first byte of the PPP packet. For PPPoE, |
9064 | | * that offset is relative to the beginning of the total |
9065 | | * link-layer payload, including any 802.2 LLC header, so |
9066 | | * it's 6 bytes past cstate->off_nl. |
9067 | | */ |
9068 | 182 | PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable, |
9069 | 182 | cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */ |
9070 | 182 | cstate->off_linkpl.reg); |
9071 | | |
9072 | 182 | cstate->off_linktype = cstate->off_linkhdr; |
9073 | 182 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2; |
9074 | | |
9075 | 182 | cstate->off_nl = 0; |
9076 | 182 | cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ |
9077 | | |
9078 | 182 | return b0; |
9079 | 196 | } |
9080 | | |
9081 | | /* Check that this is Geneve and the VNI is correct if |
9082 | | * specified. Parameterized to handle both IPv4 and IPv6. */ |
9083 | | static struct block * |
9084 | | gen_geneve_check(compiler_state_t *cstate, |
9085 | | struct block *(*gen_portfn)(compiler_state_t *, uint16_t, int, int), |
9086 | | enum e_offrel offrel, bpf_u_int32 vni, int has_vni) |
9087 | 0 | { |
9088 | 0 | struct block *b0, *b1; |
9089 | |
|
9090 | 0 | b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST); |
9091 | | |
9092 | | /* Check that we are operating on version 0. Otherwise, we |
9093 | | * can't decode the rest of the fields. The version is 2 bits |
9094 | | * in the first byte of the Geneve header. */ |
9095 | 0 | b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0); |
9096 | 0 | gen_and(b0, b1); |
9097 | 0 | b0 = b1; |
9098 | |
|
9099 | 0 | if (has_vni) { |
9100 | 0 | assert_maxval(cstate, "Geneve VNI", vni, 0xffffff); |
9101 | 0 | vni <<= 8; /* VNI is in the upper 3 bytes */ |
9102 | 0 | b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00); |
9103 | 0 | gen_and(b0, b1); |
9104 | 0 | b0 = b1; |
9105 | 0 | } |
9106 | |
|
9107 | 0 | return b0; |
9108 | 0 | } |
9109 | | |
9110 | | /* The IPv4 and IPv6 Geneve checks need to do two things: |
9111 | | * - Verify that this actually is Geneve with the right VNI. |
9112 | | * - Place the IP header length (plus variable link prefix if |
9113 | | * needed) into register A to be used later to compute |
9114 | | * the inner packet offsets. */ |
9115 | | static struct block * |
9116 | | gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9117 | 0 | { |
9118 | 0 | struct block *b0, *b1; |
9119 | 0 | struct slist *s, *s1; |
9120 | |
|
9121 | 0 | b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); |
9122 | | |
9123 | | /* Load the IP header length into A. */ |
9124 | 0 | s = gen_loadx_iphdrlen(cstate); |
9125 | |
|
9126 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9127 | 0 | sappend(s, s1); |
9128 | | |
9129 | | /* Forcibly append these statements to the true condition |
9130 | | * of the protocol check by creating a new block that is |
9131 | | * always true and ANDing them. */ |
9132 | 0 | b1 = gen_jmp(cstate, BPF_JMP|BPF_JEQ|BPF_X, 0, s); |
9133 | |
|
9134 | 0 | gen_and(b0, b1); |
9135 | |
|
9136 | 0 | return b1; |
9137 | 0 | } |
9138 | | |
9139 | | static struct block * |
9140 | | gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9141 | 0 | { |
9142 | 0 | struct block *b0, *b1; |
9143 | 0 | struct slist *s, *s1; |
9144 | |
|
9145 | 0 | b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); |
9146 | | |
9147 | | /* Load the IP header length. We need to account for a |
9148 | | * variable length link prefix if there is one. */ |
9149 | 0 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
9150 | 0 | if (s) { |
9151 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IMM); |
9152 | 0 | s1->s.k = 40; |
9153 | 0 | sappend(s, s1); |
9154 | |
|
9155 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9156 | 0 | s1->s.k = 0; |
9157 | 0 | sappend(s, s1); |
9158 | 0 | } else { |
9159 | 0 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
9160 | 0 | s->s.k = 40; |
9161 | 0 | } |
9162 | | |
9163 | | /* Forcibly append these statements to the true condition |
9164 | | * of the protocol check by creating a new block that is |
9165 | | * always true and ANDing them. */ |
9166 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9167 | 0 | sappend(s, s1); |
9168 | |
|
9169 | 0 | b1 = gen_jmp(cstate, BPF_JMP|BPF_JEQ|BPF_X, 0, s); |
9170 | |
|
9171 | 0 | gen_and(b0, b1); |
9172 | |
|
9173 | 0 | return b1; |
9174 | 0 | } |
9175 | | |
9176 | | /* We need to store three values based on the Geneve header:: |
9177 | | * - The offset of the linktype. |
9178 | | * - The offset of the end of the Geneve header. |
9179 | | * - The offset of the end of the encapsulated MAC header. */ |
9180 | | static struct slist * |
9181 | | gen_geneve_offsets(compiler_state_t *cstate) |
9182 | 0 | { |
9183 | 0 | struct slist *s, *s1, *s_proto; |
9184 | | |
9185 | | /* First we need to calculate the offset of the Geneve header |
9186 | | * itself. This is composed of the IP header previously calculated |
9187 | | * (include any variable link prefix) and stored in A plus the |
9188 | | * fixed sized headers (fixed link prefix, MAC length, and UDP |
9189 | | * header). */ |
9190 | 0 | s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9191 | 0 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; |
9192 | | |
9193 | | /* Stash this in X since we'll need it later. */ |
9194 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9195 | 0 | sappend(s, s1); |
9196 | | |
9197 | | /* The EtherType in Geneve is 2 bytes in. Calculate this and |
9198 | | * store it. */ |
9199 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9200 | 0 | s1->s.k = 2; |
9201 | 0 | sappend(s, s1); |
9202 | |
|
9203 | 0 | cstate->off_linktype.reg = alloc_reg(cstate); |
9204 | 0 | cstate->off_linktype.is_variable = 1; |
9205 | 0 | cstate->off_linktype.constant_part = 0; |
9206 | |
|
9207 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9208 | 0 | s1->s.k = cstate->off_linktype.reg; |
9209 | 0 | sappend(s, s1); |
9210 | | |
9211 | | /* Load the Geneve option length and mask and shift to get the |
9212 | | * number of bytes. It is stored in the first byte of the Geneve |
9213 | | * header. */ |
9214 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); |
9215 | 0 | s1->s.k = 0; |
9216 | 0 | sappend(s, s1); |
9217 | |
|
9218 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); |
9219 | 0 | s1->s.k = 0x3f; |
9220 | 0 | sappend(s, s1); |
9221 | |
|
9222 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); |
9223 | 0 | s1->s.k = 4; |
9224 | 0 | sappend(s, s1); |
9225 | | |
9226 | | /* Add in the rest of the Geneve base header. */ |
9227 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9228 | 0 | s1->s.k = 8; |
9229 | 0 | sappend(s, s1); |
9230 | | |
9231 | | /* Add the Geneve header length to its offset and store. */ |
9232 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9233 | 0 | s1->s.k = 0; |
9234 | 0 | sappend(s, s1); |
9235 | | |
9236 | | /* Set the encapsulated type as Ethernet. Even though we may |
9237 | | * not actually have Ethernet inside there are two reasons this |
9238 | | * is useful: |
9239 | | * - The linktype field is always in EtherType format regardless |
9240 | | * of whether it is in Geneve or an inner Ethernet frame. |
9241 | | * - The only link layer that we have specific support for is |
9242 | | * Ethernet. We will confirm that the packet actually is |
9243 | | * Ethernet at runtime before executing these checks. */ |
9244 | 0 | PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); |
9245 | |
|
9246 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9247 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9248 | 0 | sappend(s, s1); |
9249 | | |
9250 | | /* Calculate whether we have an Ethernet header or just raw IP/ |
9251 | | * MPLS/etc. If we have Ethernet, advance the end of the MAC offset |
9252 | | * and linktype by 14 bytes so that the network header can be found |
9253 | | * seamlessly. Otherwise, keep what we've calculated already. */ |
9254 | | |
9255 | | /* We have a bare jmp so we can't use the optimizer. */ |
9256 | 0 | cstate->no_optimize = 1; |
9257 | | |
9258 | | /* Load the EtherType in the Geneve header, 2 bytes in. */ |
9259 | 0 | s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H); |
9260 | 0 | s1->s.k = 2; |
9261 | 0 | sappend(s, s1); |
9262 | | |
9263 | | /* Load X with the end of the Geneve header. */ |
9264 | 0 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9265 | 0 | s1->s.k = cstate->off_linkhdr.reg; |
9266 | 0 | sappend(s, s1); |
9267 | | |
9268 | | /* Check if the EtherType is Transparent Ethernet Bridging. At the |
9269 | | * end of this check, we should have the total length in X. In |
9270 | | * the non-Ethernet case, it's already there. */ |
9271 | 0 | s_proto = new_stmt(cstate, JMP(BPF_JEQ)); |
9272 | 0 | s_proto->s.k = ETHERTYPE_TEB; |
9273 | 0 | sappend(s, s_proto); |
9274 | |
|
9275 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9276 | 0 | sappend(s, s1); |
9277 | 0 | s_proto->s.jt = s1; |
9278 | | |
9279 | | /* Since this is Ethernet, use the EtherType of the payload |
9280 | | * directly as the linktype. Overwrite what we already have. */ |
9281 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9282 | 0 | s1->s.k = 12; |
9283 | 0 | sappend(s, s1); |
9284 | |
|
9285 | 0 | s1 = new_stmt(cstate, BPF_ST); |
9286 | 0 | s1->s.k = cstate->off_linktype.reg; |
9287 | 0 | sappend(s, s1); |
9288 | | |
9289 | | /* Advance two bytes further to get the end of the Ethernet |
9290 | | * header. */ |
9291 | 0 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9292 | 0 | s1->s.k = 2; |
9293 | 0 | sappend(s, s1); |
9294 | | |
9295 | | /* Move the result to X. */ |
9296 | 0 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9297 | 0 | sappend(s, s1); |
9298 | | |
9299 | | /* Store the final result of our linkpl calculation. */ |
9300 | 0 | cstate->off_linkpl.reg = alloc_reg(cstate); |
9301 | 0 | cstate->off_linkpl.is_variable = 1; |
9302 | 0 | cstate->off_linkpl.constant_part = 0; |
9303 | |
|
9304 | 0 | s1 = new_stmt(cstate, BPF_STX); |
9305 | 0 | s1->s.k = cstate->off_linkpl.reg; |
9306 | 0 | sappend(s, s1); |
9307 | 0 | s_proto->s.jf = s1; |
9308 | |
|
9309 | 0 | cstate->off_nl = 0; |
9310 | |
|
9311 | 0 | return s; |
9312 | 0 | } |
9313 | | |
9314 | | /* Check to see if this is a Geneve packet. */ |
9315 | | struct block * |
9316 | | gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9317 | 0 | { |
9318 | 0 | struct block *b0, *b1; |
9319 | 0 | struct slist *s; |
9320 | | |
9321 | | /* |
9322 | | * Catch errors reported by us and routines below us, and return NULL |
9323 | | * on an error. |
9324 | | */ |
9325 | 0 | if (setjmp(cstate->top_ctx)) |
9326 | 0 | return (NULL); |
9327 | | |
9328 | 0 | b0 = gen_geneve4(cstate, vni, has_vni); |
9329 | 0 | b1 = gen_geneve6(cstate, vni, has_vni); |
9330 | |
|
9331 | 0 | gen_or(b0, b1); |
9332 | 0 | b0 = b1; |
9333 | | |
9334 | | /* Later filters should act on the payload of the Geneve frame, |
9335 | | * update all of the header pointers. Attach this code so that |
9336 | | * it gets executed in the event that the Geneve filter matches. */ |
9337 | 0 | s = gen_geneve_offsets(cstate); |
9338 | |
|
9339 | 0 | b1 = gen_true(cstate); |
9340 | 0 | sappend(s, b1->stmts); |
9341 | 0 | b1->stmts = s; |
9342 | |
|
9343 | 0 | gen_and(b0, b1); |
9344 | |
|
9345 | 0 | cstate->is_encap = 1; |
9346 | |
|
9347 | 0 | return b1; |
9348 | 0 | } |
9349 | | |
9350 | | /* Check that this is VXLAN and the VNI is correct if |
9351 | | * specified. Parameterized to handle both IPv4 and IPv6. */ |
9352 | | static struct block * |
9353 | | gen_vxlan_check(compiler_state_t *cstate, |
9354 | | struct block *(*gen_portfn)(compiler_state_t *, uint16_t, int, int), |
9355 | | enum e_offrel offrel, bpf_u_int32 vni, int has_vni) |
9356 | 1.38k | { |
9357 | 1.38k | struct block *b0, *b1; |
9358 | | |
9359 | 1.38k | b0 = gen_portfn(cstate, VXLAN_PORT, IPPROTO_UDP, Q_DST); |
9360 | | |
9361 | | /* Check that the VXLAN header has the flag bits set |
9362 | | * correctly. */ |
9363 | 1.38k | b1 = gen_cmp(cstate, offrel, 8, BPF_B, 0x08); |
9364 | 1.38k | gen_and(b0, b1); |
9365 | 1.38k | b0 = b1; |
9366 | | |
9367 | 1.38k | if (has_vni) { |
9368 | 203 | assert_maxval(cstate, "VXLAN VNI", vni, 0xffffff); |
9369 | 203 | vni <<= 8; /* VNI is in the upper 3 bytes */ |
9370 | 203 | b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00); |
9371 | 203 | gen_and(b0, b1); |
9372 | 203 | b0 = b1; |
9373 | 203 | } |
9374 | | |
9375 | 1.38k | return b0; |
9376 | 1.38k | } |
9377 | | |
9378 | | /* The IPv4 and IPv6 VXLAN checks need to do two things: |
9379 | | * - Verify that this actually is VXLAN with the right VNI. |
9380 | | * - Place the IP header length (plus variable link prefix if |
9381 | | * needed) into register A to be used later to compute |
9382 | | * the inner packet offsets. */ |
9383 | | static struct block * |
9384 | | gen_vxlan4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9385 | 698 | { |
9386 | 698 | struct block *b0, *b1; |
9387 | 698 | struct slist *s, *s1; |
9388 | | |
9389 | 698 | b0 = gen_vxlan_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); |
9390 | | |
9391 | | /* Load the IP header length into A. */ |
9392 | 698 | s = gen_loadx_iphdrlen(cstate); |
9393 | | |
9394 | 698 | s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); |
9395 | 698 | sappend(s, s1); |
9396 | | |
9397 | | /* Forcibly append these statements to the true condition |
9398 | | * of the protocol check by creating a new block that is |
9399 | | * always true and ANDing them. */ |
9400 | 698 | b1 = gen_jmp(cstate, BPF_JMP|BPF_JEQ|BPF_X, 0, s); |
9401 | | |
9402 | 698 | gen_and(b0, b1); |
9403 | | |
9404 | 698 | return b1; |
9405 | 698 | } |
9406 | | |
9407 | | static struct block * |
9408 | | gen_vxlan6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9409 | 688 | { |
9410 | 688 | struct block *b0, *b1; |
9411 | 688 | struct slist *s, *s1; |
9412 | | |
9413 | 688 | b0 = gen_vxlan_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); |
9414 | | |
9415 | | /* Load the IP header length. We need to account for a |
9416 | | * variable length link prefix if there is one. */ |
9417 | 688 | s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); |
9418 | 688 | if (s) { |
9419 | 452 | s1 = new_stmt(cstate, BPF_LD|BPF_IMM); |
9420 | 452 | s1->s.k = 40; |
9421 | 452 | sappend(s, s1); |
9422 | | |
9423 | 452 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); |
9424 | 452 | s1->s.k = 0; |
9425 | 452 | sappend(s, s1); |
9426 | 452 | } else { |
9427 | 236 | s = new_stmt(cstate, BPF_LD|BPF_IMM); |
9428 | 236 | s->s.k = 40; |
9429 | 236 | } |
9430 | | |
9431 | | /* Forcibly append these statements to the true condition |
9432 | | * of the protocol check by creating a new block that is |
9433 | | * always true and ANDing them. */ |
9434 | 688 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9435 | 688 | sappend(s, s1); |
9436 | | |
9437 | 688 | b1 = gen_jmp(cstate, BPF_JMP|BPF_JEQ|BPF_X, 0, s); |
9438 | | |
9439 | 688 | gen_and(b0, b1); |
9440 | | |
9441 | 688 | return b1; |
9442 | 688 | } |
9443 | | |
9444 | | /* We need to store three values based on the VXLAN header: |
9445 | | * - The offset of the linktype. |
9446 | | * - The offset of the end of the VXLAN header. |
9447 | | * - The offset of the end of the encapsulated MAC header. */ |
9448 | | static struct slist * |
9449 | | gen_vxlan_offsets(compiler_state_t *cstate) |
9450 | 688 | { |
9451 | 688 | struct slist *s, *s1; |
9452 | | |
9453 | | /* Calculate the offset of the VXLAN header itself. This |
9454 | | * includes the IP header computed previously (including any |
9455 | | * variable link prefix) and stored in A plus the fixed size |
9456 | | * headers (fixed link prefix, MAC length, UDP header). */ |
9457 | 688 | s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9458 | 688 | s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; |
9459 | | |
9460 | | /* Add the VXLAN header length to its offset and store */ |
9461 | 688 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9462 | 688 | s1->s.k = 8; |
9463 | 688 | sappend(s, s1); |
9464 | | |
9465 | | /* Push the link header. VXLAN packets always contain Ethernet |
9466 | | * frames. */ |
9467 | 688 | PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); |
9468 | | |
9469 | 688 | s1 = new_stmt(cstate, BPF_ST); |
9470 | 688 | s1->s.k = cstate->off_linkhdr.reg; |
9471 | 688 | sappend(s, s1); |
9472 | | |
9473 | | /* As the payload is an Ethernet packet, we can use the |
9474 | | * EtherType of the payload directly as the linktype. */ |
9475 | 688 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9476 | 688 | s1->s.k = 12; |
9477 | 688 | sappend(s, s1); |
9478 | | |
9479 | 688 | cstate->off_linktype.reg = alloc_reg(cstate); |
9480 | 688 | cstate->off_linktype.is_variable = 1; |
9481 | 688 | cstate->off_linktype.constant_part = 0; |
9482 | | |
9483 | 688 | s1 = new_stmt(cstate, BPF_ST); |
9484 | 688 | s1->s.k = cstate->off_linktype.reg; |
9485 | 688 | sappend(s, s1); |
9486 | | |
9487 | | /* Two bytes further is the end of the Ethernet header and the |
9488 | | * start of the payload. */ |
9489 | 688 | s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); |
9490 | 688 | s1->s.k = 2; |
9491 | 688 | sappend(s, s1); |
9492 | | |
9493 | | /* Move the result to X. */ |
9494 | 688 | s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); |
9495 | 688 | sappend(s, s1); |
9496 | | |
9497 | | /* Store the final result of our linkpl calculation. */ |
9498 | 688 | cstate->off_linkpl.reg = alloc_reg(cstate); |
9499 | 688 | cstate->off_linkpl.is_variable = 1; |
9500 | 688 | cstate->off_linkpl.constant_part = 0; |
9501 | | |
9502 | 688 | s1 = new_stmt(cstate, BPF_STX); |
9503 | 688 | s1->s.k = cstate->off_linkpl.reg; |
9504 | 688 | sappend(s, s1); |
9505 | | |
9506 | 688 | cstate->off_nl = 0; |
9507 | | |
9508 | 688 | return s; |
9509 | 688 | } |
9510 | | |
9511 | | /* Check to see if this is a VXLAN packet. */ |
9512 | | struct block * |
9513 | | gen_vxlan(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) |
9514 | 698 | { |
9515 | 698 | struct block *b0, *b1; |
9516 | 698 | struct slist *s; |
9517 | | |
9518 | | /* |
9519 | | * Catch errors reported by us and routines below us, and return NULL |
9520 | | * on an error. |
9521 | | */ |
9522 | 698 | if (setjmp(cstate->top_ctx)) |
9523 | 16 | return (NULL); |
9524 | | |
9525 | 682 | b0 = gen_vxlan4(cstate, vni, has_vni); |
9526 | 682 | b1 = gen_vxlan6(cstate, vni, has_vni); |
9527 | | |
9528 | 682 | gen_or(b0, b1); |
9529 | 682 | b0 = b1; |
9530 | | |
9531 | | /* Later filters should act on the payload of the VXLAN frame, |
9532 | | * update all of the header pointers. Attach this code so that |
9533 | | * it gets executed in the event that the VXLAN filter matches. */ |
9534 | 682 | s = gen_vxlan_offsets(cstate); |
9535 | | |
9536 | 682 | b1 = gen_true(cstate); |
9537 | 682 | sappend(s, b1->stmts); |
9538 | 682 | b1->stmts = s; |
9539 | | |
9540 | 682 | gen_and(b0, b1); |
9541 | | |
9542 | 682 | cstate->is_encap = 1; |
9543 | | |
9544 | 682 | return b1; |
9545 | 698 | } |
9546 | | |
9547 | | /* Check that the encapsulated frame has a link layer header |
9548 | | * for Ethernet filters. */ |
9549 | | static struct block * |
9550 | | gen_encap_ll_check(compiler_state_t *cstate) |
9551 | 44 | { |
9552 | 44 | struct block *b0; |
9553 | 44 | struct slist *s, *s1; |
9554 | | |
9555 | | /* The easiest way to see if there is a link layer present |
9556 | | * is to check if the link layer header and payload are not |
9557 | | * the same. */ |
9558 | | |
9559 | | /* Geneve always generates pure variable offsets so we can |
9560 | | * compare only the registers. */ |
9561 | 44 | s = new_stmt(cstate, BPF_LD|BPF_MEM); |
9562 | 44 | s->s.k = cstate->off_linkhdr.reg; |
9563 | | |
9564 | 44 | s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); |
9565 | 44 | s1->s.k = cstate->off_linkpl.reg; |
9566 | 44 | sappend(s, s1); |
9567 | | |
9568 | 44 | b0 = gen_jmp(cstate, BPF_JMP|BPF_JEQ|BPF_X, 0, s); |
9569 | 44 | gen_not(b0); |
9570 | | |
9571 | 44 | return b0; |
9572 | 44 | } |
9573 | | |
9574 | | static struct block * |
9575 | | gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield, |
9576 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9577 | 508 | { |
9578 | 508 | assert_atm(cstate, atmkw(atmfield)); |
9579 | | |
9580 | 508 | switch (atmfield) { |
9581 | | |
9582 | 259 | case A_VPI: |
9583 | 259 | assert_maxval(cstate, "VPI", jvalue, UINT8_MAX); |
9584 | 259 | return gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, |
9585 | 259 | 0xffffffffU, jtype, reverse, jvalue); |
9586 | | |
9587 | 240 | case A_VCI: |
9588 | 240 | assert_maxval(cstate, "VCI", jvalue, UINT16_MAX); |
9589 | 240 | return gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, |
9590 | 240 | 0xffffffffU, jtype, reverse, jvalue); |
9591 | | |
9592 | 0 | default: |
9593 | 0 | abort(); |
9594 | 508 | } |
9595 | 508 | } |
9596 | | |
9597 | | static struct block * |
9598 | | gen_atm_vpi(compiler_state_t *cstate, const uint8_t v) |
9599 | 92 | { |
9600 | 92 | return gen_atmfield_code_internal(cstate, A_VPI, v, BPF_JEQ, 0); |
9601 | 92 | } |
9602 | | |
9603 | | static struct block * |
9604 | | gen_atm_vci(compiler_state_t *cstate, const uint16_t v) |
9605 | 117 | { |
9606 | 117 | return gen_atmfield_code_internal(cstate, A_VCI, v, BPF_JEQ, 0); |
9607 | 117 | } |
9608 | | |
9609 | | static struct block * |
9610 | | gen_atm_prototype(compiler_state_t *cstate, const uint8_t v) |
9611 | 854 | { |
9612 | 854 | return gen_mcmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, v, 0x0fU); |
9613 | 854 | } |
9614 | | |
9615 | | static struct block * |
9616 | | gen_atmtype_llc(compiler_state_t *cstate) |
9617 | 72 | { |
9618 | 72 | struct block *b0; |
9619 | | |
9620 | 72 | b0 = gen_atm_prototype(cstate, PT_LLC); |
9621 | 72 | cstate->linktype = cstate->prevlinktype; |
9622 | 72 | return b0; |
9623 | 72 | } |
9624 | | |
9625 | | struct block * |
9626 | | gen_atmfield_code(compiler_state_t *cstate, int atmfield, |
9627 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9628 | 299 | { |
9629 | | /* |
9630 | | * Catch errors reported by us and routines below us, and return NULL |
9631 | | * on an error. |
9632 | | */ |
9633 | 299 | if (setjmp(cstate->top_ctx)) |
9634 | 38 | return (NULL); |
9635 | | |
9636 | 261 | return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype, |
9637 | 261 | reverse); |
9638 | 299 | } |
9639 | | |
9640 | | struct block * |
9641 | | gen_atmtype_abbrev(compiler_state_t *cstate, int type) |
9642 | 138 | { |
9643 | 138 | struct block *b0, *b1; |
9644 | | |
9645 | | /* |
9646 | | * Catch errors reported by us and routines below us, and return NULL |
9647 | | * on an error. |
9648 | | */ |
9649 | 138 | if (setjmp(cstate->top_ctx)) |
9650 | 10 | return (NULL); |
9651 | | |
9652 | 128 | assert_atm(cstate, atmkw(type)); |
9653 | | |
9654 | 128 | switch (type) { |
9655 | | |
9656 | 9 | case A_METAC: |
9657 | | /* Get all packets in Meta signalling Circuit */ |
9658 | 9 | b0 = gen_atm_vpi(cstate, 0); |
9659 | 9 | b1 = gen_atm_vci(cstate, 1); |
9660 | 9 | gen_and(b0, b1); |
9661 | 9 | return b1; |
9662 | | |
9663 | 9 | case A_BCC: |
9664 | | /* Get all packets in Broadcast Circuit*/ |
9665 | 9 | b0 = gen_atm_vpi(cstate, 0); |
9666 | 9 | b1 = gen_atm_vci(cstate, 2); |
9667 | 9 | gen_and(b0, b1); |
9668 | 9 | return b1; |
9669 | | |
9670 | 8 | case A_OAMF4SC: |
9671 | | /* Get all cells in Segment OAM F4 circuit*/ |
9672 | 8 | b0 = gen_atm_vpi(cstate, 0); |
9673 | 8 | b1 = gen_atm_vci(cstate, 3); |
9674 | 8 | gen_and(b0, b1); |
9675 | 8 | return b1; |
9676 | | |
9677 | 6 | case A_OAMF4EC: |
9678 | | /* Get all cells in End-to-End OAM F4 Circuit*/ |
9679 | 6 | b0 = gen_atm_vpi(cstate, 0); |
9680 | 6 | b1 = gen_atm_vci(cstate, 4); |
9681 | 6 | gen_and(b0, b1); |
9682 | 6 | return b1; |
9683 | | |
9684 | 34 | case A_SC: |
9685 | | /* Get all packets in connection Signalling Circuit */ |
9686 | 34 | b0 = gen_atm_vpi(cstate, 0); |
9687 | 34 | b1 = gen_atm_vci(cstate, 5); |
9688 | 34 | gen_and(b0, b1); |
9689 | 34 | return b1; |
9690 | | |
9691 | 1 | case A_ILMIC: |
9692 | | /* Get all packets in ILMI Circuit */ |
9693 | 1 | b0 = gen_atm_vpi(cstate, 0); |
9694 | 1 | b1 = gen_atm_vci(cstate, 16); |
9695 | 1 | gen_and(b0, b1); |
9696 | 1 | return b1; |
9697 | | |
9698 | 61 | case A_LANE: |
9699 | | /* Get all LANE packets */ |
9700 | 61 | b1 = gen_atm_prototype(cstate, PT_LANE); |
9701 | | |
9702 | | /* |
9703 | | * Arrange that all subsequent tests assume LANE |
9704 | | * rather than LLC-encapsulated packets, and set |
9705 | | * the offsets appropriately for LANE-encapsulated |
9706 | | * Ethernet. |
9707 | | * |
9708 | | * We assume LANE means Ethernet, not Token Ring. |
9709 | | */ |
9710 | 61 | PUSH_LINKHDR(cstate, DLT_EN10MB, 0, |
9711 | 61 | cstate->off_payload + 2, /* Ethernet header */ |
9712 | 61 | -1); |
9713 | 61 | cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; |
9714 | 61 | cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */ |
9715 | 61 | cstate->off_nl = 0; /* Ethernet II */ |
9716 | 61 | cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ |
9717 | 61 | return b1; |
9718 | | |
9719 | 0 | default: |
9720 | 0 | abort(); |
9721 | 128 | } |
9722 | 128 | } |
9723 | | |
9724 | | /* |
9725 | | * Filtering for MTP2 messages based on li value |
9726 | | * FISU, length is null |
9727 | | * LSSU, length is 1 or 2 |
9728 | | * MSU, length is 3 or more |
9729 | | * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits |
9730 | | */ |
9731 | | struct block * |
9732 | | gen_mtp2type_abbrev(compiler_state_t *cstate, int type) |
9733 | 242 | { |
9734 | 242 | struct block *b0, *b1; |
9735 | | |
9736 | | /* |
9737 | | * Catch errors reported by us and routines below us, and return NULL |
9738 | | * on an error. |
9739 | | */ |
9740 | 242 | if (setjmp(cstate->top_ctx)) |
9741 | 2 | return (NULL); |
9742 | | |
9743 | 240 | assert_ss7(cstate, ss7kw(type)); |
9744 | | |
9745 | 240 | switch (type) { |
9746 | | |
9747 | 24 | case M_FISU: |
9748 | 24 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9749 | 24 | 0x3fU, BPF_JEQ, 0, 0U); |
9750 | | |
9751 | 130 | case M_LSSU: |
9752 | 130 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9753 | 130 | 0x3fU, BPF_JGT, 1, 2U); |
9754 | 130 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9755 | 130 | 0x3fU, BPF_JGT, 0, 0U); |
9756 | 130 | gen_and(b1, b0); |
9757 | 130 | return b0; |
9758 | | |
9759 | 43 | case M_MSU: |
9760 | 43 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, |
9761 | 43 | 0x3fU, BPF_JGT, 0, 2U); |
9762 | | |
9763 | 18 | case MH_FISU: |
9764 | 18 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9765 | 18 | 0xff80U, BPF_JEQ, 0, 0U); |
9766 | | |
9767 | 14 | case MH_LSSU: |
9768 | 14 | b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9769 | 14 | 0xff80U, BPF_JGT, 1, 0x0100U); |
9770 | 14 | b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9771 | 14 | 0xff80U, BPF_JGT, 0, 0U); |
9772 | 14 | gen_and(b1, b0); |
9773 | 14 | return b0; |
9774 | | |
9775 | 11 | case MH_MSU: |
9776 | 11 | return gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, |
9777 | 11 | 0xff80U, BPF_JGT, 0, 0x0100U); |
9778 | | |
9779 | 0 | default: |
9780 | 0 | abort(); |
9781 | 240 | } |
9782 | 240 | } |
9783 | | |
9784 | | /* |
9785 | | * These maximum valid values are all-ones, so they double as the bitmasks |
9786 | | * before any bitwise shifting. |
9787 | | */ |
9788 | 224 | #define MTP2_SIO_MAXVAL UINT8_MAX |
9789 | 295 | #define MTP3_PC_MAXVAL 0x3fffU |
9790 | 138 | #define MTP3_SLS_MAXVAL 0xfU |
9791 | | |
9792 | | static struct block * |
9793 | | gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field, |
9794 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9795 | 592 | { |
9796 | 592 | u_int newoff_sio; |
9797 | 592 | u_int newoff_opc; |
9798 | 592 | u_int newoff_dpc; |
9799 | 592 | u_int newoff_sls; |
9800 | | |
9801 | 592 | newoff_sio = cstate->off_sio; |
9802 | 592 | newoff_opc = cstate->off_opc; |
9803 | 592 | newoff_dpc = cstate->off_dpc; |
9804 | 592 | newoff_sls = cstate->off_sls; |
9805 | | |
9806 | 592 | assert_ss7(cstate, ss7kw(mtp3field)); |
9807 | | |
9808 | 592 | switch (mtp3field) { |
9809 | | |
9810 | | /* |
9811 | | * See UTU-T Rec. Q.703, Section 2.2, Figure 3/Q.703. |
9812 | | * |
9813 | | * SIO is the simplest field: the size is one byte and the offset is a |
9814 | | * multiple of bytes, so the only detail to get right is the value of |
9815 | | * the [right-to-left] field offset. |
9816 | | */ |
9817 | 162 | case MH_SIO: |
9818 | 162 | newoff_sio += 3; /* offset for MTP2_HSL */ |
9819 | | /* FALLTHROUGH */ |
9820 | | |
9821 | 224 | case M_SIO: |
9822 | 224 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP2_SIO_MAXVAL); |
9823 | | // Here the bitmask means "do not apply a bitmask". |
9824 | 224 | return gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, UINT32_MAX, |
9825 | 224 | jtype, reverse, jvalue); |
9826 | | |
9827 | | /* |
9828 | | * See UTU-T Rec. Q.704, Section 2.2, Figure 3/Q.704. |
9829 | | * |
9830 | | * SLS, OPC and DPC are more complicated: none of these is sized in a |
9831 | | * multiple of 8 bits, MTP3 encoding is little-endian and MTP packet |
9832 | | * diagrams are meant to be read right-to-left. This means in the |
9833 | | * diagrams within individual fields and concatenations thereof |
9834 | | * bitwise shifts and masks can be noted in the common left-to-right |
9835 | | * manner until each final value is ready to be byte-swapped and |
9836 | | * handed to gen_ncmp(). See also gen_dnhostop(), which solves a |
9837 | | * similar problem in a similar way. |
9838 | | * |
9839 | | * Offsets of fields within the packet header always have the |
9840 | | * right-to-left meaning. Note that in DLT_MTP2 and possibly other |
9841 | | * DLTs the offset does not include the F (Flag) field at the |
9842 | | * beginning of each message. |
9843 | | * |
9844 | | * For example, if the 8-bit SIO field has a 3 byte [RTL] offset, the |
9845 | | * 32-bit standard routing header has a 4 byte [RTL] offset and could |
9846 | | * be tested entirely using a single BPF_W comparison. In this case |
9847 | | * the 14-bit DPC field [LTR] bitmask would be 0x3FFF, the 14-bit OPC |
9848 | | * field [LTR] bitmask would be (0x3FFF << 14) and the 4-bit SLS field |
9849 | | * [LTR] bitmask would be (0xF << 28), all of which conveniently |
9850 | | * correlates with the [RTL] packet diagram until the byte-swapping is |
9851 | | * done before use. |
9852 | | * |
9853 | | * The code below uses this approach for OPC, which spans 3 bytes. |
9854 | | * DPC and SLS use shorter loads, SLS also uses a different offset. |
9855 | | */ |
9856 | 67 | case MH_OPC: |
9857 | 67 | newoff_opc += 3; |
9858 | | |
9859 | | /* FALLTHROUGH */ |
9860 | 146 | case M_OPC: |
9861 | 146 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_PC_MAXVAL); |
9862 | 146 | return gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, |
9863 | 146 | SWAPLONG(MTP3_PC_MAXVAL << 14), jtype, reverse, |
9864 | 146 | SWAPLONG(jvalue << 14)); |
9865 | | |
9866 | 47 | case MH_DPC: |
9867 | 47 | newoff_dpc += 3; |
9868 | | /* FALLTHROUGH */ |
9869 | | |
9870 | 149 | case M_DPC: |
9871 | 149 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_PC_MAXVAL); |
9872 | 149 | return gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_H, |
9873 | 149 | SWAPSHORT(MTP3_PC_MAXVAL), jtype, reverse, |
9874 | 149 | SWAPSHORT(jvalue)); |
9875 | | |
9876 | 22 | case MH_SLS: |
9877 | 22 | newoff_sls += 3; |
9878 | | /* FALLTHROUGH */ |
9879 | | |
9880 | 69 | case M_SLS: |
9881 | 69 | assert_maxval(cstate, ss7kw(mtp3field), jvalue, MTP3_SLS_MAXVAL); |
9882 | 69 | return gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, |
9883 | 69 | MTP3_SLS_MAXVAL << 4, jtype, reverse, |
9884 | 69 | jvalue << 4); |
9885 | | |
9886 | 0 | default: |
9887 | 0 | abort(); |
9888 | 592 | } |
9889 | 592 | } |
9890 | | |
9891 | | struct block * |
9892 | | gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, |
9893 | | bpf_u_int32 jvalue, int jtype, int reverse) |
9894 | 592 | { |
9895 | | /* |
9896 | | * Catch errors reported by us and routines below us, and return NULL |
9897 | | * on an error. |
9898 | | */ |
9899 | 592 | if (setjmp(cstate->top_ctx)) |
9900 | 60 | return (NULL); |
9901 | | |
9902 | 532 | return gen_mtp3field_code_internal(cstate, mtp3field, jvalue, jtype, |
9903 | 532 | reverse); |
9904 | 592 | } |
9905 | | |
9906 | | static struct block * |
9907 | | gen_msg_abbrev(compiler_state_t *cstate, const uint8_t type) |
9908 | 0 | { |
9909 | | /* |
9910 | | * Q.2931 signalling protocol messages for handling virtual circuits |
9911 | | * establishment and teardown |
9912 | | */ |
9913 | 0 | return gen_cmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, |
9914 | 0 | BPF_B, type); |
9915 | 0 | } |
9916 | | |
9917 | | struct block * |
9918 | | gen_atmmulti_abbrev(compiler_state_t *cstate, int type) |
9919 | 31 | { |
9920 | 31 | struct block *b0, *b1; |
9921 | | |
9922 | | /* |
9923 | | * Catch errors reported by us and routines below us, and return NULL |
9924 | | * on an error. |
9925 | | */ |
9926 | 31 | if (setjmp(cstate->top_ctx)) |
9927 | 6 | return (NULL); |
9928 | | |
9929 | 25 | assert_atm(cstate, atmkw(type)); |
9930 | | |
9931 | 25 | switch (type) { |
9932 | | |
9933 | 15 | case A_OAM: |
9934 | | /* OAM F4 type */ |
9935 | 15 | b0 = gen_atm_vci(cstate, 3); |
9936 | 15 | b1 = gen_atm_vci(cstate, 4); |
9937 | 15 | gen_or(b0, b1); |
9938 | 15 | b0 = gen_atm_vpi(cstate, 0); |
9939 | 15 | gen_and(b0, b1); |
9940 | 15 | return b1; |
9941 | | |
9942 | 10 | case A_OAMF4: |
9943 | | /* OAM F4 type */ |
9944 | 10 | b0 = gen_atm_vci(cstate, 3); |
9945 | 10 | b1 = gen_atm_vci(cstate, 4); |
9946 | 10 | gen_or(b0, b1); |
9947 | 10 | b0 = gen_atm_vpi(cstate, 0); |
9948 | 10 | gen_and(b0, b1); |
9949 | 10 | return b1; |
9950 | | |
9951 | 0 | case A_CONNECTMSG: |
9952 | | /* |
9953 | | * Get Q.2931 signalling messages for switched |
9954 | | * virtual connection |
9955 | | */ |
9956 | 0 | b0 = gen_msg_abbrev(cstate, SETUP); |
9957 | 0 | b1 = gen_msg_abbrev(cstate, CALL_PROCEED); |
9958 | 0 | gen_or(b0, b1); |
9959 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT); |
9960 | 0 | gen_or(b0, b1); |
9961 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT_ACK); |
9962 | 0 | gen_or(b0, b1); |
9963 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE); |
9964 | 0 | gen_or(b0, b1); |
9965 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE_DONE); |
9966 | 0 | gen_or(b0, b1); |
9967 | 0 | b0 = gen_atmtype_abbrev(cstate, A_SC); |
9968 | 0 | gen_and(b0, b1); |
9969 | 0 | return b1; |
9970 | | |
9971 | 0 | case A_METACONNECT: |
9972 | 0 | b0 = gen_msg_abbrev(cstate, SETUP); |
9973 | 0 | b1 = gen_msg_abbrev(cstate, CALL_PROCEED); |
9974 | 0 | gen_or(b0, b1); |
9975 | 0 | b0 = gen_msg_abbrev(cstate, CONNECT); |
9976 | 0 | gen_or(b0, b1); |
9977 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE); |
9978 | 0 | gen_or(b0, b1); |
9979 | 0 | b0 = gen_msg_abbrev(cstate, RELEASE_DONE); |
9980 | 0 | gen_or(b0, b1); |
9981 | 0 | b0 = gen_atmtype_abbrev(cstate, A_METAC); |
9982 | 0 | gen_and(b0, b1); |
9983 | 0 | return b1; |
9984 | | |
9985 | 0 | default: |
9986 | 0 | abort(); |
9987 | 25 | } |
9988 | 25 | } |