conn_event.c 12 KB

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