ar-connevent.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/errqueue.h>
  15. #include <linux/udp.h>
  16. #include <linux/in.h>
  17. #include <linux/in6.h>
  18. #include <linux/icmp.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include <net/ip.h>
  22. #include "ar-internal.h"
  23. /*
  24. * pass a connection-level abort onto all calls on that connection
  25. */
  26. static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
  27. u32 abort_code)
  28. {
  29. struct rxrpc_call *call;
  30. struct rb_node *p;
  31. _enter("{%d},%x", conn->debug_id, abort_code);
  32. read_lock_bh(&conn->lock);
  33. for (p = rb_first(&conn->calls); p; p = rb_next(p)) {
  34. call = rb_entry(p, struct rxrpc_call, conn_node);
  35. write_lock(&call->state_lock);
  36. if (call->state <= RXRPC_CALL_COMPLETE) {
  37. call->state = state;
  38. call->abort_code = abort_code;
  39. if (state == RXRPC_CALL_LOCALLY_ABORTED)
  40. set_bit(RXRPC_CALL_CONN_ABORT, &call->events);
  41. else
  42. set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
  43. rxrpc_queue_call(call);
  44. }
  45. write_unlock(&call->state_lock);
  46. }
  47. read_unlock_bh(&conn->lock);
  48. _leave("");
  49. }
  50. /*
  51. * generate a connection-level abort
  52. */
  53. static int rxrpc_abort_connection(struct rxrpc_connection *conn,
  54. u32 error, u32 abort_code)
  55. {
  56. struct rxrpc_header hdr;
  57. struct msghdr msg;
  58. struct kvec iov[2];
  59. __be32 word;
  60. size_t len;
  61. int ret;
  62. _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
  63. /* generate a connection-level abort */
  64. spin_lock_bh(&conn->state_lock);
  65. if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
  66. conn->state = RXRPC_CONN_LOCALLY_ABORTED;
  67. conn->error = error;
  68. spin_unlock_bh(&conn->state_lock);
  69. } else {
  70. spin_unlock_bh(&conn->state_lock);
  71. _leave(" = 0 [already dead]");
  72. return 0;
  73. }
  74. rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);
  75. msg.msg_name = &conn->trans->peer->srx.transport.sin;
  76. msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
  77. msg.msg_control = NULL;
  78. msg.msg_controllen = 0;
  79. msg.msg_flags = 0;
  80. hdr.epoch = conn->epoch;
  81. hdr.cid = conn->cid;
  82. hdr.callNumber = 0;
  83. hdr.seq = 0;
  84. hdr.type = RXRPC_PACKET_TYPE_ABORT;
  85. hdr.flags = conn->out_clientflag;
  86. hdr.userStatus = 0;
  87. hdr.securityIndex = conn->security_ix;
  88. hdr._rsvd = 0;
  89. hdr.serviceId = conn->service_id;
  90. word = htonl(abort_code);
  91. iov[0].iov_base = &hdr;
  92. iov[0].iov_len = sizeof(hdr);
  93. iov[1].iov_base = &word;
  94. iov[1].iov_len = sizeof(word);
  95. len = iov[0].iov_len + iov[1].iov_len;
  96. hdr.serial = htonl(atomic_inc_return(&conn->serial));
  97. _proto("Tx CONN ABORT %%%u { %d }", ntohl(hdr.serial), abort_code);
  98. ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
  99. if (ret < 0) {
  100. _debug("sendmsg failed: %d", ret);
  101. return -EAGAIN;
  102. }
  103. _leave(" = 0");
  104. return 0;
  105. }
  106. /*
  107. * mark a call as being on a now-secured channel
  108. * - must be called with softirqs disabled
  109. */
  110. static void rxrpc_call_is_secure(struct rxrpc_call *call)
  111. {
  112. _enter("%p", call);
  113. if (call) {
  114. read_lock(&call->state_lock);
  115. if (call->state < RXRPC_CALL_COMPLETE &&
  116. !test_and_set_bit(RXRPC_CALL_SECURED, &call->events))
  117. rxrpc_queue_call(call);
  118. read_unlock(&call->state_lock);
  119. }
  120. }
  121. /*
  122. * connection-level Rx packet processor
  123. */
  124. static int rxrpc_process_event(struct rxrpc_connection *conn,
  125. struct sk_buff *skb,
  126. u32 *_abort_code)
  127. {
  128. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  129. __be32 tmp;
  130. u32 serial;
  131. int loop, ret;
  132. if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
  133. kleave(" = -ECONNABORTED [%u]", conn->state);
  134. return -ECONNABORTED;
  135. }
  136. serial = ntohl(sp->hdr.serial);
  137. _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, serial);
  138. switch (sp->hdr.type) {
  139. case RXRPC_PACKET_TYPE_ABORT:
  140. if (skb_copy_bits(skb, 0, &tmp, sizeof(tmp)) < 0)
  141. return -EPROTO;
  142. _proto("Rx ABORT %%%u { ac=%d }", serial, ntohl(tmp));
  143. conn->state = RXRPC_CONN_REMOTELY_ABORTED;
  144. rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
  145. ntohl(tmp));
  146. return -ECONNABORTED;
  147. case RXRPC_PACKET_TYPE_CHALLENGE:
  148. if (conn->security)
  149. return conn->security->respond_to_challenge(
  150. conn, skb, _abort_code);
  151. return -EPROTO;
  152. case RXRPC_PACKET_TYPE_RESPONSE:
  153. if (!conn->security)
  154. return -EPROTO;
  155. ret = conn->security->verify_response(conn, skb, _abort_code);
  156. if (ret < 0)
  157. return ret;
  158. ret = conn->security->init_connection_security(conn);
  159. if (ret < 0)
  160. return ret;
  161. conn->security->prime_packet_security(conn);
  162. read_lock_bh(&conn->lock);
  163. spin_lock(&conn->state_lock);
  164. if (conn->state == RXRPC_CONN_SERVER_CHALLENGING) {
  165. conn->state = RXRPC_CONN_SERVER;
  166. for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
  167. rxrpc_call_is_secure(conn->channels[loop]);
  168. }
  169. spin_unlock(&conn->state_lock);
  170. read_unlock_bh(&conn->lock);
  171. return 0;
  172. default:
  173. _leave(" = -EPROTO [%u]", sp->hdr.type);
  174. return -EPROTO;
  175. }
  176. }
  177. /*
  178. * set up security and issue a challenge
  179. */
  180. static void rxrpc_secure_connection(struct rxrpc_connection *conn)
  181. {
  182. u32 abort_code;
  183. int ret;
  184. _enter("{%d}", conn->debug_id);
  185. ASSERT(conn->security_ix != 0);
  186. if (!conn->key) {
  187. _debug("set up security");
  188. ret = rxrpc_init_server_conn_security(conn);
  189. switch (ret) {
  190. case 0:
  191. break;
  192. case -ENOENT:
  193. abort_code = RX_CALL_DEAD;
  194. goto abort;
  195. default:
  196. abort_code = RXKADNOAUTH;
  197. goto abort;
  198. }
  199. }
  200. ASSERT(conn->security != NULL);
  201. if (conn->security->issue_challenge(conn) < 0) {
  202. abort_code = RX_CALL_DEAD;
  203. ret = -ENOMEM;
  204. goto abort;
  205. }
  206. _leave("");
  207. return;
  208. abort:
  209. _debug("abort %d, %d", ret, abort_code);
  210. rxrpc_abort_connection(conn, -ret, abort_code);
  211. _leave(" [aborted]");
  212. }
  213. /*
  214. * connection-level event processor
  215. */
  216. void rxrpc_process_connection(struct work_struct *work)
  217. {
  218. struct rxrpc_connection *conn =
  219. container_of(work, struct rxrpc_connection, processor);
  220. struct sk_buff *skb;
  221. u32 abort_code = RX_PROTOCOL_ERROR;
  222. int ret;
  223. _enter("{%d}", conn->debug_id);
  224. atomic_inc(&conn->usage);
  225. if (test_and_clear_bit(RXRPC_CONN_CHALLENGE, &conn->events)) {
  226. rxrpc_secure_connection(conn);
  227. rxrpc_put_connection(conn);
  228. }
  229. /* go through the conn-level event packets, releasing the ref on this
  230. * connection that each one has when we've finished with it */
  231. while ((skb = skb_dequeue(&conn->rx_queue))) {
  232. ret = rxrpc_process_event(conn, skb, &abort_code);
  233. switch (ret) {
  234. case -EPROTO:
  235. case -EKEYEXPIRED:
  236. case -EKEYREJECTED:
  237. goto protocol_error;
  238. case -EAGAIN:
  239. goto requeue_and_leave;
  240. case -ECONNABORTED:
  241. default:
  242. rxrpc_put_connection(conn);
  243. rxrpc_free_skb(skb);
  244. break;
  245. }
  246. }
  247. out:
  248. rxrpc_put_connection(conn);
  249. _leave("");
  250. return;
  251. requeue_and_leave:
  252. skb_queue_head(&conn->rx_queue, skb);
  253. goto out;
  254. protocol_error:
  255. if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
  256. goto requeue_and_leave;
  257. rxrpc_put_connection(conn);
  258. rxrpc_free_skb(skb);
  259. _leave(" [EPROTO]");
  260. goto out;
  261. }
  262. /*
  263. * put a packet up for transport-level abort
  264. */
  265. void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  266. {
  267. CHECK_SLAB_OKAY(&local->usage);
  268. if (!atomic_inc_not_zero(&local->usage)) {
  269. printk("resurrected on reject\n");
  270. BUG();
  271. }
  272. skb_queue_tail(&local->reject_queue, skb);
  273. rxrpc_queue_work(&local->rejecter);
  274. }
  275. /*
  276. * reject packets through the local endpoint
  277. */
  278. void rxrpc_reject_packets(struct work_struct *work)
  279. {
  280. union {
  281. struct sockaddr sa;
  282. struct sockaddr_in sin;
  283. } sa;
  284. struct rxrpc_skb_priv *sp;
  285. struct rxrpc_header hdr;
  286. struct rxrpc_local *local;
  287. struct sk_buff *skb;
  288. struct msghdr msg;
  289. struct kvec iov[2];
  290. size_t size;
  291. __be32 code;
  292. local = container_of(work, struct rxrpc_local, rejecter);
  293. rxrpc_get_local(local);
  294. _enter("%d", local->debug_id);
  295. iov[0].iov_base = &hdr;
  296. iov[0].iov_len = sizeof(hdr);
  297. iov[1].iov_base = &code;
  298. iov[1].iov_len = sizeof(code);
  299. size = sizeof(hdr) + sizeof(code);
  300. msg.msg_name = &sa;
  301. msg.msg_control = NULL;
  302. msg.msg_controllen = 0;
  303. msg.msg_flags = 0;
  304. memset(&sa, 0, sizeof(sa));
  305. sa.sa.sa_family = local->srx.transport.family;
  306. switch (sa.sa.sa_family) {
  307. case AF_INET:
  308. msg.msg_namelen = sizeof(sa.sin);
  309. break;
  310. default:
  311. msg.msg_namelen = 0;
  312. break;
  313. }
  314. memset(&hdr, 0, sizeof(hdr));
  315. hdr.type = RXRPC_PACKET_TYPE_ABORT;
  316. while ((skb = skb_dequeue(&local->reject_queue))) {
  317. sp = rxrpc_skb(skb);
  318. switch (sa.sa.sa_family) {
  319. case AF_INET:
  320. sa.sin.sin_port = udp_hdr(skb)->source;
  321. sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  322. code = htonl(skb->priority);
  323. hdr.epoch = sp->hdr.epoch;
  324. hdr.cid = sp->hdr.cid;
  325. hdr.callNumber = sp->hdr.callNumber;
  326. hdr.serviceId = sp->hdr.serviceId;
  327. hdr.flags = sp->hdr.flags;
  328. hdr.flags ^= RXRPC_CLIENT_INITIATED;
  329. hdr.flags &= RXRPC_CLIENT_INITIATED;
  330. kernel_sendmsg(local->socket, &msg, iov, 2, size);
  331. break;
  332. default:
  333. break;
  334. }
  335. rxrpc_free_skb(skb);
  336. rxrpc_put_local(local);
  337. }
  338. rxrpc_put_local(local);
  339. _leave("");
  340. }