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