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>
20 # include <sys/timeout.h>
33 #if defined(_KERNEL) && \
34 defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)
35 # include <sys/filio.h>
36 # include <sys/fcntl.h>
38 # include <sys/ioctl.h>
41 # include <sys/protosw.h>
43 #include <sys/socket.h>
45 # include <sys/systm.h>
46 # if !defined(__SVR4) && !defined(__svr4__)
47 # include <sys/mbuf.h>
50 #if !defined(__SVR4) && !defined(__svr4__)
51 # if defined(_KERNEL) && !defined(__sgi) && !defined(AIX)
52 # include <sys/kernel.h>
55 # include <sys/byteorder.h>
57 # include <sys/dditypes.h>
59 # include <sys/stream.h>
60 # include <sys/kmem.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
70 # include <netinet/ip_var.h>
72 #include <netinet/tcp.h>
73 #include <netinet/udp.h>
74 #include <netinet/ip_icmp.h>
75 #include "netinet/ip_compat.h"
76 #include <netinet/tcpip.h>
77 #include "netinet/ip_fil.h"
78 #include "netinet/ip_nat.h"
79 #include "netinet/ip_frag.h"
80 #include "netinet/ip_state.h"
81 #include "netinet/ip_auth.h"
82 #include "netinet/ip_lookup.h"
83 #include "netinet/ip_proxy.h"
84 #include "netinet/ip_sync.h"
88 static const char sccsid[] = "@(#)ip_frag.c 1.11 3/24/96 (C) 1993-2000 Darren Reed";
89 static const char rcsid[] = "@(#)$FreeBSD$";
90 /* static const char rcsid[] = "@(#)$Id: ip_frag.c,v 2.77.2.12 2007/09/20 12:51:51 darrenr Exp $"; */
95 static ipfr_t *ipfr_frag_new __P((ipf_main_softc_t *, ipf_frag_softc_t *,
96 fr_info_t *, u_32_t, ipfr_t **,
98 static ipfr_t *ipf_frag_lookup __P((ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **, ipfrwlock_t *));
99 static void ipf_frag_deref __P((void *, ipfr_t **, ipfrwlock_t *));
100 static int ipf_frag_next __P((ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
101 ipfr_t **, ipfrwlock_t *));
103 static ipfr_t *ipfr_frag_new __P((ipf_main_softc_t *, ipf_frag_softc_t *,
104 fr_info_t *, u_32_t, ipfr_t **));
105 static ipfr_t *ipf_frag_lookup __P((ipf_main_softc_t *, ipf_frag_softc_t *, fr_info_t *, ipfr_t **));
106 static void ipf_frag_deref __P((void *, ipfr_t **));
107 static int ipf_frag_next __P((ipf_main_softc_t *, ipftoken_t *, ipfgeniter_t *,
110 static void ipf_frag_delete __P((ipf_main_softc_t *, ipfr_t *, ipfr_t ***));
111 static void ipf_frag_free __P((ipf_frag_softc_t *, ipfr_t *));
113 static frentry_t ipfr_block;
115 const ipftuneable_t ipf_tuneables[] = {
116 { { (void *)offsetof(ipf_frag_softc_t, ipfr_size) },
117 "frag_size", 1, 0x7fffffff,
118 stsizeof(ipf_frag_softc_t, ipfr_size),
119 IPFT_WRDISABLED, NULL, NULL },
120 { { (void *)offsetof(ipf_frag_softc_t, ipfr_ttl) },
121 "frag_ttl", 1, 0x7fffffff,
122 stsizeof(ipf_frag_softc_t, ipfr_ttl),
130 #define FBUMP(x) softf->ipfr_stats.x++
131 #define FBUMPD(x) do { softf->ipfr_stats.x++; DT(x); } while (0)
134 /* ------------------------------------------------------------------------ */
135 /* Function: ipf_frag_main_load */
136 /* Returns: int - 0 == success, -1 == error */
137 /* Parameters: Nil */
139 /* Initialise the filter rule associted with blocked packets - everyone can */
141 /* ------------------------------------------------------------------------ */
145 bzero((char *)&ipfr_block, sizeof(ipfr_block));
146 ipfr_block.fr_flags = FR_BLOCK|FR_QUICK;
147 ipfr_block.fr_ref = 1;
153 /* ------------------------------------------------------------------------ */
154 /* Function: ipf_frag_main_unload */
155 /* Returns: int - 0 == success, -1 == error */
156 /* Parameters: Nil */
158 /* A null-op function that exists as a placeholder so that the flow in */
159 /* other functions is obvious. */
160 /* ------------------------------------------------------------------------ */
162 ipf_frag_main_unload()
168 /* ------------------------------------------------------------------------ */
169 /* Function: ipf_frag_soft_create */
170 /* Returns: void * - NULL = failure, else pointer to local context */
171 /* Parameters: softc(I) - pointer to soft context main structure */
173 /* Allocate a new soft context structure to track fragment related info. */
174 /* ------------------------------------------------------------------------ */
177 ipf_frag_soft_create(softc)
178 ipf_main_softc_t *softc;
180 ipf_frag_softc_t *softf;
182 KMALLOC(softf, ipf_frag_softc_t *);
186 bzero((char *)softf, sizeof(*softf));
188 RWLOCK_INIT(&softf->ipfr_ipidfrag, "frag ipid lock");
189 RWLOCK_INIT(&softf->ipfr_frag, "ipf fragment rwlock");
190 RWLOCK_INIT(&softf->ipfr_natfrag, "ipf NAT fragment rwlock");
192 softf->ipfr_size = IPFT_SIZE;
193 softf->ipfr_ttl = IPF_TTLVAL(60);
194 softf->ipfr_lock = 1;
195 softf->ipfr_tail = &softf->ipfr_list;
196 softf->ipfr_nattail = &softf->ipfr_natlist;
197 softf->ipfr_ipidtail = &softf->ipfr_ipidlist;
203 /* ------------------------------------------------------------------------ */
204 /* Function: ipf_frag_soft_destroy */
206 /* Parameters: softc(I) - pointer to soft context main structure */
207 /* arg(I) - pointer to local context to use */
209 /* Initialise the hash tables for the fragment cache lookups. */
210 /* ------------------------------------------------------------------------ */
212 ipf_frag_soft_destroy(softc, arg)
213 ipf_main_softc_t *softc;
216 ipf_frag_softc_t *softf = arg;
218 RW_DESTROY(&softf->ipfr_ipidfrag);
219 RW_DESTROY(&softf->ipfr_frag);
220 RW_DESTROY(&softf->ipfr_natfrag);
226 /* ------------------------------------------------------------------------ */
227 /* Function: ipf_frag_soft_init */
228 /* Returns: int - 0 == success, -1 == error */
229 /* Parameters: softc(I) - pointer to soft context main structure */
230 /* arg(I) - pointer to local context to use */
232 /* Initialise the hash tables for the fragment cache lookups. */
233 /* ------------------------------------------------------------------------ */
236 ipf_frag_soft_init(softc, arg)
237 ipf_main_softc_t *softc;
240 ipf_frag_softc_t *softf = arg;
242 KMALLOCS(softf->ipfr_heads, ipfr_t **,
243 softf->ipfr_size * sizeof(ipfr_t *));
244 if (softf->ipfr_heads == NULL)
247 bzero((char *)softf->ipfr_heads, softf->ipfr_size * sizeof(ipfr_t *));
249 KMALLOCS(softf->ipfr_nattab, ipfr_t **,
250 softf->ipfr_size * sizeof(ipfr_t *));
251 if (softf->ipfr_nattab == NULL)
254 bzero((char *)softf->ipfr_nattab, softf->ipfr_size * sizeof(ipfr_t *));
256 KMALLOCS(softf->ipfr_ipidtab, ipfr_t **,
257 softf->ipfr_size * sizeof(ipfr_t *));
258 if (softf->ipfr_ipidtab == NULL)
261 bzero((char *)softf->ipfr_ipidtab,
262 softf->ipfr_size * sizeof(ipfr_t *));
264 softf->ipfr_lock = 0;
265 softf->ipfr_inited = 1;
271 /* ------------------------------------------------------------------------ */
272 /* Function: ipf_frag_soft_fini */
273 /* Returns: int - 0 == success, -1 == error */
274 /* Parameters: softc(I) - pointer to soft context main structure */
275 /* arg(I) - pointer to local context to use */
277 /* Free all memory allocated whilst running and from initialisation. */
278 /* ------------------------------------------------------------------------ */
280 ipf_frag_soft_fini(softc, arg)
281 ipf_main_softc_t *softc;
284 ipf_frag_softc_t *softf = arg;
286 softf->ipfr_lock = 1;
288 if (softf->ipfr_inited == 1) {
289 ipf_frag_clear(softc);
291 softf->ipfr_inited = 0;
294 if (softf->ipfr_heads != NULL)
295 KFREES(softf->ipfr_heads,
296 softf->ipfr_size * sizeof(ipfr_t *));
297 softf->ipfr_heads = NULL;
299 if (softf->ipfr_nattab != NULL)
300 KFREES(softf->ipfr_nattab,
301 softf->ipfr_size * sizeof(ipfr_t *));
302 softf->ipfr_nattab = NULL;
304 if (softf->ipfr_ipidtab != NULL)
305 KFREES(softf->ipfr_ipidtab,
306 softf->ipfr_size * sizeof(ipfr_t *));
307 softf->ipfr_ipidtab = NULL;
313 /* ------------------------------------------------------------------------ */
314 /* Function: ipf_frag_set_lock */
316 /* Parameters: arg(I) - pointer to local context to use */
317 /* tmp(I) - new value for lock */
319 /* Stub function that allows for external manipulation of ipfr_lock */
320 /* ------------------------------------------------------------------------ */
322 ipf_frag_setlock(arg, tmp)
326 ipf_frag_softc_t *softf = arg;
328 softf->ipfr_lock = tmp;
332 /* ------------------------------------------------------------------------ */
333 /* Function: ipf_frag_stats */
334 /* Returns: ipfrstat_t* - pointer to struct with current frag stats */
335 /* Parameters: arg(I) - pointer to local context to use */
337 /* Updates ipfr_stats with current information and returns a pointer to it */
338 /* ------------------------------------------------------------------------ */
343 ipf_frag_softc_t *softf = arg;
345 softf->ipfr_stats.ifs_table = softf->ipfr_heads;
346 softf->ipfr_stats.ifs_nattab = softf->ipfr_nattab;
347 return &softf->ipfr_stats;
351 /* ------------------------------------------------------------------------ */
352 /* Function: ipfr_frag_new */
353 /* Returns: ipfr_t * - pointer to fragment cache state info or NULL */
354 /* Parameters: fin(I) - pointer to packet information */
355 /* table(I) - pointer to frag table to add to */
356 /* lock(I) - pointer to lock to get a write hold of */
358 /* Add a new entry to the fragment cache, registering it as having come */
359 /* through this box, with the result of the filter operation. */
361 /* If this function succeeds, it returns with a write lock held on "lock". */
362 /* If it fails, no lock is held on return. */
363 /* ------------------------------------------------------------------------ */
365 ipfr_frag_new(softc, softf, fin, pass, table
370 ipf_main_softc_t *softc;
371 ipf_frag_softc_t *softf;
379 ipfr_t *fra, frag, *fran;
383 if (softf->ipfr_stats.ifs_inuse >= softf->ipfr_size) {
388 if ((fin->fin_flx & (FI_FRAG|FI_BAD)) != FI_FRAG) {
393 if (pass & FR_FRSTRICT) {
394 if (fin->fin_off != 0) {
395 FBUMPD(ifs_newrestrictnot0);
400 frag.ipfr_v = fin->fin_v;
402 frag.ipfr_p = fin->fin_p;
404 frag.ipfr_id = fin->fin_id;
406 frag.ipfr_source = fin->fin_fi.fi_src;
407 idx += frag.ipfr_src.s_addr;
408 frag.ipfr_dest = fin->fin_fi.fi_dst;
409 idx += frag.ipfr_dst.s_addr;
410 frag.ipfr_ifp = fin->fin_ifp;
412 idx %= softf->ipfr_size;
414 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
415 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
416 frag.ipfr_auth = fin->fin_fi.fi_auth;
418 off = fin->fin_off >> 3;
424 if (fin->fin_v == 6) {
426 ptr = (char *)fin->fin_fraghdr +
427 sizeof(struct ip6_frag);
433 end = fin->fin_plen - (ptr - (char *)fin->fin_ip);
434 frag.ipfr_firstend = end >> 3;
436 frag.ipfr_firstend = 0;
440 * allocate some memory, if possible, if not, just record that we
443 KMALLOC(fran, ipfr_t *);
452 * first, make sure it isn't already there...
454 for (fra = table[idx]; (fra != NULL); fra = fra->ipfr_hnext)
455 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp,
468 MUTEX_ENTER(&fr->fr_lock);
470 MUTEX_EXIT(&fr->fr_lock);
474 * Insert the fragment into the fragment table, copy the struct used
475 * in the search using bcopy rather than reassign each field.
476 * Set the ttl to the default.
478 if ((fra->ipfr_hnext = table[idx]) != NULL)
479 table[idx]->ipfr_hprev = &fra->ipfr_hnext;
480 fra->ipfr_hprev = table + idx;
481 fra->ipfr_data = NULL;
483 bcopy((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp, IPFR_CMPSZ);
484 fra->ipfr_v = fin->fin_v;
485 fra->ipfr_ttl = softc->ipf_ticks + softf->ipfr_ttl;
486 fra->ipfr_firstend = frag.ipfr_firstend;
489 * Compute the offset of the expected start of the next packet.
493 fra->ipfr_off = off + (fin->fin_dlen >> 3);
494 fra->ipfr_pass = pass;
497 fra->ipfr_bytes = fin->fin_plen;
504 /* ------------------------------------------------------------------------ */
505 /* Function: ipf_frag_new */
506 /* Returns: int - 0 == success, -1 == error */
507 /* Parameters: fin(I) - pointer to packet information */
509 /* Add a new entry to the fragment cache table based on the current packet */
510 /* ------------------------------------------------------------------------ */
512 ipf_frag_new(softc, fin, pass)
513 ipf_main_softc_t *softc;
517 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
520 if (softf->ipfr_lock != 0)
524 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads, &softc->ipf_frag);
526 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads);
529 *softf->ipfr_tail = fra;
530 fra->ipfr_prev = softf->ipfr_tail;
531 softf->ipfr_tail = &fra->ipfr_next;
532 fra->ipfr_next = NULL;
533 RWLOCK_EXIT(&softc->ipf_frag);
539 /* ------------------------------------------------------------------------ */
540 /* Function: ipf_frag_natnew */
541 /* Returns: int - 0 == success, -1 == error */
542 /* Parameters: fin(I) - pointer to packet information */
543 /* nat(I) - pointer to NAT structure */
545 /* Create a new NAT fragment cache entry based on the current packet and */
546 /* the NAT structure for this "session". */
547 /* ------------------------------------------------------------------------ */
549 ipf_frag_natnew(softc, fin, pass, nat)
550 ipf_main_softc_t *softc;
555 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
558 if (softf->ipfr_lock != 0)
562 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab,
563 &softf->ipfr_natfrag);
565 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab);
568 fra->ipfr_data = nat;
570 *softf->ipfr_nattail = fra;
571 fra->ipfr_prev = softf->ipfr_nattail;
572 softf->ipfr_nattail = &fra->ipfr_next;
573 fra->ipfr_next = NULL;
574 RWLOCK_EXIT(&softf->ipfr_natfrag);
581 /* ------------------------------------------------------------------------ */
582 /* Function: ipf_frag_ipidnew */
583 /* Returns: int - 0 == success, -1 == error */
584 /* Parameters: fin(I) - pointer to packet information */
585 /* ipid(I) - new IP ID for this fragmented packet */
587 /* Create a new fragment cache entry for this packet and store, as a data */
588 /* pointer, the new IP ID value. */
589 /* ------------------------------------------------------------------------ */
591 ipf_frag_ipidnew(fin, ipid)
595 ipf_main_softc_t *softc = fin->fin_main_soft;
596 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
599 if (softf->ipfr_lock)
603 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab, &softf->ipfr_ipidfrag);
605 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab);
608 fra->ipfr_data = (void *)(intptr_t)ipid;
609 *softf->ipfr_ipidtail = fra;
610 fra->ipfr_prev = softf->ipfr_ipidtail;
611 softf->ipfr_ipidtail = &fra->ipfr_next;
612 fra->ipfr_next = NULL;
613 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
619 /* ------------------------------------------------------------------------ */
620 /* Function: ipf_frag_lookup */
621 /* Returns: ipfr_t * - pointer to ipfr_t structure if there's a */
622 /* matching entry in the frag table, else NULL */
623 /* Parameters: fin(I) - pointer to packet information */
624 /* table(I) - pointer to fragment cache table to search */
626 /* Check the fragment cache to see if there is already a record of this */
627 /* packet with its filter result known. */
629 /* If this function succeeds, it returns with a write lock held on "lock". */
630 /* If it fails, no lock is held on return. */
631 /* ------------------------------------------------------------------------ */
633 ipf_frag_lookup(softc, softf, fin, table
638 ipf_main_softc_t *softc;
639 ipf_frag_softc_t *softf;
650 * We don't want to let short packets match because they could be
651 * compromising the security of other rules that want to match on
652 * layer 4 fields (and can't because they have been fragmented off.)
653 * Why do this check here? The counter acts as an indicator of this
654 * kind of attack, whereas if it was elsewhere, it wouldn't know if
655 * other matching packets had been seen.
657 if (fin->fin_flx & FI_SHORT) {
662 if ((fin->fin_flx & FI_BAD) != 0) {
668 * For fragments, we record protocol, packet id, TOS and both IP#'s
669 * (these should all be the same for all fragments of a packet).
671 * build up a hash value to index the table with.
673 frag.ipfr_v = fin->fin_v;
675 frag.ipfr_p = fin->fin_p;
677 frag.ipfr_id = fin->fin_id;
679 frag.ipfr_source = fin->fin_fi.fi_src;
680 idx += frag.ipfr_src.s_addr;
681 frag.ipfr_dest = fin->fin_fi.fi_dst;
682 idx += frag.ipfr_dst.s_addr;
683 frag.ipfr_ifp = fin->fin_ifp;
685 idx %= softf->ipfr_size;
687 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
688 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
689 frag.ipfr_auth = fin->fin_fi.fi_auth;
694 * check the table, careful to only compare the right amount of data
696 for (f = table[idx]; f; f = f->ipfr_hnext) {
697 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&f->ipfr_ifp,
702 * XXX - We really need to be guarding against the
703 * retransmission of (src,dst,id,offset-range) here
704 * because a fragmented packet is never resent with
705 * the same IP ID# (or shouldn't).
707 off = fin->fin_off >> 3;
710 FBUMPD(ifs_retrans0);
715 * Case 3. See comment for frpr_fragment6.
717 if ((f->ipfr_firstend != 0) &&
718 (off < f->ipfr_firstend)) {
720 DT2(ifs_overlap, u_short, off,
722 fin->fin_flx |= FI_BAD;
728 if (f != table[idx]) {
732 * Move fragment info. to the top of the list
733 * to speed up searches. First, delink...
736 (*fp) = f->ipfr_hnext;
737 if (f->ipfr_hnext != NULL)
738 f->ipfr_hnext->ipfr_hprev = fp;
740 * Then put back at the top of the chain.
742 f->ipfr_hnext = table[idx];
743 table[idx]->ipfr_hprev = &f->ipfr_hnext;
744 f->ipfr_hprev = table + idx;
749 * If we've follwed the fragments, and this is the
750 * last (in order), shrink expiration time.
752 if (off == f->ipfr_off) {
753 f->ipfr_off = (fin->fin_dlen >> 3) + off;
756 * Well, we could shrink the expiration time
757 * but only if every fragment has been seen
758 * in order upto this, the last. ipfr_badorder
759 * is used here to count those out of order
760 * and if it equals 0 when we get to the last
761 * fragment then we can assume all of the
762 * fragments have been seen and in order.
766 * Doing this properly requires moving it to
767 * the head of the list which is infesible.
769 if ((more == 0) && (f->ipfr_badorder == 0))
770 f->ipfr_ttl = softc->ipf_ticks + 1;
774 FBUMPD(ifs_unordered);
775 if (f->ipfr_pass & FR_FRSTRICT) {
781 f->ipfr_bytes += fin->fin_plen;
793 /* ------------------------------------------------------------------------ */
794 /* Function: ipf_frag_natknown */
795 /* Returns: nat_t* - pointer to 'parent' NAT structure if frag table */
796 /* match found, else NULL */
797 /* Parameters: fin(I) - pointer to packet information */
799 /* Functional interface for NAT lookups of the NAT fragment cache */
800 /* ------------------------------------------------------------------------ */
802 ipf_frag_natknown(fin)
805 ipf_main_softc_t *softc = fin->fin_main_soft;
806 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
810 if ((softf->ipfr_lock) || !softf->ipfr_natlist)
813 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab,
814 &softf->ipfr_natfrag);
816 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab);
819 nat = ipf->ipfr_data;
821 * This is the last fragment for this packet.
823 if ((ipf->ipfr_ttl == softc->ipf_ticks + 1) && (nat != NULL)) {
824 nat->nat_data = NULL;
825 ipf->ipfr_data = NULL;
827 RWLOCK_EXIT(&softf->ipfr_natfrag);
834 /* ------------------------------------------------------------------------ */
835 /* Function: ipf_frag_ipidknown */
836 /* Returns: u_32_t - IPv4 ID for this packet if match found, else */
837 /* return 0xfffffff to indicate no match. */
838 /* Parameters: fin(I) - pointer to packet information */
840 /* Functional interface for IP ID lookups of the IP ID fragment cache */
841 /* ------------------------------------------------------------------------ */
843 ipf_frag_ipidknown(fin)
846 ipf_main_softc_t *softc = fin->fin_main_soft;
847 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
851 if (softf->ipfr_lock || !softf->ipfr_ipidlist)
855 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab,
856 &softf->ipfr_ipidfrag);
858 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab);
861 id = (u_32_t)(intptr_t)ipf->ipfr_data;
862 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
869 /* ------------------------------------------------------------------------ */
870 /* Function: ipf_frag_known */
871 /* Returns: frentry_t* - pointer to filter rule if a match is found in */
872 /* the frag cache table, else NULL. */
873 /* Parameters: fin(I) - pointer to packet information */
874 /* passp(O) - pointer to where to store rule flags resturned */
876 /* Functional interface for normal lookups of the fragment cache. If a */
877 /* match is found, return the rule pointer and flags from the rule, except */
878 /* that if FR_LOGFIRST is set, reset FR_LOG. */
879 /* ------------------------------------------------------------------------ */
881 ipf_frag_known(fin, passp)
885 ipf_main_softc_t *softc = fin->fin_main_soft;
886 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
887 frentry_t *fr = NULL;
891 if ((softf->ipfr_lock) || (softf->ipfr_list == NULL))
895 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads,
898 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads);
901 if (fin->fin_flx & FI_BAD) {
903 fin->fin_reason = FRB_BADFRAG;
910 if ((pass & FR_KEEPSTATE) != 0) {
911 fin->fin_flx |= FI_STATE;
913 * Reset the keep state flag here so that we
914 * don't try and add a new state entry because
915 * of a match here. That leads to blocking of
916 * the packet later because the add fails.
918 pass &= ~FR_KEEPSTATE;
920 if ((pass & FR_LOGFIRST) != 0)
921 pass &= ~(FR_LOGFIRST|FR_LOG);
924 RWLOCK_EXIT(&softc->ipf_frag);
930 /* ------------------------------------------------------------------------ */
931 /* Function: ipf_frag_natforget */
933 /* Parameters: softc(I) - pointer to soft context main structure */
934 /* ptr(I) - pointer to data structure */
936 /* Search through all of the fragment cache entries for NAT and wherever a */
937 /* pointer is found to match ptr, reset it to NULL. */
938 /* ------------------------------------------------------------------------ */
940 ipf_frag_natforget(softc, ptr)
941 ipf_main_softc_t *softc;
944 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
947 WRITE_ENTER(&softf->ipfr_natfrag);
948 for (fr = softf->ipfr_natlist; fr; fr = fr->ipfr_next)
949 if (fr->ipfr_data == ptr)
950 fr->ipfr_data = NULL;
951 RWLOCK_EXIT(&softf->ipfr_natfrag);
955 /* ------------------------------------------------------------------------ */
956 /* Function: ipf_frag_delete */
958 /* Parameters: softc(I) - pointer to soft context main structure */
959 /* fra(I) - pointer to fragment structure to delete */
960 /* tail(IO) - pointer to the pointer to the tail of the frag */
963 /* Remove a fragment cache table entry from the table & list. Also free */
964 /* the filter rule it is associated with it if it is no longer used as a */
965 /* result of decreasing the reference count. */
966 /* ------------------------------------------------------------------------ */
968 ipf_frag_delete(softc, fra, tail)
969 ipf_main_softc_t *softc;
970 ipfr_t *fra, ***tail;
972 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
975 fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
976 *fra->ipfr_prev = fra->ipfr_next;
977 if (*tail == &fra->ipfr_next)
978 *tail = fra->ipfr_prev;
981 fra->ipfr_hnext->ipfr_hprev = fra->ipfr_hprev;
982 *fra->ipfr_hprev = fra->ipfr_hnext;
984 if (fra->ipfr_rule != NULL) {
985 (void) ipf_derefrule(softc, &fra->ipfr_rule);
988 if (fra->ipfr_ref <= 0)
989 ipf_frag_free(softf, fra);
993 /* ------------------------------------------------------------------------ */
994 /* Function: ipf_frag_free */
996 /* Parameters: softf(I) - pointer to fragment context information */
997 /* fra(I) - pointer to fragment structure to free */
999 /* Free up a fragment cache entry and bump relevent statistics. */
1000 /* ------------------------------------------------------------------------ */
1002 ipf_frag_free(softf, fra)
1003 ipf_frag_softc_t *softf;
1008 softf->ipfr_stats.ifs_inuse--;
1012 /* ------------------------------------------------------------------------ */
1013 /* Function: ipf_frag_clear */
1015 /* Parameters: softc(I) - pointer to soft context main structure */
1017 /* Free memory in use by fragment state information kept. Do the normal */
1018 /* fragment state stuff first and then the NAT-fragment table. */
1019 /* ------------------------------------------------------------------------ */
1021 ipf_frag_clear(softc)
1022 ipf_main_softc_t *softc;
1024 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1028 WRITE_ENTER(&softc->ipf_frag);
1029 while ((fra = softf->ipfr_list) != NULL) {
1031 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1033 softf->ipfr_tail = &softf->ipfr_list;
1034 RWLOCK_EXIT(&softc->ipf_frag);
1036 WRITE_ENTER(&softc->ipf_nat);
1037 WRITE_ENTER(&softf->ipfr_natfrag);
1038 while ((fra = softf->ipfr_natlist) != NULL) {
1039 nat = fra->ipfr_data;
1041 if (nat->nat_data == fra)
1042 nat->nat_data = NULL;
1045 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1047 softf->ipfr_nattail = &softf->ipfr_natlist;
1048 RWLOCK_EXIT(&softf->ipfr_natfrag);
1049 RWLOCK_EXIT(&softc->ipf_nat);
1053 /* ------------------------------------------------------------------------ */
1054 /* Function: ipf_frag_expire */
1056 /* Parameters: softc(I) - pointer to soft context main structure */
1058 /* Expire entries in the fragment cache table that have been there too long */
1059 /* ------------------------------------------------------------------------ */
1061 ipf_frag_expire(softc)
1062 ipf_main_softc_t *softc;
1064 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1069 if (softf->ipfr_lock)
1073 WRITE_ENTER(&softc->ipf_frag);
1075 * Go through the entire table, looking for entries to expire,
1076 * which is indicated by the ttl being less than or equal to ipf_ticks.
1078 for (fp = &softf->ipfr_list; ((fra = *fp) != NULL); ) {
1079 if (fra->ipfr_ttl > softc->ipf_ticks)
1082 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1084 RWLOCK_EXIT(&softc->ipf_frag);
1086 WRITE_ENTER(&softf->ipfr_ipidfrag);
1087 for (fp = &softf->ipfr_ipidlist; ((fra = *fp) != NULL); ) {
1088 if (fra->ipfr_ttl > softc->ipf_ticks)
1091 ipf_frag_delete(softc, fra, &softf->ipfr_ipidtail);
1093 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
1096 * Same again for the NAT table, except that if the structure also
1097 * still points to a NAT structure, and the NAT structure points back
1098 * at the one to be free'd, NULL the reference from the NAT struct.
1099 * NOTE: We need to grab both mutex's early, and in this order so as
1100 * to prevent a deadlock if both try to expire at the same time.
1101 * The extra if() statement here is because it locks out all NAT
1102 * operations - no need to do that if there are no entries in this
1105 if (softf->ipfr_natlist != NULL) {
1106 WRITE_ENTER(&softc->ipf_nat);
1107 WRITE_ENTER(&softf->ipfr_natfrag);
1108 for (fp = &softf->ipfr_natlist; ((fra = *fp) != NULL); ) {
1109 if (fra->ipfr_ttl > softc->ipf_ticks)
1111 nat = fra->ipfr_data;
1113 if (nat->nat_data == fra)
1114 nat->nat_data = NULL;
1117 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1119 RWLOCK_EXIT(&softf->ipfr_natfrag);
1120 RWLOCK_EXIT(&softc->ipf_nat);
1126 /* ------------------------------------------------------------------------ */
1127 /* Function: ipf_frag_pkt_next */
1128 /* Returns: int - 0 == success, else error */
1129 /* Parameters: softc(I) - pointer to soft context main structure */
1130 /* token(I) - pointer to token information for this caller */
1131 /* itp(I) - pointer to generic iterator from caller */
1133 /* This function is used to step through the fragment cache list used for */
1134 /* filter rules. The hard work is done by the more generic ipf_frag_next. */
1135 /* ------------------------------------------------------------------------ */
1137 ipf_frag_pkt_next(softc, token, itp)
1138 ipf_main_softc_t *softc;
1142 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1145 return ipf_frag_next(softc, token, itp, &softf->ipfr_list,
1148 return ipf_frag_next(softc, token, itp, &softf->ipfr_list);
1153 /* ------------------------------------------------------------------------ */
1154 /* Function: ipf_frag_nat_next */
1155 /* Returns: int - 0 == success, else error */
1156 /* Parameters: softc(I) - pointer to soft context main structure */
1157 /* token(I) - pointer to token information for this caller */
1158 /* itp(I) - pointer to generic iterator from caller */
1160 /* This function is used to step through the fragment cache list used for */
1161 /* NAT. The hard work is done by the more generic ipf_frag_next. */
1162 /* ------------------------------------------------------------------------ */
1164 ipf_frag_nat_next(softc, token, itp)
1165 ipf_main_softc_t *softc;
1169 ipf_frag_softc_t *softf = softc->ipf_frag_soft;;
1172 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist,
1173 &softf->ipfr_natfrag);
1175 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist);
1179 /* ------------------------------------------------------------------------ */
1180 /* Function: ipf_frag_next */
1181 /* Returns: int - 0 == success, else error */
1182 /* Parameters: softc(I) - pointer to soft context main structure */
1183 /* token(I) - pointer to token information for this caller */
1184 /* itp(I) - pointer to generic iterator from caller */
1185 /* top(I) - top of the fragment list */
1186 /* lock(I) - fragment cache lock */
1188 /* This function is used to interate through the list of entries in the */
1189 /* fragment cache. It increases the reference count on the one currently */
1190 /* being returned so that the caller can come back and resume from it later.*/
1192 /* This function is used for both the NAT fragment cache as well as the ipf */
1193 /* fragment cache - hence the reason for passing in top and lock. */
1194 /* ------------------------------------------------------------------------ */
1196 ipf_frag_next(softc, token, itp, top
1201 ipf_main_softc_t *softc;
1209 ipfr_t *frag, *next, zero;
1212 if (itp->igi_data == NULL) {
1217 if (itp->igi_nitems != 1) {
1222 frag = token->ipt_data;
1229 next = frag->ipfr_next;
1232 ATOMIC_INC(next->ipfr_ref);
1233 token->ipt_data = next;
1235 bzero(&zero, sizeof(zero));
1237 token->ipt_data = NULL;
1239 if (next->ipfr_next == NULL)
1240 ipf_token_mark_complete(token);
1244 error = COPYOUT(next, itp->igi_data, sizeof(*next));
1250 ipf_frag_deref(softc, &frag, lock);
1252 ipf_frag_deref(softc, &frag);
1259 /* ------------------------------------------------------------------------ */
1260 /* Function: ipf_frag_pkt_deref */
1262 /* Parameters: softc(I) - pointer to soft context main structure */
1263 /* data(I) - pointer to frag cache pointer */
1265 /* This function is the external interface for dropping a reference to a */
1266 /* fragment cache entry used by filter rules. */
1267 /* ------------------------------------------------------------------------ */
1269 ipf_frag_pkt_deref(softc, data)
1270 ipf_main_softc_t *softc;
1273 ipfr_t **frp = data;
1276 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1278 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_frag);
1280 ipf_frag_deref(softc->ipf_frag_soft, frp);
1285 /* ------------------------------------------------------------------------ */
1286 /* Function: ipf_frag_nat_deref */
1288 /* Parameters: softc(I) - pointer to soft context main structure */
1289 /* data(I) - pointer to frag cache pointer */
1291 /* This function is the external interface for dropping a reference to a */
1292 /* fragment cache entry used by NAT table entries. */
1293 /* ------------------------------------------------------------------------ */
1295 ipf_frag_nat_deref(softc, data)
1296 ipf_main_softc_t *softc;
1299 ipfr_t **frp = data;
1302 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1304 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_natfrag);
1306 ipf_frag_deref(softc->ipf_frag_soft, frp);
1311 /* ------------------------------------------------------------------------ */
1312 /* Function: ipf_frag_deref */
1314 /* Parameters: frp(IO) - pointer to fragment structure to deference */
1315 /* lock(I) - lock associated with the fragment */
1317 /* This function dereferences a fragment structure (ipfr_t). The pointer */
1318 /* passed in will always be reset back to NULL, even if the structure is */
1319 /* not freed, to enforce the notion that the caller is no longer entitled */
1320 /* to use the pointer it is dropping the reference to. */
1321 /* ------------------------------------------------------------------------ */
1323 ipf_frag_deref(arg, frp
1334 ipf_frag_softc_t *softf = arg;
1342 if (fra->ipfr_ref <= 0)
1343 ipf_frag_free(softf, fra);