conn_event.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* connection-level event handling
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/errqueue.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include <net/ip.h>
  19. #include "ar-internal.h"
  20. /*
  21. * Retransmit terminal ACK or ABORT of the previous call.
  22. */
  23. static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
  24. struct sk_buff *skb)
  25. {
  26. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  27. struct rxrpc_channel *chan;
  28. struct msghdr msg;
  29. struct kvec iov;
  30. struct {
  31. struct rxrpc_wire_header whdr;
  32. union {
  33. struct {
  34. __be32 code;
  35. } abort;
  36. struct {
  37. struct rxrpc_ackpacket ack;
  38. u8 padding[3];
  39. struct rxrpc_ackinfo info;
  40. };
  41. };
  42. } __attribute__((packed)) pkt;
  43. size_t len;
  44. u32 serial, mtu, call_id;
  45. _enter("%d", conn->debug_id);
  46. chan = &conn->channels[sp->hdr.cid & RXRPC_CHANNELMASK];
  47. /* If the last call got moved on whilst we were waiting to run, just
  48. * ignore this packet.
  49. */
  50. call_id = READ_ONCE(chan->last_call);
  51. /* Sync with __rxrpc_disconnect_call() */
  52. smp_rmb();
  53. if (call_id != sp->hdr.callNumber)
  54. return;
  55. msg.msg_name = &conn->params.peer->srx.transport;
  56. msg.msg_namelen = conn->params.peer->srx.transport_len;
  57. msg.msg_control = NULL;
  58. msg.msg_controllen = 0;
  59. msg.msg_flags = 0;
  60. pkt.whdr.epoch = htonl(sp->hdr.epoch);
  61. pkt.whdr.cid = htonl(sp->hdr.cid);
  62. pkt.whdr.callNumber = htonl(sp->hdr.callNumber);
  63. pkt.whdr.seq = 0;
  64. pkt.whdr.type = chan->last_type;
  65. pkt.whdr.flags = conn->out_clientflag;
  66. pkt.whdr.userStatus = 0;
  67. pkt.whdr.securityIndex = conn->security_ix;
  68. pkt.whdr._rsvd = 0;
  69. pkt.whdr.serviceId = htons(chan->last_service_id);
  70. len = sizeof(pkt.whdr);
  71. switch (chan->last_type) {
  72. case RXRPC_PACKET_TYPE_ABORT:
  73. pkt.abort.code = htonl(chan->last_abort);
  74. len += sizeof(pkt.abort);
  75. break;
  76. case RXRPC_PACKET_TYPE_ACK:
  77. mtu = conn->params.peer->if_mtu;
  78. mtu -= conn->params.peer->hdrsize;
  79. pkt.ack.bufferSpace = 0;
  80. pkt.ack.maxSkew = htons(skb->priority);
  81. pkt.ack.firstPacket = htonl(chan->last_seq);
  82. pkt.ack.previousPacket = htonl(chan->last_seq - 1);
  83. pkt.ack.serial = htonl(sp->hdr.serial);
  84. pkt.ack.reason = RXRPC_ACK_DUPLICATE;
  85. pkt.ack.nAcks = 0;
  86. pkt.info.rxMTU = htonl(rxrpc_rx_mtu);
  87. pkt.info.maxMTU = htonl(mtu);
  88. pkt.info.rwind = htonl(rxrpc_rx_window_size);
  89. pkt.info.jumbo_max = htonl(rxrpc_rx_jumbo_max);
  90. pkt.whdr.flags |= RXRPC_SLOW_START_OK;
  91. len += sizeof(pkt.ack) + sizeof(pkt.info);
  92. break;
  93. }
  94. /* Resync with __rxrpc_disconnect_call() and check that the last call
  95. * didn't get advanced whilst we were filling out the packets.
  96. */
  97. smp_rmb();
  98. if (READ_ONCE(chan->last_call) != call_id)
  99. return;
  100. iov.iov_base = &pkt;
  101. iov.iov_len = len;
  102. serial = atomic_inc_return(&conn->serial);
  103. pkt.whdr.serial = htonl(serial);
  104. switch (chan->last_type) {
  105. case RXRPC_PACKET_TYPE_ABORT:
  106. _proto("Tx ABORT %%%u { %d } [re]", serial, conn->local_abort);
  107. break;
  108. case RXRPC_PACKET_TYPE_ACK:
  109. trace_rxrpc_tx_ack(NULL, serial, chan->last_seq, 0,
  110. RXRPC_ACK_DUPLICATE, 0);
  111. _proto("Tx ACK %%%u [re]", serial);
  112. break;
  113. }
  114. kernel_sendmsg(conn->params.local->socket, &msg, &iov, 1, len);
  115. _leave("");
  116. return;
  117. }
  118. /*
  119. * pass a connection-level abort onto all calls on that connection
  120. */
  121. static void rxrpc_abort_calls(struct rxrpc_connection *conn,
  122. enum rxrpc_call_completion compl,
  123. u32 abort_code, int error)
  124. {
  125. struct rxrpc_call *call;
  126. int i;
  127. _enter("{%d},%x", conn->debug_id, abort_code);
  128. spin_lock(&conn->channel_lock);
  129. for (i = 0; i < RXRPC_MAXCALLS; i++) {
  130. call = rcu_dereference_protected(
  131. conn->channels[i].call,
  132. lockdep_is_held(&conn->channel_lock));
  133. if (call) {
  134. if (compl == RXRPC_CALL_LOCALLY_ABORTED)
  135. trace_rxrpc_abort("CON", call->cid,
  136. call->call_id, 0,
  137. abort_code, error);
  138. if (rxrpc_set_call_completion(call, compl,
  139. abort_code, error))
  140. rxrpc_notify_socket(call);
  141. }
  142. }
  143. spin_unlock(&conn->channel_lock);
  144. _leave("");
  145. }
  146. /*
  147. * generate a connection-level abort
  148. */
  149. static int rxrpc_abort_connection(struct rxrpc_connection *conn,
  150. u32 error, u32 abort_code)
  151. {
  152. struct rxrpc_wire_header whdr;
  153. struct msghdr msg;
  154. struct kvec iov[2];
  155. __be32 word;
  156. size_t len;
  157. u32 serial;
  158. int ret;
  159. _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
  160. /* generate a connection-level abort */
  161. spin_lock_bh(&conn->state_lock);
  162. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  163. spin_unlock_bh(&conn->state_lock);
  164. _leave(" = 0 [already dead]");
  165. return 0;
  166. }
  167. conn->state = RXRPC_CONN_LOCALLY_ABORTED;
  168. spin_unlock_bh(&conn->state_lock);
  169. rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code, error);
  170. msg.msg_name = &conn->params.peer->srx.transport;
  171. msg.msg_namelen = conn->params.peer->srx.transport_len;
  172. msg.msg_control = NULL;
  173. msg.msg_controllen = 0;
  174. msg.msg_flags = 0;
  175. whdr.epoch = htonl(conn->proto.epoch);
  176. whdr.cid = htonl(conn->proto.cid);
  177. whdr.callNumber = 0;
  178. whdr.seq = 0;
  179. whdr.type = RXRPC_PACKET_TYPE_ABORT;
  180. whdr.flags = conn->out_clientflag;
  181. whdr.userStatus = 0;
  182. whdr.securityIndex = conn->security_ix;
  183. whdr._rsvd = 0;
  184. whdr.serviceId = htons(conn->params.service_id);
  185. word = htonl(conn->local_abort);
  186. iov[0].iov_base = &whdr;
  187. iov[0].iov_len = sizeof(whdr);
  188. iov[1].iov_base = &word;
  189. iov[1].iov_len = sizeof(word);
  190. len = iov[0].iov_len + iov[1].iov_len;
  191. serial = atomic_inc_return(&conn->serial);
  192. whdr.serial = htonl(serial);
  193. _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
  194. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
  195. if (ret < 0) {
  196. _debug("sendmsg failed: %d", ret);
  197. return -EAGAIN;
  198. }
  199. _leave(" = 0");
  200. return 0;
  201. }
  202. /*
  203. * mark a call as being on a now-secured channel
  204. * - must be called with BH's disabled.
  205. */
  206. static void rxrpc_call_is_secure(struct rxrpc_call *call)
  207. {
  208. _enter("%p", call);
  209. if (call) {
  210. write_lock_bh(&call->state_lock);
  211. if (call->state == RXRPC_CALL_SERVER_SECURING) {
  212. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  213. rxrpc_notify_socket(call);
  214. }
  215. write_unlock_bh(&call->state_lock);
  216. }
  217. }
  218. /*
  219. * connection-level Rx packet processor
  220. */
  221. static int rxrpc_process_event(struct rxrpc_connection *conn,
  222. struct sk_buff *skb,
  223. u32 *_abort_code)
  224. {
  225. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  226. __be32 wtmp;
  227. u32 abort_code;
  228. int loop, ret;
  229. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  230. _leave(" = -ECONNABORTED [%u]", conn->state);
  231. return -ECONNABORTED;
  232. }
  233. _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
  234. switch (sp->hdr.type) {
  235. case RXRPC_PACKET_TYPE_DATA:
  236. case RXRPC_PACKET_TYPE_ACK:
  237. rxrpc_conn_retransmit_call(conn, skb);
  238. return 0;
  239. case RXRPC_PACKET_TYPE_BUSY:
  240. /* Just ignore BUSY packets for now. */
  241. return 0;
  242. case RXRPC_PACKET_TYPE_ABORT:
  243. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  244. &wtmp, sizeof(wtmp)) < 0)
  245. return -EPROTO;
  246. abort_code = ntohl(wtmp);
  247. _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
  248. conn->state = RXRPC_CONN_REMOTELY_ABORTED;
  249. rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
  250. abort_code, ECONNABORTED);
  251. return -ECONNABORTED;
  252. case RXRPC_PACKET_TYPE_CHALLENGE:
  253. return conn->security->respond_to_challenge(conn, skb,
  254. _abort_code);
  255. case RXRPC_PACKET_TYPE_RESPONSE:
  256. ret = conn->security->verify_response(conn, skb, _abort_code);
  257. if (ret < 0)
  258. return ret;
  259. ret = conn->security->init_connection_security(conn);
  260. if (ret < 0)
  261. return ret;
  262. ret = conn->security->prime_packet_security(conn);
  263. if (ret < 0)
  264. return ret;
  265. spin_lock(&conn->channel_lock);
  266. spin_lock(&conn->state_lock);
  267. if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
  268. conn->state = RXRPC_CONN_SERVICE;
  269. spin_unlock(&conn->state_lock);
  270. for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
  271. rxrpc_call_is_secure(
  272. rcu_dereference_protected(
  273. conn->channels[loop].call,
  274. lockdep_is_held(&conn->channel_lock)));
  275. } else {
  276. spin_unlock(&conn->state_lock);
  277. }
  278. spin_unlock(&conn->channel_lock);
  279. return 0;
  280. default:
  281. _leave(" = -EPROTO [%u]", sp->hdr.type);
  282. return -EPROTO;
  283. }
  284. }
  285. /*
  286. * set up security and issue a challenge
  287. */
  288. static void rxrpc_secure_connection(struct rxrpc_connection *conn)
  289. {
  290. u32 abort_code;
  291. int ret;
  292. _enter("{%d}", conn->debug_id);
  293. ASSERT(conn->security_ix != 0);
  294. if (!conn->params.key) {
  295. _debug("set up security");
  296. ret = rxrpc_init_server_conn_security(conn);
  297. switch (ret) {
  298. case 0:
  299. break;
  300. case -ENOENT:
  301. abort_code = RX_CALL_DEAD;
  302. goto abort;
  303. default:
  304. abort_code = RXKADNOAUTH;
  305. goto abort;
  306. }
  307. }
  308. if (conn->security->issue_challenge(conn) < 0) {
  309. abort_code = RX_CALL_DEAD;
  310. ret = -ENOMEM;
  311. goto abort;
  312. }
  313. _leave("");
  314. return;
  315. abort:
  316. _debug("abort %d, %d", ret, abort_code);
  317. rxrpc_abort_connection(conn, -ret, abort_code);
  318. _leave(" [aborted]");
  319. }
  320. /*
  321. * connection-level event processor
  322. */
  323. void rxrpc_process_connection(struct work_struct *work)
  324. {
  325. struct rxrpc_connection *conn =
  326. container_of(work, struct rxrpc_connection, processor);
  327. struct sk_buff *skb;
  328. u32 abort_code = RX_PROTOCOL_ERROR;
  329. int ret;
  330. rxrpc_see_connection(conn);
  331. if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
  332. rxrpc_secure_connection(conn);
  333. /* go through the conn-level event packets, releasing the ref on this
  334. * connection that each one has when we've finished with it */
  335. while ((skb = skb_dequeue(&conn->rx_queue))) {
  336. rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
  337. ret = rxrpc_process_event(conn, skb, &abort_code);
  338. switch (ret) {
  339. case -EPROTO:
  340. case -EKEYEXPIRED:
  341. case -EKEYREJECTED:
  342. goto protocol_error;
  343. case -EAGAIN:
  344. goto requeue_and_leave;
  345. case -ECONNABORTED:
  346. default:
  347. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  348. break;
  349. }
  350. }
  351. out:
  352. rxrpc_put_connection(conn);
  353. _leave("");
  354. return;
  355. requeue_and_leave:
  356. skb_queue_head(&conn->rx_queue, skb);
  357. goto out;
  358. protocol_error:
  359. if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
  360. goto requeue_and_leave;
  361. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  362. _leave(" [EPROTO]");
  363. goto out;
  364. }