hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/net/rxrpc/af_rxrpc.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* AF_RXRPC implementation
23 *
34 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
45 * Written by David Howells (dhowells@redhat.com)
5
- *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU General Public License
8
- * as published by the Free Software Foundation; either version
9
- * 2 of the License, or (at your option) any later version.
106 */
117
128 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -97,7 +93,8 @@
9793 srx->transport_len > len)
9894 return -EINVAL;
9995
100
- if (srx->transport.family != rx->family)
96
+ if (srx->transport.family != rx->family &&
97
+ srx->transport.family == AF_INET && rx->family != AF_INET6)
10198 return -EAFNOSUPPORT;
10299
103100 switch (srx->transport.family) {
....@@ -134,7 +131,7 @@
134131 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
135132 struct rxrpc_local *local;
136133 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
137
- u16 service_id = srx->srx_service;
134
+ u16 service_id;
138135 int ret;
139136
140137 _enter("%p,%p,%d", rx, saddr, len);
....@@ -142,6 +139,7 @@
142139 ret = rxrpc_validate_address(rx, srx, len);
143140 if (ret < 0)
144141 goto error;
142
+ service_id = srx->srx_service;
145143
146144 lock_sock(&rx->sk);
147145
....@@ -248,7 +246,7 @@
248246 ret = 0;
249247 break;
250248 }
251
- /* Fall through */
249
+ fallthrough;
252250 default:
253251 ret = -EBUSY;
254252 break;
....@@ -269,6 +267,7 @@
269267 * @gfp: The allocation constraints
270268 * @notify_rx: Where to send notifications instead of socket queue
271269 * @upgrade: Request service upgrade for call
270
+ * @interruptibility: The call is interruptible, or can be canceled.
272271 * @debug_id: The debug ID for tracing to be assigned to the call
273272 *
274273 * Allow a kernel service to begin a call on the nominated socket. This just
....@@ -286,6 +285,7 @@
286285 gfp_t gfp,
287286 rxrpc_notify_rx_t notify_rx,
288287 bool upgrade,
288
+ enum rxrpc_interruptibility interruptibility,
289289 unsigned int debug_id)
290290 {
291291 struct rxrpc_conn_parameters cp;
....@@ -308,8 +308,10 @@
308308 key = NULL; /* a no-security key */
309309
310310 memset(&p, 0, sizeof(p));
311
- p.user_call_ID = user_call_ID;
312
- p.tx_total_len = tx_total_len;
311
+ p.user_call_ID = user_call_ID;
312
+ p.tx_total_len = tx_total_len;
313
+ p.interruptibility = interruptibility;
314
+ p.kernel = true;
313315
314316 memset(&cp, 0, sizeof(cp));
315317 cp.local = rx->local;
....@@ -349,7 +351,7 @@
349351 */
350352 void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
351353 {
352
- _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
354
+ _enter("%d{%d}", call->debug_id, refcount_read(&call->ref));
353355
354356 mutex_lock(&call->user_mutex);
355357 rxrpc_release_call(rxrpc_sk(sock->sk), call);
....@@ -371,89 +373,29 @@
371373 * @sock: The socket the call is on
372374 * @call: The call to check
373375 *
374
- * Allow a kernel service to find out whether a call is still alive - ie. we're
375
- * getting ACKs from the server. Returns a number representing the life state
376
- * which can be compared to that returned by a previous call.
377
- *
378
- * If this is a client call, ping ACKs will be sent to the server to find out
379
- * whether it's still responsive and whether the call is still alive on the
380
- * server.
376
+ * Allow a kernel service to find out whether a call is still alive -
377
+ * ie. whether it has completed.
381378 */
382
-u32 rxrpc_kernel_check_life(struct socket *sock, struct rxrpc_call *call)
379
+bool rxrpc_kernel_check_life(const struct socket *sock,
380
+ const struct rxrpc_call *call)
383381 {
384
- return call->acks_latest;
382
+ return call->state != RXRPC_CALL_COMPLETE;
385383 }
386384 EXPORT_SYMBOL(rxrpc_kernel_check_life);
387385
388386 /**
389
- * rxrpc_kernel_check_call - Check a call's state
387
+ * rxrpc_kernel_get_epoch - Retrieve the epoch value from a call.
390388 * @sock: The socket the call is on
391
- * @call: The call to check
392
- * @_compl: Where to store the completion state
393
- * @_abort_code: Where to store any abort code
389
+ * @call: The call to query
394390 *
395
- * Allow a kernel service to query the state of a call and find out the manner
396
- * of its termination if it has completed. Returns -EINPROGRESS if the call is
397
- * still going, 0 if the call finished successfully, -ECONNABORTED if the call
398
- * was aborted and an appropriate error if the call failed in some other way.
391
+ * Allow a kernel service to retrieve the epoch value from a service call to
392
+ * see if the client at the other end rebooted.
399393 */
400
-int rxrpc_kernel_check_call(struct socket *sock, struct rxrpc_call *call,
401
- enum rxrpc_call_completion *_compl, u32 *_abort_code)
394
+u32 rxrpc_kernel_get_epoch(struct socket *sock, struct rxrpc_call *call)
402395 {
403
- if (call->state != RXRPC_CALL_COMPLETE)
404
- return -EINPROGRESS;
405
- smp_rmb();
406
- *_compl = call->completion;
407
- *_abort_code = call->abort_code;
408
- return call->error;
396
+ return call->conn->proto.epoch;
409397 }
410
-EXPORT_SYMBOL(rxrpc_kernel_check_call);
411
-
412
-/**
413
- * rxrpc_kernel_retry_call - Allow a kernel service to retry a call
414
- * @sock: The socket the call is on
415
- * @call: The call to retry
416
- * @srx: The address of the peer to contact
417
- * @key: The security context to use (defaults to socket setting)
418
- *
419
- * Allow a kernel service to try resending a client call that failed due to a
420
- * network error to a new address. The Tx queue is maintained intact, thereby
421
- * relieving the need to re-encrypt any request data that has already been
422
- * buffered.
423
- */
424
-int rxrpc_kernel_retry_call(struct socket *sock, struct rxrpc_call *call,
425
- struct sockaddr_rxrpc *srx, struct key *key)
426
-{
427
- struct rxrpc_conn_parameters cp;
428
- struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
429
- int ret;
430
-
431
- _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
432
-
433
- if (!key)
434
- key = rx->key;
435
- if (key && !key->payload.data[0])
436
- key = NULL; /* a no-security key */
437
-
438
- memset(&cp, 0, sizeof(cp));
439
- cp.local = rx->local;
440
- cp.key = key;
441
- cp.security_level = 0;
442
- cp.exclusive = false;
443
- cp.service_id = srx->srx_service;
444
-
445
- mutex_lock(&call->user_mutex);
446
-
447
- ret = rxrpc_prepare_call_for_retry(rx, call);
448
- if (ret == 0)
449
- ret = rxrpc_retry_client_call(rx, call, &cp, srx, GFP_KERNEL);
450
-
451
- mutex_unlock(&call->user_mutex);
452
- rxrpc_put_peer(cp.peer);
453
- _leave(" = %d", ret);
454
- return ret;
455
-}
456
-EXPORT_SYMBOL(rxrpc_kernel_retry_call);
398
+EXPORT_SYMBOL(rxrpc_kernel_get_epoch);
457399
458400 /**
459401 * rxrpc_kernel_new_call_notification - Get notifications of new calls
....@@ -474,6 +416,31 @@
474416 rx->discard_new_call = discard_new_call;
475417 }
476418 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
419
+
420
+/**
421
+ * rxrpc_kernel_set_max_life - Set maximum lifespan on a call
422
+ * @sock: The socket the call is on
423
+ * @call: The call to configure
424
+ * @hard_timeout: The maximum lifespan of the call in jiffies
425
+ *
426
+ * Set the maximum lifespan of a call. The call will end with ETIME or
427
+ * ETIMEDOUT if it takes longer than this.
428
+ */
429
+void rxrpc_kernel_set_max_life(struct socket *sock, struct rxrpc_call *call,
430
+ unsigned long hard_timeout)
431
+{
432
+ unsigned long now;
433
+
434
+ mutex_lock(&call->user_mutex);
435
+
436
+ now = jiffies;
437
+ hard_timeout += now;
438
+ WRITE_ONCE(call->expect_term_by, hard_timeout);
439
+ rxrpc_reduce_call_timer(call, hard_timeout, now, rxrpc_timer_set_for_hard);
440
+
441
+ mutex_unlock(&call->user_mutex);
442
+}
443
+EXPORT_SYMBOL(rxrpc_kernel_set_max_life);
477444
478445 /*
479446 * connect an RxRPC socket
....@@ -579,7 +546,7 @@
579546
580547 rx->local = local;
581548 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
582
- /* Fall through */
549
+ fallthrough;
583550
584551 case RXRPC_CLIENT_BOUND:
585552 if (!m->msg_name &&
....@@ -587,7 +554,7 @@
587554 m->msg_name = &rx->connect_srx;
588555 m->msg_namelen = sizeof(rx->connect_srx);
589556 }
590
- /* Fall through */
557
+ fallthrough;
591558 case RXRPC_SERVER_BOUND:
592559 case RXRPC_SERVER_LISTENING:
593560 ret = rxrpc_do_sendmsg(rx, m, len);
....@@ -605,11 +572,24 @@
605572 return ret;
606573 }
607574
575
+int rxrpc_sock_set_min_security_level(struct sock *sk, unsigned int val)
576
+{
577
+ if (sk->sk_state != RXRPC_UNBOUND)
578
+ return -EISCONN;
579
+ if (val > RXRPC_SECURITY_MAX)
580
+ return -EINVAL;
581
+ lock_sock(sk);
582
+ rxrpc_sk(sk)->min_sec_level = val;
583
+ release_sock(sk);
584
+ return 0;
585
+}
586
+EXPORT_SYMBOL(rxrpc_sock_set_min_security_level);
587
+
608588 /*
609589 * set RxRPC socket options
610590 */
611591 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
612
- char __user *optval, unsigned int optlen)
592
+ sockptr_t optval, unsigned int optlen)
613593 {
614594 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
615595 unsigned int min_sec_level;
....@@ -660,8 +640,8 @@
660640 ret = -EISCONN;
661641 if (rx->sk.sk_state != RXRPC_UNBOUND)
662642 goto error;
663
- ret = get_user(min_sec_level,
664
- (unsigned int __user *) optval);
643
+ ret = copy_from_sockptr(&min_sec_level, optval,
644
+ sizeof(unsigned int));
665645 if (ret < 0)
666646 goto error;
667647 ret = -EINVAL;
....@@ -679,7 +659,7 @@
679659 if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
680660 goto error;
681661 ret = -EFAULT;
682
- if (copy_from_user(service_upgrade, optval,
662
+ if (copy_from_sockptr(service_upgrade, optval,
683663 sizeof(service_upgrade)) != 0)
684664 goto error;
685665 ret = -EINVAL;
....@@ -981,7 +961,7 @@
981961 int ret = -1;
982962 unsigned int tmp;
983963
984
- BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
964
+ BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof_field(struct sk_buff, cb));
985965
986966 get_random_bytes(&tmp, sizeof(tmp));
987967 tmp &= 0x3fffffff;