rxrpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /* Maintain an RxRPC server socket to do AFS communications through
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * 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.
  10. */
  11. #include <net/sock.h>
  12. #include <net/af_rxrpc.h>
  13. #include <rxrpc/packet.h>
  14. #include "internal.h"
  15. #include "afs_cm.h"
  16. static struct socket *afs_socket; /* my RxRPC socket */
  17. static struct workqueue_struct *afs_async_calls;
  18. static void afs_wake_up_call_waiter(struct afs_call *);
  19. static int afs_wait_for_call_to_complete(struct afs_call *);
  20. static void afs_wake_up_async_call(struct afs_call *);
  21. static int afs_dont_wait_for_call_to_complete(struct afs_call *);
  22. static void afs_process_async_call(struct work_struct *);
  23. static void afs_rx_interceptor(struct sock *, unsigned long, struct sk_buff *);
  24. static int afs_deliver_cm_op_id(struct afs_call *, struct sk_buff *, bool);
  25. /* synchronous call management */
  26. const struct afs_wait_mode afs_sync_call = {
  27. .rx_wakeup = afs_wake_up_call_waiter,
  28. .wait = afs_wait_for_call_to_complete,
  29. };
  30. /* asynchronous call management */
  31. const struct afs_wait_mode afs_async_call = {
  32. .rx_wakeup = afs_wake_up_async_call,
  33. .wait = afs_dont_wait_for_call_to_complete,
  34. };
  35. /* asynchronous incoming call management */
  36. static const struct afs_wait_mode afs_async_incoming_call = {
  37. .rx_wakeup = afs_wake_up_async_call,
  38. };
  39. /* asynchronous incoming call initial processing */
  40. static const struct afs_call_type afs_RXCMxxxx = {
  41. .deliver = afs_deliver_cm_op_id,
  42. .abort_to_error = afs_abort_to_error,
  43. };
  44. static void afs_collect_incoming_call(struct work_struct *);
  45. static struct sk_buff_head afs_incoming_calls;
  46. static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
  47. /*
  48. * open an RxRPC socket and bind it to be a server for callback notifications
  49. * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
  50. */
  51. int afs_open_socket(void)
  52. {
  53. struct sockaddr_rxrpc srx;
  54. struct socket *socket;
  55. int ret;
  56. _enter("");
  57. skb_queue_head_init(&afs_incoming_calls);
  58. afs_async_calls = create_singlethread_workqueue("kafsd");
  59. if (!afs_async_calls) {
  60. _leave(" = -ENOMEM [wq]");
  61. return -ENOMEM;
  62. }
  63. ret = sock_create_kern(AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
  64. if (ret < 0) {
  65. destroy_workqueue(afs_async_calls);
  66. _leave(" = %d [socket]", ret);
  67. return ret;
  68. }
  69. socket->sk->sk_allocation = GFP_NOFS;
  70. /* bind the callback manager's address to make this a server socket */
  71. srx.srx_family = AF_RXRPC;
  72. srx.srx_service = CM_SERVICE;
  73. srx.transport_type = SOCK_DGRAM;
  74. srx.transport_len = sizeof(srx.transport.sin);
  75. srx.transport.sin.sin_family = AF_INET;
  76. srx.transport.sin.sin_port = htons(AFS_CM_PORT);
  77. memset(&srx.transport.sin.sin_addr, 0,
  78. sizeof(srx.transport.sin.sin_addr));
  79. ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
  80. if (ret < 0) {
  81. sock_release(socket);
  82. _leave(" = %d [bind]", ret);
  83. return ret;
  84. }
  85. rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
  86. afs_socket = socket;
  87. _leave(" = 0");
  88. return 0;
  89. }
  90. /*
  91. * close the RxRPC socket AFS was using
  92. */
  93. void afs_close_socket(void)
  94. {
  95. _enter("");
  96. sock_release(afs_socket);
  97. _debug("dework");
  98. destroy_workqueue(afs_async_calls);
  99. _leave("");
  100. }
  101. /*
  102. * allocate a call with flat request and reply buffers
  103. */
  104. struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
  105. size_t request_size, size_t reply_size)
  106. {
  107. struct afs_call *call;
  108. call = kzalloc(sizeof(*call), GFP_NOFS);
  109. if (!call)
  110. goto nomem_call;
  111. if (request_size) {
  112. call->request = kmalloc(request_size, GFP_NOFS);
  113. if (!call->request)
  114. goto nomem_request;
  115. }
  116. if (reply_size) {
  117. call->buffer = kmalloc(reply_size, GFP_NOFS);
  118. if (!call->buffer)
  119. goto nomem_buffer;
  120. }
  121. call->type = type;
  122. call->request_size = request_size;
  123. call->reply_max = reply_size;
  124. init_waitqueue_head(&call->waitq);
  125. skb_queue_head_init(&call->rx_queue);
  126. return call;
  127. nomem_buffer:
  128. kfree(call->request);
  129. nomem_request:
  130. kfree(call);
  131. nomem_call:
  132. return NULL;
  133. }
  134. /*
  135. * clean up a call with flat buffer
  136. */
  137. void afs_flat_call_destructor(struct afs_call *call)
  138. {
  139. _enter("");
  140. kfree(call->request);
  141. call->request = NULL;
  142. kfree(call->buffer);
  143. call->buffer = NULL;
  144. }
  145. /*
  146. * initiate a call
  147. */
  148. int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
  149. const struct afs_wait_mode *wait_mode)
  150. {
  151. struct sockaddr_rxrpc srx;
  152. struct rxrpc_call *rxcall;
  153. struct msghdr msg;
  154. struct kvec iov[1];
  155. int ret;
  156. _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
  157. call->wait_mode = wait_mode;
  158. INIT_WORK(&call->async_work, afs_process_async_call);
  159. memset(&srx, 0, sizeof(srx));
  160. srx.srx_family = AF_RXRPC;
  161. srx.srx_service = call->service_id;
  162. srx.transport_type = SOCK_DGRAM;
  163. srx.transport_len = sizeof(srx.transport.sin);
  164. srx.transport.sin.sin_family = AF_INET;
  165. srx.transport.sin.sin_port = call->port;
  166. memcpy(&srx.transport.sin.sin_addr, addr, 4);
  167. /* create a call */
  168. rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
  169. (unsigned long) call, gfp);
  170. if (IS_ERR(rxcall)) {
  171. ret = PTR_ERR(rxcall);
  172. goto error_kill_call;
  173. }
  174. call->rxcall = rxcall;
  175. /* send the request */
  176. iov[0].iov_base = call->request;
  177. iov[0].iov_len = call->request_size;
  178. msg.msg_name = NULL;
  179. msg.msg_namelen = 0;
  180. msg.msg_iov = (struct iovec *) iov;
  181. msg.msg_iovlen = 1;
  182. msg.msg_control = NULL;
  183. msg.msg_controllen = 0;
  184. msg.msg_flags = 0;
  185. /* have to change the state *before* sending the last packet as RxRPC
  186. * might give us the reply before it returns from sending the
  187. * request */
  188. call->state = AFS_CALL_AWAIT_REPLY;
  189. ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
  190. if (ret < 0)
  191. goto error_do_abort;
  192. /* at this point, an async call may no longer exist as it may have
  193. * already completed */
  194. return wait_mode->wait(call);
  195. error_do_abort:
  196. rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
  197. rxrpc_kernel_end_call(rxcall);
  198. error_kill_call:
  199. call->type->destructor(call);
  200. ASSERT(skb_queue_empty(&call->rx_queue));
  201. kfree(call);
  202. _leave(" = %d", ret);
  203. return ret;
  204. }
  205. /*
  206. * handles intercepted messages that were arriving in the socket's Rx queue
  207. * - called with the socket receive queue lock held to ensure message ordering
  208. * - called with softirqs disabled
  209. */
  210. static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
  211. struct sk_buff *skb)
  212. {
  213. struct afs_call *call = (struct afs_call *) user_call_ID;
  214. _enter("%p,,%u", call, skb->mark);
  215. ASSERTCMP(sk, ==, afs_socket->sk);
  216. if (!call) {
  217. /* its an incoming call for our callback service */
  218. __skb_queue_tail(&afs_incoming_calls, skb);
  219. schedule_work(&afs_collect_incoming_call_work);
  220. } else {
  221. /* route the messages directly to the appropriate call */
  222. __skb_queue_tail(&call->rx_queue, skb);
  223. call->wait_mode->rx_wakeup(call);
  224. }
  225. _leave("");
  226. }
  227. /*
  228. * deliver messages to a call
  229. */
  230. static void afs_deliver_to_call(struct afs_call *call)
  231. {
  232. struct sk_buff *skb;
  233. bool last;
  234. u32 abort_code;
  235. int ret;
  236. _enter("");
  237. while ((call->state == AFS_CALL_AWAIT_REPLY ||
  238. call->state == AFS_CALL_AWAIT_OP_ID ||
  239. call->state == AFS_CALL_AWAIT_REQUEST ||
  240. call->state == AFS_CALL_AWAIT_ACK) &&
  241. (skb = skb_dequeue(&call->rx_queue))) {
  242. switch (skb->mark) {
  243. case RXRPC_SKB_MARK_DATA:
  244. _debug("Rcv DATA");
  245. last = rxrpc_kernel_is_data_last(skb);
  246. ret = call->type->deliver(call, skb, last);
  247. switch (ret) {
  248. case 0:
  249. if (last &&
  250. call->state == AFS_CALL_AWAIT_REPLY)
  251. call->state = AFS_CALL_COMPLETE;
  252. break;
  253. case -ENOTCONN:
  254. abort_code = RX_CALL_DEAD;
  255. goto do_abort;
  256. case -ENOTSUPP:
  257. abort_code = RX_INVALID_OPERATION;
  258. goto do_abort;
  259. default:
  260. abort_code = RXGEN_CC_UNMARSHAL;
  261. if (call->state != AFS_CALL_AWAIT_REPLY)
  262. abort_code = RXGEN_SS_UNMARSHAL;
  263. do_abort:
  264. rxrpc_kernel_abort_call(call->rxcall,
  265. abort_code);
  266. call->error = ret;
  267. call->state = AFS_CALL_ERROR;
  268. break;
  269. }
  270. rxrpc_kernel_data_delivered(skb);
  271. skb = NULL;
  272. break;
  273. case RXRPC_SKB_MARK_FINAL_ACK:
  274. _debug("Rcv ACK");
  275. call->state = AFS_CALL_COMPLETE;
  276. break;
  277. case RXRPC_SKB_MARK_BUSY:
  278. _debug("Rcv BUSY");
  279. call->error = -EBUSY;
  280. call->state = AFS_CALL_BUSY;
  281. break;
  282. case RXRPC_SKB_MARK_REMOTE_ABORT:
  283. abort_code = rxrpc_kernel_get_abort_code(skb);
  284. call->error = call->type->abort_to_error(abort_code);
  285. call->state = AFS_CALL_ABORTED;
  286. _debug("Rcv ABORT %u -> %d", abort_code, call->error);
  287. break;
  288. case RXRPC_SKB_MARK_NET_ERROR:
  289. call->error = -rxrpc_kernel_get_error_number(skb);
  290. call->state = AFS_CALL_ERROR;
  291. _debug("Rcv NET ERROR %d", call->error);
  292. break;
  293. case RXRPC_SKB_MARK_LOCAL_ERROR:
  294. call->error = -rxrpc_kernel_get_error_number(skb);
  295. call->state = AFS_CALL_ERROR;
  296. _debug("Rcv LOCAL ERROR %d", call->error);
  297. break;
  298. default:
  299. BUG();
  300. break;
  301. }
  302. rxrpc_kernel_free_skb(skb);
  303. }
  304. /* make sure the queue is empty if the call is done with (we might have
  305. * aborted the call early because of an unmarshalling error) */
  306. if (call->state >= AFS_CALL_COMPLETE) {
  307. while ((skb = skb_dequeue(&call->rx_queue)))
  308. rxrpc_kernel_free_skb(skb);
  309. if (call->incoming) {
  310. rxrpc_kernel_end_call(call->rxcall);
  311. call->type->destructor(call);
  312. ASSERT(skb_queue_empty(&call->rx_queue));
  313. kfree(call);
  314. }
  315. }
  316. _leave("");
  317. }
  318. /*
  319. * wait synchronously for a call to complete
  320. */
  321. static int afs_wait_for_call_to_complete(struct afs_call *call)
  322. {
  323. struct sk_buff *skb;
  324. int ret;
  325. DECLARE_WAITQUEUE(myself, current);
  326. _enter("");
  327. add_wait_queue(&call->waitq, &myself);
  328. for (;;) {
  329. set_current_state(TASK_INTERRUPTIBLE);
  330. /* deliver any messages that are in the queue */
  331. if (!skb_queue_empty(&call->rx_queue)) {
  332. __set_current_state(TASK_RUNNING);
  333. afs_deliver_to_call(call);
  334. continue;
  335. }
  336. ret = call->error;
  337. if (call->state >= AFS_CALL_COMPLETE)
  338. break;
  339. ret = -EINTR;
  340. if (signal_pending(current))
  341. break;
  342. schedule();
  343. }
  344. remove_wait_queue(&call->waitq, &myself);
  345. __set_current_state(TASK_RUNNING);
  346. /* kill the call */
  347. if (call->state < AFS_CALL_COMPLETE) {
  348. _debug("call incomplete");
  349. rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
  350. while ((skb = skb_dequeue(&call->rx_queue)))
  351. rxrpc_kernel_free_skb(skb);
  352. }
  353. _debug("call complete");
  354. rxrpc_kernel_end_call(call->rxcall);
  355. call->type->destructor(call);
  356. ASSERT(skb_queue_empty(&call->rx_queue));
  357. kfree(call);
  358. _leave(" = %d", ret);
  359. return ret;
  360. }
  361. /*
  362. * wake up a waiting call
  363. */
  364. static void afs_wake_up_call_waiter(struct afs_call *call)
  365. {
  366. wake_up(&call->waitq);
  367. }
  368. /*
  369. * wake up an asynchronous call
  370. */
  371. static void afs_wake_up_async_call(struct afs_call *call)
  372. {
  373. _enter("");
  374. queue_work(afs_async_calls, &call->async_work);
  375. }
  376. /*
  377. * put a call into asynchronous mode
  378. * - mustn't touch the call descriptor as the call my have completed by the
  379. * time we get here
  380. */
  381. static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
  382. {
  383. _enter("");
  384. return -EINPROGRESS;
  385. }
  386. /*
  387. * delete an asynchronous call
  388. */
  389. static void afs_delete_async_call(struct work_struct *work)
  390. {
  391. struct afs_call *call =
  392. container_of(work, struct afs_call, async_work);
  393. _enter("");
  394. ASSERT(skb_queue_empty(&call->rx_queue));
  395. ASSERT(!work_pending(&call->async_work));
  396. kfree(call);
  397. _leave("");
  398. }
  399. /*
  400. * perform processing on an asynchronous call
  401. * - on a multiple-thread workqueue this work item may try to run on several
  402. * CPUs at the same time
  403. */
  404. static void afs_process_async_call(struct work_struct *work)
  405. {
  406. struct afs_call *call =
  407. container_of(work, struct afs_call, async_work);
  408. _enter("");
  409. if (!skb_queue_empty(&call->rx_queue))
  410. afs_deliver_to_call(call);
  411. if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
  412. if (call->wait_mode->async_complete)
  413. call->wait_mode->async_complete(call->reply,
  414. call->error);
  415. call->reply = NULL;
  416. /* kill the call */
  417. rxrpc_kernel_end_call(call->rxcall);
  418. if (call->type->destructor)
  419. call->type->destructor(call);
  420. /* we can't just delete the call because the work item may be
  421. * queued */
  422. PREPARE_WORK(&call->async_work, afs_delete_async_call);
  423. queue_work(afs_async_calls, &call->async_work);
  424. }
  425. _leave("");
  426. }
  427. /*
  428. * empty a socket buffer into a flat reply buffer
  429. */
  430. void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
  431. {
  432. size_t len = skb->len;
  433. if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
  434. BUG();
  435. call->reply_size += len;
  436. }
  437. /*
  438. * accept the backlog of incoming calls
  439. */
  440. static void afs_collect_incoming_call(struct work_struct *work)
  441. {
  442. struct rxrpc_call *rxcall;
  443. struct afs_call *call = NULL;
  444. struct sk_buff *skb;
  445. while ((skb = skb_dequeue(&afs_incoming_calls))) {
  446. _debug("new call");
  447. /* don't need the notification */
  448. rxrpc_kernel_free_skb(skb);
  449. if (!call) {
  450. call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
  451. if (!call) {
  452. rxrpc_kernel_reject_call(afs_socket);
  453. return;
  454. }
  455. INIT_WORK(&call->async_work, afs_process_async_call);
  456. call->wait_mode = &afs_async_incoming_call;
  457. call->type = &afs_RXCMxxxx;
  458. init_waitqueue_head(&call->waitq);
  459. skb_queue_head_init(&call->rx_queue);
  460. call->state = AFS_CALL_AWAIT_OP_ID;
  461. }
  462. rxcall = rxrpc_kernel_accept_call(afs_socket,
  463. (unsigned long) call);
  464. if (!IS_ERR(rxcall)) {
  465. call->rxcall = rxcall;
  466. call = NULL;
  467. }
  468. }
  469. kfree(call);
  470. }
  471. /*
  472. * grab the operation ID from an incoming cache manager call
  473. */
  474. static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
  475. bool last)
  476. {
  477. size_t len = skb->len;
  478. void *oibuf = (void *) &call->operation_ID;
  479. _enter("{%u},{%zu},%d", call->offset, len, last);
  480. ASSERTCMP(call->offset, <, 4);
  481. /* the operation ID forms the first four bytes of the request data */
  482. len = min_t(size_t, len, 4 - call->offset);
  483. if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
  484. BUG();
  485. if (!pskb_pull(skb, len))
  486. BUG();
  487. call->offset += len;
  488. if (call->offset < 4) {
  489. if (last) {
  490. _leave(" = -EBADMSG [op ID short]");
  491. return -EBADMSG;
  492. }
  493. _leave(" = 0 [incomplete]");
  494. return 0;
  495. }
  496. call->state = AFS_CALL_AWAIT_REQUEST;
  497. /* ask the cache manager to route the call (it'll change the call type
  498. * if successful) */
  499. if (!afs_cm_incoming_call(call))
  500. return -ENOTSUPP;
  501. /* pass responsibility for the remainer of this message off to the
  502. * cache manager op */
  503. return call->type->deliver(call, skb, last);
  504. }
  505. /*
  506. * send an empty reply
  507. */
  508. void afs_send_empty_reply(struct afs_call *call)
  509. {
  510. struct msghdr msg;
  511. struct iovec iov[1];
  512. _enter("");
  513. iov[0].iov_base = NULL;
  514. iov[0].iov_len = 0;
  515. msg.msg_name = NULL;
  516. msg.msg_namelen = 0;
  517. msg.msg_iov = iov;
  518. msg.msg_iovlen = 0;
  519. msg.msg_control = NULL;
  520. msg.msg_controllen = 0;
  521. msg.msg_flags = 0;
  522. call->state = AFS_CALL_AWAIT_ACK;
  523. switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
  524. case 0:
  525. _leave(" [replied]");
  526. return;
  527. case -ENOMEM:
  528. _debug("oom");
  529. rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
  530. default:
  531. rxrpc_kernel_end_call(call->rxcall);
  532. call->rxcall = NULL;
  533. call->type->destructor(call);
  534. ASSERT(skb_queue_empty(&call->rx_queue));
  535. kfree(call);
  536. _leave(" [error]");
  537. return;
  538. }
  539. }
  540. /*
  541. * extract a piece of data from the received data socket buffers
  542. */
  543. int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
  544. bool last, void *buf, size_t count)
  545. {
  546. size_t len = skb->len;
  547. _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
  548. ASSERTCMP(call->offset, <, count);
  549. len = min_t(size_t, len, count - call->offset);
  550. if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
  551. !pskb_pull(skb, len))
  552. BUG();
  553. call->offset += len;
  554. if (call->offset < count) {
  555. if (last) {
  556. _leave(" = -EBADMSG [%d < %lu]", call->offset, count);
  557. return -EBADMSG;
  558. }
  559. _leave(" = -EAGAIN");
  560. return -EAGAIN;
  561. }
  562. return 0;
  563. }