input.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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 (dhowells@redhat.com)
  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. rxrpc_notify_socket(call);
  245. state = call->state;
  246. break;
  247. default:
  248. goto bad_state;
  249. }
  250. write_unlock(&call->state_lock);
  251. if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
  252. trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
  253. else
  254. trace_rxrpc_transmit(call, rxrpc_transmit_end);
  255. _leave(" = ok");
  256. return true;
  257. bad_state:
  258. write_unlock(&call->state_lock);
  259. kdebug("end_tx %s", rxrpc_call_states[call->state]);
  260. rxrpc_proto_abort(abort_why, call, call->tx_top);
  261. return false;
  262. }
  263. /*
  264. * Begin the reply reception phase of a call.
  265. */
  266. static bool rxrpc_receiving_reply(struct rxrpc_call *call)
  267. {
  268. struct rxrpc_ack_summary summary = { 0 };
  269. unsigned long now, timo;
  270. rxrpc_seq_t top = READ_ONCE(call->tx_top);
  271. if (call->ackr_reason) {
  272. spin_lock_bh(&call->lock);
  273. call->ackr_reason = 0;
  274. spin_unlock_bh(&call->lock);
  275. now = jiffies;
  276. timo = now + MAX_JIFFY_OFFSET;
  277. WRITE_ONCE(call->resend_at, timo);
  278. WRITE_ONCE(call->ack_at, timo);
  279. trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
  280. }
  281. if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  282. if (!rxrpc_rotate_tx_window(call, top, &summary)) {
  283. rxrpc_proto_abort("TXL", call, top);
  284. return false;
  285. }
  286. }
  287. if (!rxrpc_end_tx_phase(call, true, "ETD"))
  288. return false;
  289. call->tx_phase = false;
  290. return true;
  291. }
  292. /*
  293. * Scan a data packet to validate its structure and to work out how many
  294. * subpackets it contains.
  295. *
  296. * A jumbo packet is a collection of consecutive packets glued together with
  297. * little headers between that indicate how to change the initial header for
  298. * each subpacket.
  299. *
  300. * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
  301. * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
  302. * size.
  303. */
  304. static bool rxrpc_validate_data(struct sk_buff *skb)
  305. {
  306. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  307. unsigned int offset = sizeof(struct rxrpc_wire_header);
  308. unsigned int len = skb->len;
  309. u8 flags = sp->hdr.flags;
  310. for (;;) {
  311. if (flags & RXRPC_REQUEST_ACK)
  312. __set_bit(sp->nr_subpackets, sp->rx_req_ack);
  313. sp->nr_subpackets++;
  314. if (!(flags & RXRPC_JUMBO_PACKET))
  315. break;
  316. if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
  317. goto protocol_error;
  318. if (flags & RXRPC_LAST_PACKET)
  319. goto protocol_error;
  320. offset += RXRPC_JUMBO_DATALEN;
  321. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  322. goto protocol_error;
  323. offset += sizeof(struct rxrpc_jumbo_header);
  324. }
  325. if (flags & RXRPC_LAST_PACKET)
  326. sp->rx_flags |= RXRPC_SKB_INCL_LAST;
  327. return true;
  328. protocol_error:
  329. return false;
  330. }
  331. /*
  332. * Handle reception of a duplicate packet.
  333. *
  334. * We have to take care to avoid an attack here whereby we're given a series of
  335. * jumbograms, each with a sequence number one before the preceding one and
  336. * filled up to maximum UDP size. If they never send us the first packet in
  337. * the sequence, they can cause us to have to hold on to around 2MiB of kernel
  338. * space until the call times out.
  339. *
  340. * We limit the space usage by only accepting three duplicate jumbo packets per
  341. * call. After that, we tell the other side we're no longer accepting jumbos
  342. * (that information is encoded in the ACK packet).
  343. */
  344. static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
  345. bool is_jumbo, bool *_jumbo_bad)
  346. {
  347. /* Discard normal packets that are duplicates. */
  348. if (is_jumbo)
  349. return;
  350. /* Skip jumbo subpackets that are duplicates. When we've had three or
  351. * more partially duplicate jumbo packets, we refuse to take any more
  352. * jumbos for this call.
  353. */
  354. if (!*_jumbo_bad) {
  355. call->nr_jumbo_bad++;
  356. *_jumbo_bad = true;
  357. }
  358. }
  359. /*
  360. * Process a DATA packet, adding the packet to the Rx ring. The caller's
  361. * packet ref must be passed on or discarded.
  362. */
  363. static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
  364. {
  365. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  366. enum rxrpc_call_state state;
  367. unsigned int j, nr_subpackets;
  368. rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
  369. rxrpc_seq_t seq0 = sp->hdr.seq, hard_ack;
  370. bool immediate_ack = false, jumbo_bad = false;
  371. u8 ack = 0;
  372. _enter("{%u,%u},{%u,%u}",
  373. call->rx_hard_ack, call->rx_top, skb->len, seq0);
  374. _proto("Rx DATA %%%u { #%u f=%02x n=%u }",
  375. sp->hdr.serial, seq0, sp->hdr.flags, sp->nr_subpackets);
  376. state = READ_ONCE(call->state);
  377. if (state >= RXRPC_CALL_COMPLETE) {
  378. rxrpc_free_skb(skb, rxrpc_skb_freed);
  379. return;
  380. }
  381. if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
  382. unsigned long timo = READ_ONCE(call->next_req_timo);
  383. unsigned long now, expect_req_by;
  384. if (timo) {
  385. now = jiffies;
  386. expect_req_by = now + timo;
  387. WRITE_ONCE(call->expect_req_by, expect_req_by);
  388. rxrpc_reduce_call_timer(call, expect_req_by, now,
  389. rxrpc_timer_set_for_idle);
  390. }
  391. }
  392. spin_lock(&call->input_lock);
  393. /* Received data implicitly ACKs all of the request packets we sent
  394. * when we're acting as a client.
  395. */
  396. if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  397. state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
  398. !rxrpc_receiving_reply(call))
  399. goto unlock;
  400. call->ackr_prev_seq = seq0;
  401. hard_ack = READ_ONCE(call->rx_hard_ack);
  402. nr_subpackets = sp->nr_subpackets;
  403. if (nr_subpackets > 1) {
  404. if (call->nr_jumbo_bad > 3) {
  405. ack = RXRPC_ACK_NOSPACE;
  406. ack_serial = serial;
  407. goto ack;
  408. }
  409. }
  410. for (j = 0; j < nr_subpackets; j++) {
  411. rxrpc_serial_t serial = sp->hdr.serial + j;
  412. rxrpc_seq_t seq = seq0 + j;
  413. unsigned int ix = seq & RXRPC_RXTX_BUFF_MASK;
  414. bool terminal = (j == nr_subpackets - 1);
  415. bool last = terminal && (sp->rx_flags & RXRPC_SKB_INCL_LAST);
  416. u8 flags, annotation = j;
  417. _proto("Rx DATA+%u %%%u { #%x t=%u l=%u }",
  418. j, serial, seq, terminal, last);
  419. if (last) {
  420. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  421. seq != call->rx_top) {
  422. rxrpc_proto_abort("LSN", call, seq);
  423. goto unlock;
  424. }
  425. } else {
  426. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  427. after_eq(seq, call->rx_top)) {
  428. rxrpc_proto_abort("LSA", call, seq);
  429. goto unlock;
  430. }
  431. }
  432. flags = 0;
  433. if (last)
  434. flags |= RXRPC_LAST_PACKET;
  435. if (!terminal)
  436. flags |= RXRPC_JUMBO_PACKET;
  437. if (test_bit(j, sp->rx_req_ack))
  438. flags |= RXRPC_REQUEST_ACK;
  439. trace_rxrpc_rx_data(call->debug_id, seq, serial, flags, annotation);
  440. if (before_eq(seq, hard_ack)) {
  441. ack = RXRPC_ACK_DUPLICATE;
  442. ack_serial = serial;
  443. continue;
  444. }
  445. if (call->rxtx_buffer[ix]) {
  446. rxrpc_input_dup_data(call, seq, nr_subpackets > 1,
  447. &jumbo_bad);
  448. if (ack != RXRPC_ACK_DUPLICATE) {
  449. ack = RXRPC_ACK_DUPLICATE;
  450. ack_serial = serial;
  451. }
  452. immediate_ack = true;
  453. continue;
  454. }
  455. if (after(seq, hard_ack + call->rx_winsize)) {
  456. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  457. ack_serial = serial;
  458. if (flags & RXRPC_JUMBO_PACKET) {
  459. if (!jumbo_bad) {
  460. call->nr_jumbo_bad++;
  461. jumbo_bad = true;
  462. }
  463. }
  464. goto ack;
  465. }
  466. if (flags & RXRPC_REQUEST_ACK && !ack) {
  467. ack = RXRPC_ACK_REQUESTED;
  468. ack_serial = serial;
  469. }
  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. if (last) {
  502. set_bit(RXRPC_CALL_RX_LAST, &call->flags);
  503. if (!ack) {
  504. ack = RXRPC_ACK_DELAY;
  505. ack_serial = serial;
  506. }
  507. trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
  508. } else {
  509. trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
  510. }
  511. if (after_eq(seq, call->rx_expect_next)) {
  512. if (after(seq, call->rx_expect_next)) {
  513. _net("OOS %u > %u", seq, call->rx_expect_next);
  514. ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  515. ack_serial = serial;
  516. }
  517. call->rx_expect_next = seq + 1;
  518. }
  519. }
  520. ack:
  521. if (ack)
  522. rxrpc_propose_ACK(call, ack, ack_serial,
  523. immediate_ack, true,
  524. rxrpc_propose_ack_input_data);
  525. else
  526. rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, serial,
  527. false, true,
  528. rxrpc_propose_ack_input_data);
  529. trace_rxrpc_notify_socket(call->debug_id, serial);
  530. rxrpc_notify_socket(call);
  531. unlock:
  532. spin_unlock(&call->input_lock);
  533. rxrpc_free_skb(skb, rxrpc_skb_freed);
  534. _leave(" [queued]");
  535. }
  536. /*
  537. * Process a requested ACK.
  538. */
  539. static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  540. ktime_t resp_time,
  541. rxrpc_serial_t orig_serial,
  542. rxrpc_serial_t ack_serial)
  543. {
  544. struct rxrpc_skb_priv *sp;
  545. struct sk_buff *skb;
  546. ktime_t sent_at;
  547. int ix;
  548. for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  549. skb = call->rxtx_buffer[ix];
  550. if (!skb)
  551. continue;
  552. sent_at = skb->tstamp;
  553. smp_rmb(); /* Read timestamp before serial. */
  554. sp = rxrpc_skb(skb);
  555. if (sp->hdr.serial != orig_serial)
  556. continue;
  557. goto found;
  558. }
  559. return;
  560. found:
  561. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  562. orig_serial, ack_serial, sent_at, resp_time);
  563. }
  564. /*
  565. * Process the response to a ping that we sent to find out if we lost an ACK.
  566. *
  567. * If we got back a ping response that indicates a lower tx_top than what we
  568. * had at the time of the ping transmission, we adjudge all the DATA packets
  569. * sent between the response tx_top and the ping-time tx_top to have been lost.
  570. */
  571. static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
  572. {
  573. rxrpc_seq_t top, bottom, seq;
  574. bool resend = false;
  575. spin_lock_bh(&call->lock);
  576. bottom = call->tx_hard_ack + 1;
  577. top = call->acks_lost_top;
  578. if (before(bottom, top)) {
  579. for (seq = bottom; before_eq(seq, top); seq++) {
  580. int ix = seq & RXRPC_RXTX_BUFF_MASK;
  581. u8 annotation = call->rxtx_annotations[ix];
  582. u8 anno_type = annotation & RXRPC_TX_ANNO_MASK;
  583. if (anno_type != RXRPC_TX_ANNO_UNACK)
  584. continue;
  585. annotation &= ~RXRPC_TX_ANNO_MASK;
  586. annotation |= RXRPC_TX_ANNO_RETRANS;
  587. call->rxtx_annotations[ix] = annotation;
  588. resend = true;
  589. }
  590. }
  591. spin_unlock_bh(&call->lock);
  592. if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  593. rxrpc_queue_call(call);
  594. }
  595. /*
  596. * Process a ping response.
  597. */
  598. static void rxrpc_input_ping_response(struct rxrpc_call *call,
  599. ktime_t resp_time,
  600. rxrpc_serial_t orig_serial,
  601. rxrpc_serial_t ack_serial)
  602. {
  603. rxrpc_serial_t ping_serial;
  604. ktime_t ping_time;
  605. ping_time = call->ping_time;
  606. smp_rmb();
  607. ping_serial = READ_ONCE(call->ping_serial);
  608. if (orig_serial == call->acks_lost_ping)
  609. rxrpc_input_check_for_lost_ack(call);
  610. if (before(orig_serial, ping_serial) ||
  611. !test_and_clear_bit(RXRPC_CALL_PINGING, &call->flags))
  612. return;
  613. if (after(orig_serial, ping_serial))
  614. return;
  615. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  616. orig_serial, ack_serial, ping_time, resp_time);
  617. }
  618. /*
  619. * Process the extra information that may be appended to an ACK packet
  620. */
  621. static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  622. struct rxrpc_ackinfo *ackinfo)
  623. {
  624. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  625. struct rxrpc_peer *peer;
  626. unsigned int mtu;
  627. bool wake = false;
  628. u32 rwind = ntohl(ackinfo->rwind);
  629. _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  630. sp->hdr.serial,
  631. ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
  632. rwind, ntohl(ackinfo->jumbo_max));
  633. if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  634. rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  635. if (call->tx_winsize != rwind) {
  636. if (rwind > call->tx_winsize)
  637. wake = true;
  638. trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
  639. call->tx_winsize = rwind;
  640. }
  641. if (call->cong_ssthresh > rwind)
  642. call->cong_ssthresh = rwind;
  643. mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  644. peer = call->peer;
  645. if (mtu < peer->maxdata) {
  646. spin_lock_bh(&peer->lock);
  647. peer->maxdata = mtu;
  648. peer->mtu = mtu + peer->hdrsize;
  649. spin_unlock_bh(&peer->lock);
  650. _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  651. }
  652. if (wake)
  653. wake_up(&call->waitq);
  654. }
  655. /*
  656. * Process individual soft ACKs.
  657. *
  658. * Each ACK in the array corresponds to one packet and can be either an ACK or
  659. * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
  660. * packets that lie beyond the end of the ACK list are scheduled for resend by
  661. * the timer on the basis that the peer might just not have processed them at
  662. * the time the ACK was sent.
  663. */
  664. static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
  665. rxrpc_seq_t seq, int nr_acks,
  666. struct rxrpc_ack_summary *summary)
  667. {
  668. int ix;
  669. u8 annotation, anno_type;
  670. for (; nr_acks > 0; nr_acks--, seq++) {
  671. ix = seq & RXRPC_RXTX_BUFF_MASK;
  672. annotation = call->rxtx_annotations[ix];
  673. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  674. annotation &= ~RXRPC_TX_ANNO_MASK;
  675. switch (*acks++) {
  676. case RXRPC_ACK_TYPE_ACK:
  677. summary->nr_acks++;
  678. if (anno_type == RXRPC_TX_ANNO_ACK)
  679. continue;
  680. summary->nr_new_acks++;
  681. call->rxtx_annotations[ix] =
  682. RXRPC_TX_ANNO_ACK | annotation;
  683. break;
  684. case RXRPC_ACK_TYPE_NACK:
  685. if (!summary->nr_nacks &&
  686. call->acks_lowest_nak != seq) {
  687. call->acks_lowest_nak = seq;
  688. summary->new_low_nack = true;
  689. }
  690. summary->nr_nacks++;
  691. if (anno_type == RXRPC_TX_ANNO_NAK)
  692. continue;
  693. summary->nr_new_nacks++;
  694. if (anno_type == RXRPC_TX_ANNO_RETRANS)
  695. continue;
  696. call->rxtx_annotations[ix] =
  697. RXRPC_TX_ANNO_NAK | annotation;
  698. break;
  699. default:
  700. return rxrpc_proto_abort("SFT", call, 0);
  701. }
  702. }
  703. }
  704. /*
  705. * Return true if the ACK is valid - ie. it doesn't appear to have regressed
  706. * with respect to the ack state conveyed by preceding ACKs.
  707. */
  708. static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
  709. rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
  710. {
  711. rxrpc_seq_t base = READ_ONCE(call->ackr_first_seq);
  712. if (after(first_pkt, base))
  713. return true; /* The window advanced */
  714. if (before(first_pkt, base))
  715. return false; /* firstPacket regressed */
  716. if (after_eq(prev_pkt, call->ackr_prev_seq))
  717. return true; /* previousPacket hasn't regressed. */
  718. /* Some rx implementations put a serial number in previousPacket. */
  719. if (after_eq(prev_pkt, base + call->tx_winsize))
  720. return false;
  721. return true;
  722. }
  723. /*
  724. * Process an ACK packet.
  725. *
  726. * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
  727. * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
  728. *
  729. * A hard-ACK means that a packet has been processed and may be discarded; a
  730. * soft-ACK means that the packet may be discarded and retransmission
  731. * requested. A phase is complete when all packets are hard-ACK'd.
  732. */
  733. static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
  734. {
  735. struct rxrpc_ack_summary summary = { 0 };
  736. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  737. union {
  738. struct rxrpc_ackpacket ack;
  739. struct rxrpc_ackinfo info;
  740. u8 acks[RXRPC_MAXACKS];
  741. } buf;
  742. rxrpc_serial_t ack_serial, acked_serial;
  743. rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
  744. int nr_acks, offset, ioffset;
  745. _enter("");
  746. offset = sizeof(struct rxrpc_wire_header);
  747. if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
  748. _debug("extraction failure");
  749. return rxrpc_proto_abort("XAK", call, 0);
  750. }
  751. offset += sizeof(buf.ack);
  752. ack_serial = sp->hdr.serial;
  753. acked_serial = ntohl(buf.ack.serial);
  754. first_soft_ack = ntohl(buf.ack.firstPacket);
  755. prev_pkt = ntohl(buf.ack.previousPacket);
  756. hard_ack = first_soft_ack - 1;
  757. nr_acks = buf.ack.nAcks;
  758. summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  759. buf.ack.reason : RXRPC_ACK__INVALID);
  760. trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
  761. first_soft_ack, prev_pkt,
  762. summary.ack_reason, nr_acks);
  763. if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  764. rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  765. ack_serial);
  766. if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  767. rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  768. ack_serial);
  769. if (buf.ack.reason == RXRPC_ACK_PING) {
  770. _proto("Rx ACK %%%u PING Request", ack_serial);
  771. rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
  772. ack_serial, true, true,
  773. rxrpc_propose_ack_respond_to_ping);
  774. } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  775. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
  776. ack_serial, true, true,
  777. rxrpc_propose_ack_respond_to_ack);
  778. }
  779. /* Discard any out-of-order or duplicate ACKs (outside lock). */
  780. if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
  781. trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
  782. first_soft_ack, call->ackr_first_seq,
  783. prev_pkt, call->ackr_prev_seq);
  784. return;
  785. }
  786. buf.info.rxMTU = 0;
  787. ioffset = offset + nr_acks + 3;
  788. if (skb->len >= ioffset + sizeof(buf.info) &&
  789. skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
  790. return rxrpc_proto_abort("XAI", call, 0);
  791. spin_lock(&call->input_lock);
  792. /* Discard any out-of-order or duplicate ACKs (inside lock). */
  793. if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
  794. trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
  795. first_soft_ack, call->ackr_first_seq,
  796. prev_pkt, call->ackr_prev_seq);
  797. goto out;
  798. }
  799. call->acks_latest_ts = skb->tstamp;
  800. call->ackr_first_seq = first_soft_ack;
  801. call->ackr_prev_seq = prev_pkt;
  802. /* Parse rwind and mtu sizes if provided. */
  803. if (buf.info.rxMTU)
  804. rxrpc_input_ackinfo(call, skb, &buf.info);
  805. if (first_soft_ack == 0) {
  806. rxrpc_proto_abort("AK0", call, 0);
  807. goto out;
  808. }
  809. /* Ignore ACKs unless we are or have just been transmitting. */
  810. switch (READ_ONCE(call->state)) {
  811. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  812. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  813. case RXRPC_CALL_SERVER_SEND_REPLY:
  814. case RXRPC_CALL_SERVER_AWAIT_ACK:
  815. break;
  816. default:
  817. goto out;
  818. }
  819. if (before(hard_ack, call->tx_hard_ack) ||
  820. after(hard_ack, call->tx_top)) {
  821. rxrpc_proto_abort("AKW", call, 0);
  822. goto out;
  823. }
  824. if (nr_acks > call->tx_top - hard_ack) {
  825. rxrpc_proto_abort("AKN", call, 0);
  826. goto out;
  827. }
  828. if (after(hard_ack, call->tx_hard_ack)) {
  829. if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
  830. rxrpc_end_tx_phase(call, false, "ETA");
  831. goto out;
  832. }
  833. }
  834. if (nr_acks > 0) {
  835. if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0) {
  836. rxrpc_proto_abort("XSA", call, 0);
  837. goto out;
  838. }
  839. rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
  840. &summary);
  841. }
  842. if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  843. RXRPC_TX_ANNO_LAST &&
  844. summary.nr_acks == call->tx_top - hard_ack &&
  845. rxrpc_is_client_call(call))
  846. rxrpc_propose_ACK(call, RXRPC_ACK_PING, ack_serial,
  847. false, true,
  848. rxrpc_propose_ack_ping_for_lost_reply);
  849. rxrpc_congestion_management(call, skb, &summary, acked_serial);
  850. out:
  851. spin_unlock(&call->input_lock);
  852. }
  853. /*
  854. * Process an ACKALL packet.
  855. */
  856. static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
  857. {
  858. struct rxrpc_ack_summary summary = { 0 };
  859. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  860. _proto("Rx ACKALL %%%u", sp->hdr.serial);
  861. spin_lock(&call->input_lock);
  862. if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
  863. rxrpc_end_tx_phase(call, false, "ETL");
  864. spin_unlock(&call->input_lock);
  865. }
  866. /*
  867. * Process an ABORT packet directed at a call.
  868. */
  869. static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  870. {
  871. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  872. __be32 wtmp;
  873. u32 abort_code = RX_CALL_DEAD;
  874. _enter("");
  875. if (skb->len >= 4 &&
  876. skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  877. &wtmp, sizeof(wtmp)) >= 0)
  878. abort_code = ntohl(wtmp);
  879. trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
  880. _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
  881. if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  882. abort_code, -ECONNABORTED))
  883. rxrpc_notify_socket(call);
  884. }
  885. /*
  886. * Process an incoming call packet.
  887. */
  888. static void rxrpc_input_call_packet(struct rxrpc_call *call,
  889. struct sk_buff *skb)
  890. {
  891. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  892. unsigned long timo;
  893. _enter("%p,%p", call, skb);
  894. timo = READ_ONCE(call->next_rx_timo);
  895. if (timo) {
  896. unsigned long now = jiffies, expect_rx_by;
  897. expect_rx_by = now + timo;
  898. WRITE_ONCE(call->expect_rx_by, expect_rx_by);
  899. rxrpc_reduce_call_timer(call, expect_rx_by, now,
  900. rxrpc_timer_set_for_normal);
  901. }
  902. switch (sp->hdr.type) {
  903. case RXRPC_PACKET_TYPE_DATA:
  904. rxrpc_input_data(call, skb);
  905. goto no_free;
  906. case RXRPC_PACKET_TYPE_ACK:
  907. rxrpc_input_ack(call, skb);
  908. break;
  909. case RXRPC_PACKET_TYPE_BUSY:
  910. _proto("Rx BUSY %%%u", sp->hdr.serial);
  911. /* Just ignore BUSY packets from the server; the retry and
  912. * lifespan timers will take care of business. BUSY packets
  913. * from the client don't make sense.
  914. */
  915. break;
  916. case RXRPC_PACKET_TYPE_ABORT:
  917. rxrpc_input_abort(call, skb);
  918. break;
  919. case RXRPC_PACKET_TYPE_ACKALL:
  920. rxrpc_input_ackall(call, skb);
  921. break;
  922. default:
  923. break;
  924. }
  925. rxrpc_free_skb(skb, rxrpc_skb_freed);
  926. no_free:
  927. _leave("");
  928. }
  929. /*
  930. * Handle a new service call on a channel implicitly completing the preceding
  931. * call on that channel. This does not apply to client conns.
  932. *
  933. * TODO: If callNumber > call_id + 1, renegotiate security.
  934. */
  935. static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
  936. struct rxrpc_connection *conn,
  937. struct rxrpc_call *call)
  938. {
  939. switch (READ_ONCE(call->state)) {
  940. case RXRPC_CALL_SERVER_AWAIT_ACK:
  941. rxrpc_call_completed(call);
  942. /* Fall through */
  943. case RXRPC_CALL_COMPLETE:
  944. break;
  945. default:
  946. if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
  947. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  948. rxrpc_queue_call(call);
  949. }
  950. trace_rxrpc_improper_term(call);
  951. break;
  952. }
  953. spin_lock(&rx->incoming_lock);
  954. __rxrpc_disconnect_call(conn, call);
  955. spin_unlock(&rx->incoming_lock);
  956. rxrpc_notify_socket(call);
  957. }
  958. /*
  959. * post connection-level events to the connection
  960. * - this includes challenges, responses, some aborts and call terminal packet
  961. * retransmission.
  962. */
  963. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  964. struct sk_buff *skb)
  965. {
  966. _enter("%p,%p", conn, skb);
  967. skb_queue_tail(&conn->rx_queue, skb);
  968. rxrpc_queue_conn(conn);
  969. }
  970. /*
  971. * post endpoint-level events to the local endpoint
  972. * - this includes debug and version messages
  973. */
  974. static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  975. struct sk_buff *skb)
  976. {
  977. _enter("%p,%p", local, skb);
  978. if (rxrpc_get_local_maybe(local)) {
  979. skb_queue_tail(&local->event_queue, skb);
  980. rxrpc_queue_local(local);
  981. } else {
  982. rxrpc_free_skb(skb, rxrpc_skb_freed);
  983. }
  984. }
  985. /*
  986. * put a packet up for transport-level abort
  987. */
  988. static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  989. {
  990. CHECK_SLAB_OKAY(&local->usage);
  991. if (rxrpc_get_local_maybe(local)) {
  992. skb_queue_tail(&local->reject_queue, skb);
  993. rxrpc_queue_local(local);
  994. } else {
  995. rxrpc_free_skb(skb, rxrpc_skb_freed);
  996. }
  997. }
  998. /*
  999. * Extract the wire header from a packet and translate the byte order.
  1000. */
  1001. static noinline
  1002. int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  1003. {
  1004. struct rxrpc_wire_header whdr;
  1005. /* dig out the RxRPC connection details */
  1006. if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
  1007. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
  1008. tracepoint_string("bad_hdr"));
  1009. return -EBADMSG;
  1010. }
  1011. memset(sp, 0, sizeof(*sp));
  1012. sp->hdr.epoch = ntohl(whdr.epoch);
  1013. sp->hdr.cid = ntohl(whdr.cid);
  1014. sp->hdr.callNumber = ntohl(whdr.callNumber);
  1015. sp->hdr.seq = ntohl(whdr.seq);
  1016. sp->hdr.serial = ntohl(whdr.serial);
  1017. sp->hdr.flags = whdr.flags;
  1018. sp->hdr.type = whdr.type;
  1019. sp->hdr.userStatus = whdr.userStatus;
  1020. sp->hdr.securityIndex = whdr.securityIndex;
  1021. sp->hdr._rsvd = ntohs(whdr._rsvd);
  1022. sp->hdr.serviceId = ntohs(whdr.serviceId);
  1023. return 0;
  1024. }
  1025. /*
  1026. * handle data received on the local endpoint
  1027. * - may be called in interrupt context
  1028. *
  1029. * [!] Note that as this is called from the encap_rcv hook, the socket is not
  1030. * held locked by the caller and nothing prevents sk_user_data on the UDP from
  1031. * being cleared in the middle of processing this function.
  1032. *
  1033. * Called with the RCU read lock held from the IP layer via UDP.
  1034. */
  1035. int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
  1036. {
  1037. struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
  1038. struct rxrpc_connection *conn;
  1039. struct rxrpc_channel *chan;
  1040. struct rxrpc_call *call = NULL;
  1041. struct rxrpc_skb_priv *sp;
  1042. struct rxrpc_peer *peer = NULL;
  1043. struct rxrpc_sock *rx = NULL;
  1044. unsigned int channel;
  1045. _enter("%p", udp_sk);
  1046. if (unlikely(!local)) {
  1047. kfree_skb(skb);
  1048. return 0;
  1049. }
  1050. if (skb->tstamp == 0)
  1051. skb->tstamp = ktime_get_real();
  1052. rxrpc_new_skb(skb, rxrpc_skb_received);
  1053. skb_pull(skb, sizeof(struct udphdr));
  1054. /* The UDP protocol already released all skb resources;
  1055. * we are free to add our own data there.
  1056. */
  1057. sp = rxrpc_skb(skb);
  1058. /* dig out the RxRPC connection details */
  1059. if (rxrpc_extract_header(sp, skb) < 0)
  1060. goto bad_message;
  1061. if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  1062. static int lose;
  1063. if ((lose++ & 7) == 7) {
  1064. trace_rxrpc_rx_lose(sp);
  1065. rxrpc_free_skb(skb, rxrpc_skb_lost);
  1066. return 0;
  1067. }
  1068. }
  1069. if (skb->tstamp == 0)
  1070. skb->tstamp = ktime_get_real();
  1071. trace_rxrpc_rx_packet(sp);
  1072. switch (sp->hdr.type) {
  1073. case RXRPC_PACKET_TYPE_VERSION:
  1074. if (rxrpc_to_client(sp))
  1075. goto discard;
  1076. rxrpc_post_packet_to_local(local, skb);
  1077. goto out;
  1078. case RXRPC_PACKET_TYPE_BUSY:
  1079. if (rxrpc_to_server(sp))
  1080. goto discard;
  1081. /* Fall through */
  1082. case RXRPC_PACKET_TYPE_ACK:
  1083. case RXRPC_PACKET_TYPE_ACKALL:
  1084. if (sp->hdr.callNumber == 0)
  1085. goto bad_message;
  1086. /* Fall through */
  1087. case RXRPC_PACKET_TYPE_ABORT:
  1088. break;
  1089. case RXRPC_PACKET_TYPE_DATA:
  1090. if (sp->hdr.callNumber == 0 ||
  1091. sp->hdr.seq == 0)
  1092. goto bad_message;
  1093. if (!rxrpc_validate_data(skb))
  1094. goto bad_message;
  1095. /* Unshare the packet so that it can be modified for in-place
  1096. * decryption.
  1097. */
  1098. if (sp->hdr.securityIndex != 0) {
  1099. struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
  1100. if (!nskb) {
  1101. rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
  1102. goto out;
  1103. }
  1104. if (nskb != skb) {
  1105. rxrpc_eaten_skb(skb, rxrpc_skb_received);
  1106. skb = nskb;
  1107. rxrpc_new_skb(skb, rxrpc_skb_unshared);
  1108. sp = rxrpc_skb(skb);
  1109. }
  1110. }
  1111. break;
  1112. case RXRPC_PACKET_TYPE_CHALLENGE:
  1113. if (rxrpc_to_server(sp))
  1114. goto discard;
  1115. break;
  1116. case RXRPC_PACKET_TYPE_RESPONSE:
  1117. if (rxrpc_to_client(sp))
  1118. goto discard;
  1119. break;
  1120. /* Packet types 9-11 should just be ignored. */
  1121. case RXRPC_PACKET_TYPE_PARAMS:
  1122. case RXRPC_PACKET_TYPE_10:
  1123. case RXRPC_PACKET_TYPE_11:
  1124. goto discard;
  1125. default:
  1126. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  1127. goto bad_message;
  1128. }
  1129. if (sp->hdr.serviceId == 0)
  1130. goto bad_message;
  1131. if (rxrpc_to_server(sp)) {
  1132. /* Weed out packets to services we're not offering. Packets
  1133. * that would begin a call are explicitly rejected and the rest
  1134. * are just discarded.
  1135. */
  1136. rx = rcu_dereference(local->service);
  1137. if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
  1138. sp->hdr.serviceId != rx->second_service)) {
  1139. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  1140. sp->hdr.seq == 1)
  1141. goto unsupported_service;
  1142. goto discard;
  1143. }
  1144. }
  1145. conn = rxrpc_find_connection_rcu(local, skb, &peer);
  1146. if (conn) {
  1147. if (sp->hdr.securityIndex != conn->security_ix)
  1148. goto wrong_security;
  1149. if (sp->hdr.serviceId != conn->service_id) {
  1150. int old_id;
  1151. if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
  1152. goto reupgrade;
  1153. old_id = cmpxchg(&conn->service_id, conn->params.service_id,
  1154. sp->hdr.serviceId);
  1155. if (old_id != conn->params.service_id &&
  1156. old_id != sp->hdr.serviceId)
  1157. goto reupgrade;
  1158. }
  1159. if (sp->hdr.callNumber == 0) {
  1160. /* Connection-level packet */
  1161. _debug("CONN %p {%d}", conn, conn->debug_id);
  1162. rxrpc_post_packet_to_conn(conn, skb);
  1163. goto out;
  1164. }
  1165. if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
  1166. conn->hi_serial = sp->hdr.serial;
  1167. /* Call-bound packets are routed by connection channel. */
  1168. channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  1169. chan = &conn->channels[channel];
  1170. /* Ignore really old calls */
  1171. if (sp->hdr.callNumber < chan->last_call)
  1172. goto discard;
  1173. if (sp->hdr.callNumber == chan->last_call) {
  1174. if (chan->call ||
  1175. sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
  1176. goto discard;
  1177. /* For the previous service call, if completed
  1178. * successfully, we discard all further packets.
  1179. */
  1180. if (rxrpc_conn_is_service(conn) &&
  1181. chan->last_type == RXRPC_PACKET_TYPE_ACK)
  1182. goto discard;
  1183. /* But otherwise we need to retransmit the final packet
  1184. * from data cached in the connection record.
  1185. */
  1186. if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
  1187. trace_rxrpc_rx_data(chan->call_debug_id,
  1188. sp->hdr.seq,
  1189. sp->hdr.serial,
  1190. sp->hdr.flags, 0);
  1191. rxrpc_post_packet_to_conn(conn, skb);
  1192. goto out;
  1193. }
  1194. call = rcu_dereference(chan->call);
  1195. if (sp->hdr.callNumber > chan->call_id) {
  1196. if (rxrpc_to_client(sp))
  1197. goto reject_packet;
  1198. if (call)
  1199. rxrpc_input_implicit_end_call(rx, conn, call);
  1200. call = NULL;
  1201. }
  1202. if (call) {
  1203. if (sp->hdr.serviceId != call->service_id)
  1204. call->service_id = sp->hdr.serviceId;
  1205. if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
  1206. call->rx_serial = sp->hdr.serial;
  1207. if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
  1208. set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
  1209. }
  1210. }
  1211. if (!call || atomic_read(&call->usage) == 0) {
  1212. if (rxrpc_to_client(sp) ||
  1213. sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  1214. goto bad_message;
  1215. if (sp->hdr.seq != 1)
  1216. goto discard;
  1217. call = rxrpc_new_incoming_call(local, rx, skb);
  1218. if (!call)
  1219. goto reject_packet;
  1220. }
  1221. /* Process a call packet; this either discards or passes on the ref
  1222. * elsewhere.
  1223. */
  1224. rxrpc_input_call_packet(call, skb);
  1225. goto out;
  1226. discard:
  1227. rxrpc_free_skb(skb, rxrpc_skb_freed);
  1228. out:
  1229. trace_rxrpc_rx_done(0, 0);
  1230. return 0;
  1231. wrong_security:
  1232. trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1233. RXKADINCONSISTENCY, EBADMSG);
  1234. skb->priority = RXKADINCONSISTENCY;
  1235. goto post_abort;
  1236. unsupported_service:
  1237. trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1238. RX_INVALID_OPERATION, EOPNOTSUPP);
  1239. skb->priority = RX_INVALID_OPERATION;
  1240. goto post_abort;
  1241. reupgrade:
  1242. trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1243. RX_PROTOCOL_ERROR, EBADMSG);
  1244. goto protocol_error;
  1245. bad_message:
  1246. trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  1247. RX_PROTOCOL_ERROR, EBADMSG);
  1248. protocol_error:
  1249. skb->priority = RX_PROTOCOL_ERROR;
  1250. post_abort:
  1251. skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
  1252. reject_packet:
  1253. trace_rxrpc_rx_done(skb->mark, skb->priority);
  1254. rxrpc_reject_packet(local, skb);
  1255. _leave(" [badmsg]");
  1256. return 0;
  1257. }