input.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. * Ping the other end to fill our RTT cache and to retrieve the rwind
  37. * and MTU parameters.
  38. */
  39. static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
  40. int skew)
  41. {
  42. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  43. ktime_t now = skb->tstamp;
  44. if (call->peer->rtt_usage < 3 ||
  45. ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
  46. rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  47. true, true,
  48. rxrpc_propose_ack_ping_for_params);
  49. }
  50. /*
  51. * Apply a hard ACK by advancing the Tx window.
  52. */
  53. static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
  54. {
  55. struct sk_buff *skb, *list = NULL;
  56. int ix;
  57. u8 annotation;
  58. spin_lock(&call->lock);
  59. while (before(call->tx_hard_ack, to)) {
  60. call->tx_hard_ack++;
  61. ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
  62. skb = call->rxtx_buffer[ix];
  63. annotation = call->rxtx_annotations[ix];
  64. rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
  65. call->rxtx_buffer[ix] = NULL;
  66. call->rxtx_annotations[ix] = 0;
  67. skb->next = list;
  68. list = skb;
  69. if (annotation & RXRPC_TX_ANNO_LAST)
  70. set_bit(RXRPC_CALL_TX_LAST, &call->flags);
  71. }
  72. spin_unlock(&call->lock);
  73. trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
  74. rxrpc_transmit_rotate_last :
  75. rxrpc_transmit_rotate));
  76. wake_up(&call->waitq);
  77. while (list) {
  78. skb = list;
  79. list = skb->next;
  80. skb->next = NULL;
  81. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  82. }
  83. }
  84. /*
  85. * End the transmission phase of a call.
  86. *
  87. * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
  88. * or a final ACK packet.
  89. */
  90. static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
  91. const char *abort_why)
  92. {
  93. ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
  94. write_lock(&call->state_lock);
  95. switch (call->state) {
  96. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  97. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  98. if (reply_begun)
  99. call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
  100. else
  101. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  102. break;
  103. case RXRPC_CALL_SERVER_AWAIT_ACK:
  104. __rxrpc_call_completed(call);
  105. rxrpc_notify_socket(call);
  106. break;
  107. default:
  108. goto bad_state;
  109. }
  110. write_unlock(&call->state_lock);
  111. if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
  112. trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
  113. } else {
  114. trace_rxrpc_transmit(call, rxrpc_transmit_end);
  115. }
  116. _leave(" = ok");
  117. return true;
  118. bad_state:
  119. write_unlock(&call->state_lock);
  120. kdebug("end_tx %s", rxrpc_call_states[call->state]);
  121. rxrpc_proto_abort(abort_why, call, call->tx_top);
  122. return false;
  123. }
  124. /*
  125. * Begin the reply reception phase of a call.
  126. */
  127. static bool rxrpc_receiving_reply(struct rxrpc_call *call)
  128. {
  129. rxrpc_seq_t top = READ_ONCE(call->tx_top);
  130. if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  131. rxrpc_rotate_tx_window(call, top);
  132. if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  133. rxrpc_proto_abort("TXL", call, top);
  134. return false;
  135. }
  136. if (!rxrpc_end_tx_phase(call, true, "ETD"))
  137. return false;
  138. call->tx_phase = false;
  139. return true;
  140. }
  141. /*
  142. * Scan a jumbo packet to validate its structure and to work out how many
  143. * subpackets it contains.
  144. *
  145. * A jumbo packet is a collection of consecutive packets glued together with
  146. * little headers between that indicate how to change the initial header for
  147. * each subpacket.
  148. *
  149. * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
  150. * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
  151. * size.
  152. */
  153. static bool rxrpc_validate_jumbo(struct sk_buff *skb)
  154. {
  155. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  156. unsigned int offset = sp->offset;
  157. unsigned int len = skb->len;
  158. int nr_jumbo = 1;
  159. u8 flags = sp->hdr.flags;
  160. do {
  161. nr_jumbo++;
  162. if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
  163. goto protocol_error;
  164. if (flags & RXRPC_LAST_PACKET)
  165. goto protocol_error;
  166. offset += RXRPC_JUMBO_DATALEN;
  167. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  168. goto protocol_error;
  169. offset += sizeof(struct rxrpc_jumbo_header);
  170. } while (flags & RXRPC_JUMBO_PACKET);
  171. sp->nr_jumbo = nr_jumbo;
  172. return true;
  173. protocol_error:
  174. return false;
  175. }
  176. /*
  177. * Handle reception of a duplicate packet.
  178. *
  179. * We have to take care to avoid an attack here whereby we're given a series of
  180. * jumbograms, each with a sequence number one before the preceding one and
  181. * filled up to maximum UDP size. If they never send us the first packet in
  182. * the sequence, they can cause us to have to hold on to around 2MiB of kernel
  183. * space until the call times out.
  184. *
  185. * We limit the space usage by only accepting three duplicate jumbo packets per
  186. * call. After that, we tell the other side we're no longer accepting jumbos
  187. * (that information is encoded in the ACK packet).
  188. */
  189. static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
  190. u8 annotation, bool *_jumbo_bad)
  191. {
  192. /* Discard normal packets that are duplicates. */
  193. if (annotation == 0)
  194. return;
  195. /* Skip jumbo subpackets that are duplicates. When we've had three or
  196. * more partially duplicate jumbo packets, we refuse to take any more
  197. * jumbos for this call.
  198. */
  199. if (!*_jumbo_bad) {
  200. call->nr_jumbo_bad++;
  201. *_jumbo_bad = true;
  202. }
  203. }
  204. /*
  205. * Process a DATA packet, adding the packet to the Rx ring.
  206. */
  207. static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
  208. u16 skew)
  209. {
  210. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  211. unsigned int offset = sp->offset;
  212. unsigned int ix;
  213. rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
  214. rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
  215. bool immediate_ack = false, jumbo_bad = false, queued;
  216. u16 len;
  217. u8 ack = 0, flags, annotation = 0;
  218. _enter("{%u,%u},{%u,%u}",
  219. call->rx_hard_ack, call->rx_top, skb->len, seq);
  220. _proto("Rx DATA %%%u { #%u f=%02x }",
  221. sp->hdr.serial, seq, sp->hdr.flags);
  222. if (call->state >= RXRPC_CALL_COMPLETE)
  223. return;
  224. /* Received data implicitly ACKs all of the request packets we sent
  225. * when we're acting as a client.
  226. */
  227. if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  228. call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
  229. !rxrpc_receiving_reply(call))
  230. return;
  231. call->ackr_prev_seq = seq;
  232. hard_ack = READ_ONCE(call->rx_hard_ack);
  233. if (after(seq, hard_ack + call->rx_winsize)) {
  234. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  235. ack_serial = serial;
  236. goto ack;
  237. }
  238. flags = sp->hdr.flags;
  239. if (flags & RXRPC_JUMBO_PACKET) {
  240. if (call->nr_jumbo_bad > 3) {
  241. ack = RXRPC_ACK_NOSPACE;
  242. ack_serial = serial;
  243. goto ack;
  244. }
  245. annotation = 1;
  246. }
  247. next_subpacket:
  248. queued = false;
  249. ix = seq & RXRPC_RXTX_BUFF_MASK;
  250. len = skb->len;
  251. if (flags & RXRPC_JUMBO_PACKET)
  252. len = RXRPC_JUMBO_DATALEN;
  253. if (flags & RXRPC_LAST_PACKET) {
  254. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  255. seq != call->rx_top)
  256. return rxrpc_proto_abort("LSN", call, seq);
  257. } else {
  258. if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  259. after_eq(seq, call->rx_top))
  260. return rxrpc_proto_abort("LSA", call, seq);
  261. }
  262. if (before_eq(seq, hard_ack)) {
  263. ack = RXRPC_ACK_DUPLICATE;
  264. ack_serial = serial;
  265. goto skip;
  266. }
  267. if (flags & RXRPC_REQUEST_ACK && !ack) {
  268. ack = RXRPC_ACK_REQUESTED;
  269. ack_serial = serial;
  270. }
  271. if (call->rxtx_buffer[ix]) {
  272. rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
  273. if (ack != RXRPC_ACK_DUPLICATE) {
  274. ack = RXRPC_ACK_DUPLICATE;
  275. ack_serial = serial;
  276. }
  277. immediate_ack = true;
  278. goto skip;
  279. }
  280. /* Queue the packet. We use a couple of memory barriers here as need
  281. * to make sure that rx_top is perceived to be set after the buffer
  282. * pointer and that the buffer pointer is set after the annotation and
  283. * the skb data.
  284. *
  285. * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
  286. * and also rxrpc_fill_out_ack().
  287. */
  288. rxrpc_get_skb(skb, rxrpc_skb_rx_got);
  289. call->rxtx_annotations[ix] = annotation;
  290. smp_wmb();
  291. call->rxtx_buffer[ix] = skb;
  292. if (after(seq, call->rx_top)) {
  293. smp_store_release(&call->rx_top, seq);
  294. } else if (before(seq, call->rx_top)) {
  295. /* Send an immediate ACK if we fill in a hole */
  296. if (!ack) {
  297. ack = RXRPC_ACK_DELAY;
  298. ack_serial = serial;
  299. }
  300. immediate_ack = true;
  301. }
  302. if (flags & RXRPC_LAST_PACKET) {
  303. set_bit(RXRPC_CALL_RX_LAST, &call->flags);
  304. trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
  305. } else {
  306. trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
  307. }
  308. queued = true;
  309. if (after_eq(seq, call->rx_expect_next)) {
  310. if (after(seq, call->rx_expect_next)) {
  311. _net("OOS %u > %u", seq, call->rx_expect_next);
  312. ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  313. ack_serial = serial;
  314. }
  315. call->rx_expect_next = seq + 1;
  316. }
  317. skip:
  318. offset += len;
  319. if (flags & RXRPC_JUMBO_PACKET) {
  320. if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  321. return rxrpc_proto_abort("XJF", call, seq);
  322. offset += sizeof(struct rxrpc_jumbo_header);
  323. seq++;
  324. serial++;
  325. annotation++;
  326. if (flags & RXRPC_JUMBO_PACKET)
  327. annotation |= RXRPC_RX_ANNO_JLAST;
  328. if (after(seq, hard_ack + call->rx_winsize)) {
  329. ack = RXRPC_ACK_EXCEEDS_WINDOW;
  330. ack_serial = serial;
  331. if (!jumbo_bad) {
  332. call->nr_jumbo_bad++;
  333. jumbo_bad = true;
  334. }
  335. goto ack;
  336. }
  337. _proto("Rx DATA Jumbo %%%u", serial);
  338. goto next_subpacket;
  339. }
  340. if (queued && flags & RXRPC_LAST_PACKET && !ack) {
  341. ack = RXRPC_ACK_DELAY;
  342. ack_serial = serial;
  343. }
  344. ack:
  345. if (ack)
  346. rxrpc_propose_ACK(call, ack, skew, ack_serial,
  347. immediate_ack, true,
  348. rxrpc_propose_ack_input_data);
  349. if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
  350. rxrpc_notify_socket(call);
  351. _leave(" [queued]");
  352. }
  353. /*
  354. * Process a requested ACK.
  355. */
  356. static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  357. ktime_t resp_time,
  358. rxrpc_serial_t orig_serial,
  359. rxrpc_serial_t ack_serial)
  360. {
  361. struct rxrpc_skb_priv *sp;
  362. struct sk_buff *skb;
  363. ktime_t sent_at;
  364. int ix;
  365. for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  366. skb = call->rxtx_buffer[ix];
  367. if (!skb)
  368. continue;
  369. sp = rxrpc_skb(skb);
  370. if (sp->hdr.serial != orig_serial)
  371. continue;
  372. smp_rmb();
  373. sent_at = skb->tstamp;
  374. goto found;
  375. }
  376. return;
  377. found:
  378. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  379. orig_serial, ack_serial, sent_at, resp_time);
  380. }
  381. /*
  382. * Process a ping response.
  383. */
  384. static void rxrpc_input_ping_response(struct rxrpc_call *call,
  385. ktime_t resp_time,
  386. rxrpc_serial_t orig_serial,
  387. rxrpc_serial_t ack_serial)
  388. {
  389. rxrpc_serial_t ping_serial;
  390. ktime_t ping_time;
  391. ping_time = call->ackr_ping_time;
  392. smp_rmb();
  393. ping_serial = call->ackr_ping;
  394. if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
  395. before(orig_serial, ping_serial))
  396. return;
  397. clear_bit(RXRPC_CALL_PINGING, &call->flags);
  398. if (after(orig_serial, ping_serial))
  399. return;
  400. rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  401. orig_serial, ack_serial, ping_time, resp_time);
  402. }
  403. /*
  404. * Process the extra information that may be appended to an ACK packet
  405. */
  406. static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  407. struct rxrpc_ackinfo *ackinfo)
  408. {
  409. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  410. struct rxrpc_peer *peer;
  411. unsigned int mtu;
  412. u32 rwind = ntohl(ackinfo->rwind);
  413. _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  414. sp->hdr.serial,
  415. ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
  416. rwind, ntohl(ackinfo->jumbo_max));
  417. if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  418. rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  419. call->tx_winsize = rwind;
  420. mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  421. peer = call->peer;
  422. if (mtu < peer->maxdata) {
  423. spin_lock_bh(&peer->lock);
  424. peer->maxdata = mtu;
  425. peer->mtu = mtu + peer->hdrsize;
  426. spin_unlock_bh(&peer->lock);
  427. _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  428. }
  429. }
  430. /*
  431. * Process individual soft ACKs.
  432. *
  433. * Each ACK in the array corresponds to one packet and can be either an ACK or
  434. * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
  435. * packets that lie beyond the end of the ACK list are scheduled for resend by
  436. * the timer on the basis that the peer might just not have processed them at
  437. * the time the ACK was sent.
  438. */
  439. static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
  440. rxrpc_seq_t seq, int nr_acks)
  441. {
  442. bool resend = false;
  443. int ix;
  444. u8 annotation, anno_type;
  445. for (; nr_acks > 0; nr_acks--, seq++) {
  446. ix = seq & RXRPC_RXTX_BUFF_MASK;
  447. annotation = call->rxtx_annotations[ix];
  448. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  449. annotation &= ~RXRPC_TX_ANNO_MASK;
  450. switch (*acks++) {
  451. case RXRPC_ACK_TYPE_ACK:
  452. if (anno_type == RXRPC_TX_ANNO_ACK)
  453. continue;
  454. call->rxtx_annotations[ix] =
  455. RXRPC_TX_ANNO_ACK | annotation;
  456. break;
  457. case RXRPC_ACK_TYPE_NACK:
  458. if (anno_type == RXRPC_TX_ANNO_NAK)
  459. continue;
  460. if (anno_type == RXRPC_TX_ANNO_RETRANS)
  461. continue;
  462. call->rxtx_annotations[ix] =
  463. RXRPC_TX_ANNO_NAK | annotation;
  464. resend = true;
  465. break;
  466. default:
  467. return rxrpc_proto_abort("SFT", call, 0);
  468. }
  469. }
  470. if (resend &&
  471. !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  472. rxrpc_queue_call(call);
  473. }
  474. /*
  475. * Process an ACK packet.
  476. *
  477. * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
  478. * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
  479. *
  480. * A hard-ACK means that a packet has been processed and may be discarded; a
  481. * soft-ACK means that the packet may be discarded and retransmission
  482. * requested. A phase is complete when all packets are hard-ACK'd.
  483. */
  484. static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
  485. u16 skew)
  486. {
  487. u8 ack_reason;
  488. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  489. union {
  490. struct rxrpc_ackpacket ack;
  491. struct rxrpc_ackinfo info;
  492. u8 acks[RXRPC_MAXACKS];
  493. } buf;
  494. rxrpc_serial_t acked_serial;
  495. rxrpc_seq_t first_soft_ack, hard_ack;
  496. int nr_acks, offset;
  497. _enter("");
  498. if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
  499. _debug("extraction failure");
  500. return rxrpc_proto_abort("XAK", call, 0);
  501. }
  502. sp->offset += sizeof(buf.ack);
  503. acked_serial = ntohl(buf.ack.serial);
  504. first_soft_ack = ntohl(buf.ack.firstPacket);
  505. hard_ack = first_soft_ack - 1;
  506. nr_acks = buf.ack.nAcks;
  507. ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  508. buf.ack.reason : RXRPC_ACK__INVALID);
  509. trace_rxrpc_rx_ack(call, first_soft_ack, ack_reason, nr_acks);
  510. _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
  511. sp->hdr.serial,
  512. ntohs(buf.ack.maxSkew),
  513. first_soft_ack,
  514. ntohl(buf.ack.previousPacket),
  515. acked_serial,
  516. rxrpc_ack_names[ack_reason],
  517. buf.ack.nAcks);
  518. if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  519. rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  520. sp->hdr.serial);
  521. if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  522. rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  523. sp->hdr.serial);
  524. if (buf.ack.reason == RXRPC_ACK_PING) {
  525. _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
  526. rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
  527. skew, sp->hdr.serial, true, true,
  528. rxrpc_propose_ack_respond_to_ping);
  529. } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
  530. rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
  531. skew, sp->hdr.serial, true, true,
  532. rxrpc_propose_ack_respond_to_ack);
  533. }
  534. offset = sp->offset + nr_acks + 3;
  535. if (skb->len >= offset + sizeof(buf.info)) {
  536. if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
  537. return rxrpc_proto_abort("XAI", call, 0);
  538. rxrpc_input_ackinfo(call, skb, &buf.info);
  539. }
  540. if (first_soft_ack == 0)
  541. return rxrpc_proto_abort("AK0", call, 0);
  542. /* Ignore ACKs unless we are or have just been transmitting. */
  543. switch (call->state) {
  544. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  545. case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  546. case RXRPC_CALL_SERVER_SEND_REPLY:
  547. case RXRPC_CALL_SERVER_AWAIT_ACK:
  548. break;
  549. default:
  550. return;
  551. }
  552. /* Discard any out-of-order or duplicate ACKs. */
  553. if (before_eq(sp->hdr.serial, call->acks_latest)) {
  554. _debug("discard ACK %d <= %d",
  555. sp->hdr.serial, call->acks_latest);
  556. return;
  557. }
  558. call->acks_latest = sp->hdr.serial;
  559. if (before(hard_ack, call->tx_hard_ack) ||
  560. after(hard_ack, call->tx_top))
  561. return rxrpc_proto_abort("AKW", call, 0);
  562. if (nr_acks > call->tx_top - hard_ack)
  563. return rxrpc_proto_abort("AKN", call, 0);
  564. if (after(hard_ack, call->tx_hard_ack))
  565. rxrpc_rotate_tx_window(call, hard_ack);
  566. if (nr_acks > 0) {
  567. if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
  568. return rxrpc_proto_abort("XSA", call, 0);
  569. rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
  570. }
  571. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  572. rxrpc_end_tx_phase(call, false, "ETA");
  573. return;
  574. }
  575. }
  576. /*
  577. * Process an ACKALL packet.
  578. */
  579. static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
  580. {
  581. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  582. _proto("Rx ACKALL %%%u", sp->hdr.serial);
  583. rxrpc_rotate_tx_window(call, call->tx_top);
  584. if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  585. rxrpc_end_tx_phase(call, false, "ETL");
  586. }
  587. /*
  588. * Process an ABORT packet.
  589. */
  590. static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  591. {
  592. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  593. __be32 wtmp;
  594. u32 abort_code = RX_CALL_DEAD;
  595. _enter("");
  596. if (skb->len >= 4 &&
  597. skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
  598. abort_code = ntohl(wtmp);
  599. _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
  600. if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  601. abort_code, ECONNABORTED))
  602. rxrpc_notify_socket(call);
  603. }
  604. /*
  605. * Process an incoming call packet.
  606. */
  607. static void rxrpc_input_call_packet(struct rxrpc_call *call,
  608. struct sk_buff *skb, u16 skew)
  609. {
  610. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  611. _enter("%p,%p", call, skb);
  612. switch (sp->hdr.type) {
  613. case RXRPC_PACKET_TYPE_DATA:
  614. rxrpc_input_data(call, skb, skew);
  615. break;
  616. case RXRPC_PACKET_TYPE_ACK:
  617. rxrpc_input_ack(call, skb, skew);
  618. break;
  619. case RXRPC_PACKET_TYPE_BUSY:
  620. _proto("Rx BUSY %%%u", sp->hdr.serial);
  621. /* Just ignore BUSY packets from the server; the retry and
  622. * lifespan timers will take care of business. BUSY packets
  623. * from the client don't make sense.
  624. */
  625. break;
  626. case RXRPC_PACKET_TYPE_ABORT:
  627. rxrpc_input_abort(call, skb);
  628. break;
  629. case RXRPC_PACKET_TYPE_ACKALL:
  630. rxrpc_input_ackall(call, skb);
  631. break;
  632. default:
  633. _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
  634. break;
  635. }
  636. _leave("");
  637. }
  638. /*
  639. * post connection-level events to the connection
  640. * - this includes challenges, responses, some aborts and call terminal packet
  641. * retransmission.
  642. */
  643. static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
  644. struct sk_buff *skb)
  645. {
  646. _enter("%p,%p", conn, skb);
  647. skb_queue_tail(&conn->rx_queue, skb);
  648. rxrpc_queue_conn(conn);
  649. }
  650. /*
  651. * post endpoint-level events to the local endpoint
  652. * - this includes debug and version messages
  653. */
  654. static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  655. struct sk_buff *skb)
  656. {
  657. _enter("%p,%p", local, skb);
  658. skb_queue_tail(&local->event_queue, skb);
  659. rxrpc_queue_local(local);
  660. }
  661. /*
  662. * put a packet up for transport-level abort
  663. */
  664. static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  665. {
  666. CHECK_SLAB_OKAY(&local->usage);
  667. skb_queue_tail(&local->reject_queue, skb);
  668. rxrpc_queue_local(local);
  669. }
  670. /*
  671. * Extract the wire header from a packet and translate the byte order.
  672. */
  673. static noinline
  674. int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  675. {
  676. struct rxrpc_wire_header whdr;
  677. /* dig out the RxRPC connection details */
  678. if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
  679. return -EBADMSG;
  680. memset(sp, 0, sizeof(*sp));
  681. sp->hdr.epoch = ntohl(whdr.epoch);
  682. sp->hdr.cid = ntohl(whdr.cid);
  683. sp->hdr.callNumber = ntohl(whdr.callNumber);
  684. sp->hdr.seq = ntohl(whdr.seq);
  685. sp->hdr.serial = ntohl(whdr.serial);
  686. sp->hdr.flags = whdr.flags;
  687. sp->hdr.type = whdr.type;
  688. sp->hdr.userStatus = whdr.userStatus;
  689. sp->hdr.securityIndex = whdr.securityIndex;
  690. sp->hdr._rsvd = ntohs(whdr._rsvd);
  691. sp->hdr.serviceId = ntohs(whdr.serviceId);
  692. sp->offset = sizeof(whdr);
  693. return 0;
  694. }
  695. /*
  696. * handle data received on the local endpoint
  697. * - may be called in interrupt context
  698. *
  699. * The socket is locked by the caller and this prevents the socket from being
  700. * shut down and the local endpoint from going away, thus sk_user_data will not
  701. * be cleared until this function returns.
  702. */
  703. void rxrpc_data_ready(struct sock *udp_sk)
  704. {
  705. struct rxrpc_connection *conn;
  706. struct rxrpc_channel *chan;
  707. struct rxrpc_call *call;
  708. struct rxrpc_skb_priv *sp;
  709. struct rxrpc_local *local = udp_sk->sk_user_data;
  710. struct sk_buff *skb;
  711. unsigned int channel;
  712. int ret, skew;
  713. _enter("%p", udp_sk);
  714. ASSERT(!irqs_disabled());
  715. skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
  716. if (!skb) {
  717. if (ret == -EAGAIN)
  718. return;
  719. _debug("UDP socket error %d", ret);
  720. return;
  721. }
  722. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  723. _net("recv skb %p", skb);
  724. /* we'll probably need to checksum it (didn't call sock_recvmsg) */
  725. if (skb_checksum_complete(skb)) {
  726. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  727. __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
  728. _leave(" [CSUM failed]");
  729. return;
  730. }
  731. __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
  732. /* The socket buffer we have is owned by UDP, with UDP's data all over
  733. * it, but we really want our own data there.
  734. */
  735. skb_orphan(skb);
  736. sp = rxrpc_skb(skb);
  737. /* dig out the RxRPC connection details */
  738. if (rxrpc_extract_header(sp, skb) < 0)
  739. goto bad_message;
  740. if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  741. static int lose;
  742. if ((lose++ & 7) == 7) {
  743. trace_rxrpc_rx_lose(sp);
  744. rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
  745. return;
  746. }
  747. }
  748. trace_rxrpc_rx_packet(sp);
  749. _net("Rx RxRPC %s ep=%x call=%x:%x",
  750. sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
  751. sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
  752. if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
  753. !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
  754. _proto("Rx Bad Packet Type %u", sp->hdr.type);
  755. goto bad_message;
  756. }
  757. switch (sp->hdr.type) {
  758. case RXRPC_PACKET_TYPE_VERSION:
  759. rxrpc_post_packet_to_local(local, skb);
  760. goto out;
  761. case RXRPC_PACKET_TYPE_BUSY:
  762. if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  763. goto discard;
  764. case RXRPC_PACKET_TYPE_DATA:
  765. if (sp->hdr.callNumber == 0)
  766. goto bad_message;
  767. if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
  768. !rxrpc_validate_jumbo(skb))
  769. goto bad_message;
  770. break;
  771. }
  772. rcu_read_lock();
  773. conn = rxrpc_find_connection_rcu(local, skb);
  774. if (conn) {
  775. if (sp->hdr.securityIndex != conn->security_ix)
  776. goto wrong_security;
  777. if (sp->hdr.callNumber == 0) {
  778. /* Connection-level packet */
  779. _debug("CONN %p {%d}", conn, conn->debug_id);
  780. rxrpc_post_packet_to_conn(conn, skb);
  781. goto out_unlock;
  782. }
  783. /* Note the serial number skew here */
  784. skew = (int)sp->hdr.serial - (int)conn->hi_serial;
  785. if (skew >= 0) {
  786. if (skew > 0)
  787. conn->hi_serial = sp->hdr.serial;
  788. } else {
  789. skew = -skew;
  790. skew = min(skew, 65535);
  791. }
  792. /* Call-bound packets are routed by connection channel. */
  793. channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  794. chan = &conn->channels[channel];
  795. /* Ignore really old calls */
  796. if (sp->hdr.callNumber < chan->last_call)
  797. goto discard_unlock;
  798. if (sp->hdr.callNumber == chan->last_call) {
  799. /* For the previous service call, if completed successfully, we
  800. * discard all further packets.
  801. */
  802. if (rxrpc_conn_is_service(conn) &&
  803. (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
  804. sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
  805. goto discard_unlock;
  806. /* But otherwise we need to retransmit the final packet from
  807. * data cached in the connection record.
  808. */
  809. rxrpc_post_packet_to_conn(conn, skb);
  810. goto out_unlock;
  811. }
  812. call = rcu_dereference(chan->call);
  813. } else {
  814. skew = 0;
  815. call = NULL;
  816. }
  817. if (!call || atomic_read(&call->usage) == 0) {
  818. if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
  819. sp->hdr.callNumber == 0 ||
  820. sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  821. goto bad_message_unlock;
  822. if (sp->hdr.seq != 1)
  823. goto discard_unlock;
  824. call = rxrpc_new_incoming_call(local, conn, skb);
  825. if (!call) {
  826. rcu_read_unlock();
  827. goto reject_packet;
  828. }
  829. rxrpc_send_ping(call, skb, skew);
  830. }
  831. rxrpc_input_call_packet(call, skb, skew);
  832. goto discard_unlock;
  833. discard_unlock:
  834. rcu_read_unlock();
  835. discard:
  836. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  837. out:
  838. trace_rxrpc_rx_done(0, 0);
  839. return;
  840. out_unlock:
  841. rcu_read_unlock();
  842. goto out;
  843. wrong_security:
  844. rcu_read_unlock();
  845. trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  846. RXKADINCONSISTENCY, EBADMSG);
  847. skb->priority = RXKADINCONSISTENCY;
  848. goto post_abort;
  849. bad_message_unlock:
  850. rcu_read_unlock();
  851. bad_message:
  852. trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  853. RX_PROTOCOL_ERROR, EBADMSG);
  854. skb->priority = RX_PROTOCOL_ERROR;
  855. post_abort:
  856. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  857. reject_packet:
  858. trace_rxrpc_rx_done(skb->mark, skb->priority);
  859. rxrpc_reject_packet(local, skb);
  860. _leave(" [badmsg]");
  861. }