input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. static void dccp_fin(struct sock *sk, struct sk_buff *skb)
  19. {
  20. sk->sk_shutdown |= RCV_SHUTDOWN;
  21. sock_set_flag(sk, SOCK_DONE);
  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_rcv_close(struct sock *sk, struct sk_buff *skb)
  28. {
  29. dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
  30. dccp_fin(sk, skb);
  31. dccp_set_state(sk, DCCP_CLOSED);
  32. sk_wake_async(sk, 1, POLL_HUP);
  33. }
  34. static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
  35. {
  36. /*
  37. * Step 7: Check for unexpected packet types
  38. * If (S.is_server and P.type == CloseReq)
  39. * Send Sync packet acknowledging P.seqno
  40. * Drop packet and return
  41. */
  42. if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
  43. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
  44. return;
  45. }
  46. if (sk->sk_state != DCCP_CLOSING)
  47. dccp_set_state(sk, DCCP_CLOSING);
  48. dccp_send_close(sk, 0);
  49. }
  50. static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
  51. {
  52. struct dccp_sock *dp = dccp_sk(sk);
  53. if (dccp_msk(sk)->dccpms_send_ack_vector)
  54. dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
  55. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  56. }
  57. static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
  58. {
  59. const struct dccp_hdr *dh = dccp_hdr(skb);
  60. struct dccp_sock *dp = dccp_sk(sk);
  61. u64 lswl, lawl;
  62. /*
  63. * Step 5: Prepare sequence numbers for Sync
  64. * If P.type == Sync or P.type == SyncAck,
  65. * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
  66. * / * P is valid, so update sequence number variables
  67. * accordingly. After this update, P will pass the tests
  68. * in Step 6. A SyncAck is generated if necessary in
  69. * Step 15 * /
  70. * Update S.GSR, S.SWL, S.SWH
  71. * Otherwise,
  72. * Drop packet and return
  73. */
  74. if (dh->dccph_type == DCCP_PKT_SYNC ||
  75. dh->dccph_type == DCCP_PKT_SYNCACK) {
  76. if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  77. dp->dccps_awl, dp->dccps_awh) &&
  78. dccp_delta_seqno(dp->dccps_swl,
  79. DCCP_SKB_CB(skb)->dccpd_seq) >= 0)
  80. dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
  81. else
  82. return -1;
  83. }
  84. /*
  85. * Step 6: Check sequence numbers
  86. * Let LSWL = S.SWL and LAWL = S.AWL
  87. * If P.type == CloseReq or P.type == Close or P.type == Reset,
  88. * LSWL := S.GSR + 1, LAWL := S.GAR
  89. * If LSWL <= P.seqno <= S.SWH
  90. * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
  91. * Update S.GSR, S.SWL, S.SWH
  92. * If P.type != Sync,
  93. * Update S.GAR
  94. * Otherwise,
  95. * Send Sync packet acknowledging P.seqno
  96. * Drop packet and return
  97. */
  98. lswl = dp->dccps_swl;
  99. lawl = dp->dccps_awl;
  100. if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
  101. dh->dccph_type == DCCP_PKT_CLOSE ||
  102. dh->dccph_type == DCCP_PKT_RESET) {
  103. lswl = dp->dccps_gsr;
  104. dccp_inc_seqno(&lswl);
  105. lawl = dp->dccps_gar;
  106. }
  107. if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
  108. (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
  109. between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  110. lawl, dp->dccps_awh))) {
  111. dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
  112. if (dh->dccph_type != DCCP_PKT_SYNC &&
  113. (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
  114. DCCP_PKT_WITHOUT_ACK_SEQ))
  115. dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
  116. } else {
  117. DCCP_WARN("DCCP: Step 6 failed for %s packet, "
  118. "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
  119. "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
  120. "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
  121. (unsigned long long) lswl,
  122. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq,
  123. (unsigned long long) dp->dccps_swh,
  124. (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
  125. DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
  126. (unsigned long long) lawl,
  127. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
  128. (unsigned long long) dp->dccps_awh);
  129. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  135. const struct dccp_hdr *dh, const unsigned len)
  136. {
  137. struct dccp_sock *dp = dccp_sk(sk);
  138. switch (dccp_hdr(skb)->dccph_type) {
  139. case DCCP_PKT_DATAACK:
  140. case DCCP_PKT_DATA:
  141. /*
  142. * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
  143. * option if it is.
  144. */
  145. __skb_pull(skb, dh->dccph_doff * 4);
  146. __skb_queue_tail(&sk->sk_receive_queue, skb);
  147. skb_set_owner_r(skb, sk);
  148. sk->sk_data_ready(sk, 0);
  149. return 0;
  150. case DCCP_PKT_ACK:
  151. goto discard;
  152. case DCCP_PKT_RESET:
  153. /*
  154. * Step 9: Process Reset
  155. * If P.type == Reset,
  156. * Tear down connection
  157. * S.state := TIMEWAIT
  158. * Set TIMEWAIT timer
  159. * Drop packet and return
  160. */
  161. dccp_fin(sk, skb);
  162. dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
  163. return 0;
  164. case DCCP_PKT_CLOSEREQ:
  165. dccp_rcv_closereq(sk, skb);
  166. goto discard;
  167. case DCCP_PKT_CLOSE:
  168. dccp_rcv_close(sk, skb);
  169. return 0;
  170. case DCCP_PKT_REQUEST:
  171. /* Step 7
  172. * or (S.is_server and P.type == Response)
  173. * or (S.is_client and P.type == Request)
  174. * or (S.state >= OPEN and P.type == Request
  175. * and P.seqno >= S.OSR)
  176. * or (S.state >= OPEN and P.type == Response
  177. * and P.seqno >= S.OSR)
  178. * or (S.state == RESPOND and P.type == Data),
  179. * Send Sync packet acknowledging P.seqno
  180. * Drop packet and return
  181. */
  182. if (dp->dccps_role != DCCP_ROLE_LISTEN)
  183. goto send_sync;
  184. goto check_seq;
  185. case DCCP_PKT_RESPONSE:
  186. if (dp->dccps_role != DCCP_ROLE_CLIENT)
  187. goto send_sync;
  188. check_seq:
  189. if (dccp_delta_seqno(dp->dccps_osr,
  190. DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
  191. send_sync:
  192. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  193. DCCP_PKT_SYNC);
  194. }
  195. break;
  196. case DCCP_PKT_SYNC:
  197. dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
  198. DCCP_PKT_SYNCACK);
  199. /*
  200. * From RFC 4340, sec. 5.7
  201. *
  202. * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
  203. * MAY have non-zero-length application data areas, whose
  204. * contents receivers MUST ignore.
  205. */
  206. goto discard;
  207. }
  208. DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
  209. discard:
  210. __kfree_skb(skb);
  211. return 0;
  212. }
  213. int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
  214. const struct dccp_hdr *dh, const unsigned len)
  215. {
  216. struct dccp_sock *dp = dccp_sk(sk);
  217. if (dccp_check_seqno(sk, skb))
  218. goto discard;
  219. if (dccp_parse_options(sk, skb))
  220. goto discard;
  221. if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  222. dccp_event_ack_recv(sk, skb);
  223. if (dccp_msk(sk)->dccpms_send_ack_vector &&
  224. dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
  225. DCCP_SKB_CB(skb)->dccpd_seq,
  226. DCCP_ACKVEC_STATE_RECEIVED))
  227. goto discard;
  228. ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
  229. ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
  230. return __dccp_rcv_established(sk, skb, dh, len);
  231. discard:
  232. __kfree_skb(skb);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(dccp_rcv_established);
  236. static int dccp_rcv_request_sent_state_process(struct sock *sk,
  237. struct sk_buff *skb,
  238. const struct dccp_hdr *dh,
  239. const unsigned len)
  240. {
  241. /*
  242. * Step 4: Prepare sequence numbers in REQUEST
  243. * If S.state == REQUEST,
  244. * If (P.type == Response or P.type == Reset)
  245. * and S.AWL <= P.ackno <= S.AWH,
  246. * / * Set sequence number variables corresponding to the
  247. * other endpoint, so P will pass the tests in Step 6 * /
  248. * Set S.GSR, S.ISR, S.SWL, S.SWH
  249. * / * Response processing continues in Step 10; Reset
  250. * processing continues in Step 9 * /
  251. */
  252. if (dh->dccph_type == DCCP_PKT_RESPONSE) {
  253. const struct inet_connection_sock *icsk = inet_csk(sk);
  254. struct dccp_sock *dp = dccp_sk(sk);
  255. /* Stop the REQUEST timer */
  256. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  257. BUG_TRAP(sk->sk_send_head != NULL);
  258. __kfree_skb(sk->sk_send_head);
  259. sk->sk_send_head = NULL;
  260. if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
  261. dp->dccps_awl, dp->dccps_awh)) {
  262. dccp_pr_debug("invalid ackno: S.AWL=%llu, "
  263. "P.ackno=%llu, S.AWH=%llu \n",
  264. (unsigned long long)dp->dccps_awl,
  265. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  266. (unsigned long long)dp->dccps_awh);
  267. goto out_invalid_packet;
  268. }
  269. if (dccp_parse_options(sk, skb))
  270. goto out_invalid_packet;
  271. /* Obtain RTT sample from SYN exchange (used by CCID 3) */
  272. if (dp->dccps_options_received.dccpor_timestamp_echo) {
  273. struct timeval now;
  274. dccp_timestamp(sk, &now);
  275. dp->dccps_syn_rtt = dccp_sample_rtt(sk, &now, NULL);
  276. }
  277. if (dccp_msk(sk)->dccpms_send_ack_vector &&
  278. dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
  279. DCCP_SKB_CB(skb)->dccpd_seq,
  280. DCCP_ACKVEC_STATE_RECEIVED))
  281. goto out_invalid_packet; /* FIXME: change error code */
  282. dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
  283. dccp_update_gsr(sk, dp->dccps_isr);
  284. /*
  285. * SWL and AWL are initially adjusted so that they are not less than
  286. * the initial Sequence Numbers received and sent, respectively:
  287. * SWL := max(GSR + 1 - floor(W/4), ISR),
  288. * AWL := max(GSS - W' + 1, ISS).
  289. * These adjustments MUST be applied only at the beginning of the
  290. * connection.
  291. *
  292. * AWL was adjusted in dccp_v4_connect -acme
  293. */
  294. dccp_set_seqno(&dp->dccps_swl,
  295. max48(dp->dccps_swl, dp->dccps_isr));
  296. dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  297. /*
  298. * Step 10: Process REQUEST state (second part)
  299. * If S.state == REQUEST,
  300. * / * If we get here, P is a valid Response from the
  301. * server (see Step 4), and we should move to
  302. * PARTOPEN state. PARTOPEN means send an Ack,
  303. * don't send Data packets, retransmit Acks
  304. * periodically, and always include any Init Cookie
  305. * from the Response * /
  306. * S.state := PARTOPEN
  307. * Set PARTOPEN timer
  308. * Continue with S.state == PARTOPEN
  309. * / * Step 12 will send the Ack completing the
  310. * three-way handshake * /
  311. */
  312. dccp_set_state(sk, DCCP_PARTOPEN);
  313. /* Make sure socket is routed, for correct metrics. */
  314. icsk->icsk_af_ops->rebuild_header(sk);
  315. if (!sock_flag(sk, SOCK_DEAD)) {
  316. sk->sk_state_change(sk);
  317. sk_wake_async(sk, 0, POLL_OUT);
  318. }
  319. if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
  320. icsk->icsk_accept_queue.rskq_defer_accept) {
  321. /* Save one ACK. Data will be ready after
  322. * several ticks, if write_pending is set.
  323. *
  324. * It may be deleted, but with this feature tcpdumps
  325. * look so _wonderfully_ clever, that I was not able
  326. * to stand against the temptation 8) --ANK
  327. */
  328. /*
  329. * OK, in DCCP we can as well do a similar trick, its
  330. * even in the draft, but there is no need for us to
  331. * schedule an ack here, as dccp_sendmsg does this for
  332. * us, also stated in the draft. -acme
  333. */
  334. __kfree_skb(skb);
  335. return 0;
  336. }
  337. dccp_send_ack(sk);
  338. return -1;
  339. }
  340. out_invalid_packet:
  341. /* dccp_v4_do_rcv will send a reset */
  342. DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
  343. return 1;
  344. }
  345. static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
  346. struct sk_buff *skb,
  347. const struct dccp_hdr *dh,
  348. const unsigned len)
  349. {
  350. int queued = 0;
  351. switch (dh->dccph_type) {
  352. case DCCP_PKT_RESET:
  353. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  354. break;
  355. case DCCP_PKT_DATA:
  356. if (sk->sk_state == DCCP_RESPOND)
  357. break;
  358. case DCCP_PKT_DATAACK:
  359. case DCCP_PKT_ACK:
  360. /*
  361. * FIXME: we should be reseting the PARTOPEN (DELACK) timer
  362. * here but only if we haven't used the DELACK timer for
  363. * something else, like sending a delayed ack for a TIMESTAMP
  364. * echo, etc, for now were not clearing it, sending an extra
  365. * ACK when there is nothing else to do in DELACK is not a big
  366. * deal after all.
  367. */
  368. /* Stop the PARTOPEN timer */
  369. if (sk->sk_state == DCCP_PARTOPEN)
  370. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  371. dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
  372. dccp_set_state(sk, DCCP_OPEN);
  373. if (dh->dccph_type == DCCP_PKT_DATAACK ||
  374. dh->dccph_type == DCCP_PKT_DATA) {
  375. __dccp_rcv_established(sk, skb, dh, len);
  376. queued = 1; /* packet was queued
  377. (by __dccp_rcv_established) */
  378. }
  379. break;
  380. }
  381. return queued;
  382. }
  383. int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  384. struct dccp_hdr *dh, unsigned len)
  385. {
  386. struct dccp_sock *dp = dccp_sk(sk);
  387. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  388. const int old_state = sk->sk_state;
  389. int queued = 0;
  390. /*
  391. * Step 3: Process LISTEN state
  392. *
  393. * If S.state == LISTEN,
  394. * If P.type == Request or P contains a valid Init Cookie option,
  395. * (* Must scan the packet's options to check for Init
  396. * Cookies. Only Init Cookies are processed here,
  397. * however; other options are processed in Step 8. This
  398. * scan need only be performed if the endpoint uses Init
  399. * Cookies *)
  400. * (* Generate a new socket and switch to that socket *)
  401. * Set S := new socket for this port pair
  402. * S.state = RESPOND
  403. * Choose S.ISS (initial seqno) or set from Init Cookies
  404. * Initialize S.GAR := S.ISS
  405. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
  406. * Cookies Continue with S.state == RESPOND
  407. * (* A Response packet will be generated in Step 11 *)
  408. * Otherwise,
  409. * Generate Reset(No Connection) unless P.type == Reset
  410. * Drop packet and return
  411. */
  412. if (sk->sk_state == DCCP_LISTEN) {
  413. if (dh->dccph_type == DCCP_PKT_REQUEST) {
  414. if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
  415. skb) < 0)
  416. return 1;
  417. /* FIXME: do congestion control initialization */
  418. goto discard;
  419. }
  420. if (dh->dccph_type == DCCP_PKT_RESET)
  421. goto discard;
  422. /* Caller (dccp_v4_do_rcv) will send Reset */
  423. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  424. return 1;
  425. }
  426. if (sk->sk_state != DCCP_REQUESTING) {
  427. if (dccp_check_seqno(sk, skb))
  428. goto discard;
  429. /*
  430. * Step 8: Process options and mark acknowledgeable
  431. */
  432. if (dccp_parse_options(sk, skb))
  433. goto discard;
  434. if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  435. dccp_event_ack_recv(sk, skb);
  436. if (dccp_msk(sk)->dccpms_send_ack_vector &&
  437. dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
  438. DCCP_SKB_CB(skb)->dccpd_seq,
  439. DCCP_ACKVEC_STATE_RECEIVED))
  440. goto discard;
  441. ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
  442. ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
  443. }
  444. /*
  445. * Step 9: Process Reset
  446. * If P.type == Reset,
  447. * Tear down connection
  448. * S.state := TIMEWAIT
  449. * Set TIMEWAIT timer
  450. * Drop packet and return
  451. */
  452. if (dh->dccph_type == DCCP_PKT_RESET) {
  453. /*
  454. * Queue the equivalent of TCP fin so that dccp_recvmsg
  455. * exits the loop
  456. */
  457. dccp_fin(sk, skb);
  458. dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
  459. return 0;
  460. /*
  461. * Step 7: Check for unexpected packet types
  462. * If (S.is_server and P.type == CloseReq)
  463. * or (S.is_server and P.type == Response)
  464. * or (S.is_client and P.type == Request)
  465. * or (S.state == RESPOND and P.type == Data),
  466. * Send Sync packet acknowledging P.seqno
  467. * Drop packet and return
  468. */
  469. } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
  470. (dh->dccph_type == DCCP_PKT_RESPONSE ||
  471. dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
  472. (dp->dccps_role == DCCP_ROLE_CLIENT &&
  473. dh->dccph_type == DCCP_PKT_REQUEST) ||
  474. (sk->sk_state == DCCP_RESPOND &&
  475. dh->dccph_type == DCCP_PKT_DATA)) {
  476. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
  477. goto discard;
  478. } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
  479. dccp_rcv_closereq(sk, skb);
  480. goto discard;
  481. } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
  482. dccp_rcv_close(sk, skb);
  483. return 0;
  484. }
  485. if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
  486. dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
  487. goto discard;
  488. }
  489. switch (sk->sk_state) {
  490. case DCCP_CLOSED:
  491. dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  492. return 1;
  493. case DCCP_REQUESTING:
  494. /* FIXME: do congestion control initialization */
  495. queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
  496. if (queued >= 0)
  497. return queued;
  498. __kfree_skb(skb);
  499. return 0;
  500. case DCCP_RESPOND:
  501. case DCCP_PARTOPEN:
  502. queued = dccp_rcv_respond_partopen_state_process(sk, skb,
  503. dh, len);
  504. break;
  505. }
  506. if (dh->dccph_type == DCCP_PKT_ACK ||
  507. dh->dccph_type == DCCP_PKT_DATAACK) {
  508. switch (old_state) {
  509. case DCCP_PARTOPEN:
  510. sk->sk_state_change(sk);
  511. sk_wake_async(sk, 0, POLL_OUT);
  512. break;
  513. }
  514. }
  515. if (!queued) {
  516. discard:
  517. __kfree_skb(skb);
  518. }
  519. return 0;
  520. }
  521. EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
  522. /**
  523. * dccp_sample_rtt - Sample RTT from packet exchange
  524. *
  525. * @sk: connected dccp_sock
  526. * @t_recv: receive timestamp of packet with timestamp echo
  527. * @t_hist: packet history timestamp or NULL
  528. */
  529. u32 dccp_sample_rtt(struct sock *sk, struct timeval *t_recv,
  530. struct timeval *t_hist)
  531. {
  532. struct dccp_sock *dp = dccp_sk(sk);
  533. struct dccp_options_received *or = &dp->dccps_options_received;
  534. suseconds_t delta;
  535. if (t_hist == NULL) {
  536. if (!or->dccpor_timestamp_echo) {
  537. DCCP_WARN("packet without timestamp echo\n");
  538. return DCCP_SANE_RTT_MAX;
  539. }
  540. timeval_sub_usecs(t_recv, or->dccpor_timestamp_echo * 10);
  541. delta = timeval_usecs(t_recv);
  542. } else
  543. delta = timeval_delta(t_recv, t_hist);
  544. delta -= or->dccpor_elapsed_time * 10; /* either set or 0 */
  545. if (unlikely(delta <= 0)) {
  546. DCCP_WARN("unusable RTT sample %ld, using min\n", (long)delta);
  547. return DCCP_SANE_RTT_MIN;
  548. }
  549. if (unlikely(delta - (suseconds_t)DCCP_SANE_RTT_MAX > 0)) {
  550. DCCP_WARN("RTT sample %ld too large, using max\n", (long)delta);
  551. return DCCP_SANE_RTT_MAX;
  552. }
  553. return delta;
  554. }
  555. EXPORT_SYMBOL_GPL(dccp_sample_rtt);