hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/net/ipv4/tcp_fastopen.c
....@@ -30,15 +30,15 @@
3030 * for a valid cookie, so this is an acceptable risk.
3131 */
3232 get_random_bytes(key, sizeof(key));
33
- tcp_fastopen_reset_cipher(net, NULL, key, sizeof(key));
33
+ tcp_fastopen_reset_cipher(net, NULL, key, NULL);
3434 }
3535
3636 static void tcp_fastopen_ctx_free(struct rcu_head *head)
3737 {
3838 struct tcp_fastopen_context *ctx =
3939 container_of(head, struct tcp_fastopen_context, rcu);
40
- crypto_free_cipher(ctx->tfm);
41
- kfree(ctx);
40
+
41
+ kfree_sensitive(ctx);
4242 }
4343
4444 void tcp_fastopen_destroy_cipher(struct sock *sk)
....@@ -67,31 +67,27 @@
6767 }
6868
6969 int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
70
- void *key, unsigned int len)
70
+ void *primary_key, void *backup_key)
7171 {
7272 struct tcp_fastopen_context *ctx, *octx;
7373 struct fastopen_queue *q;
74
- int err;
74
+ int err = 0;
7575
7676 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
77
- if (!ctx)
78
- return -ENOMEM;
79
- ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
80
-
81
- if (IS_ERR(ctx->tfm)) {
82
- err = PTR_ERR(ctx->tfm);
83
-error: kfree(ctx);
84
- pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
85
- return err;
77
+ if (!ctx) {
78
+ err = -ENOMEM;
79
+ goto out;
8680 }
87
- err = crypto_cipher_setkey(ctx->tfm, key, len);
88
- if (err) {
89
- pr_err("TCP: TFO cipher key error: %d\n", err);
90
- crypto_free_cipher(ctx->tfm);
91
- goto error;
92
- }
93
- memcpy(ctx->key, key, len);
9481
82
+ ctx->key[0].key[0] = get_unaligned_le64(primary_key);
83
+ ctx->key[0].key[1] = get_unaligned_le64(primary_key + 8);
84
+ if (backup_key) {
85
+ ctx->key[1].key[0] = get_unaligned_le64(backup_key);
86
+ ctx->key[1].key[1] = get_unaligned_le64(backup_key + 8);
87
+ ctx->num = 2;
88
+ } else {
89
+ ctx->num = 1;
90
+ }
9591
9692 spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
9793 if (sk) {
....@@ -108,66 +104,81 @@
108104
109105 if (octx)
110106 call_rcu(&octx->rcu, tcp_fastopen_ctx_free);
107
+out:
111108 return err;
112109 }
113110
114
-static bool __tcp_fastopen_cookie_gen(struct sock *sk, const void *path,
115
- struct tcp_fastopen_cookie *foc)
111
+int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
112
+ u64 *key)
116113 {
117114 struct tcp_fastopen_context *ctx;
118
- bool ok = false;
115
+ int n_keys = 0, i;
119116
120117 rcu_read_lock();
121
-
122
- ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
123
- if (!ctx)
124
- ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
125
-
118
+ if (icsk)
119
+ ctx = rcu_dereference(icsk->icsk_accept_queue.fastopenq.ctx);
120
+ else
121
+ ctx = rcu_dereference(net->ipv4.tcp_fastopen_ctx);
126122 if (ctx) {
127
- crypto_cipher_encrypt_one(ctx->tfm, foc->val, path);
128
- foc->len = TCP_FASTOPEN_COOKIE_SIZE;
129
- ok = true;
123
+ n_keys = tcp_fastopen_context_len(ctx);
124
+ for (i = 0; i < n_keys; i++) {
125
+ put_unaligned_le64(ctx->key[i].key[0], key + (i * 2));
126
+ put_unaligned_le64(ctx->key[i].key[1], key + (i * 2) + 1);
127
+ }
130128 }
131129 rcu_read_unlock();
132
- return ok;
130
+
131
+ return n_keys;
133132 }
134133
135
-/* Generate the fastopen cookie by doing aes128 encryption on both
136
- * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
137
- * addresses. For the longer IPv6 addresses use CBC-MAC.
138
- *
139
- * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
140
- */
141
-static bool tcp_fastopen_cookie_gen(struct sock *sk,
142
- struct request_sock *req,
143
- struct sk_buff *syn,
144
- struct tcp_fastopen_cookie *foc)
134
+static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req,
135
+ struct sk_buff *syn,
136
+ const siphash_key_t *key,
137
+ struct tcp_fastopen_cookie *foc)
145138 {
139
+ BUILD_BUG_ON(TCP_FASTOPEN_COOKIE_SIZE != sizeof(u64));
140
+
146141 if (req->rsk_ops->family == AF_INET) {
147142 const struct iphdr *iph = ip_hdr(syn);
148143
149
- __be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
150
- return __tcp_fastopen_cookie_gen(sk, path, foc);
144
+ foc->val[0] = cpu_to_le64(siphash(&iph->saddr,
145
+ sizeof(iph->saddr) +
146
+ sizeof(iph->daddr),
147
+ key));
148
+ foc->len = TCP_FASTOPEN_COOKIE_SIZE;
149
+ return true;
151150 }
152
-
153151 #if IS_ENABLED(CONFIG_IPV6)
154152 if (req->rsk_ops->family == AF_INET6) {
155153 const struct ipv6hdr *ip6h = ipv6_hdr(syn);
156
- struct tcp_fastopen_cookie tmp;
157154
158
- if (__tcp_fastopen_cookie_gen(sk, &ip6h->saddr, &tmp)) {
159
- struct in6_addr *buf = &tmp.addr;
160
- int i;
161
-
162
- for (i = 0; i < 4; i++)
163
- buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
164
- return __tcp_fastopen_cookie_gen(sk, buf, foc);
165
- }
155
+ foc->val[0] = cpu_to_le64(siphash(&ip6h->saddr,
156
+ sizeof(ip6h->saddr) +
157
+ sizeof(ip6h->daddr),
158
+ key));
159
+ foc->len = TCP_FASTOPEN_COOKIE_SIZE;
160
+ return true;
166161 }
167162 #endif
168163 return false;
169164 }
170165
166
+/* Generate the fastopen cookie by applying SipHash to both the source and
167
+ * destination addresses.
168
+ */
169
+static void tcp_fastopen_cookie_gen(struct sock *sk,
170
+ struct request_sock *req,
171
+ struct sk_buff *syn,
172
+ struct tcp_fastopen_cookie *foc)
173
+{
174
+ struct tcp_fastopen_context *ctx;
175
+
176
+ rcu_read_lock();
177
+ ctx = tcp_fastopen_get_ctx(sk);
178
+ if (ctx)
179
+ __tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[0], foc);
180
+ rcu_read_unlock();
181
+}
171182
172183 /* If an incoming SYN or SYNACK frame contains a payload and/or FIN,
173184 * queue this additional data / FIN.
....@@ -212,6 +223,35 @@
212223 tcp_fin(sk);
213224 }
214225
226
+/* returns 0 - no key match, 1 for primary, 2 for backup */
227
+static int tcp_fastopen_cookie_gen_check(struct sock *sk,
228
+ struct request_sock *req,
229
+ struct sk_buff *syn,
230
+ struct tcp_fastopen_cookie *orig,
231
+ struct tcp_fastopen_cookie *valid_foc)
232
+{
233
+ struct tcp_fastopen_cookie search_foc = { .len = -1 };
234
+ struct tcp_fastopen_cookie *foc = valid_foc;
235
+ struct tcp_fastopen_context *ctx;
236
+ int i, ret = 0;
237
+
238
+ rcu_read_lock();
239
+ ctx = tcp_fastopen_get_ctx(sk);
240
+ if (!ctx)
241
+ goto out;
242
+ for (i = 0; i < tcp_fastopen_context_len(ctx); i++) {
243
+ __tcp_fastopen_cookie_gen_cipher(req, syn, &ctx->key[i], foc);
244
+ if (tcp_fastopen_cookie_match(foc, orig)) {
245
+ ret = i + 1;
246
+ goto out;
247
+ }
248
+ foc = &search_foc;
249
+ }
250
+out:
251
+ rcu_read_unlock();
252
+ return ret;
253
+}
254
+
215255 static struct sock *tcp_fastopen_create_child(struct sock *sk,
216256 struct sk_buff *skb,
217257 struct request_sock *req)
....@@ -220,10 +260,6 @@
220260 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
221261 struct sock *child;
222262 bool own_req;
223
-
224
- req->num_retrans = 0;
225
- req->num_timeout = 0;
226
- req->sk = NULL;
227263
228264 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
229265 NULL, &own_req);
....@@ -240,7 +276,7 @@
240276 */
241277 tp = tcp_sk(child);
242278
243
- tp->fastopen_rsk = req;
279
+ rcu_assign_pointer(tp->fastopen_rsk, req);
244280 tcp_rsk(req)->tfo_listener = true;
245281
246282 /* RFC1323: The window in SYN & SYN/ACK segments is never
....@@ -259,7 +295,7 @@
259295 refcount_set(&req->rsk_refcnt, 2);
260296
261297 /* Now finish processing the fastopen child socket. */
262
- tcp_init_transfer(child, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
298
+ tcp_init_transfer(child, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, skb);
263299
264300 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
265301
....@@ -276,6 +312,7 @@
276312 static bool tcp_fastopen_queue_check(struct sock *sk)
277313 {
278314 struct fastopen_queue *fastopenq;
315
+ int max_qlen;
279316
280317 /* Make sure the listener has enabled fastopen, and we don't
281318 * exceed the max # of pending TFO requests allowed before trying
....@@ -288,10 +325,11 @@
288325 * temporarily vs a server not supporting Fast Open at all.
289326 */
290327 fastopenq = &inet_csk(sk)->icsk_accept_queue.fastopenq;
291
- if (fastopenq->max_qlen == 0)
328
+ max_qlen = READ_ONCE(fastopenq->max_qlen);
329
+ if (max_qlen == 0)
292330 return false;
293331
294
- if (fastopenq->qlen >= fastopenq->max_qlen) {
332
+ if (fastopenq->qlen >= max_qlen) {
295333 struct request_sock *req1;
296334 spin_lock(&fastopenq->lock);
297335 req1 = fastopenq->rskq_rst_head;
....@@ -313,7 +351,7 @@
313351 const struct dst_entry *dst,
314352 int flag)
315353 {
316
- return (sock_net(sk)->ipv4.sysctl_tcp_fastopen & flag) ||
354
+ return (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) & flag) ||
317355 tcp_sk(sk)->fastopen_no_cookie ||
318356 (dst && dst_metric(dst, RTAX_FASTOPEN_NO_COOKIE));
319357 }
....@@ -328,9 +366,10 @@
328366 const struct dst_entry *dst)
329367 {
330368 bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
331
- int tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
369
+ int tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
332370 struct tcp_fastopen_cookie valid_foc = { .len = -1 };
333371 struct sock *child;
372
+ int ret = 0;
334373
335374 if (foc->len == 0) /* Client requests a cookie */
336375 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENCOOKIEREQD);
....@@ -345,31 +384,44 @@
345384 if (tcp_fastopen_no_cookie(sk, dst, TFO_SERVER_COOKIE_NOT_REQD))
346385 goto fastopen;
347386
348
- if (foc->len >= 0 && /* Client presents or requests a cookie */
349
- tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc) &&
350
- foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
351
- foc->len == valid_foc.len &&
352
- !memcmp(foc->val, valid_foc.val, foc->len)) {
353
- /* Cookie is valid. Create a (full) child socket to accept
354
- * the data in SYN before returning a SYN-ACK to ack the
355
- * data. If we fail to create the socket, fall back and
356
- * ack the ISN only but includes the same cookie.
357
- *
358
- * Note: Data-less SYN with valid cookie is allowed to send
359
- * data in SYN_RECV state.
360
- */
361
-fastopen:
362
- child = tcp_fastopen_create_child(sk, skb, req);
363
- if (child) {
364
- foc->len = -1;
387
+ if (foc->len == 0) {
388
+ /* Client requests a cookie. */
389
+ tcp_fastopen_cookie_gen(sk, req, skb, &valid_foc);
390
+ } else if (foc->len > 0) {
391
+ ret = tcp_fastopen_cookie_gen_check(sk, req, skb, foc,
392
+ &valid_foc);
393
+ if (!ret) {
365394 NET_INC_STATS(sock_net(sk),
366
- LINUX_MIB_TCPFASTOPENPASSIVE);
367
- return child;
395
+ LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
396
+ } else {
397
+ /* Cookie is valid. Create a (full) child socket to
398
+ * accept the data in SYN before returning a SYN-ACK to
399
+ * ack the data. If we fail to create the socket, fall
400
+ * back and ack the ISN only but includes the same
401
+ * cookie.
402
+ *
403
+ * Note: Data-less SYN with valid cookie is allowed to
404
+ * send data in SYN_RECV state.
405
+ */
406
+fastopen:
407
+ child = tcp_fastopen_create_child(sk, skb, req);
408
+ if (child) {
409
+ if (ret == 2) {
410
+ valid_foc.exp = foc->exp;
411
+ *foc = valid_foc;
412
+ NET_INC_STATS(sock_net(sk),
413
+ LINUX_MIB_TCPFASTOPENPASSIVEALTKEY);
414
+ } else {
415
+ foc->len = -1;
416
+ }
417
+ NET_INC_STATS(sock_net(sk),
418
+ LINUX_MIB_TCPFASTOPENPASSIVE);
419
+ return child;
420
+ }
421
+ NET_INC_STATS(sock_net(sk),
422
+ LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
368423 }
369
- NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
370
- } else if (foc->len > 0) /* Client presents an invalid cookie */
371
- NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
372
-
424
+ }
373425 valid_foc.exp = foc->exp;
374426 *foc = valid_foc;
375427 return NULL;
....@@ -394,7 +446,10 @@
394446 cookie->len = -1;
395447 return true;
396448 }
397
- return cookie->len > 0;
449
+ if (cookie->len > 0)
450
+ return true;
451
+ tcp_sk(sk)->fastopen_client_fail = TFO_COOKIE_UNAVAILABLE;
452
+ return false;
398453 }
399454
400455 /* This function checks if we want to defer sending SYN until the first
....@@ -453,6 +508,9 @@
453508 {
454509 struct net *net = sock_net(sk);
455510
511
+ if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout))
512
+ return;
513
+
456514 /* Paired with READ_ONCE() in tcp_fastopen_active_should_disable() */
457515 WRITE_ONCE(net->ipv4.tfo_active_disable_stamp, jiffies);
458516
....@@ -471,11 +529,16 @@
471529 */
472530 bool tcp_fastopen_active_should_disable(struct sock *sk)
473531 {
474
- unsigned int tfo_bh_timeout = sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout;
475
- int tfo_da_times = atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times);
532
+ unsigned int tfo_bh_timeout =
533
+ READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout);
476534 unsigned long timeout;
535
+ int tfo_da_times;
477536 int multiplier;
478537
538
+ if (!tfo_bh_timeout)
539
+ return false;
540
+
541
+ tfo_da_times = atomic_read(&sock_net(sk)->ipv4.tfo_active_disable_times);
479542 if (!tfo_da_times)
480543 return false;
481544