17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/output.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 57c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 67c657876SArnaldo Carvalho de Melo * 77c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 87c657876SArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 97c657876SArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 107c657876SArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 117c657876SArnaldo Carvalho de Melo */ 127c657876SArnaldo Carvalho de Melo 137c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 1448918a4dSHerbert Xu #include <linux/kernel.h> 157c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 167c657876SArnaldo Carvalho de Melo 1714c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 187c657876SArnaldo Carvalho de Melo #include <net/sock.h> 197c657876SArnaldo Carvalho de Melo 20ae31c339SArnaldo Carvalho de Melo #include "ackvec.h" 217c657876SArnaldo Carvalho de Melo #include "ccid.h" 227c657876SArnaldo Carvalho de Melo #include "dccp.h" 237c657876SArnaldo Carvalho de Melo 247c657876SArnaldo Carvalho de Melo static inline void dccp_event_ack_sent(struct sock *sk) 257c657876SArnaldo Carvalho de Melo { 267c657876SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 277c657876SArnaldo Carvalho de Melo } 287c657876SArnaldo Carvalho de Melo 29c25a18baSArnaldo Carvalho de Melo static void dccp_skb_entail(struct sock *sk, struct sk_buff *skb) 3048918a4dSHerbert Xu { 3148918a4dSHerbert Xu skb_set_owner_w(skb, sk); 3248918a4dSHerbert Xu WARN_ON(sk->sk_send_head); 3348918a4dSHerbert Xu sk->sk_send_head = skb; 3448918a4dSHerbert Xu } 3548918a4dSHerbert Xu 367c657876SArnaldo Carvalho de Melo /* 377c657876SArnaldo Carvalho de Melo * All SKB's seen here are completely headerless. It is our 387c657876SArnaldo Carvalho de Melo * job to build the DCCP header, and pass the packet down to 397c657876SArnaldo Carvalho de Melo * IP so it can do the same plus pass the packet off to the 407c657876SArnaldo Carvalho de Melo * device. 417c657876SArnaldo Carvalho de Melo */ 4248918a4dSHerbert Xu static int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb) 437c657876SArnaldo Carvalho de Melo { 447c657876SArnaldo Carvalho de Melo if (likely(skb != NULL)) { 457c657876SArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 4657cca05aSArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 477c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 487c657876SArnaldo Carvalho de Melo struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); 497c657876SArnaldo Carvalho de Melo struct dccp_hdr *dh; 507c657876SArnaldo Carvalho de Melo /* XXX For now we're using only 48 bits sequence numbers */ 51118b2c95SArnaldo Carvalho de Melo const u32 dccp_header_size = sizeof(*dh) + 527c657876SArnaldo Carvalho de Melo sizeof(struct dccp_hdr_ext) + 537c657876SArnaldo Carvalho de Melo dccp_packet_hdr_len(dcb->dccpd_type); 547c657876SArnaldo Carvalho de Melo int err, set_ack = 1; 557c657876SArnaldo Carvalho de Melo u64 ackno = dp->dccps_gsr; 567c657876SArnaldo Carvalho de Melo 577c657876SArnaldo Carvalho de Melo dccp_inc_seqno(&dp->dccps_gss); 587c657876SArnaldo Carvalho de Melo 597c657876SArnaldo Carvalho de Melo switch (dcb->dccpd_type) { 607c657876SArnaldo Carvalho de Melo case DCCP_PKT_DATA: 617c657876SArnaldo Carvalho de Melo set_ack = 0; 62edc9e819SHerbert Xu /* fall through */ 63edc9e819SHerbert Xu case DCCP_PKT_DATAACK: 647c657876SArnaldo Carvalho de Melo break; 65edc9e819SHerbert Xu 66afe00251SAndrea Bittau case DCCP_PKT_REQUEST: 67afe00251SAndrea Bittau set_ack = 0; 68afe00251SAndrea Bittau /* fall through */ 69afe00251SAndrea Bittau 707c657876SArnaldo Carvalho de Melo case DCCP_PKT_SYNC: 717c657876SArnaldo Carvalho de Melo case DCCP_PKT_SYNCACK: 727c657876SArnaldo Carvalho de Melo ackno = dcb->dccpd_seq; 73edc9e819SHerbert Xu /* fall through */ 74edc9e819SHerbert Xu default: 75edc9e819SHerbert Xu /* 76edc9e819SHerbert Xu * Only data packets should come through with skb->sk 77edc9e819SHerbert Xu * set. 78edc9e819SHerbert Xu */ 79edc9e819SHerbert Xu WARN_ON(skb->sk); 80edc9e819SHerbert Xu skb_set_owner_w(skb, sk); 817c657876SArnaldo Carvalho de Melo break; 827c657876SArnaldo Carvalho de Melo } 837c657876SArnaldo Carvalho de Melo 8424117727SArnaldo Carvalho de Melo dcb->dccpd_seq = dp->dccps_gss; 852d0817d1SArnaldo Carvalho de Melo 862d0817d1SArnaldo Carvalho de Melo if (dccp_insert_options(sk, skb)) { 872d0817d1SArnaldo Carvalho de Melo kfree_skb(skb); 882d0817d1SArnaldo Carvalho de Melo return -EPROTO; 892d0817d1SArnaldo Carvalho de Melo } 9024117727SArnaldo Carvalho de Melo 91fda0fd6cSHerbert Xu 927c657876SArnaldo Carvalho de Melo /* Build DCCP header and checksum it. */ 939b42078eSGerrit Renker dh = dccp_zeroed_hdr(skb, dccp_header_size); 947c657876SArnaldo Carvalho de Melo dh->dccph_type = dcb->dccpd_type; 957c657876SArnaldo Carvalho de Melo dh->dccph_sport = inet->sport; 967c657876SArnaldo Carvalho de Melo dh->dccph_dport = inet->dport; 977c657876SArnaldo Carvalho de Melo dh->dccph_doff = (dccp_header_size + dcb->dccpd_opt_len) / 4; 987c657876SArnaldo Carvalho de Melo dh->dccph_ccval = dcb->dccpd_ccval; 996f4e5fffSGerrit Renker dh->dccph_cscov = dp->dccps_pcslen; 1007c657876SArnaldo Carvalho de Melo /* XXX For now we're using only 48 bits sequence numbers */ 1017c657876SArnaldo Carvalho de Melo dh->dccph_x = 1; 1027c657876SArnaldo Carvalho de Melo 1037c657876SArnaldo Carvalho de Melo dp->dccps_awh = dp->dccps_gss; 1047c657876SArnaldo Carvalho de Melo dccp_hdr_set_seq(dh, dp->dccps_gss); 1057c657876SArnaldo Carvalho de Melo if (set_ack) 1067c657876SArnaldo Carvalho de Melo dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno); 1077c657876SArnaldo Carvalho de Melo 1087c657876SArnaldo Carvalho de Melo switch (dcb->dccpd_type) { 1097c657876SArnaldo Carvalho de Melo case DCCP_PKT_REQUEST: 1107690af3fSArnaldo Carvalho de Melo dccp_hdr_request(skb)->dccph_req_service = 11167e6b629SArnaldo Carvalho de Melo dp->dccps_service; 1127c657876SArnaldo Carvalho de Melo break; 1137c657876SArnaldo Carvalho de Melo case DCCP_PKT_RESET: 1147690af3fSArnaldo Carvalho de Melo dccp_hdr_reset(skb)->dccph_reset_code = 1157690af3fSArnaldo Carvalho de Melo dcb->dccpd_reset_code; 1167c657876SArnaldo Carvalho de Melo break; 1177c657876SArnaldo Carvalho de Melo } 1187c657876SArnaldo Carvalho de Melo 1196f4e5fffSGerrit Renker icsk->icsk_af_ops->send_check(sk, 0, skb); 1207c657876SArnaldo Carvalho de Melo 1217ad07e7cSArnaldo Carvalho de Melo if (set_ack) 1227c657876SArnaldo Carvalho de Melo dccp_event_ack_sent(sk); 1237c657876SArnaldo Carvalho de Melo 1247c657876SArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 1257c657876SArnaldo Carvalho de Melo 12649c5bfafSHerbert Xu memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 12793173112SDavid S. Miller err = icsk->icsk_af_ops->queue_xmit(skb, sk, 0); 1287c657876SArnaldo Carvalho de Melo if (err <= 0) 1297c657876SArnaldo Carvalho de Melo return err; 1307c657876SArnaldo Carvalho de Melo 1317c657876SArnaldo Carvalho de Melo /* NET_XMIT_CN is special. It does not guarantee, 1327c657876SArnaldo Carvalho de Melo * that this packet is lost. It tells that device 1337c657876SArnaldo Carvalho de Melo * is about to start to drop packets or already 1347c657876SArnaldo Carvalho de Melo * drops some packets of the same priority and 1357c657876SArnaldo Carvalho de Melo * invokes us to send less aggressively. 1367c657876SArnaldo Carvalho de Melo */ 1377c657876SArnaldo Carvalho de Melo return err == NET_XMIT_CN ? 0 : err; 1387c657876SArnaldo Carvalho de Melo } 1397c657876SArnaldo Carvalho de Melo return -ENOBUFS; 1407c657876SArnaldo Carvalho de Melo } 1417c657876SArnaldo Carvalho de Melo 1427c657876SArnaldo Carvalho de Melo unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu) 1437c657876SArnaldo Carvalho de Melo { 144d83d8461SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1457c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 146d83d8461SArnaldo Carvalho de Melo int mss_now = (pmtu - icsk->icsk_af_ops->net_header_len - 14757cca05aSArnaldo Carvalho de Melo sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext)); 1487c657876SArnaldo Carvalho de Melo 1497c657876SArnaldo Carvalho de Melo /* Now subtract optional transport overhead */ 150d83d8461SArnaldo Carvalho de Melo mss_now -= icsk->icsk_ext_hdr_len; 1517c657876SArnaldo Carvalho de Melo 1527c657876SArnaldo Carvalho de Melo /* 1537c657876SArnaldo Carvalho de Melo * FIXME: this should come from the CCID infrastructure, where, say, 1547c657876SArnaldo Carvalho de Melo * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets 1557c657876SArnaldo Carvalho de Melo * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED 1567c657876SArnaldo Carvalho de Melo * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to 1577c657876SArnaldo Carvalho de Melo * make it a multiple of 4 1587c657876SArnaldo Carvalho de Melo */ 1597c657876SArnaldo Carvalho de Melo 1607c657876SArnaldo Carvalho de Melo mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4; 1617c657876SArnaldo Carvalho de Melo 1627c657876SArnaldo Carvalho de Melo /* And store cached results */ 163d83d8461SArnaldo Carvalho de Melo icsk->icsk_pmtu_cookie = pmtu; 1647c657876SArnaldo Carvalho de Melo dp->dccps_mss_cache = mss_now; 1657c657876SArnaldo Carvalho de Melo 1667c657876SArnaldo Carvalho de Melo return mss_now; 1677c657876SArnaldo Carvalho de Melo } 1687c657876SArnaldo Carvalho de Melo 169f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sync_mss); 170f21e68caSArnaldo Carvalho de Melo 171c530cfb1SArnaldo Carvalho de Melo void dccp_write_space(struct sock *sk) 172c530cfb1SArnaldo Carvalho de Melo { 173c530cfb1SArnaldo Carvalho de Melo read_lock(&sk->sk_callback_lock); 174c530cfb1SArnaldo Carvalho de Melo 175c530cfb1SArnaldo Carvalho de Melo if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) 176c530cfb1SArnaldo Carvalho de Melo wake_up_interruptible(sk->sk_sleep); 177c530cfb1SArnaldo Carvalho de Melo /* Should agree with poll, otherwise some programs break */ 178c530cfb1SArnaldo Carvalho de Melo if (sock_writeable(sk)) 179c530cfb1SArnaldo Carvalho de Melo sk_wake_async(sk, 2, POLL_OUT); 180c530cfb1SArnaldo Carvalho de Melo 181c530cfb1SArnaldo Carvalho de Melo read_unlock(&sk->sk_callback_lock); 182c530cfb1SArnaldo Carvalho de Melo } 183c530cfb1SArnaldo Carvalho de Melo 184d6809c12SArnaldo Carvalho de Melo /** 185d6809c12SArnaldo Carvalho de Melo * dccp_wait_for_ccid - Wait for ccid to tell us we can send a packet 186d6809c12SArnaldo Carvalho de Melo * @sk: socket to wait for 187d6809c12SArnaldo Carvalho de Melo * @timeo: for how long 188d6809c12SArnaldo Carvalho de Melo */ 189d6809c12SArnaldo Carvalho de Melo static int dccp_wait_for_ccid(struct sock *sk, struct sk_buff *skb, 190d6809c12SArnaldo Carvalho de Melo long *timeo) 191d6809c12SArnaldo Carvalho de Melo { 192d6809c12SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 193d6809c12SArnaldo Carvalho de Melo DEFINE_WAIT(wait); 194d6809c12SArnaldo Carvalho de Melo long delay; 195d6809c12SArnaldo Carvalho de Melo int rc; 196d6809c12SArnaldo Carvalho de Melo 197d6809c12SArnaldo Carvalho de Melo while (1) { 198d6809c12SArnaldo Carvalho de Melo prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 199d6809c12SArnaldo Carvalho de Melo 20097e5848dSIan McDonald if (sk->sk_err) 201d6809c12SArnaldo Carvalho de Melo goto do_error; 202d6809c12SArnaldo Carvalho de Melo if (!*timeo) 203d6809c12SArnaldo Carvalho de Melo goto do_nonblock; 204d6809c12SArnaldo Carvalho de Melo if (signal_pending(current)) 205d6809c12SArnaldo Carvalho de Melo goto do_interrupted; 206d6809c12SArnaldo Carvalho de Melo 207d6809c12SArnaldo Carvalho de Melo rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb, 208d6809c12SArnaldo Carvalho de Melo skb->len); 209d6809c12SArnaldo Carvalho de Melo if (rc <= 0) 210d6809c12SArnaldo Carvalho de Melo break; 211d6809c12SArnaldo Carvalho de Melo delay = msecs_to_jiffies(rc); 212d6809c12SArnaldo Carvalho de Melo if (delay > *timeo || delay < 0) 213d6809c12SArnaldo Carvalho de Melo goto do_nonblock; 214d6809c12SArnaldo Carvalho de Melo 215d6809c12SArnaldo Carvalho de Melo sk->sk_write_pending++; 216d6809c12SArnaldo Carvalho de Melo release_sock(sk); 217d6809c12SArnaldo Carvalho de Melo *timeo -= schedule_timeout(delay); 218d6809c12SArnaldo Carvalho de Melo lock_sock(sk); 219d6809c12SArnaldo Carvalho de Melo sk->sk_write_pending--; 220d6809c12SArnaldo Carvalho de Melo } 221d6809c12SArnaldo Carvalho de Melo out: 222d6809c12SArnaldo Carvalho de Melo finish_wait(sk->sk_sleep, &wait); 223d6809c12SArnaldo Carvalho de Melo return rc; 224d6809c12SArnaldo Carvalho de Melo 225d6809c12SArnaldo Carvalho de Melo do_error: 226d6809c12SArnaldo Carvalho de Melo rc = -EPIPE; 227d6809c12SArnaldo Carvalho de Melo goto out; 228d6809c12SArnaldo Carvalho de Melo do_nonblock: 229d6809c12SArnaldo Carvalho de Melo rc = -EAGAIN; 230d6809c12SArnaldo Carvalho de Melo goto out; 231d6809c12SArnaldo Carvalho de Melo do_interrupted: 232d6809c12SArnaldo Carvalho de Melo rc = sock_intr_errno(*timeo); 233d6809c12SArnaldo Carvalho de Melo goto out; 234d6809c12SArnaldo Carvalho de Melo } 235d6809c12SArnaldo Carvalho de Melo 23697e5848dSIan McDonald static void dccp_write_xmit_timer(unsigned long data) { 23797e5848dSIan McDonald struct sock *sk = (struct sock *)data; 23897e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 23997e5848dSIan McDonald 24097e5848dSIan McDonald bh_lock_sock(sk); 24197e5848dSIan McDonald if (sock_owned_by_user(sk)) 24297e5848dSIan McDonald sk_reset_timer(sk, &dp->dccps_xmit_timer, jiffies+1); 24397e5848dSIan McDonald else 24497e5848dSIan McDonald dccp_write_xmit(sk, 0); 24597e5848dSIan McDonald bh_unlock_sock(sk); 24697e5848dSIan McDonald sock_put(sk); 24797e5848dSIan McDonald } 24897e5848dSIan McDonald 24997e5848dSIan McDonald void dccp_write_xmit(struct sock *sk, int block) 25027258ee5SArnaldo Carvalho de Melo { 25197e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 25297e5848dSIan McDonald struct sk_buff *skb; 253f45b3ec4SIan McDonald long timeo = DCCP_XMIT_TIMEO; /* If a packet is taking longer than 254f45b3ec4SIan McDonald this we have other issues */ 25597e5848dSIan McDonald 25697e5848dSIan McDonald while ((skb = skb_peek(&sk->sk_write_queue))) { 257d6809c12SArnaldo Carvalho de Melo int err = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb, 258d6809c12SArnaldo Carvalho de Melo skb->len); 259d6809c12SArnaldo Carvalho de Melo 26097e5848dSIan McDonald if (err > 0) { 26197e5848dSIan McDonald if (!block) { 26297e5848dSIan McDonald sk_reset_timer(sk, &dp->dccps_xmit_timer, 26397e5848dSIan McDonald msecs_to_jiffies(err)+jiffies); 26497e5848dSIan McDonald break; 265f45b3ec4SIan McDonald } else { 26697e5848dSIan McDonald err = dccp_wait_for_ccid(sk, skb, &timeo); 267f45b3ec4SIan McDonald timeo = DCCP_XMIT_TIMEO; 268f45b3ec4SIan McDonald } 26997e5848dSIan McDonald if (err) { 27097e5848dSIan McDonald printk(KERN_CRIT "%s:err at dccp_wait_for_ccid" 27197e5848dSIan McDonald " %d\n", __FUNCTION__, err); 27297e5848dSIan McDonald dump_stack(); 27397e5848dSIan McDonald } 27497e5848dSIan McDonald } 27527258ee5SArnaldo Carvalho de Melo 27697e5848dSIan McDonald skb_dequeue(&sk->sk_write_queue); 27727258ee5SArnaldo Carvalho de Melo if (err == 0) { 27827258ee5SArnaldo Carvalho de Melo struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); 279d6809c12SArnaldo Carvalho de Melo const int len = skb->len; 28027258ee5SArnaldo Carvalho de Melo 28127258ee5SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_PARTOPEN) { 28227258ee5SArnaldo Carvalho de Melo /* See 8.1.5. Handshake Completion */ 28327258ee5SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 28427258ee5SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 28527258ee5SArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 28627258ee5SArnaldo Carvalho de Melo DCCP_RTO_MAX); 28727258ee5SArnaldo Carvalho de Melo dcb->dccpd_type = DCCP_PKT_DATAACK; 288ae31c339SArnaldo Carvalho de Melo } else if (dccp_ack_pending(sk)) 28927258ee5SArnaldo Carvalho de Melo dcb->dccpd_type = DCCP_PKT_DATAACK; 29027258ee5SArnaldo Carvalho de Melo else 29127258ee5SArnaldo Carvalho de Melo dcb->dccpd_type = DCCP_PKT_DATA; 29227258ee5SArnaldo Carvalho de Melo 29327258ee5SArnaldo Carvalho de Melo err = dccp_transmit_skb(sk, skb); 29427258ee5SArnaldo Carvalho de Melo ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, 0, len); 29597e5848dSIan McDonald if (err) { 29697e5848dSIan McDonald printk(KERN_CRIT "%s:err from " 29797e5848dSIan McDonald "ccid_hc_tx_packet_sent %d\n", 29897e5848dSIan McDonald __FUNCTION__, err); 29997e5848dSIan McDonald dump_stack(); 30097e5848dSIan McDonald } 301ffa29347SHerbert Xu } else 30297e5848dSIan McDonald kfree(skb); 30397e5848dSIan McDonald } 30427258ee5SArnaldo Carvalho de Melo } 30527258ee5SArnaldo Carvalho de Melo 3067c657876SArnaldo Carvalho de Melo int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb) 3077c657876SArnaldo Carvalho de Melo { 30857cca05aSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk) != 0) 3097c657876SArnaldo Carvalho de Melo return -EHOSTUNREACH; /* Routing failure or similar. */ 3107c657876SArnaldo Carvalho de Melo 3117c657876SArnaldo Carvalho de Melo return dccp_transmit_skb(sk, (skb_cloned(skb) ? 3127c657876SArnaldo Carvalho de Melo pskb_copy(skb, GFP_ATOMIC): 3137c657876SArnaldo Carvalho de Melo skb_clone(skb, GFP_ATOMIC))); 3147c657876SArnaldo Carvalho de Melo } 3157c657876SArnaldo Carvalho de Melo 3167c657876SArnaldo Carvalho de Melo struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst, 3177c657876SArnaldo Carvalho de Melo struct request_sock *req) 3187c657876SArnaldo Carvalho de Melo { 3197c657876SArnaldo Carvalho de Melo struct dccp_hdr *dh; 32067e6b629SArnaldo Carvalho de Melo struct dccp_request_sock *dreq; 321118b2c95SArnaldo Carvalho de Melo const u32 dccp_header_size = sizeof(struct dccp_hdr) + 3227c657876SArnaldo Carvalho de Melo sizeof(struct dccp_hdr_ext) + 3237c657876SArnaldo Carvalho de Melo sizeof(struct dccp_hdr_response); 324118b2c95SArnaldo Carvalho de Melo struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, 3257c657876SArnaldo Carvalho de Melo GFP_ATOMIC); 3267c657876SArnaldo Carvalho de Melo if (skb == NULL) 3277c657876SArnaldo Carvalho de Melo return NULL; 3287c657876SArnaldo Carvalho de Melo 3297c657876SArnaldo Carvalho de Melo /* Reserve space for headers. */ 330118b2c95SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 3317c657876SArnaldo Carvalho de Melo 3327c657876SArnaldo Carvalho de Melo skb->dst = dst_clone(dst); 3337c657876SArnaldo Carvalho de Melo 33467e6b629SArnaldo Carvalho de Melo dreq = dccp_rsk(req); 335e11d9d30SGerrit Renker if (inet_rsk(req)->acked) /* increase ISS upon retransmission */ 336e11d9d30SGerrit Renker dccp_inc_seqno(&dreq->dreq_iss); 3377c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE; 33867e6b629SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_seq = dreq->dreq_iss; 3392d0817d1SArnaldo Carvalho de Melo 3402d0817d1SArnaldo Carvalho de Melo if (dccp_insert_options(sk, skb)) { 3412d0817d1SArnaldo Carvalho de Melo kfree_skb(skb); 3422d0817d1SArnaldo Carvalho de Melo return NULL; 3432d0817d1SArnaldo Carvalho de Melo } 3447c657876SArnaldo Carvalho de Melo 3459b42078eSGerrit Renker dh = dccp_zeroed_hdr(skb, dccp_header_size); 3467c657876SArnaldo Carvalho de Melo 3477c657876SArnaldo Carvalho de Melo dh->dccph_sport = inet_sk(sk)->sport; 3487c657876SArnaldo Carvalho de Melo dh->dccph_dport = inet_rsk(req)->rmt_port; 3497690af3fSArnaldo Carvalho de Melo dh->dccph_doff = (dccp_header_size + 3507690af3fSArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len) / 4; 3517c657876SArnaldo Carvalho de Melo dh->dccph_type = DCCP_PKT_RESPONSE; 3527c657876SArnaldo Carvalho de Melo dh->dccph_x = 1; 35367e6b629SArnaldo Carvalho de Melo dccp_hdr_set_seq(dh, dreq->dreq_iss); 35467e6b629SArnaldo Carvalho de Melo dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dreq->dreq_isr); 35567e6b629SArnaldo Carvalho de Melo dccp_hdr_response(skb)->dccph_resp_service = dreq->dreq_service; 3567c657876SArnaldo Carvalho de Melo 3576f4e5fffSGerrit Renker dccp_csum_outgoing(skb); 3586f4e5fffSGerrit Renker 359e11d9d30SGerrit Renker /* We use `acked' to remember that a Response was already sent. */ 360e11d9d30SGerrit Renker inet_rsk(req)->acked = 1; 3617c657876SArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 3627c657876SArnaldo Carvalho de Melo return skb; 3637c657876SArnaldo Carvalho de Melo } 3647c657876SArnaldo Carvalho de Melo 365f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_make_response); 366f21e68caSArnaldo Carvalho de Melo 367017487d7SArnaldo Carvalho de Melo static struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst, 3687c657876SArnaldo Carvalho de Melo const enum dccp_reset_codes code) 3697c657876SArnaldo Carvalho de Melo 3707c657876SArnaldo Carvalho de Melo { 3717c657876SArnaldo Carvalho de Melo struct dccp_hdr *dh; 3727c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 373118b2c95SArnaldo Carvalho de Melo const u32 dccp_header_size = sizeof(struct dccp_hdr) + 3747c657876SArnaldo Carvalho de Melo sizeof(struct dccp_hdr_ext) + 3757c657876SArnaldo Carvalho de Melo sizeof(struct dccp_hdr_reset); 376118b2c95SArnaldo Carvalho de Melo struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, 3777c657876SArnaldo Carvalho de Melo GFP_ATOMIC); 3787c657876SArnaldo Carvalho de Melo if (skb == NULL) 3797c657876SArnaldo Carvalho de Melo return NULL; 3807c657876SArnaldo Carvalho de Melo 3817c657876SArnaldo Carvalho de Melo /* Reserve space for headers. */ 382118b2c95SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 3837c657876SArnaldo Carvalho de Melo 3847c657876SArnaldo Carvalho de Melo skb->dst = dst_clone(dst); 3857c657876SArnaldo Carvalho de Melo 3867c657876SArnaldo Carvalho de Melo dccp_inc_seqno(&dp->dccps_gss); 3877c657876SArnaldo Carvalho de Melo 3887c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_reset_code = code; 3897c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESET; 3907c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_seq = dp->dccps_gss; 3912d0817d1SArnaldo Carvalho de Melo 3922d0817d1SArnaldo Carvalho de Melo if (dccp_insert_options(sk, skb)) { 3932d0817d1SArnaldo Carvalho de Melo kfree_skb(skb); 3942d0817d1SArnaldo Carvalho de Melo return NULL; 3952d0817d1SArnaldo Carvalho de Melo } 3967c657876SArnaldo Carvalho de Melo 3979b42078eSGerrit Renker dh = dccp_zeroed_hdr(skb, dccp_header_size); 3987c657876SArnaldo Carvalho de Melo 3997c657876SArnaldo Carvalho de Melo dh->dccph_sport = inet_sk(sk)->sport; 4007c657876SArnaldo Carvalho de Melo dh->dccph_dport = inet_sk(sk)->dport; 4017690af3fSArnaldo Carvalho de Melo dh->dccph_doff = (dccp_header_size + 4027690af3fSArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_opt_len) / 4; 4037c657876SArnaldo Carvalho de Melo dh->dccph_type = DCCP_PKT_RESET; 4047c657876SArnaldo Carvalho de Melo dh->dccph_x = 1; 4057c657876SArnaldo Carvalho de Melo dccp_hdr_set_seq(dh, dp->dccps_gss); 4067c657876SArnaldo Carvalho de Melo dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr); 4077c657876SArnaldo Carvalho de Melo 4087c657876SArnaldo Carvalho de Melo dccp_hdr_reset(skb)->dccph_reset_code = code; 4096f4e5fffSGerrit Renker inet_csk(sk)->icsk_af_ops->send_check(sk, 0, skb); 4107c657876SArnaldo Carvalho de Melo 4117c657876SArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_OUTSEGS); 4127c657876SArnaldo Carvalho de Melo return skb; 4137c657876SArnaldo Carvalho de Melo } 4147c657876SArnaldo Carvalho de Melo 415017487d7SArnaldo Carvalho de Melo int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code) 416017487d7SArnaldo Carvalho de Melo { 417017487d7SArnaldo Carvalho de Melo /* 418017487d7SArnaldo Carvalho de Melo * FIXME: what if rebuild_header fails? 419017487d7SArnaldo Carvalho de Melo * Should we be doing a rebuild_header here? 420017487d7SArnaldo Carvalho de Melo */ 421017487d7SArnaldo Carvalho de Melo int err = inet_sk_rebuild_header(sk); 422017487d7SArnaldo Carvalho de Melo 423017487d7SArnaldo Carvalho de Melo if (err == 0) { 424017487d7SArnaldo Carvalho de Melo struct sk_buff *skb = dccp_make_reset(sk, sk->sk_dst_cache, 425017487d7SArnaldo Carvalho de Melo code); 426017487d7SArnaldo Carvalho de Melo if (skb != NULL) { 427017487d7SArnaldo Carvalho de Melo memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 42893173112SDavid S. Miller err = inet_csk(sk)->icsk_af_ops->queue_xmit(skb, sk, 0); 429017487d7SArnaldo Carvalho de Melo if (err == NET_XMIT_CN) 430017487d7SArnaldo Carvalho de Melo err = 0; 431017487d7SArnaldo Carvalho de Melo } 432017487d7SArnaldo Carvalho de Melo } 433017487d7SArnaldo Carvalho de Melo 434017487d7SArnaldo Carvalho de Melo return err; 435017487d7SArnaldo Carvalho de Melo } 436017487d7SArnaldo Carvalho de Melo 4377c657876SArnaldo Carvalho de Melo /* 4387c657876SArnaldo Carvalho de Melo * Do all connect socket setups that can be done AF independent. 4397c657876SArnaldo Carvalho de Melo */ 4407c657876SArnaldo Carvalho de Melo static inline void dccp_connect_init(struct sock *sk) 4417c657876SArnaldo Carvalho de Melo { 442f21e68caSArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 4437c657876SArnaldo Carvalho de Melo struct dst_entry *dst = __sk_dst_get(sk); 4447c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4457c657876SArnaldo Carvalho de Melo 4467c657876SArnaldo Carvalho de Melo sk->sk_err = 0; 4477c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 4487c657876SArnaldo Carvalho de Melo 4497c657876SArnaldo Carvalho de Melo dccp_sync_mss(sk, dst_mtu(dst)); 4507c657876SArnaldo Carvalho de Melo 4517c657876SArnaldo Carvalho de Melo /* 452f21e68caSArnaldo Carvalho de Melo * SWL and AWL are initially adjusted so that they are not less than 453f21e68caSArnaldo Carvalho de Melo * the initial Sequence Numbers received and sent, respectively: 454f21e68caSArnaldo Carvalho de Melo * SWL := max(GSR + 1 - floor(W/4), ISR), 455f21e68caSArnaldo Carvalho de Melo * AWL := max(GSS - W' + 1, ISS). 456f21e68caSArnaldo Carvalho de Melo * These adjustments MUST be applied only at the beginning of the 457f21e68caSArnaldo Carvalho de Melo * connection. 4587c657876SArnaldo Carvalho de Melo */ 459d7f7365fSGerrit Renker dccp_update_gss(sk, dp->dccps_iss); 460f21e68caSArnaldo Carvalho de Melo dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss)); 4617c657876SArnaldo Carvalho de Melo 462d7f7365fSGerrit Renker /* S.GAR - greatest valid acknowledgement number received on a non-Sync; 463d7f7365fSGerrit Renker * initialized to S.ISS (sec. 8.5) */ 464d7f7365fSGerrit Renker dp->dccps_gar = dp->dccps_iss; 465d7f7365fSGerrit Renker 4667c657876SArnaldo Carvalho de Melo icsk->icsk_retransmits = 0; 46797e5848dSIan McDonald init_timer(&dp->dccps_xmit_timer); 46897e5848dSIan McDonald dp->dccps_xmit_timer.data = (unsigned long)sk; 46997e5848dSIan McDonald dp->dccps_xmit_timer.function = dccp_write_xmit_timer; 4707c657876SArnaldo Carvalho de Melo } 4717c657876SArnaldo Carvalho de Melo 4727c657876SArnaldo Carvalho de Melo int dccp_connect(struct sock *sk) 4737c657876SArnaldo Carvalho de Melo { 4747c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 4757c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4767c657876SArnaldo Carvalho de Melo 4777c657876SArnaldo Carvalho de Melo dccp_connect_init(sk); 4787c657876SArnaldo Carvalho de Melo 479118b2c95SArnaldo Carvalho de Melo skb = alloc_skb(sk->sk_prot->max_header, sk->sk_allocation); 4807c657876SArnaldo Carvalho de Melo if (unlikely(skb == NULL)) 4817c657876SArnaldo Carvalho de Melo return -ENOBUFS; 4827c657876SArnaldo Carvalho de Melo 4837c657876SArnaldo Carvalho de Melo /* Reserve space for headers. */ 484118b2c95SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 4857c657876SArnaldo Carvalho de Melo 4867c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST; 4877c657876SArnaldo Carvalho de Melo 48848918a4dSHerbert Xu dccp_skb_entail(sk, skb); 4897c657876SArnaldo Carvalho de Melo dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL)); 4907c657876SArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS); 4917c657876SArnaldo Carvalho de Melo 4927c657876SArnaldo Carvalho de Melo /* Timer for repeating the REQUEST until an answer. */ 49327258ee5SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 49427258ee5SArnaldo Carvalho de Melo icsk->icsk_rto, DCCP_RTO_MAX); 4957c657876SArnaldo Carvalho de Melo return 0; 4967c657876SArnaldo Carvalho de Melo } 4977c657876SArnaldo Carvalho de Melo 498f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_connect); 499f21e68caSArnaldo Carvalho de Melo 5007c657876SArnaldo Carvalho de Melo void dccp_send_ack(struct sock *sk) 5017c657876SArnaldo Carvalho de Melo { 5027c657876SArnaldo Carvalho de Melo /* If we have been reset, we may not send again. */ 5037c657876SArnaldo Carvalho de Melo if (sk->sk_state != DCCP_CLOSED) { 504118b2c95SArnaldo Carvalho de Melo struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, 505118b2c95SArnaldo Carvalho de Melo GFP_ATOMIC); 5067c657876SArnaldo Carvalho de Melo 5077c657876SArnaldo Carvalho de Melo if (skb == NULL) { 5087c657876SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 5097c657876SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN; 5107690af3fSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 5117690af3fSArnaldo Carvalho de Melo TCP_DELACK_MAX, 5127690af3fSArnaldo Carvalho de Melo DCCP_RTO_MAX); 5137c657876SArnaldo Carvalho de Melo return; 5147c657876SArnaldo Carvalho de Melo } 5157c657876SArnaldo Carvalho de Melo 5167c657876SArnaldo Carvalho de Melo /* Reserve space for headers */ 517118b2c95SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 5187c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK; 5197c657876SArnaldo Carvalho de Melo dccp_transmit_skb(sk, skb); 5207c657876SArnaldo Carvalho de Melo } 5217c657876SArnaldo Carvalho de Melo } 5227c657876SArnaldo Carvalho de Melo 5237c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_ack); 5247c657876SArnaldo Carvalho de Melo 5257c657876SArnaldo Carvalho de Melo void dccp_send_delayed_ack(struct sock *sk) 5267c657876SArnaldo Carvalho de Melo { 5277c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5287c657876SArnaldo Carvalho de Melo /* 5297c657876SArnaldo Carvalho de Melo * FIXME: tune this timer. elapsed time fixes the skew, so no problem 5307c657876SArnaldo Carvalho de Melo * with using 2s, and active senders also piggyback the ACK into a 5317c657876SArnaldo Carvalho de Melo * DATAACK packet, so this is really for quiescent senders. 5327c657876SArnaldo Carvalho de Melo */ 5337c657876SArnaldo Carvalho de Melo unsigned long timeout = jiffies + 2 * HZ; 5347c657876SArnaldo Carvalho de Melo 5357c657876SArnaldo Carvalho de Melo /* Use new timeout only if there wasn't a older one earlier. */ 5367c657876SArnaldo Carvalho de Melo if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { 5377c657876SArnaldo Carvalho de Melo /* If delack timer was blocked or is about to expire, 5387c657876SArnaldo Carvalho de Melo * send ACK now. 5397c657876SArnaldo Carvalho de Melo * 5407c657876SArnaldo Carvalho de Melo * FIXME: check the "about to expire" part 5417c657876SArnaldo Carvalho de Melo */ 5427c657876SArnaldo Carvalho de Melo if (icsk->icsk_ack.blocked) { 5437c657876SArnaldo Carvalho de Melo dccp_send_ack(sk); 5447c657876SArnaldo Carvalho de Melo return; 5457c657876SArnaldo Carvalho de Melo } 5467c657876SArnaldo Carvalho de Melo 5477c657876SArnaldo Carvalho de Melo if (!time_before(timeout, icsk->icsk_ack.timeout)) 5487c657876SArnaldo Carvalho de Melo timeout = icsk->icsk_ack.timeout; 5497c657876SArnaldo Carvalho de Melo } 5507c657876SArnaldo Carvalho de Melo icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 5517c657876SArnaldo Carvalho de Melo icsk->icsk_ack.timeout = timeout; 5527c657876SArnaldo Carvalho de Melo sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 5537c657876SArnaldo Carvalho de Melo } 5547c657876SArnaldo Carvalho de Melo 555e92ae93aSArnaldo Carvalho de Melo void dccp_send_sync(struct sock *sk, const u64 seq, 556e92ae93aSArnaldo Carvalho de Melo const enum dccp_pkt_type pkt_type) 5577c657876SArnaldo Carvalho de Melo { 5587c657876SArnaldo Carvalho de Melo /* 5597c657876SArnaldo Carvalho de Melo * We are not putting this on the write queue, so 5607c657876SArnaldo Carvalho de Melo * dccp_transmit_skb() will set the ownership to this 5617c657876SArnaldo Carvalho de Melo * sock. 5627c657876SArnaldo Carvalho de Melo */ 563118b2c95SArnaldo Carvalho de Melo struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC); 5647c657876SArnaldo Carvalho de Melo 5657c657876SArnaldo Carvalho de Melo if (skb == NULL) 5667c657876SArnaldo Carvalho de Melo /* FIXME: how to make sure the sync is sent? */ 5677c657876SArnaldo Carvalho de Melo return; 5687c657876SArnaldo Carvalho de Melo 5697c657876SArnaldo Carvalho de Melo /* Reserve space for headers and prepare control bits. */ 570118b2c95SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 571e92ae93aSArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = pkt_type; 5727c657876SArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_seq = seq; 5737c657876SArnaldo Carvalho de Melo 5747c657876SArnaldo Carvalho de Melo dccp_transmit_skb(sk, skb); 5757c657876SArnaldo Carvalho de Melo } 5767c657876SArnaldo Carvalho de Melo 577b61fafc4SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_sync); 578b61fafc4SArnaldo Carvalho de Melo 5797690af3fSArnaldo Carvalho de Melo /* 5807690af3fSArnaldo Carvalho de Melo * Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This 5817690af3fSArnaldo Carvalho de Melo * cannot be allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under 5827690af3fSArnaldo Carvalho de Melo * any circumstances. 5837c657876SArnaldo Carvalho de Melo */ 5847ad07e7cSArnaldo Carvalho de Melo void dccp_send_close(struct sock *sk, const int active) 5857c657876SArnaldo Carvalho de Melo { 5867c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 5877c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 5887d877f3bSAl Viro const gfp_t prio = active ? GFP_KERNEL : GFP_ATOMIC; 5897c657876SArnaldo Carvalho de Melo 5907ad07e7cSArnaldo Carvalho de Melo skb = alloc_skb(sk->sk_prot->max_header, prio); 5917ad07e7cSArnaldo Carvalho de Melo if (skb == NULL) 5927ad07e7cSArnaldo Carvalho de Melo return; 5937c657876SArnaldo Carvalho de Melo 5947c657876SArnaldo Carvalho de Melo /* Reserve space for headers and prepare control bits. */ 5957c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 5967690af3fSArnaldo Carvalho de Melo DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ? 5977690af3fSArnaldo Carvalho de Melo DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ; 5987c657876SArnaldo Carvalho de Melo 5997ad07e7cSArnaldo Carvalho de Melo if (active) { 60097e5848dSIan McDonald dccp_write_xmit(sk, 1); 60148918a4dSHerbert Xu dccp_skb_entail(sk, skb); 6027ad07e7cSArnaldo Carvalho de Melo dccp_transmit_skb(sk, skb_clone(skb, prio)); 60397e5848dSIan McDonald /* FIXME do we need a retransmit timer here? */ 6047ad07e7cSArnaldo Carvalho de Melo } else 6057c657876SArnaldo Carvalho de Melo dccp_transmit_skb(sk, skb); 6067c657876SArnaldo Carvalho de Melo } 607