input.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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_handle_ackvec_processing(struct sock *sk, struct sk_buff *skb)
  143. {
  144. struct dccp_ackvec *av = dccp_sk(sk)->dccps_hc_rx_ackvec;
  145. if (av == NULL)
  146. return;
  147. if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  148. dccp_ackvec_clear_state(av, DCCP_SKB_CB(skb)->dccpd_ack_seq);
  149. dccp_ackvec_input(av, skb);
  150. }
  151. static void dccp_deliver_input_to_ccids(struct sock *sk, struct sk_buff *skb)
  152. {
  153. const struct dccp_sock *dp = dccp_sk(sk);
  154. /* Don't deliver to RX CCID when node has shut down read end. */
  155. if (!(sk->sk_shutdown & RCV_SHUTDOWN))
  156. ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
  157. /*
  158. * Until the TX queue has been drained, we can not honour SHUT_WR, since
  159. * we need received feedback as input to adjust congestion control.
  160. */
  161. if (sk->sk_write_queue.qlen > 0 || !(sk->sk_shutdown & SEND_SHUTDOWN))
  162. ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
  163. }
  164. static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
  165. {
  166. const struct dccp_hdr *dh = dccp_hdr(skb);
  167. struct dccp_sock *dp = dccp_sk(sk);
  168. u64 lswl, lawl, seqno = DCCP_SKB_CB(skb)->dccpd_seq,
  169. ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  170. /*
  171. * Step 5: Prepare sequence numbers for Sync
  172. * If P.type == Sync or P.type == SyncAck,
  173. * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
  174. * / * P is valid, so update sequence number variables
  175. * accordingly. After this update, P will pass the tests
  176. * in Step 6. A SyncAck is generated if necessary in
  177. * Step 15 * /
  178. * Update S.GSR, S.SWL, S.SWH
  179. * Otherwise,
  180. * Drop packet and return
  181. */
  182. if (dh->dccph_type == DCCP_PKT_SYNC ||
  183. dh->dccph_type == DCCP_PKT_SYNCACK) {
  184. if (between48(ackno, dp->dccps_awl, dp->dccps_awh) &&
  185. dccp_delta_seqno(dp->dccps_swl, seqno) >= 0)
  186. dccp_update_gsr(sk, seqno);
  187. else
  188. return -1;
  189. }
  190. /*
  191. * Step 6: Check sequence numbers
  192. * Let LSWL = S.SWL and LAWL = S.AWL
  193. * If P.type == CloseReq or P.type == Close or P.type == Reset,
  194. * LSWL := S.GSR + 1, LAWL := S.GAR
  195. * If LSWL <= P.seqno <= S.SWH
  196. * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
  197. * Update S.GSR, S.SWL, S.SWH
  198. * If P.type != Sync,
  199. * Update S.GAR
  200. */
  201. lswl = dp->dccps_swl;
  202. lawl = dp->dccps_awl;
  203. if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
  204. dh->dccph_type == DCCP_PKT_CLOSE ||
  205. dh->dccph_type == DCCP_PKT_RESET) {
  206. lswl = ADD48(dp->dccps_gsr, 1);
  207. lawl = dp->dccps_gar;
  208. }
  209. if (between48(seqno, lswl, dp->dccps_swh) &&
  210. (ackno == DCCP_PKT_WITHOUT_ACK_SEQ ||
  211. between48(ackno, lawl, dp->dccps_awh))) {
  212. dccp_update_gsr(sk, seqno);
  213. if (dh->dccph_type != DCCP_PKT_SYNC &&
  214. (ackno != DCCP_PKT_WITHOUT_ACK_SEQ))
  215. dp->dccps_gar = ackno;
  216. } else {
  217. unsigned long now = jiffies;
  218. /*
  219. * Step 6: Check sequence numbers
  220. * Otherwise,
  221. * If P.type == Reset,
  222. * Send Sync packet acknowledging S.GSR
  223. * Otherwise,
  224. * Send Sync packet acknowledging P.seqno
  225. * Drop packet and return
  226. *
  227. * These Syncs are rate-limited as per RFC 4340, 7.5.4:
  228. * at most 1 / (dccp_sync_rate_limit * HZ) Syncs per second.
  229. */
  230. if (time_before(now, (dp->dccps_rate_last +
  231. sysctl_dccp_sync_ratelimit)))
  232. return 0;
  233. DCCP_WARN("DCCP: Step 6 failed for %s packet, "
  234. "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
  235. "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
  236. "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
  237. (unsigned long long) lswl, (unsigned long long) seqno,
  238. (unsigned long long) dp->dccps_swh,
  239. (ackno == DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist"
  240. : "exists",
  241. (unsigned long long) lawl, (unsigned long long) ackno,
  242. (unsigned long long) dp->dccps_awh);
  243. dp->dccps_rate_last = now;
  244. if (dh->dccph_type == DCCP_PKT_RESET)
  245. seqno = dp->dccps_gsr;
  246. dccp_send_sync(sk, seqno, DCCP_PKT_SYNC);
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  252. const struct dccp_hdr *dh, const unsigned len)
  253. {
  254. struct dccp_sock *dp = dccp_sk(sk);
  255. switch (dccp_hdr(skb)->dccph_type) {
  256. case DCCP_PKT_DATAACK:
  257. case DCCP_PKT_DATA:
  258. /*
  259. * FIXME: schedule DATA_DROPPED (RFC 4340, 11.7.2) if and when
  260. * - sk_shutdown == RCV_SHUTDOWN, use Code 1, "Not Listening"
  261. * - sk_receive_queue is full, use Code 2, "Receive Buffer"
  262. */
  263. dccp_enqueue_skb(sk, skb);
  264. return 0;
  265. case DCCP_PKT_ACK:
  266. goto discard;
  267. case DCCP_PKT_RESET:
  268. /*
  269. * Step 9: Process Reset
  270. * If P.type == Reset,
  271. * Tear down connection
  272. * S.state := TIMEWAIT
  273. * Set TIMEWAIT timer
  274. * Drop packet and return
  275. */
  276. dccp_rcv_reset(sk, skb);
  277. return 0;
  278. case DCCP_PKT_CLOSEREQ:
  279. if (dccp_rcv_closereq(sk, skb))
  280. return 0;
  281. goto discard;
  282. case DCCP_PKT_CLOSE:
  283. if (dccp_rcv_close(sk, skb))
  284. return 0;
  285. goto discard;
  286. case DCCP_PKT_REQUEST:
  287. /* Step 7
  288. * or (S.is_server and P.type == Response)
  289. * or (S.is_client and P.type == Request)
  290. * or (S.state >= OPEN and P.type == Request
  291. * and P.seqno >= S.OSR)
  292. * or (S.state >= OPEN and P.type == Response
  293. * and P.seqno >= S.OSR)
  294. * or (S.state == RESPOND and P.type == Data),
  295. * Send Sync packet acknowledging P.seqno
  296. * Drop packet and return
  297. */
  298. if (dp->dccps_role != DCCP_ROLE_LISTEN)
  299. goto send_sync;
  300. goto check_seq;
  301. case DCCP_PKT_RESPONSE:
  302. if (dp->dccps_role != DCCP_ROLE_CLIENT)
  303. goto send_sync;
  304. check_seq:
  305. if (dccp_delta_seqno(dp->dccps_osr,
  306. DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
  307. send_sync:
  308. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  309. DCCP_PKT_SYNC);
  310. }
  311. break;
  312. case DCCP_PKT_SYNC:
  313. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  314. DCCP_PKT_SYNCACK);
  315. /*
  316. * From RFC 4340, sec. 5.7
  317. *
  318. * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
  319. * MAY have non-zero-length application data areas, whose
  320. * contents receivers MUST ignore.
  321. */
  322. goto discard;
  323. }
  324. DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
  325. discard:
  326. __kfree_skb(skb);
  327. return 0;
  328. }
  329. int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  330. const struct dccp_hdr *dh, const unsigned len)
  331. {
  332. if (dccp_check_seqno(sk, skb))
  333. goto discard;
  334. if (dccp_parse_options(sk, NULL, skb))
  335. return 1;
  336. dccp_handle_ackvec_processing(sk, skb);
  337. dccp_deliver_input_to_ccids(sk, skb);
  338. return __dccp_rcv_established(sk, skb, dh, len);
  339. discard:
  340. __kfree_skb(skb);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL_GPL(dccp_rcv_established);
  344. static int dccp_rcv_request_sent_state_process(struct sock *sk,
  345. struct sk_buff *skb,
  346. const struct dccp_hdr *dh,
  347. const unsigned len)
  348. {
  349. /*
  350. * Step 4: Prepare sequence numbers in REQUEST
  351. * If S.state == REQUEST,
  352. * If (P.type == Response or P.type == Reset)
  353. * and S.AWL <= P.ackno <= S.AWH,
  354. * / * Set sequence number variables corresponding to the
  355. * other endpoint, so P will pass the tests in Step 6 * /
  356. * Set S.GSR, S.ISR, S.SWL, S.SWH
  357. * / * Response processing continues in Step 10; Reset
  358. * processing continues in Step 9 * /
  359. */
  360. if (dh->dccph_type == DCCP_PKT_RESPONSE) {
  361. const struct inet_connection_sock *icsk = inet_csk(sk);
  362. struct dccp_sock *dp = dccp_sk(sk);
  363. long tstamp = dccp_timestamp();
  364. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  365. dp->dccps_awl, dp->dccps_awh)) {
  366. dccp_pr_debug("invalid ackno: S.AWL=%llu, "
  367. "P.ackno=%llu, S.AWH=%llu \n",
  368. (unsigned long long)dp->dccps_awl,
  369. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  370. (unsigned long long)dp->dccps_awh);
  371. goto out_invalid_packet;
  372. }
  373. /*
  374. * If option processing (Step 8) failed, return 1 here so that
  375. * dccp_v4_do_rcv() sends a Reset. The Reset code depends on
  376. * the option type and is set in dccp_parse_options().
  377. */
  378. if (dccp_parse_options(sk, NULL, skb))
  379. return 1;
  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. /* Stop the REQUEST timer */
  385. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  386. WARN_ON(sk->sk_send_head == NULL);
  387. kfree_skb(sk->sk_send_head);
  388. sk->sk_send_head = NULL;
  389. /*
  390. * Set ISR, GSR from packet. ISS was set in dccp_v{4,6}_connect
  391. * and GSS in dccp_transmit_skb(). Setting AWL/AWH and SWL/SWH
  392. * is done as part of activating the feature values below, since
  393. * these settings depend on the local/remote Sequence Window
  394. * features, which were undefined or not confirmed until now.
  395. */
  396. dp->dccps_gsr = dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
  397. dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  398. /*
  399. * Step 10: Process REQUEST state (second part)
  400. * If S.state == REQUEST,
  401. * / * If we get here, P is a valid Response from the
  402. * server (see Step 4), and we should move to
  403. * PARTOPEN state. PARTOPEN means send an Ack,
  404. * don't send Data packets, retransmit Acks
  405. * periodically, and always include any Init Cookie
  406. * from the Response * /
  407. * S.state := PARTOPEN
  408. * Set PARTOPEN timer
  409. * Continue with S.state == PARTOPEN
  410. * / * Step 12 will send the Ack completing the
  411. * three-way handshake * /
  412. */
  413. dccp_set_state(sk, DCCP_PARTOPEN);
  414. /*
  415. * If feature negotiation was successful, activate features now;
  416. * an activation failure means that this host could not activate
  417. * one ore more features (e.g. insufficient memory), which would
  418. * leave at least one feature in an undefined state.
  419. */
  420. if (dccp_feat_activate_values(sk, &dp->dccps_featneg))
  421. goto unable_to_proceed;
  422. /* Make sure socket is routed, for correct metrics. */
  423. icsk->icsk_af_ops->rebuild_header(sk);
  424. if (!sock_flag(sk, SOCK_DEAD)) {
  425. sk->sk_state_change(sk);
  426. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
  427. }
  428. if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
  429. icsk->icsk_accept_queue.rskq_defer_accept) {
  430. /* Save one ACK. Data will be ready after
  431. * several ticks, if write_pending is set.
  432. *
  433. * It may be deleted, but with this feature tcpdumps
  434. * look so _wonderfully_ clever, that I was not able
  435. * to stand against the temptation 8) --ANK
  436. */
  437. /*
  438. * OK, in DCCP we can as well do a similar trick, its
  439. * even in the draft, but there is no need for us to
  440. * schedule an ack here, as dccp_sendmsg does this for
  441. * us, also stated in the draft. -acme
  442. */
  443. __kfree_skb(skb);
  444. return 0;
  445. }
  446. dccp_send_ack(sk);
  447. return -1;
  448. }
  449. out_invalid_packet:
  450. /* dccp_v4_do_rcv will send a reset */
  451. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  452. return 1;
  453. unable_to_proceed:
  454. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_ABORTED;
  455. /*
  456. * We mark this socket as no longer usable, so that the loop in
  457. * dccp_sendmsg() terminates and the application gets notified.
  458. */
  459. dccp_set_state(sk, DCCP_CLOSED);
  460. sk->sk_err = ECOMM;
  461. return 1;
  462. }
  463. static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
  464. struct sk_buff *skb,
  465. const struct dccp_hdr *dh,
  466. const unsigned len)
  467. {
  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. case DCCP_PKT_DATAACK:
  477. case DCCP_PKT_ACK:
  478. /*
  479. * FIXME: we should be reseting the PARTOPEN (DELACK) timer
  480. * here but only if we haven't used the DELACK timer for
  481. * something else, like sending a delayed ack for a TIMESTAMP
  482. * echo, etc, for now were not clearing it, sending an extra
  483. * ACK when there is nothing else to do in DELACK is not a big
  484. * deal after all.
  485. */
  486. /* Stop the PARTOPEN timer */
  487. if (sk->sk_state == DCCP_PARTOPEN)
  488. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  489. dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
  490. dccp_set_state(sk, DCCP_OPEN);
  491. if (dh->dccph_type == DCCP_PKT_DATAACK ||
  492. dh->dccph_type == DCCP_PKT_DATA) {
  493. __dccp_rcv_established(sk, skb, dh, len);
  494. queued = 1; /* packet was queued
  495. (by __dccp_rcv_established) */
  496. }
  497. break;
  498. }
  499. return queued;
  500. }
  501. int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  502. struct dccp_hdr *dh, unsigned len)
  503. {
  504. struct dccp_sock *dp = dccp_sk(sk);
  505. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  506. const int old_state = sk->sk_state;
  507. int queued = 0;
  508. /*
  509. * Step 3: Process LISTEN state
  510. *
  511. * If S.state == LISTEN,
  512. * If P.type == Request or P contains a valid Init Cookie option,
  513. * (* Must scan the packet's options to check for Init
  514. * Cookies. Only Init Cookies are processed here,
  515. * however; other options are processed in Step 8. This
  516. * scan need only be performed if the endpoint uses Init
  517. * Cookies *)
  518. * (* Generate a new socket and switch to that socket *)
  519. * Set S := new socket for this port pair
  520. * S.state = RESPOND
  521. * Choose S.ISS (initial seqno) or set from Init Cookies
  522. * Initialize S.GAR := S.ISS
  523. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
  524. * Cookies Continue with S.state == RESPOND
  525. * (* A Response packet will be generated in Step 11 *)
  526. * Otherwise,
  527. * Generate Reset(No Connection) unless P.type == Reset
  528. * Drop packet and return
  529. */
  530. if (sk->sk_state == DCCP_LISTEN) {
  531. if (dh->dccph_type == DCCP_PKT_REQUEST) {
  532. if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
  533. skb) < 0)
  534. return 1;
  535. goto discard;
  536. }
  537. if (dh->dccph_type == DCCP_PKT_RESET)
  538. goto discard;
  539. /* Caller (dccp_v4_do_rcv) will send Reset */
  540. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  541. return 1;
  542. } else if (sk->sk_state == DCCP_CLOSED) {
  543. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  544. return 1;
  545. }
  546. /* Step 6: Check sequence numbers (omitted in LISTEN/REQUEST state) */
  547. if (sk->sk_state != DCCP_REQUESTING && dccp_check_seqno(sk, skb))
  548. goto discard;
  549. /*
  550. * Step 7: Check for unexpected packet types
  551. * If (S.is_server and P.type == Response)
  552. * or (S.is_client and P.type == Request)
  553. * or (S.state == RESPOND and P.type == Data),
  554. * Send Sync packet acknowledging P.seqno
  555. * Drop packet and return
  556. */
  557. if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
  558. dh->dccph_type == DCCP_PKT_RESPONSE) ||
  559. (dp->dccps_role == DCCP_ROLE_CLIENT &&
  560. dh->dccph_type == DCCP_PKT_REQUEST) ||
  561. (sk->sk_state == DCCP_RESPOND && dh->dccph_type == DCCP_PKT_DATA)) {
  562. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
  563. goto discard;
  564. }
  565. /* Step 8: Process options */
  566. if (dccp_parse_options(sk, NULL, skb))
  567. return 1;
  568. /*
  569. * Step 9: Process Reset
  570. * If P.type == Reset,
  571. * Tear down connection
  572. * S.state := TIMEWAIT
  573. * Set TIMEWAIT timer
  574. * Drop packet and return
  575. */
  576. if (dh->dccph_type == DCCP_PKT_RESET) {
  577. dccp_rcv_reset(sk, skb);
  578. return 0;
  579. } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) { /* Step 13 */
  580. if (dccp_rcv_closereq(sk, skb))
  581. return 0;
  582. goto discard;
  583. } else if (dh->dccph_type == DCCP_PKT_CLOSE) { /* Step 14 */
  584. if (dccp_rcv_close(sk, skb))
  585. return 0;
  586. goto discard;
  587. }
  588. switch (sk->sk_state) {
  589. case DCCP_REQUESTING:
  590. queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
  591. if (queued >= 0)
  592. return queued;
  593. __kfree_skb(skb);
  594. return 0;
  595. case DCCP_PARTOPEN:
  596. /* Step 8: if using Ack Vectors, mark packet acknowledgeable */
  597. dccp_handle_ackvec_processing(sk, skb);
  598. dccp_deliver_input_to_ccids(sk, skb);
  599. /* fall through */
  600. case DCCP_RESPOND:
  601. queued = dccp_rcv_respond_partopen_state_process(sk, skb,
  602. dh, len);
  603. break;
  604. }
  605. if (dh->dccph_type == DCCP_PKT_ACK ||
  606. dh->dccph_type == DCCP_PKT_DATAACK) {
  607. switch (old_state) {
  608. case DCCP_PARTOPEN:
  609. sk->sk_state_change(sk);
  610. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
  611. break;
  612. }
  613. } else if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
  614. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
  615. goto discard;
  616. }
  617. if (!queued) {
  618. discard:
  619. __kfree_skb(skb);
  620. }
  621. return 0;
  622. }
  623. EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
  624. /**
  625. * dccp_sample_rtt - Validate and finalise computation of RTT sample
  626. * @delta: number of microseconds between packet and acknowledgment
  627. * The routine is kept generic to work in different contexts. It should be
  628. * called immediately when the ACK used for the RTT sample arrives.
  629. */
  630. u32 dccp_sample_rtt(struct sock *sk, long delta)
  631. {
  632. /* dccpor_elapsed_time is either zeroed out or set and > 0 */
  633. delta -= dccp_sk(sk)->dccps_options_received.dccpor_elapsed_time * 10;
  634. return dccp_sane_rtt(delta);
  635. }
  636. EXPORT_SYMBOL_GPL(dccp_sample_rtt);