input.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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, 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. if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
  528. rxrpc_notify_socket(call);
  529. _leave(" [queued]");
  530. }
  531. /*
  532. * Process a requested ACK.
  533. */
  534. static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  535. ktime_t resp_time,
  536. rxrpc_serial_t orig_serial,
  537. rxrpc_serial_t ack_serial)
  538. {
  539. struct rxrpc_skb_priv *sp;
  540. struct sk_buff *skb;
  541. ktime_t sent_at;
  542. int ix;
  543. for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  544. skb = call->rxtx_buffer[ix];
  545. if (!skb)
  546. continue;
  547. sp = rxrpc_skb(skb);
  548. if (sp->hdr.serial != orig_serial)
  549. continue;
  550. smp_rmb();
  551. sent_at = skb->tstamp;
  552. goto found;
  553. }
  554. return;
  555. found:
  556. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  557. orig_serial, ack_serial, sent_at, resp_time);
  558. }
  559. /*
  560. * Process the response to a ping that we sent to find out if we lost an ACK.
  561. *
  562. * If we got back a ping response that indicates a lower tx_top than what we
  563. * had at the time of the ping transmission, we adjudge all the DATA packets
  564. * sent between the response tx_top and the ping-time tx_top to have been lost.
  565. */
  566. static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
  567. {
  568. rxrpc_seq_t top, bottom, seq;
  569. bool resend = false;
  570. spin_lock_bh(&call->lock);
  571. bottom = call->tx_hard_ack + 1;
  572. top = call->acks_lost_top;
  573. if (before(bottom, top)) {
  574. for (seq = bottom; before_eq(seq, top); seq++) {
  575. int ix = seq & RXRPC_RXTX_BUFF_MASK;
  576. u8 annotation = call->rxtx_annotations[ix];
  577. u8 anno_type = annotation & RXRPC_TX_ANNO_MASK;
  578. if (anno_type != RXRPC_TX_ANNO_UNACK)
  579. continue;
  580. annotation &= ~RXRPC_TX_ANNO_MASK;
  581. annotation |= RXRPC_TX_ANNO_RETRANS;
  582. call->rxtx_annotations[ix] = annotation;
  583. resend = true;
  584. }
  585. }
  586. spin_unlock_bh(&call->lock);
  587. if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  588. rxrpc_queue_call(call);
  589. }
  590. /*
  591. * Process a ping response.
  592. */
  593. static void rxrpc_input_ping_response(struct rxrpc_call *call,
  594. ktime_t resp_time,
  595. rxrpc_serial_t orig_serial,
  596. rxrpc_serial_t ack_serial)
  597. {
  598. rxrpc_serial_t ping_serial;
  599. ktime_t ping_time;
  600. ping_time = call->ping_time;
  601. smp_rmb();
  602. ping_serial = call->ping_serial;
  603. if (orig_serial == call->acks_lost_ping)
  604. rxrpc_input_check_for_lost_ack(call);
  605. if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
  606. before(orig_serial, ping_serial))
  607. return;
  608. clear_bit(RXRPC_CALL_PINGING, &call->flags);
  609. if (after(orig_serial, ping_serial))
  610. return;
  611. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  612. orig_serial, ack_serial, ping_time, resp_time);
  613. }
  614. /*
  615. * Process the extra information that may be appended to an ACK packet
  616. */
  617. static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  618. struct rxrpc_ackinfo *ackinfo)
  619. {
  620. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  621. struct rxrpc_peer *peer;
  622. unsigned int mtu;
  623. bool wake = false;
  624. u32 rwind = ntohl(ackinfo->rwind);
  625. _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  626. sp->hdr.serial,
  627. ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
  628. rwind, ntohl(ackinfo->jumbo_max));
  629. if (call->tx_winsize != rwind) {
  630. if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  631. rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  632. if (rwind > call->tx_winsize)
  633. wake = true;
  634. trace_rxrpc_rx_rwind_change(call, sp->hdr.serial,
  635. ntohl(ackinfo->rwind), wake);
  636. call->tx_winsize = rwind;
  637. }
  638. if (call->cong_ssthresh > rwind)
  639. call->cong_ssthresh = rwind;
  640. mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  641. peer = call->peer;
  642. if (mtu < peer->maxdata) {
  643. spin_lock_bh(&peer->lock);
  644. peer->maxdata = mtu;
  645. peer->mtu = mtu + peer->hdrsize;
  646. spin_unlock_bh(&peer->lock);
  647. _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  648. }
  649. if (wake)
  650. wake_up(&call->waitq);
  651. }
  652. /*
  653. * Process individual soft ACKs.
  654. *
  655. * Each ACK in the array corresponds to one packet and can be either an ACK or
  656. * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
  657. * packets that lie beyond the end of the ACK list are scheduled for resend by
  658. * the timer on the basis that the peer might just not have processed them at
  659. * the time the ACK was sent.
  660. */
  661. static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
  662. rxrpc_seq_t seq, int nr_acks,
  663. struct rxrpc_ack_summary *summary)
  664. {
  665. int ix;
  666. u8 annotation, anno_type;
  667. for (; nr_acks > 0; nr_acks--, seq++) {
  668. ix = seq & RXRPC_RXTX_BUFF_MASK;
  669. annotation = call->rxtx_annotations[ix];
  670. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  671. annotation &= ~RXRPC_TX_ANNO_MASK;
  672. switch (*acks++) {
  673. case RXRPC_ACK_TYPE_ACK:
  674. summary->nr_acks++;
  675. if (anno_type == RXRPC_TX_ANNO_ACK)
  676. continue;
  677. summary->nr_new_acks++;
  678. call->rxtx_annotations[ix] =
  679. RXRPC_TX_ANNO_ACK | annotation;
  680. break;
  681. case RXRPC_ACK_TYPE_NACK:
  682. if (!summary->nr_nacks &&
  683. call->acks_lowest_nak != seq) {
  684. call->acks_lowest_nak = seq;
  685. summary->new_low_nack = true;
  686. }
  687. summary->nr_nacks++;
  688. if (anno_type == RXRPC_TX_ANNO_NAK)
  689. continue;
  690. summary->nr_new_nacks++;
  691. if (anno_type == RXRPC_TX_ANNO_RETRANS)
  692. continue;
  693. call->rxtx_annotations[ix] =
  694. RXRPC_TX_ANNO_NAK | annotation;
  695. break;
  696. default:
  697. return rxrpc_proto_abort("SFT", call, 0);
  698. }
  699. }
  700. }
  701. /*
  702. * Process an ACK packet.
  703. *
  704. * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
  705. * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
  706. *
  707. * A hard-ACK means that a packet has been processed and may be discarded; a
  708. * soft-ACK means that the packet may be discarded and retransmission
  709. * requested. A phase is complete when all packets are hard-ACK'd.
  710. */
  711. static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
  712. u16 skew)
  713. {
  714. struct rxrpc_ack_summary summary = { 0 };
  715. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  716. union {
  717. struct rxrpc_ackpacket ack;
  718. struct rxrpc_ackinfo info;
  719. u8 acks[RXRPC_MAXACKS];
  720. } buf;
  721. rxrpc_serial_t acked_serial;
  722. rxrpc_seq_t first_soft_ack, hard_ack;
  723. int nr_acks, offset, ioffset;
  724. _enter("");
  725. offset = sizeof(struct rxrpc_wire_header);
  726. if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
  727. _debug("extraction failure");
  728. return rxrpc_proto_abort("XAK", call, 0);
  729. }
  730. offset += sizeof(buf.ack);
  731. acked_serial = ntohl(buf.ack.serial);
  732. first_soft_ack = ntohl(buf.ack.firstPacket);
  733. hard_ack = first_soft_ack - 1;
  734. nr_acks = buf.ack.nAcks;
  735. summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  736. buf.ack.reason : RXRPC_ACK__INVALID);
  737. trace_rxrpc_rx_ack(call, sp->hdr.serial, acked_serial,
  738. first_soft_ack, ntohl(buf.ack.previousPacket),
  739. summary.ack_reason, nr_acks);
  740. if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  741. rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  742. sp->hdr.serial);
  743. if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  744. rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  745. sp->hdr.serial);
  746. if (buf.ack.reason == RXRPC_ACK_PING) {
  747. _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
  748. rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
  749. skew, sp->hdr.serial, true, true,
  750. rxrpc_propose_ack_respond_to_ping);
  751. } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  752. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
  753. skew, sp->hdr.serial, true, true,
  754. rxrpc_propose_ack_respond_to_ack);
  755. }
  756. ioffset = offset + nr_acks + 3;
  757. if (skb->len >= ioffset + sizeof(buf.info)) {
  758. if (skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
  759. return rxrpc_proto_abort("XAI", call, 0);
  760. rxrpc_input_ackinfo(call, skb, &buf.info);
  761. }
  762. if (first_soft_ack == 0)
  763. return rxrpc_proto_abort("AK0", call, 0);
  764. /* Ignore ACKs unless we are or have just been transmitting. */
  765. switch (READ_ONCE(call->state)) {
  766. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  767. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  768. case RXRPC_CALL_SERVER_SEND_REPLY:
  769. case RXRPC_CALL_SERVER_AWAIT_ACK:
  770. break;
  771. default:
  772. return;
  773. }
  774. /* Discard any out-of-order or duplicate ACKs. */
  775. if (before_eq(sp->hdr.serial, call->acks_latest)) {
  776. _debug("discard ACK %d <= %d",
  777. sp->hdr.serial, call->acks_latest);
  778. return;
  779. }
  780. call->acks_latest_ts = skb->tstamp;
  781. call->acks_latest = sp->hdr.serial;
  782. if (before(hard_ack, call->tx_hard_ack) ||
  783. after(hard_ack, call->tx_top))
  784. return rxrpc_proto_abort("AKW", call, 0);
  785. if (nr_acks > call->tx_top - hard_ack)
  786. return rxrpc_proto_abort("AKN", call, 0);
  787. if (after(hard_ack, call->tx_hard_ack))
  788. rxrpc_rotate_tx_window(call, hard_ack, &summary);
  789. if (nr_acks > 0) {
  790. if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0)
  791. return rxrpc_proto_abort("XSA", call, 0);
  792. rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
  793. &summary);
  794. }
  795. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  796. rxrpc_end_tx_phase(call, false, "ETA");
  797. return;
  798. }
  799. if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  800. RXRPC_TX_ANNO_LAST &&
  801. summary.nr_acks == call->tx_top - hard_ack &&
  802. rxrpc_is_client_call(call))
  803. rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  804. false, true,
  805. rxrpc_propose_ack_ping_for_lost_reply);
  806. return rxrpc_congestion_management(call, skb, &summary, acked_serial);
  807. }
  808. /*
  809. * Process an ACKALL packet.
  810. */
  811. static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
  812. {
  813. struct rxrpc_ack_summary summary = { 0 };
  814. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  815. _proto("Rx ACKALL %%%u", sp->hdr.serial);
  816. rxrpc_rotate_tx_window(call, call->tx_top, &summary);
  817. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  818. rxrpc_end_tx_phase(call, false, "ETL");
  819. }
  820. /*
  821. * Process an ABORT packet directed at a call.
  822. */
  823. static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  824. {
  825. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  826. __be32 wtmp;
  827. u32 abort_code = RX_CALL_DEAD;
  828. _enter("");
  829. if (skb->len >= 4 &&
  830. skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  831. &wtmp, sizeof(wtmp)) >= 0)
  832. abort_code = ntohl(wtmp);
  833. trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
  834. _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
  835. if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  836. abort_code, -ECONNABORTED))
  837. rxrpc_notify_socket(call);
  838. }
  839. /*
  840. * Process an incoming call packet.
  841. */
  842. static void rxrpc_input_call_packet(struct rxrpc_call *call,
  843. struct sk_buff *skb, u16 skew)
  844. {
  845. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  846. unsigned long timo;
  847. _enter("%p,%p", call, skb);
  848. timo = READ_ONCE(call->next_rx_timo);
  849. if (timo) {
  850. unsigned long now = jiffies, expect_rx_by;
  851. expect_rx_by = now + timo;
  852. WRITE_ONCE(call->expect_rx_by, expect_rx_by);
  853. rxrpc_reduce_call_timer(call, expect_rx_by, now,
  854. rxrpc_timer_set_for_normal);
  855. }
  856. switch (sp->hdr.type) {
  857. case RXRPC_PACKET_TYPE_DATA:
  858. rxrpc_input_data(call, skb, skew);
  859. break;
  860. case RXRPC_PACKET_TYPE_ACK:
  861. rxrpc_input_ack(call, skb, skew);
  862. break;
  863. case RXRPC_PACKET_TYPE_BUSY:
  864. _proto("Rx BUSY %%%u", sp->hdr.serial);
  865. /* Just ignore BUSY packets from the server; the retry and
  866. * lifespan timers will take care of business. BUSY packets
  867. * from the client don't make sense.
  868. */
  869. break;
  870. case RXRPC_PACKET_TYPE_ABORT:
  871. rxrpc_input_abort(call, skb);
  872. break;
  873. case RXRPC_PACKET_TYPE_ACKALL:
  874. rxrpc_input_ackall(call, skb);
  875. break;
  876. default:
  877. break;
  878. }
  879. _leave("");
  880. }
  881. /*
  882. * Handle a new call on a channel implicitly completing the preceding call on
  883. * that channel.
  884. *
  885. * TODO: If callNumber > call_id + 1, renegotiate security.
  886. */
  887. static void rxrpc_input_implicit_end_call(struct rxrpc_connection *conn,
  888. struct rxrpc_call *call)
  889. {
  890. switch (READ_ONCE(call->state)) {
  891. case RXRPC_CALL_SERVER_AWAIT_ACK:
  892. rxrpc_call_completed(call);
  893. break;
  894. case RXRPC_CALL_COMPLETE:
  895. break;
  896. default:
  897. if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
  898. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  899. rxrpc_queue_call(call);
  900. }
  901. break;
  902. }
  903. trace_rxrpc_improper_term(call);
  904. __rxrpc_disconnect_call(conn, call);
  905. rxrpc_notify_socket(call);
  906. }
  907. /*
  908. * post connection-level events to the connection
  909. * - this includes challenges, responses, some aborts and call terminal packet
  910. * retransmission.
  911. */
  912. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  913. struct sk_buff *skb)
  914. {
  915. _enter("%p,%p", conn, skb);
  916. skb_queue_tail(&conn->rx_queue, skb);
  917. rxrpc_queue_conn(conn);
  918. }
  919. /*
  920. * post endpoint-level events to the local endpoint
  921. * - this includes debug and version messages
  922. */
  923. static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  924. struct sk_buff *skb)
  925. {
  926. _enter("%p,%p", local, skb);
  927. skb_queue_tail(&local->event_queue, skb);
  928. rxrpc_queue_local(local);
  929. }
  930. /*
  931. * put a packet up for transport-level abort
  932. */
  933. static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  934. {
  935. CHECK_SLAB_OKAY(&local->usage);
  936. skb_queue_tail(&local->reject_queue, skb);
  937. rxrpc_queue_local(local);
  938. }
  939. /*
  940. * Extract the wire header from a packet and translate the byte order.
  941. */
  942. static noinline
  943. int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  944. {
  945. struct rxrpc_wire_header whdr;
  946. /* dig out the RxRPC connection details */
  947. if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
  948. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
  949. tracepoint_string("bad_hdr"));
  950. return -EBADMSG;
  951. }
  952. memset(sp, 0, sizeof(*sp));
  953. sp->hdr.epoch = ntohl(whdr.epoch);
  954. sp->hdr.cid = ntohl(whdr.cid);
  955. sp->hdr.callNumber = ntohl(whdr.callNumber);
  956. sp->hdr.seq = ntohl(whdr.seq);
  957. sp->hdr.serial = ntohl(whdr.serial);
  958. sp->hdr.flags = whdr.flags;
  959. sp->hdr.type = whdr.type;
  960. sp->hdr.userStatus = whdr.userStatus;
  961. sp->hdr.securityIndex = whdr.securityIndex;
  962. sp->hdr._rsvd = ntohs(whdr._rsvd);
  963. sp->hdr.serviceId = ntohs(whdr.serviceId);
  964. return 0;
  965. }
  966. /*
  967. * handle data received on the local endpoint
  968. * - may be called in interrupt context
  969. *
  970. * The socket is locked by the caller and this prevents the socket from being
  971. * shut down and the local endpoint from going away, thus sk_user_data will not
  972. * be cleared until this function returns.
  973. */
  974. void rxrpc_data_ready(struct sock *udp_sk)
  975. {
  976. struct rxrpc_connection *conn;
  977. struct rxrpc_channel *chan;
  978. struct rxrpc_call *call;
  979. struct rxrpc_skb_priv *sp;
  980. struct rxrpc_local *local = udp_sk->sk_user_data;
  981. struct sk_buff *skb;
  982. unsigned int channel;
  983. int ret, skew;
  984. _enter("%p", udp_sk);
  985. ASSERT(!irqs_disabled());
  986. skb = skb_recv_udp(udp_sk, 0, 1, &ret);
  987. if (!skb) {
  988. if (ret == -EAGAIN)
  989. return;
  990. _debug("UDP socket error %d", ret);
  991. return;
  992. }
  993. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  994. _net("recv skb %p", skb);
  995. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  996. if (skb_checksum_complete(skb)) {
  997. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  998. __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
  999. _leave(" [CSUM failed]");
  1000. return;
  1001. }
  1002. __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
  1003. /* The UDP protocol already released all skb resources;
  1004. * we are free to add our own data there.
  1005. */
  1006. sp = rxrpc_skb(skb);
  1007. /* dig out the RxRPC connection details */
  1008. if (rxrpc_extract_header(sp, skb) < 0)
  1009. goto bad_message;
  1010. if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  1011. static int lose;
  1012. if ((lose++ & 7) == 7) {
  1013. trace_rxrpc_rx_lose(sp);
  1014. rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
  1015. return;
  1016. }
  1017. }
  1018. trace_rxrpc_rx_packet(sp);
  1019. _net("Rx RxRPC %s ep=%x call=%x:%x",
  1020. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  1021. sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
  1022. if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
  1023. !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
  1024. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  1025. goto bad_message;
  1026. }
  1027. switch (sp->hdr.type) {
  1028. case RXRPC_PACKET_TYPE_VERSION:
  1029. if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED))
  1030. goto discard;
  1031. rxrpc_post_packet_to_local(local, skb);
  1032. goto out;
  1033. case RXRPC_PACKET_TYPE_BUSY:
  1034. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  1035. goto discard;
  1036. /* Fall through */
  1037. case RXRPC_PACKET_TYPE_DATA:
  1038. if (sp->hdr.callNumber == 0)
  1039. goto bad_message;
  1040. if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
  1041. !rxrpc_validate_jumbo(skb))
  1042. goto bad_message;
  1043. break;
  1044. /* Packet types 9-11 should just be ignored. */
  1045. case RXRPC_PACKET_TYPE_PARAMS:
  1046. case RXRPC_PACKET_TYPE_10:
  1047. case RXRPC_PACKET_TYPE_11:
  1048. goto discard;
  1049. }
  1050. rcu_read_lock();
  1051. conn = rxrpc_find_connection_rcu(local, skb);
  1052. if (conn) {
  1053. if (sp->hdr.securityIndex != conn->security_ix)
  1054. goto wrong_security;
  1055. if (sp->hdr.serviceId != conn->service_id) {
  1056. if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) ||
  1057. conn->service_id != conn->params.service_id)
  1058. goto reupgrade;
  1059. conn->service_id = sp->hdr.serviceId;
  1060. }
  1061. if (sp->hdr.callNumber == 0) {
  1062. /* Connection-level packet */
  1063. _debug("CONN %p {%d}", conn, conn->debug_id);
  1064. rxrpc_post_packet_to_conn(conn, skb);
  1065. goto out_unlock;
  1066. }
  1067. /* Note the serial number skew here */
  1068. skew = (int)sp->hdr.serial - (int)conn->hi_serial;
  1069. if (skew >= 0) {
  1070. if (skew > 0)
  1071. conn->hi_serial = sp->hdr.serial;
  1072. } else {
  1073. skew = -skew;
  1074. skew = min(skew, 65535);
  1075. }
  1076. /* Call-bound packets are routed by connection channel. */
  1077. channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  1078. chan = &conn->channels[channel];
  1079. /* Ignore really old calls */
  1080. if (sp->hdr.callNumber < chan->last_call)
  1081. goto discard_unlock;
  1082. if (sp->hdr.callNumber == chan->last_call) {
  1083. if (chan->call ||
  1084. sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
  1085. goto discard_unlock;
  1086. /* For the previous service call, if completed
  1087. * successfully, we discard all further packets.
  1088. */
  1089. if (rxrpc_conn_is_service(conn) &&
  1090. chan->last_type == RXRPC_PACKET_TYPE_ACK)
  1091. goto discard_unlock;
  1092. /* But otherwise we need to retransmit the final packet
  1093. * from data cached in the connection record.
  1094. */
  1095. rxrpc_post_packet_to_conn(conn, skb);
  1096. goto out_unlock;
  1097. }
  1098. call = rcu_dereference(chan->call);
  1099. if (sp->hdr.callNumber > chan->call_id) {
  1100. if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED)) {
  1101. rcu_read_unlock();
  1102. goto reject_packet;
  1103. }
  1104. if (call)
  1105. rxrpc_input_implicit_end_call(conn, call);
  1106. call = NULL;
  1107. }
  1108. if (call) {
  1109. if (sp->hdr.serviceId != call->service_id)
  1110. call->service_id = sp->hdr.serviceId;
  1111. if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
  1112. call->rx_serial = sp->hdr.serial;
  1113. if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
  1114. set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
  1115. }
  1116. } else {
  1117. skew = 0;
  1118. call = NULL;
  1119. }
  1120. if (!call || atomic_read(&call->usage) == 0) {
  1121. if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
  1122. sp->hdr.callNumber == 0 ||
  1123. sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  1124. goto bad_message_unlock;
  1125. if (sp->hdr.seq != 1)
  1126. goto discard_unlock;
  1127. call = rxrpc_new_incoming_call(local, conn, skb);
  1128. if (!call) {
  1129. rcu_read_unlock();
  1130. goto reject_packet;
  1131. }
  1132. rxrpc_send_ping(call, skb, skew);
  1133. mutex_unlock(&call->user_mutex);
  1134. }
  1135. rxrpc_input_call_packet(call, skb, skew);
  1136. goto discard_unlock;
  1137. discard_unlock:
  1138. rcu_read_unlock();
  1139. discard:
  1140. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  1141. out:
  1142. trace_rxrpc_rx_done(0, 0);
  1143. return;
  1144. out_unlock:
  1145. rcu_read_unlock();
  1146. goto out;
  1147. wrong_security:
  1148. rcu_read_unlock();
  1149. trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1150. RXKADINCONSISTENCY, EBADMSG);
  1151. skb->priority = RXKADINCONSISTENCY;
  1152. goto post_abort;
  1153. reupgrade:
  1154. rcu_read_unlock();
  1155. trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1156. RX_PROTOCOL_ERROR, EBADMSG);
  1157. goto protocol_error;
  1158. bad_message_unlock:
  1159. rcu_read_unlock();
  1160. bad_message:
  1161. trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1162. RX_PROTOCOL_ERROR, EBADMSG);
  1163. protocol_error:
  1164. skb->priority = RX_PROTOCOL_ERROR;
  1165. post_abort:
  1166. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  1167. reject_packet:
  1168. trace_rxrpc_rx_done(skb->mark, skb->priority);
  1169. rxrpc_reject_packet(local, skb);
  1170. _leave(" [badmsg]");
  1171. }