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