ar-input.c 20 KB

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