connection.c 20 KB

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