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