input.c 39 KB

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