connection.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. 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(msg->pkt->h.uh->source),
  193. ntohs(msg->hdr.serviceId));
  194. x_port = msg->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 = msg->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_del(&conn->link);
  340. list_add_tail(&conn->link, &peer->conn_graveyard);
  341. rxrpc_krxtimod_add_timer(&conn->timeout, rxrpc_conn_timeout * HZ);
  342. spin_unlock(&peer->conn_gylock);
  343. rxrpc_put_peer(conn->peer);
  344. _leave(" [killed]");
  345. } /* end rxrpc_put_connection() */
  346. /*****************************************************************************/
  347. /*
  348. * free a connection record
  349. */
  350. static void rxrpc_conn_do_timeout(struct rxrpc_connection *conn)
  351. {
  352. struct rxrpc_peer *peer;
  353. _enter("%p{u=%d p=%hu}",
  354. conn, atomic_read(&conn->usage), ntohs(conn->addr.sin_port));
  355. peer = conn->peer;
  356. if (atomic_read(&conn->usage) < 0)
  357. BUG();
  358. /* remove from graveyard if still dead */
  359. spin_lock(&peer->conn_gylock);
  360. if (atomic_read(&conn->usage) == 0) {
  361. list_del_init(&conn->link);
  362. }
  363. else {
  364. conn = NULL;
  365. }
  366. spin_unlock(&peer->conn_gylock);
  367. if (!conn) {
  368. _leave("");
  369. return; /* resurrected */
  370. }
  371. _debug("--- Destroying Connection %p{%08x} ---",
  372. conn, ntohl(conn->conn_id));
  373. down_write(&rxrpc_conns_sem);
  374. list_del(&conn->proc_link);
  375. up_write(&rxrpc_conns_sem);
  376. write_lock(&peer->conn_idlock);
  377. list_del(&conn->id_link);
  378. write_unlock(&peer->conn_idlock);
  379. __RXACCT(atomic_dec(&rxrpc_connection_count));
  380. kfree(conn);
  381. /* if the graveyard is now empty, wake up anyone waiting for that */
  382. if (atomic_dec_and_test(&peer->conn_count))
  383. wake_up(&peer->conn_gy_waitq);
  384. _leave(" [destroyed]");
  385. } /* end rxrpc_conn_do_timeout() */
  386. /*****************************************************************************/
  387. /*
  388. * clear all connection records from a peer endpoint
  389. */
  390. void rxrpc_conn_clearall(struct rxrpc_peer *peer)
  391. {
  392. DECLARE_WAITQUEUE(myself, current);
  393. struct rxrpc_connection *conn;
  394. int err;
  395. _enter("%p", peer);
  396. /* there shouldn't be any active conns remaining */
  397. if (!list_empty(&peer->conn_active))
  398. BUG();
  399. /* manually timeout all conns in the graveyard */
  400. spin_lock(&peer->conn_gylock);
  401. while (!list_empty(&peer->conn_graveyard)) {
  402. conn = list_entry(peer->conn_graveyard.next,
  403. struct rxrpc_connection, link);
  404. err = rxrpc_krxtimod_del_timer(&conn->timeout);
  405. spin_unlock(&peer->conn_gylock);
  406. if (err == 0)
  407. rxrpc_conn_do_timeout(conn);
  408. spin_lock(&peer->conn_gylock);
  409. }
  410. spin_unlock(&peer->conn_gylock);
  411. /* wait for the the conn graveyard to be completely cleared */
  412. set_current_state(TASK_UNINTERRUPTIBLE);
  413. add_wait_queue(&peer->conn_gy_waitq, &myself);
  414. while (atomic_read(&peer->conn_count) != 0) {
  415. schedule();
  416. set_current_state(TASK_UNINTERRUPTIBLE);
  417. }
  418. remove_wait_queue(&peer->conn_gy_waitq, &myself);
  419. set_current_state(TASK_RUNNING);
  420. _leave("");
  421. } /* end rxrpc_conn_clearall() */
  422. /*****************************************************************************/
  423. /*
  424. * allocate and prepare a message for sending out through the transport
  425. * endpoint
  426. */
  427. int rxrpc_conn_newmsg(struct rxrpc_connection *conn,
  428. struct rxrpc_call *call,
  429. uint8_t type,
  430. int dcount,
  431. struct kvec diov[],
  432. int alloc_flags,
  433. struct rxrpc_message **_msg)
  434. {
  435. struct rxrpc_message *msg;
  436. int loop;
  437. _enter("%p{%d},%p,%u", conn, ntohs(conn->addr.sin_port), call, type);
  438. if (dcount > 3) {
  439. _leave(" = -EINVAL");
  440. return -EINVAL;
  441. }
  442. msg = kmalloc(sizeof(struct rxrpc_message), alloc_flags);
  443. if (!msg) {
  444. _leave(" = -ENOMEM");
  445. return -ENOMEM;
  446. }
  447. memset(msg, 0, sizeof(*msg));
  448. atomic_set(&msg->usage, 1);
  449. INIT_LIST_HEAD(&msg->link);
  450. msg->state = RXRPC_MSG_PREPARED;
  451. msg->hdr.epoch = conn->out_epoch;
  452. msg->hdr.cid = conn->conn_id | (call ? call->chan_ix : 0);
  453. msg->hdr.callNumber = call ? call->call_id : 0;
  454. msg->hdr.type = type;
  455. msg->hdr.flags = conn->out_clientflag;
  456. msg->hdr.securityIndex = conn->security_ix;
  457. msg->hdr.serviceId = conn->service_id;
  458. /* generate sequence numbers for data packets */
  459. if (call) {
  460. switch (type) {
  461. case RXRPC_PACKET_TYPE_DATA:
  462. msg->seq = ++call->snd_seq_count;
  463. msg->hdr.seq = htonl(msg->seq);
  464. break;
  465. case RXRPC_PACKET_TYPE_ACK:
  466. /* ACK sequence numbers are complicated. The following
  467. * may be wrong:
  468. * - jumbo packet ACKs should have a seq number
  469. * - normal ACKs should not
  470. */
  471. default:
  472. break;
  473. }
  474. }
  475. msg->dcount = dcount + 1;
  476. msg->dsize = sizeof(msg->hdr);
  477. msg->data[0].iov_len = sizeof(msg->hdr);
  478. msg->data[0].iov_base = &msg->hdr;
  479. for (loop=0; loop < dcount; loop++) {
  480. msg->dsize += diov[loop].iov_len;
  481. msg->data[loop+1].iov_len = diov[loop].iov_len;
  482. msg->data[loop+1].iov_base = diov[loop].iov_base;
  483. }
  484. __RXACCT(atomic_inc(&rxrpc_message_count));
  485. *_msg = msg;
  486. _leave(" = 0 (%p) #%d", msg, atomic_read(&rxrpc_message_count));
  487. return 0;
  488. } /* end rxrpc_conn_newmsg() */
  489. /*****************************************************************************/
  490. /*
  491. * free a message
  492. */
  493. void __rxrpc_put_message(struct rxrpc_message *msg)
  494. {
  495. int loop;
  496. _enter("%p #%d", msg, atomic_read(&rxrpc_message_count));
  497. if (msg->pkt)
  498. kfree_skb(msg->pkt);
  499. rxrpc_put_connection(msg->conn);
  500. for (loop = 0; loop < 8; loop++)
  501. if (test_bit(loop, &msg->dfree))
  502. kfree(msg->data[loop].iov_base);
  503. __RXACCT(atomic_dec(&rxrpc_message_count));
  504. kfree(msg);
  505. _leave("");
  506. } /* end __rxrpc_put_message() */
  507. /*****************************************************************************/
  508. /*
  509. * send a message out through the transport endpoint
  510. */
  511. int rxrpc_conn_sendmsg(struct rxrpc_connection *conn,
  512. struct rxrpc_message *msg)
  513. {
  514. struct msghdr msghdr;
  515. int ret;
  516. _enter("%p{%d}", conn, ntohs(conn->addr.sin_port));
  517. /* fill in some fields in the header */
  518. spin_lock(&conn->lock);
  519. msg->hdr.serial = htonl(++conn->serial_counter);
  520. msg->rttdone = 0;
  521. spin_unlock(&conn->lock);
  522. /* set up the message to be transmitted */
  523. msghdr.msg_name = &conn->addr;
  524. msghdr.msg_namelen = sizeof(conn->addr);
  525. msghdr.msg_control = NULL;
  526. msghdr.msg_controllen = 0;
  527. msghdr.msg_flags = MSG_CONFIRM | MSG_DONTWAIT;
  528. _net("Sending message type %d of %Zd bytes to %08x:%d",
  529. msg->hdr.type,
  530. msg->dsize,
  531. ntohl(conn->addr.sin_addr.s_addr),
  532. ntohs(conn->addr.sin_port));
  533. /* send the message */
  534. ret = kernel_sendmsg(conn->trans->socket, &msghdr,
  535. msg->data, msg->dcount, msg->dsize);
  536. if (ret < 0) {
  537. msg->state = RXRPC_MSG_ERROR;
  538. } else {
  539. msg->state = RXRPC_MSG_SENT;
  540. ret = 0;
  541. spin_lock(&conn->lock);
  542. do_gettimeofday(&conn->atime);
  543. msg->stamp = conn->atime;
  544. spin_unlock(&conn->lock);
  545. }
  546. _leave(" = %d", ret);
  547. return ret;
  548. } /* end rxrpc_conn_sendmsg() */
  549. /*****************************************************************************/
  550. /*
  551. * deal with a subsequent call packet
  552. */
  553. int rxrpc_conn_receive_call_packet(struct rxrpc_connection *conn,
  554. struct rxrpc_call *call,
  555. struct rxrpc_message *msg)
  556. {
  557. struct rxrpc_message *pmsg;
  558. struct list_head *_p;
  559. unsigned cix, seq;
  560. int ret = 0;
  561. _enter("%p,%p,%p", conn, call, msg);
  562. if (!call) {
  563. cix = ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK;
  564. spin_lock(&conn->lock);
  565. call = conn->channels[cix];
  566. if (!call || call->call_id != msg->hdr.callNumber) {
  567. spin_unlock(&conn->lock);
  568. rxrpc_trans_immediate_abort(conn->trans, msg, -ENOENT);
  569. goto out;
  570. }
  571. else {
  572. rxrpc_get_call(call);
  573. spin_unlock(&conn->lock);
  574. }
  575. }
  576. else {
  577. rxrpc_get_call(call);
  578. }
  579. _proto("Received packet %%%u [%u] on call %hu:%u:%u",
  580. ntohl(msg->hdr.serial),
  581. ntohl(msg->hdr.seq),
  582. ntohs(msg->hdr.serviceId),
  583. ntohl(conn->conn_id),
  584. ntohl(call->call_id));
  585. call->pkt_rcv_count++;
  586. if (msg->pkt->dst && msg->pkt->dst->dev)
  587. conn->peer->if_mtu =
  588. msg->pkt->dst->dev->mtu -
  589. msg->pkt->dst->dev->hard_header_len;
  590. /* queue on the call in seq order */
  591. rxrpc_get_message(msg);
  592. seq = msg->seq;
  593. spin_lock(&call->lock);
  594. list_for_each(_p, &call->rcv_receiveq) {
  595. pmsg = list_entry(_p, struct rxrpc_message, link);
  596. if (pmsg->seq > seq)
  597. break;
  598. }
  599. list_add_tail(&msg->link, _p);
  600. /* reset the activity timeout */
  601. call->flags |= RXRPC_CALL_RCV_PKT;
  602. mod_timer(&call->rcv_timeout,jiffies + rxrpc_call_rcv_timeout * HZ);
  603. spin_unlock(&call->lock);
  604. rxrpc_krxiod_queue_call(call);
  605. rxrpc_put_call(call);
  606. out:
  607. _leave(" = %d", ret);
  608. return ret;
  609. } /* end rxrpc_conn_receive_call_packet() */
  610. /*****************************************************************************/
  611. /*
  612. * handle an ICMP error being applied to a connection
  613. */
  614. void rxrpc_conn_handle_error(struct rxrpc_connection *conn,
  615. int local, int errno)
  616. {
  617. struct rxrpc_call *calls[4];
  618. int loop;
  619. _enter("%p{%d},%d", conn, ntohs(conn->addr.sin_port), errno);
  620. /* get a ref to all my calls in one go */
  621. memset(calls, 0, sizeof(calls));
  622. spin_lock(&conn->lock);
  623. for (loop = 3; loop >= 0; loop--) {
  624. if (conn->channels[loop]) {
  625. calls[loop] = conn->channels[loop];
  626. rxrpc_get_call(calls[loop]);
  627. }
  628. }
  629. spin_unlock(&conn->lock);
  630. /* now kick them all */
  631. for (loop = 3; loop >= 0; loop--) {
  632. if (calls[loop]) {
  633. rxrpc_call_handle_error(calls[loop], local, errno);
  634. rxrpc_put_call(calls[loop]);
  635. }
  636. }
  637. _leave("");
  638. } /* end rxrpc_conn_handle_error() */