Lines Matching +full:library +full:- +full:sel

1 // SPDX-License-Identifier: GPL-2.0-only
37 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
57 ~(crypto_tfm_ctx_alignment() - 1); in esp_alloc_tmp()
101 struct crypto_aead *aead = x->data; in esp_ssg_unref()
107 if (x->props.flags & XFRM_STATE_ESN) in esp_ssg_unref()
114 * Skip the first sg which comes from skb->data. in esp_ssg_unref()
116 if (req->src != req->dst) in esp_ssg_unref()
117 for (sg = sg_next(req->src); sg; sg = sg_next(sg)) in esp_ssg_unref()
119 skb->pp_recycle); in esp_ssg_unref()
132 sock_put(esk->sk); in esp_free_tcp_sk()
138 struct xfrm_encap_tmpl *encap = x->encap; in esp_find_tcp_sk()
145 sk = rcu_dereference(x->encap_sk); in esp_find_tcp_sk()
146 if (sk && sk->sk_state == TCP_ESTABLISHED) in esp_find_tcp_sk()
149 spin_lock_bh(&x->lock); in esp_find_tcp_sk()
150 sport = encap->encap_sport; in esp_find_tcp_sk()
151 dport = encap->encap_dport; in esp_find_tcp_sk()
152 nsk = rcu_dereference_protected(x->encap_sk, in esp_find_tcp_sk()
153 lockdep_is_held(&x->lock)); in esp_find_tcp_sk()
157 spin_unlock_bh(&x->lock); in esp_find_tcp_sk()
158 return ERR_PTR(-ENOMEM); in esp_find_tcp_sk()
160 RCU_INIT_POINTER(x->encap_sk, NULL); in esp_find_tcp_sk()
161 esk->sk = sk; in esp_find_tcp_sk()
162 call_rcu(&esk->rcu, esp_free_tcp_sk); in esp_find_tcp_sk()
164 spin_unlock_bh(&x->lock); in esp_find_tcp_sk()
166 sk = inet_lookup_established(net, net->ipv4.tcp_death_row.hashinfo, x->id.daddr.a4, in esp_find_tcp_sk()
167 dport, x->props.saddr.a4, sport, 0); in esp_find_tcp_sk()
169 return ERR_PTR(-ENOENT); in esp_find_tcp_sk()
173 return ERR_PTR(-EINVAL); in esp_find_tcp_sk()
176 spin_lock_bh(&x->lock); in esp_find_tcp_sk()
177 nsk = rcu_dereference_protected(x->encap_sk, in esp_find_tcp_sk()
178 lockdep_is_held(&x->lock)); in esp_find_tcp_sk()
179 if (encap->encap_sport != sport || in esp_find_tcp_sk()
180 encap->encap_dport != dport) { in esp_find_tcp_sk()
182 sk = nsk ?: ERR_PTR(-EREMCHG); in esp_find_tcp_sk()
186 rcu_assign_pointer(x->encap_sk, sk); in esp_find_tcp_sk()
188 spin_unlock_bh(&x->lock); in esp_find_tcp_sk()
221 struct xfrm_state *x = dst->xfrm; in esp_output_tcp_encap_cb()
238 return err ?: -EINPROGRESS; in esp_output_tail_tcp()
244 return -EOPNOTSUPP; in esp_output_tail_tcp()
255 if (xo && (xo->flags & XFRM_DEV_RESUME)) { in esp_output_done()
258 x = sp->xvec[sp->len - 1]; in esp_output_done()
260 x = skb_dst(skb)->xfrm; in esp_output_done()
263 tmp = ESP_SKB_CB(skb)->tmp; in esp_output_done()
267 if (xo && (xo->flags & XFRM_DEV_RESUME)) { in esp_output_done()
274 skb_push(skb, skb->data - skb_mac_header(skb)); in esp_output_done()
279 x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP) in esp_output_done()
282 xfrm_output_resume(skb->sk, skb, err); in esp_output_done()
289 struct ip_esp_hdr *esph = (void *)(skb->data + offset); in esp_restore_header()
290 void *tmp = ESP_SKB_CB(skb)->tmp; in esp_restore_header()
293 esph->seq_no = esph->spi; in esp_restore_header()
294 esph->spi = *seqhi; in esp_restore_header()
299 void *tmp = ESP_SKB_CB(skb)->tmp; in esp_output_restore_header()
302 esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff - in esp_output_restore_header()
315 if ((x->props.flags & XFRM_STATE_ESN)) { in esp_output_set_extra()
320 seqhi = xo->seq.hi; in esp_output_set_extra()
322 seqhi = XFRM_SKB_CB(skb)->seq.output.hi; in esp_output_set_extra()
324 extra->esphoff = (unsigned char *)esph - in esp_output_set_extra()
326 esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4); in esp_output_set_extra()
327 extra->seqhi = esph->spi; in esp_output_set_extra()
328 esph->seq_no = htonl(seqhi); in esp_output_set_extra()
331 esph->spi = x->id.spi; in esp_output_set_extra()
354 len = skb->len + esp->tailen - skb_transport_offset(skb); in esp_output_udp_encap()
356 return ERR_PTR(-EMSGSIZE); in esp_output_udp_encap()
358 uh = (struct udphdr *)esp->esph; in esp_output_udp_encap()
359 uh->source = sport; in esp_output_udp_encap()
360 uh->dest = dport; in esp_output_udp_encap()
361 uh->len = htons(len); in esp_output_udp_encap()
362 uh->check = 0; in esp_output_udp_encap()
379 __be16 *lenp = (void *)esp->esph; in esp_output_tcp_encap()
384 len = skb->len + esp->tailen - skb_transport_offset(skb); in esp_output_tcp_encap()
386 return ERR_PTR(-EMSGSIZE); in esp_output_tcp_encap()
405 return ERR_PTR(-EOPNOTSUPP); in esp_output_tcp_encap()
412 struct xfrm_encap_tmpl *encap = x->encap; in esp_output_encap()
417 spin_lock_bh(&x->lock); in esp_output_encap()
418 sport = encap->encap_sport; in esp_output_encap()
419 dport = encap->encap_dport; in esp_output_encap()
420 encap_type = encap->encap_type; in esp_output_encap()
421 spin_unlock_bh(&x->lock); in esp_output_encap()
436 esp->esph = esph; in esp_output_encap()
448 int tailen = esp->tailen; in esp_output_head()
450 /* this is non-NULL only with TCP/UDP Encapsulation */ in esp_output_head()
451 if (x->encap) { in esp_output_head()
459 ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE) in esp_output_head()
469 } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS) in esp_output_head()
472 struct sock *sk = skb->sk; in esp_output_head()
473 struct page_frag *pfrag = &x->xfrag; in esp_output_head()
475 esp->inplace = false; in esp_output_head()
479 spin_lock_bh(&x->lock); in esp_output_head()
482 spin_unlock_bh(&x->lock); in esp_output_head()
486 page = pfrag->page; in esp_output_head()
489 tail = page_address(page) + pfrag->offset; in esp_output_head()
491 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); in esp_output_head()
493 nfrags = skb_shinfo(skb)->nr_frags; in esp_output_head()
495 __skb_fill_page_desc(skb, nfrags, page, pfrag->offset, in esp_output_head()
497 skb_shinfo(skb)->nr_frags = ++nfrags; in esp_output_head()
499 pfrag->offset = pfrag->offset + allocsize; in esp_output_head()
501 spin_unlock_bh(&x->lock); in esp_output_head()
507 refcount_add(tailen, &sk->sk_wmem_alloc); in esp_output_head()
514 esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb); in esp_output_head()
520 esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset); in esp_output_head()
523 esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); in esp_output_head()
545 int err = -ENOMEM; in esp_output_tail()
550 if (x->props.flags & XFRM_STATE_ESN) { in esp_output_tail()
555 aead = x->data; in esp_output_tail()
559 tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen); in esp_output_tail()
568 if (esp->inplace) in esp_output_tail()
571 dsg = &sg[esp->nfrags]; in esp_output_tail()
573 esph = esp_output_set_extra(skb, x, esp->esph, extra); in esp_output_tail()
574 esp->esph = esph; in esp_output_tail()
576 sg_init_table(sg, esp->nfrags); in esp_output_tail()
578 (unsigned char *)esph - skb->data, in esp_output_tail()
579 assoclen + ivlen + esp->clen + alen); in esp_output_tail()
583 if (!esp->inplace) { in esp_output_tail()
585 struct page_frag *pfrag = &x->xfrag; in esp_output_tail()
587 allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES); in esp_output_tail()
589 spin_lock_bh(&x->lock); in esp_output_tail()
591 spin_unlock_bh(&x->lock); in esp_output_tail()
595 skb_shinfo(skb)->nr_frags = 1; in esp_output_tail()
597 page = pfrag->page; in esp_output_tail()
600 __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len); in esp_output_tail()
601 pfrag->offset = pfrag->offset + allocsize; in esp_output_tail()
602 spin_unlock_bh(&x->lock); in esp_output_tail()
604 sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1); in esp_output_tail()
606 (unsigned char *)esph - skb->data, in esp_output_tail()
607 assoclen + ivlen + esp->clen + alen); in esp_output_tail()
612 if ((x->props.flags & XFRM_STATE_ESN)) in esp_output_tail()
617 aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv); in esp_output_tail()
621 memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8), in esp_output_tail()
624 ESP_SKB_CB(skb)->tmp = tmp; in esp_output_tail()
628 case -EINPROGRESS: in esp_output_tail()
631 case -ENOSPC: in esp_output_tail()
636 if ((x->props.flags & XFRM_STATE_ESN)) in esp_output_tail()
643 if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP) in esp_output_tail()
668 aead = x->data; in esp_output()
672 if (x->tfcpad) { in esp_output()
676 padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached)); in esp_output()
677 if (skb->len < padto) in esp_output()
678 esp.tfclen = padto - skb->len; in esp_output()
681 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); in esp_output()
682 esp.plen = esp.clen - skb->len - esp.tfclen; in esp_output()
692 esph->spi = x->id.spi; in esp_output()
694 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); in esp_output()
695 esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low + in esp_output()
696 ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32)); in esp_output()
698 skb_push(skb, -skb_network_offset(skb)); in esp_output()
706 struct crypto_aead *aead = x->data; in esp_remove_trailer()
715 elen = skb->len - hlen; in esp_remove_trailer()
717 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2)) in esp_remove_trailer()
720 ret = -EINVAL; in esp_remove_trailer()
724 padlen + 2, elen - alen); in esp_remove_trailer()
729 if (skb->ip_summed == CHECKSUM_COMPLETE) { in esp_remove_trailer()
730 csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0); in esp_remove_trailer()
731 skb->csum = csum_block_sub(skb->csum, csumdiff, in esp_remove_trailer()
732 skb->len - trimlen); in esp_remove_trailer()
734 ret = pskb_trim(skb, skb->len - trimlen); in esp_remove_trailer()
749 struct crypto_aead *aead = x->data; in esp_input_done2()
753 if (!xo || !(xo->flags & CRYPTO_DONE)) in esp_input_done2()
754 kfree(ESP_SKB_CB(skb)->tmp); in esp_input_done2()
764 ihl = iph->ihl * 4; in esp_input_done2()
766 if (x->encap) { in esp_input_done2()
767 struct xfrm_encap_tmpl *encap = x->encap; in esp_input_done2()
772 switch (x->encap->encap_type) { in esp_input_done2()
774 source = th->source; in esp_input_done2()
777 source = uh->source; in esp_input_done2()
781 err = -EINVAL; in esp_input_done2()
786 * 1) if the NAT-T peer's IP or port changed then in esp_input_done2()
791 if (iph->saddr != x->props.saddr.a4 || in esp_input_done2()
792 source != encap->encap_sport) { in esp_input_done2()
795 ipaddr.a4 = iph->saddr; in esp_input_done2()
809 * of NAT-T in Transport Mode, or in esp_input_done2()
810 * perform other post-processing fixes in esp_input_done2()
811 * as per draft-ietf-ipsec-udp-encaps-06, in esp_input_done2()
814 if (x->props.mode == XFRM_MODE_TRANSPORT) in esp_input_done2()
815 skb->ip_summed = CHECKSUM_UNNECESSARY; in esp_input_done2()
819 if (x->props.mode == XFRM_MODE_TUNNEL) in esp_input_done2()
822 skb_set_transport_header(skb, -ihl); in esp_input_done2()
826 err = -EINVAL; in esp_input_done2()
855 if ((x->props.flags & XFRM_STATE_ESN)) { in esp_input_set_header()
857 *seqhi = esph->spi; in esp_input_set_header()
858 esph->spi = esph->seq_no; in esp_input_set_header()
859 esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi; in esp_input_set_header()
872 * Note: detecting truncated vs. non-truncated authentication data is very
878 struct crypto_aead *aead = x->data; in esp_input()
882 int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen; in esp_input()
890 int err = -EINVAL; in esp_input()
901 if (x->props.flags & XFRM_STATE_ESN) { in esp_input()
912 nfrags = skb_shinfo(skb)->nr_frags; in esp_input()
926 err = -ENOMEM; in esp_input()
931 ESP_SKB_CB(skb)->tmp = tmp; in esp_input()
940 err = skb_to_sgvec(skb, sg, 0, skb->len); in esp_input()
946 skb->ip_summed = CHECKSUM_NONE; in esp_input()
948 if ((x->props.flags & XFRM_STATE_ESN)) in esp_input()
957 if (err == -EINPROGRESS) in esp_input()
960 if ((x->props.flags & XFRM_STATE_ESN)) in esp_input()
971 struct net *net = dev_net(skb->dev); in esp4_err()
972 const struct iphdr *iph = (const struct iphdr *)skb->data; in esp4_err()
973 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); in esp4_err()
976 switch (icmp_hdr(skb)->type) { in esp4_err()
978 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) in esp4_err()
987 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, in esp4_err()
988 esph->spi, IPPROTO_ESP, AF_INET); in esp4_err()
992 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) in esp4_err()
1003 struct crypto_aead *aead = x->data; in esp_destroy()
1018 x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) { in esp_init_aead()
1020 return -ENAMETOOLONG; in esp_init_aead()
1028 x->data = aead; in esp_init_aead()
1030 err = crypto_aead_setkey(aead, x->aead->alg_key, in esp_init_aead()
1031 (x->aead->alg_key_len + 7) / 8); in esp_init_aead()
1035 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8); in esp_init_aead()
1058 err = -ENAMETOOLONG; in esp_init_authenc()
1060 if ((x->props.flags & XFRM_STATE_ESN)) { in esp_init_authenc()
1063 x->geniv ?: "", x->geniv ? "(" : "", in esp_init_authenc()
1064 x->aalg ? x->aalg->alg_name : "digest_null", in esp_init_authenc()
1065 x->ealg->alg_name, in esp_init_authenc()
1066 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) { in esp_init_authenc()
1073 x->geniv ?: "", x->geniv ? "(" : "", in esp_init_authenc()
1074 x->aalg ? x->aalg->alg_name : "digest_null", in esp_init_authenc()
1075 x->ealg->alg_name, in esp_init_authenc()
1076 x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) { in esp_init_authenc()
1089 x->data = aead; in esp_init_authenc()
1091 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) + in esp_init_authenc()
1092 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param)); in esp_init_authenc()
1093 err = -ENOMEM; in esp_init_authenc()
1100 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; in esp_init_authenc()
1101 rta->rta_len = RTA_LENGTH(sizeof(*param)); in esp_init_authenc()
1105 if (x->aalg) { in esp_init_authenc()
1108 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8); in esp_init_authenc()
1109 p += (x->aalg->alg_key_len + 7) / 8; in esp_init_authenc()
1111 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); in esp_init_authenc()
1114 err = -EINVAL; in esp_init_authenc()
1115 if (aalg_desc->uinfo.auth.icv_fullbits / 8 != in esp_init_authenc()
1122 aead, x->aalg->alg_trunc_len / 8); in esp_init_authenc()
1129 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8); in esp_init_authenc()
1130 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8); in esp_init_authenc()
1147 x->data = NULL; in esp_init_state()
1149 if (x->aead) { in esp_init_state()
1151 } else if (x->ealg) { in esp_init_state()
1155 err = -EINVAL; in esp_init_state()
1161 aead = x->data; in esp_init_state()
1163 x->props.header_len = sizeof(struct ip_esp_hdr) + in esp_init_state()
1165 if (x->props.mode == XFRM_MODE_TUNNEL) in esp_init_state()
1166 x->props.header_len += sizeof(struct iphdr); in esp_init_state()
1167 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6) in esp_init_state()
1168 x->props.header_len += IPV4_BEET_PHMAXLEN; in esp_init_state()
1169 if (x->encap) { in esp_init_state()
1170 struct xfrm_encap_tmpl *encap = x->encap; in esp_init_state()
1172 switch (encap->encap_type) { in esp_init_state()
1175 err = -EINVAL; in esp_init_state()
1178 x->props.header_len += sizeof(struct udphdr); in esp_init_state()
1185 x->props.header_len += 2; in esp_init_state()
1192 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead); in esp_init_state()
1226 return -EAGAIN; in esp4_init()
1231 return -EAGAIN; in esp4_init()
1245 MODULE_DESCRIPTION("IPv4 ESP transformation library");