input.c 22 KB

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