ar-input.c 20 KB

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