xref: /openbmc/linux/net/dccp/proto.c (revision 71c262a3)
17c657876SArnaldo Carvalho de Melo /*
27c657876SArnaldo Carvalho de Melo  *  net/dccp/proto.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 modify it
87c657876SArnaldo Carvalho de Melo  *	under the terms of the GNU General Public License version 2 as
97c657876SArnaldo Carvalho de Melo  *	published by the Free Software Foundation.
107c657876SArnaldo Carvalho de Melo  */
117c657876SArnaldo Carvalho de Melo 
127c657876SArnaldo Carvalho de Melo #include <linux/dccp.h>
137c657876SArnaldo Carvalho de Melo #include <linux/module.h>
147c657876SArnaldo Carvalho de Melo #include <linux/types.h>
157c657876SArnaldo Carvalho de Melo #include <linux/sched.h>
167c657876SArnaldo Carvalho de Melo #include <linux/kernel.h>
177c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h>
187c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h>
197c657876SArnaldo Carvalho de Melo #include <linux/in.h>
207c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h>
217c657876SArnaldo Carvalho de Melo #include <linux/init.h>
227c657876SArnaldo Carvalho de Melo #include <linux/random.h>
237c657876SArnaldo Carvalho de Melo #include <net/checksum.h>
247c657876SArnaldo Carvalho de Melo 
2514c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h>
267c657876SArnaldo Carvalho de Melo #include <net/sock.h>
277c657876SArnaldo Carvalho de Melo #include <net/xfrm.h>
287c657876SArnaldo Carvalho de Melo 
296273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h>
307c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h>
317c657876SArnaldo Carvalho de Melo #include <linux/timer.h>
327c657876SArnaldo Carvalho de Melo #include <linux/delay.h>
337c657876SArnaldo Carvalho de Melo #include <linux/poll.h>
347c657876SArnaldo Carvalho de Melo 
357c657876SArnaldo Carvalho de Melo #include "ccid.h"
367c657876SArnaldo Carvalho de Melo #include "dccp.h"
37afe00251SAndrea Bittau #include "feat.h"
387c657876SArnaldo Carvalho de Melo 
39ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
407c657876SArnaldo Carvalho de Melo 
41f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics);
42f21e68caSArnaldo Carvalho de Melo 
437c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0);
447c657876SArnaldo Carvalho de Melo 
45f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count);
46f21e68caSArnaldo Carvalho de Melo 
475caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo;
48075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo);
49075ae866SArnaldo Carvalho de Melo 
50b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */
51b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5;
52b1308dc0SIan McDonald 
53c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state)
54c25a18baSArnaldo Carvalho de Melo {
55c25a18baSArnaldo Carvalho de Melo 	const int oldstate = sk->sk_state;
56c25a18baSArnaldo Carvalho de Melo 
57f11135a3SGerrit Renker 	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
58c25a18baSArnaldo Carvalho de Melo 		      dccp_state_name(oldstate), dccp_state_name(state));
59c25a18baSArnaldo Carvalho de Melo 	WARN_ON(state == oldstate);
60c25a18baSArnaldo Carvalho de Melo 
61c25a18baSArnaldo Carvalho de Melo 	switch (state) {
62c25a18baSArnaldo Carvalho de Melo 	case DCCP_OPEN:
63c25a18baSArnaldo Carvalho de Melo 		if (oldstate != DCCP_OPEN)
64c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
65c25a18baSArnaldo Carvalho de Melo 		break;
66c25a18baSArnaldo Carvalho de Melo 
67c25a18baSArnaldo Carvalho de Melo 	case DCCP_CLOSED:
680c869620SGerrit Renker 		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
690c869620SGerrit Renker 		    oldstate == DCCP_CLOSING)
70c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
71c25a18baSArnaldo Carvalho de Melo 
72c25a18baSArnaldo Carvalho de Melo 		sk->sk_prot->unhash(sk);
73c25a18baSArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash != NULL &&
74c25a18baSArnaldo Carvalho de Melo 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
75ab1e0a13SArnaldo Carvalho de Melo 			inet_put_port(sk);
76c25a18baSArnaldo Carvalho de Melo 		/* fall through */
77c25a18baSArnaldo Carvalho de Melo 	default:
78c25a18baSArnaldo Carvalho de Melo 		if (oldstate == DCCP_OPEN)
79c25a18baSArnaldo Carvalho de Melo 			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
80c25a18baSArnaldo Carvalho de Melo 	}
81c25a18baSArnaldo Carvalho de Melo 
82c25a18baSArnaldo Carvalho de Melo 	/* Change state AFTER socket is unhashed to avoid closed
83c25a18baSArnaldo Carvalho de Melo 	 * socket sitting in hash tables.
84c25a18baSArnaldo Carvalho de Melo 	 */
85c25a18baSArnaldo Carvalho de Melo 	sk->sk_state = state;
86c25a18baSArnaldo Carvalho de Melo }
87c25a18baSArnaldo Carvalho de Melo 
88c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state);
89c25a18baSArnaldo Carvalho de Melo 
900c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk)
910c869620SGerrit Renker {
920c869620SGerrit Renker 	switch (sk->sk_state) {
930c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
940c869620SGerrit Renker 		/* Node (client or server) has received Close packet. */
950c869620SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
960c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
970c869620SGerrit Renker 		break;
980c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
990c869620SGerrit Renker 		/*
1000c869620SGerrit Renker 		 * Client received CloseReq. We set the `active' flag so that
1010c869620SGerrit Renker 		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
1020c869620SGerrit Renker 		 */
1030c869620SGerrit Renker 		dccp_send_close(sk, 1);
1040c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSING);
1050c869620SGerrit Renker 	}
1060c869620SGerrit Renker }
1070c869620SGerrit Renker 
108c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk)
109c25a18baSArnaldo Carvalho de Melo {
110c25a18baSArnaldo Carvalho de Melo 	dccp_set_state(sk, DCCP_CLOSED);
111c25a18baSArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
112c25a18baSArnaldo Carvalho de Melo 
113c25a18baSArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
114c25a18baSArnaldo Carvalho de Melo 
115c25a18baSArnaldo Carvalho de Melo 	if (!sock_flag(sk, SOCK_DEAD))
116c25a18baSArnaldo Carvalho de Melo 		sk->sk_state_change(sk);
117c25a18baSArnaldo Carvalho de Melo 	else
118c25a18baSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
119c25a18baSArnaldo Carvalho de Melo }
120c25a18baSArnaldo Carvalho de Melo 
121c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done);
122c25a18baSArnaldo Carvalho de Melo 
1237c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type)
1247c657876SArnaldo Carvalho de Melo {
1257c657876SArnaldo Carvalho de Melo 	static const char *dccp_packet_names[] = {
1267c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_REQUEST]  = "REQUEST",
1277c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESPONSE] = "RESPONSE",
1287c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATA]	    = "DATA",
1297c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_ACK]	    = "ACK",
1307c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATAACK]  = "DATAACK",
1317c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
1327c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSE]    = "CLOSE",
1337c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESET]    = "RESET",
1347c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNC]	    = "SYNC",
1357c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNCACK]  = "SYNCACK",
1367c657876SArnaldo Carvalho de Melo 	};
1377c657876SArnaldo Carvalho de Melo 
1387c657876SArnaldo Carvalho de Melo 	if (type >= DCCP_NR_PKT_TYPES)
1397c657876SArnaldo Carvalho de Melo 		return "INVALID";
1407c657876SArnaldo Carvalho de Melo 	else
1417c657876SArnaldo Carvalho de Melo 		return dccp_packet_names[type];
1427c657876SArnaldo Carvalho de Melo }
1437c657876SArnaldo Carvalho de Melo 
1447c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name);
1457c657876SArnaldo Carvalho de Melo 
1467c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state)
1477c657876SArnaldo Carvalho de Melo {
1487c657876SArnaldo Carvalho de Melo 	static char *dccp_state_names[] = {
1497c657876SArnaldo Carvalho de Melo 	[DCCP_OPEN]		= "OPEN",
1507c657876SArnaldo Carvalho de Melo 	[DCCP_REQUESTING]	= "REQUESTING",
1517c657876SArnaldo Carvalho de Melo 	[DCCP_PARTOPEN]		= "PARTOPEN",
1527c657876SArnaldo Carvalho de Melo 	[DCCP_LISTEN]		= "LISTEN",
1537c657876SArnaldo Carvalho de Melo 	[DCCP_RESPOND]		= "RESPOND",
1547c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSING]		= "CLOSING",
155f11135a3SGerrit Renker 	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
156f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
157f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
1587c657876SArnaldo Carvalho de Melo 	[DCCP_TIME_WAIT]	= "TIME_WAIT",
1597c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSED]		= "CLOSED",
1607c657876SArnaldo Carvalho de Melo 	};
1617c657876SArnaldo Carvalho de Melo 
1627c657876SArnaldo Carvalho de Melo 	if (state >= DCCP_MAX_STATES)
1637c657876SArnaldo Carvalho de Melo 		return "INVALID STATE!";
1647c657876SArnaldo Carvalho de Melo 	else
1657c657876SArnaldo Carvalho de Melo 		return dccp_state_names[state];
1667c657876SArnaldo Carvalho de Melo }
1677c657876SArnaldo Carvalho de Melo 
1687c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name);
1697c657876SArnaldo Carvalho de Melo 
17072478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
1713e0fadc5SArnaldo Carvalho de Melo {
1723e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
173410e27a4SGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
1743e0fadc5SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1753e0fadc5SArnaldo Carvalho de Melo 
176410e27a4SGerrit Renker 	dccp_minisock_init(&dp->dccps_minisock);
177410e27a4SGerrit Renker 
178e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
179e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
180e18d7a98SArnaldo Carvalho de Melo 	sk->sk_state		= DCCP_CLOSED;
181e18d7a98SArnaldo Carvalho de Melo 	sk->sk_write_space	= dccp_write_space;
182e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss	= dccp_sync_mss;
183410e27a4SGerrit Renker 	dp->dccps_mss_cache	= 536;
184e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_rate_last	= jiffies;
185e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
186e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
187410e27a4SGerrit Renker 	dp->dccps_l_ack_ratio	= dp->dccps_r_ack_ratio = 1;
188e18d7a98SArnaldo Carvalho de Melo 
189e18d7a98SArnaldo Carvalho de Melo 	dccp_init_xmit_timers(sk);
190e18d7a98SArnaldo Carvalho de Melo 
191ac75773cSGerrit Renker 	INIT_LIST_HEAD(&dp->dccps_featneg);
192410e27a4SGerrit Renker 	/*
193410e27a4SGerrit Renker 	 * FIXME: We're hardcoding the CCID, and doing this at this point makes
194410e27a4SGerrit Renker 	 * the listening (master) sock get CCID control blocks, which is not
195410e27a4SGerrit Renker 	 * necessary, but for now, to not mess with the test userspace apps,
196410e27a4SGerrit Renker 	 * lets leave it here, later the real solution is to do this in a
197410e27a4SGerrit Renker 	 * setsockopt(CCIDs-I-want/accept). -acme
198410e27a4SGerrit Renker 	 */
199410e27a4SGerrit Renker 	if (likely(ctl_sock_initialized)) {
200e8ef967aSGerrit Renker 		int rc = dccp_feat_init(sk);
201410e27a4SGerrit Renker 
202410e27a4SGerrit Renker 		if (rc)
203410e27a4SGerrit Renker 			return rc;
204410e27a4SGerrit Renker 
205410e27a4SGerrit Renker 		if (dmsk->dccpms_send_ack_vector) {
206410e27a4SGerrit Renker 			dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
207410e27a4SGerrit Renker 			if (dp->dccps_hc_rx_ackvec == NULL)
208410e27a4SGerrit Renker 				return -ENOMEM;
209410e27a4SGerrit Renker 		}
210410e27a4SGerrit Renker 		dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
211410e27a4SGerrit Renker 						      sk, GFP_KERNEL);
212410e27a4SGerrit Renker 		dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
213410e27a4SGerrit Renker 						      sk, GFP_KERNEL);
214410e27a4SGerrit Renker 		if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
215410e27a4SGerrit Renker 			     dp->dccps_hc_tx_ccid == NULL)) {
216410e27a4SGerrit Renker 			ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
217410e27a4SGerrit Renker 			ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
218410e27a4SGerrit Renker 			if (dmsk->dccpms_send_ack_vector) {
219410e27a4SGerrit Renker 				dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
220410e27a4SGerrit Renker 				dp->dccps_hc_rx_ackvec = NULL;
221410e27a4SGerrit Renker 			}
222410e27a4SGerrit Renker 			dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
223410e27a4SGerrit Renker 			return -ENOMEM;
224410e27a4SGerrit Renker 		}
225410e27a4SGerrit Renker 	} else {
2263e0fadc5SArnaldo Carvalho de Melo 		/* control socket doesn't need feat nego */
227410e27a4SGerrit Renker 		INIT_LIST_HEAD(&dmsk->dccpms_pending);
228410e27a4SGerrit Renker 		INIT_LIST_HEAD(&dmsk->dccpms_conf);
229410e27a4SGerrit Renker 	}
230410e27a4SGerrit Renker 
2313e0fadc5SArnaldo Carvalho de Melo 	return 0;
2323e0fadc5SArnaldo Carvalho de Melo }
2333e0fadc5SArnaldo Carvalho de Melo 
2343e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock);
2353e0fadc5SArnaldo Carvalho de Melo 
2367d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk)
2373e0fadc5SArnaldo Carvalho de Melo {
2383e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
239410e27a4SGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
2403e0fadc5SArnaldo Carvalho de Melo 
2413e0fadc5SArnaldo Carvalho de Melo 	/*
2423e0fadc5SArnaldo Carvalho de Melo 	 * DCCP doesn't use sk_write_queue, just sk_send_head
2433e0fadc5SArnaldo Carvalho de Melo 	 * for retransmissions
2443e0fadc5SArnaldo Carvalho de Melo 	 */
2453e0fadc5SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
2463e0fadc5SArnaldo Carvalho de Melo 		kfree_skb(sk->sk_send_head);
2473e0fadc5SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
2483e0fadc5SArnaldo Carvalho de Melo 	}
2493e0fadc5SArnaldo Carvalho de Melo 
2503e0fadc5SArnaldo Carvalho de Melo 	/* Clean up a referenced DCCP bind bucket. */
2513e0fadc5SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_bind_hash != NULL)
252ab1e0a13SArnaldo Carvalho de Melo 		inet_put_port(sk);
2533e0fadc5SArnaldo Carvalho de Melo 
2543e0fadc5SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
2553e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_service_list = NULL;
2563e0fadc5SArnaldo Carvalho de Melo 
257410e27a4SGerrit Renker 	if (dmsk->dccpms_send_ack_vector) {
2583e0fadc5SArnaldo Carvalho de Melo 		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
2593e0fadc5SArnaldo Carvalho de Melo 		dp->dccps_hc_rx_ackvec = NULL;
2603e0fadc5SArnaldo Carvalho de Melo 	}
2613e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
2623e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
2633e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
2643e0fadc5SArnaldo Carvalho de Melo 
2653e0fadc5SArnaldo Carvalho de Melo 	/* clean up feature negotiation state */
266d99a7bd2SGerrit Renker 	dccp_feat_list_purge(&dp->dccps_featneg);
2673e0fadc5SArnaldo Carvalho de Melo }
2683e0fadc5SArnaldo Carvalho de Melo 
2693e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock);
2703e0fadc5SArnaldo Carvalho de Melo 
27172a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog)
2727c657876SArnaldo Carvalho de Melo {
27367e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
27467e6b629SArnaldo Carvalho de Melo 
27567e6b629SArnaldo Carvalho de Melo 	dp->dccps_role = DCCP_ROLE_LISTEN;
2769eca0a47SGerrit Renker 	/* do not start to listen if feature negotiation setup fails */
2779eca0a47SGerrit Renker 	if (dccp_feat_finalise_settings(dp))
2789eca0a47SGerrit Renker 		return -EPROTO;
27972a3effaSEric Dumazet 	return inet_csk_listen_start(sk, backlog);
2807c657876SArnaldo Carvalho de Melo }
2817c657876SArnaldo Carvalho de Melo 
282ce865a61SGerrit Renker static inline int dccp_need_reset(int state)
283ce865a61SGerrit Renker {
284ce865a61SGerrit Renker 	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
285ce865a61SGerrit Renker 	       state != DCCP_REQUESTING;
286ce865a61SGerrit Renker }
287ce865a61SGerrit Renker 
2887c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags)
2897c657876SArnaldo Carvalho de Melo {
2907c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2917c657876SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
2927c657876SArnaldo Carvalho de Melo 	int err = 0;
2937c657876SArnaldo Carvalho de Melo 	const int old_state = sk->sk_state;
2947c657876SArnaldo Carvalho de Melo 
2957c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_CLOSED)
2967c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
2977c657876SArnaldo Carvalho de Melo 
298ce865a61SGerrit Renker 	/*
299ce865a61SGerrit Renker 	 * This corresponds to the ABORT function of RFC793, sec. 3.8
300ce865a61SGerrit Renker 	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
301ce865a61SGerrit Renker 	 */
3027c657876SArnaldo Carvalho de Melo 	if (old_state == DCCP_LISTEN) {
3037c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
304ce865a61SGerrit Renker 	} else if (dccp_need_reset(old_state)) {
305ce865a61SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
306ce865a61SGerrit Renker 		sk->sk_err = ECONNRESET;
3077c657876SArnaldo Carvalho de Melo 	} else if (old_state == DCCP_REQUESTING)
3087c657876SArnaldo Carvalho de Melo 		sk->sk_err = ECONNRESET;
3097c657876SArnaldo Carvalho de Melo 
3107c657876SArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
31148816322SGerrit Renker 
3127c657876SArnaldo Carvalho de Melo 	__skb_queue_purge(&sk->sk_receive_queue);
31348816322SGerrit Renker 	__skb_queue_purge(&sk->sk_write_queue);
3147c657876SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
3157c657876SArnaldo Carvalho de Melo 		__kfree_skb(sk->sk_send_head);
3167c657876SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
3177c657876SArnaldo Carvalho de Melo 	}
3187c657876SArnaldo Carvalho de Melo 
3197c657876SArnaldo Carvalho de Melo 	inet->dport = 0;
3207c657876SArnaldo Carvalho de Melo 
3217c657876SArnaldo Carvalho de Melo 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
3227c657876SArnaldo Carvalho de Melo 		inet_reset_saddr(sk);
3237c657876SArnaldo Carvalho de Melo 
3247c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = 0;
3257c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
3267c657876SArnaldo Carvalho de Melo 
3277c657876SArnaldo Carvalho de Melo 	icsk->icsk_backoff = 0;
3287c657876SArnaldo Carvalho de Melo 	inet_csk_delack_init(sk);
3297c657876SArnaldo Carvalho de Melo 	__sk_dst_reset(sk);
3307c657876SArnaldo Carvalho de Melo 
331547b792cSIlpo Järvinen 	WARN_ON(inet->num && !icsk->icsk_bind_hash);
3327c657876SArnaldo Carvalho de Melo 
3337c657876SArnaldo Carvalho de Melo 	sk->sk_error_report(sk);
3347c657876SArnaldo Carvalho de Melo 	return err;
3357c657876SArnaldo Carvalho de Melo }
3367c657876SArnaldo Carvalho de Melo 
337f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect);
338f21e68caSArnaldo Carvalho de Melo 
339331968bdSArnaldo Carvalho de Melo /*
340331968bdSArnaldo Carvalho de Melo  *	Wait for a DCCP event.
341331968bdSArnaldo Carvalho de Melo  *
342331968bdSArnaldo Carvalho de Melo  *	Note that we don't need to lock the socket, as the upper poll layers
343331968bdSArnaldo Carvalho de Melo  *	take care of normal races (between the test and the event) and we don't
344331968bdSArnaldo Carvalho de Melo  *	go look at any of the socket buffers directly.
345331968bdSArnaldo Carvalho de Melo  */
346f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock,
347331968bdSArnaldo Carvalho de Melo 		       poll_table *wait)
348331968bdSArnaldo Carvalho de Melo {
349331968bdSArnaldo Carvalho de Melo 	unsigned int mask;
350331968bdSArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
351331968bdSArnaldo Carvalho de Melo 
352331968bdSArnaldo Carvalho de Melo 	poll_wait(file, sk->sk_sleep, wait);
353331968bdSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
354331968bdSArnaldo Carvalho de Melo 		return inet_csk_listen_poll(sk);
355331968bdSArnaldo Carvalho de Melo 
356331968bdSArnaldo Carvalho de Melo 	/* Socket is not locked. We are protected from async events
357331968bdSArnaldo Carvalho de Melo 	   by poll logic and correct handling of state changes
358331968bdSArnaldo Carvalho de Melo 	   made by another threads is impossible in any case.
359331968bdSArnaldo Carvalho de Melo 	 */
360331968bdSArnaldo Carvalho de Melo 
361331968bdSArnaldo Carvalho de Melo 	mask = 0;
362331968bdSArnaldo Carvalho de Melo 	if (sk->sk_err)
363331968bdSArnaldo Carvalho de Melo 		mask = POLLERR;
364331968bdSArnaldo Carvalho de Melo 
365331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
366331968bdSArnaldo Carvalho de Melo 		mask |= POLLHUP;
367331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown & RCV_SHUTDOWN)
368f348d70aSDavide Libenzi 		mask |= POLLIN | POLLRDNORM | POLLRDHUP;
369331968bdSArnaldo Carvalho de Melo 
370331968bdSArnaldo Carvalho de Melo 	/* Connected? */
371331968bdSArnaldo Carvalho de Melo 	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
372331968bdSArnaldo Carvalho de Melo 		if (atomic_read(&sk->sk_rmem_alloc) > 0)
373331968bdSArnaldo Carvalho de Melo 			mask |= POLLIN | POLLRDNORM;
374331968bdSArnaldo Carvalho de Melo 
375331968bdSArnaldo Carvalho de Melo 		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
376331968bdSArnaldo Carvalho de Melo 			if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
377331968bdSArnaldo Carvalho de Melo 				mask |= POLLOUT | POLLWRNORM;
378331968bdSArnaldo Carvalho de Melo 			} else {  /* send SIGIO later */
379331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_ASYNC_NOSPACE,
380331968bdSArnaldo Carvalho de Melo 					&sk->sk_socket->flags);
381331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
382331968bdSArnaldo Carvalho de Melo 
383331968bdSArnaldo Carvalho de Melo 				/* Race breaker. If space is freed after
384331968bdSArnaldo Carvalho de Melo 				 * wspace test but before the flags are set,
385331968bdSArnaldo Carvalho de Melo 				 * IO signal will be lost.
386331968bdSArnaldo Carvalho de Melo 				 */
387331968bdSArnaldo Carvalho de Melo 				if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
388331968bdSArnaldo Carvalho de Melo 					mask |= POLLOUT | POLLWRNORM;
389331968bdSArnaldo Carvalho de Melo 			}
390331968bdSArnaldo Carvalho de Melo 		}
391331968bdSArnaldo Carvalho de Melo 	}
392331968bdSArnaldo Carvalho de Melo 	return mask;
393331968bdSArnaldo Carvalho de Melo }
394331968bdSArnaldo Carvalho de Melo 
395f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll);
396f21e68caSArnaldo Carvalho de Melo 
3977c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3987c657876SArnaldo Carvalho de Melo {
3996273172eSArnaldo Carvalho de Melo 	int rc = -ENOTCONN;
4006273172eSArnaldo Carvalho de Melo 
4016273172eSArnaldo Carvalho de Melo 	lock_sock(sk);
4026273172eSArnaldo Carvalho de Melo 
4036273172eSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
4046273172eSArnaldo Carvalho de Melo 		goto out;
4056273172eSArnaldo Carvalho de Melo 
4066273172eSArnaldo Carvalho de Melo 	switch (cmd) {
4076273172eSArnaldo Carvalho de Melo 	case SIOCINQ: {
4086273172eSArnaldo Carvalho de Melo 		struct sk_buff *skb;
4096273172eSArnaldo Carvalho de Melo 		unsigned long amount = 0;
4106273172eSArnaldo Carvalho de Melo 
4116273172eSArnaldo Carvalho de Melo 		skb = skb_peek(&sk->sk_receive_queue);
4126273172eSArnaldo Carvalho de Melo 		if (skb != NULL) {
4136273172eSArnaldo Carvalho de Melo 			/*
4146273172eSArnaldo Carvalho de Melo 			 * We will only return the amount of this packet since
4156273172eSArnaldo Carvalho de Melo 			 * that is all that will be read.
4166273172eSArnaldo Carvalho de Melo 			 */
4176273172eSArnaldo Carvalho de Melo 			amount = skb->len;
4186273172eSArnaldo Carvalho de Melo 		}
4196273172eSArnaldo Carvalho de Melo 		rc = put_user(amount, (int __user *)arg);
4206273172eSArnaldo Carvalho de Melo 	}
4216273172eSArnaldo Carvalho de Melo 		break;
4226273172eSArnaldo Carvalho de Melo 	default:
4236273172eSArnaldo Carvalho de Melo 		rc = -ENOIOCTLCMD;
4246273172eSArnaldo Carvalho de Melo 		break;
4256273172eSArnaldo Carvalho de Melo 	}
4266273172eSArnaldo Carvalho de Melo out:
4276273172eSArnaldo Carvalho de Melo 	release_sock(sk);
4286273172eSArnaldo Carvalho de Melo 	return rc;
4297c657876SArnaldo Carvalho de Melo }
4307c657876SArnaldo Carvalho de Melo 
431f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl);
432f21e68caSArnaldo Carvalho de Melo 
43360fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
43467e6b629SArnaldo Carvalho de Melo 				   char __user *optval, int optlen)
43567e6b629SArnaldo Carvalho de Melo {
43667e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
43767e6b629SArnaldo Carvalho de Melo 	struct dccp_service_list *sl = NULL;
43867e6b629SArnaldo Carvalho de Melo 
43967e6b629SArnaldo Carvalho de Melo 	if (service == DCCP_SERVICE_INVALID_VALUE ||
44067e6b629SArnaldo Carvalho de Melo 	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
44167e6b629SArnaldo Carvalho de Melo 		return -EINVAL;
44267e6b629SArnaldo Carvalho de Melo 
44367e6b629SArnaldo Carvalho de Melo 	if (optlen > sizeof(service)) {
44467e6b629SArnaldo Carvalho de Melo 		sl = kmalloc(optlen, GFP_KERNEL);
44567e6b629SArnaldo Carvalho de Melo 		if (sl == NULL)
44667e6b629SArnaldo Carvalho de Melo 			return -ENOMEM;
44767e6b629SArnaldo Carvalho de Melo 
44867e6b629SArnaldo Carvalho de Melo 		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
44967e6b629SArnaldo Carvalho de Melo 		if (copy_from_user(sl->dccpsl_list,
45067e6b629SArnaldo Carvalho de Melo 				   optval + sizeof(service),
45167e6b629SArnaldo Carvalho de Melo 				   optlen - sizeof(service)) ||
45267e6b629SArnaldo Carvalho de Melo 		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
45367e6b629SArnaldo Carvalho de Melo 			kfree(sl);
45467e6b629SArnaldo Carvalho de Melo 			return -EFAULT;
45567e6b629SArnaldo Carvalho de Melo 		}
45667e6b629SArnaldo Carvalho de Melo 	}
45767e6b629SArnaldo Carvalho de Melo 
45867e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
45967e6b629SArnaldo Carvalho de Melo 	dp->dccps_service = service;
46067e6b629SArnaldo Carvalho de Melo 
46167e6b629SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
46267e6b629SArnaldo Carvalho de Melo 
46367e6b629SArnaldo Carvalho de Melo 	dp->dccps_service_list = sl;
46467e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
46567e6b629SArnaldo Carvalho de Melo 	return 0;
46667e6b629SArnaldo Carvalho de Melo }
46767e6b629SArnaldo Carvalho de Melo 
46829450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
46929450559SGerrit Renker {
47029450559SGerrit Renker 	u8 *list, len;
47129450559SGerrit Renker 	int i, rc;
47229450559SGerrit Renker 
47329450559SGerrit Renker 	if (cscov < 0 || cscov > 15)
47429450559SGerrit Renker 		return -EINVAL;
47529450559SGerrit Renker 	/*
47629450559SGerrit Renker 	 * Populate a list of permissible values, in the range cscov...15. This
47729450559SGerrit Renker 	 * is necessary since feature negotiation of single values only works if
47829450559SGerrit Renker 	 * both sides incidentally choose the same value. Since the list starts
47929450559SGerrit Renker 	 * lowest-value first, negotiation will pick the smallest shared value.
48029450559SGerrit Renker 	 */
48129450559SGerrit Renker 	if (cscov == 0)
48229450559SGerrit Renker 		return 0;
48329450559SGerrit Renker 	len = 16 - cscov;
48429450559SGerrit Renker 
48529450559SGerrit Renker 	list = kmalloc(len, GFP_KERNEL);
48629450559SGerrit Renker 	if (list == NULL)
48729450559SGerrit Renker 		return -ENOBUFS;
48829450559SGerrit Renker 
48929450559SGerrit Renker 	for (i = 0; i < len; i++)
49029450559SGerrit Renker 		list[i] = cscov++;
49129450559SGerrit Renker 
49229450559SGerrit Renker 	rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
49329450559SGerrit Renker 
49429450559SGerrit Renker 	if (rc == 0) {
49529450559SGerrit Renker 		if (rx)
49629450559SGerrit Renker 			dccp_sk(sk)->dccps_pcrlen = cscov;
49729450559SGerrit Renker 		else
49829450559SGerrit Renker 			dccp_sk(sk)->dccps_pcslen = cscov;
49929450559SGerrit Renker 	}
50029450559SGerrit Renker 	kfree(list);
50129450559SGerrit Renker 	return rc;
50229450559SGerrit Renker }
50329450559SGerrit Renker 
504b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type,
505b20a9c24SGerrit Renker 				char __user *optval, int optlen)
506b20a9c24SGerrit Renker {
507b20a9c24SGerrit Renker 	u8 *val;
508b20a9c24SGerrit Renker 	int rc = 0;
509b20a9c24SGerrit Renker 
510b20a9c24SGerrit Renker 	if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
511b20a9c24SGerrit Renker 		return -EINVAL;
512b20a9c24SGerrit Renker 
513b20a9c24SGerrit Renker 	val = kmalloc(optlen, GFP_KERNEL);
514b20a9c24SGerrit Renker 	if (val == NULL)
515b20a9c24SGerrit Renker 		return -ENOMEM;
516b20a9c24SGerrit Renker 
517b20a9c24SGerrit Renker 	if (copy_from_user(val, optval, optlen)) {
518b20a9c24SGerrit Renker 		kfree(val);
519b20a9c24SGerrit Renker 		return -EFAULT;
520b20a9c24SGerrit Renker 	}
521b20a9c24SGerrit Renker 
522b20a9c24SGerrit Renker 	lock_sock(sk);
523b20a9c24SGerrit Renker 	if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
524b20a9c24SGerrit Renker 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
525b20a9c24SGerrit Renker 
526b20a9c24SGerrit Renker 	if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
527b20a9c24SGerrit Renker 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
528b20a9c24SGerrit Renker 	release_sock(sk);
529b20a9c24SGerrit Renker 
530b20a9c24SGerrit Renker 	kfree(val);
531b20a9c24SGerrit Renker 	return rc;
532b20a9c24SGerrit Renker }
533b20a9c24SGerrit Renker 
5343fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
535a1d3a355SArnaldo Carvalho de Melo 		char __user *optval, int optlen)
5367c657876SArnaldo Carvalho de Melo {
53709dbc389SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
53809dbc389SGerrit Renker 	int val, err = 0;
5397c657876SArnaldo Carvalho de Melo 
54019102996SGerrit Renker 	switch (optname) {
54119102996SGerrit Renker 	case DCCP_SOCKOPT_PACKET_SIZE:
54219102996SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
54319102996SGerrit Renker 		return 0;
54419102996SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_L:
54519102996SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_R:
54619102996SGerrit Renker 		DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
54719102996SGerrit Renker 		return 0;
548b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_CCID:
549b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_RX_CCID:
550b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_TX_CCID:
551b20a9c24SGerrit Renker 		return dccp_setsockopt_ccid(sk, optname, optval, optlen);
55219102996SGerrit Renker 	}
55319102996SGerrit Renker 
55419102996SGerrit Renker 	if (optlen < (int)sizeof(int))
555a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
556a84ffe43SArnaldo Carvalho de Melo 
557a84ffe43SArnaldo Carvalho de Melo 	if (get_user(val, (int __user *)optval))
558a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
559a84ffe43SArnaldo Carvalho de Melo 
56067e6b629SArnaldo Carvalho de Melo 	if (optname == DCCP_SOCKOPT_SERVICE)
56167e6b629SArnaldo Carvalho de Melo 		return dccp_setsockopt_service(sk, val, optval, optlen);
562a84ffe43SArnaldo Carvalho de Melo 
56367e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
564a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
565b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
566b8599d20SGerrit Renker 		if (dp->dccps_role != DCCP_ROLE_SERVER)
567b8599d20SGerrit Renker 			err = -EOPNOTSUPP;
568b8599d20SGerrit Renker 		else
569b8599d20SGerrit Renker 			dp->dccps_server_timewait = (val != 0);
570b8599d20SGerrit Renker 		break;
57129450559SGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
57229450559SGerrit Renker 		err = dccp_setsockopt_cscov(sk, val, false);
573d6da3511STomasz Grobelny 		break;
57429450559SGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
57529450559SGerrit Renker 		err = dccp_setsockopt_cscov(sk, val, true);
576d6da3511STomasz Grobelny 		break;
577a84ffe43SArnaldo Carvalho de Melo 	default:
578a84ffe43SArnaldo Carvalho de Melo 		err = -ENOPROTOOPT;
579a84ffe43SArnaldo Carvalho de Melo 		break;
580a84ffe43SArnaldo Carvalho de Melo 	}
581410e27a4SGerrit Renker 	release_sock(sk);
58219102996SGerrit Renker 
583a84ffe43SArnaldo Carvalho de Melo 	return err;
5847c657876SArnaldo Carvalho de Melo }
5857c657876SArnaldo Carvalho de Melo 
5863fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname,
5873fdadf7dSDmitry Mishin 		    char __user *optval, int optlen)
5883fdadf7dSDmitry Mishin {
5893fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
5903fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
5913fdadf7dSDmitry Mishin 							     optname, optval,
5923fdadf7dSDmitry Mishin 							     optlen);
5933fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5943fdadf7dSDmitry Mishin }
595543d9cfeSArnaldo Carvalho de Melo 
596f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt);
597f21e68caSArnaldo Carvalho de Melo 
5983fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5993fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
6003fdadf7dSDmitry Mishin 			   char __user *optval, int optlen)
6013fdadf7dSDmitry Mishin {
602dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
603dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_setsockopt(sk, level, optname,
604dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
6053fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
6063fdadf7dSDmitry Mishin }
607543d9cfeSArnaldo Carvalho de Melo 
6083fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
6093fdadf7dSDmitry Mishin #endif
6103fdadf7dSDmitry Mishin 
61167e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len,
61260fe62e7SAndrea Bittau 				   __be32 __user *optval,
61367e6b629SArnaldo Carvalho de Melo 				   int __user *optlen)
61467e6b629SArnaldo Carvalho de Melo {
61567e6b629SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
61667e6b629SArnaldo Carvalho de Melo 	const struct dccp_service_list *sl;
61767e6b629SArnaldo Carvalho de Melo 	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
61867e6b629SArnaldo Carvalho de Melo 
61967e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
62067e6b629SArnaldo Carvalho de Melo 	if ((sl = dp->dccps_service_list) != NULL) {
62167e6b629SArnaldo Carvalho de Melo 		slen = sl->dccpsl_nr * sizeof(u32);
62267e6b629SArnaldo Carvalho de Melo 		total_len += slen;
62367e6b629SArnaldo Carvalho de Melo 	}
62467e6b629SArnaldo Carvalho de Melo 
62567e6b629SArnaldo Carvalho de Melo 	err = -EINVAL;
62667e6b629SArnaldo Carvalho de Melo 	if (total_len > len)
62767e6b629SArnaldo Carvalho de Melo 		goto out;
62867e6b629SArnaldo Carvalho de Melo 
62967e6b629SArnaldo Carvalho de Melo 	err = 0;
63067e6b629SArnaldo Carvalho de Melo 	if (put_user(total_len, optlen) ||
63167e6b629SArnaldo Carvalho de Melo 	    put_user(dp->dccps_service, optval) ||
63267e6b629SArnaldo Carvalho de Melo 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
63367e6b629SArnaldo Carvalho de Melo 		err = -EFAULT;
63467e6b629SArnaldo Carvalho de Melo out:
63567e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
63667e6b629SArnaldo Carvalho de Melo 	return err;
63767e6b629SArnaldo Carvalho de Melo }
63867e6b629SArnaldo Carvalho de Melo 
6393fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
640a1d3a355SArnaldo Carvalho de Melo 		    char __user *optval, int __user *optlen)
6417c657876SArnaldo Carvalho de Melo {
642a84ffe43SArnaldo Carvalho de Melo 	struct dccp_sock *dp;
643a84ffe43SArnaldo Carvalho de Melo 	int val, len;
6447c657876SArnaldo Carvalho de Melo 
645a84ffe43SArnaldo Carvalho de Melo 	if (get_user(len, optlen))
646a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
647a84ffe43SArnaldo Carvalho de Melo 
64839ebc027SArnaldo Carvalho de Melo 	if (len < (int)sizeof(int))
649a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
650a84ffe43SArnaldo Carvalho de Melo 
651a84ffe43SArnaldo Carvalho de Melo 	dp = dccp_sk(sk);
652a84ffe43SArnaldo Carvalho de Melo 
653a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
654a84ffe43SArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_PACKET_SIZE:
6555aed3243SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
656841bac1dSArnaldo Carvalho de Melo 		return 0;
65788f964dbSArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_SERVICE:
65888f964dbSArnaldo Carvalho de Melo 		return dccp_getsockopt_service(sk, len,
65960fe62e7SAndrea Bittau 					       (__be32 __user *)optval, optlen);
6607c559a9eSGerrit Renker 	case DCCP_SOCKOPT_GET_CUR_MPS:
6617c559a9eSGerrit Renker 		val = dp->dccps_mss_cache;
6627c559a9eSGerrit Renker 		break;
663d90ebcbfSGerrit Renker 	case DCCP_SOCKOPT_AVAILABLE_CCIDS:
664d90ebcbfSGerrit Renker 		return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
66571c262a3SGerrit Renker 	case DCCP_SOCKOPT_TX_CCID:
66671c262a3SGerrit Renker 		val = ccid_get_current_tx_ccid(dp);
66771c262a3SGerrit Renker 		if (val < 0)
66871c262a3SGerrit Renker 			return -ENOPROTOOPT;
66971c262a3SGerrit Renker 		break;
67071c262a3SGerrit Renker 	case DCCP_SOCKOPT_RX_CCID:
67171c262a3SGerrit Renker 		val = ccid_get_current_rx_ccid(dp);
67271c262a3SGerrit Renker 		if (val < 0)
67371c262a3SGerrit Renker 			return -ENOPROTOOPT;
67471c262a3SGerrit Renker 		break;
675b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
676b8599d20SGerrit Renker 		val = dp->dccps_server_timewait;
677b8599d20SGerrit Renker 		break;
6786f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
6796f4e5fffSGerrit Renker 		val = dp->dccps_pcslen;
6806f4e5fffSGerrit Renker 		break;
6816f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
6826f4e5fffSGerrit Renker 		val = dp->dccps_pcrlen;
6836f4e5fffSGerrit Renker 		break;
68488f964dbSArnaldo Carvalho de Melo 	case 128 ... 191:
68588f964dbSArnaldo Carvalho de Melo 		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
68688f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
68788f964dbSArnaldo Carvalho de Melo 	case 192 ... 255:
68888f964dbSArnaldo Carvalho de Melo 		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
68988f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
690a84ffe43SArnaldo Carvalho de Melo 	default:
691a84ffe43SArnaldo Carvalho de Melo 		return -ENOPROTOOPT;
692a84ffe43SArnaldo Carvalho de Melo 	}
693a84ffe43SArnaldo Carvalho de Melo 
69479133506SGerrit Renker 	len = sizeof(val);
695a84ffe43SArnaldo Carvalho de Melo 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
696a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
697a84ffe43SArnaldo Carvalho de Melo 
698a84ffe43SArnaldo Carvalho de Melo 	return 0;
6997c657876SArnaldo Carvalho de Melo }
7007c657876SArnaldo Carvalho de Melo 
7013fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname,
7023fdadf7dSDmitry Mishin 		    char __user *optval, int __user *optlen)
7033fdadf7dSDmitry Mishin {
7043fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
7053fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
7063fdadf7dSDmitry Mishin 							     optname, optval,
7073fdadf7dSDmitry Mishin 							     optlen);
7083fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
7093fdadf7dSDmitry Mishin }
710543d9cfeSArnaldo Carvalho de Melo 
711f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt);
712f21e68caSArnaldo Carvalho de Melo 
7133fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
7143fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
7153fdadf7dSDmitry Mishin 			   char __user *optval, int __user *optlen)
7163fdadf7dSDmitry Mishin {
717dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
718dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_getsockopt(sk, level, optname,
719dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
7203fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
7213fdadf7dSDmitry Mishin }
722543d9cfeSArnaldo Carvalho de Melo 
7233fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
7243fdadf7dSDmitry Mishin #endif
7253fdadf7dSDmitry Mishin 
7267c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7277c657876SArnaldo Carvalho de Melo 		 size_t len)
7287c657876SArnaldo Carvalho de Melo {
7297c657876SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
7307c657876SArnaldo Carvalho de Melo 	const int flags = msg->msg_flags;
7317c657876SArnaldo Carvalho de Melo 	const int noblock = flags & MSG_DONTWAIT;
7327c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
7337c657876SArnaldo Carvalho de Melo 	int rc, size;
7347c657876SArnaldo Carvalho de Melo 	long timeo;
7357c657876SArnaldo Carvalho de Melo 
7367c657876SArnaldo Carvalho de Melo 	if (len > dp->dccps_mss_cache)
7377c657876SArnaldo Carvalho de Melo 		return -EMSGSIZE;
7387c657876SArnaldo Carvalho de Melo 
7397c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
740b1308dc0SIan McDonald 
741410e27a4SGerrit Renker 	if (sysctl_dccp_tx_qlen &&
742410e27a4SGerrit Renker 	    (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
743b1308dc0SIan McDonald 		rc = -EAGAIN;
744b1308dc0SIan McDonald 		goto out_release;
745b1308dc0SIan McDonald 	}
746b1308dc0SIan McDonald 
74727258ee5SArnaldo Carvalho de Melo 	timeo = sock_sndtimeo(sk, noblock);
7487c657876SArnaldo Carvalho de Melo 
7497c657876SArnaldo Carvalho de Melo 	/*
7507c657876SArnaldo Carvalho de Melo 	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
7517c657876SArnaldo Carvalho de Melo 	 * so that the trick in dccp_rcv_request_sent_state_process.
7527c657876SArnaldo Carvalho de Melo 	 */
7537c657876SArnaldo Carvalho de Melo 	/* Wait for a connection to finish. */
754cecd8d0eSGerrit Renker 	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7557c657876SArnaldo Carvalho de Melo 		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
75627258ee5SArnaldo Carvalho de Melo 			goto out_release;
7577c657876SArnaldo Carvalho de Melo 
7587c657876SArnaldo Carvalho de Melo 	size = sk->sk_prot->max_header + len;
7597c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7607c657876SArnaldo Carvalho de Melo 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
7617c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7627c657876SArnaldo Carvalho de Melo 	if (skb == NULL)
7637c657876SArnaldo Carvalho de Melo 		goto out_release;
7647c657876SArnaldo Carvalho de Melo 
7657c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
7667c657876SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
76727258ee5SArnaldo Carvalho de Melo 	if (rc != 0)
76827258ee5SArnaldo Carvalho de Melo 		goto out_discard;
7697c657876SArnaldo Carvalho de Melo 
770410e27a4SGerrit Renker 	skb_queue_tail(&sk->sk_write_queue, skb);
771410e27a4SGerrit Renker 	dccp_write_xmit(sk,0);
7727c657876SArnaldo Carvalho de Melo out_release:
7737c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7747c657876SArnaldo Carvalho de Melo 	return rc ? : len;
77527258ee5SArnaldo Carvalho de Melo out_discard:
77627258ee5SArnaldo Carvalho de Melo 	kfree_skb(skb);
7777c657876SArnaldo Carvalho de Melo 	goto out_release;
7787c657876SArnaldo Carvalho de Melo }
7797c657876SArnaldo Carvalho de Melo 
780f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg);
781f21e68caSArnaldo Carvalho de Melo 
7827c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7837c657876SArnaldo Carvalho de Melo 		 size_t len, int nonblock, int flags, int *addr_len)
7847c657876SArnaldo Carvalho de Melo {
7857c657876SArnaldo Carvalho de Melo 	const struct dccp_hdr *dh;
7867c657876SArnaldo Carvalho de Melo 	long timeo;
7877c657876SArnaldo Carvalho de Melo 
7887c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7897c657876SArnaldo Carvalho de Melo 
790531669a0SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
791531669a0SArnaldo Carvalho de Melo 		len = -ENOTCONN;
7927c657876SArnaldo Carvalho de Melo 		goto out;
793531669a0SArnaldo Carvalho de Melo 	}
7947c657876SArnaldo Carvalho de Melo 
7957c657876SArnaldo Carvalho de Melo 	timeo = sock_rcvtimeo(sk, nonblock);
7967c657876SArnaldo Carvalho de Melo 
7977c657876SArnaldo Carvalho de Melo 	do {
798531669a0SArnaldo Carvalho de Melo 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
7997c657876SArnaldo Carvalho de Melo 
800531669a0SArnaldo Carvalho de Melo 		if (skb == NULL)
801531669a0SArnaldo Carvalho de Melo 			goto verify_sock_status;
8027c657876SArnaldo Carvalho de Melo 
8037c657876SArnaldo Carvalho de Melo 		dh = dccp_hdr(skb);
8047c657876SArnaldo Carvalho de Melo 
8050c869620SGerrit Renker 		switch (dh->dccph_type) {
8060c869620SGerrit Renker 		case DCCP_PKT_DATA:
8070c869620SGerrit Renker 		case DCCP_PKT_DATAACK:
8087c657876SArnaldo Carvalho de Melo 			goto found_ok_skb;
8097c657876SArnaldo Carvalho de Melo 
8100c869620SGerrit Renker 		case DCCP_PKT_CLOSE:
8110c869620SGerrit Renker 		case DCCP_PKT_CLOSEREQ:
8120c869620SGerrit Renker 			if (!(flags & MSG_PEEK))
8130c869620SGerrit Renker 				dccp_finish_passive_close(sk);
8140c869620SGerrit Renker 			/* fall through */
8150c869620SGerrit Renker 		case DCCP_PKT_RESET:
8160c869620SGerrit Renker 			dccp_pr_debug("found fin (%s) ok!\n",
8170c869620SGerrit Renker 				      dccp_packet_name(dh->dccph_type));
818531669a0SArnaldo Carvalho de Melo 			len = 0;
8197c657876SArnaldo Carvalho de Melo 			goto found_fin_ok;
8200c869620SGerrit Renker 		default:
8217690af3fSArnaldo Carvalho de Melo 			dccp_pr_debug("packet_type=%s\n",
8227690af3fSArnaldo Carvalho de Melo 				      dccp_packet_name(dh->dccph_type));
823624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8240c869620SGerrit Renker 		}
825531669a0SArnaldo Carvalho de Melo verify_sock_status:
826531669a0SArnaldo Carvalho de Melo 		if (sock_flag(sk, SOCK_DONE)) {
827531669a0SArnaldo Carvalho de Melo 			len = 0;
8287c657876SArnaldo Carvalho de Melo 			break;
8297c657876SArnaldo Carvalho de Melo 		}
8307c657876SArnaldo Carvalho de Melo 
831531669a0SArnaldo Carvalho de Melo 		if (sk->sk_err) {
832531669a0SArnaldo Carvalho de Melo 			len = sock_error(sk);
8337c657876SArnaldo Carvalho de Melo 			break;
834531669a0SArnaldo Carvalho de Melo 		}
835531669a0SArnaldo Carvalho de Melo 
836531669a0SArnaldo Carvalho de Melo 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
837531669a0SArnaldo Carvalho de Melo 			len = 0;
838531669a0SArnaldo Carvalho de Melo 			break;
839531669a0SArnaldo Carvalho de Melo 		}
8407c657876SArnaldo Carvalho de Melo 
8417c657876SArnaldo Carvalho de Melo 		if (sk->sk_state == DCCP_CLOSED) {
8427c657876SArnaldo Carvalho de Melo 			if (!sock_flag(sk, SOCK_DONE)) {
8437c657876SArnaldo Carvalho de Melo 				/* This occurs when user tries to read
8447c657876SArnaldo Carvalho de Melo 				 * from never connected socket.
8457c657876SArnaldo Carvalho de Melo 				 */
846531669a0SArnaldo Carvalho de Melo 				len = -ENOTCONN;
8477c657876SArnaldo Carvalho de Melo 				break;
8487c657876SArnaldo Carvalho de Melo 			}
849531669a0SArnaldo Carvalho de Melo 			len = 0;
8507c657876SArnaldo Carvalho de Melo 			break;
8517c657876SArnaldo Carvalho de Melo 		}
8527c657876SArnaldo Carvalho de Melo 
8537c657876SArnaldo Carvalho de Melo 		if (!timeo) {
854531669a0SArnaldo Carvalho de Melo 			len = -EAGAIN;
8557c657876SArnaldo Carvalho de Melo 			break;
8567c657876SArnaldo Carvalho de Melo 		}
8577c657876SArnaldo Carvalho de Melo 
8587c657876SArnaldo Carvalho de Melo 		if (signal_pending(current)) {
859531669a0SArnaldo Carvalho de Melo 			len = sock_intr_errno(timeo);
8607c657876SArnaldo Carvalho de Melo 			break;
8617c657876SArnaldo Carvalho de Melo 		}
8627c657876SArnaldo Carvalho de Melo 
8637c657876SArnaldo Carvalho de Melo 		sk_wait_data(sk, &timeo);
8647c657876SArnaldo Carvalho de Melo 		continue;
8657c657876SArnaldo Carvalho de Melo 	found_ok_skb:
866531669a0SArnaldo Carvalho de Melo 		if (len > skb->len)
867531669a0SArnaldo Carvalho de Melo 			len = skb->len;
868531669a0SArnaldo Carvalho de Melo 		else if (len < skb->len)
869531669a0SArnaldo Carvalho de Melo 			msg->msg_flags |= MSG_TRUNC;
8707c657876SArnaldo Carvalho de Melo 
871531669a0SArnaldo Carvalho de Melo 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
8727c657876SArnaldo Carvalho de Melo 			/* Exception. Bailout! */
873531669a0SArnaldo Carvalho de Melo 			len = -EFAULT;
8747c657876SArnaldo Carvalho de Melo 			break;
8757c657876SArnaldo Carvalho de Melo 		}
8767c657876SArnaldo Carvalho de Melo 	found_fin_ok:
8777c657876SArnaldo Carvalho de Melo 		if (!(flags & MSG_PEEK))
878624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8797c657876SArnaldo Carvalho de Melo 		break;
880531669a0SArnaldo Carvalho de Melo 	} while (1);
8817c657876SArnaldo Carvalho de Melo out:
8827c657876SArnaldo Carvalho de Melo 	release_sock(sk);
883531669a0SArnaldo Carvalho de Melo 	return len;
8847c657876SArnaldo Carvalho de Melo }
8857c657876SArnaldo Carvalho de Melo 
886f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg);
887f21e68caSArnaldo Carvalho de Melo 
888f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog)
8897c657876SArnaldo Carvalho de Melo {
8907c657876SArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
8917c657876SArnaldo Carvalho de Melo 	unsigned char old_state;
8927c657876SArnaldo Carvalho de Melo 	int err;
8937c657876SArnaldo Carvalho de Melo 
8947c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
8957c657876SArnaldo Carvalho de Melo 
8967c657876SArnaldo Carvalho de Melo 	err = -EINVAL;
8977c657876SArnaldo Carvalho de Melo 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
8987c657876SArnaldo Carvalho de Melo 		goto out;
8997c657876SArnaldo Carvalho de Melo 
9007c657876SArnaldo Carvalho de Melo 	old_state = sk->sk_state;
9017c657876SArnaldo Carvalho de Melo 	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
9027c657876SArnaldo Carvalho de Melo 		goto out;
9037c657876SArnaldo Carvalho de Melo 
9047c657876SArnaldo Carvalho de Melo 	/* Really, if the socket is already in listen state
9057c657876SArnaldo Carvalho de Melo 	 * we can only allow the backlog to be adjusted.
9067c657876SArnaldo Carvalho de Melo 	 */
9077c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_LISTEN) {
9087c657876SArnaldo Carvalho de Melo 		/*
9097c657876SArnaldo Carvalho de Melo 		 * FIXME: here it probably should be sk->sk_prot->listen_start
9107c657876SArnaldo Carvalho de Melo 		 * see tcp_listen_start
9117c657876SArnaldo Carvalho de Melo 		 */
91272a3effaSEric Dumazet 		err = dccp_listen_start(sk, backlog);
9137c657876SArnaldo Carvalho de Melo 		if (err)
9147c657876SArnaldo Carvalho de Melo 			goto out;
9157c657876SArnaldo Carvalho de Melo 	}
9167c657876SArnaldo Carvalho de Melo 	sk->sk_max_ack_backlog = backlog;
9177c657876SArnaldo Carvalho de Melo 	err = 0;
9187c657876SArnaldo Carvalho de Melo 
9197c657876SArnaldo Carvalho de Melo out:
9207c657876SArnaldo Carvalho de Melo 	release_sock(sk);
9217c657876SArnaldo Carvalho de Melo 	return err;
9227c657876SArnaldo Carvalho de Melo }
9237c657876SArnaldo Carvalho de Melo 
924f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen);
925f21e68caSArnaldo Carvalho de Melo 
9260c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk)
9277c657876SArnaldo Carvalho de Melo {
9280c869620SGerrit Renker 	u8 next_state = DCCP_CLOSED;
9297c657876SArnaldo Carvalho de Melo 
9300c869620SGerrit Renker 	switch (sk->sk_state) {
9310c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
9320c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
9330c869620SGerrit Renker 		dccp_finish_passive_close(sk);
9340c869620SGerrit Renker 		break;
9350c869620SGerrit Renker 	case DCCP_PARTOPEN:
9360c869620SGerrit Renker 		dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
9370c869620SGerrit Renker 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
9380c869620SGerrit Renker 		/* fall through */
9390c869620SGerrit Renker 	case DCCP_OPEN:
9400c869620SGerrit Renker 		dccp_send_close(sk, 1);
9417c657876SArnaldo Carvalho de Melo 
942b8599d20SGerrit Renker 		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
943b8599d20SGerrit Renker 		    !dccp_sk(sk)->dccps_server_timewait)
9440c869620SGerrit Renker 			next_state = DCCP_ACTIVE_CLOSEREQ;
9450c869620SGerrit Renker 		else
9460c869620SGerrit Renker 			next_state = DCCP_CLOSING;
9470c869620SGerrit Renker 		/* fall through */
9480c869620SGerrit Renker 	default:
9490c869620SGerrit Renker 		dccp_set_state(sk, next_state);
9500c869620SGerrit Renker 	}
9517c657876SArnaldo Carvalho de Melo }
9527c657876SArnaldo Carvalho de Melo 
9537c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout)
9547c657876SArnaldo Carvalho de Melo {
95597e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
9567c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
957d83bd95bSGerrit Renker 	u32 data_was_unread = 0;
958134af346SHerbert Xu 	int state;
9597c657876SArnaldo Carvalho de Melo 
9607c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
9617c657876SArnaldo Carvalho de Melo 
9627c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
9637c657876SArnaldo Carvalho de Melo 
9647c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
9657c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
9667c657876SArnaldo Carvalho de Melo 
9677c657876SArnaldo Carvalho de Melo 		/* Special case. */
9687c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
9697c657876SArnaldo Carvalho de Melo 
9707c657876SArnaldo Carvalho de Melo 		goto adjudge_to_death;
9717c657876SArnaldo Carvalho de Melo 	}
9727c657876SArnaldo Carvalho de Melo 
97397e5848dSIan McDonald 	sk_stop_timer(sk, &dp->dccps_xmit_timer);
97497e5848dSIan McDonald 
9757c657876SArnaldo Carvalho de Melo 	/*
9767c657876SArnaldo Carvalho de Melo 	 * We need to flush the recv. buffs.  We do this only on the
9777c657876SArnaldo Carvalho de Melo 	 * descriptor close, not protocol-sourced closes, because the
9787c657876SArnaldo Carvalho de Melo 	  *reader process may not have drained the data yet!
9797c657876SArnaldo Carvalho de Melo 	 */
9807c657876SArnaldo Carvalho de Melo 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
981d83bd95bSGerrit Renker 		data_was_unread += skb->len;
9827c657876SArnaldo Carvalho de Melo 		__kfree_skb(skb);
9837c657876SArnaldo Carvalho de Melo 	}
9847c657876SArnaldo Carvalho de Melo 
985d83bd95bSGerrit Renker 	if (data_was_unread) {
986d83bd95bSGerrit Renker 		/* Unread data was tossed, send an appropriate Reset Code */
987d83bd95bSGerrit Renker 		DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
988d83bd95bSGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
989d83bd95bSGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
990d83bd95bSGerrit Renker 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
9917c657876SArnaldo Carvalho de Melo 		/* Check zero linger _after_ checking for unread data. */
9927c657876SArnaldo Carvalho de Melo 		sk->sk_prot->disconnect(sk, 0);
9930c869620SGerrit Renker 	} else if (sk->sk_state != DCCP_CLOSED) {
9940c869620SGerrit Renker 		dccp_terminate_connection(sk);
9957c657876SArnaldo Carvalho de Melo 	}
9967c657876SArnaldo Carvalho de Melo 
9977c657876SArnaldo Carvalho de Melo 	sk_stream_wait_close(sk, timeout);
9987c657876SArnaldo Carvalho de Melo 
9997c657876SArnaldo Carvalho de Melo adjudge_to_death:
1000134af346SHerbert Xu 	state = sk->sk_state;
1001134af346SHerbert Xu 	sock_hold(sk);
1002134af346SHerbert Xu 	sock_orphan(sk);
1003134af346SHerbert Xu 	atomic_inc(sk->sk_prot->orphan_count);
1004134af346SHerbert Xu 
10057ad07e7cSArnaldo Carvalho de Melo 	/*
10067ad07e7cSArnaldo Carvalho de Melo 	 * It is the last release_sock in its life. It will remove backlog.
10077ad07e7cSArnaldo Carvalho de Melo 	 */
10087c657876SArnaldo Carvalho de Melo 	release_sock(sk);
10097c657876SArnaldo Carvalho de Melo 	/*
10107c657876SArnaldo Carvalho de Melo 	 * Now socket is owned by kernel and we acquire BH lock
10117c657876SArnaldo Carvalho de Melo 	 * to finish close. No need to check for user refs.
10127c657876SArnaldo Carvalho de Melo 	 */
10137c657876SArnaldo Carvalho de Melo 	local_bh_disable();
10147c657876SArnaldo Carvalho de Melo 	bh_lock_sock(sk);
1015547b792cSIlpo Järvinen 	WARN_ON(sock_owned_by_user(sk));
10167c657876SArnaldo Carvalho de Melo 
1017134af346SHerbert Xu 	/* Have we already been destroyed by a softirq or backlog? */
1018134af346SHerbert Xu 	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1019134af346SHerbert Xu 		goto out;
10207c657876SArnaldo Carvalho de Melo 
10217c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_CLOSED)
10227c657876SArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
10237c657876SArnaldo Carvalho de Melo 
10247c657876SArnaldo Carvalho de Melo 	/* Otherwise, socket is reprieved until protocol close. */
10257c657876SArnaldo Carvalho de Melo 
1026134af346SHerbert Xu out:
10277c657876SArnaldo Carvalho de Melo 	bh_unlock_sock(sk);
10287c657876SArnaldo Carvalho de Melo 	local_bh_enable();
10297c657876SArnaldo Carvalho de Melo 	sock_put(sk);
10307c657876SArnaldo Carvalho de Melo }
10317c657876SArnaldo Carvalho de Melo 
1032f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close);
1033f21e68caSArnaldo Carvalho de Melo 
10347c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how)
10357c657876SArnaldo Carvalho de Melo {
10368e8c71f1SGerrit Renker 	dccp_pr_debug("called shutdown(%x)\n", how);
10377c657876SArnaldo Carvalho de Melo }
10387c657876SArnaldo Carvalho de Melo 
1039f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown);
1040f21e68caSArnaldo Carvalho de Melo 
104124e8b7e4SYOSHIFUJI Hideaki static inline int dccp_mib_init(void)
10427c657876SArnaldo Carvalho de Melo {
104324e8b7e4SYOSHIFUJI Hideaki 	return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
10447c657876SArnaldo Carvalho de Melo }
10457c657876SArnaldo Carvalho de Melo 
104624e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void)
104746f09ffaSArnaldo Carvalho de Melo {
104824e8b7e4SYOSHIFUJI Hideaki 	snmp_mib_free((void**)dccp_statistics);
104946f09ffaSArnaldo Carvalho de Melo }
105046f09ffaSArnaldo Carvalho de Melo 
10517c657876SArnaldo Carvalho de Melo static int thash_entries;
10527c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444);
10537c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
10547c657876SArnaldo Carvalho de Melo 
1055a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG
10567c657876SArnaldo Carvalho de Melo int dccp_debug;
105743264991SGerrit Renker module_param(dccp_debug, bool, 0644);
10587c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1059f21e68caSArnaldo Carvalho de Melo 
1060f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug);
1061a1d3a355SArnaldo Carvalho de Melo #endif
10627c657876SArnaldo Carvalho de Melo 
10637c657876SArnaldo Carvalho de Melo static int __init dccp_init(void)
10647c657876SArnaldo Carvalho de Melo {
10657c657876SArnaldo Carvalho de Melo 	unsigned long goal;
10667c657876SArnaldo Carvalho de Melo 	int ehash_order, bhash_order, i;
1067b61fafc4SArnaldo Carvalho de Melo 	int rc = -ENOBUFS;
10687c657876SArnaldo Carvalho de Melo 
1069028b0275SPatrick McHardy 	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1070028b0275SPatrick McHardy 		     FIELD_SIZEOF(struct sk_buff, cb));
1071028b0275SPatrick McHardy 
10725caea4eaSEric Dumazet 	inet_hashinfo_init(&dccp_hashinfo);
10737690af3fSArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep =
10747690af3fSArnaldo Carvalho de Melo 		kmem_cache_create("dccp_bind_bucket",
10757690af3fSArnaldo Carvalho de Melo 				  sizeof(struct inet_bind_bucket), 0,
107620c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN, NULL);
10777c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bind_bucket_cachep)
1078b61fafc4SArnaldo Carvalho de Melo 		goto out;
10797c657876SArnaldo Carvalho de Melo 
10807c657876SArnaldo Carvalho de Melo 	/*
10817c657876SArnaldo Carvalho de Melo 	 * Size and allocate the main established and bind bucket
10827c657876SArnaldo Carvalho de Melo 	 * hash tables.
10837c657876SArnaldo Carvalho de Melo 	 *
10847c657876SArnaldo Carvalho de Melo 	 * The methodology is similar to that of the buffer cache.
10857c657876SArnaldo Carvalho de Melo 	 */
10867c657876SArnaldo Carvalho de Melo 	if (num_physpages >= (128 * 1024))
10877c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (21 - PAGE_SHIFT);
10887c657876SArnaldo Carvalho de Melo 	else
10897c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (23 - PAGE_SHIFT);
10907c657876SArnaldo Carvalho de Melo 
10917c657876SArnaldo Carvalho de Melo 	if (thash_entries)
10927690af3fSArnaldo Carvalho de Melo 		goal = (thash_entries *
10937690af3fSArnaldo Carvalho de Melo 			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
10947c657876SArnaldo Carvalho de Melo 	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
10957c657876SArnaldo Carvalho de Melo 		;
10967c657876SArnaldo Carvalho de Melo 	do {
10977c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
10987c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_ehash_bucket);
10997690af3fSArnaldo Carvalho de Melo 		while (dccp_hashinfo.ehash_size &
11007690af3fSArnaldo Carvalho de Melo 		       (dccp_hashinfo.ehash_size - 1))
11017c657876SArnaldo Carvalho de Melo 			dccp_hashinfo.ehash_size--;
11027c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
11037c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, ehash_order);
11047c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
11057c657876SArnaldo Carvalho de Melo 
11067c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.ehash) {
110759348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP established hash table");
11087c657876SArnaldo Carvalho de Melo 		goto out_free_bind_bucket_cachep;
11097c657876SArnaldo Carvalho de Melo 	}
11107c657876SArnaldo Carvalho de Melo 
1111dbca9b27SEric Dumazet 	for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
11123ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
11133ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
11147c657876SArnaldo Carvalho de Melo 	}
11157c657876SArnaldo Carvalho de Melo 
1116230140cfSEric Dumazet 	if (inet_ehash_locks_alloc(&dccp_hashinfo))
1117230140cfSEric Dumazet 			goto out_free_dccp_ehash;
1118230140cfSEric Dumazet 
11197c657876SArnaldo Carvalho de Melo 	bhash_order = ehash_order;
11207c657876SArnaldo Carvalho de Melo 
11217c657876SArnaldo Carvalho de Melo 	do {
11227c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
11237c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_bind_hashbucket);
11247690af3fSArnaldo Carvalho de Melo 		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
11257690af3fSArnaldo Carvalho de Melo 		    bhash_order > 0)
11267c657876SArnaldo Carvalho de Melo 			continue;
11277c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
11287c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, bhash_order);
11297c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
11307c657876SArnaldo Carvalho de Melo 
11317c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bhash) {
113259348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP bind hash table");
1133230140cfSEric Dumazet 		goto out_free_dccp_locks;
11347c657876SArnaldo Carvalho de Melo 	}
11357c657876SArnaldo Carvalho de Melo 
11367c657876SArnaldo Carvalho de Melo 	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
11377c657876SArnaldo Carvalho de Melo 		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
11387c657876SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
11397c657876SArnaldo Carvalho de Melo 	}
11407c657876SArnaldo Carvalho de Melo 
114146f09ffaSArnaldo Carvalho de Melo 	rc = dccp_mib_init();
1142fa23e2ecSArnaldo Carvalho de Melo 	if (rc)
11437c657876SArnaldo Carvalho de Melo 		goto out_free_dccp_bhash;
11447c657876SArnaldo Carvalho de Melo 
11459b07ef5dSArnaldo Carvalho de Melo 	rc = dccp_ackvec_init();
11467c657876SArnaldo Carvalho de Melo 	if (rc)
1147b61fafc4SArnaldo Carvalho de Melo 		goto out_free_dccp_mib;
11489b07ef5dSArnaldo Carvalho de Melo 
1149e55d912fSArnaldo Carvalho de Melo 	rc = dccp_sysctl_init();
11509b07ef5dSArnaldo Carvalho de Melo 	if (rc)
11519b07ef5dSArnaldo Carvalho de Melo 		goto out_ackvec_exit;
11524c70f383SGerrit Renker 
11534c70f383SGerrit Renker 	dccp_timestamping_init();
11547c657876SArnaldo Carvalho de Melo out:
11557c657876SArnaldo Carvalho de Melo 	return rc;
11569b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit:
11579b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1158b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib:
115946f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
11607c657876SArnaldo Carvalho de Melo out_free_dccp_bhash:
11617c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
11627c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bhash = NULL;
1163230140cfSEric Dumazet out_free_dccp_locks:
1164230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11657c657876SArnaldo Carvalho de Melo out_free_dccp_ehash:
11667c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
11677c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.ehash = NULL;
11687c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep:
11697c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11707c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep = NULL;
11717c657876SArnaldo Carvalho de Melo 	goto out;
11727c657876SArnaldo Carvalho de Melo }
11737c657876SArnaldo Carvalho de Melo 
11747c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void)
11757c657876SArnaldo Carvalho de Melo {
117646f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
1177725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash,
1178725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.bhash_size *
1179725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_bind_hashbucket)));
1180725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash,
1181725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.ehash_size *
1182725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_ehash_bucket)));
1183230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11847c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11859b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1186e55d912fSArnaldo Carvalho de Melo 	dccp_sysctl_exit();
11877c657876SArnaldo Carvalho de Melo }
11887c657876SArnaldo Carvalho de Melo 
11897c657876SArnaldo Carvalho de Melo module_init(dccp_init);
11907c657876SArnaldo Carvalho de Melo module_exit(dccp_fini);
11917c657876SArnaldo Carvalho de Melo 
11927c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
11937c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
11947c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1195