1/*
2 * Copyright (C) 1993-2001, 2003 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6#if defined(KERNEL) || defined(_KERNEL)
7# undef KERNEL
8# undef _KERNEL
--- 39 unchanged lines hidden (view full) ---
48# endif
49# include <sys/stream.h>
50# include <sys/kmem.h>
51#endif
52#if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
53# include <sys/malloc.h>
54#endif
55
56#if defined(_KERNEL) && (defined(__osf__) || defined(AIX) || \
57 defined(__hpux) || defined(__sgi))
58# include "radix_ipf_local.h"
59# define _RADIX_H_
60#endif
61#include <net/if.h>
62#include <netinet/in.h>
63
--- 6 unchanged lines hidden (view full) ---
70 !defined(__hpux) && !defined(__sgi))
71static int rn_freenode __P((struct radix_node *, void *));
72#endif
73
74/* END OF INCLUDES */
75
76#if !defined(lint)
77static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
78static const char rcsid[] = "@(#)$Id: ip_pool.c,v 2.55.2.20 2007/05/31 12:27:35 darrenr Exp $";
79#endif
80
81#ifdef IPFILTER_LOOKUP
82
83# ifndef RADIX_NODE_HEAD_LOCK
84# define RADIX_NODE_HEAD_LOCK(x) ;
85# endif
86# ifndef RADIX_NODE_HEAD_UNLOCK
87# define RADIX_NODE_HEAD_UNLOCK(x) ;
88# endif
89
90static void ip_pool_clearnodes __P((ip_pool_t *));
91static void *ip_pool_exists __P((int, char *));
92
93ip_pool_stat_t ipoolstat;
94ipfrwlock_t ip_poolrw;
--- 164 unchanged lines hidden (view full) ---
259/* function for the radix tree that supports the pools. ip_pool_destroy() is*/
260/* used to delete the pools one by one to ensure they're properly freed up. */
261/* ------------------------------------------------------------------------ */
262void ip_pool_fini()
263{
264 ip_pool_t *p, *q;
265 int i;
266
267 ASSERT(rw_read_locked(&ipf_global.ipf_lk) == 0);
268
269 for (i = 0; i <= IPL_LOGMAX; i++) {
270 for (q = ip_pool_list[i]; (p = q) != NULL; ) {
271 q = p->ipo_next;
272 (void) ip_pool_destroy(i, p->ipo_name);
273 }
274 }
275
276#if (!defined(_KERNEL) || (BSD < 199306))
--- 181 unchanged lines hidden (view full) ---
458int ip_pool_insert(ipo, addr, mask, info)
459ip_pool_t *ipo;
460i6addr_t *addr, *mask;
461int info;
462{
463 struct radix_node *rn;
464 ip_pool_node_t *x;
465
466 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
467
468 KMALLOC(x, ip_pool_node_t *);
469 if (x == NULL) {
470 return ENOMEM;
471 }
472
473 bzero(x, sizeof(*x));
474
475 x->ipn_info = info;
--- 48 unchanged lines hidden (view full) ---
524/* ------------------------------------------------------------------------ */
525int ip_pool_create(op)
526iplookupop_t *op;
527{
528 char name[FR_GROUPLEN];
529 int poolnum, unit;
530 ip_pool_t *h;
531
532 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
533
534 unit = op->iplo_unit;
535
536 if ((op->iplo_arg & LOOKUP_ANON) == 0)
537 h = ip_pool_exists(unit, op->iplo_name);
538 else
539 h = NULL;
540
541 if (h != NULL) {
542 if ((h->ipo_flags & IPOOL_DELETE) != 0) {
543 h->ipo_flags &= ~IPOOL_DELETE;
544 return 0;
545 }
546 return EEXIST;
547 } else {
548 KMALLOC(h, ip_pool_t *);
549 if (h == NULL)
550 return ENOMEM;
551 bzero(h, sizeof(*h));
552
553 if (rn_inithead((void **)&h->ipo_head,
554 offsetof(addrfamily_t, adf_addr) << 3) == 0) {
555 KFREE(h);
556 return ENOMEM;
557 }
558 }
559
560 if ((op->iplo_arg & LOOKUP_ANON) != 0) {
561 ip_pool_t *p;
562
563 h->ipo_flags |= IPOOL_ANON;
564 poolnum = LOOKUP_ANON;
565
--- 18 unchanged lines hidden (view full) ---
584 }
585
586 (void)strncpy(h->ipo_name, name, sizeof(h->ipo_name));
587 (void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
588 } else {
589 (void)strncpy(h->ipo_name, op->iplo_name, sizeof(h->ipo_name));
590 }
591
592 if ((h->ipo_flags & IPOOL_DELETE) == 0) {
593 h->ipo_ref = 1;
594 h->ipo_list = NULL;
595 h->ipo_unit = unit;
596 h->ipo_next = ip_pool_list[unit];
597 if (ip_pool_list[unit] != NULL)
598 ip_pool_list[unit]->ipo_pnext = &h->ipo_next;
599 h->ipo_pnext = &ip_pool_list[unit];
600 ip_pool_list[unit] = h;
601
602 ipoolstat.ipls_pools++;
603 }
604
605 return 0;
606}
607
608
609/* ------------------------------------------------------------------------ */
610/* Function: ip_pool_remove */
611/* Returns: int - 0 = success, else error */
612/* Parameters: ipo(I) - pointer to the pool to remove the node from. */
613/* ipe(I) - address being deleted as a node */
614/* Locks: WRITE(ip_poolrw) */
615/* */
616/* Remove a node from the pool given by ipo. */
617/* ------------------------------------------------------------------------ */
618int ip_pool_remove(ipo, ipe)
619ip_pool_t *ipo;
620ip_pool_node_t *ipe;
621{
622
623 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
624
625 if (ipe->ipn_pnext != NULL)
626 *ipe->ipn_pnext = ipe->ipn_next;
627 if (ipe->ipn_next != NULL)
628 ipe->ipn_next->ipn_pnext = ipe->ipn_pnext;
629
630 RADIX_NODE_HEAD_LOCK(ipo->ipo_head);
631 ipo->ipo_head->rnh_deladdr(&ipe->ipn_addr, &ipe->ipn_mask,
632 ipo->ipo_head);
--- 151 unchanged lines hidden (view full) ---
784/* */
785/* Drop the number of known references to this pool structure by one and if */
786/* we arrive at zero known references, free it. */
787/* ------------------------------------------------------------------------ */
788void ip_pool_deref(ipo)
789ip_pool_t *ipo;
790{
791
792 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
793
794 ipo->ipo_ref--;
795
796 if (ipo->ipo_ref == 0)
797 ip_pool_free(ipo);
798
799 else if ((ipo->ipo_ref == 1) && (ipo->ipo_flags & IPOOL_DELETE))
800 ip_pool_destroy(ipo->ipo_unit, ipo->ipo_name);
801}
--- 51 unchanged lines hidden (view full) ---
853 if (ipo == NULL) {
854 nextipo = ip_pool_list[(int)ilp->ili_unit];
855 } else {
856 nextipo = ipo->ipo_next;
857 }
858
859 if (nextipo != NULL) {
860 ATOMIC_INC(nextipo->ipo_ref);
861 if (nextipo->ipo_next == NULL)
862 token->ipt_alive = 0;
863 } else {
864 bzero((char *)&zp, sizeof(zp));
865 nextipo = &zp;
866 }
867 break;
868
869 case IPFLOOKUPITER_NODE :
870 node = token->ipt_data;
871 if (node == NULL) {
872 ipo = ip_pool_exists(ilp->ili_unit, ilp->ili_name);
873 if (ipo == NULL)
874 err = ESRCH;
875 else {
876 nextnode = ipo->ipo_list;
877 ipo = NULL;
878 }
879 } else {
880 nextnode = node->ipn_next;
881 }
882
883 if (nextnode != NULL) {
884 ATOMIC_INC(nextnode->ipn_ref);
885 if (nextnode->ipn_next == NULL)
886 token->ipt_alive = 0;
887 } else {
888 bzero((char *)&zn, sizeof(zn));
889 nextnode = &zn;
890 }
891 break;
892 default :
893 err = EINVAL;
894 break;
895 }
896
897 RWLOCK_EXIT(&ip_poolrw);
--- 4 unchanged lines hidden (view full) ---
902 switch (ilp->ili_otype)
903 {
904 case IPFLOOKUPITER_LIST :
905 if (ipo != NULL) {
906 WRITE_ENTER(&ip_poolrw);
907 ip_pool_deref(ipo);
908 RWLOCK_EXIT(&ip_poolrw);
909 }
910 token->ipt_data = nextipo;
911 err = COPYOUT(nextipo, ilp->ili_data, sizeof(*nextipo));
912 if (err != 0)
913 err = EFAULT;
914 break;
915
916 case IPFLOOKUPITER_NODE :
917 if (node != NULL) {
918 WRITE_ENTER(&ip_poolrw);
919 ip_pool_node_deref(node);
920 RWLOCK_EXIT(&ip_poolrw);
921 }
922 token->ipt_data = nextnode;
923 err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
924 if (err != 0)
925 err = EFAULT;
926 break;
927 }
928
929 return err;
930}
--- 75 unchanged lines hidden ---
2 * Copyright (C) 1993-2001, 2003 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6#if defined(KERNEL) || defined(_KERNEL)
7# undef KERNEL
8# undef _KERNEL
--- 39 unchanged lines hidden (view full) ---
48# endif
49# include <sys/stream.h>
50# include <sys/kmem.h>
51#endif
52#if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
53# include <sys/malloc.h>
54#endif
55
56#if defined(_KERNEL) && (defined(__osf__) || defined(AIX) || \
57 defined(__hpux) || defined(__sgi))
58# include "radix_ipf_local.h"
59# define _RADIX_H_
60#endif
61#include <net/if.h>
62#include <netinet/in.h>
63
--- 6 unchanged lines hidden (view full) ---
70 !defined(__hpux) && !defined(__sgi))
71static int rn_freenode __P((struct radix_node *, void *));
72#endif
73
74/* END OF INCLUDES */
75
76#if !defined(lint)
77static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
78static const char rcsid[] = "@(#)$Id: ip_pool.c,v 2.55.2.20 2007/05/31 12:27:35 darrenr Exp $";
79#endif
80
81#ifdef IPFILTER_LOOKUP
82
83# ifndef RADIX_NODE_HEAD_LOCK
84# define RADIX_NODE_HEAD_LOCK(x) ;
85# endif
86# ifndef RADIX_NODE_HEAD_UNLOCK
87# define RADIX_NODE_HEAD_UNLOCK(x) ;
88# endif
89
90static void ip_pool_clearnodes __P((ip_pool_t *));
91static void *ip_pool_exists __P((int, char *));
92
93ip_pool_stat_t ipoolstat;
94ipfrwlock_t ip_poolrw;
--- 164 unchanged lines hidden (view full) ---
259/* function for the radix tree that supports the pools. ip_pool_destroy() is*/
260/* used to delete the pools one by one to ensure they're properly freed up. */
261/* ------------------------------------------------------------------------ */
262void ip_pool_fini()
263{
264 ip_pool_t *p, *q;
265 int i;
266
267 ASSERT(rw_read_locked(&ipf_global.ipf_lk) == 0);
268
269 for (i = 0; i <= IPL_LOGMAX; i++) {
270 for (q = ip_pool_list[i]; (p = q) != NULL; ) {
271 q = p->ipo_next;
272 (void) ip_pool_destroy(i, p->ipo_name);
273 }
274 }
275
276#if (!defined(_KERNEL) || (BSD < 199306))
--- 181 unchanged lines hidden (view full) ---
458int ip_pool_insert(ipo, addr, mask, info)
459ip_pool_t *ipo;
460i6addr_t *addr, *mask;
461int info;
462{
463 struct radix_node *rn;
464 ip_pool_node_t *x;
465
466 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
467
468 KMALLOC(x, ip_pool_node_t *);
469 if (x == NULL) {
470 return ENOMEM;
471 }
472
473 bzero(x, sizeof(*x));
474
475 x->ipn_info = info;
--- 48 unchanged lines hidden (view full) ---
524/* ------------------------------------------------------------------------ */
525int ip_pool_create(op)
526iplookupop_t *op;
527{
528 char name[FR_GROUPLEN];
529 int poolnum, unit;
530 ip_pool_t *h;
531
532 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
533
534 unit = op->iplo_unit;
535
536 if ((op->iplo_arg & LOOKUP_ANON) == 0)
537 h = ip_pool_exists(unit, op->iplo_name);
538 else
539 h = NULL;
540
541 if (h != NULL) {
542 if ((h->ipo_flags & IPOOL_DELETE) != 0) {
543 h->ipo_flags &= ~IPOOL_DELETE;
544 return 0;
545 }
546 return EEXIST;
547 } else {
548 KMALLOC(h, ip_pool_t *);
549 if (h == NULL)
550 return ENOMEM;
551 bzero(h, sizeof(*h));
552
553 if (rn_inithead((void **)&h->ipo_head,
554 offsetof(addrfamily_t, adf_addr) << 3) == 0) {
555 KFREE(h);
556 return ENOMEM;
557 }
558 }
559
560 if ((op->iplo_arg & LOOKUP_ANON) != 0) {
561 ip_pool_t *p;
562
563 h->ipo_flags |= IPOOL_ANON;
564 poolnum = LOOKUP_ANON;
565
--- 18 unchanged lines hidden (view full) ---
584 }
585
586 (void)strncpy(h->ipo_name, name, sizeof(h->ipo_name));
587 (void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
588 } else {
589 (void)strncpy(h->ipo_name, op->iplo_name, sizeof(h->ipo_name));
590 }
591
592 if ((h->ipo_flags & IPOOL_DELETE) == 0) {
593 h->ipo_ref = 1;
594 h->ipo_list = NULL;
595 h->ipo_unit = unit;
596 h->ipo_next = ip_pool_list[unit];
597 if (ip_pool_list[unit] != NULL)
598 ip_pool_list[unit]->ipo_pnext = &h->ipo_next;
599 h->ipo_pnext = &ip_pool_list[unit];
600 ip_pool_list[unit] = h;
601
602 ipoolstat.ipls_pools++;
603 }
604
605 return 0;
606}
607
608
609/* ------------------------------------------------------------------------ */
610/* Function: ip_pool_remove */
611/* Returns: int - 0 = success, else error */
612/* Parameters: ipo(I) - pointer to the pool to remove the node from. */
613/* ipe(I) - address being deleted as a node */
614/* Locks: WRITE(ip_poolrw) */
615/* */
616/* Remove a node from the pool given by ipo. */
617/* ------------------------------------------------------------------------ */
618int ip_pool_remove(ipo, ipe)
619ip_pool_t *ipo;
620ip_pool_node_t *ipe;
621{
622
623 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
624
625 if (ipe->ipn_pnext != NULL)
626 *ipe->ipn_pnext = ipe->ipn_next;
627 if (ipe->ipn_next != NULL)
628 ipe->ipn_next->ipn_pnext = ipe->ipn_pnext;
629
630 RADIX_NODE_HEAD_LOCK(ipo->ipo_head);
631 ipo->ipo_head->rnh_deladdr(&ipe->ipn_addr, &ipe->ipn_mask,
632 ipo->ipo_head);
--- 151 unchanged lines hidden (view full) ---
784/* */
785/* Drop the number of known references to this pool structure by one and if */
786/* we arrive at zero known references, free it. */
787/* ------------------------------------------------------------------------ */
788void ip_pool_deref(ipo)
789ip_pool_t *ipo;
790{
791
792 ASSERT(rw_read_locked(&ip_poolrw.ipf_lk) == 0);
793
794 ipo->ipo_ref--;
795
796 if (ipo->ipo_ref == 0)
797 ip_pool_free(ipo);
798
799 else if ((ipo->ipo_ref == 1) && (ipo->ipo_flags & IPOOL_DELETE))
800 ip_pool_destroy(ipo->ipo_unit, ipo->ipo_name);
801}
--- 51 unchanged lines hidden (view full) ---
853 if (ipo == NULL) {
854 nextipo = ip_pool_list[(int)ilp->ili_unit];
855 } else {
856 nextipo = ipo->ipo_next;
857 }
858
859 if (nextipo != NULL) {
860 ATOMIC_INC(nextipo->ipo_ref);
861 if (nextipo->ipo_next == NULL)
862 token->ipt_alive = 0;
863 } else {
864 bzero((char *)&zp, sizeof(zp));
865 nextipo = &zp;
866 }
867 break;
868
869 case IPFLOOKUPITER_NODE :
870 node = token->ipt_data;
871 if (node == NULL) {
872 ipo = ip_pool_exists(ilp->ili_unit, ilp->ili_name);
873 if (ipo == NULL)
874 err = ESRCH;
875 else {
876 nextnode = ipo->ipo_list;
877 ipo = NULL;
878 }
879 } else {
880 nextnode = node->ipn_next;
881 }
882
883 if (nextnode != NULL) {
884 ATOMIC_INC(nextnode->ipn_ref);
885 if (nextnode->ipn_next == NULL)
886 token->ipt_alive = 0;
887 } else {
888 bzero((char *)&zn, sizeof(zn));
889 nextnode = &zn;
890 }
891 break;
892 default :
893 err = EINVAL;
894 break;
895 }
896
897 RWLOCK_EXIT(&ip_poolrw);
--- 4 unchanged lines hidden (view full) ---
902 switch (ilp->ili_otype)
903 {
904 case IPFLOOKUPITER_LIST :
905 if (ipo != NULL) {
906 WRITE_ENTER(&ip_poolrw);
907 ip_pool_deref(ipo);
908 RWLOCK_EXIT(&ip_poolrw);
909 }
910 token->ipt_data = nextipo;
911 err = COPYOUT(nextipo, ilp->ili_data, sizeof(*nextipo));
912 if (err != 0)
913 err = EFAULT;
914 break;
915
916 case IPFLOOKUPITER_NODE :
917 if (node != NULL) {
918 WRITE_ENTER(&ip_poolrw);
919 ip_pool_node_deref(node);
920 RWLOCK_EXIT(&ip_poolrw);
921 }
922 token->ipt_data = nextnode;
923 err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
924 if (err != 0)
925 err = EFAULT;
926 break;
927 }
928
929 return err;
930}
--- 75 unchanged lines hidden ---