input.c 19 KB

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