ar-connection.c 23 KB

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