* allocating a new SA for key_add() and key_getspi() call, * and copy the values of mhp into new buffer. * When SAD message type is SADB_GETSPI set SA state to LARVAL. * For SADB_ADD create and initialize SA with MATURE state. * OUT: NULL : fail * others : pointer to new secasvar. */
| 2895 | * others : pointer to new secasvar. |
| 2896 | */ |
| 2897 | static struct secasvar * |
| 2898 | key_newsav(const struct sadb_msghdr *mhp, struct secasindex *saidx, |
| 2899 | uint32_t spi, int *errp) |
| 2900 | { |
| 2901 | struct secashead *sah; |
| 2902 | struct secasvar *sav; |
| 2903 | int isnew; |
| 2904 | |
| 2905 | IPSEC_ASSERT(mhp != NULL, ("null msghdr")); |
| 2906 | IPSEC_ASSERT(mhp->msg != NULL, ("null msg")); |
| 2907 | IPSEC_ASSERT(mhp->msg->sadb_msg_type == SADB_GETSPI || |
| 2908 | mhp->msg->sadb_msg_type == SADB_ADD, ("wrong message type")); |
| 2909 | |
| 2910 | sav = NULL; |
| 2911 | sah = NULL; |
| 2912 | /* check SPI value */ |
| 2913 | switch (saidx->proto) { |
| 2914 | case IPPROTO_ESP: |
| 2915 | case IPPROTO_AH: |
| 2916 | /* |
| 2917 | * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values |
| 2918 | * 1-255 reserved by IANA for future use, |
| 2919 | * 0 for implementation specific, local use. |
| 2920 | */ |
| 2921 | if (ntohl(spi) <= 255) { |
| 2922 | ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n", |
| 2923 | __func__, ntohl(spi))); |
| 2924 | *errp = EINVAL; |
| 2925 | goto done; |
| 2926 | } |
| 2927 | break; |
| 2928 | } |
| 2929 | |
| 2930 | sav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT | M_ZERO); |
| 2931 | if (sav == NULL) { |
| 2932 | *errp = ENOBUFS; |
| 2933 | goto done; |
| 2934 | } |
| 2935 | sav->lock = malloc(sizeof(struct mtx), M_IPSEC_MISC, |
| 2936 | M_NOWAIT | M_ZERO); |
| 2937 | if (sav->lock == NULL) { |
| 2938 | *errp = ENOBUFS; |
| 2939 | goto done; |
| 2940 | } |
| 2941 | mtx_init(sav->lock, "ipsec association", NULL, MTX_DEF); |
| 2942 | sav->lft_c = uma_zalloc_pcpu(V_key_lft_zone, M_NOWAIT); |
| 2943 | if (sav->lft_c == NULL) { |
| 2944 | *errp = ENOBUFS; |
| 2945 | goto done; |
| 2946 | } |
| 2947 | counter_u64_zero(sav->lft_c_allocations); |
| 2948 | counter_u64_zero(sav->lft_c_bytes); |
| 2949 | |
| 2950 | sav->spi = spi; |
| 2951 | sav->seq = mhp->msg->sadb_msg_seq; |
| 2952 | sav->state = SADB_SASTATE_LARVAL; |
| 2953 | sav->pid = (pid_t)mhp->msg->sadb_msg_pid; |
| 2954 | SAV_INITREF(sav); |
no test coverage detected