Commit cf80e0e4 authored by Herbert Xu's avatar Herbert Xu

tcp: Use ahash

This patch replaces uses of the long obsolete hash interface with
ahash.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Acked-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 5821c769
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <linux/cache.h> #include <linux/cache.h>
#include <linux/percpu.h> #include <linux/percpu.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
#include <linux/crypto.h>
#include <linux/cryptohash.h> #include <linux/cryptohash.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/ktime.h> #include <linux/ktime.h>
...@@ -1325,9 +1324,6 @@ static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp) ...@@ -1325,9 +1324,6 @@ static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
tp->retransmit_skb_hint = NULL; tp->retransmit_skb_hint = NULL;
} }
/* MD5 Signature */
struct crypto_hash;
union tcp_md5_addr { union tcp_md5_addr {
struct in_addr a4; struct in_addr a4;
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
...@@ -1376,7 +1372,7 @@ union tcp_md5sum_block { ...@@ -1376,7 +1372,7 @@ union tcp_md5sum_block {
/* - pool: digest algorithm, hash description and scratch buffer */ /* - pool: digest algorithm, hash description and scratch buffer */
struct tcp_md5sig_pool { struct tcp_md5sig_pool {
struct hash_desc md5_desc; struct ahash_request *md5_req;
union tcp_md5sum_block md5_blk; union tcp_md5sum_block md5_blk;
}; };
......
...@@ -247,6 +247,7 @@ ...@@ -247,6 +247,7 @@
#define pr_fmt(fmt) "TCP: " fmt #define pr_fmt(fmt) "TCP: " fmt
#include <crypto/hash.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/types.h> #include <linux/types.h>
...@@ -266,7 +267,6 @@ ...@@ -266,7 +267,6 @@
#include <linux/swap.h> #include <linux/swap.h>
#include <linux/cache.h> #include <linux/cache.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/crypto.h>
#include <linux/time.h> #include <linux/time.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -2939,17 +2939,26 @@ static bool tcp_md5sig_pool_populated = false; ...@@ -2939,17 +2939,26 @@ static bool tcp_md5sig_pool_populated = false;
static void __tcp_alloc_md5sig_pool(void) static void __tcp_alloc_md5sig_pool(void)
{ {
struct crypto_ahash *hash;
int cpu; int cpu;
hash = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR_OR_NULL(hash))
return;
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
if (!per_cpu(tcp_md5sig_pool, cpu).md5_desc.tfm) { struct ahash_request *req;
struct crypto_hash *hash;
hash = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); if (per_cpu(tcp_md5sig_pool, cpu).md5_req)
if (IS_ERR_OR_NULL(hash)) continue;
req = ahash_request_alloc(hash, GFP_KERNEL);
if (!req)
return; return;
per_cpu(tcp_md5sig_pool, cpu).md5_desc.tfm = hash;
} ahash_request_set_callback(req, 0, NULL, NULL);
per_cpu(tcp_md5sig_pool, cpu).md5_req = req;
} }
/* before setting tcp_md5sig_pool_populated, we must commit all writes /* before setting tcp_md5sig_pool_populated, we must commit all writes
* to memory. See smp_rmb() in tcp_get_md5sig_pool() * to memory. See smp_rmb() in tcp_get_md5sig_pool()
...@@ -2999,7 +3008,6 @@ int tcp_md5_hash_header(struct tcp_md5sig_pool *hp, ...@@ -2999,7 +3008,6 @@ int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
{ {
struct scatterlist sg; struct scatterlist sg;
struct tcphdr hdr; struct tcphdr hdr;
int err;
/* We are not allowed to change tcphdr, make a local copy */ /* We are not allowed to change tcphdr, make a local copy */
memcpy(&hdr, th, sizeof(hdr)); memcpy(&hdr, th, sizeof(hdr));
...@@ -3007,8 +3015,8 @@ int tcp_md5_hash_header(struct tcp_md5sig_pool *hp, ...@@ -3007,8 +3015,8 @@ int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
/* options aren't included in the hash */ /* options aren't included in the hash */
sg_init_one(&sg, &hdr, sizeof(hdr)); sg_init_one(&sg, &hdr, sizeof(hdr));
err = crypto_hash_update(&hp->md5_desc, &sg, sizeof(hdr)); ahash_request_set_crypt(hp->md5_req, &sg, NULL, sizeof(hdr));
return err; return crypto_ahash_update(hp->md5_req);
} }
EXPORT_SYMBOL(tcp_md5_hash_header); EXPORT_SYMBOL(tcp_md5_hash_header);
...@@ -3017,7 +3025,7 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp, ...@@ -3017,7 +3025,7 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
{ {
struct scatterlist sg; struct scatterlist sg;
const struct tcphdr *tp = tcp_hdr(skb); const struct tcphdr *tp = tcp_hdr(skb);
struct hash_desc *desc = &hp->md5_desc; struct ahash_request *req = hp->md5_req;
unsigned int i; unsigned int i;
const unsigned int head_data_len = skb_headlen(skb) > header_len ? const unsigned int head_data_len = skb_headlen(skb) > header_len ?
skb_headlen(skb) - header_len : 0; skb_headlen(skb) - header_len : 0;
...@@ -3027,7 +3035,8 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp, ...@@ -3027,7 +3035,8 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
sg_init_table(&sg, 1); sg_init_table(&sg, 1);
sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len); sg_set_buf(&sg, ((u8 *) tp) + header_len, head_data_len);
if (crypto_hash_update(desc, &sg, head_data_len)) ahash_request_set_crypt(req, &sg, NULL, head_data_len);
if (crypto_ahash_update(req))
return 1; return 1;
for (i = 0; i < shi->nr_frags; ++i) { for (i = 0; i < shi->nr_frags; ++i) {
...@@ -3037,7 +3046,8 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp, ...@@ -3037,7 +3046,8 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
sg_set_page(&sg, page, skb_frag_size(f), sg_set_page(&sg, page, skb_frag_size(f),
offset_in_page(offset)); offset_in_page(offset));
if (crypto_hash_update(desc, &sg, skb_frag_size(f))) ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
if (crypto_ahash_update(req))
return 1; return 1;
} }
...@@ -3054,7 +3064,8 @@ int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *ke ...@@ -3054,7 +3064,8 @@ int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *ke
struct scatterlist sg; struct scatterlist sg;
sg_init_one(&sg, key->key, key->keylen); sg_init_one(&sg, key->key, key->keylen);
return crypto_hash_update(&hp->md5_desc, &sg, key->keylen); ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen);
return crypto_ahash_update(hp->md5_req);
} }
EXPORT_SYMBOL(tcp_md5_hash_key); EXPORT_SYMBOL(tcp_md5_hash_key);
......
#include <linux/crypto.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/kernel.h> #include <linux/kernel.h>
......
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/crypto.h> #include <crypto/hash.h>
#include <linux/scatterlist.h> #include <linux/scatterlist.h>
int sysctl_tcp_tw_reuse __read_mostly; int sysctl_tcp_tw_reuse __read_mostly;
...@@ -1031,21 +1031,22 @@ static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp, ...@@ -1031,21 +1031,22 @@ static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
bp->len = cpu_to_be16(nbytes); bp->len = cpu_to_be16(nbytes);
sg_init_one(&sg, bp, sizeof(*bp)); sg_init_one(&sg, bp, sizeof(*bp));
return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp)); ahash_request_set_crypt(hp->md5_req, &sg, NULL, sizeof(*bp));
return crypto_ahash_update(hp->md5_req);
} }
static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
__be32 daddr, __be32 saddr, const struct tcphdr *th) __be32 daddr, __be32 saddr, const struct tcphdr *th)
{ {
struct tcp_md5sig_pool *hp; struct tcp_md5sig_pool *hp;
struct hash_desc *desc; struct ahash_request *req;
hp = tcp_get_md5sig_pool(); hp = tcp_get_md5sig_pool();
if (!hp) if (!hp)
goto clear_hash_noput; goto clear_hash_noput;
desc = &hp->md5_desc; req = hp->md5_req;
if (crypto_hash_init(desc)) if (crypto_ahash_init(req))
goto clear_hash; goto clear_hash;
if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2)) if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
goto clear_hash; goto clear_hash;
...@@ -1053,7 +1054,8 @@ static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, ...@@ -1053,7 +1054,8 @@ static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
goto clear_hash; goto clear_hash;
if (tcp_md5_hash_key(hp, key)) if (tcp_md5_hash_key(hp, key))
goto clear_hash; goto clear_hash;
if (crypto_hash_final(desc, md5_hash)) ahash_request_set_crypt(req, NULL, md5_hash, 0);
if (crypto_ahash_final(req))
goto clear_hash; goto clear_hash;
tcp_put_md5sig_pool(); tcp_put_md5sig_pool();
...@@ -1071,7 +1073,7 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, ...@@ -1071,7 +1073,7 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
const struct sk_buff *skb) const struct sk_buff *skb)
{ {
struct tcp_md5sig_pool *hp; struct tcp_md5sig_pool *hp;
struct hash_desc *desc; struct ahash_request *req;
const struct tcphdr *th = tcp_hdr(skb); const struct tcphdr *th = tcp_hdr(skb);
__be32 saddr, daddr; __be32 saddr, daddr;
...@@ -1087,9 +1089,9 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, ...@@ -1087,9 +1089,9 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
hp = tcp_get_md5sig_pool(); hp = tcp_get_md5sig_pool();
if (!hp) if (!hp)
goto clear_hash_noput; goto clear_hash_noput;
desc = &hp->md5_desc; req = hp->md5_req;
if (crypto_hash_init(desc)) if (crypto_ahash_init(req))
goto clear_hash; goto clear_hash;
if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len)) if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
...@@ -1100,7 +1102,8 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, ...@@ -1100,7 +1102,8 @@ int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
goto clear_hash; goto clear_hash;
if (tcp_md5_hash_key(hp, key)) if (tcp_md5_hash_key(hp, key))
goto clear_hash; goto clear_hash;
if (crypto_hash_final(desc, md5_hash)) ahash_request_set_crypt(req, NULL, md5_hash, 0);
if (crypto_ahash_final(req))
goto clear_hash; goto clear_hash;
tcp_put_md5sig_pool(); tcp_put_md5sig_pool();
......
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/crypto.h> #include <crypto/hash.h>
#include <linux/scatterlist.h> #include <linux/scatterlist.h>
static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb); static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb);
...@@ -540,7 +540,8 @@ static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp, ...@@ -540,7 +540,8 @@ static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
bp->len = cpu_to_be32(nbytes); bp->len = cpu_to_be32(nbytes);
sg_init_one(&sg, bp, sizeof(*bp)); sg_init_one(&sg, bp, sizeof(*bp));
return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp)); ahash_request_set_crypt(hp->md5_req, &sg, NULL, sizeof(*bp));
return crypto_ahash_update(hp->md5_req);
} }
static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key, static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
...@@ -548,14 +549,14 @@ static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key, ...@@ -548,14 +549,14 @@ static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
const struct tcphdr *th) const struct tcphdr *th)
{ {
struct tcp_md5sig_pool *hp; struct tcp_md5sig_pool *hp;
struct hash_desc *desc; struct ahash_request *req;
hp = tcp_get_md5sig_pool(); hp = tcp_get_md5sig_pool();
if (!hp) if (!hp)
goto clear_hash_noput; goto clear_hash_noput;
desc = &hp->md5_desc; req = hp->md5_req;
if (crypto_hash_init(desc)) if (crypto_ahash_init(req))
goto clear_hash; goto clear_hash;
if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2)) if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
goto clear_hash; goto clear_hash;
...@@ -563,7 +564,8 @@ static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key, ...@@ -563,7 +564,8 @@ static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
goto clear_hash; goto clear_hash;
if (tcp_md5_hash_key(hp, key)) if (tcp_md5_hash_key(hp, key))
goto clear_hash; goto clear_hash;
if (crypto_hash_final(desc, md5_hash)) ahash_request_set_crypt(req, NULL, md5_hash, 0);
if (crypto_ahash_final(req))
goto clear_hash; goto clear_hash;
tcp_put_md5sig_pool(); tcp_put_md5sig_pool();
...@@ -583,7 +585,7 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, ...@@ -583,7 +585,7 @@ static int tcp_v6_md5_hash_skb(char *md5_hash,
{ {
const struct in6_addr *saddr, *daddr; const struct in6_addr *saddr, *daddr;
struct tcp_md5sig_pool *hp; struct tcp_md5sig_pool *hp;
struct hash_desc *desc; struct ahash_request *req;
const struct tcphdr *th = tcp_hdr(skb); const struct tcphdr *th = tcp_hdr(skb);
if (sk) { /* valid for establish/request sockets */ if (sk) { /* valid for establish/request sockets */
...@@ -598,9 +600,9 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, ...@@ -598,9 +600,9 @@ static int tcp_v6_md5_hash_skb(char *md5_hash,
hp = tcp_get_md5sig_pool(); hp = tcp_get_md5sig_pool();
if (!hp) if (!hp)
goto clear_hash_noput; goto clear_hash_noput;
desc = &hp->md5_desc; req = hp->md5_req;
if (crypto_hash_init(desc)) if (crypto_ahash_init(req))
goto clear_hash; goto clear_hash;
if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len)) if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
...@@ -611,7 +613,8 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, ...@@ -611,7 +613,8 @@ static int tcp_v6_md5_hash_skb(char *md5_hash,
goto clear_hash; goto clear_hash;
if (tcp_md5_hash_key(hp, key)) if (tcp_md5_hash_key(hp, key))
goto clear_hash; goto clear_hash;
if (crypto_hash_final(desc, md5_hash)) ahash_request_set_crypt(req, NULL, md5_hash, 0);
if (crypto_ahash_final(req))
goto clear_hash; goto clear_hash;
tcp_put_md5sig_pool(); tcp_put_md5sig_pool();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment