input.c 36 KB

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