call_accept.c 14 KB

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