ar-input.c 19 KB

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