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