input.c 38 KB

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