af_rxrpc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /* AF_RXRPC implementation
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/net.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/random.h>
  18. #include <linux/poll.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/key-type.h>
  21. #include <net/net_namespace.h>
  22. #include <net/sock.h>
  23. #include <net/af_rxrpc.h>
  24. #define CREATE_TRACE_POINTS
  25. #include "ar-internal.h"
  26. MODULE_DESCRIPTION("RxRPC network protocol");
  27. MODULE_AUTHOR("Red Hat, Inc.");
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS_NETPROTO(PF_RXRPC);
  30. unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
  31. module_param_named(debug, rxrpc_debug, uint, 0644);
  32. MODULE_PARM_DESC(debug, "RxRPC debugging mask");
  33. static struct proto rxrpc_proto;
  34. static const struct proto_ops rxrpc_rpc_ops;
  35. /* current debugging ID */
  36. atomic_t rxrpc_debug_id;
  37. EXPORT_SYMBOL(rxrpc_debug_id);
  38. /* count of skbs currently in use */
  39. atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
  40. struct workqueue_struct *rxrpc_workqueue;
  41. static void rxrpc_sock_destructor(struct sock *);
  42. /*
  43. * see if an RxRPC socket is currently writable
  44. */
  45. static inline int rxrpc_writable(struct sock *sk)
  46. {
  47. return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
  48. }
  49. /*
  50. * wait for write bufferage to become available
  51. */
  52. static void rxrpc_write_space(struct sock *sk)
  53. {
  54. _enter("%p", sk);
  55. rcu_read_lock();
  56. if (rxrpc_writable(sk)) {
  57. struct socket_wq *wq = rcu_dereference(sk->sk_wq);
  58. if (skwq_has_sleeper(wq))
  59. wake_up_interruptible(&wq->wait);
  60. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  61. }
  62. rcu_read_unlock();
  63. }
  64. /*
  65. * validate an RxRPC address
  66. */
  67. static int rxrpc_validate_address(struct rxrpc_sock *rx,
  68. struct sockaddr_rxrpc *srx,
  69. int len)
  70. {
  71. unsigned int tail;
  72. if (len < sizeof(struct sockaddr_rxrpc))
  73. return -EINVAL;
  74. if (srx->srx_family != AF_RXRPC)
  75. return -EAFNOSUPPORT;
  76. if (srx->transport_type != SOCK_DGRAM)
  77. return -ESOCKTNOSUPPORT;
  78. len -= offsetof(struct sockaddr_rxrpc, transport);
  79. if (srx->transport_len < sizeof(sa_family_t) ||
  80. srx->transport_len > len)
  81. return -EINVAL;
  82. if (srx->transport.family != rx->family)
  83. return -EAFNOSUPPORT;
  84. switch (srx->transport.family) {
  85. case AF_INET:
  86. if (srx->transport_len < sizeof(struct sockaddr_in))
  87. return -EINVAL;
  88. tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
  89. break;
  90. #ifdef CONFIG_AF_RXRPC_IPV6
  91. case AF_INET6:
  92. if (srx->transport_len < sizeof(struct sockaddr_in6))
  93. return -EINVAL;
  94. tail = offsetof(struct sockaddr_rxrpc, transport) +
  95. sizeof(struct sockaddr_in6);
  96. break;
  97. #endif
  98. default:
  99. return -EAFNOSUPPORT;
  100. }
  101. if (tail < len)
  102. memset((void *)srx + tail, 0, len - tail);
  103. _debug("INET: %pISp", &srx->transport);
  104. return 0;
  105. }
  106. /*
  107. * bind a local address to an RxRPC socket
  108. */
  109. static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
  110. {
  111. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
  112. struct rxrpc_local *local;
  113. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  114. u16 service_id = srx->srx_service;
  115. int ret;
  116. _enter("%p,%p,%d", rx, saddr, len);
  117. ret = rxrpc_validate_address(rx, srx, len);
  118. if (ret < 0)
  119. goto error;
  120. lock_sock(&rx->sk);
  121. switch (rx->sk.sk_state) {
  122. case RXRPC_UNBOUND:
  123. rx->srx = *srx;
  124. local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
  125. if (IS_ERR(local)) {
  126. ret = PTR_ERR(local);
  127. goto error_unlock;
  128. }
  129. if (service_id) {
  130. write_lock(&local->services_lock);
  131. if (rcu_access_pointer(local->service))
  132. goto service_in_use;
  133. rx->local = local;
  134. rcu_assign_pointer(local->service, rx);
  135. write_unlock(&local->services_lock);
  136. rx->sk.sk_state = RXRPC_SERVER_BOUND;
  137. } else {
  138. rx->local = local;
  139. rx->sk.sk_state = RXRPC_CLIENT_BOUND;
  140. }
  141. break;
  142. case RXRPC_SERVER_BOUND:
  143. ret = -EINVAL;
  144. if (service_id == 0)
  145. goto error_unlock;
  146. ret = -EADDRINUSE;
  147. if (service_id == rx->srx.srx_service)
  148. goto error_unlock;
  149. ret = -EINVAL;
  150. srx->srx_service = rx->srx.srx_service;
  151. if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
  152. goto error_unlock;
  153. rx->second_service = service_id;
  154. rx->sk.sk_state = RXRPC_SERVER_BOUND2;
  155. break;
  156. default:
  157. ret = -EINVAL;
  158. goto error_unlock;
  159. }
  160. release_sock(&rx->sk);
  161. _leave(" = 0");
  162. return 0;
  163. service_in_use:
  164. write_unlock(&local->services_lock);
  165. rxrpc_put_local(local);
  166. ret = -EADDRINUSE;
  167. error_unlock:
  168. release_sock(&rx->sk);
  169. error:
  170. _leave(" = %d", ret);
  171. return ret;
  172. }
  173. /*
  174. * set the number of pending calls permitted on a listening socket
  175. */
  176. static int rxrpc_listen(struct socket *sock, int backlog)
  177. {
  178. struct sock *sk = sock->sk;
  179. struct rxrpc_sock *rx = rxrpc_sk(sk);
  180. unsigned int max, old;
  181. int ret;
  182. _enter("%p,%d", rx, backlog);
  183. lock_sock(&rx->sk);
  184. switch (rx->sk.sk_state) {
  185. case RXRPC_UNBOUND:
  186. ret = -EADDRNOTAVAIL;
  187. break;
  188. case RXRPC_SERVER_BOUND:
  189. case RXRPC_SERVER_BOUND2:
  190. ASSERT(rx->local != NULL);
  191. max = READ_ONCE(rxrpc_max_backlog);
  192. ret = -EINVAL;
  193. if (backlog == INT_MAX)
  194. backlog = max;
  195. else if (backlog < 0 || backlog > max)
  196. break;
  197. old = sk->sk_max_ack_backlog;
  198. sk->sk_max_ack_backlog = backlog;
  199. ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
  200. if (ret == 0)
  201. rx->sk.sk_state = RXRPC_SERVER_LISTENING;
  202. else
  203. sk->sk_max_ack_backlog = old;
  204. break;
  205. case RXRPC_SERVER_LISTENING:
  206. if (backlog == 0) {
  207. rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
  208. sk->sk_max_ack_backlog = 0;
  209. rxrpc_discard_prealloc(rx);
  210. ret = 0;
  211. break;
  212. }
  213. /* Fall through */
  214. default:
  215. ret = -EBUSY;
  216. break;
  217. }
  218. release_sock(&rx->sk);
  219. _leave(" = %d", ret);
  220. return ret;
  221. }
  222. /**
  223. * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
  224. * @sock: The socket on which to make the call
  225. * @srx: The address of the peer to contact
  226. * @key: The security context to use (defaults to socket setting)
  227. * @user_call_ID: The ID to use
  228. * @tx_total_len: Total length of data to transmit during the call (or -1)
  229. * @gfp: The allocation constraints
  230. * @notify_rx: Where to send notifications instead of socket queue
  231. * @upgrade: Request service upgrade for call
  232. * @debug_id: The debug ID for tracing to be assigned to the call
  233. *
  234. * Allow a kernel service to begin a call on the nominated socket. This just
  235. * sets up all the internal tracking structures and allocates connection and
  236. * call IDs as appropriate. The call to be used is returned.
  237. *
  238. * The default socket destination address and security may be overridden by
  239. * supplying @srx and @key.
  240. */
  241. struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
  242. struct sockaddr_rxrpc *srx,
  243. struct key *key,
  244. unsigned long user_call_ID,
  245. s64 tx_total_len,
  246. gfp_t gfp,
  247. rxrpc_notify_rx_t notify_rx,
  248. bool upgrade,
  249. unsigned int debug_id)
  250. {
  251. struct rxrpc_conn_parameters cp;
  252. struct rxrpc_call_params p;
  253. struct rxrpc_call *call;
  254. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  255. int ret;
  256. _enter(",,%x,%lx", key_serial(key), user_call_ID);
  257. ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
  258. if (ret < 0)
  259. return ERR_PTR(ret);
  260. lock_sock(&rx->sk);
  261. if (!key)
  262. key = rx->key;
  263. if (key && !key->payload.data[0])
  264. key = NULL; /* a no-security key */
  265. memset(&p, 0, sizeof(p));
  266. p.user_call_ID = user_call_ID;
  267. p.tx_total_len = tx_total_len;
  268. memset(&cp, 0, sizeof(cp));
  269. cp.local = rx->local;
  270. cp.key = key;
  271. cp.security_level = rx->min_sec_level;
  272. cp.exclusive = false;
  273. cp.upgrade = upgrade;
  274. cp.service_id = srx->srx_service;
  275. call = rxrpc_new_client_call(rx, &cp, srx, &p, gfp, debug_id);
  276. /* The socket has been unlocked. */
  277. if (!IS_ERR(call)) {
  278. call->notify_rx = notify_rx;
  279. mutex_unlock(&call->user_mutex);
  280. }
  281. rxrpc_put_peer(cp.peer);
  282. _leave(" = %p", call);
  283. return call;
  284. }
  285. EXPORT_SYMBOL(rxrpc_kernel_begin_call);
  286. /*
  287. * Dummy function used to stop the notifier talking to recvmsg().
  288. */
  289. static void rxrpc_dummy_notify_rx(struct sock *sk, struct rxrpc_call *rxcall,
  290. unsigned long call_user_ID)
  291. {
  292. }
  293. /**
  294. * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
  295. * @sock: The socket the call is on
  296. * @call: The call to end
  297. *
  298. * Allow a kernel service to end a call it was using. The call must be
  299. * complete before this is called (the call should be aborted if necessary).
  300. */
  301. void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
  302. {
  303. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  304. mutex_lock(&call->user_mutex);
  305. rxrpc_release_call(rxrpc_sk(sock->sk), call);
  306. /* Make sure we're not going to call back into a kernel service */
  307. if (call->notify_rx) {
  308. spin_lock_bh(&call->notify_lock);
  309. call->notify_rx = rxrpc_dummy_notify_rx;
  310. spin_unlock_bh(&call->notify_lock);
  311. }
  312. mutex_unlock(&call->user_mutex);
  313. rxrpc_put_call(call, rxrpc_call_put_kernel);
  314. }
  315. EXPORT_SYMBOL(rxrpc_kernel_end_call);
  316. /**
  317. * rxrpc_kernel_check_life - Check to see whether a call is still alive
  318. * @sock: The socket the call is on
  319. * @call: The call to check
  320. *
  321. * Allow a kernel service to find out whether a call is still alive - ie. we're
  322. * getting ACKs from the server. Returns a number representing the life state
  323. * which can be compared to that returned by a previous call.
  324. *
  325. * If this is a client call, ping ACKs will be sent to the server to find out
  326. * whether it's still responsive and whether the call is still alive on the
  327. * server.
  328. */
  329. u32 rxrpc_kernel_check_life(struct socket *sock, struct rxrpc_call *call)
  330. {
  331. return call->acks_latest;
  332. }
  333. EXPORT_SYMBOL(rxrpc_kernel_check_life);
  334. /**
  335. * rxrpc_kernel_check_call - Check a call's state
  336. * @sock: The socket the call is on
  337. * @call: The call to check
  338. * @_compl: Where to store the completion state
  339. * @_abort_code: Where to store any abort code
  340. *
  341. * Allow a kernel service to query the state of a call and find out the manner
  342. * of its termination if it has completed. Returns -EINPROGRESS if the call is
  343. * still going, 0 if the call finished successfully, -ECONNABORTED if the call
  344. * was aborted and an appropriate error if the call failed in some other way.
  345. */
  346. int rxrpc_kernel_check_call(struct socket *sock, struct rxrpc_call *call,
  347. enum rxrpc_call_completion *_compl, u32 *_abort_code)
  348. {
  349. if (call->state != RXRPC_CALL_COMPLETE)
  350. return -EINPROGRESS;
  351. smp_rmb();
  352. *_compl = call->completion;
  353. *_abort_code = call->abort_code;
  354. return call->error;
  355. }
  356. EXPORT_SYMBOL(rxrpc_kernel_check_call);
  357. /**
  358. * rxrpc_kernel_retry_call - Allow a kernel service to retry a call
  359. * @sock: The socket the call is on
  360. * @call: The call to retry
  361. * @srx: The address of the peer to contact
  362. * @key: The security context to use (defaults to socket setting)
  363. *
  364. * Allow a kernel service to try resending a client call that failed due to a
  365. * network error to a new address. The Tx queue is maintained intact, thereby
  366. * relieving the need to re-encrypt any request data that has already been
  367. * buffered.
  368. */
  369. int rxrpc_kernel_retry_call(struct socket *sock, struct rxrpc_call *call,
  370. struct sockaddr_rxrpc *srx, struct key *key)
  371. {
  372. struct rxrpc_conn_parameters cp;
  373. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  374. int ret;
  375. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  376. if (!key)
  377. key = rx->key;
  378. if (key && !key->payload.data[0])
  379. key = NULL; /* a no-security key */
  380. memset(&cp, 0, sizeof(cp));
  381. cp.local = rx->local;
  382. cp.key = key;
  383. cp.security_level = 0;
  384. cp.exclusive = false;
  385. cp.service_id = srx->srx_service;
  386. mutex_lock(&call->user_mutex);
  387. ret = rxrpc_prepare_call_for_retry(rx, call);
  388. if (ret == 0)
  389. ret = rxrpc_retry_client_call(rx, call, &cp, srx, GFP_KERNEL);
  390. mutex_unlock(&call->user_mutex);
  391. rxrpc_put_peer(cp.peer);
  392. _leave(" = %d", ret);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL(rxrpc_kernel_retry_call);
  396. /**
  397. * rxrpc_kernel_new_call_notification - Get notifications of new calls
  398. * @sock: The socket to intercept received messages on
  399. * @notify_new_call: Function to be called when new calls appear
  400. * @discard_new_call: Function to discard preallocated calls
  401. *
  402. * Allow a kernel service to be given notifications about new calls.
  403. */
  404. void rxrpc_kernel_new_call_notification(
  405. struct socket *sock,
  406. rxrpc_notify_new_call_t notify_new_call,
  407. rxrpc_discard_new_call_t discard_new_call)
  408. {
  409. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  410. rx->notify_new_call = notify_new_call;
  411. rx->discard_new_call = discard_new_call;
  412. }
  413. EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
  414. /*
  415. * connect an RxRPC socket
  416. * - this just targets it at a specific destination; no actual connection
  417. * negotiation takes place
  418. */
  419. static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
  420. int addr_len, int flags)
  421. {
  422. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
  423. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  424. int ret;
  425. _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
  426. ret = rxrpc_validate_address(rx, srx, addr_len);
  427. if (ret < 0) {
  428. _leave(" = %d [bad addr]", ret);
  429. return ret;
  430. }
  431. lock_sock(&rx->sk);
  432. ret = -EISCONN;
  433. if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
  434. goto error;
  435. switch (rx->sk.sk_state) {
  436. case RXRPC_UNBOUND:
  437. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  438. case RXRPC_CLIENT_UNBOUND:
  439. case RXRPC_CLIENT_BOUND:
  440. break;
  441. default:
  442. ret = -EBUSY;
  443. goto error;
  444. }
  445. rx->connect_srx = *srx;
  446. set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
  447. ret = 0;
  448. error:
  449. release_sock(&rx->sk);
  450. return ret;
  451. }
  452. /*
  453. * send a message through an RxRPC socket
  454. * - in a client this does a number of things:
  455. * - finds/sets up a connection for the security specified (if any)
  456. * - initiates a call (ID in control data)
  457. * - ends the request phase of a call (if MSG_MORE is not set)
  458. * - sends a call data packet
  459. * - may send an abort (abort code in control data)
  460. */
  461. static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  462. {
  463. struct rxrpc_local *local;
  464. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  465. int ret;
  466. _enter(",{%d},,%zu", rx->sk.sk_state, len);
  467. if (m->msg_flags & MSG_OOB)
  468. return -EOPNOTSUPP;
  469. if (m->msg_name) {
  470. ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
  471. if (ret < 0) {
  472. _leave(" = %d [bad addr]", ret);
  473. return ret;
  474. }
  475. }
  476. lock_sock(&rx->sk);
  477. switch (rx->sk.sk_state) {
  478. case RXRPC_UNBOUND:
  479. rx->srx.srx_family = AF_RXRPC;
  480. rx->srx.srx_service = 0;
  481. rx->srx.transport_type = SOCK_DGRAM;
  482. rx->srx.transport.family = rx->family;
  483. switch (rx->family) {
  484. case AF_INET:
  485. rx->srx.transport_len = sizeof(struct sockaddr_in);
  486. break;
  487. #ifdef CONFIG_AF_RXRPC_IPV6
  488. case AF_INET6:
  489. rx->srx.transport_len = sizeof(struct sockaddr_in6);
  490. break;
  491. #endif
  492. default:
  493. ret = -EAFNOSUPPORT;
  494. goto error_unlock;
  495. }
  496. local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
  497. if (IS_ERR(local)) {
  498. ret = PTR_ERR(local);
  499. goto error_unlock;
  500. }
  501. rx->local = local;
  502. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  503. /* Fall through */
  504. case RXRPC_CLIENT_UNBOUND:
  505. case RXRPC_CLIENT_BOUND:
  506. if (!m->msg_name &&
  507. test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
  508. m->msg_name = &rx->connect_srx;
  509. m->msg_namelen = sizeof(rx->connect_srx);
  510. }
  511. /* Fall through */
  512. case RXRPC_SERVER_BOUND:
  513. case RXRPC_SERVER_LISTENING:
  514. ret = rxrpc_do_sendmsg(rx, m, len);
  515. /* The socket has been unlocked */
  516. goto out;
  517. default:
  518. ret = -EINVAL;
  519. goto error_unlock;
  520. }
  521. error_unlock:
  522. release_sock(&rx->sk);
  523. out:
  524. _leave(" = %d", ret);
  525. return ret;
  526. }
  527. /*
  528. * set RxRPC socket options
  529. */
  530. static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
  531. char __user *optval, unsigned int optlen)
  532. {
  533. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  534. unsigned int min_sec_level;
  535. u16 service_upgrade[2];
  536. int ret;
  537. _enter(",%d,%d,,%d", level, optname, optlen);
  538. lock_sock(&rx->sk);
  539. ret = -EOPNOTSUPP;
  540. if (level == SOL_RXRPC) {
  541. switch (optname) {
  542. case RXRPC_EXCLUSIVE_CONNECTION:
  543. ret = -EINVAL;
  544. if (optlen != 0)
  545. goto error;
  546. ret = -EISCONN;
  547. if (rx->sk.sk_state != RXRPC_UNBOUND)
  548. goto error;
  549. rx->exclusive = true;
  550. goto success;
  551. case RXRPC_SECURITY_KEY:
  552. ret = -EINVAL;
  553. if (rx->key)
  554. goto error;
  555. ret = -EISCONN;
  556. if (rx->sk.sk_state != RXRPC_UNBOUND)
  557. goto error;
  558. ret = rxrpc_request_key(rx, optval, optlen);
  559. goto error;
  560. case RXRPC_SECURITY_KEYRING:
  561. ret = -EINVAL;
  562. if (rx->key)
  563. goto error;
  564. ret = -EISCONN;
  565. if (rx->sk.sk_state != RXRPC_UNBOUND)
  566. goto error;
  567. ret = rxrpc_server_keyring(rx, optval, optlen);
  568. goto error;
  569. case RXRPC_MIN_SECURITY_LEVEL:
  570. ret = -EINVAL;
  571. if (optlen != sizeof(unsigned int))
  572. goto error;
  573. ret = -EISCONN;
  574. if (rx->sk.sk_state != RXRPC_UNBOUND)
  575. goto error;
  576. ret = get_user(min_sec_level,
  577. (unsigned int __user *) optval);
  578. if (ret < 0)
  579. goto error;
  580. ret = -EINVAL;
  581. if (min_sec_level > RXRPC_SECURITY_MAX)
  582. goto error;
  583. rx->min_sec_level = min_sec_level;
  584. goto success;
  585. case RXRPC_UPGRADEABLE_SERVICE:
  586. ret = -EINVAL;
  587. if (optlen != sizeof(service_upgrade) ||
  588. rx->service_upgrade.from != 0)
  589. goto error;
  590. ret = -EISCONN;
  591. if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
  592. goto error;
  593. ret = -EFAULT;
  594. if (copy_from_user(service_upgrade, optval,
  595. sizeof(service_upgrade)) != 0)
  596. goto error;
  597. ret = -EINVAL;
  598. if ((service_upgrade[0] != rx->srx.srx_service ||
  599. service_upgrade[1] != rx->second_service) &&
  600. (service_upgrade[0] != rx->second_service ||
  601. service_upgrade[1] != rx->srx.srx_service))
  602. goto error;
  603. rx->service_upgrade.from = service_upgrade[0];
  604. rx->service_upgrade.to = service_upgrade[1];
  605. goto success;
  606. default:
  607. break;
  608. }
  609. }
  610. success:
  611. ret = 0;
  612. error:
  613. release_sock(&rx->sk);
  614. return ret;
  615. }
  616. /*
  617. * Get socket options.
  618. */
  619. static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
  620. char __user *optval, int __user *_optlen)
  621. {
  622. int optlen;
  623. if (level != SOL_RXRPC)
  624. return -EOPNOTSUPP;
  625. if (get_user(optlen, _optlen))
  626. return -EFAULT;
  627. switch (optname) {
  628. case RXRPC_SUPPORTED_CMSG:
  629. if (optlen < sizeof(int))
  630. return -ETOOSMALL;
  631. if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
  632. put_user(sizeof(int), _optlen))
  633. return -EFAULT;
  634. return 0;
  635. default:
  636. return -EOPNOTSUPP;
  637. }
  638. }
  639. /*
  640. * permit an RxRPC socket to be polled
  641. */
  642. static __poll_t rxrpc_poll_mask(struct socket *sock, __poll_t events)
  643. {
  644. struct sock *sk = sock->sk;
  645. struct rxrpc_sock *rx = rxrpc_sk(sk);
  646. __poll_t mask = 0;
  647. /* the socket is readable if there are any messages waiting on the Rx
  648. * queue */
  649. if (!list_empty(&rx->recvmsg_q))
  650. mask |= EPOLLIN | EPOLLRDNORM;
  651. /* the socket is writable if there is space to add new data to the
  652. * socket; there is no guarantee that any particular call in progress
  653. * on the socket may have space in the Tx ACK window */
  654. if (rxrpc_writable(sk))
  655. mask |= EPOLLOUT | EPOLLWRNORM;
  656. return mask;
  657. }
  658. /*
  659. * create an RxRPC socket
  660. */
  661. static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
  662. int kern)
  663. {
  664. struct rxrpc_net *rxnet;
  665. struct rxrpc_sock *rx;
  666. struct sock *sk;
  667. _enter("%p,%d", sock, protocol);
  668. /* we support transport protocol UDP/UDP6 only */
  669. if (protocol != PF_INET &&
  670. IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
  671. return -EPROTONOSUPPORT;
  672. if (sock->type != SOCK_DGRAM)
  673. return -ESOCKTNOSUPPORT;
  674. sock->ops = &rxrpc_rpc_ops;
  675. sock->state = SS_UNCONNECTED;
  676. sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
  677. if (!sk)
  678. return -ENOMEM;
  679. sock_init_data(sock, sk);
  680. sock_set_flag(sk, SOCK_RCU_FREE);
  681. sk->sk_state = RXRPC_UNBOUND;
  682. sk->sk_write_space = rxrpc_write_space;
  683. sk->sk_max_ack_backlog = 0;
  684. sk->sk_destruct = rxrpc_sock_destructor;
  685. rx = rxrpc_sk(sk);
  686. rx->family = protocol;
  687. rx->calls = RB_ROOT;
  688. spin_lock_init(&rx->incoming_lock);
  689. INIT_LIST_HEAD(&rx->sock_calls);
  690. INIT_LIST_HEAD(&rx->to_be_accepted);
  691. INIT_LIST_HEAD(&rx->recvmsg_q);
  692. rwlock_init(&rx->recvmsg_lock);
  693. rwlock_init(&rx->call_lock);
  694. memset(&rx->srx, 0, sizeof(rx->srx));
  695. rxnet = rxrpc_net(sock_net(&rx->sk));
  696. timer_reduce(&rxnet->peer_keepalive_timer, jiffies + 1);
  697. _leave(" = 0 [%p]", rx);
  698. return 0;
  699. }
  700. /*
  701. * Kill all the calls on a socket and shut it down.
  702. */
  703. static int rxrpc_shutdown(struct socket *sock, int flags)
  704. {
  705. struct sock *sk = sock->sk;
  706. struct rxrpc_sock *rx = rxrpc_sk(sk);
  707. int ret = 0;
  708. _enter("%p,%d", sk, flags);
  709. if (flags != SHUT_RDWR)
  710. return -EOPNOTSUPP;
  711. if (sk->sk_state == RXRPC_CLOSE)
  712. return -ESHUTDOWN;
  713. lock_sock(sk);
  714. spin_lock_bh(&sk->sk_receive_queue.lock);
  715. if (sk->sk_state < RXRPC_CLOSE) {
  716. sk->sk_state = RXRPC_CLOSE;
  717. sk->sk_shutdown = SHUTDOWN_MASK;
  718. } else {
  719. ret = -ESHUTDOWN;
  720. }
  721. spin_unlock_bh(&sk->sk_receive_queue.lock);
  722. rxrpc_discard_prealloc(rx);
  723. release_sock(sk);
  724. return ret;
  725. }
  726. /*
  727. * RxRPC socket destructor
  728. */
  729. static void rxrpc_sock_destructor(struct sock *sk)
  730. {
  731. _enter("%p", sk);
  732. rxrpc_purge_queue(&sk->sk_receive_queue);
  733. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  734. WARN_ON(!sk_unhashed(sk));
  735. WARN_ON(sk->sk_socket);
  736. if (!sock_flag(sk, SOCK_DEAD)) {
  737. printk("Attempt to release alive rxrpc socket: %p\n", sk);
  738. return;
  739. }
  740. }
  741. /*
  742. * release an RxRPC socket
  743. */
  744. static int rxrpc_release_sock(struct sock *sk)
  745. {
  746. struct rxrpc_sock *rx = rxrpc_sk(sk);
  747. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  748. _enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
  749. /* declare the socket closed for business */
  750. sock_orphan(sk);
  751. sk->sk_shutdown = SHUTDOWN_MASK;
  752. /* We want to kill off all connections from a service socket
  753. * as fast as possible because we can't share these; client
  754. * sockets, on the other hand, can share an endpoint.
  755. */
  756. switch (sk->sk_state) {
  757. case RXRPC_SERVER_BOUND:
  758. case RXRPC_SERVER_BOUND2:
  759. case RXRPC_SERVER_LISTENING:
  760. case RXRPC_SERVER_LISTEN_DISABLED:
  761. rx->local->service_closed = true;
  762. break;
  763. }
  764. spin_lock_bh(&sk->sk_receive_queue.lock);
  765. sk->sk_state = RXRPC_CLOSE;
  766. spin_unlock_bh(&sk->sk_receive_queue.lock);
  767. if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
  768. write_lock(&rx->local->services_lock);
  769. rcu_assign_pointer(rx->local->service, NULL);
  770. write_unlock(&rx->local->services_lock);
  771. }
  772. /* try to flush out this socket */
  773. rxrpc_discard_prealloc(rx);
  774. rxrpc_release_calls_on_socket(rx);
  775. flush_workqueue(rxrpc_workqueue);
  776. rxrpc_purge_queue(&sk->sk_receive_queue);
  777. rxrpc_queue_work(&rxnet->service_conn_reaper);
  778. rxrpc_queue_work(&rxnet->client_conn_reaper);
  779. rxrpc_put_local(rx->local);
  780. rx->local = NULL;
  781. key_put(rx->key);
  782. rx->key = NULL;
  783. key_put(rx->securities);
  784. rx->securities = NULL;
  785. sock_put(sk);
  786. _leave(" = 0");
  787. return 0;
  788. }
  789. /*
  790. * release an RxRPC BSD socket on close() or equivalent
  791. */
  792. static int rxrpc_release(struct socket *sock)
  793. {
  794. struct sock *sk = sock->sk;
  795. _enter("%p{%p}", sock, sk);
  796. if (!sk)
  797. return 0;
  798. sock->sk = NULL;
  799. return rxrpc_release_sock(sk);
  800. }
  801. /*
  802. * RxRPC network protocol
  803. */
  804. static const struct proto_ops rxrpc_rpc_ops = {
  805. .family = PF_RXRPC,
  806. .owner = THIS_MODULE,
  807. .release = rxrpc_release,
  808. .bind = rxrpc_bind,
  809. .connect = rxrpc_connect,
  810. .socketpair = sock_no_socketpair,
  811. .accept = sock_no_accept,
  812. .getname = sock_no_getname,
  813. .poll_mask = rxrpc_poll_mask,
  814. .ioctl = sock_no_ioctl,
  815. .listen = rxrpc_listen,
  816. .shutdown = rxrpc_shutdown,
  817. .setsockopt = rxrpc_setsockopt,
  818. .getsockopt = rxrpc_getsockopt,
  819. .sendmsg = rxrpc_sendmsg,
  820. .recvmsg = rxrpc_recvmsg,
  821. .mmap = sock_no_mmap,
  822. .sendpage = sock_no_sendpage,
  823. };
  824. static struct proto rxrpc_proto = {
  825. .name = "RXRPC",
  826. .owner = THIS_MODULE,
  827. .obj_size = sizeof(struct rxrpc_sock),
  828. .max_header = sizeof(struct rxrpc_wire_header),
  829. };
  830. static const struct net_proto_family rxrpc_family_ops = {
  831. .family = PF_RXRPC,
  832. .create = rxrpc_create,
  833. .owner = THIS_MODULE,
  834. };
  835. /*
  836. * initialise and register the RxRPC protocol
  837. */
  838. static int __init af_rxrpc_init(void)
  839. {
  840. int ret = -1;
  841. unsigned int tmp;
  842. BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
  843. get_random_bytes(&tmp, sizeof(tmp));
  844. tmp &= 0x3fffffff;
  845. if (tmp == 0)
  846. tmp = 1;
  847. idr_set_cursor(&rxrpc_client_conn_ids, tmp);
  848. ret = -ENOMEM;
  849. rxrpc_call_jar = kmem_cache_create(
  850. "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
  851. SLAB_HWCACHE_ALIGN, NULL);
  852. if (!rxrpc_call_jar) {
  853. pr_notice("Failed to allocate call jar\n");
  854. goto error_call_jar;
  855. }
  856. rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
  857. if (!rxrpc_workqueue) {
  858. pr_notice("Failed to allocate work queue\n");
  859. goto error_work_queue;
  860. }
  861. ret = rxrpc_init_security();
  862. if (ret < 0) {
  863. pr_crit("Cannot initialise security\n");
  864. goto error_security;
  865. }
  866. ret = register_pernet_subsys(&rxrpc_net_ops);
  867. if (ret)
  868. goto error_pernet;
  869. ret = proto_register(&rxrpc_proto, 1);
  870. if (ret < 0) {
  871. pr_crit("Cannot register protocol\n");
  872. goto error_proto;
  873. }
  874. ret = sock_register(&rxrpc_family_ops);
  875. if (ret < 0) {
  876. pr_crit("Cannot register socket family\n");
  877. goto error_sock;
  878. }
  879. ret = register_key_type(&key_type_rxrpc);
  880. if (ret < 0) {
  881. pr_crit("Cannot register client key type\n");
  882. goto error_key_type;
  883. }
  884. ret = register_key_type(&key_type_rxrpc_s);
  885. if (ret < 0) {
  886. pr_crit("Cannot register server key type\n");
  887. goto error_key_type_s;
  888. }
  889. ret = rxrpc_sysctl_init();
  890. if (ret < 0) {
  891. pr_crit("Cannot register sysctls\n");
  892. goto error_sysctls;
  893. }
  894. return 0;
  895. error_sysctls:
  896. unregister_key_type(&key_type_rxrpc_s);
  897. error_key_type_s:
  898. unregister_key_type(&key_type_rxrpc);
  899. error_key_type:
  900. sock_unregister(PF_RXRPC);
  901. error_sock:
  902. proto_unregister(&rxrpc_proto);
  903. error_proto:
  904. unregister_pernet_subsys(&rxrpc_net_ops);
  905. error_pernet:
  906. rxrpc_exit_security();
  907. error_security:
  908. destroy_workqueue(rxrpc_workqueue);
  909. error_work_queue:
  910. kmem_cache_destroy(rxrpc_call_jar);
  911. error_call_jar:
  912. return ret;
  913. }
  914. /*
  915. * unregister the RxRPC protocol
  916. */
  917. static void __exit af_rxrpc_exit(void)
  918. {
  919. _enter("");
  920. rxrpc_sysctl_exit();
  921. unregister_key_type(&key_type_rxrpc_s);
  922. unregister_key_type(&key_type_rxrpc);
  923. sock_unregister(PF_RXRPC);
  924. proto_unregister(&rxrpc_proto);
  925. unregister_pernet_subsys(&rxrpc_net_ops);
  926. ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
  927. ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
  928. /* Make sure the local and peer records pinned by any dying connections
  929. * are released.
  930. */
  931. rcu_barrier();
  932. rxrpc_destroy_client_conn_ids();
  933. destroy_workqueue(rxrpc_workqueue);
  934. rxrpc_exit_security();
  935. kmem_cache_destroy(rxrpc_call_jar);
  936. _leave("");
  937. }
  938. module_init(af_rxrpc_init);
  939. module_exit(af_rxrpc_exit);