| @@ -7,6 +7,7 @@ · 6 unmodified lines |
| 7 | 7 | #include <linux/tcp.h> |
| 8 | 8 | #include <linux/rcupdate.h> |
| 9 | 9 | #include <linux/rculist.h> |
| 10 | +#include <linux/siphash.h> |
| 10 | 11 | #include <net/inetpeer.h> |
| 11 | 12 | #include <net/tcp.h> |
| 12 | 13 | |
| @@ -37,14 +38,8 @@ static void tcp_fastopen_ctx_free(struct rcu_head *head) · 24 unmodified lines |
| 37 | 38 | { |
| 38 | 39 | struct tcp_fastopen_context *ctx = |
| 39 | 40 | container_of(head, struct tcp_fastopen_context, rcu); |
| 40 | | − int i; |
| 41 | 41 | |
| 42 | | − /* We own ctx, thus no need to hold the Fastopen-lock */ |
| 43 | | − for (i = 0; i < TCP_FASTOPEN_KEY_MAX; i++) { |
| 44 | | − if (ctx->tfm[i]) |
| 45 | | − crypto_free_cipher(ctx->tfm[i]); |
| 46 | | − } |
| 47 | | − kfree(ctx); |
| 42 | + kzfree(ctx); |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | void tcp_fastopen_destroy_cipher(struct sock *sk) |
| @@ -72,41 +67,6 @@ void tcp_fastopen_ctx_destroy(struct net *net) · 21 unmodified lines |
| 72 | 67 | call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free); |
| 73 | 68 | } |
| 74 | 69 | |
| 75 | | −static struct tcp_fastopen_context *tcp_fastopen_alloc_ctx(void *primary_key, |
| 76 | | − void *backup_key, |
| 77 | | − unsigned int len) |
| 78 | | −{ |
| 79 | | − struct tcp_fastopen_context *new_ctx; |
| 80 | | − void *key = primary_key; |
| 81 | | − int err, i; |
| 82 | | − |
| 83 | | − new_ctx = kmalloc(sizeof(*new_ctx), GFP_KERNEL); |
| 84 | | − if (!new_ctx) |
| 85 | | − return ERR_PTR(-ENOMEM); |
| 86 | | − for (i = 0; i < TCP_FASTOPEN_KEY_MAX; i++) |
| 87 | | − new_ctx->tfm[i] = NULL; |
| 88 | | − for (i = 0; i < (backup_key ? 2 : 1); i++) { |
| 89 | | − new_ctx->tfm[i] = crypto_alloc_cipher("aes", 0, 0); |
| 90 | | − if (IS_ERR(new_ctx->tfm[i])) { |
| 91 | | − err = PTR_ERR(new_ctx->tfm[i]); |
| 92 | | − new_ctx->tfm[i] = NULL; |
| 93 | | − pr_err("TCP: TFO aes cipher alloc error: %d\n", err); |
| 94 | | − goto out; |
| 95 | | − } |
| 96 | | − err = crypto_cipher_setkey(new_ctx->tfm[i], key, len); |
| 97 | | − if (err) { |
| 98 | | − pr_err("TCP: TFO cipher key error: %d\n", err); |
| 99 | | − goto out; |
| 100 | | − } |
| 101 | | − memcpy(&new_ctx->key[i * TCP_FASTOPEN_KEY_LENGTH], key, len); |
| 102 | | − key = backup_key; |
| 103 | | − } |
| 104 | | − return new_ctx; |
| 105 | | −out: |
| 106 | | − tcp_fastopen_ctx_free(&new_ctx->rcu); |
| 107 | | − return ERR_PTR(err); |
| 108 | | −} |
| 109 | | − |
| 110 | 70 | int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk, |
| 111 | 71 | void *primary_key, void *backup_key, |
| 112 | 72 | unsigned int len) |
| @@ -115,11 +75,20 @@ int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk, · 2 unmodified lines |
| 115 | 75 | struct fastopen_queue *q; |
| 116 | 76 | int err = 0; |
| 117 | 77 | |
| 118 | | − ctx = tcp_fastopen_alloc_ctx(primary_key, backup_key, len); |
| 119 | | − if (IS_ERR(ctx)) { |
| 120 | | − err = PTR_ERR(ctx); |
| 78 | + ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
| 79 | + if (!ctx) { |
| 80 | + err = -ENOMEM; |
| 121 | 81 | goto out; |
| 122 | 82 | } |
| 83 | + |
| 84 | + memcpy(ctx->key[0], primary_key, len); |
| 85 | + if (backup_key) { |
| 86 | + memcpy(ctx->key[1], backup_key, len); |
| 87 | + ctx->num = 2; |
| 88 | + } else { |
| 89 | + ctx->num = 1; |
| 90 | + } |
| 91 | + |
| 123 | 92 | spin_lock(&net->ipv4.tcp_fastopen_ctx_lock); |
| 124 | 93 | if (sk) { |
| 125 | 94 | q = &inet_csk(sk)->icsk_accept_queue.fastopenq; |
| @@ -141,31 +110,30 @@ out: · 15 unmodified lines |
| 141 | 110 | |
| 142 | 111 | static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req, |
| 143 | 112 | struct sk_buff *syn, |
| 144 | | − struct crypto_cipher *tfm, |
| 113 | + const u8 *key, |
| 145 | 114 | struct tcp_fastopen_cookie *foc) |
| 146 | 115 | { |
| 116 | + BUILD_BUG_ON(TCP_FASTOPEN_KEY_LENGTH != sizeof(siphash_key_t)); |
| 117 | + BUILD_BUG_ON(TCP_FASTOPEN_COOKIE_SIZE != sizeof(u64)); |
| 118 | + |
| 147 | 119 | if (req->rsk_ops->family == AF_INET) { |
| 148 | 120 | const struct iphdr *iph = ip_hdr(syn); |
| 149 | | − __be32 path[4] = { iph->saddr, iph->daddr, 0, 0 }; |
| 150 | 121 | |
| 151 | | − crypto_cipher_encrypt_one(tfm, foc->val, (void *)path); |
| 122 | + foc->val[0] = siphash(&iph->saddr, |
| 123 | + sizeof(iph->saddr) + |
| 124 | + sizeof(iph->daddr), |
| 125 | + (const siphash_key_t *)key); |
| 152 | 126 | foc->len = TCP_FASTOPEN_COOKIE_SIZE; |
| 153 | 127 | return true; |
| 154 | 128 | } |
| 155 | | − |
| 156 | 129 | #if IS_ENABLED(CONFIG_IPV6) |
| 157 | 130 | if (req->rsk_ops->family == AF_INET6) { |
| 158 | 131 | const struct ipv6hdr *ip6h = ipv6_hdr(syn); |
| 159 | | − struct tcp_fastopen_cookie tmp; |
| 160 | | − struct in6_addr *buf; |
| 161 | | − int i; |
| 162 | | − |
| 163 | | − crypto_cipher_encrypt_one(tfm, tmp.val, |
| 164 | | − (void *)&ip6h->saddr); |
| 165 | | − buf = &tmp.addr; |
| 166 | | − for (i = 0; i < 4; i++) |
| 167 | | − buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i]; |
| 168 | | − crypto_cipher_encrypt_one(tfm, foc->val, (void *)buf); |
| 132 | + |
| 133 | + foc->val[0] = siphash(&ip6h->saddr, |
| 134 | + sizeof(ip6h->saddr) + |
| 135 | + sizeof(ip6h->daddr), |
| 136 | + (const siphash_key_t *)key); |
| 169 | 137 | foc->len = TCP_FASTOPEN_COOKIE_SIZE; |
| 170 | 138 | return true; |
| 171 | 139 | } |
| @@ -173,11 +141,8 @@ static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req, · 1 unmodified line |
| 173 | 141 | return false; |
| 174 | 142 | } |
| 175 | 143 | |
| 176 | | −/* Generate the fastopen cookie by doing aes128 encryption on both |
| 177 | | − * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6 |
| 178 | | − * addresses. For the longer IPv6 addresses use CBC-MAC. |
| 179 | | − * |
| 180 | | − * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE. |
| 144 | +/* Generate the fastopen cookie by applying SipHash to both the source and |
| 145 | + * destination addresses. |
| 181 | 146 | */ |
| 182 | 147 | static void tcp_fastopen_cookie_gen(struct sock *sk, |
| 183 | 148 | struct request_sock *req, |
| @@ -189,7 +154,7 @@ static void tcp_fastopen_cookie_gen(struct sock *sk, · 5 unmodified lines |
| 189 | 154 | rcu_read_lock(); |
| 190 | 155 | ctx = tcp_fastopen_get_ctx(sk); |
| 191 | 156 | if (ctx) |
| 192 | | − __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->tfm[0], foc); |
| 157 | + __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->key[0], foc); |
| 193 | 158 | rcu_read_unlock(); |
| 194 | 159 | } |
| 195 | 160 | |
| @@ -253,7 +218,7 @@ static int tcp_fastopen_cookie_gen_check(struct sock *sk, · 57 unmodified lines |
| 253 | 218 | if (!ctx) |
| 254 | 219 | goto out; |
| 255 | 220 | for (i = 0; i < tcp_fastopen_context_len(ctx); i++) { |
| 256 | | − __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->tfm[i], foc); |
| 221 | + __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->key[i], foc); |
| 257 | 222 | if (tcp_fastopen_cookie_match(foc, orig)) { |
| 258 | 223 | ret = i + 1; |
| 259 | 224 | goto out; |