input.c 19 KB

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