* allocating a SP for OUTBOUND or INBOUND packet. * Must call key_freesp() later. * OUT: NULL: not found * others: found and return the pointer. */
| 879 | * others: found and return the pointer. |
| 880 | */ |
| 881 | struct secpolicy * |
| 882 | key_allocsp(struct secpolicyindex *spidx, u_int dir) |
| 883 | { |
| 884 | struct spdcache_entry *entry, *lastentry, *tmpentry; |
| 885 | struct secpolicy *sp; |
| 886 | uint32_t hashv; |
| 887 | int nb_entries; |
| 888 | |
| 889 | if (!SPDCACHE_ACTIVE()) { |
| 890 | sp = key_do_allocsp(spidx, dir); |
| 891 | goto out; |
| 892 | } |
| 893 | |
| 894 | hashv = SPDCACHE_HASHVAL(spidx); |
| 895 | SPDCACHE_LOCK(hashv); |
| 896 | nb_entries = 0; |
| 897 | LIST_FOREACH_SAFE(entry, &V_spdcachehashtbl[hashv], chain, tmpentry) { |
| 898 | /* Removed outdated entries */ |
| 899 | if (entry->sp != NULL && |
| 900 | entry->sp->state == IPSEC_SPSTATE_DEAD) { |
| 901 | LIST_REMOVE(entry, chain); |
| 902 | spdcache_entry_free(entry); |
| 903 | continue; |
| 904 | } |
| 905 | |
| 906 | nb_entries++; |
| 907 | if (!key_cmpspidx_exactly(&entry->spidx, spidx)) { |
| 908 | lastentry = entry; |
| 909 | continue; |
| 910 | } |
| 911 | |
| 912 | sp = entry->sp; |
| 913 | if (entry->sp != NULL) |
| 914 | SP_ADDREF(sp); |
| 915 | |
| 916 | /* IPSECSTAT_INC(ips_spdcache_hits); */ |
| 917 | |
| 918 | SPDCACHE_UNLOCK(hashv); |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | /* IPSECSTAT_INC(ips_spdcache_misses); */ |
| 923 | |
| 924 | sp = key_do_allocsp(spidx, dir); |
| 925 | entry = spdcache_entry_alloc(spidx, sp); |
| 926 | if (entry != NULL) { |
| 927 | if (nb_entries >= SPDCACHE_MAX_ENTRIES_PER_HASH) { |
| 928 | LIST_REMOVE(lastentry, chain); |
| 929 | spdcache_entry_free(lastentry); |
| 930 | } |
| 931 | |
| 932 | LIST_INSERT_HEAD(&V_spdcachehashtbl[hashv], entry, chain); |
| 933 | } |
| 934 | |
| 935 | SPDCACHE_UNLOCK(hashv); |
| 936 | |
| 937 | out: |
| 938 | if (sp != NULL) { /* found a SPD entry */ |
no test coverage detected