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