ar-connection.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. } else {
  326. spin_lock(&trans->client_lock);
  327. }
  328. /* we've got a connection with a free channel and we can now attach the
  329. * call to it
  330. * - we're holding the transport's client lock
  331. * - we're holding a reference on the connection
  332. */
  333. for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
  334. if (!conn->channels[chan])
  335. goto found_channel;
  336. goto no_free_channels;
  337. found_channel:
  338. atomic_inc(&conn->usage);
  339. conn->channels[chan] = call;
  340. call->conn = conn;
  341. call->channel = chan;
  342. call->cid = conn->cid | htonl(chan);
  343. call->call_id = htonl(++conn->call_counter);
  344. _net("CONNECT client on conn %d chan %d as call %x",
  345. conn->debug_id, chan, ntohl(call->call_id));
  346. spin_unlock(&trans->client_lock);
  347. rxrpc_add_call_ID_to_conn(conn, call);
  348. _leave(" = 0");
  349. return 0;
  350. no_free_channels:
  351. spin_unlock(&trans->client_lock);
  352. _leave(" = -ENOSR");
  353. return -ENOSR;
  354. }
  355. /*
  356. * find a connection for a call
  357. * - called in process context with IRQs enabled
  358. */
  359. int rxrpc_connect_call(struct rxrpc_sock *rx,
  360. struct rxrpc_transport *trans,
  361. struct rxrpc_conn_bundle *bundle,
  362. struct rxrpc_call *call,
  363. gfp_t gfp)
  364. {
  365. struct rxrpc_connection *conn, *candidate;
  366. int chan, ret;
  367. DECLARE_WAITQUEUE(myself, current);
  368. _enter("%p,%lx,", rx, call->user_call_ID);
  369. if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags))
  370. return rxrpc_connect_exclusive(rx, trans, bundle->service_id,
  371. call, gfp);
  372. spin_lock(&trans->client_lock);
  373. for (;;) {
  374. /* see if the bundle has a call slot available */
  375. if (!list_empty(&bundle->avail_conns)) {
  376. _debug("avail");
  377. conn = list_entry(bundle->avail_conns.next,
  378. struct rxrpc_connection,
  379. bundle_link);
  380. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  381. list_del_init(&conn->bundle_link);
  382. bundle->num_conns--;
  383. continue;
  384. }
  385. if (--conn->avail_calls == 0)
  386. list_move(&conn->bundle_link,
  387. &bundle->busy_conns);
  388. ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
  389. ASSERT(conn->channels[0] == NULL ||
  390. conn->channels[1] == NULL ||
  391. conn->channels[2] == NULL ||
  392. conn->channels[3] == NULL);
  393. atomic_inc(&conn->usage);
  394. break;
  395. }
  396. if (!list_empty(&bundle->unused_conns)) {
  397. _debug("unused");
  398. conn = list_entry(bundle->unused_conns.next,
  399. struct rxrpc_connection,
  400. bundle_link);
  401. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  402. list_del_init(&conn->bundle_link);
  403. bundle->num_conns--;
  404. continue;
  405. }
  406. ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
  407. conn->avail_calls = RXRPC_MAXCALLS - 1;
  408. ASSERT(conn->channels[0] == NULL &&
  409. conn->channels[1] == NULL &&
  410. conn->channels[2] == NULL &&
  411. conn->channels[3] == NULL);
  412. atomic_inc(&conn->usage);
  413. list_move(&conn->bundle_link, &bundle->avail_conns);
  414. break;
  415. }
  416. /* need to allocate a new connection */
  417. _debug("get new conn [%d]", bundle->num_conns);
  418. spin_unlock(&trans->client_lock);
  419. if (signal_pending(current))
  420. goto interrupted;
  421. if (bundle->num_conns >= 20) {
  422. _debug("too many conns");
  423. if (!(gfp & __GFP_WAIT)) {
  424. _leave(" = -EAGAIN");
  425. return -EAGAIN;
  426. }
  427. add_wait_queue(&bundle->chanwait, &myself);
  428. for (;;) {
  429. set_current_state(TASK_INTERRUPTIBLE);
  430. if (bundle->num_conns < 20 ||
  431. !list_empty(&bundle->unused_conns) ||
  432. !list_empty(&bundle->avail_conns))
  433. break;
  434. if (signal_pending(current))
  435. goto interrupted_dequeue;
  436. schedule();
  437. }
  438. remove_wait_queue(&bundle->chanwait, &myself);
  439. __set_current_state(TASK_RUNNING);
  440. spin_lock(&trans->client_lock);
  441. continue;
  442. }
  443. /* not yet present - create a candidate for a new connection and then
  444. * redo the check */
  445. candidate = rxrpc_alloc_connection(gfp);
  446. if (!candidate) {
  447. _leave(" = -ENOMEM");
  448. return -ENOMEM;
  449. }
  450. candidate->trans = trans;
  451. candidate->bundle = bundle;
  452. candidate->service_id = bundle->service_id;
  453. candidate->epoch = rxrpc_epoch;
  454. candidate->in_clientflag = 0;
  455. candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
  456. candidate->cid = 0;
  457. candidate->state = RXRPC_CONN_CLIENT;
  458. candidate->avail_calls = RXRPC_MAXCALLS;
  459. candidate->security_level = rx->min_sec_level;
  460. candidate->key = key_get(bundle->key);
  461. ret = rxrpc_init_client_conn_security(candidate);
  462. if (ret < 0) {
  463. key_put(candidate->key);
  464. kfree(candidate);
  465. _leave(" = %d [key]", ret);
  466. return ret;
  467. }
  468. write_lock_bh(&rxrpc_connection_lock);
  469. list_add_tail(&candidate->link, &rxrpc_connections);
  470. write_unlock_bh(&rxrpc_connection_lock);
  471. spin_lock(&trans->client_lock);
  472. list_add(&candidate->bundle_link, &bundle->unused_conns);
  473. bundle->num_conns++;
  474. atomic_inc(&bundle->usage);
  475. atomic_inc(&trans->usage);
  476. _net("CONNECT new %d on TRANS %d",
  477. candidate->debug_id, candidate->trans->debug_id);
  478. rxrpc_assign_connection_id(candidate);
  479. if (candidate->security)
  480. candidate->security->prime_packet_security(candidate);
  481. /* leave the candidate lurking in zombie mode attached to the
  482. * bundle until we're ready for it */
  483. rxrpc_put_connection(candidate);
  484. candidate = NULL;
  485. }
  486. /* we've got a connection with a free channel and we can now attach the
  487. * call to it
  488. * - we're holding the transport's client lock
  489. * - we're holding a reference on the connection
  490. * - we're holding a reference on the bundle
  491. */
  492. for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
  493. if (!conn->channels[chan])
  494. goto found_channel;
  495. ASSERT(conn->channels[0] == NULL ||
  496. conn->channels[1] == NULL ||
  497. conn->channels[2] == NULL ||
  498. conn->channels[3] == NULL);
  499. BUG();
  500. found_channel:
  501. conn->channels[chan] = call;
  502. call->conn = conn;
  503. call->channel = chan;
  504. call->cid = conn->cid | htonl(chan);
  505. call->call_id = htonl(++conn->call_counter);
  506. _net("CONNECT client on conn %d chan %d as call %x",
  507. conn->debug_id, chan, ntohl(call->call_id));
  508. ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
  509. spin_unlock(&trans->client_lock);
  510. rxrpc_add_call_ID_to_conn(conn, call);
  511. _leave(" = 0");
  512. return 0;
  513. interrupted_dequeue:
  514. remove_wait_queue(&bundle->chanwait, &myself);
  515. __set_current_state(TASK_RUNNING);
  516. interrupted:
  517. _leave(" = -ERESTARTSYS");
  518. return -ERESTARTSYS;
  519. }
  520. /*
  521. * get a record of an incoming connection
  522. */
  523. struct rxrpc_connection *
  524. rxrpc_incoming_connection(struct rxrpc_transport *trans,
  525. struct rxrpc_header *hdr,
  526. gfp_t gfp)
  527. {
  528. struct rxrpc_connection *conn, *candidate = NULL;
  529. struct rb_node *p, **pp;
  530. const char *new = "old";
  531. __be32 epoch;
  532. u32 conn_id;
  533. _enter("");
  534. ASSERT(hdr->flags & RXRPC_CLIENT_INITIATED);
  535. epoch = hdr->epoch;
  536. conn_id = ntohl(hdr->cid) & RXRPC_CIDMASK;
  537. /* search the connection list first */
  538. read_lock_bh(&trans->conn_lock);
  539. p = trans->server_conns.rb_node;
  540. while (p) {
  541. conn = rb_entry(p, struct rxrpc_connection, node);
  542. _debug("maybe %x", conn->real_conn_id);
  543. if (epoch < conn->epoch)
  544. p = p->rb_left;
  545. else if (epoch > conn->epoch)
  546. p = p->rb_right;
  547. else if (conn_id < conn->real_conn_id)
  548. p = p->rb_left;
  549. else if (conn_id > conn->real_conn_id)
  550. p = p->rb_right;
  551. else
  552. goto found_extant_connection;
  553. }
  554. read_unlock_bh(&trans->conn_lock);
  555. /* not yet present - create a candidate for a new record and then
  556. * redo the search */
  557. candidate = rxrpc_alloc_connection(gfp);
  558. if (!candidate) {
  559. _leave(" = -ENOMEM");
  560. return ERR_PTR(-ENOMEM);
  561. }
  562. candidate->trans = trans;
  563. candidate->epoch = hdr->epoch;
  564. candidate->cid = hdr->cid & cpu_to_be32(RXRPC_CIDMASK);
  565. candidate->service_id = hdr->serviceId;
  566. candidate->security_ix = hdr->securityIndex;
  567. candidate->in_clientflag = RXRPC_CLIENT_INITIATED;
  568. candidate->out_clientflag = 0;
  569. candidate->real_conn_id = conn_id;
  570. candidate->state = RXRPC_CONN_SERVER;
  571. if (candidate->service_id)
  572. candidate->state = RXRPC_CONN_SERVER_UNSECURED;
  573. write_lock_bh(&trans->conn_lock);
  574. pp = &trans->server_conns.rb_node;
  575. p = NULL;
  576. while (*pp) {
  577. p = *pp;
  578. conn = rb_entry(p, struct rxrpc_connection, node);
  579. if (epoch < conn->epoch)
  580. pp = &(*pp)->rb_left;
  581. else if (epoch > conn->epoch)
  582. pp = &(*pp)->rb_right;
  583. else if (conn_id < conn->real_conn_id)
  584. pp = &(*pp)->rb_left;
  585. else if (conn_id > conn->real_conn_id)
  586. pp = &(*pp)->rb_right;
  587. else
  588. goto found_extant_second;
  589. }
  590. /* we can now add the new candidate to the list */
  591. conn = candidate;
  592. candidate = NULL;
  593. rb_link_node(&conn->node, p, pp);
  594. rb_insert_color(&conn->node, &trans->server_conns);
  595. atomic_inc(&conn->trans->usage);
  596. write_unlock_bh(&trans->conn_lock);
  597. write_lock_bh(&rxrpc_connection_lock);
  598. list_add_tail(&conn->link, &rxrpc_connections);
  599. write_unlock_bh(&rxrpc_connection_lock);
  600. new = "new";
  601. success:
  602. _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->real_conn_id);
  603. _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
  604. return conn;
  605. /* we found the connection in the list immediately */
  606. found_extant_connection:
  607. if (hdr->securityIndex != conn->security_ix) {
  608. read_unlock_bh(&trans->conn_lock);
  609. goto security_mismatch;
  610. }
  611. atomic_inc(&conn->usage);
  612. read_unlock_bh(&trans->conn_lock);
  613. goto success;
  614. /* we found the connection on the second time through the list */
  615. found_extant_second:
  616. if (hdr->securityIndex != conn->security_ix) {
  617. write_unlock_bh(&trans->conn_lock);
  618. goto security_mismatch;
  619. }
  620. atomic_inc(&conn->usage);
  621. write_unlock_bh(&trans->conn_lock);
  622. kfree(candidate);
  623. goto success;
  624. security_mismatch:
  625. kfree(candidate);
  626. _leave(" = -EKEYREJECTED");
  627. return ERR_PTR(-EKEYREJECTED);
  628. }
  629. /*
  630. * find a connection based on transport and RxRPC connection ID for an incoming
  631. * packet
  632. */
  633. struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
  634. struct rxrpc_header *hdr)
  635. {
  636. struct rxrpc_connection *conn;
  637. struct rb_node *p;
  638. __be32 epoch;
  639. u32 conn_id;
  640. _enter(",{%x,%x}", ntohl(hdr->cid), hdr->flags);
  641. read_lock_bh(&trans->conn_lock);
  642. conn_id = ntohl(hdr->cid) & RXRPC_CIDMASK;
  643. epoch = hdr->epoch;
  644. if (hdr->flags & RXRPC_CLIENT_INITIATED)
  645. p = trans->server_conns.rb_node;
  646. else
  647. p = trans->client_conns.rb_node;
  648. while (p) {
  649. conn = rb_entry(p, struct rxrpc_connection, node);
  650. _debug("maybe %x", conn->real_conn_id);
  651. if (epoch < conn->epoch)
  652. p = p->rb_left;
  653. else if (epoch > conn->epoch)
  654. p = p->rb_right;
  655. else if (conn_id < conn->real_conn_id)
  656. p = p->rb_left;
  657. else if (conn_id > conn->real_conn_id)
  658. p = p->rb_right;
  659. else
  660. goto found;
  661. }
  662. read_unlock_bh(&trans->conn_lock);
  663. _leave(" = NULL");
  664. return NULL;
  665. found:
  666. atomic_inc(&conn->usage);
  667. read_unlock_bh(&trans->conn_lock);
  668. _leave(" = %p", conn);
  669. return conn;
  670. }
  671. /*
  672. * release a virtual connection
  673. */
  674. void rxrpc_put_connection(struct rxrpc_connection *conn)
  675. {
  676. _enter("%p{u=%d,d=%d}",
  677. conn, atomic_read(&conn->usage), conn->debug_id);
  678. ASSERTCMP(atomic_read(&conn->usage), >, 0);
  679. conn->put_time = get_seconds();
  680. if (atomic_dec_and_test(&conn->usage)) {
  681. _debug("zombie");
  682. rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
  683. }
  684. _leave("");
  685. }
  686. /*
  687. * destroy a virtual connection
  688. */
  689. static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
  690. {
  691. _enter("%p{%d}", conn, atomic_read(&conn->usage));
  692. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  693. _net("DESTROY CONN %d", conn->debug_id);
  694. if (conn->bundle)
  695. rxrpc_put_bundle(conn->trans, conn->bundle);
  696. ASSERT(RB_EMPTY_ROOT(&conn->calls));
  697. rxrpc_purge_queue(&conn->rx_queue);
  698. rxrpc_clear_conn_security(conn);
  699. rxrpc_put_transport(conn->trans);
  700. kfree(conn);
  701. _leave("");
  702. }
  703. /*
  704. * reap dead connections
  705. */
  706. static void rxrpc_connection_reaper(struct work_struct *work)
  707. {
  708. struct rxrpc_connection *conn, *_p;
  709. unsigned long now, earliest, reap_time;
  710. LIST_HEAD(graveyard);
  711. _enter("");
  712. now = get_seconds();
  713. earliest = ULONG_MAX;
  714. write_lock_bh(&rxrpc_connection_lock);
  715. list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
  716. _debug("reap CONN %d { u=%d,t=%ld }",
  717. conn->debug_id, atomic_read(&conn->usage),
  718. (long) now - (long) conn->put_time);
  719. if (likely(atomic_read(&conn->usage) > 0))
  720. continue;
  721. spin_lock(&conn->trans->client_lock);
  722. write_lock(&conn->trans->conn_lock);
  723. reap_time = conn->put_time + rxrpc_connection_timeout;
  724. if (atomic_read(&conn->usage) > 0) {
  725. ;
  726. } else if (reap_time <= now) {
  727. list_move_tail(&conn->link, &graveyard);
  728. if (conn->out_clientflag)
  729. rb_erase(&conn->node,
  730. &conn->trans->client_conns);
  731. else
  732. rb_erase(&conn->node,
  733. &conn->trans->server_conns);
  734. if (conn->bundle) {
  735. list_del_init(&conn->bundle_link);
  736. conn->bundle->num_conns--;
  737. }
  738. } else if (reap_time < earliest) {
  739. earliest = reap_time;
  740. }
  741. write_unlock(&conn->trans->conn_lock);
  742. spin_unlock(&conn->trans->client_lock);
  743. }
  744. write_unlock_bh(&rxrpc_connection_lock);
  745. if (earliest != ULONG_MAX) {
  746. _debug("reschedule reaper %ld", (long) earliest - now);
  747. ASSERTCMP(earliest, >, now);
  748. rxrpc_queue_delayed_work(&rxrpc_connection_reap,
  749. (earliest - now) * HZ);
  750. }
  751. /* then destroy all those pulled out */
  752. while (!list_empty(&graveyard)) {
  753. conn = list_entry(graveyard.next, struct rxrpc_connection,
  754. link);
  755. list_del_init(&conn->link);
  756. ASSERTCMP(atomic_read(&conn->usage), ==, 0);
  757. rxrpc_destroy_connection(conn);
  758. }
  759. _leave("");
  760. }
  761. /*
  762. * preemptively destroy all the connection records rather than waiting for them
  763. * to time out
  764. */
  765. void __exit rxrpc_destroy_all_connections(void)
  766. {
  767. _enter("");
  768. rxrpc_connection_timeout = 0;
  769. cancel_delayed_work(&rxrpc_connection_reap);
  770. rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
  771. _leave("");
  772. }