input.c 26 KB

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