input.c 21 KB

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