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