--- ../plan3/pf.c.vm3 2016-10-30 12:20:23.834100000 +1300 +++ ../plan3/pf.c.vm3 2016-11-02 20:15:08.147099000 +1300 @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -777,12 +778,34 @@ struct pf_keyhash *kh; struct pf_idhash *ih; struct pf_srchash *sh; + u_long hw_physmem; u_int i; - - if (pf_hashsize == 0 || !powerof2(pf_hashsize)) + size_t size; + + /* Get the available hardware memory */ + size=sizeof(hw_physmem); + if (kernel_sysctlbyname(&thread0, "hw.physmem", &hw_physmem, \ + &size, 0, 0, 0, 0) != 0) + /* make the check against hw.physmem irrelevant */ + hw_physmem = ULONG_MAX; + + if (pf_hashsize == 0 || !powerof2(pf_hashsize) || \ + (pf_hashsize * sizeof(struct pf_keyhash)) > hw_physmem || \ + (pf_hashsize * sizeof(struct pf_idhash)) > hw_physmem){ + printf("pf: Invalid states hash table size : %lu. " + "Using the default value %lu.\n", + (unsigned long)pf_hashsize, + (unsigned long)PF_HASHSIZ); pf_hashsize = PF_HASHSIZ; - if (pf_srchashsize == 0 || !powerof2(pf_srchashsize)) + } + if (pf_srchashsize == 0 || !powerof2(pf_srchashsize) || \ + (pf_srchashsize * sizeof(struct pf_srchash)) > hw_physmem ){ + printf("pf: Invalid source node hash table size : %lu. " + "Using the default value %lu.\n", + (unsigned long)pf_srchashsize, + (unsigned long)(PF_HASHSIZ/4)); pf_srchashsize = PF_HASHSIZ / 4; + } V_pf_hashseed = arc4random();