input.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/dccp/input.c
  4. *
  5. * An implementation of the DCCP protocol
  6. * Arnaldo Carvalho de Melo <[email protected]>
  7. */
  8. #include <linux/dccp.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/slab.h>
  11. #include <net/sock.h>
  12. #include "ackvec.h"
  13. #include "ccid.h"
  14. #include "dccp.h"
  15. /* rate-limit for syncs in reply to sequence-invalid packets; RFC 4340, 7.5.4 */
  16. int sysctl_dccp_sync_ratelimit __read_mostly = HZ / 8;
  17. static void dccp_enqueue_skb(struct sock *sk, struct sk_buff *skb)
  18. {
  19. __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
  20. __skb_queue_tail(&sk->sk_receive_queue, skb);
  21. skb_set_owner_r(skb, sk);
  22. sk->sk_data_ready(sk);
  23. }
  24. static void dccp_fin(struct sock *sk, struct sk_buff *skb)
  25. {
  26. /*
  27. * On receiving Close/CloseReq, both RD/WR shutdown are performed.
  28. * RFC 4340, 8.3 says that we MAY send further Data/DataAcks after
  29. * receiving the closing segment, but there is no guarantee that such
  30. * data will be processed at all.
  31. */
  32. sk->sk_shutdown = SHUTDOWN_MASK;
  33. sock_set_flag(sk, SOCK_DONE);
  34. dccp_enqueue_skb(sk, skb);
  35. }
  36. static int dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
  37. {
  38. int queued = 0;
  39. switch (sk->sk_state) {
  40. /*
  41. * We ignore Close when received in one of the following states:
  42. * - CLOSED (may be a late or duplicate packet)
  43. * - PASSIVE_CLOSEREQ (the peer has sent a CloseReq earlier)
  44. * - RESPOND (already handled by dccp_check_req)
  45. */
  46. case DCCP_CLOSING:
  47. /*
  48. * Simultaneous-close: receiving a Close after sending one. This
  49. * can happen if both client and server perform active-close and
  50. * will result in an endless ping-pong of crossing and retrans-
  51. * mitted Close packets, which only terminates when one of the
  52. * nodes times out (min. 64 seconds). Quicker convergence can be
  53. * achieved when one of the nodes acts as tie-breaker.
  54. * This is ok as both ends are done with data transfer and each
  55. * end is just waiting for the other to acknowledge termination.
  56. */
  57. if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT)
  58. break;
  59. fallthrough;
  60. case DCCP_REQUESTING:
  61. case DCCP_ACTIVE_CLOSEREQ:
  62. dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
  63. dccp_done(sk);
  64. break;
  65. case DCCP_OPEN:
  66. case DCCP_PARTOPEN:
  67. /* Give waiting application a chance to read pending data */
  68. queued = 1;
  69. dccp_fin(sk, skb);
  70. dccp_set_state(sk, DCCP_PASSIVE_CLOSE);
  71. fallthrough;
  72. case DCCP_PASSIVE_CLOSE:
  73. /*
  74. * Retransmitted Close: we have already enqueued the first one.
  75. */
  76. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
  77. }
  78. return queued;
  79. }
  80. static int dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
  81. {
  82. int queued = 0;
  83. /*
  84. * Step 7: Check for unexpected packet types
  85. * If (S.is_server and P.type == CloseReq)
  86. * Send Sync packet acknowledging P.seqno
  87. * Drop packet and return
  88. */
  89. if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
  90. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
  91. return queued;
  92. }
  93. /* Step 13: process relevant Client states < CLOSEREQ */
  94. switch (sk->sk_state) {
  95. case DCCP_REQUESTING:
  96. dccp_send_close(sk, 0);
  97. dccp_set_state(sk, DCCP_CLOSING);
  98. break;
  99. case DCCP_OPEN:
  100. case DCCP_PARTOPEN:
  101. /* Give waiting application a chance to read pending data */
  102. queued = 1;
  103. dccp_fin(sk, skb);
  104. dccp_set_state(sk, DCCP_PASSIVE_CLOSEREQ);
  105. fallthrough;
  106. case DCCP_PASSIVE_CLOSEREQ:
  107. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
  108. }
  109. return queued;
  110. }
  111. static u16 dccp_reset_code_convert(const u8 code)
  112. {
  113. static const u16 error_code[] = {
  114. [DCCP_RESET_CODE_CLOSED] = 0, /* normal termination */
  115. [DCCP_RESET_CODE_UNSPECIFIED] = 0, /* nothing known */
  116. [DCCP_RESET_CODE_ABORTED] = ECONNRESET,
  117. [DCCP_RESET_CODE_NO_CONNECTION] = ECONNREFUSED,
  118. [DCCP_RESET_CODE_CONNECTION_REFUSED] = ECONNREFUSED,
  119. [DCCP_RESET_CODE_TOO_BUSY] = EUSERS,
  120. [DCCP_RESET_CODE_AGGRESSION_PENALTY] = EDQUOT,
  121. [DCCP_RESET_CODE_PACKET_ERROR] = ENOMSG,
  122. [DCCP_RESET_CODE_BAD_INIT_COOKIE] = EBADR,
  123. [DCCP_RESET_CODE_BAD_SERVICE_CODE] = EBADRQC,
  124. [DCCP_RESET_CODE_OPTION_ERROR] = EILSEQ,
  125. [DCCP_RESET_CODE_MANDATORY_ERROR] = EOPNOTSUPP,
  126. };
  127. return code >= DCCP_MAX_RESET_CODES ? 0 : error_code[code];
  128. }
  129. static void dccp_rcv_reset(struct sock *sk, struct sk_buff *skb)
  130. {
  131. u16 err = dccp_reset_code_convert(dccp_hdr_reset(skb)->dccph_reset_code);
  132. sk->sk_err = err;
  133. /* Queue the equivalent of TCP fin so that dccp_recvmsg exits the loop */
  134. dccp_fin(sk, skb);
  135. if (err && !sock_flag(sk, SOCK_DEAD))
  136. sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
  137. dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
  138. }
  139. static void dccp_handle_ackvec_processing(struct sock *sk, struct sk_buff *skb)
  140. {
  141. struct dccp_ackvec *av = dccp_sk(sk)->dccps_hc_rx_ackvec;
  142. if (av == NULL)
  143. return;
  144. if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  145. dccp_ackvec_clear_state(av, DCCP_SKB_CB(skb)->dccpd_ack_seq);
  146. dccp_ackvec_input(av, skb);
  147. }
  148. static void dccp_deliver_input_to_ccids(struct sock *sk, struct sk_buff *skb)
  149. {
  150. const struct dccp_sock *dp = dccp_sk(sk);
  151. /* Don't deliver to RX CCID when node has shut down read end. */
  152. if (!(sk->sk_shutdown & RCV_SHUTDOWN))
  153. ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
  154. /*
  155. * Until the TX queue has been drained, we can not honour SHUT_WR, since
  156. * we need received feedback as input to adjust congestion control.
  157. */
  158. if (sk->sk_write_queue.qlen > 0 || !(sk->sk_shutdown & SEND_SHUTDOWN))
  159. ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
  160. }
  161. static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
  162. {
  163. const struct dccp_hdr *dh = dccp_hdr(skb);
  164. struct dccp_sock *dp = dccp_sk(sk);
  165. u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
  166. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  167. /*
  168. * Step 5: Prepare sequence numbers for Sync
  169. * If P.type == Sync or P.type == SyncAck,
  170. * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
  171. * / * P is valid, so update sequence number variables
  172. * accordingly. After this update, P will pass the tests
  173. * in Step 6. A SyncAck is generated if necessary in
  174. * Step 15 * /
  175. * Update S.GSR, S.SWL, S.SWH
  176. * Otherwise,
  177. * Drop packet and return
  178. */
  179. if (dh->dccph_type == DCCP_PKT_SYNC ||
  180. dh->dccph_type == DCCP_PKT_SYNCACK) {
  181. if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
  182. dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
  183. dccp_update_gsr(sk, seqno);
  184. else
  185. return -1;
  186. }
  187. /*
  188. * Step 6: Check sequence numbers
  189. * Let LSWL = S.SWL and LAWL = S.AWL
  190. * If P.type == CloseReq or P.type == Close or P.type == Reset,
  191. * LSWL := S.GSR + 1, LAWL := S.GAR
  192. * If LSWL <= P.seqno <= S.SWH
  193. * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
  194. * Update S.GSR, S.SWL, S.SWH
  195. * If P.type != Sync,
  196. * Update S.GAR
  197. */
  198. lswl = dp->dccps_swl;
  199. lawl = dp->dccps_awl;
  200. if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
  201. dh->dccph_type == DCCP_PKT_CLOSE ||
  202. dh->dccph_type == DCCP_PKT_RESET) {
  203. lswl = ADD48(dp->dccps_gsr, 1);
  204. lawl = dp->dccps_gar;
  205. }
  206. if (between48(seqno, lswl, dp->dccps_swh) &&
  207. (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
  208. between48(ackno, lawl, dp->dccps_awh))) {
  209. dccp_update_gsr(sk, seqno);
  210. if (dh->dccph_type != DCCP_PKT_SYNC &&
  211. ackno != DCCP_PKT_WITHOUT_ACK_SEQ &&
  212. after48(ackno, dp->dccps_gar))
  213. dp->dccps_gar = ackno;
  214. } else {
  215. unsigned long now = jiffies;
  216. /*
  217. * Step 6: Check sequence numbers
  218. * Otherwise,
  219. * If P.type == Reset,
  220. * Send Sync packet acknowledging S.GSR
  221. * Otherwise,
  222. * Send Sync packet acknowledging P.seqno
  223. * Drop packet and return
  224. *
  225. * These Syncs are rate-limited as per RFC 4340, 7.5.4:
  226. * at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
  227. */
  228. if (time_before(now, (dp->dccps_rate_last +
  229. sysctl_dccp_sync_ratelimit)))
  230. return -1;
  231. DCCP_WARN("Step 6 failed for %s packet, "
  232. "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
  233. "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
  234. "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
  235. (unsigned long long) lswl, (unsigned long long) seqno,
  236. (unsigned long long) dp->dccps_swh,
  237. (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
  238. : "exists",
  239. (unsigned long long) lawl, (unsigned long long) ackno,
  240. (unsigned long long) dp->dccps_awh);
  241. dp->dccps_rate_last = now;
  242. if (dh->dccph_type == DCCP_PKT_RESET)
  243. seqno = dp->dccps_gsr;
  244. dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  250. const struct dccp_hdr *dh, const unsigned int len)
  251. {
  252. struct dccp_sock *dp = dccp_sk(sk);
  253. switch (dccp_hdr(skb)->dccph_type) {
  254. case DCCP_PKT_DATAACK:
  255. case DCCP_PKT_DATA:
  256. /*
  257. * FIXME: schedule DATA_DROPPED (RFC 4340, 11.7.2) if and when
  258. * - sk_shutdown == RCV_SHUTDOWN, use Code 1, "Not Listening"
  259. * - sk_receive_queue is full, use Code 2, "Receive Buffer"
  260. */
  261. dccp_enqueue_skb(sk, skb);
  262. return 0;
  263. case DCCP_PKT_ACK:
  264. goto discard;
  265. case DCCP_PKT_RESET:
  266. /*
  267. * Step 9: Process Reset
  268. * If P.type == Reset,
  269. * Tear down connection
  270. * S.state := TIMEWAIT
  271. * Set TIMEWAIT timer
  272. * Drop packet and return
  273. */
  274. dccp_rcv_reset(sk, skb);
  275. return 0;
  276. case DCCP_PKT_CLOSEREQ:
  277. if (dccp_rcv_closereq(sk, skb))
  278. return 0;
  279. goto discard;
  280. case DCCP_PKT_CLOSE:
  281. if (dccp_rcv_close(sk, skb))
  282. return 0;
  283. goto discard;
  284. case DCCP_PKT_REQUEST:
  285. /* Step 7
  286. * or (S.is_server and P.type == Response)
  287. * or (S.is_client and P.type == Request)
  288. * or (S.state >= OPEN and P.type == Request
  289. * and P.seqno >= S.OSR)
  290. * or (S.state >= OPEN and P.type == Response
  291. * and P.seqno >= S.OSR)
  292. * or (S.state == RESPOND and P.type == Data),
  293. * Send Sync packet acknowledging P.seqno
  294. * Drop packet and return
  295. */
  296. if (dp->dccps_role != DCCP_ROLE_LISTEN)
  297. goto send_sync;
  298. goto check_seq;
  299. case DCCP_PKT_RESPONSE:
  300. if (dp->dccps_role != DCCP_ROLE_CLIENT)
  301. goto send_sync;
  302. check_seq:
  303. if (dccp_delta_seqno(dp->dccps_osr,
  304. DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
  305. send_sync:
  306. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  307. DCCP_PKT_SYNC);
  308. }
  309. break;
  310. case DCCP_PKT_SYNC:
  311. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  312. DCCP_PKT_SYNCACK);
  313. /*
  314. * From RFC 4340, sec. 5.7
  315. *
  316. * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
  317. * MAY have non-zero-length application data areas, whose
  318. * contents receivers MUST ignore.
  319. */
  320. goto discard;
  321. }
  322. DCCP_INC_STATS(DCCP_MIB_INERRS);
  323. discard:
  324. __kfree_skb(skb);
  325. return 0;
  326. }
  327. int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  328. const struct dccp_hdr *dh, const unsigned int len)
  329. {
  330. if (dccp_check_seqno(sk, skb))
  331. goto discard;
  332. if (dccp_parse_options(sk, NULL, skb))
  333. return 1;
  334. dccp_handle_ackvec_processing(sk, skb);
  335. dccp_deliver_input_to_ccids(sk, skb);
  336. return __dccp_rcv_established(sk, skb, dh, len);
  337. discard:
  338. __kfree_skb(skb);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(dccp_rcv_established);
  342. static int dccp_rcv_request_sent_state_process(struct sock *sk,
  343. struct sk_buff *skb,
  344. const struct dccp_hdr *dh,
  345. const unsigned int len)
  346. {
  347. /*
  348. * Step 4: Prepare sequence numbers in REQUEST
  349. * If S.state == REQUEST,
  350. * If (P.type == Response or P.type == Reset)
  351. * and S.AWL <= P.ackno <= S.AWH,
  352. * / * Set sequence number variables corresponding to the
  353. * other endpoint, so P will pass the tests in Step 6 * /
  354. * Set S.GSR, S.ISR, S.SWL, S.SWH
  355. * / * Response processing continues in Step 10; Reset
  356. * processing continues in Step 9 * /
  357. */
  358. if (dh->dccph_type == DCCP_PKT_RESPONSE) {
  359. const struct inet_connection_sock *icsk = inet_csk(sk);
  360. struct dccp_sock *dp = dccp_sk(sk);
  361. long tstamp = dccp_timestamp();
  362. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  363. dp->dccps_awl, dp->dccps_awh)) {
  364. dccp_pr_debug("invalid ackno: S.AWL=%llu, "
  365. "P.ackno=%llu, S.AWH=%llu\n",
  366. (unsigned long long)dp->dccps_awl,
  367. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  368. (unsigned long long)dp->dccps_awh);
  369. goto out_invalid_packet;
  370. }
  371. /*
  372. * If option processing (Step 8) failed, return 1 here so that
  373. * dccp_v4_do_rcv() sends a Reset. The Reset code depends on
  374. * the option type and is set in dccp_parse_options().
  375. */
  376. if (dccp_parse_options(sk, NULL, skb))
  377. return 1;
  378. /* Obtain usec RTT sample from SYN exchange (used by TFRC). */
  379. if (likely(dp->dccps_options_received.dccpor_timestamp_echo))
  380. dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * (tstamp -
  381. dp->dccps_options_received.dccpor_timestamp_echo));
  382. /* Stop the REQUEST timer */
  383. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  384. WARN_ON(sk->sk_send_head == NULL);
  385. kfree_skb(sk->sk_send_head);
  386. sk->sk_send_head = NULL;
  387. /*
  388. * Set ISR, GSR from packet. ISS was set in dccp_v{4,6}_connect
  389. * and GSS in dccp_transmit_skb(). Setting AWL/AWH and SWL/SWH
  390. * is done as part of activating the feature values below, since
  391. * these settings depend on the local/remote Sequence Window
  392. * features, which were undefined or not confirmed until now.
  393. */
  394. dp->dccps_gsr = dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
  395. dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  396. /*
  397. * Step 10: Process REQUEST state (second part)
  398. * If S.state == REQUEST,
  399. * / * If we get here, P is a valid Response from the
  400. * server (see Step 4), and we should move to
  401. * PARTOPEN state. PARTOPEN means send an Ack,
  402. * don't send Data packets, retransmit Acks
  403. * periodically, and always include any Init Cookie
  404. * from the Response * /
  405. * S.state := PARTOPEN
  406. * Set PARTOPEN timer
  407. * Continue with S.state == PARTOPEN
  408. * / * Step 12 will send the Ack completing the
  409. * three-way handshake * /
  410. */
  411. dccp_set_state(sk, DCCP_PARTOPEN);
  412. /*
  413. * If feature negotiation was successful, activate features now;
  414. * an activation failure means that this host could not activate
  415. * one ore more features (e.g. insufficient memory), which would
  416. * leave at least one feature in an undefined state.
  417. */
  418. if (dccp_feat_activate_values(sk, &dp->dccps_featneg))
  419. goto unable_to_proceed;
  420. /* Make sure socket is routed, for correct metrics. */
  421. icsk->icsk_af_ops->rebuild_header(sk);
  422. if (!sock_flag(sk, SOCK_DEAD)) {
  423. sk->sk_state_change(sk);
  424. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
  425. }
  426. if (sk->sk_write_pending || inet_csk_in_pingpong_mode(sk) ||
  427. icsk->icsk_accept_queue.rskq_defer_accept) {
  428. /* Save one ACK. Data will be ready after
  429. * several ticks, if write_pending is set.
  430. *
  431. * It may be deleted, but with this feature tcpdumps
  432. * look so _wonderfully_ clever, that I was not able
  433. * to stand against the temptation 8) --ANK
  434. */
  435. /*
  436. * OK, in DCCP we can as well do a similar trick, its
  437. * even in the draft, but there is no need for us to
  438. * schedule an ack here, as dccp_sendmsg does this for
  439. * us, also stated in the draft. -acme
  440. */
  441. __kfree_skb(skb);
  442. return 0;
  443. }
  444. dccp_send_ack(sk);
  445. return -1;
  446. }
  447. out_invalid_packet:
  448. /* dccp_v4_do_rcv will send a reset */
  449. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  450. return 1;
  451. unable_to_proceed:
  452. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_ABORTED;
  453. /*
  454. * We mark this socket as no longer usable, so that the loop in
  455. * dccp_sendmsg() terminates and the application gets notified.
  456. */
  457. dccp_set_state(sk, DCCP_CLOSED);
  458. sk->sk_err = ECOMM;
  459. return 1;
  460. }
  461. static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
  462. struct sk_buff *skb,
  463. const struct dccp_hdr *dh,
  464. const unsigned int len)
  465. {
  466. struct dccp_sock *dp = dccp_sk(sk);
  467. u32 sample = dp->dccps_options_received.dccpor_timestamp_echo;
  468. int queued = 0;
  469. switch (dh->dccph_type) {
  470. case DCCP_PKT_RESET:
  471. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  472. break;
  473. case DCCP_PKT_DATA:
  474. if (sk->sk_state == DCCP_RESPOND)
  475. break;
  476. fallthrough;
  477. case DCCP_PKT_DATAACK:
  478. case DCCP_PKT_ACK:
  479. /*
  480. * FIXME: we should be resetting the PARTOPEN (DELACK) timer
  481. * here but only if we haven't used the DELACK timer for
  482. * something else, like sending a delayed ack for a TIMESTAMP
  483. * echo, etc, for now were not clearing it, sending an extra
  484. * ACK when there is nothing else to do in DELACK is not a big
  485. * deal after all.
  486. */
  487. /* Stop the PARTOPEN timer */
  488. if (sk->sk_state == DCCP_PARTOPEN)
  489. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  490. /* Obtain usec RTT sample from SYN exchange (used by TFRC). */
  491. if (likely(sample)) {
  492. long delta = dccp_timestamp() - sample;
  493. dp->dccps_syn_rtt = dccp_sample_rtt(sk, 10 * delta);
  494. }
  495. dp->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
  496. dccp_set_state(sk, DCCP_OPEN);
  497. if (dh->dccph_type == DCCP_PKT_DATAACK ||
  498. dh->dccph_type == DCCP_PKT_DATA) {
  499. __dccp_rcv_established(sk, skb, dh, len);
  500. queued = 1; /* packet was queued
  501. (by __dccp_rcv_established) */
  502. }
  503. break;
  504. }
  505. return queued;
  506. }
  507. int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  508. struct dccp_hdr *dh, unsigned int len)
  509. {
  510. struct dccp_sock *dp = dccp_sk(sk);
  511. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  512. const int old_state = sk->sk_state;
  513. bool acceptable;
  514. int queued = 0;
  515. /*
  516. * Step 3: Process LISTEN state
  517. *
  518. * If S.state == LISTEN,
  519. * If P.type == Request or P contains a valid Init Cookie option,
  520. * (* Must scan the packet's options to check for Init
  521. * Cookies. Only Init Cookies are processed here,
  522. * however; other options are processed in Step 8. This
  523. * scan need only be performed if the endpoint uses Init
  524. * Cookies *)
  525. * (* Generate a new socket and switch to that socket *)
  526. * Set S := new socket for this port pair
  527. * S.state = RESPOND
  528. * Choose S.ISS (initial seqno) or set from Init Cookies
  529. * Initialize S.GAR := S.ISS
  530. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
  531. * Cookies Continue with S.state == RESPOND
  532. * (* A Response packet will be generated in Step 11 *)
  533. * Otherwise,
  534. * Generate Reset(No Connection) unless P.type == Reset
  535. * Drop packet and return
  536. */
  537. if (sk->sk_state == DCCP_LISTEN) {
  538. if (dh->dccph_type == DCCP_PKT_REQUEST) {
  539. /* It is possible that we process SYN packets from backlog,
  540. * so we need to make sure to disable BH and RCU right there.
  541. */
  542. rcu_read_lock();
  543. local_bh_disable();
  544. acceptable = inet_csk(sk)->icsk_af_ops->conn_request(sk, skb) >= 0;
  545. local_bh_enable();
  546. rcu_read_unlock();
  547. if (!acceptable)
  548. return 1;
  549. consume_skb(skb);
  550. return 0;
  551. }
  552. if (dh->dccph_type == DCCP_PKT_RESET)
  553. goto discard;
  554. /* Caller (dccp_v4_do_rcv) will send Reset */
  555. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  556. return 1;
  557. } else if (sk->sk_state == DCCP_CLOSED) {
  558. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  559. return 1;
  560. }
  561. /* Step 6: Check sequence numbers (omitted in LISTEN/REQUEST state) */
  562. if (sk->sk_state != DCCP_REQUESTING && dccp_check_seqno(sk, skb))
  563. goto discard;
  564. /*
  565. * Step 7: Check for unexpected packet types
  566. * If (S.is_server and P.type == Response)
  567. * or (S.is_client and P.type == Request)
  568. * or (S.state == RESPOND and P.type == Data),
  569. * Send Sync packet acknowledging P.seqno
  570. * Drop packet and return
  571. */
  572. if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
  573. dh->dccph_type == DCCP_PKT_RESPONSE) ||
  574. (dp->dccps_role == DCCP_ROLE_CLIENT &&
  575. dh->dccph_type == DCCP_PKT_REQUEST) ||
  576. (sk->sk_state == DCCP_RESPOND && dh->dccph_type == DCCP_PKT_DATA)) {
  577. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
  578. goto discard;
  579. }
  580. /* Step 8: Process options */
  581. if (dccp_parse_options(sk, NULL, skb))
  582. return 1;
  583. /*
  584. * Step 9: Process Reset
  585. * If P.type == Reset,
  586. * Tear down connection
  587. * S.state := TIMEWAIT
  588. * Set TIMEWAIT timer
  589. * Drop packet and return
  590. */
  591. if (dh->dccph_type == DCCP_PKT_RESET) {
  592. dccp_rcv_reset(sk, skb);
  593. return 0;
  594. } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) { /* Step 13 */
  595. if (dccp_rcv_closereq(sk, skb))
  596. return 0;
  597. goto discard;
  598. } else if (dh->dccph_type == DCCP_PKT_CLOSE) { /* Step 14 */
  599. if (dccp_rcv_close(sk, skb))
  600. return 0;
  601. goto discard;
  602. }
  603. switch (sk->sk_state) {
  604. case DCCP_REQUESTING:
  605. queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
  606. if (queued >= 0)
  607. return queued;
  608. __kfree_skb(skb);
  609. return 0;
  610. case DCCP_PARTOPEN:
  611. /* Step 8: if using Ack Vectors, mark packet acknowledgeable */
  612. dccp_handle_ackvec_processing(sk, skb);
  613. dccp_deliver_input_to_ccids(sk, skb);
  614. fallthrough;
  615. case DCCP_RESPOND:
  616. queued = dccp_rcv_respond_partopen_state_process(sk, skb,
  617. dh, len);
  618. break;
  619. }
  620. if (dh->dccph_type == DCCP_PKT_ACK ||
  621. dh->dccph_type == DCCP_PKT_DATAACK) {
  622. switch (old_state) {
  623. case DCCP_PARTOPEN:
  624. sk->sk_state_change(sk);
  625. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
  626. break;
  627. }
  628. } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
  629. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
  630. goto discard;
  631. }
  632. if (!queued) {
  633. discard:
  634. __kfree_skb(skb);
  635. }
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
  639. /**
  640. * dccp_sample_rtt - Validate and finalise computation of RTT sample
  641. * @sk: socket structure
  642. * @delta: number of microseconds between packet and acknowledgment
  643. *
  644. * The routine is kept generic to work in different contexts. It should be
  645. * called immediately when the ACK used for the RTT sample arrives.
  646. */
  647. u32 dccp_sample_rtt(struct sock *sk, long delta)
  648. {
  649. /* dccpor_elapsed_time is either zeroed out or set and > 0 */
  650. delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
  651. if (unlikely(delta <= 0)) {
  652. DCCP_WARN("unusable RTT sample %ld, using min\n", delta);
  653. return DCCP_SANE_RTT_MIN;
  654. }
  655. if (unlikely(delta > DCCP_SANE_RTT_MAX)) {
  656. DCCP_WARN("RTT sample %ld too large, using max\n", delta);
  657. return DCCP_SANE_RTT_MAX;
  658. }
  659. return delta;
  660. }