ar-input.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* RxRPC packet reception
  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. unsigned long rxrpc_ack_timeout = 1;
  24. const char *rxrpc_pkts[] = {
  25. "?00",
  26. "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG",
  27. "?09", "?10", "?11", "?12", "?13", "?14", "?15"
  28. };
  29. /*
  30. * queue a packet for recvmsg to pass to userspace
  31. * - the caller must hold a lock on call->lock
  32. * - must not be called with interrupts disabled (sk_filter() disables BH's)
  33. * - eats the packet whether successful or not
  34. * - there must be just one reference to the packet, which the caller passes to
  35. * this function
  36. */
  37. int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
  38. bool force, bool terminal)
  39. {
  40. struct rxrpc_skb_priv *sp;
  41. struct rxrpc_sock *rx = call->socket;
  42. struct sock *sk;
  43. int skb_len, ret;
  44. _enter(",,%d,%d", force, terminal);
  45. ASSERT(!irqs_disabled());
  46. sp = rxrpc_skb(skb);
  47. ASSERTCMP(sp->call, ==, call);
  48. /* if we've already posted the terminal message for a call, then we
  49. * don't post any more */
  50. if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
  51. _debug("already terminated");
  52. ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
  53. skb->destructor = NULL;
  54. sp->call = NULL;
  55. rxrpc_put_call(call);
  56. rxrpc_free_skb(skb);
  57. return 0;
  58. }
  59. sk = &rx->sk;
  60. if (!force) {
  61. /* cast skb->rcvbuf to unsigned... It's pointless, but
  62. * reduces number of warnings when compiling with -W
  63. * --ANK */
  64. // ret = -ENOBUFS;
  65. // if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  66. // (unsigned) sk->sk_rcvbuf)
  67. // goto out;
  68. ret = sk_filter(sk, skb);
  69. if (ret < 0)
  70. goto out;
  71. }
  72. spin_lock_bh(&sk->sk_receive_queue.lock);
  73. if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
  74. !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  75. call->socket->sk.sk_state != RXRPC_CLOSE) {
  76. skb->destructor = rxrpc_packet_destructor;
  77. skb->dev = NULL;
  78. skb->sk = sk;
  79. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  80. if (terminal) {
  81. _debug("<<<< TERMINAL MESSAGE >>>>");
  82. set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
  83. }
  84. /* allow interception by a kernel service */
  85. if (rx->interceptor) {
  86. rx->interceptor(sk, call->user_call_ID, skb);
  87. spin_unlock_bh(&sk->sk_receive_queue.lock);
  88. } else {
  89. /* Cache the SKB length before we tack it onto the
  90. * receive queue. Once it is added it no longer
  91. * belongs to us and may be freed by other threads of
  92. * control pulling packets from the queue */
  93. skb_len = skb->len;
  94. _net("post skb %p", skb);
  95. __skb_queue_tail(&sk->sk_receive_queue, skb);
  96. spin_unlock_bh(&sk->sk_receive_queue.lock);
  97. if (!sock_flag(sk, SOCK_DEAD))
  98. sk->sk_data_ready(sk, skb_len);
  99. }
  100. skb = NULL;
  101. } else {
  102. spin_unlock_bh(&sk->sk_receive_queue.lock);
  103. }
  104. ret = 0;
  105. out:
  106. /* release the socket buffer */
  107. if (skb) {
  108. skb->destructor = NULL;
  109. sp->call = NULL;
  110. rxrpc_put_call(call);
  111. rxrpc_free_skb(skb);
  112. }
  113. _leave(" = %d", ret);
  114. return ret;
  115. }
  116. /*
  117. * process a DATA packet, posting the packet to the appropriate queue
  118. * - eats the packet if successful
  119. */
  120. static int rxrpc_fast_process_data(struct rxrpc_call *call,
  121. struct sk_buff *skb, u32 seq)
  122. {
  123. struct rxrpc_skb_priv *sp;
  124. bool terminal;
  125. int ret, ackbit, ack;
  126. _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
  127. sp = rxrpc_skb(skb);
  128. ASSERTCMP(sp->call, ==, NULL);
  129. spin_lock(&call->lock);
  130. if (call->state > RXRPC_CALL_COMPLETE)
  131. goto discard;
  132. ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
  133. ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
  134. ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
  135. if (seq < call->rx_data_post) {
  136. _debug("dup #%u [-%u]", seq, call->rx_data_post);
  137. ack = RXRPC_ACK_DUPLICATE;
  138. ret = -ENOBUFS;
  139. goto discard_and_ack;
  140. }
  141. /* we may already have the packet in the out of sequence queue */
  142. ackbit = seq - (call->rx_data_eaten + 1);
  143. ASSERTCMP(ackbit, >=, 0);
  144. if (__test_and_set_bit(ackbit, call->ackr_window)) {
  145. _debug("dup oos #%u [%u,%u]",
  146. seq, call->rx_data_eaten, call->rx_data_post);
  147. ack = RXRPC_ACK_DUPLICATE;
  148. goto discard_and_ack;
  149. }
  150. if (seq >= call->ackr_win_top) {
  151. _debug("exceed #%u [%u]", seq, call->ackr_win_top);
  152. __clear_bit(ackbit, call->ackr_window);
  153. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  154. goto discard_and_ack;
  155. }
  156. if (seq == call->rx_data_expect) {
  157. clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
  158. call->rx_data_expect++;
  159. } else if (seq > call->rx_data_expect) {
  160. _debug("oos #%u [%u]", seq, call->rx_data_expect);
  161. call->rx_data_expect = seq + 1;
  162. if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
  163. ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  164. goto enqueue_and_ack;
  165. }
  166. goto enqueue_packet;
  167. }
  168. if (seq != call->rx_data_post) {
  169. _debug("ahead #%u [%u]", seq, call->rx_data_post);
  170. goto enqueue_packet;
  171. }
  172. if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
  173. goto protocol_error;
  174. /* if the packet need security things doing to it, then it goes down
  175. * the slow path */
  176. if (call->conn->security)
  177. goto enqueue_packet;
  178. sp->call = call;
  179. rxrpc_get_call(call);
  180. terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
  181. !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
  182. ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
  183. if (ret < 0) {
  184. if (ret == -ENOMEM || ret == -ENOBUFS) {
  185. __clear_bit(ackbit, call->ackr_window);
  186. ack = RXRPC_ACK_NOSPACE;
  187. goto discard_and_ack;
  188. }
  189. goto out;
  190. }
  191. skb = NULL;
  192. _debug("post #%u", seq);
  193. ASSERTCMP(call->rx_data_post, ==, seq);
  194. call->rx_data_post++;
  195. if (sp->hdr.flags & RXRPC_LAST_PACKET)
  196. set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
  197. /* if we've reached an out of sequence packet then we need to drain
  198. * that queue into the socket Rx queue now */
  199. if (call->rx_data_post == call->rx_first_oos) {
  200. _debug("drain rx oos now");
  201. read_lock(&call->state_lock);
  202. if (call->state < RXRPC_CALL_COMPLETE &&
  203. !test_and_set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events))
  204. rxrpc_queue_call(call);
  205. read_unlock(&call->state_lock);
  206. }
  207. spin_unlock(&call->lock);
  208. atomic_inc(&call->ackr_not_idle);
  209. rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, sp->hdr.serial, false);
  210. _leave(" = 0 [posted]");
  211. return 0;
  212. protocol_error:
  213. ret = -EBADMSG;
  214. out:
  215. spin_unlock(&call->lock);
  216. _leave(" = %d", ret);
  217. return ret;
  218. discard_and_ack:
  219. _debug("discard and ACK packet %p", skb);
  220. __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
  221. discard:
  222. spin_unlock(&call->lock);
  223. rxrpc_free_skb(skb);
  224. _leave(" = 0 [discarded]");
  225. return 0;
  226. enqueue_and_ack:
  227. __rxrpc_propose_ACK(call, ack, sp->hdr.serial, true);
  228. enqueue_packet:
  229. _net("defer skb %p", skb);
  230. spin_unlock(&call->lock);
  231. skb_queue_tail(&call->rx_queue, skb);
  232. atomic_inc(&call->ackr_not_idle);
  233. read_lock(&call->state_lock);
  234. if (call->state < RXRPC_CALL_DEAD)
  235. rxrpc_queue_call(call);
  236. read_unlock(&call->state_lock);
  237. _leave(" = 0 [queued]");
  238. return 0;
  239. }
  240. /*
  241. * assume an implicit ACKALL of the transmission phase of a client socket upon
  242. * reception of the first reply packet
  243. */
  244. static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
  245. {
  246. write_lock_bh(&call->state_lock);
  247. switch (call->state) {
  248. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  249. call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
  250. call->acks_latest = serial;
  251. _debug("implicit ACKALL %%%u", call->acks_latest);
  252. set_bit(RXRPC_CALL_RCVD_ACKALL, &call->events);
  253. write_unlock_bh(&call->state_lock);
  254. if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
  255. clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
  256. clear_bit(RXRPC_CALL_RESEND, &call->events);
  257. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  258. }
  259. break;
  260. default:
  261. write_unlock_bh(&call->state_lock);
  262. break;
  263. }
  264. }
  265. /*
  266. * post an incoming packet to the nominated call to deal with
  267. * - must get rid of the sk_buff, either by freeing it or by queuing it
  268. */
  269. void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
  270. {
  271. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  272. __be32 _abort_code;
  273. u32 serial, hi_serial, seq, abort_code;
  274. _enter("%p,%p", call, skb);
  275. ASSERT(!irqs_disabled());
  276. #if 0 // INJECT RX ERROR
  277. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
  278. static int skip = 0;
  279. if (++skip == 3) {
  280. printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
  281. skip = 0;
  282. goto free_packet;
  283. }
  284. }
  285. #endif
  286. /* track the latest serial number on this connection for ACK packet
  287. * information */
  288. serial = ntohl(sp->hdr.serial);
  289. hi_serial = atomic_read(&call->conn->hi_serial);
  290. while (serial > hi_serial)
  291. hi_serial = atomic_cmpxchg(&call->conn->hi_serial, hi_serial,
  292. serial);
  293. /* request ACK generation for any ACK or DATA packet that requests
  294. * it */
  295. if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  296. _proto("ACK Requested on %%%u", serial);
  297. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, sp->hdr.serial,
  298. !(sp->hdr.flags & RXRPC_MORE_PACKETS));
  299. }
  300. switch (sp->hdr.type) {
  301. case RXRPC_PACKET_TYPE_ABORT:
  302. _debug("abort");
  303. if (skb_copy_bits(skb, 0, &_abort_code,
  304. sizeof(_abort_code)) < 0)
  305. goto protocol_error;
  306. abort_code = ntohl(_abort_code);
  307. _proto("Rx ABORT %%%u { %x }", serial, abort_code);
  308. write_lock_bh(&call->state_lock);
  309. if (call->state < RXRPC_CALL_COMPLETE) {
  310. call->state = RXRPC_CALL_REMOTELY_ABORTED;
  311. call->abort_code = abort_code;
  312. set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
  313. rxrpc_queue_call(call);
  314. }
  315. goto free_packet_unlock;
  316. case RXRPC_PACKET_TYPE_BUSY:
  317. _proto("Rx BUSY %%%u", serial);
  318. if (call->conn->out_clientflag)
  319. goto protocol_error;
  320. write_lock_bh(&call->state_lock);
  321. switch (call->state) {
  322. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  323. call->state = RXRPC_CALL_SERVER_BUSY;
  324. set_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
  325. rxrpc_queue_call(call);
  326. case RXRPC_CALL_SERVER_BUSY:
  327. goto free_packet_unlock;
  328. default:
  329. goto protocol_error_locked;
  330. }
  331. default:
  332. _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], serial);
  333. goto protocol_error;
  334. case RXRPC_PACKET_TYPE_DATA:
  335. seq = ntohl(sp->hdr.seq);
  336. _proto("Rx DATA %%%u { #%u }", serial, seq);
  337. if (seq == 0)
  338. goto protocol_error;
  339. call->ackr_prev_seq = sp->hdr.seq;
  340. /* received data implicitly ACKs all of the request packets we
  341. * sent when we're acting as a client */
  342. if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
  343. rxrpc_assume_implicit_ackall(call, serial);
  344. switch (rxrpc_fast_process_data(call, skb, seq)) {
  345. case 0:
  346. skb = NULL;
  347. goto done;
  348. default:
  349. BUG();
  350. /* data packet received beyond the last packet */
  351. case -EBADMSG:
  352. goto protocol_error;
  353. }
  354. case RXRPC_PACKET_TYPE_ACK:
  355. /* ACK processing is done in process context */
  356. read_lock_bh(&call->state_lock);
  357. if (call->state < RXRPC_CALL_DEAD) {
  358. skb_queue_tail(&call->rx_queue, skb);
  359. rxrpc_queue_call(call);
  360. skb = NULL;
  361. }
  362. read_unlock_bh(&call->state_lock);
  363. goto free_packet;
  364. }
  365. protocol_error:
  366. _debug("protocol error");
  367. write_lock_bh(&call->state_lock);
  368. protocol_error_locked:
  369. if (call->state <= RXRPC_CALL_COMPLETE) {
  370. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  371. call->abort_code = RX_PROTOCOL_ERROR;
  372. set_bit(RXRPC_CALL_ABORT, &call->events);
  373. rxrpc_queue_call(call);
  374. }
  375. free_packet_unlock:
  376. write_unlock_bh(&call->state_lock);
  377. free_packet:
  378. rxrpc_free_skb(skb);
  379. done:
  380. _leave("");
  381. }
  382. /*
  383. * split up a jumbo data packet
  384. */
  385. static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
  386. struct sk_buff *jumbo)
  387. {
  388. struct rxrpc_jumbo_header jhdr;
  389. struct rxrpc_skb_priv *sp;
  390. struct sk_buff *part;
  391. _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
  392. sp = rxrpc_skb(jumbo);
  393. do {
  394. sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
  395. /* make a clone to represent the first subpacket in what's left
  396. * of the jumbo packet */
  397. part = skb_clone(jumbo, GFP_ATOMIC);
  398. if (!part) {
  399. /* simply ditch the tail in the event of ENOMEM */
  400. pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
  401. break;
  402. }
  403. rxrpc_new_skb(part);
  404. pskb_trim(part, RXRPC_JUMBO_DATALEN);
  405. if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
  406. goto protocol_error;
  407. if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
  408. goto protocol_error;
  409. if (!pskb_pull(jumbo, sizeof(jhdr)))
  410. BUG();
  411. sp->hdr.seq = htonl(ntohl(sp->hdr.seq) + 1);
  412. sp->hdr.serial = htonl(ntohl(sp->hdr.serial) + 1);
  413. sp->hdr.flags = jhdr.flags;
  414. sp->hdr._rsvd = jhdr._rsvd;
  415. _proto("Rx DATA Jumbo %%%u", ntohl(sp->hdr.serial) - 1);
  416. rxrpc_fast_process_packet(call, part);
  417. part = NULL;
  418. } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
  419. rxrpc_fast_process_packet(call, jumbo);
  420. _leave("");
  421. return;
  422. protocol_error:
  423. _debug("protocol error");
  424. rxrpc_free_skb(part);
  425. rxrpc_free_skb(jumbo);
  426. write_lock_bh(&call->state_lock);
  427. if (call->state <= RXRPC_CALL_COMPLETE) {
  428. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  429. call->abort_code = RX_PROTOCOL_ERROR;
  430. set_bit(RXRPC_CALL_ABORT, &call->events);
  431. rxrpc_queue_call(call);
  432. }
  433. write_unlock_bh(&call->state_lock);
  434. _leave("");
  435. }
  436. /*
  437. * post an incoming packet to the appropriate call/socket to deal with
  438. * - must get rid of the sk_buff, either by freeing it or by queuing it
  439. */
  440. static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
  441. struct sk_buff *skb)
  442. {
  443. struct rxrpc_skb_priv *sp;
  444. struct rxrpc_call *call;
  445. struct rb_node *p;
  446. __be32 call_id;
  447. _enter("%p,%p", conn, skb);
  448. read_lock_bh(&conn->lock);
  449. sp = rxrpc_skb(skb);
  450. /* look at extant calls by channel number first */
  451. call = conn->channels[ntohl(sp->hdr.cid) & RXRPC_CHANNELMASK];
  452. if (!call || call->call_id != sp->hdr.callNumber)
  453. goto call_not_extant;
  454. _debug("extant call [%d]", call->state);
  455. ASSERTCMP(call->conn, ==, conn);
  456. read_lock(&call->state_lock);
  457. switch (call->state) {
  458. case RXRPC_CALL_LOCALLY_ABORTED:
  459. if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events))
  460. rxrpc_queue_call(call);
  461. case RXRPC_CALL_REMOTELY_ABORTED:
  462. case RXRPC_CALL_NETWORK_ERROR:
  463. case RXRPC_CALL_DEAD:
  464. goto free_unlock;
  465. default:
  466. break;
  467. }
  468. read_unlock(&call->state_lock);
  469. rxrpc_get_call(call);
  470. read_unlock_bh(&conn->lock);
  471. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  472. sp->hdr.flags & RXRPC_JUMBO_PACKET)
  473. rxrpc_process_jumbo_packet(call, skb);
  474. else
  475. rxrpc_fast_process_packet(call, skb);
  476. rxrpc_put_call(call);
  477. goto done;
  478. call_not_extant:
  479. /* search the completed calls in case what we're dealing with is
  480. * there */
  481. _debug("call not extant");
  482. call_id = sp->hdr.callNumber;
  483. p = conn->calls.rb_node;
  484. while (p) {
  485. call = rb_entry(p, struct rxrpc_call, conn_node);
  486. if (call_id < call->call_id)
  487. p = p->rb_left;
  488. else if (call_id > call->call_id)
  489. p = p->rb_right;
  490. else
  491. goto found_completed_call;
  492. }
  493. dead_call:
  494. /* it's a either a really old call that we no longer remember or its a
  495. * new incoming call */
  496. read_unlock_bh(&conn->lock);
  497. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
  498. sp->hdr.seq == __constant_cpu_to_be32(1)) {
  499. _debug("incoming call");
  500. skb_queue_tail(&conn->trans->local->accept_queue, skb);
  501. rxrpc_queue_work(&conn->trans->local->acceptor);
  502. goto done;
  503. }
  504. _debug("dead call");
  505. skb->priority = RX_CALL_DEAD;
  506. rxrpc_reject_packet(conn->trans->local, skb);
  507. goto done;
  508. /* resend last packet of a completed call
  509. * - client calls may have been aborted or ACK'd
  510. * - server calls may have been aborted
  511. */
  512. found_completed_call:
  513. _debug("completed call");
  514. if (atomic_read(&call->usage) == 0)
  515. goto dead_call;
  516. /* synchronise any state changes */
  517. read_lock(&call->state_lock);
  518. ASSERTIFCMP(call->state != RXRPC_CALL_CLIENT_FINAL_ACK,
  519. call->state, >=, RXRPC_CALL_COMPLETE);
  520. if (call->state == RXRPC_CALL_LOCALLY_ABORTED ||
  521. call->state == RXRPC_CALL_REMOTELY_ABORTED ||
  522. call->state == RXRPC_CALL_DEAD) {
  523. read_unlock(&call->state_lock);
  524. goto dead_call;
  525. }
  526. if (call->conn->in_clientflag) {
  527. read_unlock(&call->state_lock);
  528. goto dead_call; /* complete server call */
  529. }
  530. _debug("final ack again");
  531. rxrpc_get_call(call);
  532. set_bit(RXRPC_CALL_ACK_FINAL, &call->events);
  533. rxrpc_queue_call(call);
  534. free_unlock:
  535. read_unlock(&call->state_lock);
  536. read_unlock_bh(&conn->lock);
  537. rxrpc_free_skb(skb);
  538. done:
  539. _leave("");
  540. }
  541. /*
  542. * post connection-level events to the connection
  543. * - this includes challenges, responses and some aborts
  544. */
  545. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  546. struct sk_buff *skb)
  547. {
  548. _enter("%p,%p", conn, skb);
  549. atomic_inc(&conn->usage);
  550. skb_queue_tail(&conn->rx_queue, skb);
  551. rxrpc_queue_conn(conn);
  552. }
  553. /*
  554. * handle data received on the local endpoint
  555. * - may be called in interrupt context
  556. */
  557. void rxrpc_data_ready(struct sock *sk, int count)
  558. {
  559. struct rxrpc_connection *conn;
  560. struct rxrpc_transport *trans;
  561. struct rxrpc_skb_priv *sp;
  562. struct rxrpc_local *local;
  563. struct rxrpc_peer *peer;
  564. struct sk_buff *skb;
  565. int ret;
  566. _enter("%p, %d", sk, count);
  567. ASSERT(!irqs_disabled());
  568. read_lock_bh(&rxrpc_local_lock);
  569. local = sk->sk_user_data;
  570. if (local && atomic_read(&local->usage) > 0)
  571. rxrpc_get_local(local);
  572. else
  573. local = NULL;
  574. read_unlock_bh(&rxrpc_local_lock);
  575. if (!local) {
  576. _leave(" [local dead]");
  577. return;
  578. }
  579. skb = skb_recv_datagram(sk, 0, 1, &ret);
  580. if (!skb) {
  581. rxrpc_put_local(local);
  582. if (ret == -EAGAIN)
  583. return;
  584. _debug("UDP socket error %d", ret);
  585. return;
  586. }
  587. rxrpc_new_skb(skb);
  588. _net("recv skb %p", skb);
  589. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  590. if (skb_checksum_complete(skb)) {
  591. rxrpc_free_skb(skb);
  592. rxrpc_put_local(local);
  593. _leave(" [CSUM failed]");
  594. return;
  595. }
  596. /* the socket buffer we have is owned by UDP, with UDP's data all over
  597. * it, but we really want our own */
  598. skb_orphan(skb);
  599. sp = rxrpc_skb(skb);
  600. memset(sp, 0, sizeof(*sp));
  601. _net("Rx UDP packet from %08x:%04hu",
  602. ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
  603. /* dig out the RxRPC connection details */
  604. if (skb_copy_bits(skb, sizeof(struct udphdr), &sp->hdr,
  605. sizeof(sp->hdr)) < 0)
  606. goto bad_message;
  607. if (!pskb_pull(skb, sizeof(struct udphdr) + sizeof(sp->hdr)))
  608. BUG();
  609. _net("Rx RxRPC %s ep=%x call=%x:%x",
  610. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  611. ntohl(sp->hdr.epoch),
  612. ntohl(sp->hdr.cid),
  613. ntohl(sp->hdr.callNumber));
  614. if (sp->hdr.type == 0 || sp->hdr.type >= RXRPC_N_PACKET_TYPES) {
  615. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  616. goto bad_message;
  617. }
  618. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  619. (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
  620. goto bad_message;
  621. peer = rxrpc_find_peer(local, ip_hdr(skb)->saddr, udp_hdr(skb)->source);
  622. if (IS_ERR(peer))
  623. goto cant_route_call;
  624. trans = rxrpc_find_transport(local, peer);
  625. rxrpc_put_peer(peer);
  626. if (!trans)
  627. goto cant_route_call;
  628. conn = rxrpc_find_connection(trans, &sp->hdr);
  629. rxrpc_put_transport(trans);
  630. if (!conn)
  631. goto cant_route_call;
  632. _debug("CONN %p {%d}", conn, conn->debug_id);
  633. if (sp->hdr.callNumber == 0)
  634. rxrpc_post_packet_to_conn(conn, skb);
  635. else
  636. rxrpc_post_packet_to_call(conn, skb);
  637. rxrpc_put_connection(conn);
  638. rxrpc_put_local(local);
  639. return;
  640. cant_route_call:
  641. _debug("can't route call");
  642. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
  643. sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
  644. if (sp->hdr.seq == __constant_cpu_to_be32(1)) {
  645. _debug("first packet");
  646. skb_queue_tail(&local->accept_queue, skb);
  647. rxrpc_queue_work(&local->acceptor);
  648. rxrpc_put_local(local);
  649. _leave(" [incoming]");
  650. return;
  651. }
  652. skb->priority = RX_INVALID_OPERATION;
  653. } else {
  654. skb->priority = RX_CALL_DEAD;
  655. }
  656. _debug("reject");
  657. rxrpc_reject_packet(local, skb);
  658. rxrpc_put_local(local);
  659. _leave(" [no call]");
  660. return;
  661. bad_message:
  662. skb->priority = RX_PROTOCOL_ERROR;
  663. rxrpc_reject_packet(local, skb);
  664. rxrpc_put_local(local);
  665. _leave(" [badmsg]");
  666. }