call_accept.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* incoming call handling
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/net.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/errqueue.h>
  12. #include <linux/udp.h>
  13. #include <linux/in.h>
  14. #include <linux/in6.h>
  15. #include <linux/icmp.h>
  16. #include <linux/gfp.h>
  17. #include <linux/circ_buf.h>
  18. #include <net/sock.h>
  19. #include <net/af_rxrpc.h>
  20. #include <net/ip.h>
  21. #include "ar-internal.h"
  22. static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
  23. unsigned long user_call_ID)
  24. {
  25. }
  26. /*
  27. * Preallocate a single service call, connection and peer and, if possible,
  28. * give them a user ID and attach the user's side of the ID to them.
  29. */
  30. static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
  31. struct rxrpc_backlog *b,
  32. rxrpc_notify_rx_t notify_rx,
  33. unsigned long user_call_ID, gfp_t gfp,
  34. unsigned int debug_id)
  35. {
  36. struct rxrpc_call *call, *xcall;
  37. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  38. struct rb_node *parent, **pp;
  39. int max, tmp;
  40. unsigned int size = RXRPC_BACKLOG_MAX;
  41. unsigned int head, tail, call_head, call_tail;
  42. max = rx->sk.sk_max_ack_backlog;
  43. tmp = rx->sk.sk_ack_backlog;
  44. if (tmp >= max) {
  45. _leave(" = -ENOBUFS [full %u]", max);
  46. return -ENOBUFS;
  47. }
  48. max -= tmp;
  49. /* We don't need more conns and peers than we have calls, but on the
  50. * other hand, we shouldn't ever use more peers than conns or conns
  51. * than calls.
  52. */
  53. call_head = b->call_backlog_head;
  54. call_tail = READ_ONCE(b->call_backlog_tail);
  55. tmp = CIRC_CNT(call_head, call_tail, size);
  56. if (tmp >= max) {
  57. _leave(" = -ENOBUFS [enough %u]", tmp);
  58. return -ENOBUFS;
  59. }
  60. max = tmp + 1;
  61. head = b->peer_backlog_head;
  62. tail = READ_ONCE(b->peer_backlog_tail);
  63. if (CIRC_CNT(head, tail, size) < max) {
  64. struct rxrpc_peer *peer;
  65. peer = rxrpc_alloc_peer(rx->local, gfp, rxrpc_peer_new_prealloc);
  66. if (!peer)
  67. return -ENOMEM;
  68. b->peer_backlog[head] = peer;
  69. smp_store_release(&b->peer_backlog_head,
  70. (head + 1) & (size - 1));
  71. }
  72. head = b->conn_backlog_head;
  73. tail = READ_ONCE(b->conn_backlog_tail);
  74. if (CIRC_CNT(head, tail, size) < max) {
  75. struct rxrpc_connection *conn;
  76. conn = rxrpc_prealloc_service_connection(rxnet, gfp);
  77. if (!conn)
  78. return -ENOMEM;
  79. b->conn_backlog[head] = conn;
  80. smp_store_release(&b->conn_backlog_head,
  81. (head + 1) & (size - 1));
  82. }
  83. /* Now it gets complicated, because calls get registered with the
  84. * socket here, with a user ID preassigned by the user.
  85. */
  86. call = rxrpc_alloc_call(rx, gfp, debug_id);
  87. if (!call)
  88. return -ENOMEM;
  89. call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
  90. rxrpc_set_call_state(call, RXRPC_CALL_SERVER_PREALLOC);
  91. __set_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events);
  92. trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
  93. user_call_ID, rxrpc_call_new_prealloc_service);
  94. write_lock(&rx->call_lock);
  95. /* Check the user ID isn't already in use */
  96. pp = &rx->calls.rb_node;
  97. parent = NULL;
  98. while (*pp) {
  99. parent = *pp;
  100. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  101. if (user_call_ID < xcall->user_call_ID)
  102. pp = &(*pp)->rb_left;
  103. else if (user_call_ID > xcall->user_call_ID)
  104. pp = &(*pp)->rb_right;
  105. else
  106. goto id_in_use;
  107. }
  108. call->user_call_ID = user_call_ID;
  109. call->notify_rx = notify_rx;
  110. if (rx->app_ops &&
  111. rx->app_ops->user_attach_call) {
  112. rxrpc_get_call(call, rxrpc_call_get_kernel_service);
  113. rx->app_ops->user_attach_call(call, user_call_ID);
  114. }
  115. rxrpc_get_call(call, rxrpc_call_get_userid);
  116. rb_link_node(&call->sock_node, parent, pp);
  117. rb_insert_color(&call->sock_node, &rx->calls);
  118. set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  119. list_add(&call->sock_link, &rx->sock_calls);
  120. write_unlock(&rx->call_lock);
  121. rxnet = call->rxnet;
  122. spin_lock(&rxnet->call_lock);
  123. list_add_tail_rcu(&call->link, &rxnet->calls);
  124. spin_unlock(&rxnet->call_lock);
  125. b->call_backlog[call_head] = call;
  126. smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
  127. _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
  128. return 0;
  129. id_in_use:
  130. write_unlock(&rx->call_lock);
  131. rxrpc_prefail_call(call, RXRPC_CALL_LOCAL_ERROR, -EBADSLT);
  132. rxrpc_cleanup_call(call);
  133. _leave(" = -EBADSLT");
  134. return -EBADSLT;
  135. }
  136. /*
  137. * Allocate the preallocation buffers for incoming service calls. These must
  138. * be charged manually.
  139. */
  140. int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
  141. {
  142. struct rxrpc_backlog *b = rx->backlog;
  143. if (!b) {
  144. b = kzalloc_obj(struct rxrpc_backlog, gfp);
  145. if (!b)
  146. return -ENOMEM;
  147. rx->backlog = b;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Discard the preallocation on a service.
  153. */
  154. void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
  155. {
  156. struct rxrpc_backlog *b = rx->backlog;
  157. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  158. unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
  159. if (!b)
  160. return;
  161. rx->backlog = NULL;
  162. /* Make sure that there aren't any incoming calls in progress before we
  163. * clear the preallocation buffers.
  164. */
  165. spin_lock_irq(&rx->incoming_lock);
  166. spin_unlock_irq(&rx->incoming_lock);
  167. head = b->peer_backlog_head;
  168. tail = b->peer_backlog_tail;
  169. while (CIRC_CNT(head, tail, size) > 0) {
  170. struct rxrpc_peer *peer = b->peer_backlog[tail];
  171. rxrpc_put_local(peer->local, rxrpc_local_put_prealloc_peer);
  172. kfree(peer);
  173. tail = (tail + 1) & (size - 1);
  174. }
  175. head = b->conn_backlog_head;
  176. tail = b->conn_backlog_tail;
  177. while (CIRC_CNT(head, tail, size) > 0) {
  178. struct rxrpc_connection *conn = b->conn_backlog[tail];
  179. write_lock(&rxnet->conn_lock);
  180. list_del(&conn->link);
  181. list_del(&conn->proc_link);
  182. write_unlock(&rxnet->conn_lock);
  183. kfree(conn);
  184. if (atomic_dec_and_test(&rxnet->nr_conns))
  185. wake_up_var(&rxnet->nr_conns);
  186. tail = (tail + 1) & (size - 1);
  187. }
  188. head = b->call_backlog_head;
  189. tail = b->call_backlog_tail;
  190. while (CIRC_CNT(head, tail, size) > 0) {
  191. struct rxrpc_call *call = b->call_backlog[tail];
  192. rxrpc_see_call(call, rxrpc_call_see_discard);
  193. rcu_assign_pointer(call->socket, rx);
  194. if (rx->app_ops &&
  195. rx->app_ops->discard_new_call) {
  196. _debug("discard %lx", call->user_call_ID);
  197. rx->app_ops->discard_new_call(call, call->user_call_ID);
  198. if (call->notify_rx)
  199. call->notify_rx = rxrpc_dummy_notify;
  200. rxrpc_put_call(call, rxrpc_call_put_kernel);
  201. }
  202. rxrpc_call_completed(call);
  203. rxrpc_release_call(rx, call);
  204. rxrpc_put_call(call, rxrpc_call_put_discard_prealloc);
  205. tail = (tail + 1) & (size - 1);
  206. }
  207. kfree(b);
  208. }
  209. /*
  210. * Allocate a new incoming call from the prealloc pool, along with a connection
  211. * and a peer as necessary.
  212. */
  213. static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
  214. struct rxrpc_local *local,
  215. struct rxrpc_peer *peer,
  216. struct rxrpc_connection *conn,
  217. const struct rxrpc_security *sec,
  218. struct sockaddr_rxrpc *peer_srx,
  219. struct sk_buff *skb)
  220. {
  221. struct rxrpc_backlog *b = rx->backlog;
  222. struct rxrpc_call *call;
  223. unsigned short call_head, conn_head, peer_head;
  224. unsigned short call_tail, conn_tail, peer_tail;
  225. unsigned short call_count, conn_count;
  226. if (!b)
  227. return NULL;
  228. /* #calls >= #conns >= #peers must hold true. */
  229. call_head = smp_load_acquire(&b->call_backlog_head);
  230. call_tail = b->call_backlog_tail;
  231. call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
  232. conn_head = smp_load_acquire(&b->conn_backlog_head);
  233. conn_tail = b->conn_backlog_tail;
  234. conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
  235. ASSERTCMP(conn_count, >=, call_count);
  236. peer_head = smp_load_acquire(&b->peer_backlog_head);
  237. peer_tail = b->peer_backlog_tail;
  238. ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
  239. conn_count);
  240. if (call_count == 0)
  241. return NULL;
  242. if (!conn) {
  243. if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_service_conn))
  244. peer = NULL;
  245. if (!peer) {
  246. peer = b->peer_backlog[peer_tail];
  247. peer->srx = *peer_srx;
  248. b->peer_backlog[peer_tail] = NULL;
  249. smp_store_release(&b->peer_backlog_tail,
  250. (peer_tail + 1) &
  251. (RXRPC_BACKLOG_MAX - 1));
  252. rxrpc_new_incoming_peer(local, peer);
  253. }
  254. /* Now allocate and set up the connection */
  255. conn = b->conn_backlog[conn_tail];
  256. b->conn_backlog[conn_tail] = NULL;
  257. smp_store_release(&b->conn_backlog_tail,
  258. (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  259. conn->local = rxrpc_get_local(local, rxrpc_local_get_prealloc_conn);
  260. conn->peer = peer;
  261. rxrpc_see_connection(conn, rxrpc_conn_see_new_service_conn);
  262. rxrpc_new_incoming_connection(rx, conn, sec, skb);
  263. } else {
  264. rxrpc_get_connection(conn, rxrpc_conn_get_service_conn);
  265. atomic_inc(&conn->active);
  266. }
  267. /* And now we can allocate and set up a new call */
  268. call = b->call_backlog[call_tail];
  269. b->call_backlog[call_tail] = NULL;
  270. smp_store_release(&b->call_backlog_tail,
  271. (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  272. rxrpc_see_call(call, rxrpc_call_see_accept);
  273. call->local = rxrpc_get_local(conn->local, rxrpc_local_get_call);
  274. call->conn = conn;
  275. call->security = conn->security;
  276. call->security_ix = conn->security_ix;
  277. call->peer = rxrpc_get_peer(conn->peer, rxrpc_peer_get_accept);
  278. call->dest_srx = peer->srx;
  279. call->cong_ssthresh = call->peer->cong_ssthresh;
  280. call->tx_last_sent = ktime_get_real();
  281. return call;
  282. }
  283. /*
  284. * Set up a new incoming call. Called from the I/O thread.
  285. *
  286. * If this is for a kernel service, when we allocate the call, it will have
  287. * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
  288. * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
  289. * services only have the ref from the backlog buffer.
  290. *
  291. * If we want to report an error, we mark the skb with the packet type and
  292. * abort code and return false.
  293. */
  294. bool rxrpc_new_incoming_call(struct rxrpc_local *local,
  295. struct rxrpc_peer *peer,
  296. struct rxrpc_connection *conn,
  297. struct sockaddr_rxrpc *peer_srx,
  298. struct sk_buff *skb)
  299. {
  300. const struct rxrpc_security *sec = NULL;
  301. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  302. struct rxrpc_call *call = NULL;
  303. struct rxrpc_sock *rx;
  304. _enter("");
  305. /* Don't set up a call for anything other than a DATA packet. */
  306. if (sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  307. return rxrpc_protocol_error(skb, rxrpc_eproto_no_service_call);
  308. read_lock_irq(&local->services_lock);
  309. /* Weed out packets to services we're not offering. Packets that would
  310. * begin a call are explicitly rejected and the rest are just
  311. * discarded.
  312. */
  313. rx = local->service;
  314. if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
  315. sp->hdr.serviceId != rx->second_service)
  316. ) {
  317. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  318. sp->hdr.seq == 1)
  319. goto unsupported_service;
  320. goto discard;
  321. }
  322. if (!conn) {
  323. sec = rxrpc_get_incoming_security(rx, skb);
  324. if (!sec)
  325. goto unsupported_security;
  326. }
  327. spin_lock(&rx->incoming_lock);
  328. if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
  329. rx->sk.sk_state == RXRPC_CLOSE) {
  330. rxrpc_direct_conn_abort(skb, rxrpc_abort_shut_down,
  331. RX_INVALID_OPERATION, -ESHUTDOWN);
  332. goto no_call;
  333. }
  334. call = rxrpc_alloc_incoming_call(rx, local, peer, conn, sec, peer_srx,
  335. skb);
  336. if (!call) {
  337. skb->mark = RXRPC_SKB_MARK_REJECT_BUSY;
  338. goto no_call;
  339. }
  340. trace_rxrpc_receive(call, rxrpc_receive_incoming,
  341. sp->hdr.serial, sp->hdr.seq);
  342. /* Make the call live. */
  343. rxrpc_incoming_call(rx, call, skb);
  344. conn = call->conn;
  345. if (rx->app_ops &&
  346. rx->app_ops->notify_new_call)
  347. rx->app_ops->notify_new_call(&rx->sk, call, call->user_call_ID);
  348. spin_lock(&conn->state_lock);
  349. if (conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
  350. conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
  351. set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
  352. rxrpc_queue_conn(call->conn, rxrpc_conn_queue_challenge);
  353. }
  354. spin_unlock(&conn->state_lock);
  355. spin_unlock(&rx->incoming_lock);
  356. read_unlock_irq(&local->services_lock);
  357. rxrpc_assess_MTU_size(local, call->peer);
  358. if (hlist_unhashed(&call->error_link)) {
  359. spin_lock_irq(&call->peer->lock);
  360. hlist_add_head(&call->error_link, &call->peer->error_targets);
  361. spin_unlock_irq(&call->peer->lock);
  362. }
  363. _leave(" = %p{%d}", call, call->debug_id);
  364. rxrpc_queue_rx_call_packet(call, skb);
  365. rxrpc_put_call(call, rxrpc_call_put_input);
  366. return true;
  367. unsupported_service:
  368. read_unlock_irq(&local->services_lock);
  369. return rxrpc_direct_conn_abort(skb, rxrpc_abort_service_not_offered,
  370. RX_INVALID_OPERATION, -EOPNOTSUPP);
  371. unsupported_security:
  372. read_unlock_irq(&local->services_lock);
  373. return rxrpc_direct_conn_abort(skb, rxrpc_abort_service_not_offered,
  374. RX_INVALID_OPERATION, -EKEYREJECTED);
  375. no_call:
  376. spin_unlock(&rx->incoming_lock);
  377. read_unlock_irq(&local->services_lock);
  378. _leave(" = f [%u]", skb->mark);
  379. return false;
  380. discard:
  381. read_unlock_irq(&local->services_lock);
  382. return true;
  383. }
  384. /*
  385. * Charge up socket with preallocated calls, attaching user call IDs.
  386. */
  387. int rxrpc_user_charge_accept(struct rxrpc_sock *rx, unsigned long user_call_ID)
  388. {
  389. struct rxrpc_backlog *b = rx->backlog;
  390. if (rx->sk.sk_state == RXRPC_CLOSE)
  391. return -ESHUTDOWN;
  392. return rxrpc_service_prealloc_one(rx, b, NULL, user_call_ID, GFP_KERNEL,
  393. atomic_inc_return(&rxrpc_debug_id));
  394. }
  395. /*
  396. * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
  397. * @sock: The socket on which to preallocate
  398. * @notify_rx: Event notification function for the call
  399. * @user_call_ID: The tag to attach to the preallocated call
  400. * @gfp: The allocation conditions.
  401. * @debug_id: The tracing debug ID.
  402. *
  403. * Charge up the socket with preallocated calls, each with a user ID. The
  404. * ->user_attach_call() callback function should be provided to effect the
  405. * attachment from the user's side. The user is given a ref to hold on the
  406. * call.
  407. *
  408. * Note that the call may be come connected before this function returns.
  409. */
  410. int rxrpc_kernel_charge_accept(struct socket *sock, rxrpc_notify_rx_t notify_rx,
  411. unsigned long user_call_ID, gfp_t gfp,
  412. unsigned int debug_id)
  413. {
  414. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  415. struct rxrpc_backlog *b = rx->backlog;
  416. if (sock->sk->sk_state == RXRPC_CLOSE)
  417. return -ESHUTDOWN;
  418. return rxrpc_service_prealloc_one(rx, b, notify_rx, user_call_ID,
  419. gfp, debug_id);
  420. }
  421. EXPORT_SYMBOL(rxrpc_kernel_charge_accept);