xref: /openbmc/linux/net/dccp/output.c (revision a47e598f)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27c657876SArnaldo Carvalho de Melo /*
37c657876SArnaldo Carvalho de Melo  *  net/dccp/output.c
47c657876SArnaldo Carvalho de Melo  *
57c657876SArnaldo Carvalho de Melo  *  An implementation of the DCCP protocol
67c657876SArnaldo Carvalho de Melo  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
77c657876SArnaldo Carvalho de Melo  */
87c657876SArnaldo Carvalho de Melo 
97c657876SArnaldo Carvalho de Melo #include <linux/dccp.h>
1048918a4dSHerbert Xu #include <linux/kernel.h>
117c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13174cd4b1SIngo Molnar #include <linux/sched/signal.h>
147c657876SArnaldo Carvalho de Melo 
1514c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h>
167c657876SArnaldo Carvalho de Melo #include <net/sock.h>
177c657876SArnaldo Carvalho de Melo 
18ae31c339SArnaldo Carvalho de Melo #include "ackvec.h"
197c657876SArnaldo Carvalho de Melo #include "ccid.h"
207c657876SArnaldo Carvalho de Melo #include "dccp.h"
217c657876SArnaldo Carvalho de Melo 
dccp_event_ack_sent(struct sock * sk)227c657876SArnaldo Carvalho de Melo static inline void dccp_event_ack_sent(struct sock *sk)
237c657876SArnaldo Carvalho de Melo {
247c657876SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
257c657876SArnaldo Carvalho de Melo }
267c657876SArnaldo Carvalho de Melo 
278695e801SGerrit Renker /* enqueue @skb on sk_send_head for retransmission, return clone to send now */
dccp_skb_entail(struct sock * sk,struct sk_buff * skb)288695e801SGerrit Renker static struct sk_buff *dccp_skb_entail(struct sock *sk, struct sk_buff *skb)
2948918a4dSHerbert Xu {
3048918a4dSHerbert Xu 	skb_set_owner_w(skb, sk);
3148918a4dSHerbert Xu 	WARN_ON(sk->sk_send_head);
3248918a4dSHerbert Xu 	sk->sk_send_head = skb;
338695e801SGerrit Renker 	return skb_clone(sk->sk_send_head, gfp_any());
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  */
dccp_transmit_skb(struct sock * sk,struct sk_buff * skb)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)) {
45d9d8da80SDavid S. Miller 		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;
5673f18fdbSGerrit Renker 		/*
5773f18fdbSGerrit Renker 		 * Increment GSS here already in case the option code needs it.
5873f18fdbSGerrit Renker 		 * Update GSS for real only if option processing below succeeds.
5973f18fdbSGerrit Renker 		 */
6073f18fdbSGerrit Renker 		dcb->dccpd_seq = ADD48(dp->dccps_gss, 1);
617c657876SArnaldo Carvalho de Melo 
627c657876SArnaldo Carvalho de Melo 		switch (dcb->dccpd_type) {
637c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_DATA:
647c657876SArnaldo Carvalho de Melo 			set_ack = 0;
65df561f66SGustavo A. R. Silva 			fallthrough;
66edc9e819SHerbert Xu 		case DCCP_PKT_DATAACK:
67ee1a1592SGerrit Renker 		case DCCP_PKT_RESET:
687c657876SArnaldo Carvalho de Melo 			break;
69edc9e819SHerbert Xu 
70afe00251SAndrea Bittau 		case DCCP_PKT_REQUEST:
71afe00251SAndrea Bittau 			set_ack = 0;
7273f18fdbSGerrit Renker 			/* Use ISS on the first (non-retransmitted) Request. */
7373f18fdbSGerrit Renker 			if (icsk->icsk_retransmits == 0)
7473f18fdbSGerrit Renker 				dcb->dccpd_seq = dp->dccps_iss;
75df561f66SGustavo A. R. Silva 			fallthrough;
76afe00251SAndrea Bittau 
777c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_SYNC:
787c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_SYNCACK:
79b0d045caSGerrit Renker 			ackno = dcb->dccpd_ack_seq;
80df561f66SGustavo A. R. Silva 			fallthrough;
81edc9e819SHerbert Xu 		default:
82edc9e819SHerbert Xu 			/*
83ee1a1592SGerrit Renker 			 * Set owner/destructor: some skbs are allocated via
84ee1a1592SGerrit Renker 			 * alloc_skb (e.g. when retransmission may happen).
85ee1a1592SGerrit Renker 			 * Only Data, DataAck, and Reset packets should come
86ee1a1592SGerrit Renker 			 * through here with skb->sk set.
87edc9e819SHerbert Xu 			 */
88edc9e819SHerbert Xu 			WARN_ON(skb->sk);
89edc9e819SHerbert Xu 			skb_set_owner_w(skb, sk);
907c657876SArnaldo Carvalho de Melo 			break;
917c657876SArnaldo Carvalho de Melo 		}
927c657876SArnaldo Carvalho de Melo 
932d0817d1SArnaldo Carvalho de Melo 		if (dccp_insert_options(sk, skb)) {
942d0817d1SArnaldo Carvalho de Melo 			kfree_skb(skb);
952d0817d1SArnaldo Carvalho de Melo 			return -EPROTO;
962d0817d1SArnaldo Carvalho de Melo 		}
9724117727SArnaldo Carvalho de Melo 
98fda0fd6cSHerbert Xu 
997c657876SArnaldo Carvalho de Melo 		/* Build DCCP header and checksum it. */
1009b42078eSGerrit Renker 		dh = dccp_zeroed_hdr(skb, dccp_header_size);
1017c657876SArnaldo Carvalho de Melo 		dh->dccph_type	= dcb->dccpd_type;
102c720c7e8SEric Dumazet 		dh->dccph_sport	= inet->inet_sport;
103c720c7e8SEric Dumazet 		dh->dccph_dport	= inet->inet_dport;
1047c657876SArnaldo Carvalho de Melo 		dh->dccph_doff	= (dccp_header_size + dcb->dccpd_opt_len) / 4;
1057c657876SArnaldo Carvalho de Melo 		dh->dccph_ccval	= dcb->dccpd_ccval;
1066f4e5fffSGerrit Renker 		dh->dccph_cscov = dp->dccps_pcslen;
1077c657876SArnaldo Carvalho de Melo 		/* XXX For now we're using only 48 bits sequence numbers */
1087c657876SArnaldo Carvalho de Melo 		dh->dccph_x	= 1;
1097c657876SArnaldo Carvalho de Melo 
11073f18fdbSGerrit Renker 		dccp_update_gss(sk, dcb->dccpd_seq);
1117c657876SArnaldo Carvalho de Melo 		dccp_hdr_set_seq(dh, dp->dccps_gss);
1127c657876SArnaldo Carvalho de Melo 		if (set_ack)
1137c657876SArnaldo Carvalho de Melo 			dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
1147c657876SArnaldo Carvalho de Melo 
1157c657876SArnaldo Carvalho de Melo 		switch (dcb->dccpd_type) {
1167c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_REQUEST:
1177690af3fSArnaldo Carvalho de Melo 			dccp_hdr_request(skb)->dccph_req_service =
11867e6b629SArnaldo Carvalho de Melo 							dp->dccps_service;
11973f18fdbSGerrit Renker 			/*
12073f18fdbSGerrit Renker 			 * Limit Ack window to ISS <= P.ackno <= GSS, so that
12173f18fdbSGerrit Renker 			 * only Responses to Requests we sent are considered.
12273f18fdbSGerrit Renker 			 */
12373f18fdbSGerrit Renker 			dp->dccps_awl = dp->dccps_iss;
1247c657876SArnaldo Carvalho de Melo 			break;
1257c657876SArnaldo Carvalho de Melo 		case DCCP_PKT_RESET:
1267690af3fSArnaldo Carvalho de Melo 			dccp_hdr_reset(skb)->dccph_reset_code =
1277690af3fSArnaldo Carvalho de Melo 							dcb->dccpd_reset_code;
1287c657876SArnaldo Carvalho de Melo 			break;
1297c657876SArnaldo Carvalho de Melo 		}
1307c657876SArnaldo Carvalho de Melo 
131bb296246SHerbert Xu 		icsk->icsk_af_ops->send_check(sk, skb);
1327c657876SArnaldo Carvalho de Melo 
1337ad07e7cSArnaldo Carvalho de Melo 		if (set_ack)
1347c657876SArnaldo Carvalho de Melo 			dccp_event_ack_sent(sk);
1357c657876SArnaldo Carvalho de Melo 
1367c657876SArnaldo Carvalho de Melo 		DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
1377c657876SArnaldo Carvalho de Melo 
138b0270e91SEric Dumazet 		err = icsk->icsk_af_ops->queue_xmit(sk, skb, &inet->cork.fl);
139b9df3cb8SGerrit Renker 		return net_xmit_eval(err);
1407c657876SArnaldo Carvalho de Melo 	}
1417c657876SArnaldo Carvalho de Melo 	return -ENOBUFS;
1427c657876SArnaldo Carvalho de Melo }
1437c657876SArnaldo Carvalho de Melo 
1446179983aSGerrit Renker /**
14525985edcSLucas De Marchi  * dccp_determine_ccmps  -  Find out about CCID-specific packet-size limits
1460b609b55SAndrew Lunn  * @dp: socket to find packet size limits of
1470b609b55SAndrew Lunn  *
1486179983aSGerrit Renker  * We only consider the HC-sender CCID for setting the CCMPS (RFC 4340, 14.),
1496179983aSGerrit Renker  * since the RX CCID is restricted to feedback packets (Acks), which are small
1506179983aSGerrit Renker  * in comparison with the data traffic. A value of 0 means "no current CCMPS".
1516179983aSGerrit Renker  */
dccp_determine_ccmps(const struct dccp_sock * dp)1526179983aSGerrit Renker static u32 dccp_determine_ccmps(const struct dccp_sock *dp)
1536179983aSGerrit Renker {
1546179983aSGerrit Renker 	const struct ccid *tx_ccid = dp->dccps_hc_tx_ccid;
1556179983aSGerrit Renker 
1566179983aSGerrit Renker 	if (tx_ccid == NULL || tx_ccid->ccid_ops == NULL)
1576179983aSGerrit Renker 		return 0;
1586179983aSGerrit Renker 	return tx_ccid->ccid_ops->ccid_ccmps;
1596179983aSGerrit Renker }
1606179983aSGerrit Renker 
dccp_sync_mss(struct sock * sk,u32 pmtu)1617c657876SArnaldo Carvalho de Melo unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu)
1627c657876SArnaldo Carvalho de Melo {
163d83d8461SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1647c657876SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
1656179983aSGerrit Renker 	u32 ccmps = dccp_determine_ccmps(dp);
166361a5c1dSGerrit Renker 	u32 cur_mps = ccmps ? min(pmtu, ccmps) : pmtu;
1677c657876SArnaldo Carvalho de Melo 
1686179983aSGerrit Renker 	/* Account for header lengths and IPv4/v6 option overhead */
1696179983aSGerrit Renker 	cur_mps -= (icsk->icsk_af_ops->net_header_len + icsk->icsk_ext_hdr_len +
1706179983aSGerrit Renker 		    sizeof(struct dccp_hdr) + sizeof(struct dccp_hdr_ext));
1717c657876SArnaldo Carvalho de Melo 
1727c657876SArnaldo Carvalho de Melo 	/*
173361a5c1dSGerrit Renker 	 * Leave enough headroom for common DCCP header options.
174361a5c1dSGerrit Renker 	 * This only considers options which may appear on DCCP-Data packets, as
175361a5c1dSGerrit Renker 	 * per table 3 in RFC 4340, 5.8. When running out of space for other
176361a5c1dSGerrit Renker 	 * options (eg. Ack Vector which can take up to 255 bytes), it is better
177361a5c1dSGerrit Renker 	 * to schedule a separate Ack. Thus we leave headroom for the following:
178361a5c1dSGerrit Renker 	 *  - 1 byte for Slow Receiver (11.6)
179361a5c1dSGerrit Renker 	 *  - 6 bytes for Timestamp (13.1)
180361a5c1dSGerrit Renker 	 *  - 10 bytes for Timestamp Echo (13.3)
181361a5c1dSGerrit Renker 	 *  - 8 bytes for NDP count (7.7, when activated)
182361a5c1dSGerrit Renker 	 *  - 6 bytes for Data Checksum (9.3)
183361a5c1dSGerrit Renker 	 *  - %DCCPAV_MIN_OPTLEN bytes for Ack Vector size (11.4, when enabled)
1847c657876SArnaldo Carvalho de Melo 	 */
185361a5c1dSGerrit Renker 	cur_mps -= roundup(1 + 6 + 10 + dp->dccps_send_ndp_count * 8 + 6 +
186361a5c1dSGerrit Renker 			   (dp->dccps_hc_rx_ackvec ? DCCPAV_MIN_OPTLEN : 0), 4);
1877c657876SArnaldo Carvalho de Melo 
1887c657876SArnaldo Carvalho de Melo 	/* And store cached results */
189d83d8461SArnaldo Carvalho de Melo 	icsk->icsk_pmtu_cookie = pmtu;
190*a47e598fSEric Dumazet 	WRITE_ONCE(dp->dccps_mss_cache, cur_mps);
1917c657876SArnaldo Carvalho de Melo 
1926179983aSGerrit Renker 	return cur_mps;
1937c657876SArnaldo Carvalho de Melo }
1947c657876SArnaldo Carvalho de Melo 
195f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sync_mss);
196f21e68caSArnaldo Carvalho de Melo 
dccp_write_space(struct sock * sk)197c530cfb1SArnaldo Carvalho de Melo void dccp_write_space(struct sock *sk)
198c530cfb1SArnaldo Carvalho de Melo {
19943815482SEric Dumazet 	struct socket_wq *wq;
200c530cfb1SArnaldo Carvalho de Melo 
20143815482SEric Dumazet 	rcu_read_lock();
20243815482SEric Dumazet 	wq = rcu_dereference(sk->sk_wq);
2031ce0bf50SHerbert Xu 	if (skwq_has_sleeper(wq))
20443815482SEric Dumazet 		wake_up_interruptible(&wq->wait);
205c530cfb1SArnaldo Carvalho de Melo 	/* Should agree with poll, otherwise some programs break */
206c530cfb1SArnaldo Carvalho de Melo 	if (sock_writeable(sk))
2078d8ad9d7SPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
208c530cfb1SArnaldo Carvalho de Melo 
20943815482SEric Dumazet 	rcu_read_unlock();
210c530cfb1SArnaldo Carvalho de Melo }
211c530cfb1SArnaldo Carvalho de Melo 
212d6809c12SArnaldo Carvalho de Melo /**
213b1fcf55eSGerrit Renker  * dccp_wait_for_ccid  -  Await CCID send permission
214d6809c12SArnaldo Carvalho de Melo  * @sk:    socket to wait for
215b1fcf55eSGerrit Renker  * @delay: timeout in jiffies
2162c53040fSBen Hutchings  *
217b1fcf55eSGerrit Renker  * This is used by CCIDs which need to delay the send time in process context.
218d6809c12SArnaldo Carvalho de Melo  */
dccp_wait_for_ccid(struct sock * sk,unsigned long delay)219b1fcf55eSGerrit Renker static int dccp_wait_for_ccid(struct sock *sk, unsigned long delay)
220d6809c12SArnaldo Carvalho de Melo {
221d6809c12SArnaldo Carvalho de Melo 	DEFINE_WAIT(wait);
222b1fcf55eSGerrit Renker 	long remaining;
223bc849872SGerrit Renker 
224aa395145SEric Dumazet 	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
225bc849872SGerrit Renker 	sk->sk_write_pending++;
226bc849872SGerrit Renker 	release_sock(sk);
227b1fcf55eSGerrit Renker 
228b1fcf55eSGerrit Renker 	remaining = schedule_timeout(delay);
229b1fcf55eSGerrit Renker 
230bc849872SGerrit Renker 	lock_sock(sk);
231bc849872SGerrit Renker 	sk->sk_write_pending--;
232aa395145SEric Dumazet 	finish_wait(sk_sleep(sk), &wait);
233d6809c12SArnaldo Carvalho de Melo 
234b1fcf55eSGerrit Renker 	if (signal_pending(current) || sk->sk_err)
235b1fcf55eSGerrit Renker 		return -1;
236b1fcf55eSGerrit Renker 	return remaining;
237d6809c12SArnaldo Carvalho de Melo }
238d6809c12SArnaldo Carvalho de Melo 
239dc841e30SGerrit Renker /**
240dc841e30SGerrit Renker  * dccp_xmit_packet  -  Send data packet under control of CCID
2410b609b55SAndrew Lunn  * @sk: socket to send data packet on
2420b609b55SAndrew Lunn  *
243dc841e30SGerrit Renker  * Transmits next-queued payload and informs CCID to account for the packet.
244dc841e30SGerrit Renker  */
dccp_xmit_packet(struct sock * sk)245dc841e30SGerrit Renker static void dccp_xmit_packet(struct sock *sk)
24627258ee5SArnaldo Carvalho de Melo {
247dc841e30SGerrit Renker 	int err, len;
24897e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
249871a2c16STomasz Grobelny 	struct sk_buff *skb = dccp_qpolicy_pop(sk);
25097e5848dSIan McDonald 
251dc841e30SGerrit Renker 	if (unlikely(skb == NULL))
252dc841e30SGerrit Renker 		return;
253dc841e30SGerrit Renker 	len = skb->len;
25427258ee5SArnaldo Carvalho de Melo 
25527258ee5SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_PARTOPEN) {
25686739fb9SGerrit Renker 		const u32 cur_mps = dp->dccps_mss_cache - DCCP_FEATNEG_OVERHEAD;
25786739fb9SGerrit Renker 		/*
25886739fb9SGerrit Renker 		 * See 8.1.5 - Handshake Completion.
25986739fb9SGerrit Renker 		 *
26086739fb9SGerrit Renker 		 * For robustness we resend Confirm options until the client has
26186739fb9SGerrit Renker 		 * entered OPEN. During the initial feature negotiation, the MPS
26286739fb9SGerrit Renker 		 * is smaller than usual, reduced by the Change/Confirm options.
26386739fb9SGerrit Renker 		 */
26486739fb9SGerrit Renker 		if (!list_empty(&dp->dccps_featneg) && len > cur_mps) {
26586739fb9SGerrit Renker 			DCCP_WARN("Payload too large (%d) for featneg.\n", len);
26686739fb9SGerrit Renker 			dccp_send_ack(sk);
26786739fb9SGerrit Renker 			dccp_feat_list_purge(&dp->dccps_featneg);
26886739fb9SGerrit Renker 		}
26986739fb9SGerrit Renker 
27027258ee5SArnaldo Carvalho de Melo 		inet_csk_schedule_ack(sk);
27127258ee5SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
27227258ee5SArnaldo Carvalho de Melo 					      inet_csk(sk)->icsk_rto,
27327258ee5SArnaldo Carvalho de Melo 					      DCCP_RTO_MAX);
274dc841e30SGerrit Renker 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_DATAACK;
275dc841e30SGerrit Renker 	} else if (dccp_ack_pending(sk)) {
276dc841e30SGerrit Renker 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_DATAACK;
277dc841e30SGerrit Renker 	} else {
278dc841e30SGerrit Renker 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_DATA;
279dc841e30SGerrit Renker 	}
28027258ee5SArnaldo Carvalho de Melo 
28127258ee5SArnaldo Carvalho de Melo 	err = dccp_transmit_skb(sk, skb);
28259348b19SGerrit Renker 	if (err)
283dc841e30SGerrit Renker 		dccp_pr_debug("transmit_skb() returned err=%d\n", err);
284dc841e30SGerrit Renker 	/*
285dc841e30SGerrit Renker 	 * Register this one as sent even if an error occurred. To the remote
286dc841e30SGerrit Renker 	 * end a local packet drop is indistinguishable from network loss, i.e.
287dc841e30SGerrit Renker 	 * any local drop will eventually be reported via receiver feedback.
288dc841e30SGerrit Renker 	 */
289dc841e30SGerrit Renker 	ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, len);
290d83447f0SGerrit Renker 
291d83447f0SGerrit Renker 	/*
292d83447f0SGerrit Renker 	 * If the CCID needs to transfer additional header options out-of-band
293d83447f0SGerrit Renker 	 * (e.g. Ack Vectors or feature-negotiation options), it activates this
294d83447f0SGerrit Renker 	 * flag to schedule a Sync. The Sync will automatically incorporate all
295d83447f0SGerrit Renker 	 * currently pending header options, thus clearing the backlog.
296d83447f0SGerrit Renker 	 */
297d83447f0SGerrit Renker 	if (dp->dccps_sync_scheduled)
298d83447f0SGerrit Renker 		dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
299dc841e30SGerrit Renker }
300dc841e30SGerrit Renker 
301b1fcf55eSGerrit Renker /**
302b1fcf55eSGerrit Renker  * dccp_flush_write_queue  -  Drain queue at end of connection
3030b609b55SAndrew Lunn  * @sk: socket to be drained
3040b609b55SAndrew Lunn  * @time_budget: time allowed to drain the queue
3050b609b55SAndrew Lunn  *
306b1fcf55eSGerrit Renker  * Since dccp_sendmsg queues packets without waiting for them to be sent, it may
307b1fcf55eSGerrit Renker  * happen that the TX queue is not empty at the end of a connection. We give the
308b1fcf55eSGerrit Renker  * HC-sender CCID a grace period of up to @time_budget jiffies. If this function
309b1fcf55eSGerrit Renker  * returns with a non-empty write queue, it will be purged later.
310b1fcf55eSGerrit Renker  */
dccp_flush_write_queue(struct sock * sk,long * time_budget)311b1fcf55eSGerrit Renker void dccp_flush_write_queue(struct sock *sk, long *time_budget)
312b1fcf55eSGerrit Renker {
313b1fcf55eSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
314b1fcf55eSGerrit Renker 	struct sk_buff *skb;
315b1fcf55eSGerrit Renker 	long delay, rc;
316b1fcf55eSGerrit Renker 
317b1fcf55eSGerrit Renker 	while (*time_budget > 0 && (skb = skb_peek(&sk->sk_write_queue))) {
318b1fcf55eSGerrit Renker 		rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb);
319b1fcf55eSGerrit Renker 
320b1fcf55eSGerrit Renker 		switch (ccid_packet_dequeue_eval(rc)) {
321b1fcf55eSGerrit Renker 		case CCID_PACKET_WILL_DEQUEUE_LATER:
322b1fcf55eSGerrit Renker 			/*
323b1fcf55eSGerrit Renker 			 * If the CCID determines when to send, the next sending
324b1fcf55eSGerrit Renker 			 * time is unknown or the CCID may not even send again
325b1fcf55eSGerrit Renker 			 * (e.g. remote host crashes or lost Ack packets).
326b1fcf55eSGerrit Renker 			 */
327b1fcf55eSGerrit Renker 			DCCP_WARN("CCID did not manage to send all packets\n");
328b1fcf55eSGerrit Renker 			return;
329b1fcf55eSGerrit Renker 		case CCID_PACKET_DELAY:
330b1fcf55eSGerrit Renker 			delay = msecs_to_jiffies(rc);
331b1fcf55eSGerrit Renker 			if (delay > *time_budget)
332b1fcf55eSGerrit Renker 				return;
333b1fcf55eSGerrit Renker 			rc = dccp_wait_for_ccid(sk, delay);
334b1fcf55eSGerrit Renker 			if (rc < 0)
335b1fcf55eSGerrit Renker 				return;
336b1fcf55eSGerrit Renker 			*time_budget -= (delay - rc);
337b1fcf55eSGerrit Renker 			/* check again if we can send now */
338b1fcf55eSGerrit Renker 			break;
339b1fcf55eSGerrit Renker 		case CCID_PACKET_SEND_AT_ONCE:
340b1fcf55eSGerrit Renker 			dccp_xmit_packet(sk);
341b1fcf55eSGerrit Renker 			break;
342b1fcf55eSGerrit Renker 		case CCID_PACKET_ERR:
343b1fcf55eSGerrit Renker 			skb_dequeue(&sk->sk_write_queue);
344b1fcf55eSGerrit Renker 			kfree_skb(skb);
345b1fcf55eSGerrit Renker 			dccp_pr_debug("packet discarded due to err=%ld\n", rc);
346b1fcf55eSGerrit Renker 		}
347b1fcf55eSGerrit Renker 	}
348b1fcf55eSGerrit Renker }
349b1fcf55eSGerrit Renker 
dccp_write_xmit(struct sock * sk)350b1fcf55eSGerrit Renker void dccp_write_xmit(struct sock *sk)
351dc841e30SGerrit Renker {
352dc841e30SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
353dc841e30SGerrit Renker 	struct sk_buff *skb;
354dc841e30SGerrit Renker 
355871a2c16STomasz Grobelny 	while ((skb = dccp_qpolicy_top(sk))) {
356dc841e30SGerrit Renker 		int rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb);
357dc841e30SGerrit Renker 
358dc841e30SGerrit Renker 		switch (ccid_packet_dequeue_eval(rc)) {
359dc841e30SGerrit Renker 		case CCID_PACKET_WILL_DEQUEUE_LATER:
360dc841e30SGerrit Renker 			return;
361dc841e30SGerrit Renker 		case CCID_PACKET_DELAY:
362dc841e30SGerrit Renker 			sk_reset_timer(sk, &dp->dccps_xmit_timer,
363b1fcf55eSGerrit Renker 				       jiffies + msecs_to_jiffies(rc));
364dc841e30SGerrit Renker 			return;
365dc841e30SGerrit Renker 		case CCID_PACKET_SEND_AT_ONCE:
366dc841e30SGerrit Renker 			dccp_xmit_packet(sk);
367dc841e30SGerrit Renker 			break;
368dc841e30SGerrit Renker 		case CCID_PACKET_ERR:
369871a2c16STomasz Grobelny 			dccp_qpolicy_drop(sk, skb);
370dc841e30SGerrit Renker 			dccp_pr_debug("packet discarded due to err=%d\n", rc);
37197e5848dSIan McDonald 		}
37227258ee5SArnaldo Carvalho de Melo 	}
373f6282f4dSGerrit Renker }
37427258ee5SArnaldo Carvalho de Melo 
37559435444SGerrit Renker /**
37659435444SGerrit Renker  * dccp_retransmit_skb  -  Retransmit Request, Close, or CloseReq packets
3770b609b55SAndrew Lunn  * @sk: socket to perform retransmit on
3780b609b55SAndrew Lunn  *
37959435444SGerrit Renker  * There are only four retransmittable packet types in DCCP:
38059435444SGerrit Renker  * - Request  in client-REQUEST  state (sec. 8.1.1),
38159435444SGerrit Renker  * - CloseReq in server-CLOSEREQ state (sec. 8.3),
38259435444SGerrit Renker  * - Close    in   node-CLOSING  state (sec. 8.3),
38359435444SGerrit Renker  * - Acks in client-PARTOPEN state (sec. 8.1.5, handled by dccp_delack_timer()).
38459435444SGerrit Renker  * This function expects sk->sk_send_head to contain the original skb.
38559435444SGerrit Renker  */
dccp_retransmit_skb(struct sock * sk)38659435444SGerrit Renker int dccp_retransmit_skb(struct sock *sk)
3877c657876SArnaldo Carvalho de Melo {
38859435444SGerrit Renker 	WARN_ON(sk->sk_send_head == NULL);
38959435444SGerrit Renker 
39057cca05aSArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk) != 0)
3917c657876SArnaldo Carvalho de Melo 		return -EHOSTUNREACH; /* Routing failure or similar. */
3927c657876SArnaldo Carvalho de Melo 
39359435444SGerrit Renker 	/* this count is used to distinguish original and retransmitted skb */
39459435444SGerrit Renker 	inet_csk(sk)->icsk_retransmits++;
39559435444SGerrit Renker 
39659435444SGerrit Renker 	return dccp_transmit_skb(sk, skb_clone(sk->sk_send_head, GFP_ATOMIC));
3977c657876SArnaldo Carvalho de Melo }
3987c657876SArnaldo Carvalho de Melo 
dccp_make_response(const struct sock * sk,struct dst_entry * dst,struct request_sock * req)399802885fcSEric Dumazet struct sk_buff *dccp_make_response(const struct sock *sk, struct dst_entry *dst,
4007c657876SArnaldo Carvalho de Melo 				   struct request_sock *req)
4017c657876SArnaldo Carvalho de Melo {
4027c657876SArnaldo Carvalho de Melo 	struct dccp_hdr *dh;
40367e6b629SArnaldo Carvalho de Melo 	struct dccp_request_sock *dreq;
404118b2c95SArnaldo Carvalho de Melo 	const u32 dccp_header_size = sizeof(struct dccp_hdr) +
4057c657876SArnaldo Carvalho de Melo 				     sizeof(struct dccp_hdr_ext) +
4067c657876SArnaldo Carvalho de Melo 				     sizeof(struct dccp_hdr_response);
407802885fcSEric Dumazet 	struct sk_buff *skb;
408802885fcSEric Dumazet 
409802885fcSEric Dumazet 	/* sk is marked const to clearly express we dont hold socket lock.
410802885fcSEric Dumazet 	 * sock_wmalloc() will atomically change sk->sk_wmem_alloc,
411802885fcSEric Dumazet 	 * it is safe to promote sk to non const.
412802885fcSEric Dumazet 	 */
413802885fcSEric Dumazet 	skb = sock_wmalloc((struct sock *)sk, MAX_DCCP_HEADER, 1,
4147c657876SArnaldo Carvalho de Melo 			   GFP_ATOMIC);
415802885fcSEric Dumazet 	if (!skb)
4167c657876SArnaldo Carvalho de Melo 		return NULL;
4177c657876SArnaldo Carvalho de Melo 
418802885fcSEric Dumazet 	skb_reserve(skb, MAX_DCCP_HEADER);
4197c657876SArnaldo Carvalho de Melo 
420adf30907SEric Dumazet 	skb_dst_set(skb, dst_clone(dst));
4217c657876SArnaldo Carvalho de Melo 
42267e6b629SArnaldo Carvalho de Melo 	dreq = dccp_rsk(req);
423f541fb7eSSamuel Jero 	if (inet_rsk(req)->acked)	/* increase GSS upon retransmission */
424f541fb7eSSamuel Jero 		dccp_inc_seqno(&dreq->dreq_gss);
4257c657876SArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE;
426f541fb7eSSamuel Jero 	DCCP_SKB_CB(skb)->dccpd_seq  = dreq->dreq_gss;
4272d0817d1SArnaldo Carvalho de Melo 
4280c116839SGerrit Renker 	/* Resolve feature dependencies resulting from choice of CCID */
4290c116839SGerrit Renker 	if (dccp_feat_server_ccid_dependencies(dreq))
4300c116839SGerrit Renker 		goto response_failed;
4310c116839SGerrit Renker 
4320c116839SGerrit Renker 	if (dccp_insert_options_rsk(dreq, skb))
4330c116839SGerrit Renker 		goto response_failed;
4347c657876SArnaldo Carvalho de Melo 
43509dbc389SGerrit Renker 	/* Build and checksum header */
4369b42078eSGerrit Renker 	dh = dccp_zeroed_hdr(skb, dccp_header_size);
4377c657876SArnaldo Carvalho de Melo 
438b44084c2SEric Dumazet 	dh->dccph_sport	= htons(inet_rsk(req)->ir_num);
439634fb979SEric Dumazet 	dh->dccph_dport	= inet_rsk(req)->ir_rmt_port;
4407690af3fSArnaldo Carvalho de Melo 	dh->dccph_doff	= (dccp_header_size +
4417690af3fSArnaldo Carvalho de Melo 			   DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
4427c657876SArnaldo Carvalho de Melo 	dh->dccph_type	= DCCP_PKT_RESPONSE;
4437c657876SArnaldo Carvalho de Melo 	dh->dccph_x	= 1;
444f541fb7eSSamuel Jero 	dccp_hdr_set_seq(dh, dreq->dreq_gss);
445f541fb7eSSamuel Jero 	dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dreq->dreq_gsr);
44667e6b629SArnaldo Carvalho de Melo 	dccp_hdr_response(skb)->dccph_resp_service = dreq->dreq_service;
4477c657876SArnaldo Carvalho de Melo 
4486f4e5fffSGerrit Renker 	dccp_csum_outgoing(skb);
4496f4e5fffSGerrit Renker 
450e11d9d30SGerrit Renker 	/* We use `acked' to remember that a Response was already sent. */
451e11d9d30SGerrit Renker 	inet_rsk(req)->acked = 1;
4527c657876SArnaldo Carvalho de Melo 	DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
4537c657876SArnaldo Carvalho de Melo 	return skb;
4540c116839SGerrit Renker response_failed:
4550c116839SGerrit Renker 	kfree_skb(skb);
4560c116839SGerrit Renker 	return NULL;
4577c657876SArnaldo Carvalho de Melo }
4587c657876SArnaldo Carvalho de Melo 
459f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_make_response);
460f21e68caSArnaldo Carvalho de Melo 
461e356d37aSGerrit Renker /* answer offending packet in @rcv_skb with Reset from control socket @ctl */
dccp_ctl_make_reset(struct sock * sk,struct sk_buff * rcv_skb)4627630f026SDenis V. Lunev struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *rcv_skb)
463e356d37aSGerrit Renker {
464e356d37aSGerrit Renker 	struct dccp_hdr *rxdh = dccp_hdr(rcv_skb), *dh;
465e356d37aSGerrit Renker 	struct dccp_skb_cb *dcb = DCCP_SKB_CB(rcv_skb);
466e356d37aSGerrit Renker 	const u32 dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
467e356d37aSGerrit Renker 				       sizeof(struct dccp_hdr_ext) +
468e356d37aSGerrit Renker 				       sizeof(struct dccp_hdr_reset);
469e356d37aSGerrit Renker 	struct dccp_hdr_reset *dhr;
470e356d37aSGerrit Renker 	struct sk_buff *skb;
471e356d37aSGerrit Renker 
4727630f026SDenis V. Lunev 	skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC);
473e356d37aSGerrit Renker 	if (skb == NULL)
474e356d37aSGerrit Renker 		return NULL;
475e356d37aSGerrit Renker 
4767630f026SDenis V. Lunev 	skb_reserve(skb, sk->sk_prot->max_header);
477e356d37aSGerrit Renker 
478e356d37aSGerrit Renker 	/* Swap the send and the receive. */
479e356d37aSGerrit Renker 	dh = dccp_zeroed_hdr(skb, dccp_hdr_reset_len);
480e356d37aSGerrit Renker 	dh->dccph_type	= DCCP_PKT_RESET;
481e356d37aSGerrit Renker 	dh->dccph_sport	= rxdh->dccph_dport;
482e356d37aSGerrit Renker 	dh->dccph_dport	= rxdh->dccph_sport;
483e356d37aSGerrit Renker 	dh->dccph_doff	= dccp_hdr_reset_len / 4;
484e356d37aSGerrit Renker 	dh->dccph_x	= 1;
485e356d37aSGerrit Renker 
486e356d37aSGerrit Renker 	dhr = dccp_hdr_reset(skb);
487e356d37aSGerrit Renker 	dhr->dccph_reset_code = dcb->dccpd_reset_code;
488e356d37aSGerrit Renker 
489e356d37aSGerrit Renker 	switch (dcb->dccpd_reset_code) {
490e356d37aSGerrit Renker 	case DCCP_RESET_CODE_PACKET_ERROR:
491e356d37aSGerrit Renker 		dhr->dccph_reset_data[0] = rxdh->dccph_type;
492e356d37aSGerrit Renker 		break;
493df561f66SGustavo A. R. Silva 	case DCCP_RESET_CODE_OPTION_ERROR:
494e356d37aSGerrit Renker 	case DCCP_RESET_CODE_MANDATORY_ERROR:
495e356d37aSGerrit Renker 		memcpy(dhr->dccph_reset_data, dcb->dccpd_reset_data, 3);
496e356d37aSGerrit Renker 		break;
497e356d37aSGerrit Renker 	}
498e356d37aSGerrit Renker 	/*
499e356d37aSGerrit Renker 	 * From RFC 4340, 8.3.1:
500e356d37aSGerrit Renker 	 *   If P.ackno exists, set R.seqno := P.ackno + 1.
501e356d37aSGerrit Renker 	 *   Else set R.seqno := 0.
502e356d37aSGerrit Renker 	 */
503e356d37aSGerrit Renker 	if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
504e356d37aSGerrit Renker 		dccp_hdr_set_seq(dh, ADD48(dcb->dccpd_ack_seq, 1));
505e356d37aSGerrit Renker 	dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dcb->dccpd_seq);
506e356d37aSGerrit Renker 
507e356d37aSGerrit Renker 	dccp_csum_outgoing(skb);
508e356d37aSGerrit Renker 	return skb;
509e356d37aSGerrit Renker }
510e356d37aSGerrit Renker 
511e356d37aSGerrit Renker EXPORT_SYMBOL_GPL(dccp_ctl_make_reset);
512e356d37aSGerrit Renker 
513ee1a1592SGerrit Renker /* send Reset on established socket, to close or abort the connection */
dccp_send_reset(struct sock * sk,enum dccp_reset_codes code)514017487d7SArnaldo Carvalho de Melo int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code)
515017487d7SArnaldo Carvalho de Melo {
516ee1a1592SGerrit Renker 	struct sk_buff *skb;
517017487d7SArnaldo Carvalho de Melo 	/*
518017487d7SArnaldo Carvalho de Melo 	 * FIXME: what if rebuild_header fails?
519017487d7SArnaldo Carvalho de Melo 	 * Should we be doing a rebuild_header here?
520017487d7SArnaldo Carvalho de Melo 	 */
521f53dc67cSGerrit Renker 	int err = inet_csk(sk)->icsk_af_ops->rebuild_header(sk);
522017487d7SArnaldo Carvalho de Melo 
523ee1a1592SGerrit Renker 	if (err != 0)
524017487d7SArnaldo Carvalho de Melo 		return err;
525ee1a1592SGerrit Renker 
526ee1a1592SGerrit Renker 	skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1, GFP_ATOMIC);
527ee1a1592SGerrit Renker 	if (skb == NULL)
528ee1a1592SGerrit Renker 		return -ENOBUFS;
529ee1a1592SGerrit Renker 
530ee1a1592SGerrit Renker 	/* Reserve space for headers and prepare control bits. */
531ee1a1592SGerrit Renker 	skb_reserve(skb, sk->sk_prot->max_header);
532ee1a1592SGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_type	   = DCCP_PKT_RESET;
533ee1a1592SGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_reset_code = code;
534ee1a1592SGerrit Renker 
535ee1a1592SGerrit Renker 	return dccp_transmit_skb(sk, skb);
536017487d7SArnaldo Carvalho de Melo }
537017487d7SArnaldo Carvalho de Melo 
5387c657876SArnaldo Carvalho de Melo /*
5397c657876SArnaldo Carvalho de Melo  * Do all connect socket setups that can be done AF independent.
5407c657876SArnaldo Carvalho de Melo  */
dccp_connect(struct sock * sk)54193344af4SGerrit Renker int dccp_connect(struct sock *sk)
5427c657876SArnaldo Carvalho de Melo {
54393344af4SGerrit Renker 	struct sk_buff *skb;
544f21e68caSArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
5457c657876SArnaldo Carvalho de Melo 	struct dst_entry *dst = __sk_dst_get(sk);
5467c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
5477c657876SArnaldo Carvalho de Melo 
5487c657876SArnaldo Carvalho de Melo 	sk->sk_err = 0;
5497c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
5507c657876SArnaldo Carvalho de Melo 
5517c657876SArnaldo Carvalho de Melo 	dccp_sync_mss(sk, dst_mtu(dst));
5527c657876SArnaldo Carvalho de Melo 
5539eca0a47SGerrit Renker 	/* do not connect if feature negotiation setup fails */
5549eca0a47SGerrit Renker 	if (dccp_feat_finalise_settings(dccp_sk(sk)))
5559eca0a47SGerrit Renker 		return -EPROTO;
5569eca0a47SGerrit Renker 
55793344af4SGerrit Renker 	/* Initialise GAR as per 8.5; AWL/AWH are set in dccp_transmit_skb() */
55893344af4SGerrit Renker 	dp->dccps_gar = dp->dccps_iss;
5597c657876SArnaldo Carvalho de Melo 
560118b2c95SArnaldo Carvalho de Melo 	skb = alloc_skb(sk->sk_prot->max_header, sk->sk_allocation);
5617c657876SArnaldo Carvalho de Melo 	if (unlikely(skb == NULL))
5627c657876SArnaldo Carvalho de Melo 		return -ENOBUFS;
5637c657876SArnaldo Carvalho de Melo 
5647c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers. */
565118b2c95SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
5667c657876SArnaldo Carvalho de Melo 
5677c657876SArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
5687c657876SArnaldo Carvalho de Melo 
5698695e801SGerrit Renker 	dccp_transmit_skb(sk, dccp_skb_entail(sk, skb));
5707c657876SArnaldo Carvalho de Melo 	DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS);
5717c657876SArnaldo Carvalho de Melo 
5727c657876SArnaldo Carvalho de Melo 	/* Timer for repeating the REQUEST until an answer. */
57393344af4SGerrit Renker 	icsk->icsk_retransmits = 0;
57427258ee5SArnaldo Carvalho de Melo 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
57527258ee5SArnaldo Carvalho de Melo 				  icsk->icsk_rto, DCCP_RTO_MAX);
5767c657876SArnaldo Carvalho de Melo 	return 0;
5777c657876SArnaldo Carvalho de Melo }
5787c657876SArnaldo Carvalho de Melo 
579f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_connect);
580f21e68caSArnaldo Carvalho de Melo 
dccp_send_ack(struct sock * sk)5817c657876SArnaldo Carvalho de Melo void dccp_send_ack(struct sock *sk)
5827c657876SArnaldo Carvalho de Melo {
5837c657876SArnaldo Carvalho de Melo 	/* If we have been reset, we may not send again. */
5847c657876SArnaldo Carvalho de Melo 	if (sk->sk_state != DCCP_CLOSED) {
585118b2c95SArnaldo Carvalho de Melo 		struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header,
586118b2c95SArnaldo Carvalho de Melo 						GFP_ATOMIC);
5877c657876SArnaldo Carvalho de Melo 
5887c657876SArnaldo Carvalho de Melo 		if (skb == NULL) {
5897c657876SArnaldo Carvalho de Melo 			inet_csk_schedule_ack(sk);
5907c657876SArnaldo Carvalho de Melo 			inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
5917690af3fSArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
5927690af3fSArnaldo Carvalho de Melo 						  TCP_DELACK_MAX,
5937690af3fSArnaldo Carvalho de Melo 						  DCCP_RTO_MAX);
5947c657876SArnaldo Carvalho de Melo 			return;
5957c657876SArnaldo Carvalho de Melo 		}
5967c657876SArnaldo Carvalho de Melo 
5977c657876SArnaldo Carvalho de Melo 		/* Reserve space for headers */
598118b2c95SArnaldo Carvalho de Melo 		skb_reserve(skb, sk->sk_prot->max_header);
5997c657876SArnaldo Carvalho de Melo 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
6007c657876SArnaldo Carvalho de Melo 		dccp_transmit_skb(sk, skb);
6017c657876SArnaldo Carvalho de Melo 	}
6027c657876SArnaldo Carvalho de Melo }
6037c657876SArnaldo Carvalho de Melo 
6047c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_ack);
6057c657876SArnaldo Carvalho de Melo 
6061e2f0e5eSGerrit Renker #if 0
607727ecc5fSGerrit Renker /* FIXME: Is this still necessary (11.3) - currently nowhere used by DCCP. */
6087c657876SArnaldo Carvalho de Melo void dccp_send_delayed_ack(struct sock *sk)
6097c657876SArnaldo Carvalho de Melo {
6107c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
6117c657876SArnaldo Carvalho de Melo 	/*
6127c657876SArnaldo Carvalho de Melo 	 * FIXME: tune this timer. elapsed time fixes the skew, so no problem
6137c657876SArnaldo Carvalho de Melo 	 * with using 2s, and active senders also piggyback the ACK into a
6147c657876SArnaldo Carvalho de Melo 	 * DATAACK packet, so this is really for quiescent senders.
6157c657876SArnaldo Carvalho de Melo 	 */
6167c657876SArnaldo Carvalho de Melo 	unsigned long timeout = jiffies + 2 * HZ;
6177c657876SArnaldo Carvalho de Melo 
6187c657876SArnaldo Carvalho de Melo 	/* Use new timeout only if there wasn't a older one earlier. */
6197c657876SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
6207c657876SArnaldo Carvalho de Melo 		/* If delack timer was blocked or is about to expire,
6217c657876SArnaldo Carvalho de Melo 		 * send ACK now.
6227c657876SArnaldo Carvalho de Melo 		 *
6237c657876SArnaldo Carvalho de Melo 		 * FIXME: check the "about to expire" part
6247c657876SArnaldo Carvalho de Melo 		 */
6257c657876SArnaldo Carvalho de Melo 		if (icsk->icsk_ack.blocked) {
6267c657876SArnaldo Carvalho de Melo 			dccp_send_ack(sk);
6277c657876SArnaldo Carvalho de Melo 			return;
6287c657876SArnaldo Carvalho de Melo 		}
6297c657876SArnaldo Carvalho de Melo 
6307c657876SArnaldo Carvalho de Melo 		if (!time_before(timeout, icsk->icsk_ack.timeout))
6317c657876SArnaldo Carvalho de Melo 			timeout = icsk->icsk_ack.timeout;
6327c657876SArnaldo Carvalho de Melo 	}
6337c657876SArnaldo Carvalho de Melo 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
6347c657876SArnaldo Carvalho de Melo 	icsk->icsk_ack.timeout = timeout;
6357c657876SArnaldo Carvalho de Melo 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
6367c657876SArnaldo Carvalho de Melo }
6371e2f0e5eSGerrit Renker #endif
6387c657876SArnaldo Carvalho de Melo 
dccp_send_sync(struct sock * sk,const u64 ackno,const enum dccp_pkt_type pkt_type)639b0d045caSGerrit Renker void dccp_send_sync(struct sock *sk, const u64 ackno,
640e92ae93aSArnaldo Carvalho de Melo 		    const enum dccp_pkt_type pkt_type)
6417c657876SArnaldo Carvalho de Melo {
6427c657876SArnaldo Carvalho de Melo 	/*
6437c657876SArnaldo Carvalho de Melo 	 * We are not putting this on the write queue, so
6447c657876SArnaldo Carvalho de Melo 	 * dccp_transmit_skb() will set the ownership to this
6457c657876SArnaldo Carvalho de Melo 	 * sock.
6467c657876SArnaldo Carvalho de Melo 	 */
647118b2c95SArnaldo Carvalho de Melo 	struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC);
6487c657876SArnaldo Carvalho de Melo 
649b0d045caSGerrit Renker 	if (skb == NULL) {
6507c657876SArnaldo Carvalho de Melo 		/* FIXME: how to make sure the sync is sent? */
651b0d045caSGerrit Renker 		DCCP_CRIT("could not send %s", dccp_packet_name(pkt_type));
6527c657876SArnaldo Carvalho de Melo 		return;
653b0d045caSGerrit Renker 	}
6547c657876SArnaldo Carvalho de Melo 
6557c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers and prepare control bits. */
656118b2c95SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
657e92ae93aSArnaldo Carvalho de Melo 	DCCP_SKB_CB(skb)->dccpd_type = pkt_type;
658b0d045caSGerrit Renker 	DCCP_SKB_CB(skb)->dccpd_ack_seq = ackno;
6597c657876SArnaldo Carvalho de Melo 
660d83447f0SGerrit Renker 	/*
661d83447f0SGerrit Renker 	 * Clear the flag in case the Sync was scheduled for out-of-band data,
662d83447f0SGerrit Renker 	 * such as carrying a long Ack Vector.
663d83447f0SGerrit Renker 	 */
664d83447f0SGerrit Renker 	dccp_sk(sk)->dccps_sync_scheduled = 0;
665d83447f0SGerrit Renker 
6667c657876SArnaldo Carvalho de Melo 	dccp_transmit_skb(sk, skb);
6677c657876SArnaldo Carvalho de Melo }
6687c657876SArnaldo Carvalho de Melo 
669b61fafc4SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_send_sync);
670b61fafc4SArnaldo Carvalho de Melo 
6717690af3fSArnaldo Carvalho de Melo /*
6727690af3fSArnaldo Carvalho de Melo  * Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This
6737690af3fSArnaldo Carvalho de Melo  * cannot be allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under
6747690af3fSArnaldo Carvalho de Melo  * any circumstances.
6757c657876SArnaldo Carvalho de Melo  */
dccp_send_close(struct sock * sk,const int active)6767ad07e7cSArnaldo Carvalho de Melo void dccp_send_close(struct sock *sk, const int active)
6777c657876SArnaldo Carvalho de Melo {
6787c657876SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
6797c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
6807d877f3bSAl Viro 	const gfp_t prio = active ? GFP_KERNEL : GFP_ATOMIC;
6817c657876SArnaldo Carvalho de Melo 
6827ad07e7cSArnaldo Carvalho de Melo 	skb = alloc_skb(sk->sk_prot->max_header, prio);
6837ad07e7cSArnaldo Carvalho de Melo 	if (skb == NULL)
6847ad07e7cSArnaldo Carvalho de Melo 		return;
6857c657876SArnaldo Carvalho de Melo 
6867c657876SArnaldo Carvalho de Melo 	/* Reserve space for headers and prepare control bits. */
6877c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
688b8599d20SGerrit Renker 	if (dp->dccps_role == DCCP_ROLE_SERVER && !dp->dccps_server_timewait)
689b8599d20SGerrit Renker 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_CLOSEREQ;
690b8599d20SGerrit Renker 	else
691b8599d20SGerrit Renker 		DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_CLOSE;
6927c657876SArnaldo Carvalho de Melo 
6937ad07e7cSArnaldo Carvalho de Melo 	if (active) {
6948695e801SGerrit Renker 		skb = dccp_skb_entail(sk, skb);
69592d31920SGerrit Renker 		/*
69692d31920SGerrit Renker 		 * Retransmission timer for active-close: RFC 4340, 8.3 requires
69792d31920SGerrit Renker 		 * to retransmit the Close/CloseReq until the CLOSING/CLOSEREQ
69892d31920SGerrit Renker 		 * state can be left. The initial timeout is 2 RTTs.
69992d31920SGerrit Renker 		 * Since RTT measurement is done by the CCIDs, there is no easy
70092d31920SGerrit Renker 		 * way to get an RTT sample. The fallback RTT from RFC 4340, 3.4
70192d31920SGerrit Renker 		 * is too low (200ms); we use a high value to avoid unnecessary
70292d31920SGerrit Renker 		 * retransmissions when the link RTT is > 0.2 seconds.
70392d31920SGerrit Renker 		 * FIXME: Let main module sample RTTs and use that instead.
70492d31920SGerrit Renker 		 */
70592d31920SGerrit Renker 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
70692d31920SGerrit Renker 					  DCCP_TIMEOUT_INIT, DCCP_RTO_MAX);
7078695e801SGerrit Renker 	}
7087c657876SArnaldo Carvalho de Melo 	dccp_transmit_skb(sk, skb);
7097c657876SArnaldo Carvalho de Melo }
710