ar-connection.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /* RxRPC virtual connection handler
  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 <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/crypto.h>
  15. #include <net/sock.h>
  16. #include <net/af_rxrpc.h>
  17. #include "ar-internal.h"
  18. static void rxrpc_connection_reaper(struct work_struct *work);
  19. LIST_HEAD(rxrpc_connections);
  20. DEFINE_RWLOCK(rxrpc_connection_lock);
  21. static unsigned long rxrpc_connection_timeout = 10 * 60;
  22. static DECLARE_DELAYED_WORK(rxrpc_connection_reap, rxrpc_connection_reaper);
  23. /*
  24. * allocate a new client connection bundle
  25. */
  26. static struct rxrpc_conn_bundle *rxrpc_alloc_bundle(gfp_t gfp)
  27. {
  28. struct rxrpc_conn_bundle *bundle;
  29. _enter("");
  30. bundle = kzalloc(sizeof(struct rxrpc_conn_bundle), gfp);
  31. if (bundle) {
  32. INIT_LIST_HEAD(&bundle->unused_conns);
  33. INIT_LIST_HEAD(&bundle->avail_conns);
  34. INIT_LIST_HEAD(&bundle->busy_conns);
  35. init_waitqueue_head(&bundle->chanwait);
  36. atomic_set(&bundle->usage, 1);
  37. }
  38. _leave(" = %p", bundle);
  39. return bundle;
  40. }
  41. /*
  42. * compare bundle parameters with what we're looking for
  43. * - return -ve, 0 or +ve
  44. */
  45. static inline
  46. int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle *bundle,
  47. struct key *key, __be16 service_id)
  48. {
  49. return (bundle->service_id - service_id) ?:
  50. ((unsigned long) bundle->key - (unsigned long) key);
  51. }
  52. /*
  53. * get bundle of client connections that a client socket can make use of
  54. */
  55. struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *rx,
  56. struct rxrpc_transport *trans,
  57. struct key *key,
  58. __be16 service_id,
  59. gfp_t gfp)
  60. {
  61. struct rxrpc_conn_bundle *bundle, *candidate;
  62. struct rb_node *p, *parent, **pp;
  63. _enter("%p{%x},%x,%hx,",
  64. rx, key_serial(key), trans->debug_id, ntohs(service_id));
  65. if (rx->trans == trans && rx->bundle) {
  66. atomic_inc(&rx->bundle->usage);
  67. return rx->bundle;
  68. }
  69. /* search the extant bundles first for one that matches the specified
  70. * user ID */
  71. spin_lock(&trans->client_lock);
  72. p = trans->bundles.rb_node;
  73. while (p) {
  74. bundle = rb_entry(p, struct rxrpc_conn_bundle, node);
  75. if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
  76. p = p->rb_left;
  77. else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
  78. p = p->rb_right;
  79. else
  80. goto found_extant_bundle;
  81. }
  82. spin_unlock(&trans->client_lock);
  83. /* not yet present - create a candidate for a new record and then
  84. * redo the search */
  85. candidate = rxrpc_alloc_bundle(gfp);
  86. if (!candidate) {
  87. _leave(" = -ENOMEM");
  88. return ERR_PTR(-ENOMEM);
  89. }
  90. candidate->key = key_get(key);
  91. candidate->service_id = service_id;
  92. spin_lock(&trans->client_lock);
  93. pp = &trans->bundles.rb_node;
  94. parent = NULL;
  95. while (*pp) {
  96. parent = *pp;
  97. bundle = rb_entry(parent, struct rxrpc_conn_bundle, node);
  98. if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
  99. pp = &(*pp)->rb_left;
  100. else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
  101. pp = &(*pp)->rb_right;
  102. else
  103. goto found_extant_second;
  104. }
  105. /* second search also failed; add the new bundle */
  106. bundle = candidate;
  107. candidate = NULL;
  108. rb_link_node(&bundle->node, parent, pp);
  109. rb_insert_color(&bundle->node, &trans->bundles);
  110. spin_unlock(&trans->client_lock);
  111. _net("BUNDLE new on trans %d", trans->debug_id);
  112. if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
  113. atomic_inc(&bundle->usage);
  114. rx->bundle = bundle;
  115. }
  116. _leave(" = %p [new]", bundle);
  117. return bundle;
  118. /* we found the bundle in the list immediately */
  119. found_extant_bundle:
  120. atomic_inc(&bundle->usage);
  121. spin_unlock(&trans->client_lock);
  122. _net("BUNDLE old on trans %d", trans->debug_id);
  123. if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
  124. atomic_inc(&bundle->usage);
  125. rx->bundle = bundle;
  126. }
  127. _leave(" = %p [extant %d]", bundle, atomic_read(&bundle->usage));
  128. return bundle;
  129. /* we found the bundle on the second time through the list */
  130. found_extant_second:
  131. atomic_inc(&bundle->usage);
  132. spin_unlock(&trans->client_lock);
  133. kfree(candidate);
  134. _net("BUNDLE old2 on trans %d", trans->debug_id);
  135. if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
  136. atomic_inc(&bundle->usage);
  137. rx->bundle = bundle;
  138. }
  139. _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage));
  140. return bundle;
  141. }
  142. /*
  143. * release a bundle
  144. */
  145. void rxrpc_put_bundle(struct rxrpc_transport *trans,
  146. struct rxrpc_conn_bundle *bundle)
  147. {
  148. _enter("%p,%p{%d}",trans, bundle, atomic_read(&bundle->usage));
  149. if (atomic_dec_and_lock(&bundle->usage, &trans->client_lock)) {
  150. _debug("Destroy bundle");
  151. rb_erase(&bundle->node, &trans->bundles);
  152. spin_unlock(&trans->client_lock);
  153. ASSERT(list_empty(&bundle->unused_conns));
  154. ASSERT(list_empty(&bundle->avail_conns));
  155. ASSERT(list_empty(&bundle->busy_conns));
  156. ASSERTCMP(bundle->num_conns, ==, 0);
  157. key_put(bundle->key);
  158. kfree(bundle);
  159. }
  160. _leave("");
  161. }
  162. /*
  163. * allocate a new connection
  164. */
  165. static struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
  166. {
  167. struct rxrpc_connection *conn;
  168. _enter("");
  169. conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
  170. if (conn) {
  171. INIT_WORK(&conn->processor, &rxrpc_process_connection);
  172. INIT_LIST_HEAD(&conn->bundle_link);
  173. conn->calls = RB_ROOT;
  174. skb_queue_head_init(&conn->rx_queue);
  175. rwlock_init(&conn->lock);
  176. spin_lock_init(&conn->state_lock);
  177. atomic_set(&conn->usage, 1);
  178. conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
  179. conn->avail_calls = RXRPC_MAXCALLS;
  180. conn->size_align = 4;
  181. conn->header_size = sizeof(struct rxrpc_header);
  182. }
  183. _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
  184. return conn;
  185. }
  186. /*
  187. * assign a connection ID to a connection and add it to the transport's
  188. * connection lookup tree
  189. * - called with transport client lock held
  190. */
  191. static void rxrpc_assign_connection_id(struct rxrpc_connection *conn)
  192. {
  193. struct rxrpc_connection *xconn;
  194. struct rb_node *parent, **p;
  195. __be32 epoch;
  196. u32 real_conn_id;
  197. _enter("");
  198. epoch = conn->epoch;
  199. write_lock_bh(&conn->trans->conn_lock);
  200. conn->trans->conn_idcounter += RXRPC_CID_INC;
  201. if (conn->trans->conn_idcounter < RXRPC_CID_INC)
  202. conn->trans->conn_idcounter = RXRPC_CID_INC;
  203. real_conn_id = conn->trans->conn_idcounter;
  204. attempt_insertion:
  205. parent = NULL;
  206. p = &conn->trans->client_conns.rb_node;
  207. while (*p) {
  208. parent = *p;
  209. xconn = rb_entry(parent, struct rxrpc_connection, node);
  210. if (epoch < xconn->epoch)
  211. p = &(*p)->rb_left;
  212. else if (epoch > xconn->epoch)
  213. p = &(*p)->rb_right;
  214. else if (real_conn_id < xconn->real_conn_id)
  215. p = &(*p)->rb_left;
  216. else if (real_conn_id > xconn->real_conn_id)
  217. p = &(*p)->rb_right;
  218. else
  219. goto id_exists;
  220. }
  221. /* we've found a suitable hole - arrange for this connection to occupy
  222. * it */
  223. rb_link_node(&conn->node, parent, p);
  224. rb_insert_color(&conn->node, &conn->trans->client_conns);
  225. conn->real_conn_id = real_conn_id;
  226. conn->cid = htonl(real_conn_id);
  227. write_unlock_bh(&conn->trans->conn_lock);
  228. _leave(" [CONNID %x CID %x]", real_conn_id, ntohl(conn->cid));
  229. return;
  230. /* we found a connection with the proposed ID - walk the tree from that
  231. * point looking for the next unused ID */
  232. id_exists:
  233. for (;;) {
  234. real_conn_id += RXRPC_CID_INC;
  235. if (real_conn_id < RXRPC_CID_INC) {
  236. real_conn_id = RXRPC_CID_INC;
  237. conn->trans->conn_idcounter = real_conn_id;
  238. goto attempt_insertion;
  239. }
  240. parent = rb_next(parent);
  241. if (!parent)
  242. goto attempt_insertion;
  243. xconn = rb_entry(parent, struct rxrpc_connection, node);
  244. if (epoch < xconn->epoch ||
  245. real_conn_id < xconn->real_conn_id)
  246. goto attempt_insertion;
  247. }
  248. }
  249. /*
  250. * add a call to a connection's call-by-ID tree
  251. */
  252. static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn,
  253. struct rxrpc_call *call)
  254. {
  255. struct rxrpc_call *xcall;
  256. struct rb_node *parent, **p;
  257. __be32 call_id;
  258. write_lock_bh(&conn->lock);
  259. call_id = call->call_id;
  260. p = &conn->calls.rb_node;
  261. parent = NULL;
  262. while (*p) {
  263. parent = *p;
  264. xcall = rb_entry(parent, struct rxrpc_call, conn_node);
  265. if (call_id < xcall->call_id)
  266. p = &(*p)->rb_left;
  267. else if (call_id > xcall->call_id)
  268. p = &(*p)->rb_right;
  269. else
  270. BUG();
  271. }
  272. rb_link_node(&call->conn_node, parent, p);
  273. rb_insert_color(&call->conn_node, &conn->calls);
  274. write_unlock_bh(&conn->lock);
  275. }
  276. /*
  277. * connect a call on an exclusive connection
  278. */
  279. static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
  280. struct rxrpc_transport *trans,
  281. __be16 service_id,
  282. struct rxrpc_call *call,
  283. gfp_t gfp)
  284. {
  285. struct rxrpc_connection *conn;
  286. int chan, ret;
  287. _enter("");
  288. conn = rx->conn;
  289. if (!conn) {
  290. /* not yet present - create a candidate for a new connection
  291. * and then redo the check */
  292. conn = rxrpc_alloc_connection(gfp);
  293. if (!conn) {
  294. _leave(" = -ENOMEM");
  295. return -ENOMEM;
  296. }
  297. conn->trans = trans;
  298. conn->bundle = NULL;
  299. conn->service_id = service_id;
  300. conn->epoch = rxrpc_epoch;
  301. conn->in_clientflag = 0;
  302. conn->out_clientflag = RXRPC_CLIENT_INITIATED;
  303. conn->cid = 0;
  304. conn->state = RXRPC_CONN_CLIENT;
  305. conn->avail_calls = RXRPC_MAXCALLS - 1;
  306. conn->security_level = rx->min_sec_level;
  307. conn->key = key_get(rx->key);
  308. ret = rxrpc_init_client_conn_security(conn);
  309. if (ret < 0) {
  310. key_put(conn->key);
  311. kfree(conn);
  312. _leave(" = %d [key]", ret);
  313. return ret;
  314. }
  315. write_lock_bh(&rxrpc_connection_lock);
  316. list_add_tail(&conn->link, &rxrpc_connections);
  317. write_unlock_bh(&rxrpc_connection_lock);
  318. spin_lock(&trans->client_lock);
  319. atomic_inc(&trans->usage);
  320. _net("CONNECT EXCL new %d on TRANS %d",
  321. conn->debug_id, conn->trans->debug_id);
  322. rxrpc_assign_connection_id(conn);
  323. rx->conn = conn;
  324. }
  325. /* we've got a connection with a free channel and we can now attach the
  326. * call to it
  327. * - we're holding the transport's client lock
  328. * - we're holding a reference on the connection
  329. */
  330. for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
  331. if (!conn->channels[chan])
  332. goto found_channel;
  333. goto no_free_channels;
  334. found_channel:
  335. atomic_inc(&conn->usage);
  336. conn->channels[chan] = call;
  337. call->conn = conn;
  338. call->channel = chan;
  339. call->cid = conn->cid | htonl(chan);
  340. call->call_id = htonl(++conn->call_counter);
  341. _net("CONNECT client on conn %d chan %d as call %x",
  342. conn->debug_id, chan, ntohl(call->call_id));
  343. spin_unlock(&trans->client_lock);
  344. rxrpc_add_call_ID_to_conn(conn, call);
  345. _leave(" = 0");
  346. return 0;
  347. no_free_channels:
  348. spin_unlock(&trans->client_lock);
  349. _leave(" = -ENOSR");
  350. return -ENOSR;
  351. }
  352. /*
  353. * find a connection for a call
  354. * - called in process context with IRQs enabled
  355. */
  356. int rxrpc_connect_call(struct rxrpc_sock *rx,
  357. struct rxrpc_transport *trans,
  358. struct rxrpc_conn_bundle *bundle,
  359. struct rxrpc_call *call,
  360. gfp_t gfp)
  361. {
  362. struct rxrpc_connection *conn, *candidate;
  363. int chan, ret;
  364. DECLARE_WAITQUEUE(myself, current);
  365. _enter("%p,%lx,", rx, call->user_call_ID);
  366. if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags))
  367. return rxrpc_connect_exclusive(rx, trans, bundle->service_id,
  368. call, gfp);
  369. spin_lock(&trans->client_lock);
  370. for (;;) {
  371. /* see if the bundle has a call slot available */
  372. if (!list_empty(&bundle->avail_conns)) {
  373. _debug("avail");
  374. conn = list_entry(bundle->avail_conns.next,
  375. struct rxrpc_connection,
  376. bundle_link);
  377. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  378. list_del_init(&conn->bundle_link);
  379. bundle->num_conns--;
  380. continue;
  381. }
  382. if (--conn->avail_calls == 0)
  383. list_move(&conn->bundle_link,
  384. &bundle->busy_conns);
  385. ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
  386. ASSERT(conn->channels[0] == NULL ||
  387. conn->channels[1] == NULL ||
  388. conn->channels[2] == NULL ||
  389. conn->channels[3] == NULL);
  390. atomic_inc(&conn->usage);
  391. break;
  392. }
  393. if (!list_empty(&bundle->unused_conns)) {
  394. _debug("unused");
  395. conn = list_entry(bundle->unused_conns.next,
  396. struct rxrpc_connection,
  397. bundle_link);
  398. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  399. list_del_init(&conn->bundle_link);
  400. bundle->num_conns--;
  401. continue;
  402. }
  403. ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
  404. conn->avail_calls = RXRPC_MAXCALLS - 1;
  405. ASSERT(conn->channels[0] == NULL &&
  406. conn->channels[1] == NULL &&
  407. conn->channels[2] == NULL &&
  408. conn->channels[3] == NULL);
  409. atomic_inc(&conn->usage);
  410. list_move(&conn->bundle_link, &bundle->avail_conns);
  411. break;
  412. }
  413. /* need to allocate a new connection */
  414. _debug("get new conn [%d]", bundle->num_conns);
  415. spin_unlock(&trans->client_lock);
  416. if (signal_pending(current))
  417. goto interrupted;
  418. if (bundle->num_conns >= 20) {
  419. _debug("too many conns");
  420. if (!(gfp & __GFP_WAIT)) {
  421. _leave(" = -EAGAIN");
  422. return -EAGAIN;
  423. }
  424. add_wait_queue(&bundle->chanwait, &myself);
  425. for (;;) {
  426. set_current_state(TASK_INTERRUPTIBLE);
  427. if (bundle->num_conns < 20 ||
  428. !list_empty(&bundle->unused_conns) ||
  429. !list_empty(&bundle->avail_conns))
  430. break;
  431. if (signal_pending(current))
  432. goto interrupted_dequeue;
  433. schedule();
  434. }
  435. remove_wait_queue(&bundle->chanwait, &myself);
  436. __set_current_state(TASK_RUNNING);
  437. spin_lock(&trans->client_lock);
  438. continue;
  439. }
  440. /* not yet present - create a candidate for a new connection and then
  441. * redo the check */
  442. candidate = rxrpc_alloc_connection(gfp);
  443. if (!candidate) {
  444. _leave(" = -ENOMEM");
  445. return -ENOMEM;
  446. }
  447. candidate->trans = trans;
  448. candidate->bundle = bundle;
  449. candidate->service_id = bundle->service_id;
  450. candidate->epoch = rxrpc_epoch;
  451. candidate->in_clientflag = 0;
  452. candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
  453. candidate->cid = 0;
  454. candidate->state = RXRPC_CONN_CLIENT;
  455. candidate->avail_calls = RXRPC_MAXCALLS;
  456. candidate->security_level = rx->min_sec_level;
  457. candidate->key = key_get(bundle->key);
  458. ret = rxrpc_init_client_conn_security(candidate);
  459. if (ret < 0) {
  460. key_put(candidate->key);
  461. kfree(candidate);
  462. _leave(" = %d [key]", ret);
  463. return ret;
  464. }
  465. write_lock_bh(&rxrpc_connection_lock);
  466. list_add_tail(&candidate->link, &rxrpc_connections);
  467. write_unlock_bh(&rxrpc_connection_lock);
  468. spin_lock(&trans->client_lock);
  469. list_add(&candidate->bundle_link, &bundle->unused_conns);
  470. bundle->num_conns++;
  471. atomic_inc(&bundle->usage);
  472. atomic_inc(&trans->usage);
  473. _net("CONNECT new %d on TRANS %d",
  474. candidate->debug_id, candidate->trans->debug_id);
  475. rxrpc_assign_connection_id(candidate);
  476. if (candidate->security)
  477. candidate->security->prime_packet_security(candidate);
  478. /* leave the candidate lurking in zombie mode attached to the
  479. * bundle until we're ready for it */
  480. rxrpc_put_connection(candidate);
  481. candidate = NULL;
  482. }
  483. /* we've got a connection with a free channel and we can now attach the
  484. * call to it
  485. * - we're holding the transport's client lock
  486. * - we're holding a reference on the connection
  487. * - we're holding a reference on the bundle
  488. */
  489. for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
  490. if (!conn->channels[chan])
  491. goto found_channel;
  492. ASSERT(conn->channels[0] == NULL ||
  493. conn->channels[1] == NULL ||
  494. conn->channels[2] == NULL ||
  495. conn->channels[3] == NULL);
  496. BUG();
  497. found_channel:
  498. conn->channels[chan] = call;
  499. call->conn = conn;
  500. call->channel = chan;
  501. call->cid = conn->cid | htonl(chan);
  502. call->call_id = htonl(++conn->call_counter);
  503. _net("CONNECT client on conn %d chan %d as call %x",
  504. conn->debug_id, chan, ntohl(call->call_id));
  505. ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
  506. spin_unlock(&trans->client_lock);
  507. rxrpc_add_call_ID_to_conn(conn, call);
  508. _leave(" = 0");
  509. return 0;
  510. interrupted_dequeue:
  511. remove_wait_queue(&bundle->chanwait, &myself);
  512. __set_current_state(TASK_RUNNING);
  513. interrupted:
  514. _leave(" = -ERESTARTSYS");
  515. return -ERESTARTSYS;
  516. }
  517. /*
  518. * get a record of an incoming connection
  519. */
  520. struct rxrpc_connection *
  521. rxrpc_incoming_connection(struct rxrpc_transport *trans,
  522. struct rxrpc_header *hdr,
  523. gfp_t gfp)
  524. {
  525. struct rxrpc_connection *conn, *candidate = NULL;
  526. struct rb_node *p, **pp;
  527. const char *new = "old";
  528. __be32 epoch;
  529. u32 conn_id;
  530. _enter("");
  531. ASSERT(hdr->flags & RXRPC_CLIENT_INITIATED);
  532. epoch = hdr->epoch;
  533. conn_id = ntohl(hdr->cid) & RXRPC_CIDMASK;
  534. /* search the connection list first */
  535. read_lock_bh(&trans->conn_lock);
  536. p = trans->server_conns.rb_node;
  537. while (p) {
  538. conn = rb_entry(p, struct rxrpc_connection, node);
  539. _debug("maybe %x", conn->real_conn_id);
  540. if (epoch < conn->epoch)
  541. p = p->rb_left;
  542. else if (epoch > conn->epoch)
  543. p = p->rb_right;
  544. else if (conn_id < conn->real_conn_id)
  545. p = p->rb_left;
  546. else if (conn_id > conn->real_conn_id)
  547. p = p->rb_right;
  548. else
  549. goto found_extant_connection;
  550. }
  551. read_unlock_bh(&trans->conn_lock);
  552. /* not yet present - create a candidate for a new record and then
  553. * redo the search */
  554. candidate = rxrpc_alloc_connection(gfp);
  555. if (!candidate) {
  556. _leave(" = -ENOMEM");
  557. return ERR_PTR(-ENOMEM);
  558. }
  559. candidate->trans = trans;
  560. candidate->epoch = hdr->epoch;
  561. candidate->cid = hdr->cid & cpu_to_be32(RXRPC_CIDMASK);
  562. candidate->service_id = hdr->serviceId;
  563. candidate->security_ix = hdr->securityIndex;
  564. candidate->in_clientflag = RXRPC_CLIENT_INITIATED;
  565. candidate->out_clientflag = 0;
  566. candidate->real_conn_id = conn_id;
  567. candidate->state = RXRPC_CONN_SERVER;
  568. if (candidate->service_id)
  569. candidate->state = RXRPC_CONN_SERVER_UNSECURED;
  570. write_lock_bh(&trans->conn_lock);
  571. pp = &trans->server_conns.rb_node;
  572. p = NULL;
  573. while (*pp) {
  574. p = *pp;
  575. conn = rb_entry(p, struct rxrpc_connection, node);
  576. if (epoch < conn->epoch)
  577. pp = &(*pp)->rb_left;
  578. else if (epoch > conn->epoch)
  579. pp = &(*pp)->rb_right;
  580. else if (conn_id < conn->real_conn_id)
  581. pp = &(*pp)->rb_left;
  582. else if (conn_id > conn->real_conn_id)
  583. pp = &(*pp)->rb_right;
  584. else
  585. goto found_extant_second;
  586. }
  587. /* we can now add the new candidate to the list */
  588. conn = candidate;
  589. candidate = NULL;
  590. rb_link_node(&conn->node, p, pp);
  591. rb_insert_color(&conn->node, &trans->server_conns);
  592. atomic_inc(&conn->trans->usage);
  593. write_unlock_bh(&trans->conn_lock);
  594. write_lock_bh(&rxrpc_connection_lock);
  595. list_add_tail(&conn->link, &rxrpc_connections);
  596. write_unlock_bh(&rxrpc_connection_lock);
  597. new = "new";
  598. success:
  599. _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->real_conn_id);
  600. _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
  601. return conn;
  602. /* we found the connection in the list immediately */
  603. found_extant_connection:
  604. if (hdr->securityIndex != conn->security_ix) {
  605. read_unlock_bh(&trans->conn_lock);
  606. goto security_mismatch;
  607. }
  608. atomic_inc(&conn->usage);
  609. read_unlock_bh(&trans->conn_lock);
  610. goto success;
  611. /* we found the connection on the second time through the list */
  612. found_extant_second:
  613. if (hdr->securityIndex != conn->security_ix) {
  614. write_unlock_bh(&trans->conn_lock);
  615. goto security_mismatch;
  616. }
  617. atomic_inc(&conn->usage);
  618. write_unlock_bh(&trans->conn_lock);
  619. kfree(candidate);
  620. goto success;
  621. security_mismatch:
  622. kfree(candidate);
  623. _leave(" = -EKEYREJECTED");
  624. return ERR_PTR(-EKEYREJECTED);
  625. }
  626. /*
  627. * find a connection based on transport and RxRPC connection ID for an incoming
  628. * packet
  629. */
  630. struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
  631. struct rxrpc_header *hdr)
  632. {
  633. struct rxrpc_connection *conn;
  634. struct rb_node *p;
  635. __be32 epoch;
  636. u32 conn_id;
  637. _enter(",{%x,%x}", ntohl(hdr->cid), hdr->flags);
  638. read_lock_bh(&trans->conn_lock);
  639. conn_id = ntohl(hdr->cid) & RXRPC_CIDMASK;
  640. epoch = hdr->epoch;
  641. if (hdr->flags & RXRPC_CLIENT_INITIATED)
  642. p = trans->server_conns.rb_node;
  643. else
  644. p = trans->client_conns.rb_node;
  645. while (p) {
  646. conn = rb_entry(p, struct rxrpc_connection, node);
  647. _debug("maybe %x", conn->real_conn_id);
  648. if (epoch < conn->epoch)
  649. p = p->rb_left;
  650. else if (epoch > conn->epoch)
  651. p = p->rb_right;
  652. else if (conn_id < conn->real_conn_id)
  653. p = p->rb_left;
  654. else if (conn_id > conn->real_conn_id)
  655. p = p->rb_right;
  656. else
  657. goto found;
  658. }
  659. read_unlock_bh(&trans->conn_lock);
  660. _leave(" = NULL");
  661. return NULL;
  662. found:
  663. atomic_inc(&conn->usage);
  664. read_unlock_bh(&trans->conn_lock);
  665. _leave(" = %p", conn);
  666. return conn;
  667. }
  668. /*
  669. * release a virtual connection
  670. */
  671. void rxrpc_put_connection(struct rxrpc_connection *conn)
  672. {
  673. _enter("%p{u=%d,d=%d}",
  674. conn, atomic_read(&conn->usage), conn->debug_id);
  675. ASSERTCMP(atomic_read(&conn->usage), >, 0);
  676. conn->put_time = get_seconds();
  677. if (atomic_dec_and_test(&conn->usage)) {
  678. _debug("zombie");
  679. rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
  680. }
  681. _leave("");
  682. }
  683. /*
  684. * destroy a virtual connection
  685. */
  686. static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
  687. {
  688. _enter("%p{%d}", conn, atomic_read(&conn->usage));
  689. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  690. _net("DESTROY CONN %d", conn->debug_id);
  691. if (conn->bundle)
  692. rxrpc_put_bundle(conn->trans, conn->bundle);
  693. ASSERT(RB_EMPTY_ROOT(&conn->calls));
  694. rxrpc_purge_queue(&conn->rx_queue);
  695. rxrpc_clear_conn_security(conn);
  696. rxrpc_put_transport(conn->trans);
  697. kfree(conn);
  698. _leave("");
  699. }
  700. /*
  701. * reap dead connections
  702. */
  703. static void rxrpc_connection_reaper(struct work_struct *work)
  704. {
  705. struct rxrpc_connection *conn, *_p;
  706. unsigned long now, earliest, reap_time;
  707. LIST_HEAD(graveyard);
  708. _enter("");
  709. now = get_seconds();
  710. earliest = ULONG_MAX;
  711. write_lock_bh(&rxrpc_connection_lock);
  712. list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
  713. _debug("reap CONN %d { u=%d,t=%ld }",
  714. conn->debug_id, atomic_read(&conn->usage),
  715. (long) now - (long) conn->put_time);
  716. if (likely(atomic_read(&conn->usage) > 0))
  717. continue;
  718. spin_lock(&conn->trans->client_lock);
  719. write_lock(&conn->trans->conn_lock);
  720. reap_time = conn->put_time + rxrpc_connection_timeout;
  721. if (atomic_read(&conn->usage) > 0) {
  722. ;
  723. } else if (reap_time <= now) {
  724. list_move_tail(&conn->link, &graveyard);
  725. if (conn->out_clientflag)
  726. rb_erase(&conn->node,
  727. &conn->trans->client_conns);
  728. else
  729. rb_erase(&conn->node,
  730. &conn->trans->server_conns);
  731. if (conn->bundle) {
  732. list_del_init(&conn->bundle_link);
  733. conn->bundle->num_conns--;
  734. }
  735. } else if (reap_time < earliest) {
  736. earliest = reap_time;
  737. }
  738. write_unlock(&conn->trans->conn_lock);
  739. spin_unlock(&conn->trans->client_lock);
  740. }
  741. write_unlock_bh(&rxrpc_connection_lock);
  742. if (earliest != ULONG_MAX) {
  743. _debug("reschedule reaper %ld", (long) earliest - now);
  744. ASSERTCMP(earliest, >, now);
  745. rxrpc_queue_delayed_work(&rxrpc_connection_reap,
  746. (earliest - now) * HZ);
  747. }
  748. /* then destroy all those pulled out */
  749. while (!list_empty(&graveyard)) {
  750. conn = list_entry(graveyard.next, struct rxrpc_connection,
  751. link);
  752. list_del_init(&conn->link);
  753. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  754. rxrpc_destroy_connection(conn);
  755. }
  756. _leave("");
  757. }
  758. /*
  759. * preemptively destroy all the connection records rather than waiting for them
  760. * to time out
  761. */
  762. void __exit rxrpc_destroy_all_connections(void)
  763. {
  764. _enter("");
  765. rxrpc_connection_timeout = 0;
  766. cancel_delayed_work(&rxrpc_connection_reap);
  767. rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
  768. _leave("");
  769. }