1/* 2 * net/dccp/output.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/kernel.h> 15#include <linux/skbuff.h> 16 17#include <net/inet_sock.h> 18#include <net/sock.h> 19 20#include "ackvec.h" 21#include "ccid.h" 22#include "dccp.h" 23 24static inline void dccp_event_ack_sent(struct sock *sk) 25{ 26 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 27} 28 29static void dccp_skb_entail(struct sock *sk, struct sk_buff *skb) 30{ 31 skb_set_owner_w(skb, sk); 32 WARN_ON(sk->sk_send_head); 33 sk->sk_send_head = skb; 34} 35 36/* 37 * All SKB's seen here are completely headerless. It is our 38 * job to build the DCCP header, and pass the packet down to 39 * IP so it can do the same plus pass the packet off to the 40 * device. 41 */ 42static int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb) 43{ 44 if (likely(skb != NULL)) { 45 const struct inet_sock *inet = inet_sk(sk); 46 const struct inet_connection_sock *icsk = inet_csk(sk); 47 struct dccp_sock *dp = dccp_sk(sk); 48 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); 49 struct dccp_hdr *dh; 50 const u32 dccp_header_size = sizeof(*dh) + 51 sizeof(struct dccp_hdr_ext) + 52 dccp_packet_hdr_len(dcb->dccpd_type); 53 int err, set_ack = 1; 54 u64 ackno = dp->dccps_gsr; 55 56 dccp_inc_seqno(&dp->dccps_gss); 57 58 switch (dcb->dccpd_type) { 59 case DCCP_PKT_DATA: 60 set_ack = 0; 61 /* fall through */ 62 case DCCP_PKT_DATAACK: 63 break; 64 65 case DCCP_PKT_REQUEST: 66 set_ack = 0; 67 /* fall through */ 68 69 case DCCP_PKT_SYNC: 70 case DCCP_PKT_SYNCACK: 71 ackno = dcb->dccpd_seq; 72 /* fall through */ 73 default: 74 /* 75 * Only data packets should come through with skb->sk 76 * set. 77 */ 78 WARN_ON(skb->sk); 79 skb_set_owner_w(skb, sk); 80 break; 81 } 82 83 dcb->dccpd_seq = dp->dccps_gss; 84 85 if (dccp_insert_options(sk, skb)) { 86 kfree_skb(skb); 87 return -EPROTO; 88 } 89 90 91 /* Build DCCP header and checksum it. */ 92 dh = dccp_zeroed_hdr(skb, dccp_header_size); 93 dh->dccph_type = dcb->dccpd_type; 94 dh->dccph_sport = inet->sport; 95 dh->dccph_dport = inet->dport; 96 dh->dccph_doff = (dccp_header_size + dcb->dccpd_opt_len) / 4; 97 dh->dccph_ccval = dcb->dccpd_ccval; 98 dh->dccph_cscov = dp->dccps_pcslen; 99 dh->dccph_x = 1; 100 101 dp->dccps_awh = dp->dccps_gss; 102 dccp_hdr_set_seq(dh, dp->dccps_gss); 103 if (set_ack) 104 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno); 105 106 switch (dcb->dccpd_type) { 107 case DCCP_PKT_REQUEST: 108 dccp_hdr_request(skb)->dccph_req_service = 109 dp->dccps_service; 110 break; 111 case DCCP_PKT_RESET: 112 dccp_hdr_reset(skb)->dccph_reset_code = 113 dcb->dccpd_reset_code; 114 break; 115 } 116 117 icsk->icsk_af_ops->send_check(sk, 0, skb); 118 119 if (set_ack) 120 dccp_event_ack_sent(sk); 121 122 DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 123 124 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 125 err = icsk->icsk_af_ops->queue_xmit(skb, 0); 126 return net_xmit_eval(err); 127 } 128 return -ENOBUFS; 129} 130 131unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu) 132{ 133 struct inet_connection_sock *icsk = inet_csk(sk); 134 struct dccp_sock *dp = dccp_sk(sk); 135 int mss_now = (pmtu - icsk->icsk_af_ops->net_header_len - 136 sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext)); 137 138 /* Now subtract optional transport overhead */ 139 mss_now -= icsk->icsk_ext_hdr_len; 140 141 142 mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4; 143 144 /* And store cached results */ 145 icsk->icsk_pmtu_cookie = pmtu; 146 dp->dccps_mss_cache = mss_now; 147 148 return mss_now; 149} 150 151EXPORT_SYMBOL_GPL(dccp_sync_mss); 152 153void dccp_write_space(struct sock *sk) 154{ 155 read_lock(&sk->sk_callback_lock); 156 157 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) 158 wake_up_interruptible(sk->sk_sleep); 159 /* Should agree with poll, otherwise some programs break */ 160 if (sock_writeable(sk)) 161 sk_wake_async(sk, 2, POLL_OUT); 162 163 read_unlock(&sk->sk_callback_lock); 164} 165 166/** 167 * dccp_wait_for_ccid - Wait for ccid to tell us we can send a packet 168 * @sk: socket to wait for 169 */ 170static int dccp_wait_for_ccid(struct sock *sk, struct sk_buff *skb) 171{ 172 struct dccp_sock *dp = dccp_sk(sk); 173 DEFINE_WAIT(wait); 174 unsigned long delay; 175 int rc; 176 177 while (1) { 178 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 179 180 if (sk->sk_err) 181 goto do_error; 182 if (signal_pending(current)) 183 goto do_interrupted; 184 185 rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb); 186 if (rc <= 0) 187 break; 188 dccp_pr_debug("delayed send by %d msec\n", rc); 189 delay = msecs_to_jiffies(rc); 190 sk->sk_write_pending++; 191 release_sock(sk); 192 schedule_timeout(delay); 193 lock_sock(sk); 194 sk->sk_write_pending--; 195 } 196out: 197 finish_wait(sk->sk_sleep, &wait); 198 return rc; 199 200do_error: 201 rc = -EPIPE; 202 goto out; 203do_interrupted: 204 rc = -EINTR; 205 goto out; 206} 207 208void dccp_write_xmit(struct sock *sk, int block) 209{ 210 struct dccp_sock *dp = dccp_sk(sk); 211 struct sk_buff *skb; 212 213 while ((skb = skb_peek(&sk->sk_write_queue))) { 214 int err = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb); 215 216 if (err > 0) { 217 if (!block) { 218 sk_reset_timer(sk, &dp->dccps_xmit_timer, 219 msecs_to_jiffies(err)+jiffies); 220 break; 221 } else 222 err = dccp_wait_for_ccid(sk, skb); 223 if (err && err != -EINTR) 224 DCCP_BUG("err=%d after dccp_wait_for_ccid", err); 225 } 226 227 skb_dequeue(&sk->sk_write_queue); 228 if (err == 0) { 229 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); 230 const int len = skb->len; 231 232 if (sk->sk_state == DCCP_PARTOPEN) { 233 /* See 8.1.5. Handshake Completion */ 234 inet_csk_schedule_ack(sk); 235 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 236 inet_csk(sk)->icsk_rto, 237 DCCP_RTO_MAX); 238 dcb->dccpd_type = DCCP_PKT_DATAACK; 239 } else if (dccp_ack_pending(sk)) 240 dcb->dccpd_type = DCCP_PKT_DATAACK; 241 else 242 dcb->dccpd_type = DCCP_PKT_DATA; 243 244 err = dccp_transmit_skb(sk, skb); 245 ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, 0, len); 246 if (err) 247 DCCP_BUG("err=%d after ccid_hc_tx_packet_sent", 248 err); 249 } else { 250 dccp_pr_debug("packet discarded due to err=%d\n", err); 251 kfree_skb(skb); 252 } 253 } 254} 255 256int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb) 257{ 258 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk) != 0) 259 return -EHOSTUNREACH; /* Routing failure or similar. */ 260 261 return dccp_transmit_skb(sk, (skb_cloned(skb) ? 262 pskb_copy(skb, GFP_ATOMIC): 263 skb_clone(skb, GFP_ATOMIC))); 264} 265 266struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst, 267 struct request_sock *req) 268{ 269 struct dccp_hdr *dh; 270 struct dccp_request_sock *dreq; 271 const u32 dccp_header_size = sizeof(struct dccp_hdr) + 272 sizeof(struct dccp_hdr_ext) + 273 sizeof(struct dccp_hdr_response); 274 struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, 275 GFP_ATOMIC); 276 if (skb == NULL) 277 return NULL; 278 279 /* Reserve space for headers. */ 280 skb_reserve(skb, sk->sk_prot->max_header); 281 282 skb->dst = dst_clone(dst); 283 284 dreq = dccp_rsk(req); 285 if (inet_rsk(req)->acked) /* increase ISS upon retransmission */ 286 dccp_inc_seqno(&dreq->dreq_iss); 287 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE; 288 DCCP_SKB_CB(skb)->dccpd_seq = dreq->dreq_iss; 289 290 if (dccp_insert_options(sk, skb)) { 291 kfree_skb(skb); 292 return NULL; 293 } 294 295 /* Build and checksum header */ 296 dh = dccp_zeroed_hdr(skb, dccp_header_size); 297 298 dh->dccph_sport = inet_sk(sk)->sport; 299 dh->dccph_dport = inet_rsk(req)->rmt_port; 300 dh->dccph_doff = (dccp_header_size + 301 DCCP_SKB_CB(skb)->dccpd_opt_len) / 4; 302 dh->dccph_type = DCCP_PKT_RESPONSE; 303 dh->dccph_x = 1; 304 dccp_hdr_set_seq(dh, dreq->dreq_iss); 305 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dreq->dreq_isr); 306 dccp_hdr_response(skb)->dccph_resp_service = dreq->dreq_service; 307 308 dccp_csum_outgoing(skb); 309 310 /* We use `acked' to remember that a Response was already sent. */ 311 inet_rsk(req)->acked = 1; 312 DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 313 return skb; 314} 315 316EXPORT_SYMBOL_GPL(dccp_make_response); 317 318static struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst, 319 const enum dccp_reset_codes code) 320{ 321 struct dccp_hdr *dh; 322 struct dccp_sock *dp = dccp_sk(sk); 323 const u32 dccp_header_size = sizeof(struct dccp_hdr) + 324 sizeof(struct dccp_hdr_ext) + 325 sizeof(struct dccp_hdr_reset); 326 struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, 327 GFP_ATOMIC); 328 if (skb == NULL) 329 return NULL; 330 331 /* Reserve space for headers. */ 332 skb_reserve(skb, sk->sk_prot->max_header); 333 334 skb->dst = dst_clone(dst); 335 336 dccp_inc_seqno(&dp->dccps_gss); 337 338 DCCP_SKB_CB(skb)->dccpd_reset_code = code; 339 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESET; 340 DCCP_SKB_CB(skb)->dccpd_seq = dp->dccps_gss; 341 342 if (dccp_insert_options(sk, skb)) { 343 kfree_skb(skb); 344 return NULL; 345 } 346 347 dh = dccp_zeroed_hdr(skb, dccp_header_size); 348 349 dh->dccph_sport = inet_sk(sk)->sport; 350 dh->dccph_dport = inet_sk(sk)->dport; 351 dh->dccph_doff = (dccp_header_size + 352 DCCP_SKB_CB(skb)->dccpd_opt_len) / 4; 353 dh->dccph_type = DCCP_PKT_RESET; 354 dh->dccph_x = 1; 355 dccp_hdr_set_seq(dh, dp->dccps_gss); 356 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr); 357 358 dccp_hdr_reset(skb)->dccph_reset_code = code; 359 inet_csk(sk)->icsk_af_ops->send_check(sk, 0, skb); 360 361 DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 362 return skb; 363} 364 365int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code) 366{ 367 int err = inet_sk_rebuild_header(sk); 368 369 if (err == 0) { 370 struct sk_buff *skb = dccp_make_reset(sk, sk->sk_dst_cache, 371 code); 372 if (skb != NULL) { 373 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 374 err = inet_csk(sk)->icsk_af_ops->queue_xmit(skb, 0); 375 return net_xmit_eval(err); 376 } 377 } 378 379 return err; 380} 381 382/* 383 * Do all connect socket setups that can be done AF independent. 384 */ 385static inline void dccp_connect_init(struct sock *sk) 386{ 387 struct dccp_sock *dp = dccp_sk(sk); 388 struct dst_entry *dst = __sk_dst_get(sk); 389 struct inet_connection_sock *icsk = inet_csk(sk); 390 391 sk->sk_err = 0; 392 sock_reset_flag(sk, SOCK_DONE); 393 394 dccp_sync_mss(sk, dst_mtu(dst)); 395 396 /* 397 * SWL and AWL are initially adjusted so that they are not less than 398 * the initial Sequence Numbers received and sent, respectively: 399 * SWL := max(GSR + 1 - floor(W/4), ISR), 400 * AWL := max(GSS - W' + 1, ISS). 401 * These adjustments MUST be applied only at the beginning of the 402 * connection. 403 */ 404 dccp_update_gss(sk, dp->dccps_iss); 405 dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss)); 406 407 /* S.GAR - greatest valid acknowledgement number received on a non-Sync; 408 * initialized to S.ISS (sec. 8.5) */ 409 dp->dccps_gar = dp->dccps_iss; 410 411 icsk->icsk_retransmits = 0; 412} 413 414int dccp_connect(struct sock *sk) 415{ 416 struct sk_buff *skb; 417 struct inet_connection_sock *icsk = inet_csk(sk); 418 419 dccp_connect_init(sk); 420 421 skb = alloc_skb(sk->sk_prot->max_header, sk->sk_allocation); 422 if (unlikely(skb == NULL)) 423 return -ENOBUFS; 424 425 /* Reserve space for headers. */ 426 skb_reserve(skb, sk->sk_prot->max_header); 427 428 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST; 429 430 dccp_skb_entail(sk, skb); 431 dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL)); 432 DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS); 433 434 /* Timer for repeating the REQUEST until an answer. */ 435 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 436 icsk->icsk_rto, DCCP_RTO_MAX); 437 return 0; 438} 439 440EXPORT_SYMBOL_GPL(dccp_connect); 441 442void dccp_send_ack(struct sock *sk) 443{ 444 /* If we have been reset, we may not send again. */ 445 if (sk->sk_state != DCCP_CLOSED) { 446 struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, 447 GFP_ATOMIC); 448 449 if (skb == NULL) { 450 inet_csk_schedule_ack(sk); 451 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN; 452 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 453 TCP_DELACK_MAX, 454 DCCP_RTO_MAX); 455 return; 456 } 457 458 /* Reserve space for headers */ 459 skb_reserve(skb, sk->sk_prot->max_header); 460 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK; 461 dccp_transmit_skb(sk, skb); 462 } 463} 464 465EXPORT_SYMBOL_GPL(dccp_send_ack); 466 467void dccp_send_delayed_ack(struct sock *sk) 468{ 469 struct inet_connection_sock *icsk = inet_csk(sk); 470 unsigned long timeout = jiffies + 2 * HZ; 471 472 /* Use new timeout only if there wasn't a older one earlier. */ 473 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { 474 if (icsk->icsk_ack.blocked) { 475 dccp_send_ack(sk); 476 return; 477 } 478 479 if (!time_before(timeout, icsk->icsk_ack.timeout)) 480 timeout = icsk->icsk_ack.timeout; 481 } 482 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 483 icsk->icsk_ack.timeout = timeout; 484 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 485} 486 487void dccp_send_sync(struct sock *sk, const u64 seq, 488 const enum dccp_pkt_type pkt_type) 489{ 490 /* 491 * We are not putting this on the write queue, so 492 * dccp_transmit_skb() will set the ownership to this 493 * sock. 494 */ 495 struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC); 496 497 if (skb == NULL) 498 return; 499 500 /* Reserve space for headers and prepare control bits. */ 501 skb_reserve(skb, sk->sk_prot->max_header); 502 DCCP_SKB_CB(skb)->dccpd_type = pkt_type; 503 DCCP_SKB_CB(skb)->dccpd_seq = seq; 504 505 dccp_transmit_skb(sk, skb); 506} 507 508EXPORT_SYMBOL_GPL(dccp_send_sync); 509 510/* 511 * Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This 512 * cannot be allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under 513 * any circumstances. 514 */ 515void dccp_send_close(struct sock *sk, const int active) 516{ 517 struct dccp_sock *dp = dccp_sk(sk); 518 struct sk_buff *skb; 519 const gfp_t prio = active ? GFP_KERNEL : GFP_ATOMIC; 520 521 skb = alloc_skb(sk->sk_prot->max_header, prio); 522 if (skb == NULL) 523 return; 524 525 /* Reserve space for headers and prepare control bits. */ 526 skb_reserve(skb, sk->sk_prot->max_header); 527 DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ? 528 DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ; 529 530 if (active) { 531 dccp_write_xmit(sk, 1); 532 dccp_skb_entail(sk, skb); 533 dccp_transmit_skb(sk, skb_clone(skb, prio)); 534 } else 535 dccp_transmit_skb(sk, skb); 536} 537