input.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /* RxRPC packet reception
  2. *
  3. * Copyright (C) 2007, 2016 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. static void rxrpc_proto_abort(const char *why,
  28. struct rxrpc_call *call, rxrpc_seq_t seq)
  29. {
  30. if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
  31. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  32. rxrpc_queue_call(call);
  33. }
  34. }
  35. /*
  36. * Do TCP-style congestion management [RFC 5681].
  37. */
  38. static void rxrpc_congestion_management(struct rxrpc_call *call,
  39. struct sk_buff *skb,
  40. struct rxrpc_ack_summary *summary,
  41. rxrpc_serial_t acked_serial)
  42. {
  43. enum rxrpc_congest_change change = rxrpc_cong_no_change;
  44. unsigned int cumulative_acks = call->cong_cumul_acks;
  45. unsigned int cwnd = call->cong_cwnd;
  46. bool resend = false;
  47. summary->flight_size =
  48. (call->tx_top - call->tx_hard_ack) - summary->nr_acks;
  49. if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
  50. summary->retrans_timeo = true;
  51. call->cong_ssthresh = max_t(unsigned int,
  52. summary->flight_size / 2, 2);
  53. cwnd = 1;
  54. if (cwnd >= call->cong_ssthresh &&
  55. call->cong_mode == RXRPC_CALL_SLOW_START) {
  56. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  57. call->cong_tstamp = skb->tstamp;
  58. cumulative_acks = 0;
  59. }
  60. }
  61. cumulative_acks += summary->nr_new_acks;
  62. cumulative_acks += summary->nr_rot_new_acks;
  63. if (cumulative_acks > 255)
  64. cumulative_acks = 255;
  65. summary->mode = call->cong_mode;
  66. summary->cwnd = call->cong_cwnd;
  67. summary->ssthresh = call->cong_ssthresh;
  68. summary->cumulative_acks = cumulative_acks;
  69. summary->dup_acks = call->cong_dup_acks;
  70. switch (call->cong_mode) {
  71. case RXRPC_CALL_SLOW_START:
  72. if (summary->nr_nacks > 0)
  73. goto packet_loss_detected;
  74. if (summary->cumulative_acks > 0)
  75. cwnd += 1;
  76. if (cwnd >= call->cong_ssthresh) {
  77. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  78. call->cong_tstamp = skb->tstamp;
  79. }
  80. goto out;
  81. case RXRPC_CALL_CONGEST_AVOIDANCE:
  82. if (summary->nr_nacks > 0)
  83. goto packet_loss_detected;
  84. /* We analyse the number of packets that get ACK'd per RTT
  85. * period and increase the window if we managed to fill it.
  86. */
  87. if (call->peer->rtt_usage == 0)
  88. goto out;
  89. if (ktime_before(skb->tstamp,
  90. ktime_add_ns(call->cong_tstamp,
  91. call->peer->rtt)))
  92. goto out_no_clear_ca;
  93. change = rxrpc_cong_rtt_window_end;
  94. call->cong_tstamp = skb->tstamp;
  95. if (cumulative_acks >= cwnd)
  96. cwnd++;
  97. goto out;
  98. case RXRPC_CALL_PACKET_LOSS:
  99. if (summary->nr_nacks == 0)
  100. goto resume_normality;
  101. if (summary->new_low_nack) {
  102. change = rxrpc_cong_new_low_nack;
  103. call->cong_dup_acks = 1;
  104. if (call->cong_extra > 1)
  105. call->cong_extra = 1;
  106. goto send_extra_data;
  107. }
  108. call->cong_dup_acks++;
  109. if (call->cong_dup_acks < 3)
  110. goto send_extra_data;
  111. change = rxrpc_cong_begin_retransmission;
  112. call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
  113. call->cong_ssthresh = max_t(unsigned int,
  114. summary->flight_size / 2, 2);
  115. cwnd = call->cong_ssthresh + 3;
  116. call->cong_extra = 0;
  117. call->cong_dup_acks = 0;
  118. resend = true;
  119. goto out;
  120. case RXRPC_CALL_FAST_RETRANSMIT:
  121. if (!summary->new_low_nack) {
  122. if (summary->nr_new_acks == 0)
  123. cwnd += 1;
  124. call->cong_dup_acks++;
  125. if (call->cong_dup_acks == 2) {
  126. change = rxrpc_cong_retransmit_again;
  127. call->cong_dup_acks = 0;
  128. resend = true;
  129. }
  130. } else {
  131. change = rxrpc_cong_progress;
  132. cwnd = call->cong_ssthresh;
  133. if (summary->nr_nacks == 0)
  134. goto resume_normality;
  135. }
  136. goto out;
  137. default:
  138. BUG();
  139. goto out;
  140. }
  141. resume_normality:
  142. change = rxrpc_cong_cleared_nacks;
  143. call->cong_dup_acks = 0;
  144. call->cong_extra = 0;
  145. call->cong_tstamp = skb->tstamp;
  146. if (cwnd < call->cong_ssthresh)
  147. call->cong_mode = RXRPC_CALL_SLOW_START;
  148. else
  149. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  150. out:
  151. cumulative_acks = 0;
  152. out_no_clear_ca:
  153. if (cwnd >= RXRPC_RXTX_BUFF_SIZE - 1)
  154. cwnd = RXRPC_RXTX_BUFF_SIZE - 1;
  155. call->cong_cwnd = cwnd;
  156. call->cong_cumul_acks = cumulative_acks;
  157. trace_rxrpc_congest(call, summary, acked_serial, change);
  158. if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  159. rxrpc_queue_call(call);
  160. return;
  161. packet_loss_detected:
  162. change = rxrpc_cong_saw_nack;
  163. call->cong_mode = RXRPC_CALL_PACKET_LOSS;
  164. call->cong_dup_acks = 0;
  165. goto send_extra_data;
  166. send_extra_data:
  167. /* Send some previously unsent DATA if we have some to advance the ACK
  168. * state.
  169. */
  170. if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  171. RXRPC_TX_ANNO_LAST ||
  172. summary->nr_acks != call->tx_top - call->tx_hard_ack) {
  173. call->cong_extra++;
  174. wake_up(&call->waitq);
  175. }
  176. goto out_no_clear_ca;
  177. }
  178. /*
  179. * Ping the other end to fill our RTT cache and to retrieve the rwind
  180. * and MTU parameters.
  181. */
  182. static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
  183. int skew)
  184. {
  185. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  186. ktime_t now = skb->tstamp;
  187. if (call->peer->rtt_usage < 3 ||
  188. ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
  189. rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  190. true, true,
  191. rxrpc_propose_ack_ping_for_params);
  192. }
  193. /*
  194. * Apply a hard ACK by advancing the Tx window.
  195. */
  196. static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
  197. struct rxrpc_ack_summary *summary)
  198. {
  199. struct sk_buff *skb, *list = NULL;
  200. int ix;
  201. u8 annotation;
  202. if (call->acks_lowest_nak == call->tx_hard_ack) {
  203. call->acks_lowest_nak = to;
  204. } else if (before_eq(call->acks_lowest_nak, to)) {
  205. summary->new_low_nack = true;
  206. call->acks_lowest_nak = to;
  207. }
  208. spin_lock(&call->lock);
  209. while (before(call->tx_hard_ack, to)) {
  210. call->tx_hard_ack++;
  211. ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
  212. skb = call->rxtx_buffer[ix];
  213. annotation = call->rxtx_annotations[ix];
  214. rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
  215. call->rxtx_buffer[ix] = NULL;
  216. call->rxtx_annotations[ix] = 0;
  217. skb->next = list;
  218. list = skb;
  219. if (annotation & RXRPC_TX_ANNO_LAST)
  220. set_bit(RXRPC_CALL_TX_LAST, &call->flags);
  221. if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)
  222. summary->nr_rot_new_acks++;
  223. }
  224. spin_unlock(&call->lock);
  225. trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
  226. rxrpc_transmit_rotate_last :
  227. rxrpc_transmit_rotate));
  228. wake_up(&call->waitq);
  229. while (list) {
  230. skb = list;
  231. list = skb->next;
  232. skb->next = NULL;
  233. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  234. }
  235. }
  236. /*
  237. * End the transmission phase of a call.
  238. *
  239. * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
  240. * or a final ACK packet.
  241. */
  242. static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
  243. const char *abort_why)
  244. {
  245. ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
  246. write_lock(&call->state_lock);
  247. switch (call->state) {
  248. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  249. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  250. if (reply_begun)
  251. call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
  252. else
  253. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  254. break;
  255. case RXRPC_CALL_SERVER_AWAIT_ACK:
  256. __rxrpc_call_completed(call);
  257. rxrpc_notify_socket(call);
  258. break;
  259. default:
  260. goto bad_state;
  261. }
  262. write_unlock(&call->state_lock);
  263. if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
  264. trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
  265. } else {
  266. trace_rxrpc_transmit(call, rxrpc_transmit_end);
  267. }
  268. _leave(" = ok");
  269. return true;
  270. bad_state:
  271. write_unlock(&call->state_lock);
  272. kdebug("end_tx %s", rxrpc_call_states[call->state]);
  273. rxrpc_proto_abort(abort_why, call, call->tx_top);
  274. return false;
  275. }
  276. /*
  277. * Begin the reply reception phase of a call.
  278. */
  279. static bool rxrpc_receiving_reply(struct rxrpc_call *call)
  280. {
  281. struct rxrpc_ack_summary summary = { 0 };
  282. unsigned long now, timo;
  283. rxrpc_seq_t top = READ_ONCE(call->tx_top);
  284. if (call->ackr_reason) {
  285. spin_lock_bh(&call->lock);
  286. call->ackr_reason = 0;
  287. spin_unlock_bh(&call->lock);
  288. now = jiffies;
  289. timo = now + MAX_JIFFY_OFFSET;
  290. WRITE_ONCE(call->resend_at, timo);
  291. WRITE_ONCE(call->ack_at, timo);
  292. trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
  293. }
  294. if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  295. rxrpc_rotate_tx_window(call, top, &summary);
  296. if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  297. rxrpc_proto_abort("TXL", call, top);
  298. return false;
  299. }
  300. if (!rxrpc_end_tx_phase(call, true, "ETD"))
  301. return false;
  302. call->tx_phase = false;
  303. return true;
  304. }
  305. /*
  306. * Scan a jumbo packet to validate its structure and to work out how many
  307. * subpackets it contains.
  308. *
  309. * A jumbo packet is a collection of consecutive packets glued together with
  310. * little headers between that indicate how to change the initial header for
  311. * each subpacket.
  312. *
  313. * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
  314. * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
  315. * size.
  316. */
  317. static bool rxrpc_validate_jumbo(struct sk_buff *skb)
  318. {
  319. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  320. unsigned int offset = sizeof(struct rxrpc_wire_header);
  321. unsigned int len = skb->len;
  322. int nr_jumbo = 1;
  323. u8 flags = sp->hdr.flags;
  324. do {
  325. nr_jumbo++;
  326. if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
  327. goto protocol_error;
  328. if (flags & RXRPC_LAST_PACKET)
  329. goto protocol_error;
  330. offset += RXRPC_JUMBO_DATALEN;
  331. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  332. goto protocol_error;
  333. offset += sizeof(struct rxrpc_jumbo_header);
  334. } while (flags & RXRPC_JUMBO_PACKET);
  335. sp->nr_jumbo = nr_jumbo;
  336. return true;
  337. protocol_error:
  338. return false;
  339. }
  340. /*
  341. * Handle reception of a duplicate packet.
  342. *
  343. * We have to take care to avoid an attack here whereby we're given a series of
  344. * jumbograms, each with a sequence number one before the preceding one and
  345. * filled up to maximum UDP size. If they never send us the first packet in
  346. * the sequence, they can cause us to have to hold on to around 2MiB of kernel
  347. * space until the call times out.
  348. *
  349. * We limit the space usage by only accepting three duplicate jumbo packets per
  350. * call. After that, we tell the other side we're no longer accepting jumbos
  351. * (that information is encoded in the ACK packet).
  352. */
  353. static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
  354. u8 annotation, bool *_jumbo_bad)
  355. {
  356. /* Discard normal packets that are duplicates. */
  357. if (annotation == 0)
  358. return;
  359. /* Skip jumbo subpackets that are duplicates. When we've had three or
  360. * more partially duplicate jumbo packets, we refuse to take any more
  361. * jumbos for this call.
  362. */
  363. if (!*_jumbo_bad) {
  364. call->nr_jumbo_bad++;
  365. *_jumbo_bad = true;
  366. }
  367. }
  368. /*
  369. * Process a DATA packet, adding the packet to the Rx ring.
  370. */
  371. static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
  372. u16 skew)
  373. {
  374. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  375. enum rxrpc_call_state state;
  376. unsigned int offset = sizeof(struct rxrpc_wire_header);
  377. unsigned int ix;
  378. rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
  379. rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
  380. bool immediate_ack = false, jumbo_bad = false, queued;
  381. u16 len;
  382. u8 ack = 0, flags, annotation = 0;
  383. _enter("{%u,%u},{%u,%u}",
  384. call->rx_hard_ack, call->rx_top, skb->len, seq);
  385. _proto("Rx DATA %%%u { #%u f=%02x }",
  386. sp->hdr.serial, seq, sp->hdr.flags);
  387. state = READ_ONCE(call->state);
  388. if (state >= RXRPC_CALL_COMPLETE)
  389. return;
  390. if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST) {
  391. unsigned long timo = READ_ONCE(call->next_req_timo);
  392. unsigned long now, expect_req_by;
  393. if (timo) {
  394. now = jiffies;
  395. expect_req_by = now + timo;
  396. WRITE_ONCE(call->expect_req_by, expect_req_by);
  397. rxrpc_reduce_call_timer(call, expect_req_by, now,
  398. rxrpc_timer_set_for_idle);
  399. }
  400. }
  401. /* Received data implicitly ACKs all of the request packets we sent
  402. * when we're acting as a client.
  403. */
  404. if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  405. state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
  406. !rxrpc_receiving_reply(call))
  407. return;
  408. call->ackr_prev_seq = seq;
  409. hard_ack = READ_ONCE(call->rx_hard_ack);
  410. if (after(seq, hard_ack + call->rx_winsize)) {
  411. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  412. ack_serial = serial;
  413. goto ack;
  414. }
  415. flags = sp->hdr.flags;
  416. if (flags & RXRPC_JUMBO_PACKET) {
  417. if (call->nr_jumbo_bad > 3) {
  418. ack = RXRPC_ACK_NOSPACE;
  419. ack_serial = serial;
  420. goto ack;
  421. }
  422. annotation = 1;
  423. }
  424. next_subpacket:
  425. queued = false;
  426. ix = seq & RXRPC_RXTX_BUFF_MASK;
  427. len = skb->len;
  428. if (flags & RXRPC_JUMBO_PACKET)
  429. len = RXRPC_JUMBO_DATALEN;
  430. if (flags & RXRPC_LAST_PACKET) {
  431. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  432. seq != call->rx_top)
  433. return rxrpc_proto_abort("LSN", call, seq);
  434. } else {
  435. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  436. after_eq(seq, call->rx_top))
  437. return rxrpc_proto_abort("LSA", call, seq);
  438. }
  439. trace_rxrpc_rx_data(call->debug_id, seq, serial, flags, annotation);
  440. if (before_eq(seq, hard_ack)) {
  441. ack = RXRPC_ACK_DUPLICATE;
  442. ack_serial = serial;
  443. goto skip;
  444. }
  445. if (flags & RXRPC_REQUEST_ACK && !ack) {
  446. ack = RXRPC_ACK_REQUESTED;
  447. ack_serial = serial;
  448. }
  449. if (call->rxtx_buffer[ix]) {
  450. rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
  451. if (ack != RXRPC_ACK_DUPLICATE) {
  452. ack = RXRPC_ACK_DUPLICATE;
  453. ack_serial = serial;
  454. }
  455. immediate_ack = true;
  456. goto skip;
  457. }
  458. /* Queue the packet. We use a couple of memory barriers here as need
  459. * to make sure that rx_top is perceived to be set after the buffer
  460. * pointer and that the buffer pointer is set after the annotation and
  461. * the skb data.
  462. *
  463. * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
  464. * and also rxrpc_fill_out_ack().
  465. */
  466. rxrpc_get_skb(skb, rxrpc_skb_rx_got);
  467. call->rxtx_annotations[ix] = annotation;
  468. smp_wmb();
  469. call->rxtx_buffer[ix] = skb;
  470. if (after(seq, call->rx_top)) {
  471. smp_store_release(&call->rx_top, seq);
  472. } else if (before(seq, call->rx_top)) {
  473. /* Send an immediate ACK if we fill in a hole */
  474. if (!ack) {
  475. ack = RXRPC_ACK_DELAY;
  476. ack_serial = serial;
  477. }
  478. immediate_ack = true;
  479. }
  480. if (flags & RXRPC_LAST_PACKET) {
  481. set_bit(RXRPC_CALL_RX_LAST, &call->flags);
  482. trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
  483. } else {
  484. trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
  485. }
  486. queued = true;
  487. if (after_eq(seq, call->rx_expect_next)) {
  488. if (after(seq, call->rx_expect_next)) {
  489. _net("OOS %u > %u", seq, call->rx_expect_next);
  490. ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  491. ack_serial = serial;
  492. }
  493. call->rx_expect_next = seq + 1;
  494. }
  495. skip:
  496. offset += len;
  497. if (flags & RXRPC_JUMBO_PACKET) {
  498. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  499. return rxrpc_proto_abort("XJF", call, seq);
  500. offset += sizeof(struct rxrpc_jumbo_header);
  501. seq++;
  502. serial++;
  503. annotation++;
  504. if (flags & RXRPC_JUMBO_PACKET)
  505. annotation |= RXRPC_RX_ANNO_JLAST;
  506. if (after(seq, hard_ack + call->rx_winsize)) {
  507. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  508. ack_serial = serial;
  509. if (!jumbo_bad) {
  510. call->nr_jumbo_bad++;
  511. jumbo_bad = true;
  512. }
  513. goto ack;
  514. }
  515. _proto("Rx DATA Jumbo %%%u", serial);
  516. goto next_subpacket;
  517. }
  518. if (queued && flags & RXRPC_LAST_PACKET && !ack) {
  519. ack = RXRPC_ACK_DELAY;
  520. ack_serial = serial;
  521. }
  522. ack:
  523. if (ack)
  524. rxrpc_propose_ACK(call, ack, skew, ack_serial,
  525. immediate_ack, true,
  526. rxrpc_propose_ack_input_data);
  527. else
  528. rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial,
  529. false, true,
  530. rxrpc_propose_ack_input_data);
  531. if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1) {
  532. trace_rxrpc_notify_socket(call->debug_id, serial);
  533. rxrpc_notify_socket(call);
  534. }
  535. _leave(" [queued]");
  536. }
  537. /*
  538. * Process a requested ACK.
  539. */
  540. static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  541. ktime_t resp_time,
  542. rxrpc_serial_t orig_serial,
  543. rxrpc_serial_t ack_serial)
  544. {
  545. struct rxrpc_skb_priv *sp;
  546. struct sk_buff *skb;
  547. ktime_t sent_at;
  548. int ix;
  549. for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  550. skb = call->rxtx_buffer[ix];
  551. if (!skb)
  552. continue;
  553. sp = rxrpc_skb(skb);
  554. if (sp->hdr.serial != orig_serial)
  555. continue;
  556. smp_rmb();
  557. sent_at = skb->tstamp;
  558. goto found;
  559. }
  560. return;
  561. found:
  562. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  563. orig_serial, ack_serial, sent_at, resp_time);
  564. }
  565. /*
  566. * Process the response to a ping that we sent to find out if we lost an ACK.
  567. *
  568. * If we got back a ping response that indicates a lower tx_top than what we
  569. * had at the time of the ping transmission, we adjudge all the DATA packets
  570. * sent between the response tx_top and the ping-time tx_top to have been lost.
  571. */
  572. static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
  573. {
  574. rxrpc_seq_t top, bottom, seq;
  575. bool resend = false;
  576. spin_lock_bh(&call->lock);
  577. bottom = call->tx_hard_ack + 1;
  578. top = call->acks_lost_top;
  579. if (before(bottom, top)) {
  580. for (seq = bottom; before_eq(seq, top); seq++) {
  581. int ix = seq & RXRPC_RXTX_BUFF_MASK;
  582. u8 annotation = call->rxtx_annotations[ix];
  583. u8 anno_type = annotation & RXRPC_TX_ANNO_MASK;
  584. if (anno_type != RXRPC_TX_ANNO_UNACK)
  585. continue;
  586. annotation &= ~RXRPC_TX_ANNO_MASK;
  587. annotation |= RXRPC_TX_ANNO_RETRANS;
  588. call->rxtx_annotations[ix] = annotation;
  589. resend = true;
  590. }
  591. }
  592. spin_unlock_bh(&call->lock);
  593. if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  594. rxrpc_queue_call(call);
  595. }
  596. /*
  597. * Process a ping response.
  598. */
  599. static void rxrpc_input_ping_response(struct rxrpc_call *call,
  600. ktime_t resp_time,
  601. rxrpc_serial_t orig_serial,
  602. rxrpc_serial_t ack_serial)
  603. {
  604. rxrpc_serial_t ping_serial;
  605. ktime_t ping_time;
  606. ping_time = call->ping_time;
  607. smp_rmb();
  608. ping_serial = call->ping_serial;
  609. if (orig_serial == call->acks_lost_ping)
  610. rxrpc_input_check_for_lost_ack(call);
  611. if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
  612. before(orig_serial, ping_serial))
  613. return;
  614. clear_bit(RXRPC_CALL_PINGING, &call->flags);
  615. if (after(orig_serial, ping_serial))
  616. return;
  617. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  618. orig_serial, ack_serial, ping_time, resp_time);
  619. }
  620. /*
  621. * Process the extra information that may be appended to an ACK packet
  622. */
  623. static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  624. struct rxrpc_ackinfo *ackinfo)
  625. {
  626. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  627. struct rxrpc_peer *peer;
  628. unsigned int mtu;
  629. bool wake = false;
  630. u32 rwind = ntohl(ackinfo->rwind);
  631. _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  632. sp->hdr.serial,
  633. ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
  634. rwind, ntohl(ackinfo->jumbo_max));
  635. if (call->tx_winsize != rwind) {
  636. if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  637. rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  638. if (rwind > call->tx_winsize)
  639. wake = true;
  640. trace_rxrpc_rx_rwind_change(call, sp->hdr.serial,
  641. ntohl(ackinfo->rwind), wake);
  642. call->tx_winsize = rwind;
  643. }
  644. if (call->cong_ssthresh > rwind)
  645. call->cong_ssthresh = rwind;
  646. mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  647. peer = call->peer;
  648. if (mtu < peer->maxdata) {
  649. spin_lock_bh(&peer->lock);
  650. peer->maxdata = mtu;
  651. peer->mtu = mtu + peer->hdrsize;
  652. spin_unlock_bh(&peer->lock);
  653. _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  654. }
  655. if (wake)
  656. wake_up(&call->waitq);
  657. }
  658. /*
  659. * Process individual soft ACKs.
  660. *
  661. * Each ACK in the array corresponds to one packet and can be either an ACK or
  662. * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
  663. * packets that lie beyond the end of the ACK list are scheduled for resend by
  664. * the timer on the basis that the peer might just not have processed them at
  665. * the time the ACK was sent.
  666. */
  667. static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
  668. rxrpc_seq_t seq, int nr_acks,
  669. struct rxrpc_ack_summary *summary)
  670. {
  671. int ix;
  672. u8 annotation, anno_type;
  673. for (; nr_acks > 0; nr_acks--, seq++) {
  674. ix = seq & RXRPC_RXTX_BUFF_MASK;
  675. annotation = call->rxtx_annotations[ix];
  676. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  677. annotation &= ~RXRPC_TX_ANNO_MASK;
  678. switch (*acks++) {
  679. case RXRPC_ACK_TYPE_ACK:
  680. summary->nr_acks++;
  681. if (anno_type == RXRPC_TX_ANNO_ACK)
  682. continue;
  683. summary->nr_new_acks++;
  684. call->rxtx_annotations[ix] =
  685. RXRPC_TX_ANNO_ACK | annotation;
  686. break;
  687. case RXRPC_ACK_TYPE_NACK:
  688. if (!summary->nr_nacks &&
  689. call->acks_lowest_nak != seq) {
  690. call->acks_lowest_nak = seq;
  691. summary->new_low_nack = true;
  692. }
  693. summary->nr_nacks++;
  694. if (anno_type == RXRPC_TX_ANNO_NAK)
  695. continue;
  696. summary->nr_new_nacks++;
  697. if (anno_type == RXRPC_TX_ANNO_RETRANS)
  698. continue;
  699. call->rxtx_annotations[ix] =
  700. RXRPC_TX_ANNO_NAK | annotation;
  701. break;
  702. default:
  703. return rxrpc_proto_abort("SFT", call, 0);
  704. }
  705. }
  706. }
  707. /*
  708. * Process an ACK packet.
  709. *
  710. * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
  711. * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
  712. *
  713. * A hard-ACK means that a packet has been processed and may be discarded; a
  714. * soft-ACK means that the packet may be discarded and retransmission
  715. * requested. A phase is complete when all packets are hard-ACK'd.
  716. */
  717. static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
  718. u16 skew)
  719. {
  720. struct rxrpc_ack_summary summary = { 0 };
  721. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  722. union {
  723. struct rxrpc_ackpacket ack;
  724. struct rxrpc_ackinfo info;
  725. u8 acks[RXRPC_MAXACKS];
  726. } buf;
  727. rxrpc_serial_t acked_serial;
  728. rxrpc_seq_t first_soft_ack, hard_ack;
  729. int nr_acks, offset, ioffset;
  730. _enter("");
  731. offset = sizeof(struct rxrpc_wire_header);
  732. if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
  733. _debug("extraction failure");
  734. return rxrpc_proto_abort("XAK", call, 0);
  735. }
  736. offset += sizeof(buf.ack);
  737. acked_serial = ntohl(buf.ack.serial);
  738. first_soft_ack = ntohl(buf.ack.firstPacket);
  739. hard_ack = first_soft_ack - 1;
  740. nr_acks = buf.ack.nAcks;
  741. summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  742. buf.ack.reason : RXRPC_ACK__INVALID);
  743. trace_rxrpc_rx_ack(call, sp->hdr.serial, acked_serial,
  744. first_soft_ack, ntohl(buf.ack.previousPacket),
  745. summary.ack_reason, nr_acks);
  746. if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  747. rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  748. sp->hdr.serial);
  749. if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  750. rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  751. sp->hdr.serial);
  752. if (buf.ack.reason == RXRPC_ACK_PING) {
  753. _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
  754. rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
  755. skew, sp->hdr.serial, true, true,
  756. rxrpc_propose_ack_respond_to_ping);
  757. } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  758. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
  759. skew, sp->hdr.serial, true, true,
  760. rxrpc_propose_ack_respond_to_ack);
  761. }
  762. ioffset = offset + nr_acks + 3;
  763. if (skb->len >= ioffset + sizeof(buf.info)) {
  764. if (skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
  765. return rxrpc_proto_abort("XAI", call, 0);
  766. rxrpc_input_ackinfo(call, skb, &buf.info);
  767. }
  768. if (first_soft_ack == 0)
  769. return rxrpc_proto_abort("AK0", call, 0);
  770. /* Ignore ACKs unless we are or have just been transmitting. */
  771. switch (READ_ONCE(call->state)) {
  772. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  773. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  774. case RXRPC_CALL_SERVER_SEND_REPLY:
  775. case RXRPC_CALL_SERVER_AWAIT_ACK:
  776. break;
  777. default:
  778. return;
  779. }
  780. /* Discard any out-of-order or duplicate ACKs. */
  781. if (before_eq(sp->hdr.serial, call->acks_latest)) {
  782. _debug("discard ACK %d <= %d",
  783. sp->hdr.serial, call->acks_latest);
  784. return;
  785. }
  786. call->acks_latest_ts = skb->tstamp;
  787. call->acks_latest = sp->hdr.serial;
  788. if (before(hard_ack, call->tx_hard_ack) ||
  789. after(hard_ack, call->tx_top))
  790. return rxrpc_proto_abort("AKW", call, 0);
  791. if (nr_acks > call->tx_top - hard_ack)
  792. return rxrpc_proto_abort("AKN", call, 0);
  793. if (after(hard_ack, call->tx_hard_ack))
  794. rxrpc_rotate_tx_window(call, hard_ack, &summary);
  795. if (nr_acks > 0) {
  796. if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0)
  797. return rxrpc_proto_abort("XSA", call, 0);
  798. rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
  799. &summary);
  800. }
  801. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  802. rxrpc_end_tx_phase(call, false, "ETA");
  803. return;
  804. }
  805. if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  806. RXRPC_TX_ANNO_LAST &&
  807. summary.nr_acks == call->tx_top - hard_ack &&
  808. rxrpc_is_client_call(call))
  809. rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  810. false, true,
  811. rxrpc_propose_ack_ping_for_lost_reply);
  812. return rxrpc_congestion_management(call, skb, &summary, acked_serial);
  813. }
  814. /*
  815. * Process an ACKALL packet.
  816. */
  817. static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
  818. {
  819. struct rxrpc_ack_summary summary = { 0 };
  820. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  821. _proto("Rx ACKALL %%%u", sp->hdr.serial);
  822. rxrpc_rotate_tx_window(call, call->tx_top, &summary);
  823. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  824. rxrpc_end_tx_phase(call, false, "ETL");
  825. }
  826. /*
  827. * Process an ABORT packet directed at a call.
  828. */
  829. static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  830. {
  831. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  832. __be32 wtmp;
  833. u32 abort_code = RX_CALL_DEAD;
  834. _enter("");
  835. if (skb->len >= 4 &&
  836. skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  837. &wtmp, sizeof(wtmp)) >= 0)
  838. abort_code = ntohl(wtmp);
  839. trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
  840. _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
  841. if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  842. abort_code, -ECONNABORTED))
  843. rxrpc_notify_socket(call);
  844. }
  845. /*
  846. * Process an incoming call packet.
  847. */
  848. static void rxrpc_input_call_packet(struct rxrpc_call *call,
  849. struct sk_buff *skb, u16 skew)
  850. {
  851. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  852. unsigned long timo;
  853. _enter("%p,%p", call, skb);
  854. timo = READ_ONCE(call->next_rx_timo);
  855. if (timo) {
  856. unsigned long now = jiffies, expect_rx_by;
  857. expect_rx_by = now + timo;
  858. WRITE_ONCE(call->expect_rx_by, expect_rx_by);
  859. rxrpc_reduce_call_timer(call, expect_rx_by, now,
  860. rxrpc_timer_set_for_normal);
  861. }
  862. switch (sp->hdr.type) {
  863. case RXRPC_PACKET_TYPE_DATA:
  864. rxrpc_input_data(call, skb, skew);
  865. break;
  866. case RXRPC_PACKET_TYPE_ACK:
  867. rxrpc_input_ack(call, skb, skew);
  868. break;
  869. case RXRPC_PACKET_TYPE_BUSY:
  870. _proto("Rx BUSY %%%u", sp->hdr.serial);
  871. /* Just ignore BUSY packets from the server; the retry and
  872. * lifespan timers will take care of business. BUSY packets
  873. * from the client don't make sense.
  874. */
  875. break;
  876. case RXRPC_PACKET_TYPE_ABORT:
  877. rxrpc_input_abort(call, skb);
  878. break;
  879. case RXRPC_PACKET_TYPE_ACKALL:
  880. rxrpc_input_ackall(call, skb);
  881. break;
  882. default:
  883. break;
  884. }
  885. _leave("");
  886. }
  887. /*
  888. * Handle a new call on a channel implicitly completing the preceding call on
  889. * that channel.
  890. *
  891. * TODO: If callNumber > call_id + 1, renegotiate security.
  892. */
  893. static void rxrpc_input_implicit_end_call(struct rxrpc_connection *conn,
  894. struct rxrpc_call *call)
  895. {
  896. switch (READ_ONCE(call->state)) {
  897. case RXRPC_CALL_SERVER_AWAIT_ACK:
  898. rxrpc_call_completed(call);
  899. break;
  900. case RXRPC_CALL_COMPLETE:
  901. break;
  902. default:
  903. if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
  904. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  905. rxrpc_queue_call(call);
  906. }
  907. break;
  908. }
  909. trace_rxrpc_improper_term(call);
  910. __rxrpc_disconnect_call(conn, call);
  911. rxrpc_notify_socket(call);
  912. }
  913. /*
  914. * post connection-level events to the connection
  915. * - this includes challenges, responses, some aborts and call terminal packet
  916. * retransmission.
  917. */
  918. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  919. struct sk_buff *skb)
  920. {
  921. _enter("%p,%p", conn, skb);
  922. skb_queue_tail(&conn->rx_queue, skb);
  923. rxrpc_queue_conn(conn);
  924. }
  925. /*
  926. * post endpoint-level events to the local endpoint
  927. * - this includes debug and version messages
  928. */
  929. static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  930. struct sk_buff *skb)
  931. {
  932. _enter("%p,%p", local, skb);
  933. skb_queue_tail(&local->event_queue, skb);
  934. rxrpc_queue_local(local);
  935. }
  936. /*
  937. * put a packet up for transport-level abort
  938. */
  939. static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  940. {
  941. CHECK_SLAB_OKAY(&local->usage);
  942. skb_queue_tail(&local->reject_queue, skb);
  943. rxrpc_queue_local(local);
  944. }
  945. /*
  946. * Extract the wire header from a packet and translate the byte order.
  947. */
  948. static noinline
  949. int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  950. {
  951. struct rxrpc_wire_header whdr;
  952. /* dig out the RxRPC connection details */
  953. if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
  954. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
  955. tracepoint_string("bad_hdr"));
  956. return -EBADMSG;
  957. }
  958. memset(sp, 0, sizeof(*sp));
  959. sp->hdr.epoch = ntohl(whdr.epoch);
  960. sp->hdr.cid = ntohl(whdr.cid);
  961. sp->hdr.callNumber = ntohl(whdr.callNumber);
  962. sp->hdr.seq = ntohl(whdr.seq);
  963. sp->hdr.serial = ntohl(whdr.serial);
  964. sp->hdr.flags = whdr.flags;
  965. sp->hdr.type = whdr.type;
  966. sp->hdr.userStatus = whdr.userStatus;
  967. sp->hdr.securityIndex = whdr.securityIndex;
  968. sp->hdr._rsvd = ntohs(whdr._rsvd);
  969. sp->hdr.serviceId = ntohs(whdr.serviceId);
  970. return 0;
  971. }
  972. /*
  973. * handle data received on the local endpoint
  974. * - may be called in interrupt context
  975. *
  976. * The socket is locked by the caller and this prevents the socket from being
  977. * shut down and the local endpoint from going away, thus sk_user_data will not
  978. * be cleared until this function returns.
  979. */
  980. void rxrpc_data_ready(struct sock *udp_sk)
  981. {
  982. struct rxrpc_connection *conn;
  983. struct rxrpc_channel *chan;
  984. struct rxrpc_call *call;
  985. struct rxrpc_skb_priv *sp;
  986. struct rxrpc_local *local = udp_sk->sk_user_data;
  987. struct sk_buff *skb;
  988. unsigned int channel;
  989. int ret, skew;
  990. _enter("%p", udp_sk);
  991. ASSERT(!irqs_disabled());
  992. skb = skb_recv_udp(udp_sk, 0, 1, &ret);
  993. if (!skb) {
  994. if (ret == -EAGAIN)
  995. return;
  996. _debug("UDP socket error %d", ret);
  997. return;
  998. }
  999. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  1000. _net("recv skb %p", skb);
  1001. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  1002. if (skb_checksum_complete(skb)) {
  1003. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  1004. __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
  1005. _leave(" [CSUM failed]");
  1006. return;
  1007. }
  1008. __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
  1009. /* The UDP protocol already released all skb resources;
  1010. * we are free to add our own data there.
  1011. */
  1012. sp = rxrpc_skb(skb);
  1013. /* dig out the RxRPC connection details */
  1014. if (rxrpc_extract_header(sp, skb) < 0)
  1015. goto bad_message;
  1016. if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  1017. static int lose;
  1018. if ((lose++ & 7) == 7) {
  1019. trace_rxrpc_rx_lose(sp);
  1020. rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
  1021. return;
  1022. }
  1023. }
  1024. trace_rxrpc_rx_packet(sp);
  1025. _net("Rx RxRPC %s ep=%x call=%x:%x",
  1026. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  1027. sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
  1028. if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
  1029. !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
  1030. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  1031. goto bad_message;
  1032. }
  1033. switch (sp->hdr.type) {
  1034. case RXRPC_PACKET_TYPE_VERSION:
  1035. if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED))
  1036. goto discard;
  1037. rxrpc_post_packet_to_local(local, skb);
  1038. goto out;
  1039. case RXRPC_PACKET_TYPE_BUSY:
  1040. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  1041. goto discard;
  1042. /* Fall through */
  1043. case RXRPC_PACKET_TYPE_DATA:
  1044. if (sp->hdr.callNumber == 0)
  1045. goto bad_message;
  1046. if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
  1047. !rxrpc_validate_jumbo(skb))
  1048. goto bad_message;
  1049. break;
  1050. /* Packet types 9-11 should just be ignored. */
  1051. case RXRPC_PACKET_TYPE_PARAMS:
  1052. case RXRPC_PACKET_TYPE_10:
  1053. case RXRPC_PACKET_TYPE_11:
  1054. goto discard;
  1055. }
  1056. rcu_read_lock();
  1057. conn = rxrpc_find_connection_rcu(local, skb);
  1058. if (conn) {
  1059. if (sp->hdr.securityIndex != conn->security_ix)
  1060. goto wrong_security;
  1061. if (sp->hdr.serviceId != conn->service_id) {
  1062. if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) ||
  1063. conn->service_id != conn->params.service_id)
  1064. goto reupgrade;
  1065. conn->service_id = sp->hdr.serviceId;
  1066. }
  1067. if (sp->hdr.callNumber == 0) {
  1068. /* Connection-level packet */
  1069. _debug("CONN %p {%d}", conn, conn->debug_id);
  1070. rxrpc_post_packet_to_conn(conn, skb);
  1071. goto out_unlock;
  1072. }
  1073. /* Note the serial number skew here */
  1074. skew = (int)sp->hdr.serial - (int)conn->hi_serial;
  1075. if (skew >= 0) {
  1076. if (skew > 0)
  1077. conn->hi_serial = sp->hdr.serial;
  1078. } else {
  1079. skew = -skew;
  1080. skew = min(skew, 65535);
  1081. }
  1082. /* Call-bound packets are routed by connection channel. */
  1083. channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  1084. chan = &conn->channels[channel];
  1085. /* Ignore really old calls */
  1086. if (sp->hdr.callNumber < chan->last_call)
  1087. goto discard_unlock;
  1088. if (sp->hdr.callNumber == chan->last_call) {
  1089. if (chan->call ||
  1090. sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
  1091. goto discard_unlock;
  1092. /* For the previous service call, if completed
  1093. * successfully, we discard all further packets.
  1094. */
  1095. if (rxrpc_conn_is_service(conn) &&
  1096. chan->last_type == RXRPC_PACKET_TYPE_ACK)
  1097. goto discard_unlock;
  1098. /* But otherwise we need to retransmit the final packet
  1099. * from data cached in the connection record.
  1100. */
  1101. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
  1102. trace_rxrpc_rx_data(chan->call_debug_id,
  1103. sp->hdr.seq,
  1104. sp->hdr.serial,
  1105. sp->hdr.flags, 0);
  1106. rxrpc_post_packet_to_conn(conn, skb);
  1107. goto out_unlock;
  1108. }
  1109. call = rcu_dereference(chan->call);
  1110. if (sp->hdr.callNumber > chan->call_id) {
  1111. if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED)) {
  1112. rcu_read_unlock();
  1113. goto reject_packet;
  1114. }
  1115. if (call)
  1116. rxrpc_input_implicit_end_call(conn, call);
  1117. call = NULL;
  1118. }
  1119. if (call) {
  1120. if (sp->hdr.serviceId != call->service_id)
  1121. call->service_id = sp->hdr.serviceId;
  1122. if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
  1123. call->rx_serial = sp->hdr.serial;
  1124. if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
  1125. set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
  1126. }
  1127. } else {
  1128. skew = 0;
  1129. call = NULL;
  1130. }
  1131. if (!call || atomic_read(&call->usage) == 0) {
  1132. if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
  1133. sp->hdr.callNumber == 0 ||
  1134. sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  1135. goto bad_message_unlock;
  1136. if (sp->hdr.seq != 1)
  1137. goto discard_unlock;
  1138. call = rxrpc_new_incoming_call(local, conn, skb);
  1139. if (!call) {
  1140. rcu_read_unlock();
  1141. goto reject_packet;
  1142. }
  1143. rxrpc_send_ping(call, skb, skew);
  1144. mutex_unlock(&call->user_mutex);
  1145. }
  1146. rxrpc_input_call_packet(call, skb, skew);
  1147. goto discard_unlock;
  1148. discard_unlock:
  1149. rcu_read_unlock();
  1150. discard:
  1151. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  1152. out:
  1153. trace_rxrpc_rx_done(0, 0);
  1154. return;
  1155. out_unlock:
  1156. rcu_read_unlock();
  1157. goto out;
  1158. wrong_security:
  1159. rcu_read_unlock();
  1160. trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1161. RXKADINCONSISTENCY, EBADMSG);
  1162. skb->priority = RXKADINCONSISTENCY;
  1163. goto post_abort;
  1164. reupgrade:
  1165. rcu_read_unlock();
  1166. trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1167. RX_PROTOCOL_ERROR, EBADMSG);
  1168. goto protocol_error;
  1169. bad_message_unlock:
  1170. rcu_read_unlock();
  1171. bad_message:
  1172. trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1173. RX_PROTOCOL_ERROR, EBADMSG);
  1174. protocol_error:
  1175. skb->priority = RX_PROTOCOL_ERROR;
  1176. post_abort:
  1177. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  1178. reject_packet:
  1179. trace_rxrpc_rx_done(skb->mark, skb->priority);
  1180. rxrpc_reject_packet(local, skb);
  1181. _leave(" [badmsg]");
  1182. }