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