input.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, true,
  265. rxrpc_propose_ack_client_tx_end);
  266. trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
  267. } else {
  268. trace_rxrpc_transmit(call, rxrpc_transmit_end);
  269. }
  270. _leave(" = ok");
  271. return true;
  272. bad_state:
  273. write_unlock(&call->state_lock);
  274. kdebug("end_tx %s", rxrpc_call_states[call->state]);
  275. rxrpc_proto_abort(abort_why, call, call->tx_top);
  276. return false;
  277. }
  278. /*
  279. * Begin the reply reception phase of a call.
  280. */
  281. static bool rxrpc_receiving_reply(struct rxrpc_call *call)
  282. {
  283. struct rxrpc_ack_summary summary = { 0 };
  284. rxrpc_seq_t top = READ_ONCE(call->tx_top);
  285. if (call->ackr_reason) {
  286. spin_lock_bh(&call->lock);
  287. call->ackr_reason = 0;
  288. call->resend_at = call->expire_at;
  289. call->ack_at = call->expire_at;
  290. spin_unlock_bh(&call->lock);
  291. rxrpc_set_timer(call, rxrpc_timer_init_for_reply,
  292. ktime_get_real());
  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. unsigned int offset = sizeof(struct rxrpc_wire_header);
  376. unsigned int ix;
  377. rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
  378. rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
  379. bool immediate_ack = false, jumbo_bad = false, queued;
  380. u16 len;
  381. u8 ack = 0, flags, annotation = 0;
  382. _enter("{%u,%u},{%u,%u}",
  383. call->rx_hard_ack, call->rx_top, skb->len, seq);
  384. _proto("Rx DATA %%%u { #%u f=%02x }",
  385. sp->hdr.serial, seq, sp->hdr.flags);
  386. if (call->state >= RXRPC_CALL_COMPLETE)
  387. return;
  388. /* Received data implicitly ACKs all of the request packets we sent
  389. * when we're acting as a client.
  390. */
  391. if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  392. call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
  393. !rxrpc_receiving_reply(call))
  394. return;
  395. call->ackr_prev_seq = seq;
  396. hard_ack = READ_ONCE(call->rx_hard_ack);
  397. if (after(seq, hard_ack + call->rx_winsize)) {
  398. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  399. ack_serial = serial;
  400. goto ack;
  401. }
  402. flags = sp->hdr.flags;
  403. if (flags & RXRPC_JUMBO_PACKET) {
  404. if (call->nr_jumbo_bad > 3) {
  405. ack = RXRPC_ACK_NOSPACE;
  406. ack_serial = serial;
  407. goto ack;
  408. }
  409. annotation = 1;
  410. }
  411. next_subpacket:
  412. queued = false;
  413. ix = seq & RXRPC_RXTX_BUFF_MASK;
  414. len = skb->len;
  415. if (flags & RXRPC_JUMBO_PACKET)
  416. len = RXRPC_JUMBO_DATALEN;
  417. if (flags & RXRPC_LAST_PACKET) {
  418. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  419. seq != call->rx_top)
  420. return rxrpc_proto_abort("LSN", call, seq);
  421. } else {
  422. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  423. after_eq(seq, call->rx_top))
  424. return rxrpc_proto_abort("LSA", call, seq);
  425. }
  426. if (before_eq(seq, hard_ack)) {
  427. ack = RXRPC_ACK_DUPLICATE;
  428. ack_serial = serial;
  429. goto skip;
  430. }
  431. if (flags & RXRPC_REQUEST_ACK && !ack) {
  432. ack = RXRPC_ACK_REQUESTED;
  433. ack_serial = serial;
  434. }
  435. if (call->rxtx_buffer[ix]) {
  436. rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
  437. if (ack != RXRPC_ACK_DUPLICATE) {
  438. ack = RXRPC_ACK_DUPLICATE;
  439. ack_serial = serial;
  440. }
  441. immediate_ack = true;
  442. goto skip;
  443. }
  444. /* Queue the packet. We use a couple of memory barriers here as need
  445. * to make sure that rx_top is perceived to be set after the buffer
  446. * pointer and that the buffer pointer is set after the annotation and
  447. * the skb data.
  448. *
  449. * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
  450. * and also rxrpc_fill_out_ack().
  451. */
  452. rxrpc_get_skb(skb, rxrpc_skb_rx_got);
  453. call->rxtx_annotations[ix] = annotation;
  454. smp_wmb();
  455. call->rxtx_buffer[ix] = skb;
  456. if (after(seq, call->rx_top)) {
  457. smp_store_release(&call->rx_top, seq);
  458. } else if (before(seq, call->rx_top)) {
  459. /* Send an immediate ACK if we fill in a hole */
  460. if (!ack) {
  461. ack = RXRPC_ACK_DELAY;
  462. ack_serial = serial;
  463. }
  464. immediate_ack = true;
  465. }
  466. if (flags & RXRPC_LAST_PACKET) {
  467. set_bit(RXRPC_CALL_RX_LAST, &call->flags);
  468. trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
  469. } else {
  470. trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
  471. }
  472. queued = true;
  473. if (after_eq(seq, call->rx_expect_next)) {
  474. if (after(seq, call->rx_expect_next)) {
  475. _net("OOS %u > %u", seq, call->rx_expect_next);
  476. ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  477. ack_serial = serial;
  478. }
  479. call->rx_expect_next = seq + 1;
  480. }
  481. skip:
  482. offset += len;
  483. if (flags & RXRPC_JUMBO_PACKET) {
  484. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  485. return rxrpc_proto_abort("XJF", call, seq);
  486. offset += sizeof(struct rxrpc_jumbo_header);
  487. seq++;
  488. serial++;
  489. annotation++;
  490. if (flags & RXRPC_JUMBO_PACKET)
  491. annotation |= RXRPC_RX_ANNO_JLAST;
  492. if (after(seq, hard_ack + call->rx_winsize)) {
  493. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  494. ack_serial = serial;
  495. if (!jumbo_bad) {
  496. call->nr_jumbo_bad++;
  497. jumbo_bad = true;
  498. }
  499. goto ack;
  500. }
  501. _proto("Rx DATA Jumbo %%%u", serial);
  502. goto next_subpacket;
  503. }
  504. if (queued && flags & RXRPC_LAST_PACKET && !ack) {
  505. ack = RXRPC_ACK_DELAY;
  506. ack_serial = serial;
  507. }
  508. ack:
  509. if (ack)
  510. rxrpc_propose_ACK(call, ack, skew, ack_serial,
  511. immediate_ack, true,
  512. rxrpc_propose_ack_input_data);
  513. if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
  514. rxrpc_notify_socket(call);
  515. _leave(" [queued]");
  516. }
  517. /*
  518. * Process a requested ACK.
  519. */
  520. static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  521. ktime_t resp_time,
  522. rxrpc_serial_t orig_serial,
  523. rxrpc_serial_t ack_serial)
  524. {
  525. struct rxrpc_skb_priv *sp;
  526. struct sk_buff *skb;
  527. ktime_t sent_at;
  528. int ix;
  529. for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  530. skb = call->rxtx_buffer[ix];
  531. if (!skb)
  532. continue;
  533. sp = rxrpc_skb(skb);
  534. if (sp->hdr.serial != orig_serial)
  535. continue;
  536. smp_rmb();
  537. sent_at = skb->tstamp;
  538. goto found;
  539. }
  540. return;
  541. found:
  542. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  543. orig_serial, ack_serial, sent_at, resp_time);
  544. }
  545. /*
  546. * Process a ping response.
  547. */
  548. static void rxrpc_input_ping_response(struct rxrpc_call *call,
  549. ktime_t resp_time,
  550. rxrpc_serial_t orig_serial,
  551. rxrpc_serial_t ack_serial)
  552. {
  553. rxrpc_serial_t ping_serial;
  554. ktime_t ping_time;
  555. ping_time = call->ping_time;
  556. smp_rmb();
  557. ping_serial = call->ping_serial;
  558. if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
  559. before(orig_serial, ping_serial))
  560. return;
  561. clear_bit(RXRPC_CALL_PINGING, &call->flags);
  562. if (after(orig_serial, ping_serial))
  563. return;
  564. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  565. orig_serial, ack_serial, ping_time, resp_time);
  566. }
  567. /*
  568. * Process the extra information that may be appended to an ACK packet
  569. */
  570. static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  571. struct rxrpc_ackinfo *ackinfo)
  572. {
  573. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  574. struct rxrpc_peer *peer;
  575. unsigned int mtu;
  576. bool wake = false;
  577. u32 rwind = ntohl(ackinfo->rwind);
  578. _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  579. sp->hdr.serial,
  580. ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
  581. rwind, ntohl(ackinfo->jumbo_max));
  582. if (call->tx_winsize != rwind) {
  583. if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  584. rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  585. if (rwind > call->tx_winsize)
  586. wake = true;
  587. call->tx_winsize = rwind;
  588. }
  589. if (call->cong_ssthresh > rwind)
  590. call->cong_ssthresh = rwind;
  591. mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  592. peer = call->peer;
  593. if (mtu < peer->maxdata) {
  594. spin_lock_bh(&peer->lock);
  595. peer->maxdata = mtu;
  596. peer->mtu = mtu + peer->hdrsize;
  597. spin_unlock_bh(&peer->lock);
  598. _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  599. }
  600. if (wake)
  601. wake_up(&call->waitq);
  602. }
  603. /*
  604. * Process individual soft ACKs.
  605. *
  606. * Each ACK in the array corresponds to one packet and can be either an ACK or
  607. * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
  608. * packets that lie beyond the end of the ACK list are scheduled for resend by
  609. * the timer on the basis that the peer might just not have processed them at
  610. * the time the ACK was sent.
  611. */
  612. static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
  613. rxrpc_seq_t seq, int nr_acks,
  614. struct rxrpc_ack_summary *summary)
  615. {
  616. int ix;
  617. u8 annotation, anno_type;
  618. for (; nr_acks > 0; nr_acks--, seq++) {
  619. ix = seq & RXRPC_RXTX_BUFF_MASK;
  620. annotation = call->rxtx_annotations[ix];
  621. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  622. annotation &= ~RXRPC_TX_ANNO_MASK;
  623. switch (*acks++) {
  624. case RXRPC_ACK_TYPE_ACK:
  625. summary->nr_acks++;
  626. if (anno_type == RXRPC_TX_ANNO_ACK)
  627. continue;
  628. summary->nr_new_acks++;
  629. call->rxtx_annotations[ix] =
  630. RXRPC_TX_ANNO_ACK | annotation;
  631. break;
  632. case RXRPC_ACK_TYPE_NACK:
  633. if (!summary->nr_nacks &&
  634. call->acks_lowest_nak != seq) {
  635. call->acks_lowest_nak = seq;
  636. summary->new_low_nack = true;
  637. }
  638. summary->nr_nacks++;
  639. if (anno_type == RXRPC_TX_ANNO_NAK)
  640. continue;
  641. summary->nr_new_nacks++;
  642. if (anno_type == RXRPC_TX_ANNO_RETRANS)
  643. continue;
  644. call->rxtx_annotations[ix] =
  645. RXRPC_TX_ANNO_NAK | annotation;
  646. break;
  647. default:
  648. return rxrpc_proto_abort("SFT", call, 0);
  649. }
  650. }
  651. }
  652. /*
  653. * Process an ACK packet.
  654. *
  655. * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
  656. * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
  657. *
  658. * A hard-ACK means that a packet has been processed and may be discarded; a
  659. * soft-ACK means that the packet may be discarded and retransmission
  660. * requested. A phase is complete when all packets are hard-ACK'd.
  661. */
  662. static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
  663. u16 skew)
  664. {
  665. struct rxrpc_ack_summary summary = { 0 };
  666. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  667. union {
  668. struct rxrpc_ackpacket ack;
  669. struct rxrpc_ackinfo info;
  670. u8 acks[RXRPC_MAXACKS];
  671. } buf;
  672. rxrpc_serial_t acked_serial;
  673. rxrpc_seq_t first_soft_ack, hard_ack;
  674. int nr_acks, offset, ioffset;
  675. _enter("");
  676. offset = sizeof(struct rxrpc_wire_header);
  677. if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
  678. _debug("extraction failure");
  679. return rxrpc_proto_abort("XAK", call, 0);
  680. }
  681. offset += sizeof(buf.ack);
  682. acked_serial = ntohl(buf.ack.serial);
  683. first_soft_ack = ntohl(buf.ack.firstPacket);
  684. hard_ack = first_soft_ack - 1;
  685. nr_acks = buf.ack.nAcks;
  686. summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  687. buf.ack.reason : RXRPC_ACK__INVALID);
  688. trace_rxrpc_rx_ack(call, first_soft_ack, summary.ack_reason, nr_acks);
  689. _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
  690. sp->hdr.serial,
  691. ntohs(buf.ack.maxSkew),
  692. first_soft_ack,
  693. ntohl(buf.ack.previousPacket),
  694. acked_serial,
  695. rxrpc_ack_names[summary.ack_reason],
  696. buf.ack.nAcks);
  697. if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  698. rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  699. sp->hdr.serial);
  700. if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  701. rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  702. sp->hdr.serial);
  703. if (buf.ack.reason == RXRPC_ACK_PING) {
  704. _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
  705. rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
  706. skew, sp->hdr.serial, true, true,
  707. rxrpc_propose_ack_respond_to_ping);
  708. } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  709. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
  710. skew, sp->hdr.serial, true, true,
  711. rxrpc_propose_ack_respond_to_ack);
  712. }
  713. ioffset = offset + nr_acks + 3;
  714. if (skb->len >= ioffset + sizeof(buf.info)) {
  715. if (skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
  716. return rxrpc_proto_abort("XAI", call, 0);
  717. rxrpc_input_ackinfo(call, skb, &buf.info);
  718. }
  719. if (first_soft_ack == 0)
  720. return rxrpc_proto_abort("AK0", call, 0);
  721. /* Ignore ACKs unless we are or have just been transmitting. */
  722. switch (call->state) {
  723. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  724. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  725. case RXRPC_CALL_SERVER_SEND_REPLY:
  726. case RXRPC_CALL_SERVER_AWAIT_ACK:
  727. break;
  728. default:
  729. return;
  730. }
  731. /* Discard any out-of-order or duplicate ACKs. */
  732. if (before_eq(sp->hdr.serial, call->acks_latest)) {
  733. _debug("discard ACK %d <= %d",
  734. sp->hdr.serial, call->acks_latest);
  735. return;
  736. }
  737. call->acks_latest_ts = skb->tstamp;
  738. call->acks_latest = sp->hdr.serial;
  739. if (before(hard_ack, call->tx_hard_ack) ||
  740. after(hard_ack, call->tx_top))
  741. return rxrpc_proto_abort("AKW", call, 0);
  742. if (nr_acks > call->tx_top - hard_ack)
  743. return rxrpc_proto_abort("AKN", call, 0);
  744. if (after(hard_ack, call->tx_hard_ack))
  745. rxrpc_rotate_tx_window(call, hard_ack, &summary);
  746. if (nr_acks > 0) {
  747. if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0)
  748. return rxrpc_proto_abort("XSA", call, 0);
  749. rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
  750. &summary);
  751. }
  752. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  753. rxrpc_end_tx_phase(call, false, "ETA");
  754. return;
  755. }
  756. if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  757. RXRPC_TX_ANNO_LAST &&
  758. summary.nr_acks == call->tx_top - hard_ack &&
  759. rxrpc_is_client_call(call))
  760. rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  761. false, true,
  762. rxrpc_propose_ack_ping_for_lost_reply);
  763. return rxrpc_congestion_management(call, skb, &summary, acked_serial);
  764. }
  765. /*
  766. * Process an ACKALL packet.
  767. */
  768. static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
  769. {
  770. struct rxrpc_ack_summary summary = { 0 };
  771. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  772. _proto("Rx ACKALL %%%u", sp->hdr.serial);
  773. rxrpc_rotate_tx_window(call, call->tx_top, &summary);
  774. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  775. rxrpc_end_tx_phase(call, false, "ETL");
  776. }
  777. /*
  778. * Process an ABORT packet.
  779. */
  780. static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  781. {
  782. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  783. __be32 wtmp;
  784. u32 abort_code = RX_CALL_DEAD;
  785. _enter("");
  786. if (skb->len >= 4 &&
  787. skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  788. &wtmp, sizeof(wtmp)) >= 0)
  789. abort_code = ntohl(wtmp);
  790. _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
  791. if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  792. abort_code, ECONNABORTED))
  793. rxrpc_notify_socket(call);
  794. }
  795. /*
  796. * Process an incoming call packet.
  797. */
  798. static void rxrpc_input_call_packet(struct rxrpc_call *call,
  799. struct sk_buff *skb, u16 skew)
  800. {
  801. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  802. _enter("%p,%p", call, skb);
  803. switch (sp->hdr.type) {
  804. case RXRPC_PACKET_TYPE_DATA:
  805. rxrpc_input_data(call, skb, skew);
  806. break;
  807. case RXRPC_PACKET_TYPE_ACK:
  808. rxrpc_input_ack(call, skb, skew);
  809. break;
  810. case RXRPC_PACKET_TYPE_BUSY:
  811. _proto("Rx BUSY %%%u", sp->hdr.serial);
  812. /* Just ignore BUSY packets from the server; the retry and
  813. * lifespan timers will take care of business. BUSY packets
  814. * from the client don't make sense.
  815. */
  816. break;
  817. case RXRPC_PACKET_TYPE_ABORT:
  818. rxrpc_input_abort(call, skb);
  819. break;
  820. case RXRPC_PACKET_TYPE_ACKALL:
  821. rxrpc_input_ackall(call, skb);
  822. break;
  823. default:
  824. _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
  825. break;
  826. }
  827. _leave("");
  828. }
  829. /*
  830. * Handle a new call on a channel implicitly completing the preceding call on
  831. * that channel.
  832. *
  833. * TODO: If callNumber > call_id + 1, renegotiate security.
  834. */
  835. static void rxrpc_input_implicit_end_call(struct rxrpc_connection *conn,
  836. struct rxrpc_call *call)
  837. {
  838. switch (call->state) {
  839. case RXRPC_CALL_SERVER_AWAIT_ACK:
  840. rxrpc_call_completed(call);
  841. break;
  842. case RXRPC_CALL_COMPLETE:
  843. break;
  844. default:
  845. if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, ESHUTDOWN)) {
  846. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  847. rxrpc_queue_call(call);
  848. }
  849. break;
  850. }
  851. __rxrpc_disconnect_call(conn, call);
  852. rxrpc_notify_socket(call);
  853. }
  854. /*
  855. * post connection-level events to the connection
  856. * - this includes challenges, responses, some aborts and call terminal packet
  857. * retransmission.
  858. */
  859. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  860. struct sk_buff *skb)
  861. {
  862. _enter("%p,%p", conn, skb);
  863. skb_queue_tail(&conn->rx_queue, skb);
  864. rxrpc_queue_conn(conn);
  865. }
  866. /*
  867. * post endpoint-level events to the local endpoint
  868. * - this includes debug and version messages
  869. */
  870. static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  871. struct sk_buff *skb)
  872. {
  873. _enter("%p,%p", local, skb);
  874. skb_queue_tail(&local->event_queue, skb);
  875. rxrpc_queue_local(local);
  876. }
  877. /*
  878. * put a packet up for transport-level abort
  879. */
  880. static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  881. {
  882. CHECK_SLAB_OKAY(&local->usage);
  883. skb_queue_tail(&local->reject_queue, skb);
  884. rxrpc_queue_local(local);
  885. }
  886. /*
  887. * Extract the wire header from a packet and translate the byte order.
  888. */
  889. static noinline
  890. int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  891. {
  892. struct rxrpc_wire_header whdr;
  893. /* dig out the RxRPC connection details */
  894. if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
  895. return -EBADMSG;
  896. memset(sp, 0, sizeof(*sp));
  897. sp->hdr.epoch = ntohl(whdr.epoch);
  898. sp->hdr.cid = ntohl(whdr.cid);
  899. sp->hdr.callNumber = ntohl(whdr.callNumber);
  900. sp->hdr.seq = ntohl(whdr.seq);
  901. sp->hdr.serial = ntohl(whdr.serial);
  902. sp->hdr.flags = whdr.flags;
  903. sp->hdr.type = whdr.type;
  904. sp->hdr.userStatus = whdr.userStatus;
  905. sp->hdr.securityIndex = whdr.securityIndex;
  906. sp->hdr._rsvd = ntohs(whdr._rsvd);
  907. sp->hdr.serviceId = ntohs(whdr.serviceId);
  908. return 0;
  909. }
  910. /*
  911. * handle data received on the local endpoint
  912. * - may be called in interrupt context
  913. *
  914. * The socket is locked by the caller and this prevents the socket from being
  915. * shut down and the local endpoint from going away, thus sk_user_data will not
  916. * be cleared until this function returns.
  917. */
  918. void rxrpc_data_ready(struct sock *udp_sk)
  919. {
  920. struct rxrpc_connection *conn;
  921. struct rxrpc_channel *chan;
  922. struct rxrpc_call *call;
  923. struct rxrpc_skb_priv *sp;
  924. struct rxrpc_local *local = udp_sk->sk_user_data;
  925. struct sk_buff *skb;
  926. unsigned int channel;
  927. int ret, skew;
  928. _enter("%p", udp_sk);
  929. ASSERT(!irqs_disabled());
  930. skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
  931. if (!skb) {
  932. if (ret == -EAGAIN)
  933. return;
  934. _debug("UDP socket error %d", ret);
  935. return;
  936. }
  937. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  938. _net("recv skb %p", skb);
  939. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  940. if (skb_checksum_complete(skb)) {
  941. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  942. __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
  943. _leave(" [CSUM failed]");
  944. return;
  945. }
  946. __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
  947. /* The socket buffer we have is owned by UDP, with UDP's data all over
  948. * it, but we really want our own data there.
  949. */
  950. skb_orphan(skb);
  951. sp = rxrpc_skb(skb);
  952. /* dig out the RxRPC connection details */
  953. if (rxrpc_extract_header(sp, skb) < 0)
  954. goto bad_message;
  955. if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  956. static int lose;
  957. if ((lose++ & 7) == 7) {
  958. trace_rxrpc_rx_lose(sp);
  959. rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
  960. return;
  961. }
  962. }
  963. trace_rxrpc_rx_packet(sp);
  964. _net("Rx RxRPC %s ep=%x call=%x:%x",
  965. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  966. sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
  967. if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
  968. !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
  969. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  970. goto bad_message;
  971. }
  972. switch (sp->hdr.type) {
  973. case RXRPC_PACKET_TYPE_VERSION:
  974. rxrpc_post_packet_to_local(local, skb);
  975. goto out;
  976. case RXRPC_PACKET_TYPE_BUSY:
  977. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  978. goto discard;
  979. case RXRPC_PACKET_TYPE_DATA:
  980. if (sp->hdr.callNumber == 0)
  981. goto bad_message;
  982. if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
  983. !rxrpc_validate_jumbo(skb))
  984. goto bad_message;
  985. break;
  986. }
  987. rcu_read_lock();
  988. conn = rxrpc_find_connection_rcu(local, skb);
  989. if (conn) {
  990. if (sp->hdr.securityIndex != conn->security_ix)
  991. goto wrong_security;
  992. if (sp->hdr.callNumber == 0) {
  993. /* Connection-level packet */
  994. _debug("CONN %p {%d}", conn, conn->debug_id);
  995. rxrpc_post_packet_to_conn(conn, skb);
  996. goto out_unlock;
  997. }
  998. /* Note the serial number skew here */
  999. skew = (int)sp->hdr.serial - (int)conn->hi_serial;
  1000. if (skew >= 0) {
  1001. if (skew > 0)
  1002. conn->hi_serial = sp->hdr.serial;
  1003. } else {
  1004. skew = -skew;
  1005. skew = min(skew, 65535);
  1006. }
  1007. /* Call-bound packets are routed by connection channel. */
  1008. channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  1009. chan = &conn->channels[channel];
  1010. /* Ignore really old calls */
  1011. if (sp->hdr.callNumber < chan->last_call)
  1012. goto discard_unlock;
  1013. if (sp->hdr.callNumber == chan->last_call) {
  1014. if (chan->call ||
  1015. sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
  1016. goto discard_unlock;
  1017. /* For the previous service call, if completed
  1018. * successfully, we discard all further packets.
  1019. */
  1020. if (rxrpc_conn_is_service(conn) &&
  1021. chan->last_type == RXRPC_PACKET_TYPE_ACK)
  1022. goto discard_unlock;
  1023. /* But otherwise we need to retransmit the final packet
  1024. * from data cached in the connection record.
  1025. */
  1026. rxrpc_post_packet_to_conn(conn, skb);
  1027. goto out_unlock;
  1028. }
  1029. call = rcu_dereference(chan->call);
  1030. if (sp->hdr.callNumber > chan->call_id) {
  1031. if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED)) {
  1032. rcu_read_unlock();
  1033. goto reject_packet;
  1034. }
  1035. if (call)
  1036. rxrpc_input_implicit_end_call(conn, call);
  1037. call = NULL;
  1038. }
  1039. } else {
  1040. skew = 0;
  1041. call = NULL;
  1042. }
  1043. if (!call || atomic_read(&call->usage) == 0) {
  1044. if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
  1045. sp->hdr.callNumber == 0 ||
  1046. sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  1047. goto bad_message_unlock;
  1048. if (sp->hdr.seq != 1)
  1049. goto discard_unlock;
  1050. call = rxrpc_new_incoming_call(local, conn, skb);
  1051. if (!call) {
  1052. rcu_read_unlock();
  1053. goto reject_packet;
  1054. }
  1055. rxrpc_send_ping(call, skb, skew);
  1056. }
  1057. rxrpc_input_call_packet(call, skb, skew);
  1058. goto discard_unlock;
  1059. discard_unlock:
  1060. rcu_read_unlock();
  1061. discard:
  1062. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  1063. out:
  1064. trace_rxrpc_rx_done(0, 0);
  1065. return;
  1066. out_unlock:
  1067. rcu_read_unlock();
  1068. goto out;
  1069. wrong_security:
  1070. rcu_read_unlock();
  1071. trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1072. RXKADINCONSISTENCY, EBADMSG);
  1073. skb->priority = RXKADINCONSISTENCY;
  1074. goto post_abort;
  1075. bad_message_unlock:
  1076. rcu_read_unlock();
  1077. bad_message:
  1078. trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1079. RX_PROTOCOL_ERROR, EBADMSG);
  1080. skb->priority = RX_PROTOCOL_ERROR;
  1081. post_abort:
  1082. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  1083. reject_packet:
  1084. trace_rxrpc_rx_done(skb->mark, skb->priority);
  1085. rxrpc_reject_packet(local, skb);
  1086. _leave(" [badmsg]");
  1087. }