pf_norm.c revision 1.5 1 /* $NetBSD: pf_norm.c,v 1.5 2004/11/13 21:13:07 yamt Exp $ */
2 /* $OpenBSD: pf_norm.c,v 1.80 2004/03/09 21:44:41 mcbride 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 u_int16_t pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t);
127 int pf_normalize_tcpopt(struct pf_rule *, struct mbuf *,
128 struct tcphdr *, int);
129
130 #define DPFPRINTF(x) if (pf_status.debug >= PF_DEBUG_MISC) \
131 { printf("%s: ", __func__); printf x ;}
132
133 /* Globals */
134 struct pool pf_frent_pl, pf_frag_pl, pf_cache_pl, pf_cent_pl;
135 struct pool pf_state_scrub_pl;
136 int pf_nfrents, pf_ncache;
137
138 void
139 pf_normalize_init(void)
140 {
141 pool_init(&pf_frent_pl, sizeof(struct pf_frent), 0, 0, 0, "pffrent",
142 NULL);
143 pool_init(&pf_frag_pl, sizeof(struct pf_fragment), 0, 0, 0, "pffrag",
144 NULL);
145 pool_init(&pf_cache_pl, sizeof(struct pf_fragment), 0, 0, 0,
146 "pffrcache", NULL);
147 pool_init(&pf_cent_pl, sizeof(struct pf_frcache), 0, 0, 0, "pffrcent",
148 NULL);
149 pool_init(&pf_state_scrub_pl, sizeof(struct pf_state_scrub), 0, 0, 0,
150 "pfstscr", NULL);
151
152 pool_sethiwat(&pf_frag_pl, PFFRAG_FRAG_HIWAT);
153 pool_sethardlimit(&pf_frent_pl, PFFRAG_FRENT_HIWAT, NULL, 0);
154 pool_sethardlimit(&pf_cache_pl, PFFRAG_FRCACHE_HIWAT, NULL, 0);
155 pool_sethardlimit(&pf_cent_pl, PFFRAG_FRCENT_HIWAT, NULL, 0);
156
157 TAILQ_INIT(&pf_fragqueue);
158 TAILQ_INIT(&pf_cachequeue);
159 }
160
161 #ifdef _LKM
162 #define TAILQ_DRAIN(list, element) \
163 do { \
164 while ((element = TAILQ_FIRST(list)) != NULL) \
165 TAILQ_REMOVE(list, element, frag_next); \
166 } while (0)
167
168 void
169 pf_normalize_destroy(void)
170 {
171 struct pf_fragment *fragment_e;
172
173 TAILQ_DRAIN(&pf_fragqueue, fragment_e);
174 TAILQ_DRAIN(&pf_cachequeue, fragment_e);
175
176 pool_destroy(&pf_state_scrub_pl);
177 pool_destroy(&pf_cent_pl);
178 pool_destroy(&pf_cache_pl);
179 pool_destroy(&pf_frag_pl);
180 pool_destroy(&pf_frent_pl);
181 }
182 #endif
183
184 static __inline int
185 pf_frag_compare(struct pf_fragment *a, struct pf_fragment *b)
186 {
187 int diff;
188
189 if ((diff = a->fr_id - b->fr_id))
190 return (diff);
191 else if ((diff = a->fr_p - b->fr_p))
192 return (diff);
193 else if (a->fr_src.s_addr < b->fr_src.s_addr)
194 return (-1);
195 else if (a->fr_src.s_addr > b->fr_src.s_addr)
196 return (1);
197 else if (a->fr_dst.s_addr < b->fr_dst.s_addr)
198 return (-1);
199 else if (a->fr_dst.s_addr > b->fr_dst.s_addr)
200 return (1);
201 return (0);
202 }
203
204 void
205 pf_purge_expired_fragments(void)
206 {
207 struct pf_fragment *frag;
208 u_int32_t expire = time.tv_sec -
209 pf_default_rule.timeout[PFTM_FRAG];
210
211 while ((frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue)) != NULL) {
212 KASSERT(BUFFER_FRAGMENTS(frag));
213 if (frag->fr_timeout > expire)
214 break;
215
216 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
217 pf_free_fragment(frag);
218 }
219
220 while ((frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue)) != NULL) {
221 KASSERT(!BUFFER_FRAGMENTS(frag));
222 if (frag->fr_timeout > expire)
223 break;
224
225 DPFPRINTF(("expiring %d(%p)\n", frag->fr_id, frag));
226 pf_free_fragment(frag);
227 KASSERT(TAILQ_EMPTY(&pf_cachequeue) ||
228 TAILQ_LAST(&pf_cachequeue, pf_cachequeue) != frag);
229 }
230 }
231
232 /*
233 * Try to flush old fragments to make space for new ones
234 */
235
236 void
237 pf_flush_fragments(void)
238 {
239 struct pf_fragment *frag;
240 int goal;
241
242 goal = pf_nfrents * 9 / 10;
243 DPFPRINTF(("trying to free > %d frents\n",
244 pf_nfrents - goal));
245 while (goal < pf_nfrents) {
246 frag = TAILQ_LAST(&pf_fragqueue, pf_fragqueue);
247 if (frag == NULL)
248 break;
249 pf_free_fragment(frag);
250 }
251
252
253 goal = pf_ncache * 9 / 10;
254 DPFPRINTF(("trying to free > %d cache entries\n",
255 pf_ncache - goal));
256 while (goal < pf_ncache) {
257 frag = TAILQ_LAST(&pf_cachequeue, pf_cachequeue);
258 if (frag == NULL)
259 break;
260 pf_free_fragment(frag);
261 }
262 }
263
264 /* Frees the fragments and all associated entries */
265
266 void
267 pf_free_fragment(struct pf_fragment *frag)
268 {
269 struct pf_frent *frent;
270 struct pf_frcache *frcache;
271
272 /* Free all fragments */
273 if (BUFFER_FRAGMENTS(frag)) {
274 for (frent = LIST_FIRST(&frag->fr_queue); frent;
275 frent = LIST_FIRST(&frag->fr_queue)) {
276 LIST_REMOVE(frent, fr_next);
277
278 m_freem(frent->fr_m);
279 pool_put(&pf_frent_pl, frent);
280 pf_nfrents--;
281 }
282 } else {
283 for (frcache = LIST_FIRST(&frag->fr_cache); frcache;
284 frcache = LIST_FIRST(&frag->fr_cache)) {
285 LIST_REMOVE(frcache, fr_next);
286
287 KASSERT(LIST_EMPTY(&frag->fr_cache) ||
288 LIST_FIRST(&frag->fr_cache)->fr_off >
289 frcache->fr_end);
290
291 pool_put(&pf_cent_pl, frcache);
292 pf_ncache--;
293 }
294 }
295
296 pf_remove_fragment(frag);
297 }
298
299 void
300 pf_ip2key(struct pf_fragment *key, struct ip *ip)
301 {
302 key->fr_p = ip->ip_p;
303 key->fr_id = ip->ip_id;
304 key->fr_src.s_addr = ip->ip_src.s_addr;
305 key->fr_dst.s_addr = ip->ip_dst.s_addr;
306 }
307
308 struct pf_fragment *
309 pf_find_fragment(struct ip *ip, struct pf_frag_tree *tree)
310 {
311 struct pf_fragment key;
312 struct pf_fragment *frag;
313
314 pf_ip2key(&key, ip);
315
316 frag = RB_FIND(pf_frag_tree, tree, &key);
317 if (frag != NULL) {
318 /* XXX Are we sure we want to update the timeout? */
319 frag->fr_timeout = time.tv_sec;
320 if (BUFFER_FRAGMENTS(frag)) {
321 TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
322 TAILQ_INSERT_HEAD(&pf_fragqueue, frag, frag_next);
323 } else {
324 TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
325 TAILQ_INSERT_HEAD(&pf_cachequeue, frag, frag_next);
326 }
327 }
328
329 return (frag);
330 }
331
332 /* Removes a fragment from the fragment queue and frees the fragment */
333
334 void
335 pf_remove_fragment(struct pf_fragment *frag)
336 {
337 if (BUFFER_FRAGMENTS(frag)) {
338 RB_REMOVE(pf_frag_tree, &pf_frag_tree, frag);
339 TAILQ_REMOVE(&pf_fragqueue, frag, frag_next);
340 pool_put(&pf_frag_pl, frag);
341 } else {
342 RB_REMOVE(pf_frag_tree, &pf_cache_tree, frag);
343 TAILQ_REMOVE(&pf_cachequeue, frag, frag_next);
344 pool_put(&pf_cache_pl, frag);
345 }
346 }
347
348 #define FR_IP_OFF(fr) ((ntohs((fr)->fr_ip->ip_off) & IP_OFFMASK) << 3)
349 struct mbuf *
350 pf_reassemble(struct mbuf **m0, struct pf_fragment **frag,
351 struct pf_frent *frent, int mff)
352 {
353 struct mbuf *m = *m0, *m2;
354 struct pf_frent *frea, *next;
355 struct pf_frent *frep = NULL;
356 struct ip *ip = frent->fr_ip;
357 int hlen = ip->ip_hl << 2;
358 u_int16_t off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
359 u_int16_t ip_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
360 u_int16_t max = ip_len + off;
361
362 KASSERT(*frag == NULL || BUFFER_FRAGMENTS(*frag));
363
364 /* Strip off ip header */
365 m->m_data += hlen;
366 m->m_len -= hlen;
367
368 /* Create a new reassembly queue for this packet */
369 if (*frag == NULL) {
370 *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
371 if (*frag == NULL) {
372 pf_flush_fragments();
373 *frag = pool_get(&pf_frag_pl, PR_NOWAIT);
374 if (*frag == NULL)
375 goto drop_fragment;
376 }
377
378 (*frag)->fr_flags = 0;
379 (*frag)->fr_max = 0;
380 (*frag)->fr_src = frent->fr_ip->ip_src;
381 (*frag)->fr_dst = frent->fr_ip->ip_dst;
382 (*frag)->fr_p = frent->fr_ip->ip_p;
383 (*frag)->fr_id = frent->fr_ip->ip_id;
384 (*frag)->fr_timeout = time.tv_sec;
385 LIST_INIT(&(*frag)->fr_queue);
386
387 RB_INSERT(pf_frag_tree, &pf_frag_tree, *frag);
388 TAILQ_INSERT_HEAD(&pf_fragqueue, *frag, frag_next);
389
390 /* We do not have a previous fragment */
391 frep = NULL;
392 goto insert;
393 }
394
395 /*
396 * Find a fragment after the current one:
397 * - off contains the real shifted offset.
398 */
399 LIST_FOREACH(frea, &(*frag)->fr_queue, fr_next) {
400 if (FR_IP_OFF(frea) > off)
401 break;
402 frep = frea;
403 }
404
405 KASSERT(frep != NULL || frea != NULL);
406
407 if (frep != NULL &&
408 FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl *
409 4 > off)
410 {
411 u_int16_t precut;
412
413 precut = FR_IP_OFF(frep) + ntohs(frep->fr_ip->ip_len) -
414 frep->fr_ip->ip_hl * 4 - off;
415 if (precut >= ip_len)
416 goto drop_fragment;
417 m_adj(frent->fr_m, precut);
418 DPFPRINTF(("overlap -%d\n", precut));
419 /* Enforce 8 byte boundaries */
420 ip->ip_off = htons(ntohs(ip->ip_off) + (precut >> 3));
421 off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
422 ip_len -= precut;
423 ip->ip_len = htons(ip_len);
424 }
425
426 for (; frea != NULL && ip_len + off > FR_IP_OFF(frea);
427 frea = next)
428 {
429 u_int16_t aftercut;
430
431 aftercut = ip_len + off - FR_IP_OFF(frea);
432 DPFPRINTF(("adjust overlap %d\n", aftercut));
433 if (aftercut < ntohs(frea->fr_ip->ip_len) - frea->fr_ip->ip_hl
434 * 4)
435 {
436 frea->fr_ip->ip_len =
437 htons(ntohs(frea->fr_ip->ip_len) - aftercut);
438 frea->fr_ip->ip_off = htons(ntohs(frea->fr_ip->ip_off) +
439 (aftercut >> 3));
440 m_adj(frea->fr_m, aftercut);
441 break;
442 }
443
444 /* This fragment is completely overlapped, loose it */
445 next = LIST_NEXT(frea, fr_next);
446 m_freem(frea->fr_m);
447 LIST_REMOVE(frea, fr_next);
448 pool_put(&pf_frent_pl, frea);
449 pf_nfrents--;
450 }
451
452 insert:
453 /* Update maximum data size */
454 if ((*frag)->fr_max < max)
455 (*frag)->fr_max = max;
456 /* This is the last segment */
457 if (!mff)
458 (*frag)->fr_flags |= PFFRAG_SEENLAST;
459
460 if (frep == NULL)
461 LIST_INSERT_HEAD(&(*frag)->fr_queue, frent, fr_next);
462 else
463 LIST_INSERT_AFTER(frep, frent, fr_next);
464
465 /* Check if we are completely reassembled */
466 if (!((*frag)->fr_flags & PFFRAG_SEENLAST))
467 return (NULL);
468
469 /* Check if we have all the data */
470 off = 0;
471 for (frep = LIST_FIRST(&(*frag)->fr_queue); frep; frep = next) {
472 next = LIST_NEXT(frep, fr_next);
473
474 off += ntohs(frep->fr_ip->ip_len) - frep->fr_ip->ip_hl * 4;
475 if (off < (*frag)->fr_max &&
476 (next == NULL || FR_IP_OFF(next) != off))
477 {
478 DPFPRINTF(("missing fragment at %d, next %d, max %d\n",
479 off, next == NULL ? -1 : FR_IP_OFF(next),
480 (*frag)->fr_max));
481 return (NULL);
482 }
483 }
484 DPFPRINTF(("%d < %d?\n", off, (*frag)->fr_max));
485 if (off < (*frag)->fr_max)
486 return (NULL);
487
488 /* We have all the data */
489 frent = LIST_FIRST(&(*frag)->fr_queue);
490 KASSERT(frent != NULL);
491 if ((frent->fr_ip->ip_hl << 2) + off > IP_MAXPACKET) {
492 DPFPRINTF(("drop: too big: %d\n", off));
493 pf_free_fragment(*frag);
494 *frag = NULL;
495 return (NULL);
496 }
497 next = LIST_NEXT(frent, fr_next);
498
499 /* Magic from ip_input */
500 ip = frent->fr_ip;
501 m = frent->fr_m;
502 m2 = m->m_next;
503 m->m_next = NULL;
504 m_cat(m, m2);
505 pool_put(&pf_frent_pl, frent);
506 pf_nfrents--;
507 for (frent = next; frent != NULL; frent = next) {
508 next = LIST_NEXT(frent, fr_next);
509
510 m2 = frent->fr_m;
511 pool_put(&pf_frent_pl, frent);
512 pf_nfrents--;
513 m_cat(m, m2);
514 }
515
516 ip->ip_src = (*frag)->fr_src;
517 ip->ip_dst = (*frag)->fr_dst;
518
519 /* Remove from fragment queue */
520 pf_remove_fragment(*frag);
521 *frag = NULL;
522
523 hlen = ip->ip_hl << 2;
524 ip->ip_len = htons(off + hlen);
525 m->m_len += hlen;
526 m->m_data -= hlen;
527
528 /* some debugging cruft by sklower, below, will go away soon */
529 /* XXX this should be done elsewhere */
530 if (m->m_flags & M_PKTHDR) {
531 int plen = 0;
532 for (m2 = m; m2; m2 = m2->m_next)
533 plen += m2->m_len;
534 m->m_pkthdr.len = plen;
535 }
536
537 DPFPRINTF(("complete: %p(%d)\n", m, ntohs(ip->ip_len)));
538 return (m);
539
540 drop_fragment:
541 /* Oops - fail safe - drop packet */
542 pool_put(&pf_frent_pl, frent);
543 pf_nfrents--;
544 m_freem(m);
545 return (NULL);
546 }
547
548 struct mbuf *
549 pf_fragcache(struct mbuf **m0, struct ip *h, struct pf_fragment **frag, int mff,
550 int drop, int *nomem)
551 {
552 struct mbuf *m = *m0;
553 struct pf_frcache *frp, *fra, *cur = NULL;
554 int ip_len = ntohs(h->ip_len) - (h->ip_hl << 2);
555 u_int16_t off = ntohs(h->ip_off) << 3;
556 u_int16_t max = ip_len + off;
557 int hosed = 0;
558
559 KASSERT(*frag == NULL || !BUFFER_FRAGMENTS(*frag));
560
561 /* Create a new range queue for this packet */
562 if (*frag == NULL) {
563 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
564 if (*frag == NULL) {
565 pf_flush_fragments();
566 *frag = pool_get(&pf_cache_pl, PR_NOWAIT);
567 if (*frag == NULL)
568 goto no_mem;
569 }
570
571 /* Get an entry for the queue */
572 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
573 if (cur == NULL) {
574 pool_put(&pf_cache_pl, *frag);
575 *frag = NULL;
576 goto no_mem;
577 }
578 pf_ncache++;
579
580 (*frag)->fr_flags = PFFRAG_NOBUFFER;
581 (*frag)->fr_max = 0;
582 (*frag)->fr_src = h->ip_src;
583 (*frag)->fr_dst = h->ip_dst;
584 (*frag)->fr_p = h->ip_p;
585 (*frag)->fr_id = h->ip_id;
586 (*frag)->fr_timeout = time.tv_sec;
587
588 cur->fr_off = off;
589 cur->fr_end = max;
590 LIST_INIT(&(*frag)->fr_cache);
591 LIST_INSERT_HEAD(&(*frag)->fr_cache, cur, fr_next);
592
593 RB_INSERT(pf_frag_tree, &pf_cache_tree, *frag);
594 TAILQ_INSERT_HEAD(&pf_cachequeue, *frag, frag_next);
595
596 DPFPRINTF(("fragcache[%d]: new %d-%d\n", h->ip_id, off, max));
597
598 goto pass;
599 }
600
601 /*
602 * Find a fragment after the current one:
603 * - off contains the real shifted offset.
604 */
605 frp = NULL;
606 LIST_FOREACH(fra, &(*frag)->fr_cache, fr_next) {
607 if (fra->fr_off > off)
608 break;
609 frp = fra;
610 }
611
612 KASSERT(frp != NULL || fra != NULL);
613
614 if (frp != NULL) {
615 int precut;
616
617 precut = frp->fr_end - off;
618 if (precut >= ip_len) {
619 /* Fragment is entirely a duplicate */
620 DPFPRINTF(("fragcache[%d]: dead (%d-%d) %d-%d\n",
621 h->ip_id, frp->fr_off, frp->fr_end, off, max));
622 goto drop_fragment;
623 }
624 if (precut == 0) {
625 /* They are adjacent. Fixup cache entry */
626 DPFPRINTF(("fragcache[%d]: adjacent (%d-%d) %d-%d\n",
627 h->ip_id, frp->fr_off, frp->fr_end, off, max));
628 frp->fr_end = max;
629 } else if (precut > 0) {
630 /* The first part of this payload overlaps with a
631 * fragment that has already been passed.
632 * Need to trim off the first part of the payload.
633 * But to do so easily, we need to create another
634 * mbuf to throw the original header into.
635 */
636
637 DPFPRINTF(("fragcache[%d]: chop %d (%d-%d) %d-%d\n",
638 h->ip_id, precut, frp->fr_off, frp->fr_end, off,
639 max));
640
641 off += precut;
642 max -= precut;
643 /* Update the previous frag to encompass this one */
644 frp->fr_end = max;
645
646 if (!drop) {
647 /* XXX Optimization opportunity
648 * This is a very heavy way to trim the payload.
649 * we could do it much faster by diddling mbuf
650 * internals but that would be even less legible
651 * than this mbuf magic. For my next trick,
652 * I'll pull a rabbit out of my laptop.
653 */
654 #ifdef __OpenBSD__
655 *m0 = m_copym2(m, 0, h->ip_hl << 2, M_NOWAIT);
656 #else
657 *m0 = m_dup(m, 0, h->ip_hl << 2, M_NOWAIT);
658 #endif
659 if (*m0 == NULL)
660 goto no_mem;
661 KASSERT((*m0)->m_next == NULL);
662 m_adj(m, precut + (h->ip_hl << 2));
663 m_cat(*m0, m);
664 m = *m0;
665 if (m->m_flags & M_PKTHDR) {
666 int plen = 0;
667 struct mbuf *t;
668 for (t = m; t; t = t->m_next)
669 plen += t->m_len;
670 m->m_pkthdr.len = plen;
671 }
672
673
674 h = mtod(m, struct ip *);
675
676
677 KASSERT((int)m->m_len ==
678 ntohs(h->ip_len) - precut);
679 h->ip_off = htons(ntohs(h->ip_off) +
680 (precut >> 3));
681 h->ip_len = htons(ntohs(h->ip_len) - precut);
682 } else {
683 hosed++;
684 }
685 } else {
686 /* There is a gap between fragments */
687
688 DPFPRINTF(("fragcache[%d]: gap %d (%d-%d) %d-%d\n",
689 h->ip_id, -precut, frp->fr_off, frp->fr_end, off,
690 max));
691
692 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
693 if (cur == NULL)
694 goto no_mem;
695 pf_ncache++;
696
697 cur->fr_off = off;
698 cur->fr_end = max;
699 LIST_INSERT_AFTER(frp, cur, fr_next);
700 }
701 }
702
703 if (fra != NULL) {
704 int aftercut;
705 int merge = 0;
706
707 aftercut = max - fra->fr_off;
708 if (aftercut == 0) {
709 /* Adjacent fragments */
710 DPFPRINTF(("fragcache[%d]: adjacent %d-%d (%d-%d)\n",
711 h->ip_id, off, max, fra->fr_off, fra->fr_end));
712 fra->fr_off = off;
713 merge = 1;
714 } else if (aftercut > 0) {
715 /* Need to chop off the tail of this fragment */
716 DPFPRINTF(("fragcache[%d]: chop %d %d-%d (%d-%d)\n",
717 h->ip_id, aftercut, off, max, fra->fr_off,
718 fra->fr_end));
719 fra->fr_off = off;
720 max -= aftercut;
721
722 merge = 1;
723
724 if (!drop) {
725 m_adj(m, -aftercut);
726 if (m->m_flags & M_PKTHDR) {
727 int plen = 0;
728 struct mbuf *t;
729 for (t = m; t; t = t->m_next)
730 plen += t->m_len;
731 m->m_pkthdr.len = plen;
732 }
733 h = mtod(m, struct ip *);
734 KASSERT((int)m->m_len ==
735 ntohs(h->ip_len) - aftercut);
736 h->ip_len = htons(ntohs(h->ip_len) - aftercut);
737 } else {
738 hosed++;
739 }
740 } else {
741 /* There is a gap between fragments */
742 DPFPRINTF(("fragcache[%d]: gap %d %d-%d (%d-%d)\n",
743 h->ip_id, -aftercut, off, max, fra->fr_off,
744 fra->fr_end));
745
746 cur = pool_get(&pf_cent_pl, PR_NOWAIT);
747 if (cur == NULL)
748 goto no_mem;
749 pf_ncache++;
750
751 cur->fr_off = off;
752 cur->fr_end = max;
753 LIST_INSERT_BEFORE(fra, cur, fr_next);
754 }
755
756
757 /* Need to glue together two separate fragment descriptors */
758 if (merge) {
759 if (cur && fra->fr_off <= cur->fr_end) {
760 /* Need to merge in a previous 'cur' */
761 DPFPRINTF(("fragcache[%d]: adjacent(merge "
762 "%d-%d) %d-%d (%d-%d)\n",
763 h->ip_id, cur->fr_off, cur->fr_end, off,
764 max, fra->fr_off, fra->fr_end));
765 fra->fr_off = cur->fr_off;
766 LIST_REMOVE(cur, fr_next);
767 pool_put(&pf_cent_pl, cur);
768 pf_ncache--;
769 cur = NULL;
770
771 } else if (frp && fra->fr_off <= frp->fr_end) {
772 /* Need to merge in a modified 'frp' */
773 KASSERT(cur == NULL);
774 DPFPRINTF(("fragcache[%d]: adjacent(merge "
775 "%d-%d) %d-%d (%d-%d)\n",
776 h->ip_id, frp->fr_off, frp->fr_end, off,
777 max, fra->fr_off, fra->fr_end));
778 fra->fr_off = frp->fr_off;
779 LIST_REMOVE(frp, fr_next);
780 pool_put(&pf_cent_pl, frp);
781 pf_ncache--;
782 frp = NULL;
783
784 }
785 }
786 }
787
788 if (hosed) {
789 /*
790 * We must keep tracking the overall fragment even when
791 * we're going to drop it anyway so that we know when to
792 * free the overall descriptor. Thus we drop the frag late.
793 */
794 goto drop_fragment;
795 }
796
797
798 pass:
799 /* Update maximum data size */
800 if ((*frag)->fr_max < max)
801 (*frag)->fr_max = max;
802
803 /* This is the last segment */
804 if (!mff)
805 (*frag)->fr_flags |= PFFRAG_SEENLAST;
806
807 /* Check if we are completely reassembled */
808 if (((*frag)->fr_flags & PFFRAG_SEENLAST) &&
809 LIST_FIRST(&(*frag)->fr_cache)->fr_off == 0 &&
810 LIST_FIRST(&(*frag)->fr_cache)->fr_end == (*frag)->fr_max) {
811 /* Remove from fragment queue */
812 DPFPRINTF(("fragcache[%d]: done 0-%d\n", h->ip_id,
813 (*frag)->fr_max));
814 pf_free_fragment(*frag);
815 *frag = NULL;
816 }
817
818 return (m);
819
820 no_mem:
821 *nomem = 1;
822
823 /* Still need to pay attention to !IP_MF */
824 if (!mff && *frag != NULL)
825 (*frag)->fr_flags |= PFFRAG_SEENLAST;
826
827 m_freem(m);
828 return (NULL);
829
830 drop_fragment:
831
832 /* Still need to pay attention to !IP_MF */
833 if (!mff && *frag != NULL)
834 (*frag)->fr_flags |= PFFRAG_SEENLAST;
835
836 if (drop) {
837 /* This fragment has been deemed bad. Don't reass */
838 if (((*frag)->fr_flags & PFFRAG_DROP) == 0)
839 DPFPRINTF(("fragcache[%d]: dropping overall fragment\n",
840 h->ip_id));
841 (*frag)->fr_flags |= PFFRAG_DROP;
842 }
843
844 m_freem(m);
845 return (NULL);
846 }
847
848 int
849 pf_normalize_ip(struct mbuf **m0, int dir, struct pfi_kif *kif, u_short *reason)
850 {
851 struct mbuf *m = *m0;
852 struct pf_rule *r;
853 struct pf_frent *frent;
854 struct pf_fragment *frag = NULL;
855 struct ip *h = mtod(m, struct ip *);
856 int mff = (ntohs(h->ip_off) & IP_MF);
857 int hlen = h->ip_hl << 2;
858 u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
859 u_int16_t max;
860 int ip_len;
861 int ip_off;
862
863 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
864 while (r != NULL) {
865 r->evaluations++;
866 if (r->kif != NULL &&
867 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
868 r = r->skip[PF_SKIP_IFP].ptr;
869 else if (r->direction && r->direction != dir)
870 r = r->skip[PF_SKIP_DIR].ptr;
871 else if (r->af && r->af != AF_INET)
872 r = r->skip[PF_SKIP_AF].ptr;
873 else if (r->proto && r->proto != h->ip_p)
874 r = r->skip[PF_SKIP_PROTO].ptr;
875 else if (PF_MISMATCHAW(&r->src.addr,
876 (struct pf_addr *)&h->ip_src.s_addr, AF_INET, r->src.not))
877 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
878 else if (PF_MISMATCHAW(&r->dst.addr,
879 (struct pf_addr *)&h->ip_dst.s_addr, AF_INET, r->dst.not))
880 r = r->skip[PF_SKIP_DST_ADDR].ptr;
881 else
882 break;
883 }
884
885 if (r == NULL)
886 return (PF_PASS);
887 else
888 r->packets++;
889
890 /* Check for illegal packets */
891 if (hlen < (int)sizeof(struct ip))
892 goto drop;
893
894 if (hlen > ntohs(h->ip_len))
895 goto drop;
896
897 /* Clear IP_DF if the rule uses the no-df option */
898 if (r->rule_flag & PFRULE_NODF)
899 h->ip_off &= htons(~IP_DF);
900
901 /* We will need other tests here */
902 if (!fragoff && !mff)
903 goto no_fragment;
904
905 /* We're dealing with a fragment now. Don't allow fragments
906 * with IP_DF to enter the cache. If the flag was cleared by
907 * no-df above, fine. Otherwise drop it.
908 */
909 if (h->ip_off & htons(IP_DF)) {
910 DPFPRINTF(("IP_DF\n"));
911 goto bad;
912 }
913
914 ip_len = ntohs(h->ip_len) - hlen;
915 ip_off = (ntohs(h->ip_off) & IP_OFFMASK) << 3;
916
917 /* All fragments are 8 byte aligned */
918 if (mff && (ip_len & 0x7)) {
919 DPFPRINTF(("mff and %d\n", ip_len));
920 goto bad;
921 }
922
923 /* Respect maximum length */
924 if (fragoff + ip_len > IP_MAXPACKET) {
925 DPFPRINTF(("max packet %d\n", fragoff + ip_len));
926 goto bad;
927 }
928 max = fragoff + ip_len;
929
930 if ((r->rule_flag & (PFRULE_FRAGCROP|PFRULE_FRAGDROP)) == 0) {
931 /* Fully buffer all of the fragments */
932
933 frag = pf_find_fragment(h, &pf_frag_tree);
934
935 /* Check if we saw the last fragment already */
936 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
937 max > frag->fr_max)
938 goto bad;
939
940 /* Get an entry for the fragment queue */
941 frent = pool_get(&pf_frent_pl, PR_NOWAIT);
942 if (frent == NULL) {
943 REASON_SET(reason, PFRES_MEMORY);
944 return (PF_DROP);
945 }
946 pf_nfrents++;
947 frent->fr_ip = h;
948 frent->fr_m = m;
949
950 /* Might return a completely reassembled mbuf, or NULL */
951 DPFPRINTF(("reass frag %d @ %d-%d\n", h->ip_id, fragoff, max));
952 *m0 = m = pf_reassemble(m0, &frag, frent, mff);
953
954 if (m == NULL)
955 return (PF_DROP);
956
957 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
958 goto drop;
959
960 h = mtod(m, struct ip *);
961 } else {
962 /* non-buffering fragment cache (drops or masks overlaps) */
963 int nomem = 0;
964
965 if (dir == PF_OUT) {
966 if (m_tag_find(m, PACKET_TAG_PF_FRAGCACHE, NULL) !=
967 NULL) {
968 /* Already passed the fragment cache in the
969 * input direction. If we continued, it would
970 * appear to be a dup and would be dropped.
971 */
972 goto fragment_pass;
973 }
974 }
975
976 frag = pf_find_fragment(h, &pf_cache_tree);
977
978 /* Check if we saw the last fragment already */
979 if (frag != NULL && (frag->fr_flags & PFFRAG_SEENLAST) &&
980 max > frag->fr_max) {
981 if (r->rule_flag & PFRULE_FRAGDROP)
982 frag->fr_flags |= PFFRAG_DROP;
983 goto bad;
984 }
985
986 *m0 = m = pf_fragcache(m0, h, &frag, mff,
987 (r->rule_flag & PFRULE_FRAGDROP) ? 1 : 0, &nomem);
988 if (m == NULL) {
989 if (nomem)
990 goto no_mem;
991 goto drop;
992 }
993
994 if (dir == PF_IN) {
995 struct m_tag *mtag;
996
997 mtag = m_tag_get(PACKET_TAG_PF_FRAGCACHE, 0, M_NOWAIT);
998 if (mtag == NULL)
999 goto no_mem;
1000 m_tag_prepend(m, mtag);
1001 }
1002 if (frag != NULL && (frag->fr_flags & PFFRAG_DROP))
1003 goto drop;
1004 goto fragment_pass;
1005 }
1006
1007 no_fragment:
1008 /* At this point, only IP_DF is allowed in ip_off */
1009 h->ip_off &= htons(IP_DF);
1010
1011 /* Enforce a minimum ttl, may cause endless packet loops */
1012 if (r->min_ttl && h->ip_ttl < r->min_ttl)
1013 h->ip_ttl = r->min_ttl;
1014
1015 if (r->rule_flag & PFRULE_RANDOMID)
1016 h->ip_id = ip_randomid();
1017
1018 return (PF_PASS);
1019
1020 fragment_pass:
1021 /* Enforce a minimum ttl, may cause endless packet loops */
1022 if (r->min_ttl && h->ip_ttl < r->min_ttl)
1023 h->ip_ttl = r->min_ttl;
1024
1025 return (PF_PASS);
1026
1027 no_mem:
1028 REASON_SET(reason, PFRES_MEMORY);
1029 if (r != NULL && r->log)
1030 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1031 return (PF_DROP);
1032
1033 drop:
1034 REASON_SET(reason, PFRES_NORM);
1035 if (r != NULL && r->log)
1036 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1037 return (PF_DROP);
1038
1039 bad:
1040 DPFPRINTF(("dropping bad fragment\n"));
1041
1042 /* Free associated fragments */
1043 if (frag != NULL)
1044 pf_free_fragment(frag);
1045
1046 REASON_SET(reason, PFRES_FRAG);
1047 if (r != NULL && r->log)
1048 PFLOG_PACKET(kif, h, m, AF_INET, dir, *reason, r, NULL, NULL);
1049
1050 return (PF_DROP);
1051 }
1052
1053 #ifdef INET6
1054 int
1055 pf_normalize_ip6(struct mbuf **m0, int dir, struct pfi_kif *kif,
1056 u_short *reason)
1057 {
1058 struct mbuf *m = *m0;
1059 struct pf_rule *r;
1060 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1061 int off;
1062 struct ip6_ext ext;
1063 struct ip6_opt opt;
1064 struct ip6_opt_jumbo jumbo;
1065 struct ip6_frag frag;
1066 u_int32_t jumbolen = 0, plen;
1067 u_int16_t fragoff = 0;
1068 int optend;
1069 int ooff;
1070 u_int8_t proto;
1071 int terminal;
1072
1073 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1074 while (r != NULL) {
1075 r->evaluations++;
1076 if (r->kif != NULL &&
1077 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1078 r = r->skip[PF_SKIP_IFP].ptr;
1079 else if (r->direction && r->direction != dir)
1080 r = r->skip[PF_SKIP_DIR].ptr;
1081 else if (r->af && r->af != AF_INET6)
1082 r = r->skip[PF_SKIP_AF].ptr;
1083 #if 0 /* header chain! */
1084 else if (r->proto && r->proto != h->ip6_nxt)
1085 r = r->skip[PF_SKIP_PROTO].ptr;
1086 #endif
1087 else if (PF_MISMATCHAW(&r->src.addr,
1088 (struct pf_addr *)&h->ip6_src, AF_INET6, r->src.not))
1089 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1090 else if (PF_MISMATCHAW(&r->dst.addr,
1091 (struct pf_addr *)&h->ip6_dst, AF_INET6, r->dst.not))
1092 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1093 else
1094 break;
1095 }
1096
1097 if (r == NULL)
1098 return (PF_PASS);
1099 else
1100 r->packets++;
1101
1102 /* Check for illegal packets */
1103 if (sizeof(struct ip6_hdr) + IPV6_MAXPACKET < m->m_pkthdr.len)
1104 goto drop;
1105
1106 off = sizeof(struct ip6_hdr);
1107 proto = h->ip6_nxt;
1108 terminal = 0;
1109 do {
1110 switch (proto) {
1111 case IPPROTO_FRAGMENT:
1112 goto fragment;
1113 break;
1114 case IPPROTO_AH:
1115 case IPPROTO_ROUTING:
1116 case IPPROTO_DSTOPTS:
1117 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1118 NULL, AF_INET6))
1119 goto shortpkt;
1120 if (proto == IPPROTO_AH)
1121 off += (ext.ip6e_len + 2) * 4;
1122 else
1123 off += (ext.ip6e_len + 1) * 8;
1124 proto = ext.ip6e_nxt;
1125 break;
1126 case IPPROTO_HOPOPTS:
1127 if (!pf_pull_hdr(m, off, &ext, sizeof(ext), NULL,
1128 NULL, AF_INET6))
1129 goto shortpkt;
1130 optend = off + (ext.ip6e_len + 1) * 8;
1131 ooff = off + sizeof(ext);
1132 do {
1133 if (!pf_pull_hdr(m, ooff, &opt.ip6o_type,
1134 sizeof(opt.ip6o_type), NULL, NULL,
1135 AF_INET6))
1136 goto shortpkt;
1137 if (opt.ip6o_type == IP6OPT_PAD1) {
1138 ooff++;
1139 continue;
1140 }
1141 if (!pf_pull_hdr(m, ooff, &opt, sizeof(opt),
1142 NULL, NULL, AF_INET6))
1143 goto shortpkt;
1144 if (ooff + sizeof(opt) + opt.ip6o_len > optend)
1145 goto drop;
1146 switch (opt.ip6o_type) {
1147 case IP6OPT_JUMBO:
1148 if (h->ip6_plen != 0)
1149 goto drop;
1150 if (!pf_pull_hdr(m, ooff, &jumbo,
1151 sizeof(jumbo), NULL, NULL,
1152 AF_INET6))
1153 goto shortpkt;
1154 memcpy(&jumbolen, jumbo.ip6oj_jumbo_len,
1155 sizeof(jumbolen));
1156 jumbolen = ntohl(jumbolen);
1157 if (jumbolen <= IPV6_MAXPACKET)
1158 goto drop;
1159 if (sizeof(struct ip6_hdr) + jumbolen !=
1160 m->m_pkthdr.len)
1161 goto drop;
1162 break;
1163 default:
1164 break;
1165 }
1166 ooff += sizeof(opt) + opt.ip6o_len;
1167 } while (ooff < optend);
1168
1169 off = optend;
1170 proto = ext.ip6e_nxt;
1171 break;
1172 default:
1173 terminal = 1;
1174 break;
1175 }
1176 } while (!terminal);
1177
1178 /* jumbo payload option must be present, or plen > 0 */
1179 if (ntohs(h->ip6_plen) == 0)
1180 plen = jumbolen;
1181 else
1182 plen = ntohs(h->ip6_plen);
1183 if (plen == 0)
1184 goto drop;
1185 if (sizeof(struct ip6_hdr) + plen > m->m_pkthdr.len)
1186 goto shortpkt;
1187
1188 /* Enforce a minimum ttl, may cause endless packet loops */
1189 if (r->min_ttl && h->ip6_hlim < r->min_ttl)
1190 h->ip6_hlim = r->min_ttl;
1191
1192 return (PF_PASS);
1193
1194 fragment:
1195 if (ntohs(h->ip6_plen) == 0 || jumbolen)
1196 goto drop;
1197 plen = ntohs(h->ip6_plen);
1198
1199 if (!pf_pull_hdr(m, off, &frag, sizeof(frag), NULL, NULL, AF_INET6))
1200 goto shortpkt;
1201 fragoff = ntohs(frag.ip6f_offlg & IP6F_OFF_MASK);
1202 if (fragoff + (plen - off - sizeof(frag)) > IPV6_MAXPACKET)
1203 goto badfrag;
1204
1205 /* do something about it */
1206 return (PF_PASS);
1207
1208 shortpkt:
1209 REASON_SET(reason, PFRES_SHORT);
1210 if (r != NULL && r->log)
1211 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1212 return (PF_DROP);
1213
1214 drop:
1215 REASON_SET(reason, PFRES_NORM);
1216 if (r != NULL && r->log)
1217 PFLOG_PACKET(kif, h, m, AF_INET6, dir, *reason, r, NULL, NULL);
1218 return (PF_DROP);
1219
1220 badfrag:
1221 REASON_SET(reason, PFRES_FRAG);
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 #endif
1227
1228 int
1229 pf_normalize_tcp(int dir, struct pfi_kif *kif, struct mbuf *m, int ipoff,
1230 int off, void *h, struct pf_pdesc *pd)
1231 {
1232 struct pf_rule *r, *rm = NULL;
1233 struct tcphdr *th = pd->hdr.tcp;
1234 int rewrite = 0;
1235 u_short reason;
1236 u_int8_t flags;
1237 sa_family_t af = pd->af;
1238
1239 r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_SCRUB].active.ptr);
1240 while (r != NULL) {
1241 r->evaluations++;
1242 if (r->kif != NULL &&
1243 (r->kif != kif && r->kif != kif->pfik_parent) == !r->ifnot)
1244 r = r->skip[PF_SKIP_IFP].ptr;
1245 else if (r->direction && r->direction != dir)
1246 r = r->skip[PF_SKIP_DIR].ptr;
1247 else if (r->af && r->af != af)
1248 r = r->skip[PF_SKIP_AF].ptr;
1249 else if (r->proto && r->proto != pd->proto)
1250 r = r->skip[PF_SKIP_PROTO].ptr;
1251 else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, r->src.not))
1252 r = r->skip[PF_SKIP_SRC_ADDR].ptr;
1253 else if (r->src.port_op && !pf_match_port(r->src.port_op,
1254 r->src.port[0], r->src.port[1], th->th_sport))
1255 r = r->skip[PF_SKIP_SRC_PORT].ptr;
1256 else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, r->dst.not))
1257 r = r->skip[PF_SKIP_DST_ADDR].ptr;
1258 else if (r->dst.port_op && !pf_match_port(r->dst.port_op,
1259 r->dst.port[0], r->dst.port[1], th->th_dport))
1260 r = r->skip[PF_SKIP_DST_PORT].ptr;
1261 else if (r->os_fingerprint != PF_OSFP_ANY && !pf_osfp_match(
1262 pf_osfp_fingerprint(pd, m, off, th),
1263 r->os_fingerprint))
1264 r = TAILQ_NEXT(r, entries);
1265 else {
1266 rm = r;
1267 break;
1268 }
1269 }
1270
1271 if (rm == NULL)
1272 return (PF_PASS);
1273 else
1274 r->packets++;
1275
1276 if (rm->rule_flag & PFRULE_REASSEMBLE_TCP)
1277 pd->flags |= PFDESC_TCP_NORM;
1278
1279 flags = th->th_flags;
1280 if (flags & TH_SYN) {
1281 /* Illegal packet */
1282 if (flags & TH_RST)
1283 goto tcp_drop;
1284
1285 if (flags & TH_FIN)
1286 flags &= ~TH_FIN;
1287 } else {
1288 /* Illegal packet */
1289 if (!(flags & (TH_ACK|TH_RST)))
1290 goto tcp_drop;
1291 }
1292
1293 if (!(flags & TH_ACK)) {
1294 /* These flags are only valid if ACK is set */
1295 if ((flags & TH_FIN) || (flags & TH_PUSH) || (flags & TH_URG))
1296 goto tcp_drop;
1297 }
1298
1299 /* Check for illegal header length */
1300 if (th->th_off < (sizeof(struct tcphdr) >> 2))
1301 goto tcp_drop;
1302
1303 /* If flags changed, or reserved data set, then adjust */
1304 if (flags != th->th_flags || th->th_x2 != 0) {
1305 u_int16_t ov, nv;
1306
1307 ov = *(u_int16_t *)(&th->th_ack + 1);
1308 th->th_flags = flags;
1309 th->th_x2 = 0;
1310 nv = *(u_int16_t *)(&th->th_ack + 1);
1311
1312 th->th_sum = pf_cksum_fixup(th->th_sum, ov, nv);
1313 rewrite = 1;
1314 }
1315
1316 /* Remove urgent pointer, if TH_URG is not set */
1317 if (!(flags & TH_URG) && th->th_urp) {
1318 th->th_sum = pf_cksum_fixup(th->th_sum, th->th_urp, 0);
1319 th->th_urp = 0;
1320 rewrite = 1;
1321 }
1322
1323 /* Process options */
1324 if (r->max_mss && pf_normalize_tcpopt(r, m, th, off))
1325 rewrite = 1;
1326
1327 /* copy back packet headers if we sanitized */
1328 if (rewrite)
1329 m_copyback(m, off, sizeof(*th), th);
1330
1331 return (PF_PASS);
1332
1333 tcp_drop:
1334 REASON_SET(&reason, PFRES_NORM);
1335 if (rm != NULL && r->log)
1336 PFLOG_PACKET(kif, h, m, AF_INET, dir, reason, r, NULL, NULL);
1337 return (PF_DROP);
1338 }
1339
1340 int
1341 pf_normalize_tcp_init(struct mbuf *m, int off, struct pf_pdesc *pd,
1342 struct tcphdr *th, struct pf_state_peer *src, struct pf_state_peer *dst)
1343 {
1344 u_int8_t hdr[60];
1345 u_int8_t *opt;
1346
1347 KASSERT(src->scrub == NULL);
1348
1349 src->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT);
1350 if (src->scrub == NULL)
1351 return (1);
1352 bzero(src->scrub, sizeof(*src->scrub));
1353
1354 switch (pd->af) {
1355 #ifdef INET
1356 case AF_INET: {
1357 struct ip *h = mtod(m, struct ip *);
1358 src->scrub->pfss_ttl = h->ip_ttl;
1359 break;
1360 }
1361 #endif /* INET */
1362 #ifdef INET6
1363 case AF_INET6: {
1364 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1365 src->scrub->pfss_ttl = h->ip6_hlim;
1366 break;
1367 }
1368 #endif /* INET6 */
1369 }
1370
1371
1372 /*
1373 * All normalizations below are only begun if we see the start of
1374 * the connections. They must all set an enabled bit in pfss_flags
1375 */
1376 if ((th->th_flags & TH_SYN) == 0)
1377 return (0);
1378
1379
1380 if (th->th_off > (sizeof(struct tcphdr) >> 2) && src->scrub &&
1381 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1382 /* Diddle with TCP options */
1383 int hlen;
1384 opt = hdr + sizeof(struct tcphdr);
1385 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1386 while (hlen >= TCPOLEN_TIMESTAMP) {
1387 switch (*opt) {
1388 case TCPOPT_EOL: /* FALLTHROUGH */
1389 case TCPOPT_NOP:
1390 opt++;
1391 hlen--;
1392 break;
1393 case TCPOPT_TIMESTAMP:
1394 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1395 src->scrub->pfss_flags |=
1396 PFSS_TIMESTAMP;
1397 src->scrub->pfss_ts_mod = arc4random();
1398 }
1399 /* FALLTHROUGH */
1400 default:
1401 hlen -= opt[1];
1402 opt += opt[1];
1403 break;
1404 }
1405 }
1406 }
1407
1408 return (0);
1409 }
1410
1411 void
1412 pf_normalize_tcp_cleanup(struct pf_state *state)
1413 {
1414 if (state->src.scrub)
1415 pool_put(&pf_state_scrub_pl, state->src.scrub);
1416 if (state->dst.scrub)
1417 pool_put(&pf_state_scrub_pl, state->dst.scrub);
1418
1419 /* Someday... flush the TCP segment reassembly descriptors. */
1420 }
1421
1422 int
1423 pf_normalize_tcp_stateful(struct mbuf *m, int off, struct pf_pdesc *pd,
1424 u_short *reason, struct tcphdr *th, struct pf_state_peer *src,
1425 struct pf_state_peer *dst, int *writeback)
1426 {
1427 u_int8_t hdr[60];
1428 u_int8_t *opt;
1429 int copyback = 0;
1430
1431 KASSERT(src->scrub || dst->scrub);
1432
1433 /*
1434 * Enforce the minimum TTL seen for this connection. Negate a common
1435 * technique to evade an intrusion detection system and confuse
1436 * firewall state code.
1437 */
1438 switch (pd->af) {
1439 #ifdef INET
1440 case AF_INET: {
1441 if (src->scrub) {
1442 struct ip *h = mtod(m, struct ip *);
1443 if (h->ip_ttl > src->scrub->pfss_ttl)
1444 src->scrub->pfss_ttl = h->ip_ttl;
1445 h->ip_ttl = src->scrub->pfss_ttl;
1446 }
1447 break;
1448 }
1449 #endif /* INET */
1450 #ifdef INET6
1451 case AF_INET6: {
1452 if (src->scrub) {
1453 struct ip6_hdr *h = mtod(m, struct ip6_hdr *);
1454 if (h->ip6_hlim > src->scrub->pfss_ttl)
1455 src->scrub->pfss_ttl = h->ip6_hlim;
1456 h->ip6_hlim = src->scrub->pfss_ttl;
1457 }
1458 break;
1459 }
1460 #endif /* INET6 */
1461 }
1462
1463 if (th->th_off > (sizeof(struct tcphdr) >> 2) &&
1464 ((src->scrub && (src->scrub->pfss_flags & PFSS_TIMESTAMP)) ||
1465 (dst->scrub && (dst->scrub->pfss_flags & PFSS_TIMESTAMP))) &&
1466 pf_pull_hdr(m, off, hdr, th->th_off << 2, NULL, NULL, pd->af)) {
1467 /* Diddle with TCP options */
1468 int hlen;
1469 opt = hdr + sizeof(struct tcphdr);
1470 hlen = (th->th_off << 2) - sizeof(struct tcphdr);
1471 while (hlen >= TCPOLEN_TIMESTAMP) {
1472 switch (*opt) {
1473 case TCPOPT_EOL: /* FALLTHROUGH */
1474 case TCPOPT_NOP:
1475 opt++;
1476 hlen--;
1477 break;
1478 case TCPOPT_TIMESTAMP:
1479 /* Modulate the timestamps. Can be used for
1480 * NAT detection, OS uptime determination or
1481 * reboot detection.
1482 */
1483 if (opt[1] >= TCPOLEN_TIMESTAMP) {
1484 u_int32_t ts_value;
1485 if (src->scrub &&
1486 (src->scrub->pfss_flags &
1487 PFSS_TIMESTAMP)) {
1488 memcpy(&ts_value, &opt[2],
1489 sizeof(u_int32_t));
1490 ts_value = htonl(ntohl(ts_value)
1491 + src->scrub->pfss_ts_mod);
1492 pf_change_a(&opt[2],
1493 &th->th_sum, ts_value, 0);
1494 copyback = 1;
1495 }
1496
1497 /* Modulate TS reply iff valid (!0) */
1498 memcpy(&ts_value, &opt[6],
1499 sizeof(u_int32_t));
1500 if (ts_value && dst->scrub &&
1501 (dst->scrub->pfss_flags &
1502 PFSS_TIMESTAMP)) {
1503 ts_value = htonl(ntohl(ts_value)
1504 - dst->scrub->pfss_ts_mod);
1505 pf_change_a(&opt[6],
1506 &th->th_sum, ts_value, 0);
1507 copyback = 1;
1508 }
1509 }
1510 /* FALLTHROUGH */
1511 default:
1512 hlen -= opt[1];
1513 opt += opt[1];
1514 break;
1515 }
1516 }
1517 if (copyback) {
1518 /* Copyback the options, caller copys back header */
1519 *writeback = 1;
1520 m_copyback(m, off + sizeof(struct tcphdr),
1521 (th->th_off << 2) - sizeof(struct tcphdr), hdr +
1522 sizeof(struct tcphdr));
1523 }
1524 }
1525
1526
1527 /* I have a dream.... TCP segment reassembly.... */
1528 return (0);
1529 }
1530 int
1531 pf_normalize_tcpopt(struct pf_rule *r, struct mbuf *m, struct tcphdr *th,
1532 int off)
1533 {
1534 u_int16_t *mss;
1535 int thoff;
1536 int opt, cnt, optlen = 0;
1537 int rewrite = 0;
1538 u_char *optp;
1539
1540 thoff = th->th_off << 2;
1541 cnt = thoff - sizeof(struct tcphdr);
1542 optp = mtod(m, caddr_t) + off + sizeof(struct tcphdr);
1543
1544 for (; cnt > 0; cnt -= optlen, optp += optlen) {
1545 opt = optp[0];
1546 if (opt == TCPOPT_EOL)
1547 break;
1548 if (opt == TCPOPT_NOP)
1549 optlen = 1;
1550 else {
1551 if (cnt < 2)
1552 break;
1553 optlen = optp[1];
1554 if (optlen < 2 || optlen > cnt)
1555 break;
1556 }
1557 switch (opt) {
1558 case TCPOPT_MAXSEG:
1559 mss = (u_int16_t *)(optp + 2);
1560 if ((ntohs(*mss)) > r->max_mss) {
1561 th->th_sum = pf_cksum_fixup(th->th_sum,
1562 *mss, htons(r->max_mss));
1563 *mss = htons(r->max_mss);
1564 rewrite = 1;
1565 }
1566 break;
1567 default:
1568 break;
1569 }
1570 }
1571
1572 return (rewrite);
1573 }
1574