ar-input.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. 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 int) 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);
  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, false);
  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_ACKALL:
  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_call *call,
  443. struct sk_buff *skb)
  444. {
  445. struct rxrpc_skb_priv *sp;
  446. _enter("%p,%p", call, skb);
  447. sp = rxrpc_skb(skb);
  448. _debug("extant call [%d]", call->state);
  449. read_lock(&call->state_lock);
  450. switch (call->state) {
  451. case RXRPC_CALL_LOCALLY_ABORTED:
  452. if (!test_and_set_bit(RXRPC_CALL_ABORT, &call->events)) {
  453. rxrpc_queue_call(call);
  454. goto free_unlock;
  455. }
  456. case RXRPC_CALL_REMOTELY_ABORTED:
  457. case RXRPC_CALL_NETWORK_ERROR:
  458. case RXRPC_CALL_DEAD:
  459. goto dead_call;
  460. case RXRPC_CALL_COMPLETE:
  461. case RXRPC_CALL_CLIENT_FINAL_ACK:
  462. /* complete server call */
  463. if (call->conn->in_clientflag)
  464. goto dead_call;
  465. /* resend last packet of a completed call */
  466. _debug("final ack again");
  467. rxrpc_get_call(call);
  468. set_bit(RXRPC_CALL_ACK_FINAL, &call->events);
  469. rxrpc_queue_call(call);
  470. goto free_unlock;
  471. default:
  472. break;
  473. }
  474. read_unlock(&call->state_lock);
  475. rxrpc_get_call(call);
  476. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  477. sp->hdr.flags & RXRPC_JUMBO_PACKET)
  478. rxrpc_process_jumbo_packet(call, skb);
  479. else
  480. rxrpc_fast_process_packet(call, skb);
  481. rxrpc_put_call(call);
  482. goto done;
  483. dead_call:
  484. if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
  485. skb->priority = RX_CALL_DEAD;
  486. rxrpc_reject_packet(call->conn->trans->local, skb);
  487. goto unlock;
  488. }
  489. free_unlock:
  490. rxrpc_free_skb(skb);
  491. unlock:
  492. read_unlock(&call->state_lock);
  493. done:
  494. _leave("");
  495. }
  496. /*
  497. * post connection-level events to the connection
  498. * - this includes challenges, responses and some aborts
  499. */
  500. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  501. struct sk_buff *skb)
  502. {
  503. _enter("%p,%p", conn, skb);
  504. atomic_inc(&conn->usage);
  505. skb_queue_tail(&conn->rx_queue, skb);
  506. rxrpc_queue_conn(conn);
  507. }
  508. static struct rxrpc_connection *rxrpc_conn_from_local(struct rxrpc_local *local,
  509. struct sk_buff *skb,
  510. struct rxrpc_skb_priv *sp)
  511. {
  512. struct rxrpc_peer *peer;
  513. struct rxrpc_transport *trans;
  514. struct rxrpc_connection *conn;
  515. peer = rxrpc_find_peer(local, ip_hdr(skb)->saddr,
  516. udp_hdr(skb)->source);
  517. if (IS_ERR(peer))
  518. goto cant_find_conn;
  519. trans = rxrpc_find_transport(local, peer);
  520. rxrpc_put_peer(peer);
  521. if (!trans)
  522. goto cant_find_conn;
  523. conn = rxrpc_find_connection(trans, &sp->hdr);
  524. rxrpc_put_transport(trans);
  525. if (!conn)
  526. goto cant_find_conn;
  527. return conn;
  528. cant_find_conn:
  529. return NULL;
  530. }
  531. /*
  532. * handle data received on the local endpoint
  533. * - may be called in interrupt context
  534. */
  535. void rxrpc_data_ready(struct sock *sk)
  536. {
  537. struct rxrpc_skb_priv *sp;
  538. struct rxrpc_local *local;
  539. struct sk_buff *skb;
  540. int ret;
  541. _enter("%p", sk);
  542. ASSERT(!irqs_disabled());
  543. read_lock_bh(&rxrpc_local_lock);
  544. local = sk->sk_user_data;
  545. if (local && atomic_read(&local->usage) > 0)
  546. rxrpc_get_local(local);
  547. else
  548. local = NULL;
  549. read_unlock_bh(&rxrpc_local_lock);
  550. if (!local) {
  551. _leave(" [local dead]");
  552. return;
  553. }
  554. skb = skb_recv_datagram(sk, 0, 1, &ret);
  555. if (!skb) {
  556. rxrpc_put_local(local);
  557. if (ret == -EAGAIN)
  558. return;
  559. _debug("UDP socket error %d", ret);
  560. return;
  561. }
  562. rxrpc_new_skb(skb);
  563. _net("recv skb %p", skb);
  564. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  565. if (skb_checksum_complete(skb)) {
  566. rxrpc_free_skb(skb);
  567. rxrpc_put_local(local);
  568. UDP_INC_STATS_BH(&init_net, UDP_MIB_INERRORS, 0);
  569. _leave(" [CSUM failed]");
  570. return;
  571. }
  572. UDP_INC_STATS_BH(&init_net, UDP_MIB_INDATAGRAMS, 0);
  573. /* the socket buffer we have is owned by UDP, with UDP's data all over
  574. * it, but we really want our own */
  575. skb_orphan(skb);
  576. sp = rxrpc_skb(skb);
  577. memset(sp, 0, sizeof(*sp));
  578. _net("Rx UDP packet from %08x:%04hu",
  579. ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
  580. /* dig out the RxRPC connection details */
  581. if (skb_copy_bits(skb, sizeof(struct udphdr), &sp->hdr,
  582. sizeof(sp->hdr)) < 0)
  583. goto bad_message;
  584. if (!pskb_pull(skb, sizeof(struct udphdr) + sizeof(sp->hdr)))
  585. BUG();
  586. _net("Rx RxRPC %s ep=%x call=%x:%x",
  587. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  588. ntohl(sp->hdr.epoch),
  589. ntohl(sp->hdr.cid),
  590. ntohl(sp->hdr.callNumber));
  591. if (sp->hdr.type == 0 || sp->hdr.type >= RXRPC_N_PACKET_TYPES) {
  592. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  593. goto bad_message;
  594. }
  595. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  596. (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
  597. goto bad_message;
  598. if (sp->hdr.callNumber == 0) {
  599. /* This is a connection-level packet. These should be
  600. * fairly rare, so the extra overhead of looking them up the
  601. * old-fashioned way doesn't really hurt */
  602. struct rxrpc_connection *conn;
  603. conn = rxrpc_conn_from_local(local, skb, sp);
  604. if (!conn)
  605. goto cant_route_call;
  606. _debug("CONN %p {%d}", conn, conn->debug_id);
  607. rxrpc_post_packet_to_conn(conn, skb);
  608. rxrpc_put_connection(conn);
  609. } else {
  610. struct rxrpc_call *call;
  611. u8 in_clientflag = 0;
  612. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  613. in_clientflag = RXRPC_CLIENT_INITIATED;
  614. call = rxrpc_find_call_hash(in_clientflag, sp->hdr.cid,
  615. sp->hdr.callNumber, sp->hdr.epoch,
  616. sp->hdr.serviceId, local, AF_INET,
  617. (u8 *)&ip_hdr(skb)->saddr);
  618. if (call)
  619. rxrpc_post_packet_to_call(call, skb);
  620. else
  621. goto cant_route_call;
  622. }
  623. rxrpc_put_local(local);
  624. return;
  625. cant_route_call:
  626. _debug("can't route call");
  627. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
  628. sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
  629. if (sp->hdr.seq == cpu_to_be32(1)) {
  630. _debug("first packet");
  631. skb_queue_tail(&local->accept_queue, skb);
  632. rxrpc_queue_work(&local->acceptor);
  633. rxrpc_put_local(local);
  634. _leave(" [incoming]");
  635. return;
  636. }
  637. skb->priority = RX_INVALID_OPERATION;
  638. } else {
  639. skb->priority = RX_CALL_DEAD;
  640. }
  641. if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
  642. _debug("reject type %d",sp->hdr.type);
  643. rxrpc_reject_packet(local, skb);
  644. }
  645. rxrpc_put_local(local);
  646. _leave(" [no call]");
  647. return;
  648. bad_message:
  649. skb->priority = RX_PROTOCOL_ERROR;
  650. rxrpc_reject_packet(local, skb);
  651. rxrpc_put_local(local);
  652. _leave(" [badmsg]");
  653. }