4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
27 #if defined(_KERNEL) && defined(__FreeBSD_version)
28 # include <sys/filio.h>
29 # include <sys/fcntl.h>
31 # include <sys/ioctl.h>
33 # include <sys/protosw.h>
34 #include <sys/socket.h>
36 # include <sys/systm.h>
37 # if !defined(__SVR4) && !defined(__svr4__)
38 # include <sys/mbuf.h>
41 #if !defined(__SVR4) && !defined(__svr4__)
43 # include <sys/kernel.h>
46 # include <sys/byteorder.h>
48 # include <sys/dditypes.h>
50 # include <sys/stream.h>
51 # include <sys/kmem.h>
57 #include <netinet/in.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/udp.h>
63 #include <netinet/ip_icmp.h>
64 #include "netinet/ip_compat.h"
65 #include <netinet/tcpip.h>
66 #include "netinet/ip_fil.h"
67 #include "netinet/ip_nat.h"
68 #include "netinet/ip_frag.h"
69 #include "netinet/ip_state.h"
70 #include "netinet/ip_auth.h"
71 #include "netinet/ip_lookup.h"
72 #include "netinet/ip_proxy.h"
73 #include "netinet/ip_sync.h"
77 static const char sccsid[] = "@(#)ip_frag.c 1.11 3/24/96 (C) 1993-2000 Darren Reed";
78 static const char rcsid[] = "@(#)$FreeBSD$";
79 /* static const char rcsid[] = "@(#)$Id: ip_frag.c,v 2.77.2.12 2007/09/20 12:51:51 darrenr Exp $"; */
84 static ipfr_t *ipfr_frag_new __P((ipf_main_softc_t *, ipf_frag_softc_t *,
85 fr_info_t *, u_32_t, ipfr_t **,
87 static ipfr_t *ipf_frag_lookup __P((ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **, ipfrwlock_t *));
88 static void ipf_frag_deref __P((void *, ipfr_t **, ipfrwlock_t *));
89 static int ipf_frag_next __P((ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
90 ipfr_t **, ipfrwlock_t *));
92 static ipfr_t *ipfr_frag_new __P((ipf_main_softc_t *, ipf_frag_softc_t *,
93 fr_info_t *, u_32_t, ipfr_t **));
94 static ipfr_t *ipf_frag_lookup __P((ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **));
95 static void ipf_frag_deref __P((void *, ipfr_t **));
96 static int ipf_frag_next __P((ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
99 static void ipf_frag_delete __P((ipf_main_softc_t *, ipfr_t *, ipfr_t ***));
100 static void ipf_frag_free __P((ipf_frag_softc_t *, ipfr_t *));
102 static frentry_t ipfr_block;
104 static ipftuneable_t ipf_frag_tuneables[] = {
105 { { (void *)offsetof(ipf_frag_softc_t, ipfr_size) },
106 "frag_size", 1, 0x7fffffff,
107 stsizeof(ipf_frag_softc_t, ipfr_size),
108 IPFT_WRDISABLED, NULL, NULL },
109 { { (void *)offsetof(ipf_frag_softc_t, ipfr_ttl) },
110 "frag_ttl", 1, 0x7fffffff,
111 stsizeof(ipf_frag_softc_t, ipfr_ttl),
119 #define FBUMP(x) softf->ipfr_stats.x++
120 #define FBUMPD(x) do { softf->ipfr_stats.x++; DT(x); } while (0)
123 /* ------------------------------------------------------------------------ */
124 /* Function: ipf_frag_main_load */
125 /* Returns: int - 0 == success, -1 == error */
126 /* Parameters: Nil */
128 /* Initialise the filter rule associted with blocked packets - everyone can */
130 /* ------------------------------------------------------------------------ */
134 bzero((char *)&ipfr_block, sizeof(ipfr_block));
135 ipfr_block.fr_flags = FR_BLOCK|FR_QUICK;
136 ipfr_block.fr_ref = 1;
142 /* ------------------------------------------------------------------------ */
143 /* Function: ipf_frag_main_unload */
144 /* Returns: int - 0 == success, -1 == error */
145 /* Parameters: Nil */
147 /* A null-op function that exists as a placeholder so that the flow in */
148 /* other functions is obvious. */
149 /* ------------------------------------------------------------------------ */
151 ipf_frag_main_unload()
157 /* ------------------------------------------------------------------------ */
158 /* Function: ipf_frag_soft_create */
159 /* Returns: void * - NULL = failure, else pointer to local context */
160 /* Parameters: softc(I) - pointer to soft context main structure */
162 /* Allocate a new soft context structure to track fragment related info. */
163 /* ------------------------------------------------------------------------ */
166 ipf_frag_soft_create(softc)
167 ipf_main_softc_t *softc;
169 ipf_frag_softc_t *softf;
171 KMALLOC(softf, ipf_frag_softc_t *);
175 bzero((char *)softf, sizeof(*softf));
177 RWLOCK_INIT(&softf->ipfr_ipidfrag, "frag ipid lock");
178 RWLOCK_INIT(&softf->ipfr_frag, "ipf fragment rwlock");
179 RWLOCK_INIT(&softf->ipfr_natfrag, "ipf NAT fragment rwlock");
181 softf->ipf_frag_tune = ipf_tune_array_copy(softf,
182 sizeof(ipf_frag_tuneables),
184 if (softf->ipf_frag_tune == NULL) {
185 ipf_frag_soft_destroy(softc, softf);
188 if (ipf_tune_array_link(softc, softf->ipf_frag_tune) == -1) {
189 ipf_frag_soft_destroy(softc, softf);
193 softf->ipfr_size = IPFT_SIZE;
194 softf->ipfr_ttl = IPF_TTLVAL(60);
195 softf->ipfr_lock = 1;
196 softf->ipfr_tail = &softf->ipfr_list;
197 softf->ipfr_nattail = &softf->ipfr_natlist;
198 softf->ipfr_ipidtail = &softf->ipfr_ipidlist;
204 /* ------------------------------------------------------------------------ */
205 /* Function: ipf_frag_soft_destroy */
207 /* Parameters: softc(I) - pointer to soft context main structure */
208 /* arg(I) - pointer to local context to use */
210 /* Initialise the hash tables for the fragment cache lookups. */
211 /* ------------------------------------------------------------------------ */
213 ipf_frag_soft_destroy(softc, arg)
214 ipf_main_softc_t *softc;
217 ipf_frag_softc_t *softf = arg;
219 RW_DESTROY(&softf->ipfr_ipidfrag);
220 RW_DESTROY(&softf->ipfr_frag);
221 RW_DESTROY(&softf->ipfr_natfrag);
223 if (softf->ipf_frag_tune != NULL) {
224 ipf_tune_array_unlink(softc, softf->ipf_frag_tune);
225 KFREES(softf->ipf_frag_tune, sizeof(ipf_frag_tuneables));
226 softf->ipf_frag_tune = NULL;
233 /* ------------------------------------------------------------------------ */
234 /* Function: ipf_frag_soft_init */
235 /* Returns: int - 0 == success, -1 == error */
236 /* Parameters: softc(I) - pointer to soft context main structure */
237 /* arg(I) - pointer to local context to use */
239 /* Initialise the hash tables for the fragment cache lookups. */
240 /* ------------------------------------------------------------------------ */
243 ipf_frag_soft_init(softc, arg)
244 ipf_main_softc_t *softc;
247 ipf_frag_softc_t *softf = arg;
249 KMALLOCS(softf->ipfr_heads, ipfr_t **,
250 softf->ipfr_size * sizeof(ipfr_t *));
251 if (softf->ipfr_heads == NULL)
254 bzero((char *)softf->ipfr_heads, softf->ipfr_size * sizeof(ipfr_t *));
256 KMALLOCS(softf->ipfr_nattab, ipfr_t **,
257 softf->ipfr_size * sizeof(ipfr_t *));
258 if (softf->ipfr_nattab == NULL)
261 bzero((char *)softf->ipfr_nattab, softf->ipfr_size * sizeof(ipfr_t *));
263 KMALLOCS(softf->ipfr_ipidtab, ipfr_t **,
264 softf->ipfr_size * sizeof(ipfr_t *));
265 if (softf->ipfr_ipidtab == NULL)
268 bzero((char *)softf->ipfr_ipidtab,
269 softf->ipfr_size * sizeof(ipfr_t *));
271 softf->ipfr_lock = 0;
272 softf->ipfr_inited = 1;
278 /* ------------------------------------------------------------------------ */
279 /* Function: ipf_frag_soft_fini */
280 /* Returns: int - 0 == success, -1 == error */
281 /* Parameters: softc(I) - pointer to soft context main structure */
282 /* arg(I) - pointer to local context to use */
284 /* Free all memory allocated whilst running and from initialisation. */
285 /* ------------------------------------------------------------------------ */
287 ipf_frag_soft_fini(softc, arg)
288 ipf_main_softc_t *softc;
291 ipf_frag_softc_t *softf = arg;
293 softf->ipfr_lock = 1;
295 if (softf->ipfr_inited == 1) {
296 ipf_frag_clear(softc);
298 softf->ipfr_inited = 0;
301 if (softf->ipfr_heads != NULL)
302 KFREES(softf->ipfr_heads,
303 softf->ipfr_size * sizeof(ipfr_t *));
304 softf->ipfr_heads = NULL;
306 if (softf->ipfr_nattab != NULL)
307 KFREES(softf->ipfr_nattab,
308 softf->ipfr_size * sizeof(ipfr_t *));
309 softf->ipfr_nattab = NULL;
311 if (softf->ipfr_ipidtab != NULL)
312 KFREES(softf->ipfr_ipidtab,
313 softf->ipfr_size * sizeof(ipfr_t *));
314 softf->ipfr_ipidtab = NULL;
320 /* ------------------------------------------------------------------------ */
321 /* Function: ipf_frag_set_lock */
323 /* Parameters: arg(I) - pointer to local context to use */
324 /* tmp(I) - new value for lock */
326 /* Stub function that allows for external manipulation of ipfr_lock */
327 /* ------------------------------------------------------------------------ */
329 ipf_frag_setlock(arg, tmp)
333 ipf_frag_softc_t *softf = arg;
335 softf->ipfr_lock = tmp;
339 /* ------------------------------------------------------------------------ */
340 /* Function: ipf_frag_stats */
341 /* Returns: ipfrstat_t* - pointer to struct with current frag stats */
342 /* Parameters: arg(I) - pointer to local context to use */
344 /* Updates ipfr_stats with current information and returns a pointer to it */
345 /* ------------------------------------------------------------------------ */
350 ipf_frag_softc_t *softf = arg;
352 softf->ipfr_stats.ifs_table = softf->ipfr_heads;
353 softf->ipfr_stats.ifs_nattab = softf->ipfr_nattab;
354 return &softf->ipfr_stats;
358 /* ------------------------------------------------------------------------ */
359 /* Function: ipfr_frag_new */
360 /* Returns: ipfr_t * - pointer to fragment cache state info or NULL */
361 /* Parameters: fin(I) - pointer to packet information */
362 /* table(I) - pointer to frag table to add to */
363 /* lock(I) - pointer to lock to get a write hold of */
365 /* Add a new entry to the fragment cache, registering it as having come */
366 /* through this box, with the result of the filter operation. */
368 /* If this function succeeds, it returns with a write lock held on "lock". */
369 /* If it fails, no lock is held on return. */
370 /* ------------------------------------------------------------------------ */
372 ipfr_frag_new(softc, softf, fin, pass, table
377 ipf_main_softc_t *softc;
378 ipf_frag_softc_t *softf;
386 ipfr_t *fra, frag, *fran;
390 if (softf->ipfr_stats.ifs_inuse >= softf->ipfr_size) {
395 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) != FI_FRAG) {
400 if (pass & FR_FRSTRICT) {
401 if (fin->fin_off != 0) {
402 FBUMPD(ifs_newrestrictnot0);
407 frag.ipfr_v = fin->fin_v;
409 frag.ipfr_p = fin->fin_p;
411 frag.ipfr_id = fin->fin_id;
413 frag.ipfr_source = fin->fin_fi.fi_src;
414 idx += frag.ipfr_src.s_addr;
415 frag.ipfr_dest = fin->fin_fi.fi_dst;
416 idx += frag.ipfr_dst.s_addr;
417 frag.ipfr_ifp = fin->fin_ifp;
419 idx %= softf->ipfr_size;
421 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
422 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
423 frag.ipfr_auth = fin->fin_fi.fi_auth;
425 off = fin->fin_off >> 3;
431 if (fin->fin_v == 6) {
433 ptr = (char *)fin->fin_fraghdr +
434 sizeof(struct ip6_frag);
440 end = fin->fin_plen - (ptr - (char *)fin->fin_ip);
441 frag.ipfr_firstend = end >> 3;
443 frag.ipfr_firstend = 0;
447 * allocate some memory, if possible, if not, just record that we
450 KMALLOC(fran, ipfr_t *);
459 * first, make sure it isn't already there...
461 for (fra = table[idx]; (fra != NULL); fra = fra->ipfr_hnext)
462 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp,
475 MUTEX_ENTER(&fr->fr_lock);
477 MUTEX_EXIT(&fr->fr_lock);
481 * Insert the fragment into the fragment table, copy the struct used
482 * in the search using bcopy rather than reassign each field.
483 * Set the ttl to the default.
485 if ((fra->ipfr_hnext = table[idx]) != NULL)
486 table[idx]->ipfr_hprev = &fra->ipfr_hnext;
487 fra->ipfr_hprev = table + idx;
488 fra->ipfr_data = NULL;
490 bcopy((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp, IPFR_CMPSZ);
491 fra->ipfr_v = fin->fin_v;
492 fra->ipfr_ttl = softc->ipf_ticks + softf->ipfr_ttl;
493 fra->ipfr_firstend = frag.ipfr_firstend;
496 * Compute the offset of the expected start of the next packet.
500 fra->ipfr_off = off + (fin->fin_dlen >> 3);
501 fra->ipfr_pass = pass;
504 fra->ipfr_bytes = fin->fin_plen;
511 /* ------------------------------------------------------------------------ */
512 /* Function: ipf_frag_new */
513 /* Returns: int - 0 == success, -1 == error */
514 /* Parameters: fin(I) - pointer to packet information */
516 /* Add a new entry to the fragment cache table based on the current packet */
517 /* ------------------------------------------------------------------------ */
519 ipf_frag_new(softc, fin, pass)
520 ipf_main_softc_t *softc;
524 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
527 if (softf->ipfr_lock != 0)
531 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads, &softc->ipf_frag);
533 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads);
536 *softf->ipfr_tail = fra;
537 fra->ipfr_prev = softf->ipfr_tail;
538 softf->ipfr_tail = &fra->ipfr_next;
539 fra->ipfr_next = NULL;
540 RWLOCK_EXIT(&softc->ipf_frag);
546 /* ------------------------------------------------------------------------ */
547 /* Function: ipf_frag_natnew */
548 /* Returns: int - 0 == success, -1 == error */
549 /* Parameters: fin(I) - pointer to packet information */
550 /* nat(I) - pointer to NAT structure */
552 /* Create a new NAT fragment cache entry based on the current packet and */
553 /* the NAT structure for this "session". */
554 /* ------------------------------------------------------------------------ */
556 ipf_frag_natnew(softc, fin, pass, nat)
557 ipf_main_softc_t *softc;
562 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
565 if (softf->ipfr_lock != 0)
569 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab,
570 &softf->ipfr_natfrag);
572 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab);
575 fra->ipfr_data = nat;
577 *softf->ipfr_nattail = fra;
578 fra->ipfr_prev = softf->ipfr_nattail;
579 softf->ipfr_nattail = &fra->ipfr_next;
580 fra->ipfr_next = NULL;
581 RWLOCK_EXIT(&softf->ipfr_natfrag);
588 /* ------------------------------------------------------------------------ */
589 /* Function: ipf_frag_ipidnew */
590 /* Returns: int - 0 == success, -1 == error */
591 /* Parameters: fin(I) - pointer to packet information */
592 /* ipid(I) - new IP ID for this fragmented packet */
594 /* Create a new fragment cache entry for this packet and store, as a data */
595 /* pointer, the new IP ID value. */
596 /* ------------------------------------------------------------------------ */
598 ipf_frag_ipidnew(fin, ipid)
602 ipf_main_softc_t *softc = fin->fin_main_soft;
603 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
606 if (softf->ipfr_lock)
610 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab, &softf->ipfr_ipidfrag);
612 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab);
615 fra->ipfr_data = (void *)(intptr_t)ipid;
616 *softf->ipfr_ipidtail = fra;
617 fra->ipfr_prev = softf->ipfr_ipidtail;
618 softf->ipfr_ipidtail = &fra->ipfr_next;
619 fra->ipfr_next = NULL;
620 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
626 /* ------------------------------------------------------------------------ */
627 /* Function: ipf_frag_lookup */
628 /* Returns: ipfr_t * - pointer to ipfr_t structure if there's a */
629 /* matching entry in the frag table, else NULL */
630 /* Parameters: fin(I) - pointer to packet information */
631 /* table(I) - pointer to fragment cache table to search */
633 /* Check the fragment cache to see if there is already a record of this */
634 /* packet with its filter result known. */
636 /* If this function succeeds, it returns with a write lock held on "lock". */
637 /* If it fails, no lock is held on return. */
638 /* ------------------------------------------------------------------------ */
640 ipf_frag_lookup(softc, softf, fin, table
645 ipf_main_softc_t *softc;
646 ipf_frag_softc_t *softf;
657 * We don't want to let short packets match because they could be
658 * compromising the security of other rules that want to match on
659 * layer 4 fields (and can't because they have been fragmented off.)
660 * Why do this check here? The counter acts as an indicator of this
661 * kind of attack, whereas if it was elsewhere, it wouldn't know if
662 * other matching packets had been seen.
664 if (fin->fin_flx & FI_SHORT) {
669 if ((fin->fin_flx & FI_BAD) != 0) {
675 * For fragments, we record protocol, packet id, TOS and both IP#'s
676 * (these should all be the same for all fragments of a packet).
678 * build up a hash value to index the table with.
680 frag.ipfr_v = fin->fin_v;
682 frag.ipfr_p = fin->fin_p;
684 frag.ipfr_id = fin->fin_id;
686 frag.ipfr_source = fin->fin_fi.fi_src;
687 idx += frag.ipfr_src.s_addr;
688 frag.ipfr_dest = fin->fin_fi.fi_dst;
689 idx += frag.ipfr_dst.s_addr;
690 frag.ipfr_ifp = fin->fin_ifp;
692 idx %= softf->ipfr_size;
694 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
695 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
696 frag.ipfr_auth = fin->fin_fi.fi_auth;
701 * check the table, careful to only compare the right amount of data
703 for (f = table[idx]; f; f = f->ipfr_hnext) {
704 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&f->ipfr_ifp,
709 * XXX - We really need to be guarding against the
710 * retransmission of (src,dst,id,offset-range) here
711 * because a fragmented packet is never resent with
712 * the same IP ID# (or shouldn't).
714 off = fin->fin_off >> 3;
717 FBUMPD(ifs_retrans0);
722 * Case 3. See comment for frpr_fragment6.
724 if ((f->ipfr_firstend != 0) &&
725 (off < f->ipfr_firstend)) {
727 DT2(ifs_overlap, u_short, off,
729 DT3(ipf_fi_bad_ifs_overlap, fr_info_t *, fin, u_short, off,
731 fin->fin_flx |= FI_BAD;
737 if (f != table[idx] && MUTEX_TRY_UPGRADE(lock)) {
741 * Move fragment info. to the top of the list
742 * to speed up searches. First, delink...
745 (*fp) = f->ipfr_hnext;
746 if (f->ipfr_hnext != NULL)
747 f->ipfr_hnext->ipfr_hprev = fp;
749 * Then put back at the top of the chain.
751 f->ipfr_hnext = table[idx];
752 table[idx]->ipfr_hprev = &f->ipfr_hnext;
753 f->ipfr_hprev = table + idx;
755 MUTEX_DOWNGRADE(lock);
759 * If we've follwed the fragments, and this is the
760 * last (in order), shrink expiration time.
762 if (off == f->ipfr_off) {
763 f->ipfr_off = (fin->fin_dlen >> 3) + off;
766 * Well, we could shrink the expiration time
767 * but only if every fragment has been seen
768 * in order upto this, the last. ipfr_badorder
769 * is used here to count those out of order
770 * and if it equals 0 when we get to the last
771 * fragment then we can assume all of the
772 * fragments have been seen and in order.
776 * Doing this properly requires moving it to
777 * the head of the list which is infesible.
779 if ((more == 0) && (f->ipfr_badorder == 0))
780 f->ipfr_ttl = softc->ipf_ticks + 1;
784 FBUMPD(ifs_unordered);
785 if (f->ipfr_pass & FR_FRSTRICT) {
791 f->ipfr_bytes += fin->fin_plen;
803 /* ------------------------------------------------------------------------ */
804 /* Function: ipf_frag_natknown */
805 /* Returns: nat_t* - pointer to 'parent' NAT structure if frag table */
806 /* match found, else NULL */
807 /* Parameters: fin(I) - pointer to packet information */
809 /* Functional interface for NAT lookups of the NAT fragment cache */
810 /* ------------------------------------------------------------------------ */
812 ipf_frag_natknown(fin)
815 ipf_main_softc_t *softc = fin->fin_main_soft;
816 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
820 if ((softf->ipfr_lock) || !softf->ipfr_natlist)
823 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab,
824 &softf->ipfr_natfrag);
826 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab);
829 nat = ipf->ipfr_data;
831 * This is the last fragment for this packet.
833 if ((ipf->ipfr_ttl == softc->ipf_ticks + 1) && (nat != NULL)) {
834 nat->nat_data = NULL;
835 ipf->ipfr_data = NULL;
837 RWLOCK_EXIT(&softf->ipfr_natfrag);
844 /* ------------------------------------------------------------------------ */
845 /* Function: ipf_frag_ipidknown */
846 /* Returns: u_32_t - IPv4 ID for this packet if match found, else */
847 /* return 0xfffffff to indicate no match. */
848 /* Parameters: fin(I) - pointer to packet information */
850 /* Functional interface for IP ID lookups of the IP ID fragment cache */
851 /* ------------------------------------------------------------------------ */
853 ipf_frag_ipidknown(fin)
856 ipf_main_softc_t *softc = fin->fin_main_soft;
857 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
861 if (softf->ipfr_lock || !softf->ipfr_ipidlist)
865 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab,
866 &softf->ipfr_ipidfrag);
868 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab);
871 id = (u_32_t)(intptr_t)ipf->ipfr_data;
872 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
879 /* ------------------------------------------------------------------------ */
880 /* Function: ipf_frag_known */
881 /* Returns: frentry_t* - pointer to filter rule if a match is found in */
882 /* the frag cache table, else NULL. */
883 /* Parameters: fin(I) - pointer to packet information */
884 /* passp(O) - pointer to where to store rule flags resturned */
886 /* Functional interface for normal lookups of the fragment cache. If a */
887 /* match is found, return the rule pointer and flags from the rule, except */
888 /* that if FR_LOGFIRST is set, reset FR_LOG. */
889 /* ------------------------------------------------------------------------ */
891 ipf_frag_known(fin, passp)
895 ipf_main_softc_t *softc = fin->fin_main_soft;
896 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
897 frentry_t *fr = NULL;
901 if ((softf->ipfr_lock) || (softf->ipfr_list == NULL))
905 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads,
908 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads);
911 if (fin->fin_flx & FI_BAD) {
913 fin->fin_reason = FRB_BADFRAG;
914 DT2(ipf_frb_badfrag, fr_info_t *, fin, uint, fra);
921 if ((pass & FR_KEEPSTATE) != 0) {
922 fin->fin_flx |= FI_STATE;
924 * Reset the keep state flag here so that we
925 * don't try and add a new state entry because
926 * of a match here. That leads to blocking of
927 * the packet later because the add fails.
929 pass &= ~FR_KEEPSTATE;
931 if ((pass & FR_LOGFIRST) != 0)
932 pass &= ~(FR_LOGFIRST|FR_LOG);
935 RWLOCK_EXIT(&softc->ipf_frag);
941 /* ------------------------------------------------------------------------ */
942 /* Function: ipf_frag_natforget */
944 /* Parameters: softc(I) - pointer to soft context main structure */
945 /* ptr(I) - pointer to data structure */
947 /* Search through all of the fragment cache entries for NAT and wherever a */
948 /* pointer is found to match ptr, reset it to NULL. */
949 /* ------------------------------------------------------------------------ */
951 ipf_frag_natforget(softc, ptr)
952 ipf_main_softc_t *softc;
955 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
958 WRITE_ENTER(&softf->ipfr_natfrag);
959 for (fr = softf->ipfr_natlist; fr; fr = fr->ipfr_next)
960 if (fr->ipfr_data == ptr)
961 fr->ipfr_data = NULL;
962 RWLOCK_EXIT(&softf->ipfr_natfrag);
966 /* ------------------------------------------------------------------------ */
967 /* Function: ipf_frag_delete */
969 /* Parameters: softc(I) - pointer to soft context main structure */
970 /* fra(I) - pointer to fragment structure to delete */
971 /* tail(IO) - pointer to the pointer to the tail of the frag */
974 /* Remove a fragment cache table entry from the table & list. Also free */
975 /* the filter rule it is associated with it if it is no longer used as a */
976 /* result of decreasing the reference count. */
977 /* ------------------------------------------------------------------------ */
979 ipf_frag_delete(softc, fra, tail)
980 ipf_main_softc_t *softc;
981 ipfr_t *fra, ***tail;
983 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
986 fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
987 *fra->ipfr_prev = fra->ipfr_next;
988 if (*tail == &fra->ipfr_next)
989 *tail = fra->ipfr_prev;
992 fra->ipfr_hnext->ipfr_hprev = fra->ipfr_hprev;
993 *fra->ipfr_hprev = fra->ipfr_hnext;
995 if (fra->ipfr_rule != NULL) {
996 (void) ipf_derefrule(softc, &fra->ipfr_rule);
999 if (fra->ipfr_ref <= 0)
1000 ipf_frag_free(softf, fra);
1004 /* ------------------------------------------------------------------------ */
1005 /* Function: ipf_frag_free */
1007 /* Parameters: softf(I) - pointer to fragment context information */
1008 /* fra(I) - pointer to fragment structure to free */
1010 /* Free up a fragment cache entry and bump relevent statistics. */
1011 /* ------------------------------------------------------------------------ */
1013 ipf_frag_free(softf, fra)
1014 ipf_frag_softc_t *softf;
1019 softf->ipfr_stats.ifs_inuse--;
1023 /* ------------------------------------------------------------------------ */
1024 /* Function: ipf_frag_clear */
1026 /* Parameters: softc(I) - pointer to soft context main structure */
1028 /* Free memory in use by fragment state information kept. Do the normal */
1029 /* fragment state stuff first and then the NAT-fragment table. */
1030 /* ------------------------------------------------------------------------ */
1032 ipf_frag_clear(softc)
1033 ipf_main_softc_t *softc;
1035 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1039 WRITE_ENTER(&softc->ipf_frag);
1040 while ((fra = softf->ipfr_list) != NULL) {
1042 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1044 softf->ipfr_tail = &softf->ipfr_list;
1045 RWLOCK_EXIT(&softc->ipf_frag);
1047 WRITE_ENTER(&softc->ipf_nat);
1048 WRITE_ENTER(&softf->ipfr_natfrag);
1049 while ((fra = softf->ipfr_natlist) != NULL) {
1050 nat = fra->ipfr_data;
1052 if (nat->nat_data == fra)
1053 nat->nat_data = NULL;
1056 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1058 softf->ipfr_nattail = &softf->ipfr_natlist;
1059 RWLOCK_EXIT(&softf->ipfr_natfrag);
1060 RWLOCK_EXIT(&softc->ipf_nat);
1064 /* ------------------------------------------------------------------------ */
1065 /* Function: ipf_frag_expire */
1067 /* Parameters: softc(I) - pointer to soft context main structure */
1069 /* Expire entries in the fragment cache table that have been there too long */
1070 /* ------------------------------------------------------------------------ */
1072 ipf_frag_expire(softc)
1073 ipf_main_softc_t *softc;
1075 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1080 if (softf->ipfr_lock)
1084 WRITE_ENTER(&softc->ipf_frag);
1086 * Go through the entire table, looking for entries to expire,
1087 * which is indicated by the ttl being less than or equal to ipf_ticks.
1089 for (fp = &softf->ipfr_list; ((fra = *fp) != NULL); ) {
1090 if (fra->ipfr_ttl > softc->ipf_ticks)
1093 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1095 RWLOCK_EXIT(&softc->ipf_frag);
1097 WRITE_ENTER(&softf->ipfr_ipidfrag);
1098 for (fp = &softf->ipfr_ipidlist; ((fra = *fp) != NULL); ) {
1099 if (fra->ipfr_ttl > softc->ipf_ticks)
1102 ipf_frag_delete(softc, fra, &softf->ipfr_ipidtail);
1104 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
1107 * Same again for the NAT table, except that if the structure also
1108 * still points to a NAT structure, and the NAT structure points back
1109 * at the one to be free'd, NULL the reference from the NAT struct.
1110 * NOTE: We need to grab both mutex's early, and in this order so as
1111 * to prevent a deadlock if both try to expire at the same time.
1112 * The extra if() statement here is because it locks out all NAT
1113 * operations - no need to do that if there are no entries in this
1116 if (softf->ipfr_natlist != NULL) {
1117 WRITE_ENTER(&softc->ipf_nat);
1118 WRITE_ENTER(&softf->ipfr_natfrag);
1119 for (fp = &softf->ipfr_natlist; ((fra = *fp) != NULL); ) {
1120 if (fra->ipfr_ttl > softc->ipf_ticks)
1122 nat = fra->ipfr_data;
1124 if (nat->nat_data == fra)
1125 nat->nat_data = NULL;
1128 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1130 RWLOCK_EXIT(&softf->ipfr_natfrag);
1131 RWLOCK_EXIT(&softc->ipf_nat);
1137 /* ------------------------------------------------------------------------ */
1138 /* Function: ipf_frag_pkt_next */
1139 /* Returns: int - 0 == success, else error */
1140 /* Parameters: softc(I) - pointer to soft context main structure */
1141 /* token(I) - pointer to token information for this caller */
1142 /* itp(I) - pointer to generic iterator from caller */
1144 /* This function is used to step through the fragment cache list used for */
1145 /* filter rules. The hard work is done by the more generic ipf_frag_next. */
1146 /* ------------------------------------------------------------------------ */
1148 ipf_frag_pkt_next(softc, token, itp)
1149 ipf_main_softc_t *softc;
1153 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1156 return ipf_frag_next(softc, token, itp, &softf->ipfr_list,
1159 return ipf_frag_next(softc, token, itp, &softf->ipfr_list);
1164 /* ------------------------------------------------------------------------ */
1165 /* Function: ipf_frag_nat_next */
1166 /* Returns: int - 0 == success, else error */
1167 /* Parameters: softc(I) - pointer to soft context main structure */
1168 /* token(I) - pointer to token information for this caller */
1169 /* itp(I) - pointer to generic iterator from caller */
1171 /* This function is used to step through the fragment cache list used for */
1172 /* NAT. The hard work is done by the more generic ipf_frag_next. */
1173 /* ------------------------------------------------------------------------ */
1175 ipf_frag_nat_next(softc, token, itp)
1176 ipf_main_softc_t *softc;
1180 ipf_frag_softc_t *softf = softc->ipf_frag_soft;;
1183 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist,
1184 &softf->ipfr_natfrag);
1186 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist);
1190 /* ------------------------------------------------------------------------ */
1191 /* Function: ipf_frag_next */
1192 /* Returns: int - 0 == success, else error */
1193 /* Parameters: softc(I) - pointer to soft context main structure */
1194 /* token(I) - pointer to token information for this caller */
1195 /* itp(I) - pointer to generic iterator from caller */
1196 /* top(I) - top of the fragment list */
1197 /* lock(I) - fragment cache lock */
1199 /* This function is used to interate through the list of entries in the */
1200 /* fragment cache. It increases the reference count on the one currently */
1201 /* being returned so that the caller can come back and resume from it later.*/
1203 /* This function is used for both the NAT fragment cache as well as the ipf */
1204 /* fragment cache - hence the reason for passing in top and lock. */
1205 /* ------------------------------------------------------------------------ */
1207 ipf_frag_next(softc, token, itp, top
1212 ipf_main_softc_t *softc;
1220 ipfr_t *frag, *next, zero;
1223 if (itp->igi_data == NULL) {
1228 if (itp->igi_nitems != 1) {
1233 frag = token->ipt_data;
1240 next = frag->ipfr_next;
1243 ATOMIC_INC(next->ipfr_ref);
1244 token->ipt_data = next;
1246 bzero(&zero, sizeof(zero));
1248 token->ipt_data = NULL;
1250 if (next->ipfr_next == NULL)
1251 ipf_token_mark_complete(token);
1255 error = COPYOUT(next, itp->igi_data, sizeof(*next));
1261 ipf_frag_deref(softc, &frag, lock);
1263 ipf_frag_deref(softc, &frag);
1270 /* ------------------------------------------------------------------------ */
1271 /* Function: ipf_frag_pkt_deref */
1273 /* Parameters: softc(I) - pointer to soft context main structure */
1274 /* data(I) - pointer to frag cache pointer */
1276 /* This function is the external interface for dropping a reference to a */
1277 /* fragment cache entry used by filter rules. */
1278 /* ------------------------------------------------------------------------ */
1280 ipf_frag_pkt_deref(softc, data)
1281 ipf_main_softc_t *softc;
1284 ipfr_t **frp = data;
1287 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1289 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_frag);
1291 ipf_frag_deref(softc->ipf_frag_soft, frp);
1296 /* ------------------------------------------------------------------------ */
1297 /* Function: ipf_frag_nat_deref */
1299 /* Parameters: softc(I) - pointer to soft context main structure */
1300 /* data(I) - pointer to frag cache pointer */
1302 /* This function is the external interface for dropping a reference to a */
1303 /* fragment cache entry used by NAT table entries. */
1304 /* ------------------------------------------------------------------------ */
1306 ipf_frag_nat_deref(softc, data)
1307 ipf_main_softc_t *softc;
1310 ipfr_t **frp = data;
1313 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1315 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_natfrag);
1317 ipf_frag_deref(softc->ipf_frag_soft, frp);
1322 /* ------------------------------------------------------------------------ */
1323 /* Function: ipf_frag_deref */
1325 /* Parameters: frp(IO) - pointer to fragment structure to deference */
1326 /* lock(I) - lock associated with the fragment */
1328 /* This function dereferences a fragment structure (ipfr_t). The pointer */
1329 /* passed in will always be reset back to NULL, even if the structure is */
1330 /* not freed, to enforce the notion that the caller is no longer entitled */
1331 /* to use the pointer it is dropping the reference to. */
1332 /* ------------------------------------------------------------------------ */
1334 ipf_frag_deref(arg, frp
1345 ipf_frag_softc_t *softf = arg;
1353 if (fra->ipfr_ref <= 0)
1354 ipf_frag_free(softf, fra);