xref: /openbmc/linux/net/dccp/output.c (revision ee1a1592)
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:
64ee1a1592SGerrit Renker 		case DCCP_PKT_RESET:
657c657876SArnaldo Carvalho de Melo 			break;
66edc9e819SHerbert Xu 
67afe00251SAndrea Bittau 		case DCCP_PKT_REQUEST:
68afe00251SAndrea Bittau 			set_ack = 0;
69afe00251SAndrea Bittau 			/* fall through */
70afe00251SAndrea Bittau 
717c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_SYNC:
727c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_SYNCACK:
73b0d045caSGerrit Renker 			ackno = dcb->dccpd_ack_seq;
74edc9e819SHerbert Xu 			/* fall through */
75edc9e819SHerbert Xu 		default:
76edc9e819SHerbert Xu 			/*
77ee1a1592SGerrit Renker 			 * Set owner/destructor: some skbs are allocated via
78ee1a1592SGerrit Renker 			 * alloc_skb (e.g. when retransmission may happen).
79ee1a1592SGerrit Renker 			 * Only Data, DataAck, and Reset packets should come
80ee1a1592SGerrit Renker 			 * through here with skb->sk set.
81edc9e819SHerbert Xu 			 */
82edc9e819SHerbert Xu 			WARN_ON(skb->sk);
83edc9e819SHerbert Xu 			skb_set_owner_w(skb, sk);
847c657876SArnaldo Carvalho de Melo 			break;
857c657876SArnaldo Carvalho de Melo 		}
867c657876SArnaldo Carvalho de Melo 
8724117727SArnaldo Carvalho de Melo 		dcb->dccpd_seq = dp->dccps_gss;
882d0817d1SArnaldo Carvalho de Melo 
892d0817d1SArnaldo Carvalho de Melo 		if (dccp_insert_options(sk, skb)) {
902d0817d1SArnaldo Carvalho de Melo 			kfree_skb(skb);
912d0817d1SArnaldo Carvalho de Melo 			return -EPROTO;
922d0817d1SArnaldo Carvalho de Melo 		}
9324117727SArnaldo Carvalho de Melo 
94fda0fd6cSHerbert Xu 
957c657876SArnaldo Carvalho de Melo 		/* Build DCCP header and checksum it. */
969b42078eSGerrit Renker 		dh = dccp_zeroed_hdr(skb, dccp_header_size);
977c657876SArnaldo Carvalho de Melo 		dh->dccph_type	= dcb->dccpd_type;
987c657876SArnaldo Carvalho de Melo 		dh->dccph_sport	= inet->sport;
997c657876SArnaldo Carvalho de Melo 		dh->dccph_dport	= inet->dport;
1007c657876SArnaldo Carvalho de Melo 		dh->dccph_doff	= (dccp_header_size + dcb->dccpd_opt_len) / 4;
1017c657876SArnaldo Carvalho de Melo 		dh->dccph_ccval	= dcb->dccpd_ccval;
1026f4e5fffSGerrit Renker 		dh->dccph_cscov = dp->dccps_pcslen;
1037c657876SArnaldo Carvalho de Melo 		/* XXX For now we're using only 48 bits sequence numbers */
1047c657876SArnaldo Carvalho de Melo 		dh->dccph_x	= 1;
1057c657876SArnaldo Carvalho de Melo 
1067c657876SArnaldo Carvalho de Melo 		dp->dccps_awh = dp->dccps_gss;
1077c657876SArnaldo Carvalho de Melo 		dccp_hdr_set_seq(dh, dp->dccps_gss);
1087c657876SArnaldo Carvalho de Melo 		if (set_ack)
1097c657876SArnaldo Carvalho de Melo 			dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
1107c657876SArnaldo Carvalho de Melo 
1117c657876SArnaldo Carvalho de Melo 		switch (dcb->dccpd_type) {
1127c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_REQUEST:
1137690af3fSArnaldo Carvalho de Melo 			dccp_hdr_request(skb)->dccph_req_service =
11467e6b629SArnaldo Carvalho de Melo 							dp->dccps_service;
1157c657876SArnaldo Carvalho de Melo 			break;
1167c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_RESET:
1177690af3fSArnaldo Carvalho de Melo 			dccp_hdr_reset(skb)->dccph_reset_code =
1187690af3fSArnaldo Carvalho de Melo 							dcb->dccpd_reset_code;
1197c657876SArnaldo Carvalho de Melo 			break;
1207c657876SArnaldo Carvalho de Melo 		}
1217c657876SArnaldo Carvalho de Melo 
1226f4e5fffSGerrit Renker 		icsk->icsk_af_ops->send_check(sk, 0, skb);
1237c657876SArnaldo Carvalho de Melo 
1247ad07e7cSArnaldo Carvalho de Melo 		if (set_ack)
1257c657876SArnaldo Carvalho de Melo 			dccp_event_ack_sent(sk);
1267c657876SArnaldo Carvalho de Melo 
1277c657876SArnaldo Carvalho de Melo 		DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
1287c657876SArnaldo Carvalho de Melo 
12949c5bfafSHerbert Xu 		memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
130e89862f4SDavid S. Miller 		err = icsk->icsk_af_ops->queue_xmit(skb, 0);
131b9df3cb8SGerrit Renker 		return net_xmit_eval(err);
1327c657876SArnaldo Carvalho de Melo 	}
1337c657876SArnaldo Carvalho de Melo 	return -ENOBUFS;
1347c657876SArnaldo Carvalho de Melo }
1357c657876SArnaldo Carvalho de Melo 
1367c657876SArnaldo Carvalho de Melo unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu)
1377c657876SArnaldo Carvalho de Melo {
138d83d8461SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1397c657876SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
140d83d8461SArnaldo Carvalho de Melo 	int mss_now = (pmtu - icsk->icsk_af_ops->net_header_len -
14157cca05aSArnaldo Carvalho de Melo 		       sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext));
1427c657876SArnaldo Carvalho de Melo 
1437c657876SArnaldo Carvalho de Melo 	/* Now subtract optional transport overhead */
144d83d8461SArnaldo Carvalho de Melo 	mss_now -= icsk->icsk_ext_hdr_len;
1457c657876SArnaldo Carvalho de Melo 
1467c657876SArnaldo Carvalho de Melo 	/*
1477c657876SArnaldo Carvalho de Melo 	 * FIXME: this should come from the CCID infrastructure, where, say,
1487c657876SArnaldo Carvalho de Melo 	 * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets
1497c657876SArnaldo Carvalho de Melo 	 * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED
1507c657876SArnaldo Carvalho de Melo 	 * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to
1517c657876SArnaldo Carvalho de Melo 	 * make it a multiple of 4
1527c657876SArnaldo Carvalho de Melo 	 */
1537c657876SArnaldo Carvalho de Melo 
1547c657876SArnaldo Carvalho de Melo 	mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
1557c657876SArnaldo Carvalho de Melo 
1567c657876SArnaldo Carvalho de Melo 	/* And store cached results */
157d83d8461SArnaldo Carvalho de Melo 	icsk->icsk_pmtu_cookie = pmtu;
1587c657876SArnaldo Carvalho de Melo 	dp->dccps_mss_cache = mss_now;
1597c657876SArnaldo Carvalho de Melo 
1607c657876SArnaldo Carvalho de Melo 	return mss_now;
1617c657876SArnaldo Carvalho de Melo }
1627c657876SArnaldo Carvalho de Melo 
163f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sync_mss);
164f21e68caSArnaldo Carvalho de Melo 
165c530cfb1SArnaldo Carvalho de Melo void dccp_write_space(struct sock *sk)
166c530cfb1SArnaldo Carvalho de Melo {
167c530cfb1SArnaldo Carvalho de Melo 	read_lock(&sk->sk_callback_lock);
168c530cfb1SArnaldo Carvalho de Melo 
169c530cfb1SArnaldo Carvalho de Melo 	if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
170c530cfb1SArnaldo Carvalho de Melo 		wake_up_interruptible(sk->sk_sleep);
171c530cfb1SArnaldo Carvalho de Melo 	/* Should agree with poll, otherwise some programs break */
172c530cfb1SArnaldo Carvalho de Melo 	if (sock_writeable(sk))
173c530cfb1SArnaldo Carvalho de Melo 		sk_wake_async(sk, 2, POLL_OUT);
174c530cfb1SArnaldo Carvalho de Melo 
175c530cfb1SArnaldo Carvalho de Melo 	read_unlock(&sk->sk_callback_lock);
176c530cfb1SArnaldo Carvalho de Melo }
177c530cfb1SArnaldo Carvalho de Melo 
178d6809c12SArnaldo Carvalho de Melo /**
179d6809c12SArnaldo Carvalho de Melo  * dccp_wait_for_ccid - Wait for ccid to tell us we can send a packet
180d6809c12SArnaldo Carvalho de Melo  * @sk: socket to wait for
181d6809c12SArnaldo Carvalho de Melo  */
1825cc3741dSIan McDonald static int dccp_wait_for_ccid(struct sock *sk, struct sk_buff *skb)
183d6809c12SArnaldo Carvalho de Melo {
184d6809c12SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
185d6809c12SArnaldo Carvalho de Melo 	DEFINE_WAIT(wait);
1865cc3741dSIan McDonald 	unsigned long delay;
187d6809c12SArnaldo Carvalho de Melo 	int rc;
188d6809c12SArnaldo Carvalho de Melo 
189d6809c12SArnaldo Carvalho de Melo 	while (1) {
190d6809c12SArnaldo Carvalho de Melo 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
191d6809c12SArnaldo Carvalho de Melo 
19297e5848dSIan McDonald 		if (sk->sk_err)
193d6809c12SArnaldo Carvalho de Melo 			goto do_error;
194d6809c12SArnaldo Carvalho de Melo 		if (signal_pending(current))
195d6809c12SArnaldo Carvalho de Melo 			goto do_interrupted;
196d6809c12SArnaldo Carvalho de Melo 
1976b57c93dSGerrit Renker 		rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb);
198d6809c12SArnaldo Carvalho de Melo 		if (rc <= 0)
199d6809c12SArnaldo Carvalho de Melo 			break;
2006626e362SGerrit Renker 		dccp_pr_debug("delayed send by %d msec\n", rc);
201d6809c12SArnaldo Carvalho de Melo 		delay = msecs_to_jiffies(rc);
202d6809c12SArnaldo Carvalho de Melo 		sk->sk_write_pending++;
203d6809c12SArnaldo Carvalho de Melo 		release_sock(sk);
2045cc3741dSIan McDonald 		schedule_timeout(delay);
205d6809c12SArnaldo Carvalho de Melo 		lock_sock(sk);
206d6809c12SArnaldo Carvalho de Melo 		sk->sk_write_pending--;
207d6809c12SArnaldo Carvalho de Melo 	}
208d6809c12SArnaldo Carvalho de Melo out:
209d6809c12SArnaldo Carvalho de Melo 	finish_wait(sk->sk_sleep, &wait);
210d6809c12SArnaldo Carvalho de Melo 	return rc;
211d6809c12SArnaldo Carvalho de Melo 
212d6809c12SArnaldo Carvalho de Melo do_error:
213d6809c12SArnaldo Carvalho de Melo 	rc = -EPIPE;
214d6809c12SArnaldo Carvalho de Melo 	goto out;
215d6809c12SArnaldo Carvalho de Melo do_interrupted:
2165cc3741dSIan McDonald 	rc = -EINTR;
217d6809c12SArnaldo Carvalho de Melo 	goto out;
218d6809c12SArnaldo Carvalho de Melo }
219d6809c12SArnaldo Carvalho de Melo 
22097e5848dSIan McDonald void dccp_write_xmit(struct sock *sk, int block)
22127258ee5SArnaldo Carvalho de Melo {
22297e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
22397e5848dSIan McDonald 	struct sk_buff *skb;
22497e5848dSIan McDonald 
22597e5848dSIan McDonald 	while ((skb = skb_peek(&sk->sk_write_queue))) {
2266b57c93dSGerrit Renker 		int err = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb);
227d6809c12SArnaldo Carvalho de Melo 
22897e5848dSIan McDonald 		if (err > 0) {
22997e5848dSIan McDonald 			if (!block) {
23097e5848dSIan McDonald 				sk_reset_timer(sk, &dp->dccps_xmit_timer,
23197e5848dSIan McDonald 						msecs_to_jiffies(err)+jiffies);
23297e5848dSIan McDonald 				break;
2335cc3741dSIan McDonald 			} else
2345cc3741dSIan McDonald 				err = dccp_wait_for_ccid(sk, skb);
235f6282f4dSGerrit Renker 			if (err && err != -EINTR)
23659348b19SGerrit Renker 				DCCP_BUG("err=%d after dccp_wait_for_ccid", err);
23797e5848dSIan McDonald 		}
23827258ee5SArnaldo Carvalho de Melo 
23997e5848dSIan McDonald 		skb_dequeue(&sk->sk_write_queue);
24027258ee5SArnaldo Carvalho de Melo 		if (err == 0) {
24127258ee5SArnaldo Carvalho de Melo 			struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
242d6809c12SArnaldo Carvalho de Melo 			const int len = skb->len;
24327258ee5SArnaldo Carvalho de Melo 
24427258ee5SArnaldo Carvalho de Melo 			if (sk->sk_state == DCCP_PARTOPEN) {
24527258ee5SArnaldo Carvalho de Melo 				/* See 8.1.5.  Handshake Completion */
24627258ee5SArnaldo Carvalho de Melo 				inet_csk_schedule_ack(sk);
24727258ee5SArnaldo Carvalho de Melo 				inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
24827258ee5SArnaldo Carvalho de Melo 						  inet_csk(sk)->icsk_rto,
24927258ee5SArnaldo Carvalho de Melo 						  DCCP_RTO_MAX);
25027258ee5SArnaldo Carvalho de Melo 				dcb->dccpd_type = DCCP_PKT_DATAACK;
251ae31c339SArnaldo Carvalho de Melo 			} else if (dccp_ack_pending(sk))
25227258ee5SArnaldo Carvalho de Melo 				dcb->dccpd_type = DCCP_PKT_DATAACK;
25327258ee5SArnaldo Carvalho de Melo 			else
25427258ee5SArnaldo Carvalho de Melo 				dcb->dccpd_type = DCCP_PKT_DATA;
25527258ee5SArnaldo Carvalho de Melo 
25627258ee5SArnaldo Carvalho de Melo 			err = dccp_transmit_skb(sk, skb);
25727258ee5SArnaldo Carvalho de Melo 			ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, 0, len);
25859348b19SGerrit Renker 			if (err)
25959348b19SGerrit Renker 				DCCP_BUG("err=%d after ccid_hc_tx_packet_sent",
26059348b19SGerrit Renker 					 err);
261f6282f4dSGerrit Renker 		} else {
2626626e362SGerrit Renker 			dccp_pr_debug("packet discarded due to err=%d\n", err);
263b08d5840SPatrick McHardy 			kfree_skb(skb);
26497e5848dSIan McDonald 		}
26527258ee5SArnaldo Carvalho de Melo 	}
266f6282f4dSGerrit Renker }
26727258ee5SArnaldo Carvalho de Melo 
2687c657876SArnaldo Carvalho de Melo int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
2697c657876SArnaldo Carvalho de Melo {
27057cca05aSArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk) != 0)
2717c657876SArnaldo Carvalho de Melo 		return -EHOSTUNREACH; /* Routing failure or similar. */
2727c657876SArnaldo Carvalho de Melo 
2737c657876SArnaldo Carvalho de Melo 	return dccp_transmit_skb(sk, (skb_cloned(skb) ?
2747c657876SArnaldo Carvalho de Melo 				      pskb_copy(skb, GFP_ATOMIC):
2757c657876SArnaldo Carvalho de Melo 				      skb_clone(skb, GFP_ATOMIC)));
2767c657876SArnaldo Carvalho de Melo }
2777c657876SArnaldo Carvalho de Melo 
2787c657876SArnaldo Carvalho de Melo struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
2797c657876SArnaldo Carvalho de Melo 				   struct request_sock *req)
2807c657876SArnaldo Carvalho de Melo {
2817c657876SArnaldo Carvalho de Melo 	struct dccp_hdr *dh;
28267e6b629SArnaldo Carvalho de Melo 	struct dccp_request_sock *dreq;
283118b2c95SArnaldo Carvalho de Melo 	const u32 dccp_header_size = sizeof(struct dccp_hdr) +
2847c657876SArnaldo Carvalho de Melo 				     sizeof(struct dccp_hdr_ext) +
2857c657876SArnaldo Carvalho de Melo 				     sizeof(struct dccp_hdr_response);
286118b2c95SArnaldo Carvalho de Melo 	struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1,
2877c657876SArnaldo Carvalho de Melo 					   GFP_ATOMIC);
2887c657876SArnaldo Carvalho de Melo 	if (skb == NULL)
2897c657876SArnaldo Carvalho de Melo 		return NULL;
2907c657876SArnaldo Carvalho de Melo 
2917c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers. */
292118b2c95SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
2937c657876SArnaldo Carvalho de Melo 
2947c657876SArnaldo Carvalho de Melo 	skb->dst = dst_clone(dst);
2957c657876SArnaldo Carvalho de Melo 
29667e6b629SArnaldo Carvalho de Melo 	dreq = dccp_rsk(req);
297e11d9d30SGerrit Renker 	if (inet_rsk(req)->acked)	/* increase ISS upon retransmission */
298e11d9d30SGerrit Renker 		dccp_inc_seqno(&dreq->dreq_iss);
2997c657876SArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE;
30067e6b629SArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_seq  = dreq->dreq_iss;
3012d0817d1SArnaldo Carvalho de Melo 
3022d0817d1SArnaldo Carvalho de Melo 	if (dccp_insert_options(sk, skb)) {
3032d0817d1SArnaldo Carvalho de Melo 		kfree_skb(skb);
3042d0817d1SArnaldo Carvalho de Melo 		return NULL;
3052d0817d1SArnaldo Carvalho de Melo 	}
3067c657876SArnaldo Carvalho de Melo 
30709dbc389SGerrit Renker 	/* Build and checksum header */
3089b42078eSGerrit Renker 	dh = dccp_zeroed_hdr(skb, dccp_header_size);
3097c657876SArnaldo Carvalho de Melo 
3107c657876SArnaldo Carvalho de Melo 	dh->dccph_sport	= inet_sk(sk)->sport;
3117c657876SArnaldo Carvalho de Melo 	dh->dccph_dport	= inet_rsk(req)->rmt_port;
3127690af3fSArnaldo Carvalho de Melo 	dh->dccph_doff	= (dccp_header_size +
3137690af3fSArnaldo Carvalho de Melo 			   DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
3147c657876SArnaldo Carvalho de Melo 	dh->dccph_type	= DCCP_PKT_RESPONSE;
3157c657876SArnaldo Carvalho de Melo 	dh->dccph_x	= 1;
31667e6b629SArnaldo Carvalho de Melo 	dccp_hdr_set_seq(dh, dreq->dreq_iss);
31767e6b629SArnaldo Carvalho de Melo 	dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dreq->dreq_isr);
31867e6b629SArnaldo Carvalho de Melo 	dccp_hdr_response(skb)->dccph_resp_service = dreq->dreq_service;
3197c657876SArnaldo Carvalho de Melo 
3206f4e5fffSGerrit Renker 	dccp_csum_outgoing(skb);
3216f4e5fffSGerrit Renker 
322e11d9d30SGerrit Renker 	/* We use `acked' to remember that a Response was already sent. */
323e11d9d30SGerrit Renker 	inet_rsk(req)->acked = 1;
3247c657876SArnaldo Carvalho de Melo 	DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
3257c657876SArnaldo Carvalho de Melo 	return skb;
3267c657876SArnaldo Carvalho de Melo }
3277c657876SArnaldo Carvalho de Melo 
328f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_make_response);
329f21e68caSArnaldo Carvalho de Melo 
330ee1a1592SGerrit Renker /* send Reset on established socket, to close or abort the connection */
331017487d7SArnaldo Carvalho de Melo int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code)
332017487d7SArnaldo Carvalho de Melo {
333ee1a1592SGerrit Renker 	struct sk_buff *skb;
334017487d7SArnaldo Carvalho de Melo 	/*
335017487d7SArnaldo Carvalho de Melo 	 * FIXME: what if rebuild_header fails?
336017487d7SArnaldo Carvalho de Melo 	 * Should we be doing a rebuild_header here?
337017487d7SArnaldo Carvalho de Melo 	 */
338017487d7SArnaldo Carvalho de Melo 	int err = inet_sk_rebuild_header(sk);
339017487d7SArnaldo Carvalho de Melo 
340ee1a1592SGerrit Renker 	if (err != 0)
341017487d7SArnaldo Carvalho de Melo 		return err;
342ee1a1592SGerrit Renker 
343ee1a1592SGerrit Renker 	skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, GFP_ATOMIC);
344ee1a1592SGerrit Renker 	if (skb == NULL)
345ee1a1592SGerrit Renker 		return -ENOBUFS;
346ee1a1592SGerrit Renker 
347ee1a1592SGerrit Renker 	/* Reserve space for headers and prepare control bits. */
348ee1a1592SGerrit Renker 	skb_reserve(skb, sk->sk_prot->max_header);
349ee1a1592SGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_type	   = DCCP_PKT_RESET;
350ee1a1592SGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_reset_code = code;
351ee1a1592SGerrit Renker 
352ee1a1592SGerrit Renker 	return dccp_transmit_skb(sk, skb);
353017487d7SArnaldo Carvalho de Melo }
354017487d7SArnaldo Carvalho de Melo 
3557c657876SArnaldo Carvalho de Melo /*
3567c657876SArnaldo Carvalho de Melo  * Do all connect socket setups that can be done AF independent.
3577c657876SArnaldo Carvalho de Melo  */
3587c657876SArnaldo Carvalho de Melo static inline void dccp_connect_init(struct sock *sk)
3597c657876SArnaldo Carvalho de Melo {
360f21e68caSArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
3617c657876SArnaldo Carvalho de Melo 	struct dst_entry *dst = __sk_dst_get(sk);
3627c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
3637c657876SArnaldo Carvalho de Melo 
3647c657876SArnaldo Carvalho de Melo 	sk->sk_err = 0;
3657c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
3667c657876SArnaldo Carvalho de Melo 
3677c657876SArnaldo Carvalho de Melo 	dccp_sync_mss(sk, dst_mtu(dst));
3687c657876SArnaldo Carvalho de Melo 
3697c657876SArnaldo Carvalho de Melo 	/*
370f21e68caSArnaldo Carvalho de Melo 	 * SWL and AWL are initially adjusted so that they are not less than
371f21e68caSArnaldo Carvalho de Melo 	 * the initial Sequence Numbers received and sent, respectively:
372f21e68caSArnaldo Carvalho de Melo 	 *	SWL := max(GSR + 1 - floor(W/4), ISR),
373f21e68caSArnaldo Carvalho de Melo 	 *	AWL := max(GSS - W' + 1, ISS).
374f21e68caSArnaldo Carvalho de Melo 	 * These adjustments MUST be applied only at the beginning of the
375f21e68caSArnaldo Carvalho de Melo 	 * connection.
3767c657876SArnaldo Carvalho de Melo 	 */
377d7f7365fSGerrit Renker 	dccp_update_gss(sk, dp->dccps_iss);
378f21e68caSArnaldo Carvalho de Melo 	dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
3797c657876SArnaldo Carvalho de Melo 
380d7f7365fSGerrit Renker 	/* S.GAR - greatest valid acknowledgement number received on a non-Sync;
381d7f7365fSGerrit Renker 	 *         initialized to S.ISS (sec. 8.5)                            */
382d7f7365fSGerrit Renker 	dp->dccps_gar = dp->dccps_iss;
383d7f7365fSGerrit Renker 
3847c657876SArnaldo Carvalho de Melo 	icsk->icsk_retransmits = 0;
3857c657876SArnaldo Carvalho de Melo }
3867c657876SArnaldo Carvalho de Melo 
3877c657876SArnaldo Carvalho de Melo int dccp_connect(struct sock *sk)
3887c657876SArnaldo Carvalho de Melo {
3897c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
3907c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
3917c657876SArnaldo Carvalho de Melo 
3927c657876SArnaldo Carvalho de Melo 	dccp_connect_init(sk);
3937c657876SArnaldo Carvalho de Melo 
394118b2c95SArnaldo Carvalho de Melo 	skb = alloc_skb(sk->sk_prot->max_header, sk->sk_allocation);
3957c657876SArnaldo Carvalho de Melo 	if (unlikely(skb == NULL))
3967c657876SArnaldo Carvalho de Melo 		return -ENOBUFS;
3977c657876SArnaldo Carvalho de Melo 
3987c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers. */
399118b2c95SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
4007c657876SArnaldo Carvalho de Melo 
4017c657876SArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
4027c657876SArnaldo Carvalho de Melo 
40348918a4dSHerbert Xu 	dccp_skb_entail(sk, skb);
4047c657876SArnaldo Carvalho de Melo 	dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL));
4057c657876SArnaldo Carvalho de Melo 	DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS);
4067c657876SArnaldo Carvalho de Melo 
4077c657876SArnaldo Carvalho de Melo 	/* Timer for repeating the REQUEST until an answer. */
40827258ee5SArnaldo Carvalho de Melo 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
40927258ee5SArnaldo Carvalho de Melo 				  icsk->icsk_rto, DCCP_RTO_MAX);
4107c657876SArnaldo Carvalho de Melo 	return 0;
4117c657876SArnaldo Carvalho de Melo }
4127c657876SArnaldo Carvalho de Melo 
413f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_connect);
414f21e68caSArnaldo Carvalho de Melo 
4157c657876SArnaldo Carvalho de Melo void dccp_send_ack(struct sock *sk)
4167c657876SArnaldo Carvalho de Melo {
4177c657876SArnaldo Carvalho de Melo 	/* If we have been reset, we may not send again. */
4187c657876SArnaldo Carvalho de Melo 	if (sk->sk_state != DCCP_CLOSED) {
419118b2c95SArnaldo Carvalho de Melo 		struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header,
420118b2c95SArnaldo Carvalho de Melo 						GFP_ATOMIC);
4217c657876SArnaldo Carvalho de Melo 
4227c657876SArnaldo Carvalho de Melo 		if (skb == NULL) {
4237c657876SArnaldo Carvalho de Melo 			inet_csk_schedule_ack(sk);
4247c657876SArnaldo Carvalho de Melo 			inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
4257690af3fSArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
4267690af3fSArnaldo Carvalho de Melo 						  TCP_DELACK_MAX,
4277690af3fSArnaldo Carvalho de Melo 						  DCCP_RTO_MAX);
4287c657876SArnaldo Carvalho de Melo 			return;
4297c657876SArnaldo Carvalho de Melo 		}
4307c657876SArnaldo Carvalho de Melo 
4317c657876SArnaldo Carvalho de Melo 		/* Reserve space for headers */
432118b2c95SArnaldo Carvalho de Melo 		skb_reserve(skb, sk->sk_prot->max_header);
4337c657876SArnaldo Carvalho de Melo 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
4347c657876SArnaldo Carvalho de Melo 		dccp_transmit_skb(sk, skb);
4357c657876SArnaldo Carvalho de Melo 	}
4367c657876SArnaldo Carvalho de Melo }
4377c657876SArnaldo Carvalho de Melo 
4387c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_ack);
4397c657876SArnaldo Carvalho de Melo 
440727ecc5fSGerrit Renker /* FIXME: Is this still necessary (11.3) - currently nowhere used by DCCP. */
4417c657876SArnaldo Carvalho de Melo void dccp_send_delayed_ack(struct sock *sk)
4427c657876SArnaldo Carvalho de Melo {
4437c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
4447c657876SArnaldo Carvalho de Melo 	/*
4457c657876SArnaldo Carvalho de Melo 	 * FIXME: tune this timer. elapsed time fixes the skew, so no problem
4467c657876SArnaldo Carvalho de Melo 	 * with using 2s, and active senders also piggyback the ACK into a
4477c657876SArnaldo Carvalho de Melo 	 * DATAACK packet, so this is really for quiescent senders.
4487c657876SArnaldo Carvalho de Melo 	 */
4497c657876SArnaldo Carvalho de Melo 	unsigned long timeout = jiffies + 2 * HZ;
4507c657876SArnaldo Carvalho de Melo 
4517c657876SArnaldo Carvalho de Melo 	/* Use new timeout only if there wasn't a older one earlier. */
4527c657876SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
4537c657876SArnaldo Carvalho de Melo 		/* If delack timer was blocked or is about to expire,
4547c657876SArnaldo Carvalho de Melo 		 * send ACK now.
4557c657876SArnaldo Carvalho de Melo 		 *
4567c657876SArnaldo Carvalho de Melo 		 * FIXME: check the "about to expire" part
4577c657876SArnaldo Carvalho de Melo 		 */
4587c657876SArnaldo Carvalho de Melo 		if (icsk->icsk_ack.blocked) {
4597c657876SArnaldo Carvalho de Melo 			dccp_send_ack(sk);
4607c657876SArnaldo Carvalho de Melo 			return;
4617c657876SArnaldo Carvalho de Melo 		}
4627c657876SArnaldo Carvalho de Melo 
4637c657876SArnaldo Carvalho de Melo 		if (!time_before(timeout, icsk->icsk_ack.timeout))
4647c657876SArnaldo Carvalho de Melo 			timeout = icsk->icsk_ack.timeout;
4657c657876SArnaldo Carvalho de Melo 	}
4667c657876SArnaldo Carvalho de Melo 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
4677c657876SArnaldo Carvalho de Melo 	icsk->icsk_ack.timeout = timeout;
4687c657876SArnaldo Carvalho de Melo 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
4697c657876SArnaldo Carvalho de Melo }
4707c657876SArnaldo Carvalho de Melo 
471b0d045caSGerrit Renker void dccp_send_sync(struct sock *sk, const u64 ackno,
472e92ae93aSArnaldo Carvalho de Melo 		    const enum dccp_pkt_type pkt_type)
4737c657876SArnaldo Carvalho de Melo {
4747c657876SArnaldo Carvalho de Melo 	/*
4757c657876SArnaldo Carvalho de Melo 	 * We are not putting this on the write queue, so
4767c657876SArnaldo Carvalho de Melo 	 * dccp_transmit_skb() will set the ownership to this
4777c657876SArnaldo Carvalho de Melo 	 * sock.
4787c657876SArnaldo Carvalho de Melo 	 */
479118b2c95SArnaldo Carvalho de Melo 	struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC);
4807c657876SArnaldo Carvalho de Melo 
481b0d045caSGerrit Renker 	if (skb == NULL) {
4827c657876SArnaldo Carvalho de Melo 		/* FIXME: how to make sure the sync is sent? */
483b0d045caSGerrit Renker 		DCCP_CRIT("could not send %s", dccp_packet_name(pkt_type));
4847c657876SArnaldo Carvalho de Melo 		return;
485b0d045caSGerrit Renker 	}
4867c657876SArnaldo Carvalho de Melo 
4877c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers and prepare control bits. */
488118b2c95SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
489e92ae93aSArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = pkt_type;
490b0d045caSGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_ack_seq = ackno;
4917c657876SArnaldo Carvalho de Melo 
4927c657876SArnaldo Carvalho de Melo 	dccp_transmit_skb(sk, skb);
4937c657876SArnaldo Carvalho de Melo }
4947c657876SArnaldo Carvalho de Melo 
495b61fafc4SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_sync);
496b61fafc4SArnaldo Carvalho de Melo 
4977690af3fSArnaldo Carvalho de Melo /*
4987690af3fSArnaldo Carvalho de Melo  * Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This
4997690af3fSArnaldo Carvalho de Melo  * cannot be allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under
5007690af3fSArnaldo Carvalho de Melo  * any circumstances.
5017c657876SArnaldo Carvalho de Melo  */
5027ad07e7cSArnaldo Carvalho de Melo void dccp_send_close(struct sock *sk, const int active)
5037c657876SArnaldo Carvalho de Melo {
5047c657876SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
5057c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
5067d877f3bSAl Viro 	const gfp_t prio = active ? GFP_KERNEL : GFP_ATOMIC;
5077c657876SArnaldo Carvalho de Melo 
5087ad07e7cSArnaldo Carvalho de Melo 	skb = alloc_skb(sk->sk_prot->max_header, prio);
5097ad07e7cSArnaldo Carvalho de Melo 	if (skb == NULL)
5107ad07e7cSArnaldo Carvalho de Melo 		return;
5117c657876SArnaldo Carvalho de Melo 
5127c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers and prepare control bits. */
5137c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
5147690af3fSArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ?
5157690af3fSArnaldo Carvalho de Melo 					DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ;
5167c657876SArnaldo Carvalho de Melo 
5177ad07e7cSArnaldo Carvalho de Melo 	if (active) {
51897e5848dSIan McDonald 		dccp_write_xmit(sk, 1);
51948918a4dSHerbert Xu 		dccp_skb_entail(sk, skb);
5207ad07e7cSArnaldo Carvalho de Melo 		dccp_transmit_skb(sk, skb_clone(skb, prio));
52197e5848dSIan McDonald 		/* FIXME do we need a retransmit timer here? */
5227ad07e7cSArnaldo Carvalho de Melo 	} else
5237c657876SArnaldo Carvalho de Melo 		dccp_transmit_skb(sk, skb);
5247c657876SArnaldo Carvalho de Melo }
525