]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | /* $apfw: pf_norm.c,v 1.10 2008/08/28 19:10:53 jhw Exp $ */ | |
30 | /* $OpenBSD: pf_norm.c,v 1.107 2006/04/16 00:59:52 pascoe Exp $ */ | |
31 | ||
32 | /* | |
33 | * Copyright 2001 Niels Provos <provos@citi.umich.edu> | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
46 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
47 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
48 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
49 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
50 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
51 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
52 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
53 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
54 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
55 | */ | |
56 | ||
57 | #include <sys/param.h> | |
58 | #include <sys/systm.h> | |
59 | #include <sys/mbuf.h> | |
60 | #include <sys/filio.h> | |
61 | #include <sys/fcntl.h> | |
62 | #include <sys/socket.h> | |
63 | #include <sys/kernel.h> | |
64 | #include <sys/time.h> | |
65 | #include <sys/random.h> | |
66 | #include <sys/mcache.h> | |
67 | ||
68 | #include <net/if.h> | |
69 | #include <net/if_types.h> | |
70 | #include <net/bpf.h> | |
71 | #include <net/route.h> | |
72 | #include <net/if_pflog.h> | |
73 | ||
74 | #include <netinet/in.h> | |
75 | #include <netinet/in_var.h> | |
76 | #include <netinet/in_systm.h> | |
77 | #include <netinet/ip.h> | |
78 | #include <netinet/ip_var.h> | |
79 | #include <netinet/tcp.h> | |
80 | #include <netinet/tcp_seq.h> | |
81 | #include <netinet/tcp_fsm.h> | |
82 | #include <netinet/udp.h> | |
83 | #include <netinet/ip_icmp.h> | |
84 | ||
85 | #if INET6 | |
86 | #include <netinet/ip6.h> | |
87 | #endif /* INET6 */ | |
88 | ||
89 | #include <net/pfvar.h> | |
90 | ||
91 | struct pf_frent { | |
92 | LIST_ENTRY(pf_frent) fr_next; | |
93 | struct ip *fr_ip; | |
94 | struct mbuf *fr_m; | |
95 | }; | |
96 | ||
97 | struct pf_frcache { | |
98 | LIST_ENTRY(pf_frcache) fr_next; | |
99 | uint16_t fr_off; | |
100 | uint16_t fr_end; | |
101 | }; | |
102 | ||
103 | #define PFFRAG_SEENLAST 0x0001 /* Seen the last fragment for this */ | |
104 | #define PFFRAG_NOBUFFER 0x0002 /* Non-buffering fragment cache */ | |
105 | #define PFFRAG_DROP 0x0004 /* Drop all fragments */ | |
106 | #define BUFFER_FRAGMENTS(fr) (!((fr)->fr_flags & PFFRAG_NOBUFFER)) | |
107 | ||
108 | struct pf_fragment { | |
109 | RB_ENTRY(pf_fragment) fr_entry; | |
110 | TAILQ_ENTRY(pf_fragment) frag_next; | |
111 | struct in_addr fr_src; | |
112 | struct in_addr fr_dst; | |
113 | u_int8_t fr_p; /* protocol of this fragment */ | |
114 | u_int8_t fr_flags; /* status flags */ | |
115 | u_int16_t fr_id; /* fragment id for reassemble */ | |
116 | u_int16_t fr_max; /* fragment data max */ | |
117 | u_int32_t fr_timeout; | |
118 | #define fr_queue fr_u.fru_queue | |
119 | #define fr_cache fr_u.fru_cache | |
120 | union { | |
121 | LIST_HEAD(pf_fragq, pf_frent) fru_queue; /* buffering */ | |
122 | LIST_HEAD(pf_cacheq, pf_frcache) fru_cache; /* non-buf */ | |
123 | } fr_u; | |
124 | }; | |
125 | ||
126 | static TAILQ_HEAD(pf_fragqueue, pf_fragment) pf_fragqueue; | |
127 | static TAILQ_HEAD(pf_cachequeue, pf_fragment) pf_cachequeue; | |
128 | ||
129 | static __inline int pf_frag_compare(struct pf_fragment *, | |
130 | struct pf_fragment *); | |
131 | static RB_HEAD(pf_frag_tree, pf_fragment) pf_frag_tree, pf_cache_tree; | |
132 | RB_PROTOTYPE_SC(__private_extern__, pf_frag_tree, pf_fragment, fr_entry, | |
133 | pf_frag_compare); | |
134 | RB_GENERATE(pf_frag_tree, pf_fragment, fr_entry, pf_frag_compare); | |
135 | ||
136 | /* Private prototypes */ | |
137 | static void pf_ip2key(struct pf_fragment *, struct ip *); | |
138 | static void pf_remove_fragment(struct pf_fragment *); | |
139 | static void pf_flush_fragments(void); | |
140 | static void pf_free_fragment(struct pf_fragment *); | |
141 | static struct pf_fragment *pf_find_fragment(struct ip *, struct pf_frag_tree *); | |
142 | static struct mbuf *pf_reassemble(struct mbuf **, struct pf_fragment **, | |
143 | struct pf_frent *, int); | |
144 | static struct mbuf *pf_fragcache(struct mbuf **, struct ip *, | |
145 | struct pf_fragment **, int, int, int *); | |
146 | #ifndef NO_APPLE_MODIFICATIONS | |
147 | static int pf_normalize_tcpopt(struct pf_rule *, int, struct pfi_kif *, | |
148 | struct pf_pdesc *, struct mbuf *, struct tcphdr *, int, int *); | |
149 | #else | |
150 | static int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *, | |
151 | struct tcphdr *, int, sa_family_t); | |
152 | #endif | |
153 | ||
154 | #define DPFPRINTF(x) do { \ | |
155 | if (pf_status.debug >= PF_DEBUG_MISC) { \ | |
156 | printf("%s: ", __func__); \ | |
157 | printf x ; \ | |
158 | } \ | |
159 | } while (0) | |
160 | ||
161 | /* Globals */ | |
162 | struct pool pf_frent_pl, pf_frag_pl; | |
163 | static struct pool pf_cache_pl, pf_cent_pl; | |
164 | struct pool pf_state_scrub_pl; | |
165 | ||
166 | static int pf_nfrents, pf_ncache; | |
167 | ||
168 | void | |
169 | pf_normalize_init(void) | |
170 | { | |
171 | pool_init(&pf_frent_pl, sizeof (struct pf_frent), 0, 0, 0, "pffrent", | |
172 | NULL); | |
173 | pool_init(&pf_frag_pl, sizeof (struct pf_fragment), 0, 0, 0, "pffrag", | |
174 | NULL); | |
175 | pool_init(&pf_cache_pl, sizeof (struct pf_fragment), 0, 0, 0, | |
176 | "pffrcache", NULL); | |
177 | pool_init(&pf_cent_pl, sizeof (struct pf_frcache), 0, 0, 0, "pffrcent", | |
178 | NULL); | |
179 | pool_init(&pf_state_scrub_pl, sizeof (struct pf_state_scrub), 0, 0, 0, | |
180 | "pfstscr", NULL); | |
181 | ||
182 | pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT); | |
183 | pool_sethardlimit(&pf_frent_pl, PFFRAG_FRENT_HIWAT, NULL, 0); | |
184 | pool_sethardlimit(&pf_cache_pl, PFFRAG_FRCACHE_HIWAT, NULL, 0); | |
185 | pool_sethardlimit(&pf_cent_pl, PFFRAG_FRCENT_HIWAT, NULL, 0); | |
186 | ||
187 | TAILQ_INIT(&pf_fragqueue); | |
188 | TAILQ_INIT(&pf_cachequeue); | |
189 | } | |
190 | ||
191 | #if 0 | |
192 | void | |
193 | pf_normalize_destroy(void) | |
194 | { | |
195 | pool_destroy(&pf_state_scrub_pl); | |
196 | pool_destroy(&pf_cent_pl); | |
197 | pool_destroy(&pf_cache_pl); | |
198 | pool_destroy(&pf_frag_pl); | |
199 | pool_destroy(&pf_frent_pl); | |
200 | } | |
201 | #endif | |
202 | ||
203 | int | |
204 | pf_normalize_isempty(void) | |
205 | { | |
206 | return (TAILQ_EMPTY(&pf_fragqueue) && TAILQ_EMPTY(&pf_cachequeue)); | |
207 | } | |
208 | ||
209 | static __inline int | |
210 | pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b) | |
211 | { | |
212 | int diff; | |
213 | ||
214 | if ((diff = a->fr_id - b->fr_id)) | |
215 | return (diff); | |
216 | else if ((diff = a->fr_p - b->fr_p)) | |
217 | return (diff); | |
218 | else if (a->fr_src.s_addr < b->fr_src.s_addr) | |
219 | return (-1); | |
220 | else if (a->fr_src.s_addr > b->fr_src.s_addr) | |
221 | return (1); | |
222 | else if (a->fr_dst.s_addr < b->fr_dst.s_addr) | |
223 | return (-1); | |
224 | else if (a->fr_dst.s_addr > b->fr_dst.s_addr) | |
225 | return (1); | |
226 | return (0); | |
227 | } | |
228 | ||
229 | void | |
230 | pf_purge_expired_fragments(void) | |
231 | { | |
232 | struct pf_fragment *frag; | |
233 | u_int32_t expire = pf_time_second() - | |
234 | pf_default_rule.timeout[PFTM_FRAG]; | |
235 | ||
236 | while ((frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue)) != NULL) { | |
237 | VERIFY(BUFFER_FRAGMENTS(frag)); | |
238 | if (frag->fr_timeout > expire) | |
239 | break; | |
240 | ||
241 | DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag)); | |
242 | pf_free_fragment(frag); | |
243 | } | |
244 | ||
245 | while ((frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue)) != NULL) { | |
246 | VERIFY(!BUFFER_FRAGMENTS(frag)); | |
247 | if (frag->fr_timeout > expire) | |
248 | break; | |
249 | ||
250 | DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag)); | |
251 | pf_free_fragment(frag); | |
252 | VERIFY(TAILQ_EMPTY(&pf_cachequeue) || | |
253 | TAILQ_LAST(&pf_cachequeue, pf_cachequeue) != frag); | |
254 | } | |
255 | } | |
256 | ||
257 | /* | |
258 | * Try to flush old fragments to make space for new ones | |
259 | */ | |
260 | ||
261 | static void | |
262 | pf_flush_fragments(void) | |
263 | { | |
264 | struct pf_fragment *frag; | |
265 | int goal; | |
266 | ||
267 | goal = pf_nfrents * 9 / 10; | |
268 | DPFPRINTF(("trying to free > %d frents\n", | |
269 | pf_nfrents - goal)); | |
270 | while (goal < pf_nfrents) { | |
271 | frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue); | |
272 | if (frag == NULL) | |
273 | break; | |
274 | pf_free_fragment(frag); | |
275 | } | |
276 | ||
277 | ||
278 | goal = pf_ncache * 9 / 10; | |
279 | DPFPRINTF(("trying to free > %d cache entries\n", | |
280 | pf_ncache - goal)); | |
281 | while (goal < pf_ncache) { | |
282 | frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue); | |
283 | if (frag == NULL) | |
284 | break; | |
285 | pf_free_fragment(frag); | |
286 | } | |
287 | } | |
288 | ||
289 | /* Frees the fragments and all associated entries */ | |
290 | ||
291 | static void | |
292 | pf_free_fragment(struct pf_fragment *frag) | |
293 | { | |
294 | struct pf_frent *frent; | |
295 | struct pf_frcache *frcache; | |
296 | ||
297 | /* Free all fragments */ | |
298 | if (BUFFER_FRAGMENTS(frag)) { | |
299 | for (frent = LIST_FIRST(&frag->fr_queue); frent; | |
300 | frent = LIST_FIRST(&frag->fr_queue)) { | |
301 | LIST_REMOVE(frent, fr_next); | |
302 | ||
303 | m_freem(frent->fr_m); | |
304 | pool_put(&pf_frent_pl, frent); | |
305 | pf_nfrents--; | |
306 | } | |
307 | } else { | |
308 | for (frcache = LIST_FIRST(&frag->fr_cache); frcache; | |
309 | frcache = LIST_FIRST(&frag->fr_cache)) { | |
310 | LIST_REMOVE(frcache, fr_next); | |
311 | ||
312 | VERIFY(LIST_EMPTY(&frag->fr_cache) || | |
313 | LIST_FIRST(&frag->fr_cache)->fr_off > | |
314 | frcache->fr_end); | |
315 | ||
316 | pool_put(&pf_cent_pl, frcache); | |
317 | pf_ncache--; | |
318 | } | |
319 | } | |
320 | ||
321 | pf_remove_fragment(frag); | |
322 | } | |
323 | ||
324 | static void | |
325 | pf_ip2key(struct pf_fragment *key, struct ip *ip) | |
326 | { | |
327 | key->fr_p = ip->ip_p; | |
328 | key->fr_id = ip->ip_id; | |
329 | key->fr_src.s_addr = ip->ip_src.s_addr; | |
330 | key->fr_dst.s_addr = ip->ip_dst.s_addr; | |
331 | } | |
332 | ||
333 | static struct pf_fragment * | |
334 | pf_find_fragment(struct ip *ip, struct pf_frag_tree *tree) | |
335 | { | |
336 | struct pf_fragment key; | |
337 | struct pf_fragment *frag; | |
338 | ||
339 | pf_ip2key(&key, ip); | |
340 | ||
341 | frag = RB_FIND(pf_frag_tree, tree, &key); | |
342 | if (frag != NULL) { | |
343 | /* XXX Are we sure we want to update the timeout? */ | |
344 | frag->fr_timeout = pf_time_second(); | |
345 | if (BUFFER_FRAGMENTS(frag)) { | |
346 | TAILQ_REMOVE(&pf_fragqueue, frag, frag_next); | |
347 | TAILQ_INSERT_HEAD(&pf_fragqueue, frag, frag_next); | |
348 | } else { | |
349 | TAILQ_REMOVE(&pf_cachequeue, frag, frag_next); | |
350 | TAILQ_INSERT_HEAD(&pf_cachequeue, frag, frag_next); | |
351 | } | |
352 | } | |
353 | ||
354 | return (frag); | |
355 | } | |
356 | ||
357 | /* Removes a fragment from the fragment queue and frees the fragment */ | |
358 | ||
359 | static void | |
360 | pf_remove_fragment(struct pf_fragment *frag) | |
361 | { | |
362 | if (BUFFER_FRAGMENTS(frag)) { | |
363 | RB_REMOVE(pf_frag_tree, &pf_frag_tree, frag); | |
364 | TAILQ_REMOVE(&pf_fragqueue, frag, frag_next); | |
365 | pool_put(&pf_frag_pl, frag); | |
366 | } else { | |
367 | RB_REMOVE(pf_frag_tree, &pf_cache_tree, frag); | |
368 | TAILQ_REMOVE(&pf_cachequeue, frag, frag_next); | |
369 | pool_put(&pf_cache_pl, frag); | |
370 | } | |
371 | } | |
372 | ||
373 | #define FR_IP_OFF(fr) ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3) | |
374 | static struct mbuf * | |
375 | pf_reassemble(struct mbuf **m0, struct pf_fragment **frag, | |
376 | struct pf_frent *frent, int mff) | |
377 | { | |
378 | struct mbuf *m = *m0, *m2; | |
379 | struct pf_frent *frea, *next; | |
380 | struct pf_frent *frep = NULL; | |
381 | struct ip *ip = frent->fr_ip; | |
382 | int hlen = ip->ip_hl << 2; | |
383 | u_int16_t off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3; | |
384 | u_int16_t ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4; | |
385 | u_int16_t fr_max = ip_len + off; | |
386 | ||
387 | VERIFY(*frag == NULL || BUFFER_FRAGMENTS(*frag)); | |
388 | ||
389 | /* Strip off ip header */ | |
390 | m->m_data += hlen; | |
391 | m->m_len -= hlen; | |
392 | ||
393 | /* Create a new reassembly queue for this packet */ | |
394 | if (*frag == NULL) { | |
395 | *frag = pool_get(&pf_frag_pl, PR_NOWAIT); | |
396 | if (*frag == NULL) { | |
397 | pf_flush_fragments(); | |
398 | *frag = pool_get(&pf_frag_pl, PR_NOWAIT); | |
399 | if (*frag == NULL) | |
400 | goto drop_fragment; | |
401 | } | |
402 | ||
403 | (*frag)->fr_flags = 0; | |
404 | (*frag)->fr_max = 0; | |
405 | (*frag)->fr_src = frent->fr_ip->ip_src; | |
406 | (*frag)->fr_dst = frent->fr_ip->ip_dst; | |
407 | (*frag)->fr_p = frent->fr_ip->ip_p; | |
408 | (*frag)->fr_id = frent->fr_ip->ip_id; | |
409 | (*frag)->fr_timeout = pf_time_second(); | |
410 | LIST_INIT(&(*frag)->fr_queue); | |
411 | ||
412 | RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag); | |
413 | TAILQ_INSERT_HEAD(&pf_fragqueue, *frag, frag_next); | |
414 | ||
415 | /* We do not have a previous fragment */ | |
416 | frep = NULL; | |
417 | goto insert; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Find a fragment after the current one: | |
422 | * - off contains the real shifted offset. | |
423 | */ | |
424 | LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) { | |
425 | if (FR_IP_OFF(frea) > off) | |
426 | break; | |
427 | frep = frea; | |
428 | } | |
429 | ||
430 | VERIFY(frep != NULL || frea != NULL); | |
431 | ||
432 | if (frep != NULL && | |
433 | FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl * | |
434 | 4 > off) { | |
435 | u_int16_t precut; | |
436 | ||
437 | precut = FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - | |
438 | frep->fr_ip->ip_hl * 4 - off; | |
439 | if (precut >= ip_len) | |
440 | goto drop_fragment; | |
441 | m_adj(frent->fr_m, precut); | |
442 | DPFPRINTF(("overlap -%d\n", precut)); | |
443 | /* Enforce 8 byte boundaries */ | |
444 | ip->ip_off = htons(ntohs(ip->ip_off) + (precut >> 3)); | |
445 | off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3; | |
446 | ip_len -= precut; | |
447 | ip->ip_len = htons(ip_len); | |
448 | } | |
449 | ||
450 | for (; frea != NULL && ip_len + off > FR_IP_OFF(frea); | |
451 | frea = next) { | |
452 | u_int16_t aftercut; | |
453 | ||
454 | aftercut = ip_len + off - FR_IP_OFF(frea); | |
455 | DPFPRINTF(("adjust overlap %d\n", aftercut)); | |
456 | if (aftercut < ntohs(frea->fr_ip->ip_len) - frea->fr_ip->ip_hl | |
457 | * 4) { | |
458 | frea->fr_ip->ip_len = | |
459 | htons(ntohs(frea->fr_ip->ip_len) - aftercut); | |
460 | frea->fr_ip->ip_off = htons(ntohs(frea->fr_ip->ip_off) + | |
461 | (aftercut >> 3)); | |
462 | m_adj(frea->fr_m, aftercut); | |
463 | break; | |
464 | } | |
465 | ||
466 | /* This fragment is completely overlapped, lose it */ | |
467 | next = LIST_NEXT(frea, fr_next); | |
468 | m_freem(frea->fr_m); | |
469 | LIST_REMOVE(frea, fr_next); | |
470 | pool_put(&pf_frent_pl, frea); | |
471 | pf_nfrents--; | |
472 | } | |
473 | ||
474 | insert: | |
475 | /* Update maximum data size */ | |
476 | if ((*frag)->fr_max < fr_max) | |
477 | (*frag)->fr_max = fr_max; | |
478 | /* This is the last segment */ | |
479 | if (!mff) | |
480 | (*frag)->fr_flags |= PFFRAG_SEENLAST; | |
481 | ||
482 | if (frep == NULL) | |
483 | LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next); | |
484 | else | |
485 | LIST_INSERT_AFTER(frep, frent, fr_next); | |
486 | ||
487 | /* Check if we are completely reassembled */ | |
488 | if (!((*frag)->fr_flags & PFFRAG_SEENLAST)) | |
489 | return (NULL); | |
490 | ||
491 | /* Check if we have all the data */ | |
492 | off = 0; | |
493 | for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) { | |
494 | next = LIST_NEXT(frep, fr_next); | |
495 | ||
496 | off += ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl * 4; | |
497 | if (off < (*frag)->fr_max && | |
498 | (next == NULL || FR_IP_OFF(next) != off)) { | |
499 | DPFPRINTF(("missing fragment at %d, next %d, max %d\n", | |
500 | off, next == NULL ? -1 : FR_IP_OFF(next), | |
501 | (*frag)->fr_max)); | |
502 | return (NULL); | |
503 | } | |
504 | } | |
505 | DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max)); | |
506 | if (off < (*frag)->fr_max) | |
507 | return (NULL); | |
508 | ||
509 | /* We have all the data */ | |
510 | frent = LIST_FIRST(&(*frag)->fr_queue); | |
511 | VERIFY(frent != NULL); | |
512 | if ((frent->fr_ip->ip_hl << 2) + off > IP_MAXPACKET) { | |
513 | DPFPRINTF(("drop: too big: %d\n", off)); | |
514 | pf_free_fragment(*frag); | |
515 | *frag = NULL; | |
516 | return (NULL); | |
517 | } | |
518 | next = LIST_NEXT(frent, fr_next); | |
519 | ||
520 | /* Magic from ip_input */ | |
521 | ip = frent->fr_ip; | |
522 | m = frent->fr_m; | |
523 | m2 = m->m_next; | |
524 | m->m_next = NULL; | |
525 | m_cat(m, m2); | |
526 | pool_put(&pf_frent_pl, frent); | |
527 | pf_nfrents--; | |
528 | for (frent = next; frent != NULL; frent = next) { | |
529 | next = LIST_NEXT(frent, fr_next); | |
530 | ||
531 | m2 = frent->fr_m; | |
532 | pool_put(&pf_frent_pl, frent); | |
533 | pf_nfrents--; | |
534 | m_cat(m, m2); | |
535 | } | |
536 | ||
537 | ip->ip_src = (*frag)->fr_src; | |
538 | ip->ip_dst = (*frag)->fr_dst; | |
539 | ||
540 | /* Remove from fragment queue */ | |
541 | pf_remove_fragment(*frag); | |
542 | *frag = NULL; | |
543 | ||
544 | hlen = ip->ip_hl << 2; | |
545 | ip->ip_len = htons(off + hlen); | |
546 | m->m_len += hlen; | |
547 | m->m_data -= hlen; | |
548 | ||
549 | /* some debugging cruft by sklower, below, will go away soon */ | |
550 | /* XXX this should be done elsewhere */ | |
551 | if (m->m_flags & M_PKTHDR) { | |
552 | int plen = 0; | |
553 | for (m2 = m; m2; m2 = m2->m_next) | |
554 | plen += m2->m_len; | |
555 | m->m_pkthdr.len = plen; | |
556 | } | |
557 | ||
558 | DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len))); | |
559 | return (m); | |
560 | ||
561 | drop_fragment: | |
562 | /* Oops - fail safe - drop packet */ | |
563 | pool_put(&pf_frent_pl, frent); | |
564 | pf_nfrents--; | |
565 | m_freem(m); | |
566 | return (NULL); | |
567 | } | |
568 | ||
569 | static struct mbuf * | |
570 | pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff, | |
571 | int drop, int *nomem) | |
572 | { | |
573 | struct mbuf *m = *m0; | |
574 | struct pf_frcache *frp, *fra, *cur = NULL; | |
575 | int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2); | |
576 | u_int16_t off = ntohs(h->ip_off) << 3; | |
577 | u_int16_t fr_max = ip_len + off; | |
578 | int hosed = 0; | |
579 | ||
580 | VERIFY(*frag == NULL || !BUFFER_FRAGMENTS(*frag)); | |
581 | ||
582 | /* Create a new range queue for this packet */ | |
583 | if (*frag == NULL) { | |
584 | *frag = pool_get(&pf_cache_pl, PR_NOWAIT); | |
585 | if (*frag == NULL) { | |
586 | pf_flush_fragments(); | |
587 | *frag = pool_get(&pf_cache_pl, PR_NOWAIT); | |
588 | if (*frag == NULL) | |
589 | goto no_mem; | |
590 | } | |
591 | ||
592 | /* Get an entry for the queue */ | |
593 | cur = pool_get(&pf_cent_pl, PR_NOWAIT); | |
594 | if (cur == NULL) { | |
595 | pool_put(&pf_cache_pl, *frag); | |
596 | *frag = NULL; | |
597 | goto no_mem; | |
598 | } | |
599 | pf_ncache++; | |
600 | ||
601 | (*frag)->fr_flags = PFFRAG_NOBUFFER; | |
602 | (*frag)->fr_max = 0; | |
603 | (*frag)->fr_src = h->ip_src; | |
604 | (*frag)->fr_dst = h->ip_dst; | |
605 | (*frag)->fr_p = h->ip_p; | |
606 | (*frag)->fr_id = h->ip_id; | |
607 | (*frag)->fr_timeout = pf_time_second(); | |
608 | ||
609 | cur->fr_off = off; | |
610 | cur->fr_end = fr_max; | |
611 | LIST_INIT(&(*frag)->fr_cache); | |
612 | LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next); | |
613 | ||
614 | RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag); | |
615 | TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next); | |
616 | ||
617 | DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, | |
618 | fr_max)); | |
619 | ||
620 | goto pass; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Find a fragment after the current one: | |
625 | * - off contains the real shifted offset. | |
626 | */ | |
627 | frp = NULL; | |
628 | LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) { | |
629 | if (fra->fr_off > off) | |
630 | break; | |
631 | frp = fra; | |
632 | } | |
633 | ||
634 | VERIFY(frp != NULL || fra != NULL); | |
635 | ||
636 | if (frp != NULL) { | |
637 | int precut; | |
638 | ||
639 | precut = frp->fr_end - off; | |
640 | if (precut >= ip_len) { | |
641 | /* Fragment is entirely a duplicate */ | |
642 | DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n", | |
643 | h->ip_id, frp->fr_off, frp->fr_end, off, fr_max)); | |
644 | goto drop_fragment; | |
645 | } | |
646 | if (precut == 0) { | |
647 | /* They are adjacent. Fixup cache entry */ | |
648 | DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n", | |
649 | h->ip_id, frp->fr_off, frp->fr_end, off, fr_max)); | |
650 | frp->fr_end = fr_max; | |
651 | } else if (precut > 0) { | |
652 | /* | |
653 | * The first part of this payload overlaps with a | |
654 | * fragment that has already been passed. | |
655 | * Need to trim off the first part of the payload. | |
656 | * But to do so easily, we need to create another | |
657 | * mbuf to throw the original header into. | |
658 | */ | |
659 | ||
660 | DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n", | |
661 | h->ip_id, precut, frp->fr_off, frp->fr_end, off, | |
662 | fr_max)); | |
663 | ||
664 | off += precut; | |
665 | fr_max -= precut; | |
666 | /* Update the previous frag to encompass this one */ | |
667 | frp->fr_end = fr_max; | |
668 | ||
669 | if (!drop) { | |
670 | /* | |
671 | * XXX Optimization opportunity | |
672 | * This is a very heavy way to trim the payload. | |
673 | * we could do it much faster by diddling mbuf | |
674 | * internals but that would be even less legible | |
675 | * than this mbuf magic. For my next trick, | |
676 | * I'll pull a rabbit out of my laptop. | |
677 | */ | |
678 | *m0 = m_copym(m, 0, h->ip_hl << 2, M_NOWAIT); | |
679 | if (*m0 == NULL) | |
680 | goto no_mem; | |
681 | VERIFY((*m0)->m_next == NULL); | |
682 | m_adj(m, precut + (h->ip_hl << 2)); | |
683 | m_cat(*m0, m); | |
684 | m = *m0; | |
685 | if (m->m_flags & M_PKTHDR) { | |
686 | int plen = 0; | |
687 | struct mbuf *t; | |
688 | for (t = m; t; t = t->m_next) | |
689 | plen += t->m_len; | |
690 | m->m_pkthdr.len = plen; | |
691 | } | |
692 | ||
693 | ||
694 | h = mtod(m, struct ip *); | |
695 | ||
696 | ||
697 | VERIFY((int)m->m_len == | |
698 | ntohs(h->ip_len) - precut); | |
699 | h->ip_off = htons(ntohs(h->ip_off) + | |
700 | (precut >> 3)); | |
701 | h->ip_len = htons(ntohs(h->ip_len) - precut); | |
702 | } else { | |
703 | hosed++; | |
704 | } | |
705 | } else { | |
706 | /* There is a gap between fragments */ | |
707 | ||
708 | DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n", | |
709 | h->ip_id, -precut, frp->fr_off, frp->fr_end, off, | |
710 | fr_max)); | |
711 | ||
712 | cur = pool_get(&pf_cent_pl, PR_NOWAIT); | |
713 | if (cur == NULL) | |
714 | goto no_mem; | |
715 | pf_ncache++; | |
716 | ||
717 | cur->fr_off = off; | |
718 | cur->fr_end = fr_max; | |
719 | LIST_INSERT_AFTER(frp, cur, fr_next); | |
720 | } | |
721 | } | |
722 | ||
723 | if (fra != NULL) { | |
724 | int aftercut; | |
725 | int merge = 0; | |
726 | ||
727 | aftercut = fr_max - fra->fr_off; | |
728 | if (aftercut == 0) { | |
729 | /* Adjacent fragments */ | |
730 | DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n", | |
731 | h->ip_id, off, fr_max, fra->fr_off, fra->fr_end)); | |
732 | fra->fr_off = off; | |
733 | merge = 1; | |
734 | } else if (aftercut > 0) { | |
735 | /* Need to chop off the tail of this fragment */ | |
736 | DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n", | |
737 | h->ip_id, aftercut, off, fr_max, fra->fr_off, | |
738 | fra->fr_end)); | |
739 | fra->fr_off = off; | |
740 | fr_max -= aftercut; | |
741 | ||
742 | merge = 1; | |
743 | ||
744 | if (!drop) { | |
745 | m_adj(m, -aftercut); | |
746 | if (m->m_flags & M_PKTHDR) { | |
747 | int plen = 0; | |
748 | struct mbuf *t; | |
749 | for (t = m; t; t = t->m_next) | |
750 | plen += t->m_len; | |
751 | m->m_pkthdr.len = plen; | |
752 | } | |
753 | h = mtod(m, struct ip *); | |
754 | VERIFY((int)m->m_len == | |
755 | ntohs(h->ip_len) - aftercut); | |
756 | h->ip_len = htons(ntohs(h->ip_len) - aftercut); | |
757 | } else { | |
758 | hosed++; | |
759 | } | |
760 | } else if (frp == NULL) { | |
761 | /* There is a gap between fragments */ | |
762 | DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n", | |
763 | h->ip_id, -aftercut, off, fr_max, fra->fr_off, | |
764 | fra->fr_end)); | |
765 | ||
766 | cur = pool_get(&pf_cent_pl, PR_NOWAIT); | |
767 | if (cur == NULL) | |
768 | goto no_mem; | |
769 | pf_ncache++; | |
770 | ||
771 | cur->fr_off = off; | |
772 | cur->fr_end = fr_max; | |
773 | LIST_INSERT_BEFORE(fra, cur, fr_next); | |
774 | } | |
775 | ||
776 | ||
777 | /* Need to glue together two separate fragment descriptors */ | |
778 | if (merge) { | |
779 | if (cur && fra->fr_off <= cur->fr_end) { | |
780 | /* Need to merge in a previous 'cur' */ | |
781 | DPFPRINTF(("fragcache[%d]: adjacent(merge " | |
782 | "%d-%d) %d-%d (%d-%d)\n", | |
783 | h->ip_id, cur->fr_off, cur->fr_end, off, | |
784 | fr_max, fra->fr_off, fra->fr_end)); | |
785 | fra->fr_off = cur->fr_off; | |
786 | LIST_REMOVE(cur, fr_next); | |
787 | pool_put(&pf_cent_pl, cur); | |
788 | pf_ncache--; | |
789 | cur = NULL; | |
790 | ||
791 | } else if (frp && fra->fr_off <= frp->fr_end) { | |
792 | /* Need to merge in a modified 'frp' */ | |
793 | VERIFY(cur == NULL); | |
794 | DPFPRINTF(("fragcache[%d]: adjacent(merge " | |
795 | "%d-%d) %d-%d (%d-%d)\n", | |
796 | h->ip_id, frp->fr_off, frp->fr_end, off, | |
797 | fr_max, fra->fr_off, fra->fr_end)); | |
798 | fra->fr_off = frp->fr_off; | |
799 | LIST_REMOVE(frp, fr_next); | |
800 | pool_put(&pf_cent_pl, frp); | |
801 | pf_ncache--; | |
802 | frp = NULL; | |
803 | ||
804 | } | |
805 | } | |
806 | } | |
807 | ||
808 | if (hosed) { | |
809 | /* | |
810 | * We must keep tracking the overall fragment even when | |
811 | * we're going to drop it anyway so that we know when to | |
812 | * free the overall descriptor. Thus we drop the frag late. | |
813 | */ | |
814 | goto drop_fragment; | |
815 | } | |
816 | ||
817 | ||
818 | pass: | |
819 | /* Update maximum data size */ | |
820 | if ((*frag)->fr_max < fr_max) | |
821 | (*frag)->fr_max = fr_max; | |
822 | ||
823 | /* This is the last segment */ | |
824 | if (!mff) | |
825 | (*frag)->fr_flags |= PFFRAG_SEENLAST; | |
826 | ||
827 | /* Check if we are completely reassembled */ | |
828 | if (((*frag)->fr_flags & PFFRAG_SEENLAST) && | |
829 | LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 && | |
830 | LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) { | |
831 | /* Remove from fragment queue */ | |
832 | DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id, | |
833 | (*frag)->fr_max)); | |
834 | pf_free_fragment(*frag); | |
835 | *frag = NULL; | |
836 | } | |
837 | ||
838 | return (m); | |
839 | ||
840 | no_mem: | |
841 | *nomem = 1; | |
842 | ||
843 | /* Still need to pay attention to !IP_MF */ | |
844 | if (!mff && *frag != NULL) | |
845 | (*frag)->fr_flags |= PFFRAG_SEENLAST; | |
846 | ||
847 | m_freem(m); | |
848 | return (NULL); | |
849 | ||
850 | drop_fragment: | |
851 | ||
852 | /* Still need to pay attention to !IP_MF */ | |
853 | if (!mff && *frag != NULL) | |
854 | (*frag)->fr_flags |= PFFRAG_SEENLAST; | |
855 | ||
856 | if (drop) { | |
857 | /* This fragment has been deemed bad. Don't reass */ | |
858 | if (((*frag)->fr_flags & PFFRAG_DROP) == 0) | |
859 | DPFPRINTF(("fragcache[%d]: dropping overall fragment\n", | |
860 | h->ip_id)); | |
861 | (*frag)->fr_flags |= PFFRAG_DROP; | |
862 | } | |
863 | ||
864 | m_freem(m); | |
865 | return (NULL); | |
866 | } | |
867 | ||
868 | int | |
869 | pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason, | |
870 | struct pf_pdesc *pd) | |
871 | { | |
872 | struct mbuf *m = *m0; | |
873 | struct pf_rule *r; | |
874 | struct pf_frent *frent; | |
875 | struct pf_fragment *frag = NULL; | |
876 | struct ip *h = mtod(m, struct ip *); | |
877 | int mff = (ntohs(h->ip_off) & IP_MF); | |
878 | int hlen = h->ip_hl << 2; | |
879 | u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; | |
880 | u_int16_t fr_max; | |
881 | int ip_len; | |
882 | int ip_off; | |
883 | ||
884 | r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); | |
885 | while (r != NULL) { | |
886 | r->evaluations++; | |
887 | if (pfi_kif_match(r->kif, kif) == r->ifnot) | |
888 | r = r->skip[PF_SKIP_IFP].ptr; | |
889 | else if (r->direction && r->direction != dir) | |
890 | r = r->skip[PF_SKIP_DIR].ptr; | |
891 | else if (r->af && r->af != AF_INET) | |
892 | r = r->skip[PF_SKIP_AF].ptr; | |
893 | else if (r->proto && r->proto != h->ip_p) | |
894 | r = r->skip[PF_SKIP_PROTO].ptr; | |
895 | else if (PF_MISMATCHAW(&r->src.addr, | |
896 | (struct pf_addr *)&h->ip_src.s_addr, AF_INET, | |
897 | r->src.neg, kif)) | |
898 | r = r->skip[PF_SKIP_SRC_ADDR].ptr; | |
899 | else if (PF_MISMATCHAW(&r->dst.addr, | |
900 | (struct pf_addr *)&h->ip_dst.s_addr, AF_INET, | |
901 | r->dst.neg, NULL)) | |
902 | r = r->skip[PF_SKIP_DST_ADDR].ptr; | |
903 | else | |
904 | break; | |
905 | } | |
906 | ||
907 | if (r == NULL || r->action == PF_NOSCRUB) | |
908 | return (PF_PASS); | |
909 | else { | |
910 | r->packets[dir == PF_OUT]++; | |
911 | r->bytes[dir == PF_OUT] += pd->tot_len; | |
912 | } | |
913 | ||
914 | /* Check for illegal packets */ | |
915 | if (hlen < (int)sizeof (struct ip)) | |
916 | goto drop; | |
917 | ||
918 | if (hlen > ntohs(h->ip_len)) | |
919 | goto drop; | |
920 | ||
921 | /* Clear IP_DF if the rule uses the no-df option */ | |
922 | if (r->rule_flag & PFRULE_NODF && h->ip_off & htons(IP_DF)) { | |
923 | u_int16_t ipoff = h->ip_off; | |
924 | ||
925 | h->ip_off &= htons(~IP_DF); | |
926 | h->ip_sum = pf_cksum_fixup(h->ip_sum, ipoff, h->ip_off, 0); | |
927 | } | |
928 | ||
929 | /* We will need other tests here */ | |
930 | if (!fragoff && !mff) | |
931 | goto no_fragment; | |
932 | ||
933 | /* | |
934 | * We're dealing with a fragment now. Don't allow fragments | |
935 | * with IP_DF to enter the cache. If the flag was cleared by | |
936 | * no-df above, fine. Otherwise drop it. | |
937 | */ | |
938 | if (h->ip_off & htons(IP_DF)) { | |
939 | DPFPRINTF(("IP_DF\n")); | |
940 | goto bad; | |
941 | } | |
942 | ||
943 | ip_len = ntohs(h->ip_len) - hlen; | |
944 | ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3; | |
945 | ||
946 | /* All fragments are 8 byte aligned */ | |
947 | if (mff && (ip_len & 0x7)) { | |
948 | DPFPRINTF(("mff and %d\n", ip_len)); | |
949 | goto bad; | |
950 | } | |
951 | ||
952 | /* Respect maximum length */ | |
953 | if (fragoff + ip_len > IP_MAXPACKET) { | |
954 | DPFPRINTF(("max packet %d\n", fragoff + ip_len)); | |
955 | goto bad; | |
956 | } | |
957 | fr_max = fragoff + ip_len; | |
958 | ||
959 | if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) { | |
960 | /* Fully buffer all of the fragments */ | |
961 | ||
962 | frag = pf_find_fragment(h, &pf_frag_tree); | |
963 | ||
964 | /* Check if we saw the last fragment already */ | |
965 | if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) && | |
966 | fr_max > frag->fr_max) | |
967 | goto bad; | |
968 | ||
969 | /* Get an entry for the fragment queue */ | |
970 | frent = pool_get(&pf_frent_pl, PR_NOWAIT); | |
971 | if (frent == NULL) { | |
972 | REASON_SET(reason, PFRES_MEMORY); | |
973 | return (PF_DROP); | |
974 | } | |
975 | pf_nfrents++; | |
976 | frent->fr_ip = h; | |
977 | frent->fr_m = m; | |
978 | ||
979 | /* Might return a completely reassembled mbuf, or NULL */ | |
980 | DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, | |
981 | fr_max)); | |
982 | *m0 = m = pf_reassemble(m0, &frag, frent, mff); | |
983 | ||
984 | if (m == NULL) | |
985 | return (PF_DROP); | |
986 | ||
987 | /* use mtag from concatenated mbuf chain */ | |
988 | pd->pf_mtag = pf_find_mtag(m); | |
989 | #ifdef DIAGNOSTIC | |
990 | if (pd->pf_mtag == NULL) { | |
991 | printf("%s: pf_find_mtag returned NULL(1)\n", __func__); | |
992 | if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) { | |
993 | m_freem(m); | |
994 | *m0 = NULL; | |
995 | goto no_mem; | |
996 | } | |
997 | } | |
998 | #endif | |
999 | if (frag != NULL && (frag->fr_flags & PFFRAG_DROP)) | |
1000 | goto drop; | |
1001 | ||
1002 | h = mtod(m, struct ip *); | |
1003 | } else { | |
1004 | /* non-buffering fragment cache (drops or masks overlaps) */ | |
1005 | int nomem = 0; | |
1006 | ||
1007 | if (dir == PF_OUT && (pd->pf_mtag->flags & PF_TAG_FRAGCACHE)) { | |
1008 | /* | |
1009 | * Already passed the fragment cache in the | |
1010 | * input direction. If we continued, it would | |
1011 | * appear to be a dup and would be dropped. | |
1012 | */ | |
1013 | goto fragment_pass; | |
1014 | } | |
1015 | ||
1016 | frag = pf_find_fragment(h, &pf_cache_tree); | |
1017 | ||
1018 | /* Check if we saw the last fragment already */ | |
1019 | if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) && | |
1020 | fr_max > frag->fr_max) { | |
1021 | if (r->rule_flag & PFRULE_FRAGDROP) | |
1022 | frag->fr_flags |= PFFRAG_DROP; | |
1023 | goto bad; | |
1024 | } | |
1025 | ||
1026 | *m0 = m = pf_fragcache(m0, h, &frag, mff, | |
1027 | (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem); | |
1028 | if (m == NULL) { | |
1029 | if (nomem) | |
1030 | goto no_mem; | |
1031 | goto drop; | |
1032 | } | |
1033 | ||
1034 | /* use mtag from copied and trimmed mbuf chain */ | |
1035 | pd->pf_mtag = pf_find_mtag(m); | |
1036 | #ifdef DIAGNOSTIC | |
1037 | if (pd->pf_mtag == NULL) { | |
1038 | printf("%s: pf_find_mtag returned NULL(2)\n", __func__); | |
1039 | if ((pd->pf_mtag = pf_get_mtag(m)) == NULL) { | |
1040 | m_freem(m); | |
1041 | *m0 = NULL; | |
1042 | goto no_mem; | |
1043 | } | |
1044 | } | |
1045 | #endif | |
1046 | if (dir == PF_IN) | |
1047 | pd->pf_mtag->flags |= PF_TAG_FRAGCACHE; | |
1048 | ||
1049 | if (frag != NULL && (frag->fr_flags & PFFRAG_DROP)) | |
1050 | goto drop; | |
1051 | goto fragment_pass; | |
1052 | } | |
1053 | ||
1054 | no_fragment: | |
1055 | /* At this point, only IP_DF is allowed in ip_off */ | |
1056 | if (h->ip_off & ~htons(IP_DF)) { | |
1057 | u_int16_t ipoff = h->ip_off; | |
1058 | ||
1059 | h->ip_off &= htons(IP_DF); | |
1060 | h->ip_sum = pf_cksum_fixup(h->ip_sum, ipoff, h->ip_off, 0); | |
1061 | } | |
1062 | ||
1063 | /* Enforce a minimum ttl, may cause endless packet loops */ | |
1064 | if (r->min_ttl && h->ip_ttl < r->min_ttl) { | |
1065 | u_int16_t ip_ttl = h->ip_ttl; | |
1066 | ||
1067 | h->ip_ttl = r->min_ttl; | |
1068 | h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0); | |
1069 | } | |
1070 | #if RANDOM_IP_ID | |
1071 | if (r->rule_flag & PFRULE_RANDOMID) { | |
1072 | u_int16_t ip_id = h->ip_id; | |
1073 | ||
1074 | h->ip_id = ip_randomid(); | |
1075 | h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0); | |
1076 | } | |
1077 | #endif /* RANDOM_IP_ID */ | |
1078 | if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) | |
1079 | pd->flags |= PFDESC_IP_REAS; | |
1080 | ||
1081 | return (PF_PASS); | |
1082 | ||
1083 | fragment_pass: | |
1084 | /* Enforce a minimum ttl, may cause endless packet loops */ | |
1085 | if (r->min_ttl && h->ip_ttl < r->min_ttl) { | |
1086 | u_int16_t ip_ttl = h->ip_ttl; | |
1087 | ||
1088 | h->ip_ttl = r->min_ttl; | |
1089 | h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0); | |
1090 | } | |
1091 | if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) | |
1092 | pd->flags |= PFDESC_IP_REAS; | |
1093 | return (PF_PASS); | |
1094 | ||
1095 | no_mem: | |
1096 | REASON_SET(reason, PFRES_MEMORY); | |
1097 | if (r != NULL && r->log) | |
1098 | PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, | |
1099 | NULL, NULL, pd); | |
1100 | return (PF_DROP); | |
1101 | ||
1102 | drop: | |
1103 | REASON_SET(reason, PFRES_NORM); | |
1104 | if (r != NULL && r->log) | |
1105 | PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, | |
1106 | NULL, NULL, pd); | |
1107 | return (PF_DROP); | |
1108 | ||
1109 | bad: | |
1110 | DPFPRINTF(("dropping bad fragment\n")); | |
1111 | ||
1112 | /* Free associated fragments */ | |
1113 | if (frag != NULL) | |
1114 | pf_free_fragment(frag); | |
1115 | ||
1116 | REASON_SET(reason, PFRES_FRAG); | |
1117 | if (r != NULL && r->log) | |
1118 | PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL, pd); | |
1119 | ||
1120 | return (PF_DROP); | |
1121 | } | |
1122 | ||
1123 | #if INET6 | |
1124 | int | |
1125 | pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif, | |
1126 | u_short *reason, struct pf_pdesc *pd) | |
1127 | { | |
1128 | struct mbuf *m = *m0; | |
1129 | struct pf_rule *r; | |
1130 | struct ip6_hdr *h = mtod(m, struct ip6_hdr *); | |
1131 | int off; | |
1132 | struct ip6_ext ext; | |
1133 | /* adi XXX */ | |
1134 | #if 0 | |
1135 | struct ip6_opt opt; | |
1136 | struct ip6_opt_jumbo jumbo; | |
1137 | int optend; | |
1138 | int ooff; | |
1139 | #endif | |
1140 | struct ip6_frag frag; | |
1141 | u_int32_t jumbolen = 0, plen; | |
1142 | u_int16_t fragoff = 0; | |
1143 | u_int8_t proto; | |
1144 | int terminal; | |
1145 | ||
1146 | r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); | |
1147 | while (r != NULL) { | |
1148 | r->evaluations++; | |
1149 | if (pfi_kif_match(r->kif, kif) == r->ifnot) | |
1150 | r = r->skip[PF_SKIP_IFP].ptr; | |
1151 | else if (r->direction && r->direction != dir) | |
1152 | r = r->skip[PF_SKIP_DIR].ptr; | |
1153 | else if (r->af && r->af != AF_INET6) | |
1154 | r = r->skip[PF_SKIP_AF].ptr; | |
1155 | #if 0 /* header chain! */ | |
1156 | else if (r->proto && r->proto != h->ip6_nxt) | |
1157 | r = r->skip[PF_SKIP_PROTO].ptr; | |
1158 | #endif | |
1159 | else if (PF_MISMATCHAW(&r->src.addr, | |
1160 | (struct pf_addr *)&h->ip6_src, AF_INET6, | |
1161 | r->src.neg, kif)) | |
1162 | r = r->skip[PF_SKIP_SRC_ADDR].ptr; | |
1163 | else if (PF_MISMATCHAW(&r->dst.addr, | |
1164 | (struct pf_addr *)&h->ip6_dst, AF_INET6, | |
1165 | r->dst.neg, NULL)) | |
1166 | r = r->skip[PF_SKIP_DST_ADDR].ptr; | |
1167 | else | |
1168 | break; | |
1169 | } | |
1170 | ||
1171 | if (r == NULL || r->action == PF_NOSCRUB) | |
1172 | return (PF_PASS); | |
1173 | else { | |
1174 | r->packets[dir == PF_OUT]++; | |
1175 | r->bytes[dir == PF_OUT] += pd->tot_len; | |
1176 | } | |
1177 | ||
1178 | /* Check for illegal packets */ | |
1179 | if ((int)(sizeof (struct ip6_hdr) + IPV6_MAXPACKET) < m->m_pkthdr.len) | |
1180 | goto drop; | |
1181 | ||
1182 | off = sizeof (struct ip6_hdr); | |
1183 | proto = h->ip6_nxt; | |
1184 | terminal = 0; | |
1185 | do { | |
1186 | switch (proto) { | |
1187 | case IPPROTO_FRAGMENT: | |
1188 | goto fragment; | |
1189 | break; | |
1190 | case IPPROTO_AH: | |
1191 | case IPPROTO_ROUTING: | |
1192 | case IPPROTO_DSTOPTS: | |
1193 | if (!pf_pull_hdr(m, off, &ext, sizeof (ext), NULL, | |
1194 | NULL, AF_INET6)) | |
1195 | goto shortpkt; | |
1196 | #ifndef NO_APPLE_EXTENSIONS | |
1197 | /* | |
1198 | * <jhw@apple.com> | |
1199 | * Routing header type zero considered harmful. | |
1200 | */ | |
1201 | if (proto == IPPROTO_ROUTING) { | |
1202 | const struct ip6_rthdr *rh = | |
1203 | (const struct ip6_rthdr *)&ext; | |
1204 | if (rh->ip6r_type == IPV6_RTHDR_TYPE_0) | |
1205 | goto drop; | |
1206 | } | |
1207 | else | |
1208 | #endif | |
1209 | if (proto == IPPROTO_AH) | |
1210 | off += (ext.ip6e_len + 2) * 4; | |
1211 | else | |
1212 | off += (ext.ip6e_len + 1) * 8; | |
1213 | proto = ext.ip6e_nxt; | |
1214 | break; | |
1215 | case IPPROTO_HOPOPTS: | |
1216 | /* adi XXX */ | |
1217 | #if 0 | |
1218 | if (!pf_pull_hdr(m, off, &ext, sizeof (ext), NULL, | |
1219 | NULL, AF_INET6)) | |
1220 | goto shortpkt; | |
1221 | optend = off + (ext.ip6e_len + 1) * 8; | |
1222 | ooff = off + sizeof (ext); | |
1223 | do { | |
1224 | if (!pf_pull_hdr(m, ooff, &opt.ip6o_type, | |
1225 | sizeof (opt.ip6o_type), NULL, NULL, | |
1226 | AF_INET6)) | |
1227 | goto shortpkt; | |
1228 | if (opt.ip6o_type == IP6OPT_PAD1) { | |
1229 | ooff++; | |
1230 | continue; | |
1231 | } | |
1232 | if (!pf_pull_hdr(m, ooff, &opt, sizeof (opt), | |
1233 | NULL, NULL, AF_INET6)) | |
1234 | goto shortpkt; | |
1235 | if (ooff + sizeof (opt) + opt.ip6o_len > optend) | |
1236 | goto drop; | |
1237 | switch (opt.ip6o_type) { | |
1238 | case IP6OPT_JUMBO: | |
1239 | if (h->ip6_plen != 0) | |
1240 | goto drop; | |
1241 | if (!pf_pull_hdr(m, ooff, &jumbo, | |
1242 | sizeof (jumbo), NULL, NULL, | |
1243 | AF_INET6)) | |
1244 | goto shortpkt; | |
1245 | memcpy(&jumbolen, jumbo.ip6oj_jumbo_len, | |
1246 | sizeof (jumbolen)); | |
1247 | jumbolen = ntohl(jumbolen); | |
1248 | if (jumbolen <= IPV6_MAXPACKET) | |
1249 | goto drop; | |
1250 | if (sizeof (struct ip6_hdr) + | |
1251 | jumbolen != m->m_pkthdr.len) | |
1252 | goto drop; | |
1253 | break; | |
1254 | default: | |
1255 | break; | |
1256 | } | |
1257 | ooff += sizeof (opt) + opt.ip6o_len; | |
1258 | } while (ooff < optend); | |
1259 | ||
1260 | off = optend; | |
1261 | proto = ext.ip6e_nxt; | |
1262 | break; | |
1263 | #endif | |
1264 | default: | |
1265 | terminal = 1; | |
1266 | break; | |
1267 | } | |
1268 | } while (!terminal); | |
1269 | ||
1270 | /* jumbo payload option must be present, or plen > 0 */ | |
1271 | if (ntohs(h->ip6_plen) == 0) | |
1272 | plen = jumbolen; | |
1273 | else | |
1274 | plen = ntohs(h->ip6_plen); | |
1275 | if (plen == 0) | |
1276 | goto drop; | |
1277 | if ((int)(sizeof (struct ip6_hdr) + plen) > m->m_pkthdr.len) | |
1278 | goto shortpkt; | |
1279 | ||
1280 | /* Enforce a minimum ttl, may cause endless packet loops */ | |
1281 | if (r->min_ttl && h->ip6_hlim < r->min_ttl) | |
1282 | h->ip6_hlim = r->min_ttl; | |
1283 | ||
1284 | return (PF_PASS); | |
1285 | ||
1286 | fragment: | |
1287 | if (ntohs(h->ip6_plen) == 0 || jumbolen) | |
1288 | goto drop; | |
1289 | plen = ntohs(h->ip6_plen); | |
1290 | ||
1291 | if (!pf_pull_hdr(m, off, &frag, sizeof (frag), NULL, NULL, AF_INET6)) | |
1292 | goto shortpkt; | |
1293 | fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK); | |
1294 | if (fragoff + (plen - off - sizeof (frag)) > IPV6_MAXPACKET) | |
1295 | goto badfrag; | |
1296 | ||
1297 | /* do something about it */ | |
1298 | /* remember to set pd->flags |= PFDESC_IP_REAS */ | |
1299 | return (PF_PASS); | |
1300 | ||
1301 | shortpkt: | |
1302 | REASON_SET(reason, PFRES_SHORT); | |
1303 | if (r != NULL && r->log) | |
1304 | PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, | |
1305 | NULL, NULL, pd); | |
1306 | return (PF_DROP); | |
1307 | ||
1308 | drop: | |
1309 | REASON_SET(reason, PFRES_NORM); | |
1310 | if (r != NULL && r->log) | |
1311 | PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, | |
1312 | NULL, NULL, pd); | |
1313 | return (PF_DROP); | |
1314 | ||
1315 | badfrag: | |
1316 | REASON_SET(reason, PFRES_FRAG); | |
1317 | if (r != NULL && r->log) | |
1318 | PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, | |
1319 | NULL, NULL, pd); | |
1320 | return (PF_DROP); | |
1321 | } | |
1322 | #endif /* INET6 */ | |
1323 | ||
1324 | int | |
1325 | pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff, | |
1326 | int off, void *h, struct pf_pdesc *pd) | |
1327 | { | |
1328 | #pragma unused(ipoff, h) | |
1329 | struct pf_rule *r, *rm = NULL; | |
1330 | struct tcphdr *th = pd->hdr.tcp; | |
1331 | int rewrite = 0; | |
1332 | u_short reason; | |
1333 | u_int8_t flags; | |
1334 | sa_family_t af = pd->af; | |
1335 | #ifndef NO_APPLE_EXTENSIONS | |
1336 | union pf_state_xport sxport, dxport; | |
1337 | ||
1338 | sxport.port = th->th_sport; | |
1339 | dxport.port = th->th_dport; | |
1340 | #endif | |
1341 | ||
1342 | r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr); | |
1343 | while (r != NULL) { | |
1344 | r->evaluations++; | |
1345 | if (pfi_kif_match(r->kif, kif) == r->ifnot) | |
1346 | r = r->skip[PF_SKIP_IFP].ptr; | |
1347 | else if (r->direction && r->direction != dir) | |
1348 | r = r->skip[PF_SKIP_DIR].ptr; | |
1349 | else if (r->af && r->af != af) | |
1350 | r = r->skip[PF_SKIP_AF].ptr; | |
1351 | else if (r->proto && r->proto != pd->proto) | |
1352 | r = r->skip[PF_SKIP_PROTO].ptr; | |
1353 | else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, | |
1354 | r->src.neg, kif)) | |
1355 | r = r->skip[PF_SKIP_SRC_ADDR].ptr; | |
1356 | #ifndef NO_APPLE_EXTENSIONS | |
1357 | else if (r->src.xport.range.op && | |
1358 | !pf_match_xport(r->src.xport.range.op, r->proto_variant, | |
1359 | &r->src.xport, &sxport)) | |
1360 | #else | |
1361 | else if (r->src.port_op && !pf_match_port(r->src.port_op, | |
1362 | r->src.port[0], r->src.port[1], th->th_sport)) | |
1363 | #endif | |
1364 | r = r->skip[PF_SKIP_SRC_PORT].ptr; | |
1365 | else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, | |
1366 | r->dst.neg, NULL)) | |
1367 | r = r->skip[PF_SKIP_DST_ADDR].ptr; | |
1368 | #ifndef NO_APPLE_EXTENSIONS | |
1369 | else if (r->dst.xport.range.op && | |
1370 | !pf_match_xport(r->dst.xport.range.op, r->proto_variant, | |
1371 | &r->dst.xport, &dxport)) | |
1372 | #else | |
1373 | else if (r->dst.port_op && !pf_match_port(r->dst.port_op, | |
1374 | r->dst.port[0], r->dst.port[1], th->th_dport)) | |
1375 | #endif | |
1376 | r = r->skip[PF_SKIP_DST_PORT].ptr; | |
1377 | else if (r->os_fingerprint != PF_OSFP_ANY && | |
1378 | !pf_osfp_match(pf_osfp_fingerprint(pd, m, off, th), | |
1379 | r->os_fingerprint)) | |
1380 | r = TAILQ_NEXT(r, entries); | |
1381 | else { | |
1382 | rm = r; | |
1383 | break; | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | if (rm == NULL || rm->action == PF_NOSCRUB) | |
1388 | return (PF_PASS); | |
1389 | else { | |
1390 | r->packets[dir == PF_OUT]++; | |
1391 | r->bytes[dir == PF_OUT] += pd->tot_len; | |
1392 | } | |
1393 | ||
1394 | if (rm->rule_flag & PFRULE_REASSEMBLE_TCP) | |
1395 | pd->flags |= PFDESC_TCP_NORM; | |
1396 | ||
1397 | flags = th->th_flags; | |
1398 | if (flags & TH_SYN) { | |
1399 | /* Illegal packet */ | |
1400 | if (flags & TH_RST) | |
1401 | goto tcp_drop; | |
1402 | ||
1403 | if (flags & TH_FIN) | |
1404 | flags &= ~TH_FIN; | |
1405 | } else { | |
1406 | /* Illegal packet */ | |
1407 | if (!(flags & (TH_ACK|TH_RST))) | |
1408 | goto tcp_drop; | |
1409 | } | |
1410 | ||
1411 | if (!(flags & TH_ACK)) { | |
1412 | /* These flags are only valid if ACK is set */ | |
1413 | if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG)) | |
1414 | goto tcp_drop; | |
1415 | } | |
1416 | ||
1417 | /* Check for illegal header length */ | |
1418 | if (th->th_off < (sizeof (struct tcphdr) >> 2)) | |
1419 | goto tcp_drop; | |
1420 | ||
1421 | /* If flags changed, or reserved data set, then adjust */ | |
1422 | if (flags != th->th_flags || th->th_x2 != 0) { | |
1423 | u_int16_t ov, nv; | |
1424 | ||
1425 | ov = *(u_int16_t *)(&th->th_ack + 1); | |
1426 | th->th_flags = flags; | |
1427 | th->th_x2 = 0; | |
1428 | nv = *(u_int16_t *)(&th->th_ack + 1); | |
1429 | ||
1430 | th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0); | |
1431 | rewrite = 1; | |
1432 | } | |
1433 | ||
1434 | /* Remove urgent pointer, if TH_URG is not set */ | |
1435 | if (!(flags & TH_URG) && th->th_urp) { | |
1436 | th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0); | |
1437 | th->th_urp = 0; | |
1438 | rewrite = 1; | |
1439 | } | |
1440 | ||
1441 | /* copy back packet headers if we sanitized */ | |
1442 | #ifndef NO_APPLE_EXTENSIONS | |
1443 | /* Process options */ | |
1444 | if (r->max_mss) { | |
1445 | int rv = pf_normalize_tcpopt(r, dir, kif, pd, m, th, off, | |
1446 | &rewrite); | |
1447 | if (rv == PF_DROP) | |
1448 | return rv; | |
1449 | m = pd->mp; | |
1450 | } | |
1451 | ||
1452 | if (rewrite) { | |
1453 | struct mbuf *mw = pf_lazy_makewritable(pd, m, | |
1454 | off + sizeof (*th)); | |
1455 | if (!mw) { | |
1456 | REASON_SET(&reason, PFRES_MEMORY); | |
1457 | if (r->log) | |
1458 | PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, | |
1459 | r, 0, 0, pd); | |
1460 | return PF_DROP; | |
1461 | } | |
1462 | ||
1463 | m_copyback(mw, off, sizeof (*th), th); | |
1464 | } | |
1465 | #else | |
1466 | /* Process options */ | |
1467 | if (r->max_mss && pf_normalize_tcpopt(r, m, th, off, pd->af)) | |
1468 | rewrite = 1; | |
1469 | ||
1470 | if (rewrite) | |
1471 | m_copyback(m, off, sizeof (*th), th); | |
1472 | #endif | |
1473 | ||
1474 | return (PF_PASS); | |
1475 | ||
1476 | tcp_drop: | |
1477 | REASON_SET(&reason, PFRES_NORM); | |
1478 | if (rm != NULL && r->log) | |
1479 | PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL, pd); | |
1480 | return (PF_DROP); | |
1481 | } | |
1482 | ||
1483 | int | |
1484 | pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd, | |
1485 | struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst) | |
1486 | { | |
1487 | #pragma unused(dst) | |
1488 | u_int32_t tsval, tsecr; | |
1489 | u_int8_t hdr[60]; | |
1490 | u_int8_t *opt; | |
1491 | ||
1492 | VERIFY(src->scrub == NULL); | |
1493 | ||
1494 | src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT); | |
1495 | if (src->scrub == NULL) | |
1496 | return (1); | |
1497 | bzero(src->scrub, sizeof (*src->scrub)); | |
1498 | ||
1499 | switch (pd->af) { | |
1500 | #if INET | |
1501 | case AF_INET: { | |
1502 | struct ip *h = mtod(m, struct ip *); | |
1503 | src->scrub->pfss_ttl = h->ip_ttl; | |
1504 | break; | |
1505 | } | |
1506 | #endif /* INET */ | |
1507 | #if INET6 | |
1508 | case AF_INET6: { | |
1509 | struct ip6_hdr *h = mtod(m, struct ip6_hdr *); | |
1510 | src->scrub->pfss_ttl = h->ip6_hlim; | |
1511 | break; | |
1512 | } | |
1513 | #endif /* INET6 */ | |
1514 | } | |
1515 | ||
1516 | ||
1517 | /* | |
1518 | * All normalizations below are only begun if we see the start of | |
1519 | * the connections. They must all set an enabled bit in pfss_flags | |
1520 | */ | |
1521 | if ((th->th_flags & TH_SYN) == 0) | |
1522 | return (0); | |
1523 | ||
1524 | ||
1525 | if (th->th_off > (sizeof (struct tcphdr) >> 2) && src->scrub && | |
1526 | pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) { | |
1527 | /* Diddle with TCP options */ | |
1528 | int hlen; | |
1529 | opt = hdr + sizeof (struct tcphdr); | |
1530 | hlen = (th->th_off << 2) - sizeof (struct tcphdr); | |
1531 | while (hlen >= TCPOLEN_TIMESTAMP) { | |
1532 | switch (*opt) { | |
1533 | case TCPOPT_EOL: /* FALLTHROUGH */ | |
1534 | case TCPOPT_NOP: | |
1535 | opt++; | |
1536 | hlen--; | |
1537 | break; | |
1538 | case TCPOPT_TIMESTAMP: | |
1539 | if (opt[1] >= TCPOLEN_TIMESTAMP) { | |
1540 | src->scrub->pfss_flags |= | |
1541 | PFSS_TIMESTAMP; | |
1542 | src->scrub->pfss_ts_mod = | |
1543 | htonl(random()); | |
1544 | ||
1545 | /* note PFSS_PAWS not set yet */ | |
1546 | memcpy(&tsval, &opt[2], | |
1547 | sizeof (u_int32_t)); | |
1548 | memcpy(&tsecr, &opt[6], | |
1549 | sizeof (u_int32_t)); | |
1550 | src->scrub->pfss_tsval0 = ntohl(tsval); | |
1551 | src->scrub->pfss_tsval = ntohl(tsval); | |
1552 | src->scrub->pfss_tsecr = ntohl(tsecr); | |
1553 | getmicrouptime(&src->scrub->pfss_last); | |
1554 | } | |
1555 | /* FALLTHROUGH */ | |
1556 | default: | |
1557 | hlen -= MAX(opt[1], 2); | |
1558 | opt += MAX(opt[1], 2); | |
1559 | break; | |
1560 | } | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | return (0); | |
1565 | } | |
1566 | ||
1567 | void | |
1568 | pf_normalize_tcp_cleanup(struct pf_state *state) | |
1569 | { | |
1570 | if (state->src.scrub) | |
1571 | pool_put(&pf_state_scrub_pl, state->src.scrub); | |
1572 | if (state->dst.scrub) | |
1573 | pool_put(&pf_state_scrub_pl, state->dst.scrub); | |
1574 | ||
1575 | /* Someday... flush the TCP segment reassembly descriptors. */ | |
1576 | } | |
1577 | ||
1578 | int | |
1579 | pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd, | |
1580 | u_short *reason, struct tcphdr *th, struct pf_state *state, | |
1581 | struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback) | |
1582 | { | |
1583 | struct timeval uptime; | |
1584 | u_int32_t tsval, tsecr; | |
1585 | u_int tsval_from_last; | |
1586 | u_int8_t hdr[60]; | |
1587 | u_int8_t *opt; | |
1588 | int copyback = 0; | |
1589 | int got_ts = 0; | |
1590 | ||
1591 | VERIFY(src->scrub || dst->scrub); | |
1592 | ||
1593 | /* | |
1594 | * Enforce the minimum TTL seen for this connection. Negate a common | |
1595 | * technique to evade an intrusion detection system and confuse | |
1596 | * firewall state code. | |
1597 | */ | |
1598 | switch (pd->af) { | |
1599 | #if INET | |
1600 | case AF_INET: { | |
1601 | if (src->scrub) { | |
1602 | struct ip *h = mtod(m, struct ip *); | |
1603 | if (h->ip_ttl > src->scrub->pfss_ttl) | |
1604 | src->scrub->pfss_ttl = h->ip_ttl; | |
1605 | h->ip_ttl = src->scrub->pfss_ttl; | |
1606 | } | |
1607 | break; | |
1608 | } | |
1609 | #endif /* INET */ | |
1610 | #if INET6 | |
1611 | case AF_INET6: { | |
1612 | if (src->scrub) { | |
1613 | struct ip6_hdr *h = mtod(m, struct ip6_hdr *); | |
1614 | if (h->ip6_hlim > src->scrub->pfss_ttl) | |
1615 | src->scrub->pfss_ttl = h->ip6_hlim; | |
1616 | h->ip6_hlim = src->scrub->pfss_ttl; | |
1617 | } | |
1618 | break; | |
1619 | } | |
1620 | #endif /* INET6 */ | |
1621 | } | |
1622 | ||
1623 | if (th->th_off > (sizeof (struct tcphdr) >> 2) && | |
1624 | ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) || | |
1625 | (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) && | |
1626 | pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) { | |
1627 | /* Diddle with TCP options */ | |
1628 | int hlen; | |
1629 | opt = hdr + sizeof (struct tcphdr); | |
1630 | hlen = (th->th_off << 2) - sizeof (struct tcphdr); | |
1631 | while (hlen >= TCPOLEN_TIMESTAMP) { | |
1632 | switch (*opt) { | |
1633 | case TCPOPT_EOL: /* FALLTHROUGH */ | |
1634 | case TCPOPT_NOP: | |
1635 | opt++; | |
1636 | hlen--; | |
1637 | break; | |
1638 | case TCPOPT_TIMESTAMP: | |
1639 | /* | |
1640 | * Modulate the timestamps. Can be used for | |
1641 | * NAT detection, OS uptime determination or | |
1642 | * reboot detection. | |
1643 | */ | |
1644 | ||
1645 | if (got_ts) { | |
1646 | /* Huh? Multiple timestamps!? */ | |
1647 | if (pf_status.debug >= PF_DEBUG_MISC) { | |
1648 | DPFPRINTF(("multiple TS??")); | |
1649 | pf_print_state(state); | |
1650 | printf("\n"); | |
1651 | } | |
1652 | REASON_SET(reason, PFRES_TS); | |
1653 | return (PF_DROP); | |
1654 | } | |
1655 | if (opt[1] >= TCPOLEN_TIMESTAMP) { | |
1656 | memcpy(&tsval, &opt[2], | |
1657 | sizeof (u_int32_t)); | |
1658 | if (tsval && src->scrub && | |
1659 | (src->scrub->pfss_flags & | |
1660 | PFSS_TIMESTAMP)) { | |
1661 | tsval = ntohl(tsval); | |
1662 | pf_change_a(&opt[2], | |
1663 | &th->th_sum, | |
1664 | htonl(tsval + | |
1665 | src->scrub->pfss_ts_mod), | |
1666 | 0); | |
1667 | copyback = 1; | |
1668 | } | |
1669 | ||
1670 | /* Modulate TS reply iff valid (!0) */ | |
1671 | memcpy(&tsecr, &opt[6], | |
1672 | sizeof (u_int32_t)); | |
1673 | if (tsecr && dst->scrub && | |
1674 | (dst->scrub->pfss_flags & | |
1675 | PFSS_TIMESTAMP)) { | |
1676 | tsecr = ntohl(tsecr) | |
1677 | - dst->scrub->pfss_ts_mod; | |
1678 | pf_change_a(&opt[6], | |
1679 | &th->th_sum, htonl(tsecr), | |
1680 | 0); | |
1681 | copyback = 1; | |
1682 | } | |
1683 | got_ts = 1; | |
1684 | } | |
1685 | /* FALLTHROUGH */ | |
1686 | default: | |
1687 | hlen -= MAX(opt[1], 2); | |
1688 | opt += MAX(opt[1], 2); | |
1689 | break; | |
1690 | } | |
1691 | } | |
1692 | if (copyback) { | |
1693 | /* Copyback the options, caller copys back header */ | |
1694 | #ifndef NO_APPLE_EXTENSIONS | |
1695 | int optoff = off + sizeof (*th); | |
1696 | int optlen = (th->th_off << 2) - sizeof (*th); | |
1697 | m = pf_lazy_makewritable(pd, m, optoff + optlen); | |
1698 | if (!m) { | |
1699 | REASON_SET(reason, PFRES_MEMORY); | |
1700 | return PF_DROP; | |
1701 | } | |
1702 | *writeback = optoff + optlen; | |
1703 | m_copyback(m, optoff, optlen, hdr + sizeof (*th)); | |
1704 | #else | |
1705 | *writeback = 1; | |
1706 | m_copyback(m, off + sizeof (struct tcphdr), | |
1707 | (th->th_off << 2) - sizeof (struct tcphdr), hdr + | |
1708 | sizeof (struct tcphdr)); | |
1709 | #endif | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | ||
1714 | /* | |
1715 | * Must invalidate PAWS checks on connections idle for too long. | |
1716 | * The fastest allowed timestamp clock is 1ms. That turns out to | |
1717 | * be about 24 days before it wraps. XXX Right now our lowerbound | |
1718 | * TS echo check only works for the first 12 days of a connection | |
1719 | * when the TS has exhausted half its 32bit space | |
1720 | */ | |
1721 | #define TS_MAX_IDLE (24*24*60*60) | |
1722 | #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */ | |
1723 | ||
1724 | getmicrouptime(&uptime); | |
1725 | if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) && | |
1726 | (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE || | |
1727 | pf_time_second() - state->creation > TS_MAX_CONN)) { | |
1728 | if (pf_status.debug >= PF_DEBUG_MISC) { | |
1729 | DPFPRINTF(("src idled out of PAWS\n")); | |
1730 | pf_print_state(state); | |
1731 | printf("\n"); | |
1732 | } | |
1733 | src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS) | |
1734 | | PFSS_PAWS_IDLED; | |
1735 | } | |
1736 | if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) && | |
1737 | uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) { | |
1738 | if (pf_status.debug >= PF_DEBUG_MISC) { | |
1739 | DPFPRINTF(("dst idled out of PAWS\n")); | |
1740 | pf_print_state(state); | |
1741 | printf("\n"); | |
1742 | } | |
1743 | dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS) | |
1744 | | PFSS_PAWS_IDLED; | |
1745 | } | |
1746 | ||
1747 | if (got_ts && src->scrub && dst->scrub && | |
1748 | (src->scrub->pfss_flags & PFSS_PAWS) && | |
1749 | (dst->scrub->pfss_flags & PFSS_PAWS)) { | |
1750 | /* | |
1751 | * Validate that the timestamps are "in-window". | |
1752 | * RFC1323 describes TCP Timestamp options that allow | |
1753 | * measurement of RTT (round trip time) and PAWS | |
1754 | * (protection against wrapped sequence numbers). PAWS | |
1755 | * gives us a set of rules for rejecting packets on | |
1756 | * long fat pipes (packets that were somehow delayed | |
1757 | * in transit longer than the time it took to send the | |
1758 | * full TCP sequence space of 4Gb). We can use these | |
1759 | * rules and infer a few others that will let us treat | |
1760 | * the 32bit timestamp and the 32bit echoed timestamp | |
1761 | * as sequence numbers to prevent a blind attacker from | |
1762 | * inserting packets into a connection. | |
1763 | * | |
1764 | * RFC1323 tells us: | |
1765 | * - The timestamp on this packet must be greater than | |
1766 | * or equal to the last value echoed by the other | |
1767 | * endpoint. The RFC says those will be discarded | |
1768 | * since it is a dup that has already been acked. | |
1769 | * This gives us a lowerbound on the timestamp. | |
1770 | * timestamp >= other last echoed timestamp | |
1771 | * - The timestamp will be less than or equal to | |
1772 | * the last timestamp plus the time between the | |
1773 | * last packet and now. The RFC defines the max | |
1774 | * clock rate as 1ms. We will allow clocks to be | |
1775 | * up to 10% fast and will allow a total difference | |
1776 | * or 30 seconds due to a route change. And this | |
1777 | * gives us an upperbound on the timestamp. | |
1778 | * timestamp <= last timestamp + max ticks | |
1779 | * We have to be careful here. Windows will send an | |
1780 | * initial timestamp of zero and then initialize it | |
1781 | * to a random value after the 3whs; presumably to | |
1782 | * avoid a DoS by having to call an expensive RNG | |
1783 | * during a SYN flood. Proof MS has at least one | |
1784 | * good security geek. | |
1785 | * | |
1786 | * - The TCP timestamp option must also echo the other | |
1787 | * endpoints timestamp. The timestamp echoed is the | |
1788 | * one carried on the earliest unacknowledged segment | |
1789 | * on the left edge of the sequence window. The RFC | |
1790 | * states that the host will reject any echoed | |
1791 | * timestamps that were larger than any ever sent. | |
1792 | * This gives us an upperbound on the TS echo. | |
1793 | * tescr <= largest_tsval | |
1794 | * - The lowerbound on the TS echo is a little more | |
1795 | * tricky to determine. The other endpoint's echoed | |
1796 | * values will not decrease. But there may be | |
1797 | * network conditions that re-order packets and | |
1798 | * cause our view of them to decrease. For now the | |
1799 | * only lowerbound we can safely determine is that | |
1800 | * the TS echo will never be less than the original | |
1801 | * TS. XXX There is probably a better lowerbound. | |
1802 | * Remove TS_MAX_CONN with better lowerbound check. | |
1803 | * tescr >= other original TS | |
1804 | * | |
1805 | * It is also important to note that the fastest | |
1806 | * timestamp clock of 1ms will wrap its 32bit space in | |
1807 | * 24 days. So we just disable TS checking after 24 | |
1808 | * days of idle time. We actually must use a 12d | |
1809 | * connection limit until we can come up with a better | |
1810 | * lowerbound to the TS echo check. | |
1811 | */ | |
1812 | struct timeval delta_ts; | |
1813 | int ts_fudge; | |
1814 | ||
1815 | ||
1816 | /* | |
1817 | * PFTM_TS_DIFF is how many seconds of leeway to allow | |
1818 | * a host's timestamp. This can happen if the previous | |
1819 | * packet got delayed in transit for much longer than | |
1820 | * this packet. | |
1821 | */ | |
1822 | if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0) | |
1823 | ts_fudge = pf_default_rule.timeout[PFTM_TS_DIFF]; | |
1824 | ||
1825 | ||
1826 | /* Calculate max ticks since the last timestamp */ | |
1827 | #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */ | |
1828 | #define TS_MICROSECS 1000000 /* microseconds per second */ | |
1829 | timersub(&uptime, &src->scrub->pfss_last, &delta_ts); | |
1830 | tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ; | |
1831 | tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ); | |
1832 | ||
1833 | ||
1834 | if ((src->state >= TCPS_ESTABLISHED && | |
1835 | dst->state >= TCPS_ESTABLISHED) && | |
1836 | (SEQ_LT(tsval, dst->scrub->pfss_tsecr) || | |
1837 | SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) || | |
1838 | (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) || | |
1839 | SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) { | |
1840 | /* | |
1841 | * Bad RFC1323 implementation or an insertion attack. | |
1842 | * | |
1843 | * - Solaris 2.6 and 2.7 are known to send another ACK | |
1844 | * after the FIN,FIN|ACK,ACK closing that carries | |
1845 | * an old timestamp. | |
1846 | */ | |
1847 | ||
1848 | DPFPRINTF(("Timestamp failed %c%c%c%c\n", | |
1849 | SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ', | |
1850 | SEQ_GT(tsval, src->scrub->pfss_tsval + | |
1851 | tsval_from_last) ? '1' : ' ', | |
1852 | SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ', | |
1853 | SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' ')); | |
1854 | DPFPRINTF((" tsval: %u tsecr: %u +ticks: %u " | |
1855 | "idle: %lus %ums\n", | |
1856 | tsval, tsecr, tsval_from_last, delta_ts.tv_sec, | |
1857 | delta_ts.tv_usec / 1000)); | |
1858 | DPFPRINTF((" src->tsval: %u tsecr: %u\n", | |
1859 | src->scrub->pfss_tsval, src->scrub->pfss_tsecr)); | |
1860 | DPFPRINTF((" dst->tsval: %u tsecr: %u tsval0: %u\n", | |
1861 | dst->scrub->pfss_tsval, dst->scrub->pfss_tsecr, | |
1862 | dst->scrub->pfss_tsval0)); | |
1863 | if (pf_status.debug >= PF_DEBUG_MISC) { | |
1864 | pf_print_state(state); | |
1865 | pf_print_flags(th->th_flags); | |
1866 | printf("\n"); | |
1867 | } | |
1868 | REASON_SET(reason, PFRES_TS); | |
1869 | return (PF_DROP); | |
1870 | } | |
1871 | ||
1872 | /* XXX I'd really like to require tsecr but it's optional */ | |
1873 | ||
1874 | } else if (!got_ts && (th->th_flags & TH_RST) == 0 && | |
1875 | ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED) | |
1876 | || pd->p_len > 0 || (th->th_flags & TH_SYN)) && | |
1877 | src->scrub && dst->scrub && | |
1878 | (src->scrub->pfss_flags & PFSS_PAWS) && | |
1879 | (dst->scrub->pfss_flags & PFSS_PAWS)) { | |
1880 | /* | |
1881 | * Didn't send a timestamp. Timestamps aren't really useful | |
1882 | * when: | |
1883 | * - connection opening or closing (often not even sent). | |
1884 | * but we must not let an attacker to put a FIN on a | |
1885 | * data packet to sneak it through our ESTABLISHED check. | |
1886 | * - on a TCP reset. RFC suggests not even looking at TS. | |
1887 | * - on an empty ACK. The TS will not be echoed so it will | |
1888 | * probably not help keep the RTT calculation in sync and | |
1889 | * there isn't as much danger when the sequence numbers | |
1890 | * got wrapped. So some stacks don't include TS on empty | |
1891 | * ACKs :-( | |
1892 | * | |
1893 | * To minimize the disruption to mostly RFC1323 conformant | |
1894 | * stacks, we will only require timestamps on data packets. | |
1895 | * | |
1896 | * And what do ya know, we cannot require timestamps on data | |
1897 | * packets. There appear to be devices that do legitimate | |
1898 | * TCP connection hijacking. There are HTTP devices that allow | |
1899 | * a 3whs (with timestamps) and then buffer the HTTP request. | |
1900 | * If the intermediate device has the HTTP response cache, it | |
1901 | * will spoof the response but not bother timestamping its | |
1902 | * packets. So we can look for the presence of a timestamp in | |
1903 | * the first data packet and if there, require it in all future | |
1904 | * packets. | |
1905 | */ | |
1906 | ||
1907 | if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) { | |
1908 | /* | |
1909 | * Hey! Someone tried to sneak a packet in. Or the | |
1910 | * stack changed its RFC1323 behavior?!?! | |
1911 | */ | |
1912 | if (pf_status.debug >= PF_DEBUG_MISC) { | |
1913 | DPFPRINTF(("Did not receive expected RFC1323 " | |
1914 | "timestamp\n")); | |
1915 | pf_print_state(state); | |
1916 | pf_print_flags(th->th_flags); | |
1917 | printf("\n"); | |
1918 | } | |
1919 | REASON_SET(reason, PFRES_TS); | |
1920 | return (PF_DROP); | |
1921 | } | |
1922 | } | |
1923 | ||
1924 | ||
1925 | /* | |
1926 | * We will note if a host sends his data packets with or without | |
1927 | * timestamps. And require all data packets to contain a timestamp | |
1928 | * if the first does. PAWS implicitly requires that all data packets be | |
1929 | * timestamped. But I think there are middle-man devices that hijack | |
1930 | * TCP streams immediately after the 3whs and don't timestamp their | |
1931 | * packets (seen in a WWW accelerator or cache). | |
1932 | */ | |
1933 | if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags & | |
1934 | (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) { | |
1935 | if (got_ts) | |
1936 | src->scrub->pfss_flags |= PFSS_DATA_TS; | |
1937 | else { | |
1938 | src->scrub->pfss_flags |= PFSS_DATA_NOTS; | |
1939 | if (pf_status.debug >= PF_DEBUG_MISC && dst->scrub && | |
1940 | (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) { | |
1941 | /* Don't warn if other host rejected RFC1323 */ | |
1942 | DPFPRINTF(("Broken RFC1323 stack did not " | |
1943 | "timestamp data packet. Disabled PAWS " | |
1944 | "security.\n")); | |
1945 | pf_print_state(state); | |
1946 | pf_print_flags(th->th_flags); | |
1947 | printf("\n"); | |
1948 | } | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | ||
1953 | /* | |
1954 | * Update PAWS values | |
1955 | */ | |
1956 | if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags & | |
1957 | (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) { | |
1958 | getmicrouptime(&src->scrub->pfss_last); | |
1959 | if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) || | |
1960 | (src->scrub->pfss_flags & PFSS_PAWS) == 0) | |
1961 | src->scrub->pfss_tsval = tsval; | |
1962 | ||
1963 | if (tsecr) { | |
1964 | if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) || | |
1965 | (src->scrub->pfss_flags & PFSS_PAWS) == 0) | |
1966 | src->scrub->pfss_tsecr = tsecr; | |
1967 | ||
1968 | if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 && | |
1969 | (SEQ_LT(tsval, src->scrub->pfss_tsval0) || | |
1970 | src->scrub->pfss_tsval0 == 0)) { | |
1971 | /* tsval0 MUST be the lowest timestamp */ | |
1972 | src->scrub->pfss_tsval0 = tsval; | |
1973 | } | |
1974 | ||
1975 | /* Only fully initialized after a TS gets echoed */ | |
1976 | if ((src->scrub->pfss_flags & PFSS_PAWS) == 0) | |
1977 | src->scrub->pfss_flags |= PFSS_PAWS; | |
1978 | } | |
1979 | } | |
1980 | ||
1981 | /* I have a dream.... TCP segment reassembly.... */ | |
1982 | return (0); | |
1983 | } | |
1984 | ||
1985 | #ifndef NO_APPLE_EXTENSIONS | |
1986 | static int | |
1987 | pf_normalize_tcpopt(struct pf_rule *r, int dir, struct pfi_kif *kif, | |
1988 | struct pf_pdesc *pd, struct mbuf *m, struct tcphdr *th, int off, | |
1989 | int *rewrptr) | |
1990 | { | |
1991 | #pragma unused(dir, kif) | |
1992 | sa_family_t af = pd->af; | |
1993 | #else | |
1994 | static int | |
1995 | pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th, | |
1996 | int off, sa_family_t af) | |
1997 | { | |
1998 | #endif | |
1999 | u_int16_t *mss; | |
2000 | int thoff; | |
2001 | int opt, cnt, optlen = 0; | |
2002 | int rewrite = 0; | |
2003 | u_char opts[MAX_TCPOPTLEN]; | |
2004 | u_char *optp = opts; | |
2005 | ||
2006 | thoff = th->th_off << 2; | |
2007 | cnt = thoff - sizeof (struct tcphdr); | |
2008 | ||
2009 | #ifndef NO_APPLE_MODIFICATIONS | |
2010 | if (cnt > 0 && !pf_pull_hdr(m, off + sizeof (*th), opts, cnt, | |
2011 | NULL, NULL, af)) | |
2012 | return PF_DROP; | |
2013 | #else | |
2014 | if (cnt > 0 && !pf_pull_hdr(m, off + sizeof (*th), opts, cnt, | |
2015 | NULL, NULL, af)) | |
2016 | return (rewrite); | |
2017 | #endif | |
2018 | ||
2019 | for (; cnt > 0; cnt -= optlen, optp += optlen) { | |
2020 | opt = optp[0]; | |
2021 | if (opt == TCPOPT_EOL) | |
2022 | break; | |
2023 | if (opt == TCPOPT_NOP) | |
2024 | optlen = 1; | |
2025 | else { | |
2026 | if (cnt < 2) | |
2027 | break; | |
2028 | optlen = optp[1]; | |
2029 | if (optlen < 2 || optlen > cnt) | |
2030 | break; | |
2031 | } | |
2032 | switch (opt) { | |
2033 | case TCPOPT_MAXSEG: | |
2034 | mss = (u_int16_t *)(optp + 2); | |
2035 | if ((ntohs(*mss)) > r->max_mss) { | |
2036 | #ifndef NO_APPLE_MODIFICATIONS | |
2037 | /* | |
2038 | * <jhw@apple.com> | |
2039 | * Only do the TCP checksum fixup if delayed | |
2040 | * checksum calculation will not be performed. | |
2041 | */ | |
2042 | if (m->m_pkthdr.rcvif || | |
2043 | !(m->m_pkthdr.csum_flags & CSUM_TCP)) | |
2044 | th->th_sum = pf_cksum_fixup(th->th_sum, | |
2045 | *mss, htons(r->max_mss), 0); | |
2046 | #else | |
2047 | th->th_sum = pf_cksum_fixup(th->th_sum, | |
2048 | *mss, htons(r->max_mss), 0); | |
2049 | #endif | |
2050 | *mss = htons(r->max_mss); | |
2051 | rewrite = 1; | |
2052 | } | |
2053 | break; | |
2054 | default: | |
2055 | break; | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | #ifndef NO_APPLE_MODIFICATIONS | |
2060 | if (rewrite) { | |
2061 | struct mbuf *mw; | |
2062 | u_short reason; | |
2063 | ||
2064 | mw = pf_lazy_makewritable(pd, pd->mp, | |
2065 | off + sizeof (*th) + thoff); | |
2066 | if (!mw) { | |
2067 | REASON_SET(&reason, PFRES_MEMORY); | |
2068 | if (r->log) | |
2069 | PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, | |
2070 | r, 0, 0, pd); | |
2071 | return PF_DROP; | |
2072 | } | |
2073 | ||
2074 | *rewrptr = 1; | |
2075 | m_copyback(mw, off + sizeof (*th), thoff - sizeof (*th), opts); | |
2076 | } | |
2077 | ||
2078 | return PF_PASS; | |
2079 | #else | |
2080 | if (rewrite) | |
2081 | m_copyback(m, off + sizeof (*th), thoff - sizeof (*th), opts); | |
2082 | ||
2083 | return (rewrite); | |
2084 | #endif | |
2085 | } |