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>
38 # include <sys/mbuf.h>
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 memset(&frag, 0, sizeof(frag));
408 frag.ipfr_v = fin->fin_v;
410 frag.ipfr_p = fin->fin_p;
412 frag.ipfr_id = fin->fin_id;
414 frag.ipfr_source = fin->fin_fi.fi_src;
415 idx += frag.ipfr_src.s_addr;
416 frag.ipfr_dest = fin->fin_fi.fi_dst;
417 idx += frag.ipfr_dst.s_addr;
418 frag.ipfr_ifp = fin->fin_ifp;
420 idx %= softf->ipfr_size;
422 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
423 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
424 frag.ipfr_auth = fin->fin_fi.fi_auth;
426 off = fin->fin_off >> 3;
432 if (fin->fin_v == 6) {
434 ptr = (char *)fin->fin_fraghdr +
435 sizeof(struct ip6_frag);
441 end = fin->fin_plen - (ptr - (char *)fin->fin_ip);
442 frag.ipfr_firstend = end >> 3;
444 frag.ipfr_firstend = 0;
448 * allocate some memory, if possible, if not, just record that we
451 KMALLOC(fran, ipfr_t *);
456 memset(fran, 0, sizeof(*fran));
461 * first, make sure it isn't already there...
463 for (fra = table[idx]; (fra != NULL); fra = fra->ipfr_hnext)
464 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp,
477 MUTEX_ENTER(&fr->fr_lock);
479 MUTEX_EXIT(&fr->fr_lock);
483 * Insert the fragment into the fragment table, copy the struct used
484 * in the search using bcopy rather than reassign each field.
485 * Set the ttl to the default.
487 if ((fra->ipfr_hnext = table[idx]) != NULL)
488 table[idx]->ipfr_hprev = &fra->ipfr_hnext;
489 fra->ipfr_hprev = table + idx;
490 fra->ipfr_data = NULL;
492 bcopy((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp, IPFR_CMPSZ);
493 fra->ipfr_v = fin->fin_v;
494 fra->ipfr_p = fin->fin_p;
495 fra->ipfr_ttl = softc->ipf_ticks + softf->ipfr_ttl;
496 fra->ipfr_firstend = frag.ipfr_firstend;
499 * Compute the offset of the expected start of the next packet.
503 fra->ipfr_off = off + (fin->fin_dlen >> 3);
504 fra->ipfr_pass = pass;
507 fra->ipfr_bytes = fin->fin_plen;
514 /* ------------------------------------------------------------------------ */
515 /* Function: ipf_frag_new */
516 /* Returns: int - 0 == success, -1 == error */
517 /* Parameters: fin(I) - pointer to packet information */
519 /* Add a new entry to the fragment cache table based on the current packet */
520 /* ------------------------------------------------------------------------ */
522 ipf_frag_new(softc, fin, pass)
523 ipf_main_softc_t *softc;
527 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
530 if (softf->ipfr_lock != 0)
534 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads, &softc->ipf_frag);
536 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_heads);
539 *softf->ipfr_tail = fra;
540 fra->ipfr_prev = softf->ipfr_tail;
541 softf->ipfr_tail = &fra->ipfr_next;
542 fra->ipfr_next = NULL;
543 RWLOCK_EXIT(&softc->ipf_frag);
549 /* ------------------------------------------------------------------------ */
550 /* Function: ipf_frag_natnew */
551 /* Returns: int - 0 == success, -1 == error */
552 /* Parameters: fin(I) - pointer to packet information */
553 /* nat(I) - pointer to NAT structure */
555 /* Create a new NAT fragment cache entry based on the current packet and */
556 /* the NAT structure for this "session". */
557 /* ------------------------------------------------------------------------ */
559 ipf_frag_natnew(softc, fin, pass, nat)
560 ipf_main_softc_t *softc;
565 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
568 if (softf->ipfr_lock != 0)
572 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab,
573 &softf->ipfr_natfrag);
575 fra = ipfr_frag_new(softc, softf, fin, pass, softf->ipfr_nattab);
578 fra->ipfr_data = nat;
580 *softf->ipfr_nattail = fra;
581 fra->ipfr_prev = softf->ipfr_nattail;
582 softf->ipfr_nattail = &fra->ipfr_next;
583 fra->ipfr_next = NULL;
584 RWLOCK_EXIT(&softf->ipfr_natfrag);
591 /* ------------------------------------------------------------------------ */
592 /* Function: ipf_frag_ipidnew */
593 /* Returns: int - 0 == success, -1 == error */
594 /* Parameters: fin(I) - pointer to packet information */
595 /* ipid(I) - new IP ID for this fragmented packet */
597 /* Create a new fragment cache entry for this packet and store, as a data */
598 /* pointer, the new IP ID value. */
599 /* ------------------------------------------------------------------------ */
601 ipf_frag_ipidnew(fin, ipid)
605 ipf_main_softc_t *softc = fin->fin_main_soft;
606 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
609 if (softf->ipfr_lock)
613 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab, &softf->ipfr_ipidfrag);
615 fra = ipfr_frag_new(softc, softf, fin, 0, softf->ipfr_ipidtab);
618 fra->ipfr_data = (void *)(intptr_t)ipid;
619 *softf->ipfr_ipidtail = fra;
620 fra->ipfr_prev = softf->ipfr_ipidtail;
621 softf->ipfr_ipidtail = &fra->ipfr_next;
622 fra->ipfr_next = NULL;
623 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
629 /* ------------------------------------------------------------------------ */
630 /* Function: ipf_frag_lookup */
631 /* Returns: ipfr_t * - pointer to ipfr_t structure if there's a */
632 /* matching entry in the frag table, else NULL */
633 /* Parameters: fin(I) - pointer to packet information */
634 /* table(I) - pointer to fragment cache table to search */
636 /* Check the fragment cache to see if there is already a record of this */
637 /* packet with its filter result known. */
639 /* If this function succeeds, it returns with a write lock held on "lock". */
640 /* If it fails, no lock is held on return. */
641 /* ------------------------------------------------------------------------ */
643 ipf_frag_lookup(softc, softf, fin, table
648 ipf_main_softc_t *softc;
649 ipf_frag_softc_t *softf;
660 * We don't want to let short packets match because they could be
661 * compromising the security of other rules that want to match on
662 * layer 4 fields (and can't because they have been fragmented off.)
663 * Why do this check here? The counter acts as an indicator of this
664 * kind of attack, whereas if it was elsewhere, it wouldn't know if
665 * other matching packets had been seen.
667 if (fin->fin_flx & FI_SHORT) {
672 if ((fin->fin_flx & FI_BAD) != 0) {
678 * For fragments, we record protocol, packet id, TOS and both IP#'s
679 * (these should all be the same for all fragments of a packet).
681 * build up a hash value to index the table with.
683 memset(&frag, 0, sizeof(frag));
684 frag.ipfr_v = fin->fin_v;
686 frag.ipfr_p = fin->fin_p;
688 frag.ipfr_id = fin->fin_id;
690 frag.ipfr_source = fin->fin_fi.fi_src;
691 idx += frag.ipfr_src.s_addr;
692 frag.ipfr_dest = fin->fin_fi.fi_dst;
693 idx += frag.ipfr_dst.s_addr;
694 frag.ipfr_ifp = fin->fin_ifp;
696 idx %= softf->ipfr_size;
698 frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
699 frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
700 frag.ipfr_auth = fin->fin_fi.fi_auth;
705 * check the table, careful to only compare the right amount of data
707 for (f = table[idx]; f; f = f->ipfr_hnext) {
708 if (!bcmp((char *)&frag.ipfr_ifp, (char *)&f->ipfr_ifp,
713 * XXX - We really need to be guarding against the
714 * retransmission of (src,dst,id,offset-range) here
715 * because a fragmented packet is never resent with
716 * the same IP ID# (or shouldn't).
718 off = fin->fin_off >> 3;
721 FBUMPD(ifs_retrans0);
726 * Case 3. See comment for frpr_fragment6.
728 if ((f->ipfr_firstend != 0) &&
729 (off < f->ipfr_firstend)) {
731 DT2(ifs_overlap, u_short, off,
733 DT3(ipf_fi_bad_ifs_overlap, fr_info_t *, fin, u_short, off,
735 fin->fin_flx |= FI_BAD;
741 if (f != table[idx] && MUTEX_TRY_UPGRADE(lock)) {
745 * Move fragment info. to the top of the list
746 * to speed up searches. First, delink...
749 (*fp) = f->ipfr_hnext;
750 if (f->ipfr_hnext != NULL)
751 f->ipfr_hnext->ipfr_hprev = fp;
753 * Then put back at the top of the chain.
755 f->ipfr_hnext = table[idx];
756 table[idx]->ipfr_hprev = &f->ipfr_hnext;
757 f->ipfr_hprev = table + idx;
759 MUTEX_DOWNGRADE(lock);
763 * If we've follwed the fragments, and this is the
764 * last (in order), shrink expiration time.
766 if (off == f->ipfr_off) {
767 f->ipfr_off = (fin->fin_dlen >> 3) + off;
770 * Well, we could shrink the expiration time
771 * but only if every fragment has been seen
772 * in order upto this, the last. ipfr_badorder
773 * is used here to count those out of order
774 * and if it equals 0 when we get to the last
775 * fragment then we can assume all of the
776 * fragments have been seen and in order.
780 * Doing this properly requires moving it to
781 * the head of the list which is infesible.
783 if ((more == 0) && (f->ipfr_badorder == 0))
784 f->ipfr_ttl = softc->ipf_ticks + 1;
788 FBUMPD(ifs_unordered);
789 if (f->ipfr_pass & FR_FRSTRICT) {
795 f->ipfr_bytes += fin->fin_plen;
807 /* ------------------------------------------------------------------------ */
808 /* Function: ipf_frag_natknown */
809 /* Returns: nat_t* - pointer to 'parent' NAT structure if frag table */
810 /* match found, else NULL */
811 /* Parameters: fin(I) - pointer to packet information */
813 /* Functional interface for NAT lookups of the NAT fragment cache */
814 /* ------------------------------------------------------------------------ */
816 ipf_frag_natknown(fin)
819 ipf_main_softc_t *softc = fin->fin_main_soft;
820 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
824 if ((softf->ipfr_lock) || !softf->ipfr_natlist)
827 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab,
828 &softf->ipfr_natfrag);
830 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_nattab);
833 nat = ipf->ipfr_data;
835 * This is the last fragment for this packet.
837 if ((ipf->ipfr_ttl == softc->ipf_ticks + 1) && (nat != NULL)) {
838 nat->nat_data = NULL;
839 ipf->ipfr_data = NULL;
841 RWLOCK_EXIT(&softf->ipfr_natfrag);
848 /* ------------------------------------------------------------------------ */
849 /* Function: ipf_frag_ipidknown */
850 /* Returns: u_32_t - IPv4 ID for this packet if match found, else */
851 /* return 0xfffffff to indicate no match. */
852 /* Parameters: fin(I) - pointer to packet information */
854 /* Functional interface for IP ID lookups of the IP ID fragment cache */
855 /* ------------------------------------------------------------------------ */
857 ipf_frag_ipidknown(fin)
860 ipf_main_softc_t *softc = fin->fin_main_soft;
861 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
865 if (softf->ipfr_lock || !softf->ipfr_ipidlist)
869 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab,
870 &softf->ipfr_ipidfrag);
872 ipf = ipf_frag_lookup(softc, softf, fin, softf->ipfr_ipidtab);
875 id = (u_32_t)(intptr_t)ipf->ipfr_data;
876 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
883 /* ------------------------------------------------------------------------ */
884 /* Function: ipf_frag_known */
885 /* Returns: frentry_t* - pointer to filter rule if a match is found in */
886 /* the frag cache table, else NULL. */
887 /* Parameters: fin(I) - pointer to packet information */
888 /* passp(O) - pointer to where to store rule flags resturned */
890 /* Functional interface for normal lookups of the fragment cache. If a */
891 /* match is found, return the rule pointer and flags from the rule, except */
892 /* that if FR_LOGFIRST is set, reset FR_LOG. */
893 /* ------------------------------------------------------------------------ */
895 ipf_frag_known(fin, passp)
899 ipf_main_softc_t *softc = fin->fin_main_soft;
900 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
901 frentry_t *fr = NULL;
905 if ((softf->ipfr_lock) || (softf->ipfr_list == NULL))
909 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads,
912 fra = ipf_frag_lookup(softc, softf, fin, softf->ipfr_heads);
915 if (fin->fin_flx & FI_BAD) {
917 fin->fin_reason = FRB_BADFRAG;
918 DT2(ipf_frb_badfrag, fr_info_t *, fin, uint, fra);
925 if ((pass & FR_KEEPSTATE) != 0) {
926 fin->fin_flx |= FI_STATE;
928 * Reset the keep state flag here so that we
929 * don't try and add a new state entry because
930 * of a match here. That leads to blocking of
931 * the packet later because the add fails.
933 pass &= ~FR_KEEPSTATE;
935 if ((pass & FR_LOGFIRST) != 0)
936 pass &= ~(FR_LOGFIRST|FR_LOG);
939 RWLOCK_EXIT(&softc->ipf_frag);
945 /* ------------------------------------------------------------------------ */
946 /* Function: ipf_frag_natforget */
948 /* Parameters: softc(I) - pointer to soft context main structure */
949 /* ptr(I) - pointer to data structure */
951 /* Search through all of the fragment cache entries for NAT and wherever a */
952 /* pointer is found to match ptr, reset it to NULL. */
953 /* ------------------------------------------------------------------------ */
955 ipf_frag_natforget(softc, ptr)
956 ipf_main_softc_t *softc;
959 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
962 WRITE_ENTER(&softf->ipfr_natfrag);
963 for (fr = softf->ipfr_natlist; fr; fr = fr->ipfr_next)
964 if (fr->ipfr_data == ptr)
965 fr->ipfr_data = NULL;
966 RWLOCK_EXIT(&softf->ipfr_natfrag);
970 /* ------------------------------------------------------------------------ */
971 /* Function: ipf_frag_delete */
973 /* Parameters: softc(I) - pointer to soft context main structure */
974 /* fra(I) - pointer to fragment structure to delete */
975 /* tail(IO) - pointer to the pointer to the tail of the frag */
978 /* Remove a fragment cache table entry from the table & list. Also free */
979 /* the filter rule it is associated with it if it is no longer used as a */
980 /* result of decreasing the reference count. */
981 /* ------------------------------------------------------------------------ */
983 ipf_frag_delete(softc, fra, tail)
984 ipf_main_softc_t *softc;
985 ipfr_t *fra, ***tail;
987 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
990 fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
991 *fra->ipfr_prev = fra->ipfr_next;
992 if (*tail == &fra->ipfr_next)
993 *tail = fra->ipfr_prev;
996 fra->ipfr_hnext->ipfr_hprev = fra->ipfr_hprev;
997 *fra->ipfr_hprev = fra->ipfr_hnext;
999 if (fra->ipfr_rule != NULL) {
1000 (void) ipf_derefrule(softc, &fra->ipfr_rule);
1003 if (fra->ipfr_ref <= 0)
1004 ipf_frag_free(softf, fra);
1008 /* ------------------------------------------------------------------------ */
1009 /* Function: ipf_frag_free */
1011 /* Parameters: softf(I) - pointer to fragment context information */
1012 /* fra(I) - pointer to fragment structure to free */
1014 /* Free up a fragment cache entry and bump relevent statistics. */
1015 /* ------------------------------------------------------------------------ */
1017 ipf_frag_free(softf, fra)
1018 ipf_frag_softc_t *softf;
1023 softf->ipfr_stats.ifs_inuse--;
1027 /* ------------------------------------------------------------------------ */
1028 /* Function: ipf_frag_clear */
1030 /* Parameters: softc(I) - pointer to soft context main structure */
1032 /* Free memory in use by fragment state information kept. Do the normal */
1033 /* fragment state stuff first and then the NAT-fragment table. */
1034 /* ------------------------------------------------------------------------ */
1036 ipf_frag_clear(softc)
1037 ipf_main_softc_t *softc;
1039 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1043 WRITE_ENTER(&softc->ipf_frag);
1044 while ((fra = softf->ipfr_list) != NULL) {
1046 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1048 softf->ipfr_tail = &softf->ipfr_list;
1049 RWLOCK_EXIT(&softc->ipf_frag);
1051 WRITE_ENTER(&softc->ipf_nat);
1052 WRITE_ENTER(&softf->ipfr_natfrag);
1053 while ((fra = softf->ipfr_natlist) != NULL) {
1054 nat = fra->ipfr_data;
1056 if (nat->nat_data == fra)
1057 nat->nat_data = NULL;
1060 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1062 softf->ipfr_nattail = &softf->ipfr_natlist;
1063 RWLOCK_EXIT(&softf->ipfr_natfrag);
1064 RWLOCK_EXIT(&softc->ipf_nat);
1068 /* ------------------------------------------------------------------------ */
1069 /* Function: ipf_frag_expire */
1071 /* Parameters: softc(I) - pointer to soft context main structure */
1073 /* Expire entries in the fragment cache table that have been there too long */
1074 /* ------------------------------------------------------------------------ */
1076 ipf_frag_expire(softc)
1077 ipf_main_softc_t *softc;
1079 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1084 if (softf->ipfr_lock)
1088 WRITE_ENTER(&softc->ipf_frag);
1090 * Go through the entire table, looking for entries to expire,
1091 * which is indicated by the ttl being less than or equal to ipf_ticks.
1093 for (fp = &softf->ipfr_list; ((fra = *fp) != NULL); ) {
1094 if (fra->ipfr_ttl > softc->ipf_ticks)
1097 ipf_frag_delete(softc, fra, &softf->ipfr_tail);
1099 RWLOCK_EXIT(&softc->ipf_frag);
1101 WRITE_ENTER(&softf->ipfr_ipidfrag);
1102 for (fp = &softf->ipfr_ipidlist; ((fra = *fp) != NULL); ) {
1103 if (fra->ipfr_ttl > softc->ipf_ticks)
1106 ipf_frag_delete(softc, fra, &softf->ipfr_ipidtail);
1108 RWLOCK_EXIT(&softf->ipfr_ipidfrag);
1111 * Same again for the NAT table, except that if the structure also
1112 * still points to a NAT structure, and the NAT structure points back
1113 * at the one to be free'd, NULL the reference from the NAT struct.
1114 * NOTE: We need to grab both mutex's early, and in this order so as
1115 * to prevent a deadlock if both try to expire at the same time.
1116 * The extra if() statement here is because it locks out all NAT
1117 * operations - no need to do that if there are no entries in this
1120 if (softf->ipfr_natlist != NULL) {
1121 WRITE_ENTER(&softc->ipf_nat);
1122 WRITE_ENTER(&softf->ipfr_natfrag);
1123 for (fp = &softf->ipfr_natlist; ((fra = *fp) != NULL); ) {
1124 if (fra->ipfr_ttl > softc->ipf_ticks)
1126 nat = fra->ipfr_data;
1128 if (nat->nat_data == fra)
1129 nat->nat_data = NULL;
1132 ipf_frag_delete(softc, fra, &softf->ipfr_nattail);
1134 RWLOCK_EXIT(&softf->ipfr_natfrag);
1135 RWLOCK_EXIT(&softc->ipf_nat);
1141 /* ------------------------------------------------------------------------ */
1142 /* Function: ipf_frag_pkt_next */
1143 /* Returns: int - 0 == success, else error */
1144 /* Parameters: softc(I) - pointer to soft context main structure */
1145 /* token(I) - pointer to token information for this caller */
1146 /* itp(I) - pointer to generic iterator from caller */
1148 /* This function is used to step through the fragment cache list used for */
1149 /* filter rules. The hard work is done by the more generic ipf_frag_next. */
1150 /* ------------------------------------------------------------------------ */
1152 ipf_frag_pkt_next(softc, token, itp)
1153 ipf_main_softc_t *softc;
1157 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1160 return ipf_frag_next(softc, token, itp, &softf->ipfr_list,
1163 return ipf_frag_next(softc, token, itp, &softf->ipfr_list);
1168 /* ------------------------------------------------------------------------ */
1169 /* Function: ipf_frag_nat_next */
1170 /* Returns: int - 0 == success, else error */
1171 /* Parameters: softc(I) - pointer to soft context main structure */
1172 /* token(I) - pointer to token information for this caller */
1173 /* itp(I) - pointer to generic iterator from caller */
1175 /* This function is used to step through the fragment cache list used for */
1176 /* NAT. The hard work is done by the more generic ipf_frag_next. */
1177 /* ------------------------------------------------------------------------ */
1179 ipf_frag_nat_next(softc, token, itp)
1180 ipf_main_softc_t *softc;
1184 ipf_frag_softc_t *softf = softc->ipf_frag_soft;;
1187 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist,
1188 &softf->ipfr_natfrag);
1190 return ipf_frag_next(softc, token, itp, &softf->ipfr_natlist);
1194 /* ------------------------------------------------------------------------ */
1195 /* Function: ipf_frag_next */
1196 /* Returns: int - 0 == success, else error */
1197 /* Parameters: softc(I) - pointer to soft context main structure */
1198 /* token(I) - pointer to token information for this caller */
1199 /* itp(I) - pointer to generic iterator from caller */
1200 /* top(I) - top of the fragment list */
1201 /* lock(I) - fragment cache lock */
1203 /* This function is used to interate through the list of entries in the */
1204 /* fragment cache. It increases the reference count on the one currently */
1205 /* being returned so that the caller can come back and resume from it later.*/
1207 /* This function is used for both the NAT fragment cache as well as the ipf */
1208 /* fragment cache - hence the reason for passing in top and lock. */
1209 /* ------------------------------------------------------------------------ */
1211 ipf_frag_next(softc, token, itp, top
1216 ipf_main_softc_t *softc;
1224 ipfr_t *frag, *next, zero;
1227 if (itp->igi_data == NULL) {
1232 if (itp->igi_nitems != 1) {
1237 frag = token->ipt_data;
1244 next = frag->ipfr_next;
1247 ATOMIC_INC(next->ipfr_ref);
1248 token->ipt_data = next;
1250 bzero(&zero, sizeof(zero));
1252 token->ipt_data = NULL;
1254 if (next->ipfr_next == NULL)
1255 ipf_token_mark_complete(token);
1259 error = COPYOUT(next, itp->igi_data, sizeof(*next));
1265 ipf_frag_deref(softc, &frag, lock);
1267 ipf_frag_deref(softc, &frag);
1274 /* ------------------------------------------------------------------------ */
1275 /* Function: ipf_frag_pkt_deref */
1277 /* Parameters: softc(I) - pointer to soft context main structure */
1278 /* data(I) - pointer to frag cache pointer */
1280 /* This function is the external interface for dropping a reference to a */
1281 /* fragment cache entry used by filter rules. */
1282 /* ------------------------------------------------------------------------ */
1284 ipf_frag_pkt_deref(softc, data)
1285 ipf_main_softc_t *softc;
1288 ipfr_t **frp = data;
1291 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1293 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_frag);
1295 ipf_frag_deref(softc->ipf_frag_soft, frp);
1300 /* ------------------------------------------------------------------------ */
1301 /* Function: ipf_frag_nat_deref */
1303 /* Parameters: softc(I) - pointer to soft context main structure */
1304 /* data(I) - pointer to frag cache pointer */
1306 /* This function is the external interface for dropping a reference to a */
1307 /* fragment cache entry used by NAT table entries. */
1308 /* ------------------------------------------------------------------------ */
1310 ipf_frag_nat_deref(softc, data)
1311 ipf_main_softc_t *softc;
1314 ipfr_t **frp = data;
1317 ipf_frag_softc_t *softf = softc->ipf_frag_soft;
1319 ipf_frag_deref(softc->ipf_frag_soft, frp, &softf->ipfr_natfrag);
1321 ipf_frag_deref(softc->ipf_frag_soft, frp);
1326 /* ------------------------------------------------------------------------ */
1327 /* Function: ipf_frag_deref */
1329 /* Parameters: frp(IO) - pointer to fragment structure to deference */
1330 /* lock(I) - lock associated with the fragment */
1332 /* This function dereferences a fragment structure (ipfr_t). The pointer */
1333 /* passed in will always be reset back to NULL, even if the structure is */
1334 /* not freed, to enforce the notion that the caller is no longer entitled */
1335 /* to use the pointer it is dropping the reference to. */
1336 /* ------------------------------------------------------------------------ */
1338 ipf_frag_deref(arg, frp
1349 ipf_frag_softc_t *softf = arg;
1357 if (fra->ipfr_ref <= 0)
1358 ipf_frag_free(softf, fra);