ip_pool.c (170263) | ip_pool.c (172771) |
---|---|
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 | 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(SOLARIS2) && !defined(_KERNEL) 57# include "radix_ipf.h" 58#endif |
|
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"; | 59#if defined(_KERNEL) && (defined(__osf__) || defined(AIX) || \ 60 defined(__hpux) || defined(__sgi)) 61# include "radix_ipf_local.h" 62# define _RADIX_H_ 63#endif 64#include <net/if.h> 65#include <netinet/in.h> 66 --- 6 unchanged lines hidden (view full) --- 73 !defined(__hpux) && !defined(__sgi)) 74static int rn_freenode __P((struct radix_node *, void *)); 75#endif 76 77/* END OF INCLUDES */ 78 79#if !defined(lint) 80static 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 $"; | 81static const char rcsid[] = "@(#)$Id: ip_pool.c,v 2.55.2.24 2007/10/10 09:45:37 darrenr Exp $"; |
79#endif 80 81#ifdef IPFILTER_LOOKUP 82 | 82#endif 83 84#ifdef IPFILTER_LOOKUP 85 |
83# ifndef RADIX_NODE_HEAD_LOCK | 86# if !defined(RADIX_NODE_HEAD_LOCK) || !defined(RADIX_NODE_HEAD_UNLOCK) || \ 87 !defined(_KERNEL) 88# undef RADIX_NODE_HEAD_LOCK 89# undef RADIX_NODE_HEAD_UNLOCK |
84# define RADIX_NODE_HEAD_LOCK(x) ; | 90# 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 | 91# define RADIX_NODE_HEAD_UNLOCK(x) ; 92# endif 93 94static void ip_pool_clearnodes __P((ip_pool_t *)); 95static void *ip_pool_exists __P((int, char *)); 96 97ip_pool_stat_t ipoolstat; 98ipfrwlock_t ip_poolrw; --- 164 unchanged lines hidden (view full) --- 263/* function for the radix tree that supports the pools. ip_pool_destroy() is*/ 264/* used to delete the pools one by one to ensure they're properly freed up. */ 265/* ------------------------------------------------------------------------ */ 266void ip_pool_fini() 267{ 268 ip_pool_t *p, *q; 269 int i; 270 |
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 | 271 for (i = 0; i <= IPL_LOGMAX; i++) { 272 for (q = ip_pool_list[i]; (p = q) != NULL; ) { 273 q = p->ipo_next; 274 (void) ip_pool_destroy(i, p->ipo_name); 275 } 276 } 277 278#if (!defined(_KERNEL) || (BSD < 199306)) --- 181 unchanged lines hidden (view full) --- 460int ip_pool_insert(ipo, addr, mask, info) 461ip_pool_t *ipo; 462i6addr_t *addr, *mask; 463int info; 464{ 465 struct radix_node *rn; 466 ip_pool_node_t *x; 467 |
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 | 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 | 532 unit = op->iplo_unit; 533 |
536 if ((op->iplo_arg & LOOKUP_ANON) == 0) | 534 if ((op->iplo_arg & LOOKUP_ANON) == 0) { |
537 h = ip_pool_exists(unit, op->iplo_name); | 535 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) { | 536 if (h != NULL) { 537 if ((h->ipo_flags & IPOOL_DELETE) == 0) 538 return EEXIST; |
543 h->ipo_flags &= ~IPOOL_DELETE; 544 return 0; 545 } | 539 h->ipo_flags &= ~IPOOL_DELETE; 540 return 0; 541 } |
546 return EEXIST; 547 } else { 548 KMALLOC(h, ip_pool_t *); 549 if (h == NULL) 550 return ENOMEM; 551 bzero(h, sizeof(*h)); | 542 } |
552 | 543 |
553 if (rn_inithead((void **)&h->ipo_head, 554 offsetof(addrfamily_t, adf_addr) << 3) == 0) { 555 KFREE(h); 556 return ENOMEM; 557 } | 544 KMALLOC(h, ip_pool_t *); 545 if (h == NULL) 546 return ENOMEM; 547 bzero(h, sizeof(*h)); 548 549 if (rn_inithead((void **)&h->ipo_head, 550 offsetof(addrfamily_t, adf_addr) << 3) == 0) { 551 KFREE(h); 552 return ENOMEM; |
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 | 553 } 554 555 if ((op->iplo_arg & LOOKUP_ANON) != 0) { 556 ip_pool_t *p; 557 558 h->ipo_flags |= IPOOL_ANON; 559 poolnum = LOOKUP_ANON; 560 --- 18 unchanged lines hidden (view full) --- 579 } 580 581 (void)strncpy(h->ipo_name, name, sizeof(h->ipo_name)); 582 (void)strncpy(op->iplo_name, name, sizeof(op->iplo_name)); 583 } else { 584 (void)strncpy(h->ipo_name, op->iplo_name, sizeof(h->ipo_name)); 585 } 586 |
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; | 587 h->ipo_ref = 1; 588 h->ipo_list = NULL; 589 h->ipo_unit = unit; 590 h->ipo_next = ip_pool_list[unit]; 591 if (ip_pool_list[unit] != NULL) 592 ip_pool_list[unit]->ipo_pnext = &h->ipo_next; 593 h->ipo_pnext = &ip_pool_list[unit]; 594 ip_pool_list[unit] = h; |
601 | 595 |
602 ipoolstat.ipls_pools++; 603 } | 596 ipoolstat.ipls_pools++; |
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 | 597 598 return 0; 599} 600 601 602/* ------------------------------------------------------------------------ */ 603/* Function: ip_pool_remove */ 604/* Returns: int - 0 = success, else error */ 605/* Parameters: ipo(I) - pointer to the pool to remove the node from. */ 606/* ipe(I) - address being deleted as a node */ 607/* Locks: WRITE(ip_poolrw) */ 608/* */ 609/* Remove a node from the pool given by ipo. */ 610/* ------------------------------------------------------------------------ */ 611int ip_pool_remove(ipo, ipe) 612ip_pool_t *ipo; 613ip_pool_node_t *ipe; 614{ 615 |
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 | 616 if (ipe->ipn_pnext != NULL) 617 *ipe->ipn_pnext = ipe->ipn_next; 618 if (ipe->ipn_next != NULL) 619 ipe->ipn_next->ipn_pnext = ipe->ipn_pnext; 620 621 RADIX_NODE_HEAD_LOCK(ipo->ipo_head); 622 ipo->ipo_head->rnh_deladdr(&ipe->ipn_addr, &ipe->ipn_mask, 623 ipo->ipo_head); --- 151 unchanged lines hidden (view full) --- 775/* */ 776/* Drop the number of known references to this pool structure by one and if */ 777/* we arrive at zero known references, free it. */ 778/* ------------------------------------------------------------------------ */ 779void ip_pool_deref(ipo) 780ip_pool_t *ipo; 781{ 782 |
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); | 783 ipo->ipo_ref--; 784 785 if (ipo->ipo_ref == 0) 786 ip_pool_free(ipo); 787 788 else if ((ipo->ipo_ref == 1) && (ipo->ipo_flags & IPOOL_DELETE)) 789 ip_pool_destroy(ipo->ipo_unit, ipo->ipo_name); 790} --- 51 unchanged lines hidden (view full) --- 842 if (ipo == NULL) { 843 nextipo = ip_pool_list[(int)ilp->ili_unit]; 844 } else { 845 nextipo = ipo->ipo_next; 846 } 847 848 if (nextipo != NULL) { 849 ATOMIC_INC(nextipo->ipo_ref); |
861 if (nextipo->ipo_next == NULL) 862 token->ipt_alive = 0; | 850 token->ipt_data = nextipo; |
863 } else { 864 bzero((char *)&zp, sizeof(zp)); 865 nextipo = &zp; | 851 } else { 852 bzero((char *)&zp, sizeof(zp)); 853 nextipo = &zp; |
854 token->ipt_data = NULL; |
|
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); | 855 } 856 break; 857 858 case IPFLOOKUPITER_NODE : 859 node = token->ipt_data; 860 if (node == NULL) { 861 ipo = ip_pool_exists(ilp->ili_unit, ilp->ili_name); 862 if (ipo == NULL) 863 err = ESRCH; 864 else { 865 nextnode = ipo->ipo_list; 866 ipo = NULL; 867 } 868 } else { 869 nextnode = node->ipn_next; 870 } 871 872 if (nextnode != NULL) { 873 ATOMIC_INC(nextnode->ipn_ref); |
885 if (nextnode->ipn_next == NULL) 886 token->ipt_alive = 0; | 874 token->ipt_data = nextnode; |
887 } else { 888 bzero((char *)&zn, sizeof(zn)); 889 nextnode = &zn; | 875 } else { 876 bzero((char *)&zn, sizeof(zn)); 877 nextnode = &zn; |
878 token->ipt_data = NULL; |
|
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 } | 879 } 880 break; 881 default : 882 err = EINVAL; 883 break; 884 } 885 886 RWLOCK_EXIT(&ip_poolrw); --- 4 unchanged lines hidden (view full) --- 891 switch (ilp->ili_otype) 892 { 893 case IPFLOOKUPITER_LIST : 894 if (ipo != NULL) { 895 WRITE_ENTER(&ip_poolrw); 896 ip_pool_deref(ipo); 897 RWLOCK_EXIT(&ip_poolrw); 898 } |
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 } | 899 err = COPYOUT(nextipo, ilp->ili_data, sizeof(*nextipo)); 900 if (err != 0) 901 err = EFAULT; 902 break; 903 904 case IPFLOOKUPITER_NODE : 905 if (node != NULL) { 906 WRITE_ENTER(&ip_poolrw); 907 ip_pool_node_deref(node); 908 RWLOCK_EXIT(&ip_poolrw); 909 } |
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 --- | 910 err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode)); 911 if (err != 0) 912 err = EFAULT; 913 break; 914 } 915 916 return err; 917} --- 75 unchanged lines hidden --- |