Skip to content

Commit 149479d

Browse files
yuchungchengdavem330
authored andcommitted
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 <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Acked-by: Neal Cardwell <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 469230d commit 149479d

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

include/net/tcp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,8 @@ void tcp_free_fastopen_req(struct tcp_sock *tp);
13001300

13011301
extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
13021302
int tcp_fastopen_reset_cipher(void *key, unsigned int len);
1303-
void tcp_fastopen_cookie_gen(__be32 addr, struct tcp_fastopen_cookie *foc);
1303+
extern void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
1304+
struct tcp_fastopen_cookie *foc);
13041305

13051306
#define TCP_FASTOPEN_KEY_LENGTH 16
13061307

net/ipv4/tcp_fastopen.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,22 @@ error: kfree(ctx);
5858
return err;
5959
}
6060

61-
/* Computes the fastopen cookie for the peer.
62-
* The peer address is a 128 bits long (pad with zeros for IPv4).
61+
/* Computes the fastopen cookie for the IP path.
62+
* The path is a 128 bits long (pad with zeros for IPv4).
6363
*
6464
* The caller must check foc->len to determine if a valid cookie
6565
* has been generated successfully.
6666
*/
67-
void tcp_fastopen_cookie_gen(__be32 addr, struct tcp_fastopen_cookie *foc)
67+
void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
68+
struct tcp_fastopen_cookie *foc)
6869
{
69-
__be32 peer_addr[4] = { addr, 0, 0, 0 };
70+
__be32 path[4] = { src, dst, 0, 0 };
7071
struct tcp_fastopen_context *ctx;
7172

7273
rcu_read_lock();
7374
ctx = rcu_dereference(tcp_fastopen_ctx);
7475
if (ctx) {
75-
crypto_cipher_encrypt_one(ctx->tfm,
76-
foc->val,
77-
(__u8 *)peer_addr);
76+
crypto_cipher_encrypt_one(ctx->tfm, foc->val, (__u8 *)path);
7877
foc->len = TCP_FASTOPEN_COOKIE_SIZE;
7978
}
8079
rcu_read_unlock();

net/ipv4/tcp_ipv4.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,11 @@ static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
13161316
tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
13171317
return true;
13181318
}
1319+
13191320
if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) {
13201321
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);
13221324
if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) ||
13231325
memcmp(&foc->val[0], &valid_foc->val[0],
13241326
TCP_FASTOPEN_COOKIE_SIZE) != 0)
@@ -1329,14 +1331,16 @@ static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
13291331
tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
13301332
return true;
13311333
} 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);
13331336
NET_INC_STATS_BH(sock_net(sk),
13341337
LINUX_MIB_TCPFASTOPENCOOKIEREQD);
13351338
} else {
13361339
/* Client sent a cookie with wrong size. Treat it
13371340
* the same as invalid and return a valid one.
13381341
*/
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);
13401344
}
13411345
return false;
13421346
}

0 commit comments

Comments
 (0)