connection.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /* connection.c: Rx connection routines
  2. *
  3. * Copyright (C) 2002 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/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <rxrpc/rxrpc.h>
  15. #include <rxrpc/transport.h>
  16. #include <rxrpc/peer.h>
  17. #include <rxrpc/connection.h>
  18. #include <rxrpc/call.h>
  19. #include <rxrpc/message.h>
  20. #include <linux/udp.h>
  21. #include <linux/ip.h>
  22. #include <net/sock.h>
  23. #include <asm/uaccess.h>
  24. #include "internal.h"
  25. __RXACCT_DECL(atomic_t rxrpc_connection_count);
  26. LIST_HEAD(rxrpc_conns);
  27. DECLARE_RWSEM(rxrpc_conns_sem);
  28. unsigned long rxrpc_conn_timeout = 60 * 60;
  29. static void rxrpc_conn_do_timeout(struct rxrpc_connection *conn);
  30. static void __rxrpc_conn_timeout(rxrpc_timer_t *timer)
  31. {
  32. struct rxrpc_connection *conn =
  33. list_entry(timer, struct rxrpc_connection, timeout);
  34. _debug("Rx CONN TIMEOUT [%p{u=%d}]", conn, atomic_read(&conn->usage));
  35. rxrpc_conn_do_timeout(conn);
  36. }
  37. static const struct rxrpc_timer_ops rxrpc_conn_timer_ops = {
  38. .timed_out = __rxrpc_conn_timeout,
  39. };
  40. /*****************************************************************************/
  41. /*
  42. * create a new connection record
  43. */
  44. static inline int __rxrpc_create_connection(struct rxrpc_peer *peer,
  45. struct rxrpc_connection **_conn)
  46. {
  47. struct rxrpc_connection *conn;
  48. _enter("%p",peer);
  49. /* allocate and initialise a connection record */
  50. conn = kmalloc(sizeof(struct rxrpc_connection), GFP_KERNEL);
  51. if (!conn) {
  52. _leave(" = -ENOMEM");
  53. return -ENOMEM;
  54. }
  55. memset(conn, 0, sizeof(struct rxrpc_connection));
  56. atomic_set(&conn->usage, 1);
  57. INIT_LIST_HEAD(&conn->link);
  58. INIT_LIST_HEAD(&conn->id_link);
  59. init_waitqueue_head(&conn->chanwait);
  60. spin_lock_init(&conn->lock);
  61. rxrpc_timer_init(&conn->timeout, &rxrpc_conn_timer_ops);
  62. do_gettimeofday(&conn->atime);
  63. conn->mtu_size = 1024;
  64. conn->peer = peer;
  65. conn->trans = peer->trans;
  66. __RXACCT(atomic_inc(&rxrpc_connection_count));
  67. *_conn = conn;
  68. _leave(" = 0 (%p)", conn);
  69. return 0;
  70. } /* end __rxrpc_create_connection() */
  71. /*****************************************************************************/
  72. /*
  73. * create a new connection record for outgoing connections
  74. */
  75. int rxrpc_create_connection(struct rxrpc_transport *trans,
  76. __be16 port,
  77. __be32 addr,
  78. uint16_t service_id,
  79. void *security,
  80. struct rxrpc_connection **_conn)
  81. {
  82. struct rxrpc_connection *candidate, *conn;
  83. struct rxrpc_peer *peer;
  84. struct list_head *_p;
  85. __be32 connid;
  86. int ret;
  87. _enter("%p{%hu},%u,%hu", trans, trans->port, ntohs(port), service_id);
  88. /* get a peer record */
  89. ret = rxrpc_peer_lookup(trans, addr, &peer);
  90. if (ret < 0) {
  91. _leave(" = %d", ret);
  92. return ret;
  93. }
  94. /* allocate and initialise a connection record */
  95. ret = __rxrpc_create_connection(peer, &candidate);
  96. if (ret < 0) {
  97. rxrpc_put_peer(peer);
  98. _leave(" = %d", ret);
  99. return ret;
  100. }
  101. /* fill in the specific bits */
  102. candidate->addr.sin_family = AF_INET;
  103. candidate->addr.sin_port = port;
  104. candidate->addr.sin_addr.s_addr = addr;
  105. candidate->in_epoch = rxrpc_epoch;
  106. candidate->out_epoch = rxrpc_epoch;
  107. candidate->in_clientflag = 0;
  108. candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
  109. candidate->service_id = htons(service_id);
  110. /* invent a unique connection ID */
  111. write_lock(&peer->conn_idlock);
  112. try_next_id:
  113. connid = htonl(peer->conn_idcounter & RXRPC_CIDMASK);
  114. peer->conn_idcounter += RXRPC_MAXCALLS;
  115. list_for_each(_p, &peer->conn_idlist) {
  116. conn = list_entry(_p, struct rxrpc_connection, id_link);
  117. if (connid == conn->conn_id)
  118. goto try_next_id;
  119. if (connid > conn->conn_id)
  120. break;
  121. }
  122. _debug("selected candidate conn ID %x.%u",
  123. ntohl(peer->addr.s_addr), ntohl(connid));
  124. candidate->conn_id = connid;
  125. list_add_tail(&candidate->id_link, _p);
  126. write_unlock(&peer->conn_idlock);
  127. /* attach to peer */
  128. candidate->peer = peer;
  129. write_lock(&peer->conn_lock);
  130. /* search the peer's transport graveyard list */
  131. spin_lock(&peer->conn_gylock);
  132. list_for_each(_p, &peer->conn_graveyard) {
  133. conn = list_entry(_p, struct rxrpc_connection, link);
  134. if (conn->addr.sin_port == candidate->addr.sin_port &&
  135. conn->security_ix == candidate->security_ix &&
  136. conn->service_id == candidate->service_id &&
  137. conn->in_clientflag == 0)
  138. goto found_in_graveyard;
  139. }
  140. spin_unlock(&peer->conn_gylock);
  141. /* pick the new candidate */
  142. _debug("created connection: {%08x} [out]", ntohl(candidate->conn_id));
  143. atomic_inc(&peer->conn_count);
  144. conn = candidate;
  145. candidate = NULL;
  146. make_active:
  147. list_add_tail(&conn->link, &peer->conn_active);
  148. write_unlock(&peer->conn_lock);
  149. if (candidate) {
  150. write_lock(&peer->conn_idlock);
  151. list_del(&candidate->id_link);
  152. write_unlock(&peer->conn_idlock);
  153. __RXACCT(atomic_dec(&rxrpc_connection_count));
  154. kfree(candidate);
  155. }
  156. else {
  157. down_write(&rxrpc_conns_sem);
  158. list_add_tail(&conn->proc_link, &rxrpc_conns);
  159. up_write(&rxrpc_conns_sem);
  160. }
  161. *_conn = conn;
  162. _leave(" = 0 (%p)", conn);
  163. return 0;
  164. /* handle resurrecting a connection from the graveyard */
  165. found_in_graveyard:
  166. _debug("resurrecting connection: {%08x} [out]", ntohl(conn->conn_id));
  167. rxrpc_get_connection(conn);
  168. rxrpc_krxtimod_del_timer(&conn->timeout);
  169. list_del_init(&conn->link);
  170. spin_unlock(&peer->conn_gylock);
  171. goto make_active;
  172. } /* end rxrpc_create_connection() */
  173. /*****************************************************************************/
  174. /*
  175. * lookup the connection for an incoming packet
  176. * - create a new connection record for unrecorded incoming connections
  177. */
  178. int rxrpc_connection_lookup(struct rxrpc_peer *peer,
  179. struct rxrpc_message *msg,
  180. struct rxrpc_connection **_conn)
  181. {
  182. struct rxrpc_connection *conn, *candidate = NULL;
  183. struct list_head *_p;
  184. struct sk_buff *pkt = msg->pkt;
  185. int ret, fresh = 0;
  186. __be32 x_epoch, x_connid;
  187. __be16 x_port, x_servid;
  188. __u32 x_secix;
  189. u8 x_clflag;
  190. _enter("%p{{%hu}},%u,%hu",
  191. peer,
  192. peer->trans->port,
  193. ntohs(pkt->h.uh->source),
  194. ntohs(msg->hdr.serviceId));
  195. x_port = pkt->h.uh->source;
  196. x_epoch = msg->hdr.epoch;
  197. x_clflag = msg->hdr.flags & RXRPC_CLIENT_INITIATED;
  198. x_connid = htonl(ntohl(msg->hdr.cid) & RXRPC_CIDMASK);
  199. x_servid = msg->hdr.serviceId;
  200. x_secix = msg->hdr.securityIndex;
  201. /* [common case] search the transport's active list first */
  202. read_lock(&peer->conn_lock);
  203. list_for_each(_p, &peer->conn_active) {
  204. conn = list_entry(_p, struct rxrpc_connection, link);
  205. if (conn->addr.sin_port == x_port &&
  206. conn->in_epoch == x_epoch &&
  207. conn->conn_id == x_connid &&
  208. conn->security_ix == x_secix &&
  209. conn->service_id == x_servid &&
  210. conn->in_clientflag == x_clflag)
  211. goto found_active;
  212. }
  213. read_unlock(&peer->conn_lock);
  214. /* [uncommon case] not active
  215. * - create a candidate for a new record if an inbound connection
  216. * - only examine the graveyard for an outbound connection
  217. */
  218. if (x_clflag) {
  219. ret = __rxrpc_create_connection(peer, &candidate);
  220. if (ret < 0) {
  221. _leave(" = %d", ret);
  222. return ret;
  223. }
  224. /* fill in the specifics */
  225. candidate->addr.sin_family = AF_INET;
  226. candidate->addr.sin_port = x_port;
  227. candidate->addr.sin_addr.s_addr = pkt->nh.iph->saddr;
  228. candidate->in_epoch = x_epoch;
  229. candidate->out_epoch = x_epoch;
  230. candidate->in_clientflag = RXRPC_CLIENT_INITIATED;
  231. candidate->out_clientflag = 0;
  232. candidate->conn_id = x_connid;
  233. candidate->service_id = x_servid;
  234. candidate->security_ix = x_secix;
  235. }
  236. /* search the active list again, just in case it appeared whilst we
  237. * were busy */
  238. write_lock(&peer->conn_lock);
  239. list_for_each(_p, &peer->conn_active) {
  240. conn = list_entry(_p, struct rxrpc_connection, link);
  241. if (conn->addr.sin_port == x_port &&
  242. conn->in_epoch == x_epoch &&
  243. conn->conn_id == x_connid &&
  244. conn->security_ix == x_secix &&
  245. conn->service_id == x_servid &&
  246. conn->in_clientflag == x_clflag)
  247. goto found_active_second_chance;
  248. }
  249. /* search the transport's graveyard list */
  250. spin_lock(&peer->conn_gylock);
  251. list_for_each(_p, &peer->conn_graveyard) {
  252. conn = list_entry(_p, struct rxrpc_connection, link);
  253. if (conn->addr.sin_port == x_port &&
  254. conn->in_epoch == x_epoch &&
  255. conn->conn_id == x_connid &&
  256. conn->security_ix == x_secix &&
  257. conn->service_id == x_servid &&
  258. conn->in_clientflag == x_clflag)
  259. goto found_in_graveyard;
  260. }
  261. spin_unlock(&peer->conn_gylock);
  262. /* outbound connections aren't created here */
  263. if (!x_clflag) {
  264. write_unlock(&peer->conn_lock);
  265. _leave(" = -ENOENT");
  266. return -ENOENT;
  267. }
  268. /* we can now add the new candidate to the list */
  269. _debug("created connection: {%08x} [in]", ntohl(candidate->conn_id));
  270. rxrpc_get_peer(peer);
  271. conn = candidate;
  272. candidate = NULL;
  273. atomic_inc(&peer->conn_count);
  274. fresh = 1;
  275. make_active:
  276. list_add_tail(&conn->link, &peer->conn_active);
  277. success_uwfree:
  278. write_unlock(&peer->conn_lock);
  279. if (candidate) {
  280. write_lock(&peer->conn_idlock);
  281. list_del(&candidate->id_link);
  282. write_unlock(&peer->conn_idlock);
  283. __RXACCT(atomic_dec(&rxrpc_connection_count));
  284. kfree(candidate);
  285. }
  286. if (fresh) {
  287. down_write(&rxrpc_conns_sem);
  288. list_add_tail(&conn->proc_link, &rxrpc_conns);
  289. up_write(&rxrpc_conns_sem);
  290. }
  291. success:
  292. *_conn = conn;
  293. _leave(" = 0 (%p)", conn);
  294. return 0;
  295. /* handle the connection being found in the active list straight off */
  296. found_active:
  297. rxrpc_get_connection(conn);
  298. read_unlock(&peer->conn_lock);
  299. goto success;
  300. /* handle resurrecting a connection from the graveyard */
  301. found_in_graveyard:
  302. _debug("resurrecting connection: {%08x} [in]", ntohl(conn->conn_id));
  303. rxrpc_get_peer(peer);
  304. rxrpc_get_connection(conn);
  305. rxrpc_krxtimod_del_timer(&conn->timeout);
  306. list_del_init(&conn->link);
  307. spin_unlock(&peer->conn_gylock);
  308. goto make_active;
  309. /* handle finding the connection on the second time through the active
  310. * list */
  311. found_active_second_chance:
  312. rxrpc_get_connection(conn);
  313. goto success_uwfree;
  314. } /* end rxrpc_connection_lookup() */
  315. /*****************************************************************************/
  316. /*
  317. * finish using a connection record
  318. * - it will be transferred to the peer's connection graveyard when refcount
  319. * reaches 0
  320. */
  321. void rxrpc_put_connection(struct rxrpc_connection *conn)
  322. {
  323. struct rxrpc_peer *peer;
  324. if (!conn)
  325. return;
  326. _enter("%p{u=%d p=%hu}",
  327. conn, atomic_read(&conn->usage), ntohs(conn->addr.sin_port));
  328. peer = conn->peer;
  329. spin_lock(&peer->conn_gylock);
  330. /* sanity check */
  331. if (atomic_read(&conn->usage) <= 0)
  332. BUG();
  333. if (likely(!atomic_dec_and_test(&conn->usage))) {
  334. spin_unlock(&peer->conn_gylock);
  335. _leave("");
  336. return;
  337. }
  338. /* move to graveyard queue */
  339. _debug("burying connection: {%08x}", ntohl(conn->conn_id));
  340. list_del(&conn->link);
  341. list_add_tail(&conn->link, &peer->conn_graveyard);
  342. rxrpc_krxtimod_add_timer(&conn->timeout, rxrpc_conn_timeout * HZ);
  343. spin_unlock(&peer->conn_gylock);
  344. rxrpc_put_peer(conn->peer);
  345. _leave(" [killed]");
  346. } /* end rxrpc_put_connection() */
  347. /*****************************************************************************/
  348. /*
  349. * free a connection record
  350. */
  351. static void rxrpc_conn_do_timeout(struct rxrpc_connection *conn)
  352. {
  353. struct rxrpc_peer *peer;
  354. _enter("%p{u=%d p=%hu}",
  355. conn, atomic_read(&conn->usage), ntohs(conn->addr.sin_port));
  356. peer = conn->peer;
  357. if (atomic_read(&conn->usage) < 0)
  358. BUG();
  359. /* remove from graveyard if still dead */
  360. spin_lock(&peer->conn_gylock);
  361. if (atomic_read(&conn->usage) == 0) {
  362. list_del_init(&conn->link);
  363. }
  364. else {
  365. conn = NULL;
  366. }
  367. spin_unlock(&peer->conn_gylock);
  368. if (!conn) {
  369. _leave("");
  370. return; /* resurrected */
  371. }
  372. _debug("--- Destroying Connection %p{%08x} ---",
  373. conn, ntohl(conn->conn_id));
  374. down_write(&rxrpc_conns_sem);
  375. list_del(&conn->proc_link);
  376. up_write(&rxrpc_conns_sem);
  377. write_lock(&peer->conn_idlock);
  378. list_del(&conn->id_link);
  379. write_unlock(&peer->conn_idlock);
  380. __RXACCT(atomic_dec(&rxrpc_connection_count));
  381. kfree(conn);
  382. /* if the graveyard is now empty, wake up anyone waiting for that */
  383. if (atomic_dec_and_test(&peer->conn_count))
  384. wake_up(&peer->conn_gy_waitq);
  385. _leave(" [destroyed]");
  386. } /* end rxrpc_conn_do_timeout() */
  387. /*****************************************************************************/
  388. /*
  389. * clear all connection records from a peer endpoint
  390. */
  391. void rxrpc_conn_clearall(struct rxrpc_peer *peer)
  392. {
  393. DECLARE_WAITQUEUE(myself, current);
  394. struct rxrpc_connection *conn;
  395. int err;
  396. _enter("%p", peer);
  397. /* there shouldn't be any active conns remaining */
  398. if (!list_empty(&peer->conn_active))
  399. BUG();
  400. /* manually timeout all conns in the graveyard */
  401. spin_lock(&peer->conn_gylock);
  402. while (!list_empty(&peer->conn_graveyard)) {
  403. conn = list_entry(peer->conn_graveyard.next,
  404. struct rxrpc_connection, link);
  405. err = rxrpc_krxtimod_del_timer(&conn->timeout);
  406. spin_unlock(&peer->conn_gylock);
  407. if (err == 0)
  408. rxrpc_conn_do_timeout(conn);
  409. spin_lock(&peer->conn_gylock);
  410. }
  411. spin_unlock(&peer->conn_gylock);
  412. /* wait for the the conn graveyard to be completely cleared */
  413. set_current_state(TASK_UNINTERRUPTIBLE);
  414. add_wait_queue(&peer->conn_gy_waitq, &myself);
  415. while (atomic_read(&peer->conn_count) != 0) {
  416. schedule();
  417. set_current_state(TASK_UNINTERRUPTIBLE);
  418. }
  419. remove_wait_queue(&peer->conn_gy_waitq, &myself);
  420. set_current_state(TASK_RUNNING);
  421. _leave("");
  422. } /* end rxrpc_conn_clearall() */
  423. /*****************************************************************************/
  424. /*
  425. * allocate and prepare a message for sending out through the transport
  426. * endpoint
  427. */
  428. int rxrpc_conn_newmsg(struct rxrpc_connection *conn,
  429. struct rxrpc_call *call,
  430. uint8_t type,
  431. int dcount,
  432. struct kvec diov[],
  433. gfp_t alloc_flags,
  434. struct rxrpc_message **_msg)
  435. {
  436. struct rxrpc_message *msg;
  437. int loop;
  438. _enter("%p{%d},%p,%u", conn, ntohs(conn->addr.sin_port), call, type);
  439. if (dcount > 3) {
  440. _leave(" = -EINVAL");
  441. return -EINVAL;
  442. }
  443. msg = kmalloc(sizeof(struct rxrpc_message), alloc_flags);
  444. if (!msg) {
  445. _leave(" = -ENOMEM");
  446. return -ENOMEM;
  447. }
  448. memset(msg, 0, sizeof(*msg));
  449. atomic_set(&msg->usage, 1);
  450. INIT_LIST_HEAD(&msg->link);
  451. msg->state = RXRPC_MSG_PREPARED;
  452. msg->hdr.epoch = conn->out_epoch;
  453. msg->hdr.cid = conn->conn_id | (call ? call->chan_ix : 0);
  454. msg->hdr.callNumber = call ? call->call_id : 0;
  455. msg->hdr.type = type;
  456. msg->hdr.flags = conn->out_clientflag;
  457. msg->hdr.securityIndex = conn->security_ix;
  458. msg->hdr.serviceId = conn->service_id;
  459. /* generate sequence numbers for data packets */
  460. if (call) {
  461. switch (type) {
  462. case RXRPC_PACKET_TYPE_DATA:
  463. msg->seq = ++call->snd_seq_count;
  464. msg->hdr.seq = htonl(msg->seq);
  465. break;
  466. case RXRPC_PACKET_TYPE_ACK:
  467. /* ACK sequence numbers are complicated. The following
  468. * may be wrong:
  469. * - jumbo packet ACKs should have a seq number
  470. * - normal ACKs should not
  471. */
  472. default:
  473. break;
  474. }
  475. }
  476. msg->dcount = dcount + 1;
  477. msg->dsize = sizeof(msg->hdr);
  478. msg->data[0].iov_len = sizeof(msg->hdr);
  479. msg->data[0].iov_base = &msg->hdr;
  480. for (loop=0; loop < dcount; loop++) {
  481. msg->dsize += diov[loop].iov_len;
  482. msg->data[loop+1].iov_len = diov[loop].iov_len;
  483. msg->data[loop+1].iov_base = diov[loop].iov_base;
  484. }
  485. __RXACCT(atomic_inc(&rxrpc_message_count));
  486. *_msg = msg;
  487. _leave(" = 0 (%p) #%d", msg, atomic_read(&rxrpc_message_count));
  488. return 0;
  489. } /* end rxrpc_conn_newmsg() */
  490. /*****************************************************************************/
  491. /*
  492. * free a message
  493. */
  494. void __rxrpc_put_message(struct rxrpc_message *msg)
  495. {
  496. int loop;
  497. _enter("%p #%d", msg, atomic_read(&rxrpc_message_count));
  498. if (msg->pkt)
  499. kfree_skb(msg->pkt);
  500. rxrpc_put_connection(msg->conn);
  501. for (loop = 0; loop < 8; loop++)
  502. if (test_bit(loop, &msg->dfree))
  503. kfree(msg->data[loop].iov_base);
  504. __RXACCT(atomic_dec(&rxrpc_message_count));
  505. kfree(msg);
  506. _leave("");
  507. } /* end __rxrpc_put_message() */
  508. /*****************************************************************************/
  509. /*
  510. * send a message out through the transport endpoint
  511. */
  512. int rxrpc_conn_sendmsg(struct rxrpc_connection *conn,
  513. struct rxrpc_message *msg)
  514. {
  515. struct msghdr msghdr;
  516. int ret;
  517. _enter("%p{%d}", conn, ntohs(conn->addr.sin_port));
  518. /* fill in some fields in the header */
  519. spin_lock(&conn->lock);
  520. msg->hdr.serial = htonl(++conn->serial_counter);
  521. msg->rttdone = 0;
  522. spin_unlock(&conn->lock);
  523. /* set up the message to be transmitted */
  524. msghdr.msg_name = &conn->addr;
  525. msghdr.msg_namelen = sizeof(conn->addr);
  526. msghdr.msg_control = NULL;
  527. msghdr.msg_controllen = 0;
  528. msghdr.msg_flags = MSG_CONFIRM | MSG_DONTWAIT;
  529. _net("Sending message type %d of %Zd bytes to %08x:%d",
  530. msg->hdr.type,
  531. msg->dsize,
  532. ntohl(conn->addr.sin_addr.s_addr),
  533. ntohs(conn->addr.sin_port));
  534. /* send the message */
  535. ret = kernel_sendmsg(conn->trans->socket, &msghdr,
  536. msg->data, msg->dcount, msg->dsize);
  537. if (ret < 0) {
  538. msg->state = RXRPC_MSG_ERROR;
  539. } else {
  540. msg->state = RXRPC_MSG_SENT;
  541. ret = 0;
  542. spin_lock(&conn->lock);
  543. do_gettimeofday(&conn->atime);
  544. msg->stamp = conn->atime;
  545. spin_unlock(&conn->lock);
  546. }
  547. _leave(" = %d", ret);
  548. return ret;
  549. } /* end rxrpc_conn_sendmsg() */
  550. /*****************************************************************************/
  551. /*
  552. * deal with a subsequent call packet
  553. */
  554. int rxrpc_conn_receive_call_packet(struct rxrpc_connection *conn,
  555. struct rxrpc_call *call,
  556. struct rxrpc_message *msg)
  557. {
  558. struct rxrpc_message *pmsg;
  559. struct dst_entry *dst;
  560. struct list_head *_p;
  561. unsigned cix, seq;
  562. int ret = 0;
  563. _enter("%p,%p,%p", conn, call, msg);
  564. if (!call) {
  565. cix = ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK;
  566. spin_lock(&conn->lock);
  567. call = conn->channels[cix];
  568. if (!call || call->call_id != msg->hdr.callNumber) {
  569. spin_unlock(&conn->lock);
  570. rxrpc_trans_immediate_abort(conn->trans, msg, -ENOENT);
  571. goto out;
  572. }
  573. else {
  574. rxrpc_get_call(call);
  575. spin_unlock(&conn->lock);
  576. }
  577. }
  578. else {
  579. rxrpc_get_call(call);
  580. }
  581. _proto("Received packet %%%u [%u] on call %hu:%u:%u",
  582. ntohl(msg->hdr.serial),
  583. ntohl(msg->hdr.seq),
  584. ntohs(msg->hdr.serviceId),
  585. ntohl(conn->conn_id),
  586. ntohl(call->call_id));
  587. call->pkt_rcv_count++;
  588. dst = msg->pkt->dst;
  589. if (dst && dst->dev)
  590. conn->peer->if_mtu =
  591. dst->dev->mtu - dst->dev->hard_header_len;
  592. /* queue on the call in seq order */
  593. rxrpc_get_message(msg);
  594. seq = msg->seq;
  595. spin_lock(&call->lock);
  596. list_for_each(_p, &call->rcv_receiveq) {
  597. pmsg = list_entry(_p, struct rxrpc_message, link);
  598. if (pmsg->seq > seq)
  599. break;
  600. }
  601. list_add_tail(&msg->link, _p);
  602. /* reset the activity timeout */
  603. call->flags |= RXRPC_CALL_RCV_PKT;
  604. mod_timer(&call->rcv_timeout,jiffies + rxrpc_call_rcv_timeout * HZ);
  605. spin_unlock(&call->lock);
  606. rxrpc_krxiod_queue_call(call);
  607. rxrpc_put_call(call);
  608. out:
  609. _leave(" = %d", ret);
  610. return ret;
  611. } /* end rxrpc_conn_receive_call_packet() */
  612. /*****************************************************************************/
  613. /*
  614. * handle an ICMP error being applied to a connection
  615. */
  616. void rxrpc_conn_handle_error(struct rxrpc_connection *conn,
  617. int local, int errno)
  618. {
  619. struct rxrpc_call *calls[4];
  620. int loop;
  621. _enter("%p{%d},%d", conn, ntohs(conn->addr.sin_port), errno);
  622. /* get a ref to all my calls in one go */
  623. memset(calls, 0, sizeof(calls));
  624. spin_lock(&conn->lock);
  625. for (loop = 3; loop >= 0; loop--) {
  626. if (conn->channels[loop]) {
  627. calls[loop] = conn->channels[loop];
  628. rxrpc_get_call(calls[loop]);
  629. }
  630. }
  631. spin_unlock(&conn->lock);
  632. /* now kick them all */
  633. for (loop = 3; loop >= 0; loop--) {
  634. if (calls[loop]) {
  635. rxrpc_call_handle_error(calls[loop], local, errno);
  636. rxrpc_put_call(calls[loop]);
  637. }
  638. }
  639. _leave("");
  640. } /* end rxrpc_conn_handle_error() */