pf_norm.c revision 1.3.2.6 1 /* $NetBSD: pf_norm.c,v 1.3.2.6 2004/12/18 09:32:35 skrll 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 }
528
529 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
530 return (m);
531
532 drop_fragment:
533 /* Oops - fail safe - drop packet */
534 pool_put(&pf_frent_pl, frent);
535 pf_nfrents--;
536 m_freem(m);
537 return (NULL);
538 }
539
540 struct mbuf *
541 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
542 int drop, int *nomem)
543 {
544 struct mbuf *m = *m0;
545 struct pf_frcache *frp, *fra, *cur = NULL;
546 int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
547 u_int16_t off = ntohs(h->ip_off) << 3;
548 u_int16_t max = ip_len + off;
549 int hosed = 0;
550
551 KASSERT(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
552
553 /* Create a new range queue for this packet */
554 if (*frag == NULL) {
555 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
556 if (*frag == NULL) {
557 pf_flush_fragments();
558 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
559 if (*frag == NULL)
560 goto no_mem;
561 }
562
563 /* Get an entry for the queue */
564 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
565 if (cur == NULL) {
566 pool_put(&pf_cache_pl, *frag);
567 *frag = NULL;
568 goto no_mem;
569 }
570 pf_ncache++;
571
572 (*frag)->fr_flags = PFFRAG_NOBUFFER;
573 (*frag)->fr_max = 0;
574 (*frag)->fr_src = h->ip_src;
575 (*frag)->fr_dst = h->ip_dst;
576 (*frag)->fr_p = h->ip_p;
577 (*frag)->fr_id = h->ip_id;
578 (*frag)->fr_timeout = time_second;
579
580 cur->fr_off = off;
581 cur->fr_end = max;
582 LIST_INIT(&(*frag)->fr_cache);
583 LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
584
585 RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag);
586 TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next);
587
588 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
589
590 goto pass;
591 }
592
593 /*
594 * Find a fragment after the current one:
595 * - off contains the real shifted offset.
596 */
597 frp = NULL;
598 LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
599 if (fra->fr_off > off)
600 break;
601 frp = fra;
602 }
603
604 KASSERT(frp != NULL || fra != NULL);
605
606 if (frp != NULL) {
607 int precut;
608
609 precut = frp->fr_end - off;
610 if (precut >= ip_len) {
611 /* Fragment is entirely a duplicate */
612 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
613 h->ip_id, frp->fr_off, frp->fr_end, off, max));
614 goto drop_fragment;
615 }
616 if (precut == 0) {
617 /* They are adjacent. Fixup cache entry */
618 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
619 h->ip_id, frp->fr_off, frp->fr_end, off, max));
620 frp->fr_end = max;
621 } else if (precut > 0) {
622 /* The first part of this payload overlaps with a
623 * fragment that has already been passed.
624 * Need to trim off the first part of the payload.
625 * But to do so easily, we need to create another
626 * mbuf to throw the original header into.
627 */
628
629 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
630 h->ip_id, precut, frp->fr_off, frp->fr_end, off,
631 max));
632
633 off += precut;
634 max -= precut;
635 /* Update the previous frag to encompass this one */
636 frp->fr_end = max;
637
638 if (!drop) {
639 /* XXX Optimization opportunity
640 * This is a very heavy way to trim the payload.
641 * we could do it much faster by diddling mbuf
642 * internals but that would be even less legible
643 * than this mbuf magic. For my next trick,
644 * I'll pull a rabbit out of my laptop.
645 */
646 #ifdef __OpenBSD__
647 *m0 = m_copym2(m, 0, h->ip_hl << 2, M_NOWAIT);
648 #else
649 *m0 = m_dup(m, 0, h->ip_hl << 2, M_NOWAIT);
650 #endif
651 if (*m0 == NULL)
652 goto no_mem;
653 KASSERT((*m0)->m_next == NULL);
654 m_adj(m, precut + (h->ip_hl << 2));
655 m_cat(*m0, m);
656 m = *m0;
657 if (m->m_flags & M_PKTHDR) {
658 int plen = 0;
659 struct mbuf *t;
660 for (t = m; t; t = t->m_next)
661 plen += t->m_len;
662 m->m_pkthdr.len = plen;
663 }
664
665
666 h = mtod(m, struct ip *);
667
668
669 KASSERT((int)m->m_len ==
670 ntohs(h->ip_len) - precut);
671 h->ip_off = htons(ntohs(h->ip_off) +
672 (precut >> 3));
673 h->ip_len = htons(ntohs(h->ip_len) - precut);
674 } else {
675 hosed++;
676 }
677 } else {
678 /* There is a gap between fragments */
679
680 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
681 h->ip_id, -precut, frp->fr_off, frp->fr_end, off,
682 max));
683
684 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
685 if (cur == NULL)
686 goto no_mem;
687 pf_ncache++;
688
689 cur->fr_off = off;
690 cur->fr_end = max;
691 LIST_INSERT_AFTER(frp, cur, fr_next);
692 }
693 }
694
695 if (fra != NULL) {
696 int aftercut;
697 int merge = 0;
698
699 aftercut = max - fra->fr_off;
700 if (aftercut == 0) {
701 /* Adjacent fragments */
702 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
703 h->ip_id, off, max, fra->fr_off, fra->fr_end));
704 fra->fr_off = off;
705 merge = 1;
706 } else if (aftercut > 0) {
707 /* Need to chop off the tail of this fragment */
708 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
709 h->ip_id, aftercut, off, max, fra->fr_off,
710 fra->fr_end));
711 fra->fr_off = off;
712 max -= aftercut;
713
714 merge = 1;
715
716 if (!drop) {
717 m_adj(m, -aftercut);
718 if (m->m_flags & M_PKTHDR) {
719 int plen = 0;
720 struct mbuf *t;
721 for (t = m; t; t = t->m_next)
722 plen += t->m_len;
723 m->m_pkthdr.len = plen;
724 }
725 h = mtod(m, struct ip *);
726 KASSERT((int)m->m_len ==
727 ntohs(h->ip_len) - aftercut);
728 h->ip_len = htons(ntohs(h->ip_len) - aftercut);
729 } else {
730 hosed++;
731 }
732 } else {
733 /* There is a gap between fragments */
734 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
735 h->ip_id, -aftercut, off, max, fra->fr_off,
736 fra->fr_end));
737
738 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
739 if (cur == NULL)
740 goto no_mem;
741 pf_ncache++;
742
743 cur->fr_off = off;
744 cur->fr_end = max;
745 LIST_INSERT_BEFORE(fra, cur, fr_next);
746 }
747
748
749 /* Need to glue together two separate fragment descriptors */
750 if (merge) {
751 if (cur && fra->fr_off <= cur->fr_end) {
752 /* Need to merge in a previous 'cur' */
753 DPFPRINTF(("fragcache[%d]: adjacent(merge "
754 "%d-%d) %d-%d (%d-%d)\n",
755 h->ip_id, cur->fr_off, cur->fr_end, off,
756 max, fra->fr_off, fra->fr_end));
757 fra->fr_off = cur->fr_off;
758 LIST_REMOVE(cur, fr_next);
759 pool_put(&pf_cent_pl, cur);
760 pf_ncache--;
761 cur = NULL;
762
763 } else if (frp && fra->fr_off <= frp->fr_end) {
764 /* Need to merge in a modified 'frp' */
765 KASSERT(cur == NULL);
766 DPFPRINTF(("fragcache[%d]: adjacent(merge "
767 "%d-%d) %d-%d (%d-%d)\n",
768 h->ip_id, frp->fr_off, frp->fr_end, off,
769 max, fra->fr_off, fra->fr_end));
770 fra->fr_off = frp->fr_off;
771 LIST_REMOVE(frp, fr_next);
772 pool_put(&pf_cent_pl, frp);
773 pf_ncache--;
774 frp = NULL;
775
776 }
777 }
778 }
779
780 if (hosed) {
781 /*
782 * We must keep tracking the overall fragment even when
783 * we're going to drop it anyway so that we know when to
784 * free the overall descriptor. Thus we drop the frag late.
785 */
786 goto drop_fragment;
787 }
788
789
790 pass:
791 /* Update maximum data size */
792 if ((*frag)->fr_max < max)
793 (*frag)->fr_max = max;
794
795 /* This is the last segment */
796 if (!mff)
797 (*frag)->fr_flags |= PFFRAG_SEENLAST;
798
799 /* Check if we are completely reassembled */
800 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
801 LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
802 LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) {
803 /* Remove from fragment queue */
804 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
805 (*frag)->fr_max));
806 pf_free_fragment(*frag);
807 *frag = NULL;
808 }
809
810 return (m);
811
812 no_mem:
813 *nomem = 1;
814
815 /* Still need to pay attention to !IP_MF */
816 if (!mff && *frag != NULL)
817 (*frag)->fr_flags |= PFFRAG_SEENLAST;
818
819 m_freem(m);
820 return (NULL);
821
822 drop_fragment:
823
824 /* Still need to pay attention to !IP_MF */
825 if (!mff && *frag != NULL)
826 (*frag)->fr_flags |= PFFRAG_SEENLAST;
827
828 if (drop) {
829 /* This fragment has been deemed bad. Don't reass */
830 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
831 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
832 h->ip_id));
833 (*frag)->fr_flags |= PFFRAG_DROP;
834 }
835
836 m_freem(m);
837 return (NULL);
838 }
839
840 int
841 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason,
842 struct pf_pdesc *pd)
843 {
844 struct mbuf *m = *m0;
845 struct pf_rule *r;
846 struct pf_frent *frent;
847 struct pf_fragment *frag = NULL;
848 struct ip *h = mtod(m, struct ip *);
849 int mff = (ntohs(h->ip_off) & IP_MF);
850 int hlen = h->ip_hl << 2;
851 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
852 u_int16_t max;
853 int ip_len;
854 int ip_off;
855
856 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
857 while (r != NULL) {
858 r->evaluations++;
859 if (r->kif != NULL &&
860 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
861 r = r->skip[PF_SKIP_IFP].ptr;
862 else if (r->direction && r->direction != dir)
863 r = r->skip[PF_SKIP_DIR].ptr;
864 else if (r->af && r->af != AF_INET)
865 r = r->skip[PF_SKIP_AF].ptr;
866 else if (r->proto && r->proto != h->ip_p)
867 r = r->skip[PF_SKIP_PROTO].ptr;
868 else if (PF_MISMATCHAW(&r->src.addr,
869 (struct pf_addr *)&h->ip_src.s_addr, AF_INET, r->src.neg))
870 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
871 else if (PF_MISMATCHAW(&r->dst.addr,
872 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET, r->dst.neg))
873 r = r->skip[PF_SKIP_DST_ADDR].ptr;
874 else
875 break;
876 }
877
878 if (r == NULL)
879 return (PF_PASS);
880 else
881 r->packets++;
882
883 /* Check for illegal packets */
884 if (hlen < (int)sizeof(struct ip))
885 goto drop;
886
887 if (hlen > ntohs(h->ip_len))
888 goto drop;
889
890 /* Clear IP_DF if the rule uses the no-df option */
891 if (r->rule_flag & PFRULE_NODF)
892 h->ip_off &= htons(~IP_DF);
893
894 /* We will need other tests here */
895 if (!fragoff && !mff)
896 goto no_fragment;
897
898 /* We're dealing with a fragment now. Don't allow fragments
899 * with IP_DF to enter the cache. If the flag was cleared by
900 * no-df above, fine. Otherwise drop it.
901 */
902 if (h->ip_off & htons(IP_DF)) {
903 DPFPRINTF(("IP_DF\n"));
904 goto bad;
905 }
906
907 ip_len = ntohs(h->ip_len) - hlen;
908 ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
909
910 /* All fragments are 8 byte aligned */
911 if (mff && (ip_len & 0x7)) {
912 DPFPRINTF(("mff and %d\n", ip_len));
913 goto bad;
914 }
915
916 /* Respect maximum length */
917 if (fragoff + ip_len > IP_MAXPACKET) {
918 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
919 goto bad;
920 }
921 max = fragoff + ip_len;
922
923 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
924 /* Fully buffer all of the fragments */
925
926 frag = pf_find_fragment(h, &pf_frag_tree);
927
928 /* Check if we saw the last fragment already */
929 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
930 max > frag->fr_max)
931 goto bad;
932
933 /* Get an entry for the fragment queue */
934 frent = pool_get(&pf_frent_pl, PR_NOWAIT);
935 if (frent == NULL) {
936 REASON_SET(reason, PFRES_MEMORY);
937 return (PF_DROP);
938 }
939 pf_nfrents++;
940 frent->fr_ip = h;
941 frent->fr_m = m;
942
943 /* Might return a completely reassembled mbuf, or NULL */
944 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
945 *m0 = m = pf_reassemble(m0, &frag, frent, mff);
946
947 if (m == NULL)
948 return (PF_DROP);
949
950 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
951 goto drop;
952
953 h = mtod(m, struct ip *);
954 } else {
955 /* non-buffering fragment cache (drops or masks overlaps) */
956 int nomem = 0;
957
958 if (dir == PF_OUT) {
959 if (m_tag_find(m, PACKET_TAG_PF_FRAGCACHE, NULL) !=
960 NULL) {
961 /* Already passed the fragment cache in the
962 * input direction. If we continued, it would
963 * appear to be a dup and would be dropped.
964 */
965 goto fragment_pass;
966 }
967 }
968
969 frag = pf_find_fragment(h, &pf_cache_tree);
970
971 /* Check if we saw the last fragment already */
972 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
973 max > frag->fr_max) {
974 if (r->rule_flag & PFRULE_FRAGDROP)
975 frag->fr_flags |= PFFRAG_DROP;
976 goto bad;
977 }
978
979 *m0 = m = pf_fragcache(m0, h, &frag, mff,
980 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
981 if (m == NULL) {
982 if (nomem)
983 goto no_mem;
984 goto drop;
985 }
986
987 if (dir == PF_IN) {
988 struct m_tag *mtag;
989
990 mtag = m_tag_get(PACKET_TAG_PF_FRAGCACHE, 0, M_NOWAIT);
991 if (mtag == NULL)
992 goto no_mem;
993 m_tag_prepend(m, mtag);
994 }
995 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
996 goto drop;
997 goto fragment_pass;
998 }
999
1000 no_fragment:
1001 /* At this point, only IP_DF is allowed in ip_off */
1002 h->ip_off &= htons(IP_DF);
1003
1004 /* Enforce a minimum ttl, may cause endless packet loops */
1005 if (r->min_ttl && h->ip_ttl < r->min_ttl)
1006 h->ip_ttl = r->min_ttl;
1007
1008 if (r->rule_flag & PFRULE_RANDOMID) {
1009 u_int16_t ip_id = h->ip_id;
1010
1011 h->ip_id = ip_randomid();
1012 h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_id, h->ip_id, 0);
1013 }
1014 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1015 pd->flags |= PFDESC_IP_REAS;
1016
1017 return (PF_PASS);
1018
1019 fragment_pass:
1020 /* Enforce a minimum ttl, may cause endless packet loops */
1021 if (r->min_ttl && h->ip_ttl < r->min_ttl)
1022 h->ip_ttl = r->min_ttl;
1023 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0)
1024 pd->flags |= PFDESC_IP_REAS;
1025 return (PF_PASS);
1026
1027 no_mem:
1028 REASON_SET(reason, PFRES_MEMORY);
1029 if (r != NULL && r->log)
1030 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1031 return (PF_DROP);
1032
1033 drop:
1034 REASON_SET(reason, PFRES_NORM);
1035 if (r != NULL && r->log)
1036 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1037 return (PF_DROP);
1038
1039 bad:
1040 DPFPRINTF(("dropping bad fragment\n"));
1041
1042 /* Free associated fragments */
1043 if (frag != NULL)
1044 pf_free_fragment(frag);
1045
1046 REASON_SET(reason, PFRES_FRAG);
1047 if (r != NULL && r->log)
1048 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1049
1050 return (PF_DROP);
1051 }
1052
1053 #ifdef INET6
1054 int
1055 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1056 u_short *reason, struct pf_pdesc *pd)
1057 {
1058 struct mbuf *m = *m0;
1059 struct pf_rule *r;
1060 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1061 int off;
1062 struct ip6_ext ext;
1063 struct ip6_opt opt;
1064 struct ip6_opt_jumbo jumbo;
1065 struct ip6_frag frag;
1066 u_int32_t jumbolen = 0, plen;
1067 u_int16_t fragoff = 0;
1068 int optend;
1069 int ooff;
1070 u_int8_t proto;
1071 int terminal;
1072
1073 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1074 while (r != NULL) {
1075 r->evaluations++;
1076 if (r->kif != NULL &&
1077 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1078 r = r->skip[PF_SKIP_IFP].ptr;
1079 else if (r->direction && r->direction != dir)
1080 r = r->skip[PF_SKIP_DIR].ptr;
1081 else if (r->af && r->af != AF_INET6)
1082 r = r->skip[PF_SKIP_AF].ptr;
1083 #if 0 /* header chain! */
1084 else if (r->proto && r->proto != h->ip6_nxt)
1085 r = r->skip[PF_SKIP_PROTO].ptr;
1086 #endif
1087 else if (PF_MISMATCHAW(&r->src.addr,
1088 (struct pf_addr *)&h->ip6_src, AF_INET6, r->src.neg))
1089 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1090 else if (PF_MISMATCHAW(&r->dst.addr,
1091 (struct pf_addr *)&h->ip6_dst, AF_INET6, r->dst.neg))
1092 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1093 else
1094 break;
1095 }
1096
1097 if (r == NULL)
1098 return (PF_PASS);
1099 else
1100 r->packets++;
1101
1102 /* Check for illegal packets */
1103 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1104 goto drop;
1105
1106 off = sizeof(struct ip6_hdr);
1107 proto = h->ip6_nxt;
1108 terminal = 0;
1109 do {
1110 switch (proto) {
1111 case IPPROTO_FRAGMENT:
1112 goto fragment;
1113 break;
1114 case IPPROTO_AH:
1115 case IPPROTO_ROUTING:
1116 case IPPROTO_DSTOPTS:
1117 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1118 NULL, AF_INET6))
1119 goto shortpkt;
1120 if (proto == IPPROTO_AH)
1121 off += (ext.ip6e_len + 2) * 4;
1122 else
1123 off += (ext.ip6e_len + 1) * 8;
1124 proto = ext.ip6e_nxt;
1125 break;
1126 case IPPROTO_HOPOPTS:
1127 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1128 NULL, AF_INET6))
1129 goto shortpkt;
1130 optend = off + (ext.ip6e_len + 1) * 8;
1131 ooff = off + sizeof(ext);
1132 do {
1133 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1134 sizeof(opt.ip6o_type), NULL, NULL,
1135 AF_INET6))
1136 goto shortpkt;
1137 if (opt.ip6o_type == IP6OPT_PAD1) {
1138 ooff++;
1139 continue;
1140 }
1141 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1142 NULL, NULL, AF_INET6))
1143 goto shortpkt;
1144 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1145 goto drop;
1146 switch (opt.ip6o_type) {
1147 case IP6OPT_JUMBO:
1148 if (h->ip6_plen != 0)
1149 goto drop;
1150 if (!pf_pull_hdr(m, ooff, &jumbo,
1151 sizeof(jumbo), NULL, NULL,
1152 AF_INET6))
1153 goto shortpkt;
1154 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1155 sizeof(jumbolen));
1156 jumbolen = ntohl(jumbolen);
1157 if (jumbolen <= IPV6_MAXPACKET)
1158 goto drop;
1159 if (sizeof(struct ip6_hdr) + jumbolen !=
1160 m->m_pkthdr.len)
1161 goto drop;
1162 break;
1163 default:
1164 break;
1165 }
1166 ooff += sizeof(opt) + opt.ip6o_len;
1167 } while (ooff < optend);
1168
1169 off = optend;
1170 proto = ext.ip6e_nxt;
1171 break;
1172 default:
1173 terminal = 1;
1174 break;
1175 }
1176 } while (!terminal);
1177
1178 /* jumbo payload option must be present, or plen > 0 */
1179 if (ntohs(h->ip6_plen) == 0)
1180 plen = jumbolen;
1181 else
1182 plen = ntohs(h->ip6_plen);
1183 if (plen == 0)
1184 goto drop;
1185 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1186 goto shortpkt;
1187
1188 /* Enforce a minimum ttl, may cause endless packet loops */
1189 if (r->min_ttl && h->ip6_hlim < r->min_ttl)
1190 h->ip6_hlim = r->min_ttl;
1191
1192 return (PF_PASS);
1193
1194 fragment:
1195 if (ntohs(h->ip6_plen) == 0 || jumbolen)
1196 goto drop;
1197 plen = ntohs(h->ip6_plen);
1198
1199 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1200 goto shortpkt;
1201 fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
1202 if (fragoff + (plen - off - sizeof(frag)) > IPV6_MAXPACKET)
1203 goto badfrag;
1204
1205 /* do something about it */
1206 /* remember to set pd->flags |= PFDESC_IP_REAS */
1207 return (PF_PASS);
1208
1209 shortpkt:
1210 REASON_SET(reason, PFRES_SHORT);
1211 if (r != NULL && r->log)
1212 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1213 return (PF_DROP);
1214
1215 drop:
1216 REASON_SET(reason, PFRES_NORM);
1217 if (r != NULL && r->log)
1218 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1219 return (PF_DROP);
1220
1221 badfrag:
1222 REASON_SET(reason, PFRES_FRAG);
1223 if (r != NULL && r->log)
1224 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1225 return (PF_DROP);
1226 }
1227 #endif /* INET6 */
1228
1229 int
1230 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1231 int off, void *h, struct pf_pdesc *pd)
1232 {
1233 struct pf_rule *r, *rm = NULL;
1234 struct tcphdr *th = pd->hdr.tcp;
1235 int rewrite = 0;
1236 u_short reason;
1237 u_int8_t flags;
1238 sa_family_t af = pd->af;
1239
1240 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1241 while (r != NULL) {
1242 r->evaluations++;
1243 if (r->kif != NULL &&
1244 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1245 r = r->skip[PF_SKIP_IFP].ptr;
1246 else if (r->direction && r->direction != dir)
1247 r = r->skip[PF_SKIP_DIR].ptr;
1248 else if (r->af && r->af != af)
1249 r = r->skip[PF_SKIP_AF].ptr;
1250 else if (r->proto && r->proto != pd->proto)
1251 r = r->skip[PF_SKIP_PROTO].ptr;
1252 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.neg))
1253 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1254 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1255 r->src.port[0], r->src.port[1], th->th_sport))
1256 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1257 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.neg))
1258 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1259 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1260 r->dst.port[0], r->dst.port[1], th->th_dport))
1261 r = r->skip[PF_SKIP_DST_PORT].ptr;
1262 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1263 pf_osfp_fingerprint(pd, m, off, th),
1264 r->os_fingerprint))
1265 r = TAILQ_NEXT(r, entries);
1266 else {
1267 rm = r;
1268 break;
1269 }
1270 }
1271
1272 if (rm == NULL)
1273 return (PF_PASS);
1274 else
1275 r->packets++;
1276
1277 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1278 pd->flags |= PFDESC_TCP_NORM;
1279
1280 flags = th->th_flags;
1281 if (flags & TH_SYN) {
1282 /* Illegal packet */
1283 if (flags & TH_RST)
1284 goto tcp_drop;
1285
1286 if (flags & TH_FIN)
1287 flags &= ~TH_FIN;
1288 } else {
1289 /* Illegal packet */
1290 if (!(flags & (TH_ACK|TH_RST)))
1291 goto tcp_drop;
1292 }
1293
1294 if (!(flags & TH_ACK)) {
1295 /* These flags are only valid if ACK is set */
1296 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1297 goto tcp_drop;
1298 }
1299
1300 /* Check for illegal header length */
1301 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1302 goto tcp_drop;
1303
1304 /* If flags changed, or reserved data set, then adjust */
1305 if (flags != th->th_flags || th->th_x2 != 0) {
1306 u_int16_t ov, nv;
1307
1308 ov = *(u_int16_t *)(&th->th_ack + 1);
1309 th->th_flags = flags;
1310 th->th_x2 = 0;
1311 nv = *(u_int16_t *)(&th->th_ack + 1);
1312
1313 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv, 0);
1314 rewrite = 1;
1315 }
1316
1317 /* Remove urgent pointer, if TH_URG is not set */
1318 if (!(flags & TH_URG) && th->th_urp) {
1319 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0, 0);
1320 th->th_urp = 0;
1321 rewrite = 1;
1322 }
1323
1324 /* Process options */
1325 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off))
1326 rewrite = 1;
1327
1328 /* copy back packet headers if we sanitized */
1329 if (rewrite)
1330 m_copyback(m, off, sizeof(*th), th);
1331
1332 return (PF_PASS);
1333
1334 tcp_drop:
1335 REASON_SET(&reason, PFRES_NORM);
1336 if (rm != NULL && r->log)
1337 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL);
1338 return (PF_DROP);
1339 }
1340
1341 int
1342 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1343 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1344 {
1345 u_int32_t tsval, tsecr;
1346 u_int8_t hdr[60];
1347 u_int8_t *opt;
1348
1349 KASSERT(src->scrub == NULL);
1350
1351 src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
1352 if (src->scrub == NULL)
1353 return (1);
1354 bzero(src->scrub, sizeof(*src->scrub));
1355
1356 switch (pd->af) {
1357 #ifdef INET
1358 case AF_INET: {
1359 struct ip *h = mtod(m, struct ip *);
1360 src->scrub->pfss_ttl = h->ip_ttl;
1361 break;
1362 }
1363 #endif /* INET */
1364 #ifdef INET6
1365 case AF_INET6: {
1366 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1367 src->scrub->pfss_ttl = h->ip6_hlim;
1368 break;
1369 }
1370 #endif /* INET6 */
1371 }
1372
1373
1374 /*
1375 * All normalizations below are only begun if we see the start of
1376 * the connections. They must all set an enabled bit in pfss_flags
1377 */
1378 if ((th->th_flags & TH_SYN) == 0)
1379 return (0);
1380
1381
1382 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1383 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1384 /* Diddle with TCP options */
1385 int hlen;
1386 opt = hdr + sizeof(struct tcphdr);
1387 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1388 while (hlen >= TCPOLEN_TIMESTAMP) {
1389 switch (*opt) {
1390 case TCPOPT_EOL: /* FALLTHROUGH */
1391 case TCPOPT_NOP:
1392 opt++;
1393 hlen--;
1394 break;
1395 case TCPOPT_TIMESTAMP:
1396 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1397 src->scrub->pfss_flags |=
1398 PFSS_TIMESTAMP;
1399 src->scrub->pfss_ts_mod =
1400 htonl(arc4random());
1401
1402 /* note PFSS_PAWS not set yet */
1403 memcpy(&tsval, &opt[2],
1404 sizeof(u_int32_t));
1405 memcpy(&tsecr, &opt[6],
1406 sizeof(u_int32_t));
1407 src->scrub->pfss_tsval0 = ntohl(tsval);
1408 src->scrub->pfss_tsval = ntohl(tsval);
1409 src->scrub->pfss_tsecr = ntohl(tsecr);
1410 getmicrouptime(&src->scrub->pfss_last);
1411 }
1412 /* FALLTHROUGH */
1413 default:
1414 hlen -= MAX(opt[1], 2);
1415 opt += MAX(opt[1], 2);
1416 break;
1417 }
1418 }
1419 }
1420
1421 return (0);
1422 }
1423
1424 void
1425 pf_normalize_tcp_cleanup(struct pf_state *state)
1426 {
1427 if (state->src.scrub)
1428 pool_put(&pf_state_scrub_pl, state->src.scrub);
1429 if (state->dst.scrub)
1430 pool_put(&pf_state_scrub_pl, state->dst.scrub);
1431
1432 /* Someday... flush the TCP segment reassembly descriptors. */
1433 }
1434
1435 int
1436 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1437 u_short *reason, struct tcphdr *th, struct pf_state *state,
1438 struct pf_state_peer *src, struct pf_state_peer *dst, int *writeback)
1439 {
1440 struct timeval uptime;
1441 u_int32_t tsval, tsecr;
1442 u_int tsval_from_last;
1443 u_int8_t hdr[60];
1444 u_int8_t *opt;
1445 int copyback = 0;
1446 int got_ts = 0;
1447
1448 KASSERT(src->scrub || dst->scrub);
1449
1450 /*
1451 * Enforce the minimum TTL seen for this connection. Negate a common
1452 * technique to evade an intrusion detection system and confuse
1453 * firewall state code.
1454 */
1455 switch (pd->af) {
1456 #ifdef INET
1457 case AF_INET: {
1458 if (src->scrub) {
1459 struct ip *h = mtod(m, struct ip *);
1460 if (h->ip_ttl > src->scrub->pfss_ttl)
1461 src->scrub->pfss_ttl = h->ip_ttl;
1462 h->ip_ttl = src->scrub->pfss_ttl;
1463 }
1464 break;
1465 }
1466 #endif /* INET */
1467 #ifdef INET6
1468 case AF_INET6: {
1469 if (src->scrub) {
1470 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1471 if (h->ip6_hlim > src->scrub->pfss_ttl)
1472 src->scrub->pfss_ttl = h->ip6_hlim;
1473 h->ip6_hlim = src->scrub->pfss_ttl;
1474 }
1475 break;
1476 }
1477 #endif /* INET6 */
1478 }
1479
1480 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1481 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1482 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1483 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1484 /* Diddle with TCP options */
1485 int hlen;
1486 opt = hdr + sizeof(struct tcphdr);
1487 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1488 while (hlen >= TCPOLEN_TIMESTAMP) {
1489 switch (*opt) {
1490 case TCPOPT_EOL: /* FALLTHROUGH */
1491 case TCPOPT_NOP:
1492 opt++;
1493 hlen--;
1494 break;
1495 case TCPOPT_TIMESTAMP:
1496 /* Modulate the timestamps. Can be used for
1497 * NAT detection, OS uptime determination or
1498 * reboot detection.
1499 */
1500
1501 if (got_ts) {
1502 /* Huh? Multiple timestamps!? */
1503 if (pf_status.debug >= PF_DEBUG_MISC) {
1504 DPFPRINTF(("multiple TS??"));
1505 pf_print_state(state);
1506 printf("\n");
1507 }
1508 REASON_SET(reason, PFRES_TS);
1509 return (PF_DROP);
1510 }
1511 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1512 memcpy(&tsval, &opt[2],
1513 sizeof(u_int32_t));
1514 if (tsval && src->scrub &&
1515 (src->scrub->pfss_flags &
1516 PFSS_TIMESTAMP)) {
1517 tsval = ntohl(tsval);
1518 pf_change_a(&opt[2],
1519 &th->th_sum,
1520 htonl(tsval +
1521 src->scrub->pfss_ts_mod),
1522 0);
1523 copyback = 1;
1524 }
1525
1526 /* Modulate TS reply iff valid (!0) */
1527 memcpy(&tsecr, &opt[6],
1528 sizeof(u_int32_t));
1529 if (tsecr && dst->scrub &&
1530 (dst->scrub->pfss_flags &
1531 PFSS_TIMESTAMP)) {
1532 tsecr = ntohl(tsecr)
1533 - dst->scrub->pfss_ts_mod;
1534 pf_change_a(&opt[6],
1535 &th->th_sum, htonl(tsecr),
1536 0);
1537 copyback = 1;
1538 }
1539 got_ts = 1;
1540 }
1541 /* FALLTHROUGH */
1542 default:
1543 hlen -= MAX(opt[1], 2);
1544 opt += MAX(opt[1], 2);
1545 break;
1546 }
1547 }
1548 if (copyback) {
1549 /* Copyback the options, caller copys back header */
1550 *writeback = 1;
1551 m_copyback(m, off + sizeof(struct tcphdr),
1552 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1553 sizeof(struct tcphdr));
1554 }
1555 }
1556
1557
1558 /*
1559 * Must invalidate PAWS checks on connections idle for too long.
1560 * The fastest allowed timestamp clock is 1ms. That turns out to
1561 * be about 24 days before it wraps. XXX Right now our lowerbound
1562 * TS echo check only works for the first 12 days of a connection
1563 * when the TS has exhausted half its 32bit space
1564 */
1565 #define TS_MAX_IDLE (24*24*60*60)
1566 #define TS_MAX_CONN (12*24*60*60) /* XXX remove when better tsecr check */
1567
1568 getmicrouptime(&uptime);
1569 if (src->scrub && (src->scrub->pfss_flags & PFSS_PAWS) &&
1570 (uptime.tv_sec - src->scrub->pfss_last.tv_sec > TS_MAX_IDLE ||
1571 time_second - state->creation > TS_MAX_CONN)) {
1572 if (pf_status.debug >= PF_DEBUG_MISC) {
1573 DPFPRINTF(("src idled out of PAWS\n"));
1574 pf_print_state(state);
1575 printf("\n");
1576 }
1577 src->scrub->pfss_flags = (src->scrub->pfss_flags & ~PFSS_PAWS)
1578 | PFSS_PAWS_IDLED;
1579 }
1580 if (dst->scrub && (dst->scrub->pfss_flags & PFSS_PAWS) &&
1581 uptime.tv_sec - dst->scrub->pfss_last.tv_sec > TS_MAX_IDLE) {
1582 if (pf_status.debug >= PF_DEBUG_MISC) {
1583 DPFPRINTF(("dst idled out of PAWS\n"));
1584 pf_print_state(state);
1585 printf("\n");
1586 }
1587 dst->scrub->pfss_flags = (dst->scrub->pfss_flags & ~PFSS_PAWS)
1588 | PFSS_PAWS_IDLED;
1589 }
1590
1591 if (got_ts && src->scrub && dst->scrub &&
1592 (src->scrub->pfss_flags & PFSS_PAWS) &&
1593 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1594 /* Validate that the timestamps are "in-window".
1595 * RFC1323 describes TCP Timestamp options that allow
1596 * measurement of RTT (round trip time) and PAWS
1597 * (protection against wrapped sequence numbers). PAWS
1598 * gives us a set of rules for rejecting packets on
1599 * long fat pipes (packets that were somehow delayed
1600 * in transit longer than the time it took to send the
1601 * full TCP sequence space of 4Gb). We can use these
1602 * rules and infer a few others that will let us treat
1603 * the 32bit timestamp and the 32bit echoed timestamp
1604 * as sequence numbers to prevent a blind attacker from
1605 * inserting packets into a connection.
1606 *
1607 * RFC1323 tells us:
1608 * - The timestamp on this packet must be greater than
1609 * or equal to the last value echoed by the other
1610 * endpoint. The RFC says those will be discarded
1611 * since it is a dup that has already been acked.
1612 * This gives us a lowerbound on the timestamp.
1613 * timestamp >= other last echoed timestamp
1614 * - The timestamp will be less than or equal to
1615 * the last timestamp plus the time between the
1616 * last packet and now. The RFC defines the max
1617 * clock rate as 1ms. We will allow clocks to be
1618 * up to 10% fast and will allow a total difference
1619 * or 30 seconds due to a route change. And this
1620 * gives us an upperbound on the timestamp.
1621 * timestamp <= last timestamp + max ticks
1622 * We have to be careful here. Windows will send an
1623 * initial timestamp of zero and then initialize it
1624 * to a random value after the 3whs; presumably to
1625 * avoid a DoS by having to call an expensive RNG
1626 * during a SYN flood. Proof MS has at least one
1627 * good security geek.
1628 *
1629 * - The TCP timestamp option must also echo the other
1630 * endpoints timestamp. The timestamp echoed is the
1631 * one carried on the earliest unacknowledged segment
1632 * on the left edge of the sequence window. The RFC
1633 * states that the host will reject any echoed
1634 * timestamps that were larger than any ever sent.
1635 * This gives us an upperbound on the TS echo.
1636 * tescr <= largest_tsval
1637 * - The lowerbound on the TS echo is a little more
1638 * tricky to determine. The other endpoint's echoed
1639 * values will not decrease. But there may be
1640 * network conditions that re-order packets and
1641 * cause our view of them to decrease. For now the
1642 * only lowerbound we can safely determine is that
1643 * the TS echo will never be less than the orginal
1644 * TS. XXX There is probably a better lowerbound.
1645 * Remove TS_MAX_CONN with better lowerbound check.
1646 * tescr >= other original TS
1647 *
1648 * It is also important to note that the fastest
1649 * timestamp clock of 1ms will wrap its 32bit space in
1650 * 24 days. So we just disable TS checking after 24
1651 * days of idle time. We actually must use a 12d
1652 * connection limit until we can come up with a better
1653 * lowerbound to the TS echo check.
1654 */
1655 struct timeval delta_ts;
1656 int ts_fudge;
1657
1658
1659 /*
1660 * PFTM_TS_DIFF is how many seconds of leeway to allow
1661 * a host's timestamp. This can happen if the previous
1662 * packet got delayed in transit for much longer than
1663 * this packet.
1664 */
1665 if ((ts_fudge = state->rule.ptr->timeout[PFTM_TS_DIFF]) == 0)
1666 ts_fudge = pf_default_rule.timeout[PFTM_TS_DIFF];
1667
1668
1669 /* Calculate max ticks since the last timestamp */
1670 #define TS_MAXFREQ 1100 /* RFC max TS freq of 1Khz + 10% skew */
1671 #define TS_MICROSECS 1000000 /* microseconds per second */
1672 timersub(&uptime, &src->scrub->pfss_last, &delta_ts);
1673 tsval_from_last = (delta_ts.tv_sec + ts_fudge) * TS_MAXFREQ;
1674 tsval_from_last += delta_ts.tv_usec / (TS_MICROSECS/TS_MAXFREQ);
1675
1676
1677 if ((src->state >= TCPS_ESTABLISHED &&
1678 dst->state >= TCPS_ESTABLISHED) &&
1679 (SEQ_LT(tsval, dst->scrub->pfss_tsecr) ||
1680 SEQ_GT(tsval, src->scrub->pfss_tsval + tsval_from_last) ||
1681 (tsecr && (SEQ_GT(tsecr, dst->scrub->pfss_tsval) ||
1682 SEQ_LT(tsecr, dst->scrub->pfss_tsval0))))) {
1683 /* Bad RFC1323 implementation or an insertion attack.
1684 *
1685 * - Solaris 2.6 and 2.7 are known to send another ACK
1686 * after the FIN,FIN|ACK,ACK closing that carries
1687 * an old timestamp.
1688 */
1689
1690 DPFPRINTF(("Timestamp failed %c%c%c%c\n",
1691 SEQ_LT(tsval, dst->scrub->pfss_tsecr) ? '0' : ' ',
1692 SEQ_GT(tsval, src->scrub->pfss_tsval +
1693 tsval_from_last) ? '1' : ' ',
1694 SEQ_GT(tsecr, dst->scrub->pfss_tsval) ? '2' : ' ',
1695 SEQ_LT(tsecr, dst->scrub->pfss_tsval0)? '3' : ' '));
1696 DPFPRINTF((" tsval: %" PRIu32 " tsecr: %" PRIu32
1697 " +ticks: %" PRIu32 " idle: %lus %lums\n",
1698 tsval, tsecr, tsval_from_last, delta_ts.tv_sec,
1699 delta_ts.tv_usec / 1000));
1700 DPFPRINTF((" src->tsval: %" PRIu32 " tsecr: %" PRIu32
1701 "\n",
1702 src->scrub->pfss_tsval, src->scrub->pfss_tsecr));
1703 DPFPRINTF((" dst->tsval: %" PRIu32 " tsecr: %" PRIu32
1704 " tsval0: %" PRIu32 "\n",
1705 dst->scrub->pfss_tsval,
1706 dst->scrub->pfss_tsecr, dst->scrub->pfss_tsval0));
1707 if (pf_status.debug >= PF_DEBUG_MISC) {
1708 pf_print_state(state);
1709 pf_print_flags(th->th_flags);
1710 printf("\n");
1711 }
1712 REASON_SET(reason, PFRES_TS);
1713 return (PF_DROP);
1714 }
1715
1716 /* XXX I'd really like to require tsecr but it's optional */
1717
1718 } else if (!got_ts && (th->th_flags & TH_RST) == 0 &&
1719 ((src->state == TCPS_ESTABLISHED && dst->state == TCPS_ESTABLISHED)
1720 || pd->p_len > 0 || (th->th_flags & TH_SYN)) &&
1721 src->scrub && dst->scrub &&
1722 (src->scrub->pfss_flags & PFSS_PAWS) &&
1723 (dst->scrub->pfss_flags & PFSS_PAWS)) {
1724 /* Didn't send a timestamp. Timestamps aren't really useful
1725 * when:
1726 * - connection opening or closing (often not even sent).
1727 * but we must not let an attacker to put a FIN on a
1728 * data packet to sneak it through our ESTABLISHED check.
1729 * - on a TCP reset. RFC suggests not even looking at TS.
1730 * - on an empty ACK. The TS will not be echoed so it will
1731 * probably not help keep the RTT calculation in sync and
1732 * there isn't as much danger when the sequence numbers
1733 * got wrapped. So some stacks don't include TS on empty
1734 * ACKs :-(
1735 *
1736 * To minimize the disruption to mostly RFC1323 conformant
1737 * stacks, we will only require timestamps on data packets.
1738 *
1739 * And what do ya know, we cannot require timestamps on data
1740 * packets. There appear to be devices that do legitimate
1741 * TCP connection hijacking. There are HTTP devices that allow
1742 * a 3whs (with timestamps) and then buffer the HTTP request.
1743 * If the intermediate device has the HTTP response cache, it
1744 * will spoof the response but not bother timestamping its
1745 * packets. So we can look for the presence of a timestamp in
1746 * the first data packet and if there, require it in all future
1747 * packets.
1748 */
1749
1750 if (pd->p_len > 0 && (src->scrub->pfss_flags & PFSS_DATA_TS)) {
1751 /*
1752 * Hey! Someone tried to sneak a packet in. Or the
1753 * stack changed its RFC1323 behavior?!?!
1754 */
1755 if (pf_status.debug >= PF_DEBUG_MISC) {
1756 DPFPRINTF(("Did not receive expected RFC1323 "
1757 "timestamp\n"));
1758 pf_print_state(state);
1759 pf_print_flags(th->th_flags);
1760 printf("\n");
1761 }
1762 REASON_SET(reason, PFRES_TS);
1763 return (PF_DROP);
1764 }
1765 }
1766
1767
1768 /*
1769 * We will note if a host sends his data packets with or without
1770 * timestamps. And require all data packets to contain a timestamp
1771 * if the first does. PAWS implicitly requires that all data packets be
1772 * timestamped. But I think there are middle-man devices that hijack
1773 * TCP streams immedietly after the 3whs and don't timestamp their
1774 * packets (seen in a WWW accelerator or cache).
1775 */
1776 if (pd->p_len > 0 && src->scrub && (src->scrub->pfss_flags &
1777 (PFSS_TIMESTAMP|PFSS_DATA_TS|PFSS_DATA_NOTS)) == PFSS_TIMESTAMP) {
1778 if (got_ts)
1779 src->scrub->pfss_flags |= PFSS_DATA_TS;
1780 else {
1781 src->scrub->pfss_flags |= PFSS_DATA_NOTS;
1782 if (pf_status.debug >= PF_DEBUG_MISC && dst->scrub &&
1783 (dst->scrub->pfss_flags & PFSS_TIMESTAMP)) {
1784 /* Don't warn if other host rejected RFC1323 */
1785 DPFPRINTF(("Broken RFC1323 stack did not "
1786 "timestamp data packet. Disabled PAWS "
1787 "security.\n"));
1788 pf_print_state(state);
1789 pf_print_flags(th->th_flags);
1790 printf("\n");
1791 }
1792 }
1793 }
1794
1795
1796 /*
1797 * Update PAWS values
1798 */
1799 if (got_ts && src->scrub && PFSS_TIMESTAMP == (src->scrub->pfss_flags &
1800 (PFSS_PAWS_IDLED|PFSS_TIMESTAMP))) {
1801 getmicrouptime(&src->scrub->pfss_last);
1802 if (SEQ_GEQ(tsval, src->scrub->pfss_tsval) ||
1803 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1804 src->scrub->pfss_tsval = tsval;
1805
1806 if (tsecr) {
1807 if (SEQ_GEQ(tsecr, src->scrub->pfss_tsecr) ||
1808 (src->scrub->pfss_flags & PFSS_PAWS) == 0)
1809 src->scrub->pfss_tsecr = tsecr;
1810
1811 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0 &&
1812 (SEQ_LT(tsval, src->scrub->pfss_tsval0) ||
1813 src->scrub->pfss_tsval0 == 0)) {
1814 /* tsval0 MUST be the lowest timestamp */
1815 src->scrub->pfss_tsval0 = tsval;
1816 }
1817
1818 /* Only fully initialized after a TS gets echoed */
1819 if ((src->scrub->pfss_flags & PFSS_PAWS) == 0)
1820 src->scrub->pfss_flags |= PFSS_PAWS;
1821 }
1822 }
1823
1824 /* I have a dream.... TCP segment reassembly.... */
1825 return (0);
1826 }
1827
1828 int
1829 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
1830 int off)
1831 {
1832 u_int16_t *mss;
1833 int thoff;
1834 int opt, cnt, optlen = 0;
1835 int rewrite = 0;
1836 u_char *optp;
1837
1838 thoff = th->th_off << 2;
1839 cnt = thoff - sizeof(struct tcphdr);
1840 optp = mtod(m, caddr_t) + off + sizeof(struct tcphdr);
1841
1842 for (; cnt > 0; cnt -= optlen, optp += optlen) {
1843 opt = optp[0];
1844 if (opt == TCPOPT_EOL)
1845 break;
1846 if (opt == TCPOPT_NOP)
1847 optlen = 1;
1848 else {
1849 if (cnt < 2)
1850 break;
1851 optlen = optp[1];
1852 if (optlen < 2 || optlen > cnt)
1853 break;
1854 }
1855 switch (opt) {
1856 case TCPOPT_MAXSEG:
1857 mss = (u_int16_t *)(optp + 2);
1858 if ((ntohs(*mss)) > r->max_mss) {
1859 th->th_sum = pf_cksum_fixup(th->th_sum,
1860 *mss, htons(r->max_mss), 0);
1861 *mss = htons(r->max_mss);
1862 rewrite = 1;
1863 }
1864 break;
1865 default:
1866 break;
1867 }
1868 }
1869
1870 return (rewrite);
1871 }
1872