aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuchung Cheng <ycheng@google.com>2013-08-08 17:06:22 -0400
committerDavid S. Miller <davem@davemloft.net>2013-08-10 03:35:33 -0400
commit149479d019e06df5a7f4096f95c00cfb1380309c (patch)
tree693d54479d3377a5b4bd7ddfb61394f26706defd
parent469230d118dc0822f6bf46c75ab147fa9f00741f (diff)
tcp: add server ip to encrypt cookie in fast open
Encrypt the cookie with both server and client IPv4 addresses, such that multi-homed server will grant different cookies based on both the source and destination IPs. No client change is needed since cookie is opaque to the client. Signed-off-by: Yuchung Cheng <ycheng@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Acked-by: Neal Cardwell <ncardwell@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/net/tcp.h3
-rw-r--r--net/ipv4/tcp_fastopen.c13
-rw-r--r--net/ipv4/tcp_ipv4.c10
3 files changed, 15 insertions, 11 deletions
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 27b652f379ef..09cb5c11ceea 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1300,7 +1300,8 @@ void tcp_free_fastopen_req(struct tcp_sock *tp);
1300 1300
1301extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx; 1301extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
1302int tcp_fastopen_reset_cipher(void *key, unsigned int len); 1302int tcp_fastopen_reset_cipher(void *key, unsigned int len);
1303void tcp_fastopen_cookie_gen(__be32 addr, struct tcp_fastopen_cookie *foc); 1303extern void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
1304 struct tcp_fastopen_cookie *foc);
1304 1305
1305#define TCP_FASTOPEN_KEY_LENGTH 16 1306#define TCP_FASTOPEN_KEY_LENGTH 16
1306 1307
diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index 8f7ef0ad80e5..ab7bd35bb312 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -58,23 +58,22 @@ error: kfree(ctx);
58 return err; 58 return err;
59} 59}
60 60
61/* Computes the fastopen cookie for the peer. 61/* Computes the fastopen cookie for the IP path.
62 * The peer address is a 128 bits long (pad with zeros for IPv4). 62 * The path is a 128 bits long (pad with zeros for IPv4).
63 * 63 *
64 * The caller must check foc->len to determine if a valid cookie 64 * The caller must check foc->len to determine if a valid cookie
65 * has been generated successfully. 65 * has been generated successfully.
66*/ 66*/
67void tcp_fastopen_cookie_gen(__be32 addr, struct tcp_fastopen_cookie *foc) 67void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
68 struct tcp_fastopen_cookie *foc)
68{ 69{
69 __be32 peer_addr[4] = { addr, 0, 0, 0 }; 70 __be32 path[4] = { src, dst, 0, 0 };
70 struct tcp_fastopen_context *ctx; 71 struct tcp_fastopen_context *ctx;
71 72
72 rcu_read_lock(); 73 rcu_read_lock();
73 ctx = rcu_dereference(tcp_fastopen_ctx); 74 ctx = rcu_dereference(tcp_fastopen_ctx);
74 if (ctx) { 75 if (ctx) {
75 crypto_cipher_encrypt_one(ctx->tfm, 76 crypto_cipher_encrypt_one(ctx->tfm, foc->val, (__u8 *)path);
76 foc->val,
77 (__u8 *)peer_addr);
78 foc->len = TCP_FASTOPEN_COOKIE_SIZE; 77 foc->len = TCP_FASTOPEN_COOKIE_SIZE;
79 } 78 }
80 rcu_read_unlock(); 79 rcu_read_unlock();
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 280efe5f19c1..ec2702882d8d 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -1316,9 +1316,11 @@ static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
1316 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 1316 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1317 return true; 1317 return true;
1318 } 1318 }
1319
1319 if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) { 1320 if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) {
1320 if ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_CHKED) == 0) { 1321 if ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_CHKED) == 0) {
1321 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); 1322 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
1323 ip_hdr(skb)->daddr, valid_foc);
1322 if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) || 1324 if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) ||
1323 memcmp(&foc->val[0], &valid_foc->val[0], 1325 memcmp(&foc->val[0], &valid_foc->val[0],
1324 TCP_FASTOPEN_COOKIE_SIZE) != 0) 1326 TCP_FASTOPEN_COOKIE_SIZE) != 0)
@@ -1329,14 +1331,16 @@ static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
1329 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 1331 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1330 return true; 1332 return true;
1331 } else if (foc->len == 0) { /* Client requesting a cookie */ 1333 } else if (foc->len == 0) { /* Client requesting a cookie */
1332 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); 1334 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
1335 ip_hdr(skb)->daddr, valid_foc);
1333 NET_INC_STATS_BH(sock_net(sk), 1336 NET_INC_STATS_BH(sock_net(sk),
1334 LINUX_MIB_TCPFASTOPENCOOKIEREQD); 1337 LINUX_MIB_TCPFASTOPENCOOKIEREQD);
1335 } else { 1338 } else {
1336 /* Client sent a cookie with wrong size. Treat it 1339 /* Client sent a cookie with wrong size. Treat it
1337 * the same as invalid and return a valid one. 1340 * the same as invalid and return a valid one.
1338 */ 1341 */
1339 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); 1342 tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
1343 ip_hdr(skb)->daddr, valid_foc);
1340 } 1344 }
1341 return false; 1345 return false;
1342} 1346}