ar-input.c 20 KB

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