xref: /openbmc/linux/net/dccp/proto.c (revision 3ab5aee7)
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 
47075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
48075ae866SArnaldo Carvalho de Melo 	.lhash_lock	= RW_LOCK_UNLOCKED,
49075ae866SArnaldo Carvalho de Melo 	.lhash_users	= ATOMIC_INIT(0),
50075ae866SArnaldo Carvalho de Melo 	.lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
51075ae866SArnaldo Carvalho de Melo };
52075ae866SArnaldo Carvalho de Melo 
53075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo);
54075ae866SArnaldo Carvalho de Melo 
55b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */
56b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5;
57b1308dc0SIan McDonald 
58c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state)
59c25a18baSArnaldo Carvalho de Melo {
60c25a18baSArnaldo Carvalho de Melo 	const int oldstate = sk->sk_state;
61c25a18baSArnaldo Carvalho de Melo 
62f11135a3SGerrit Renker 	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
63c25a18baSArnaldo Carvalho de Melo 		      dccp_state_name(oldstate), dccp_state_name(state));
64c25a18baSArnaldo Carvalho de Melo 	WARN_ON(state == oldstate);
65c25a18baSArnaldo Carvalho de Melo 
66c25a18baSArnaldo Carvalho de Melo 	switch (state) {
67c25a18baSArnaldo Carvalho de Melo 	case DCCP_OPEN:
68c25a18baSArnaldo Carvalho de Melo 		if (oldstate != DCCP_OPEN)
69c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
70c25a18baSArnaldo Carvalho de Melo 		break;
71c25a18baSArnaldo Carvalho de Melo 
72c25a18baSArnaldo Carvalho de Melo 	case DCCP_CLOSED:
730c869620SGerrit Renker 		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
740c869620SGerrit Renker 		    oldstate == DCCP_CLOSING)
75c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
76c25a18baSArnaldo Carvalho de Melo 
77c25a18baSArnaldo Carvalho de Melo 		sk->sk_prot->unhash(sk);
78c25a18baSArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash != NULL &&
79c25a18baSArnaldo Carvalho de Melo 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
80ab1e0a13SArnaldo Carvalho de Melo 			inet_put_port(sk);
81c25a18baSArnaldo Carvalho de Melo 		/* fall through */
82c25a18baSArnaldo Carvalho de Melo 	default:
83c25a18baSArnaldo Carvalho de Melo 		if (oldstate == DCCP_OPEN)
84c25a18baSArnaldo Carvalho de Melo 			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
85c25a18baSArnaldo Carvalho de Melo 	}
86c25a18baSArnaldo Carvalho de Melo 
87c25a18baSArnaldo Carvalho de Melo 	/* Change state AFTER socket is unhashed to avoid closed
88c25a18baSArnaldo Carvalho de Melo 	 * socket sitting in hash tables.
89c25a18baSArnaldo Carvalho de Melo 	 */
90c25a18baSArnaldo Carvalho de Melo 	sk->sk_state = state;
91c25a18baSArnaldo Carvalho de Melo }
92c25a18baSArnaldo Carvalho de Melo 
93c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state);
94c25a18baSArnaldo Carvalho de Melo 
950c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk)
960c869620SGerrit Renker {
970c869620SGerrit Renker 	switch (sk->sk_state) {
980c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
990c869620SGerrit Renker 		/* Node (client or server) has received Close packet. */
1000c869620SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
1010c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
1020c869620SGerrit Renker 		break;
1030c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
1040c869620SGerrit Renker 		/*
1050c869620SGerrit Renker 		 * Client received CloseReq. We set the `active' flag so that
1060c869620SGerrit Renker 		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
1070c869620SGerrit Renker 		 */
1080c869620SGerrit Renker 		dccp_send_close(sk, 1);
1090c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSING);
1100c869620SGerrit Renker 	}
1110c869620SGerrit Renker }
1120c869620SGerrit Renker 
113c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk)
114c25a18baSArnaldo Carvalho de Melo {
115c25a18baSArnaldo Carvalho de Melo 	dccp_set_state(sk, DCCP_CLOSED);
116c25a18baSArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
117c25a18baSArnaldo Carvalho de Melo 
118c25a18baSArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
119c25a18baSArnaldo Carvalho de Melo 
120c25a18baSArnaldo Carvalho de Melo 	if (!sock_flag(sk, SOCK_DEAD))
121c25a18baSArnaldo Carvalho de Melo 		sk->sk_state_change(sk);
122c25a18baSArnaldo Carvalho de Melo 	else
123c25a18baSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
124c25a18baSArnaldo Carvalho de Melo }
125c25a18baSArnaldo Carvalho de Melo 
126c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done);
127c25a18baSArnaldo Carvalho de Melo 
1287c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type)
1297c657876SArnaldo Carvalho de Melo {
1307c657876SArnaldo Carvalho de Melo 	static const char *dccp_packet_names[] = {
1317c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_REQUEST]  = "REQUEST",
1327c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESPONSE] = "RESPONSE",
1337c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATA]	    = "DATA",
1347c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_ACK]	    = "ACK",
1357c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATAACK]  = "DATAACK",
1367c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
1377c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSE]    = "CLOSE",
1387c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESET]    = "RESET",
1397c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNC]	    = "SYNC",
1407c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNCACK]  = "SYNCACK",
1417c657876SArnaldo Carvalho de Melo 	};
1427c657876SArnaldo Carvalho de Melo 
1437c657876SArnaldo Carvalho de Melo 	if (type >= DCCP_NR_PKT_TYPES)
1447c657876SArnaldo Carvalho de Melo 		return "INVALID";
1457c657876SArnaldo Carvalho de Melo 	else
1467c657876SArnaldo Carvalho de Melo 		return dccp_packet_names[type];
1477c657876SArnaldo Carvalho de Melo }
1487c657876SArnaldo Carvalho de Melo 
1497c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name);
1507c657876SArnaldo Carvalho de Melo 
1517c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state)
1527c657876SArnaldo Carvalho de Melo {
1537c657876SArnaldo Carvalho de Melo 	static char *dccp_state_names[] = {
1547c657876SArnaldo Carvalho de Melo 	[DCCP_OPEN]		= "OPEN",
1557c657876SArnaldo Carvalho de Melo 	[DCCP_REQUESTING]	= "REQUESTING",
1567c657876SArnaldo Carvalho de Melo 	[DCCP_PARTOPEN]		= "PARTOPEN",
1577c657876SArnaldo Carvalho de Melo 	[DCCP_LISTEN]		= "LISTEN",
1587c657876SArnaldo Carvalho de Melo 	[DCCP_RESPOND]		= "RESPOND",
1597c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSING]		= "CLOSING",
160f11135a3SGerrit Renker 	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
161f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
162f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
1637c657876SArnaldo Carvalho de Melo 	[DCCP_TIME_WAIT]	= "TIME_WAIT",
1647c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSED]		= "CLOSED",
1657c657876SArnaldo Carvalho de Melo 	};
1667c657876SArnaldo Carvalho de Melo 
1677c657876SArnaldo Carvalho de Melo 	if (state >= DCCP_MAX_STATES)
1687c657876SArnaldo Carvalho de Melo 		return "INVALID STATE!";
1697c657876SArnaldo Carvalho de Melo 	else
1707c657876SArnaldo Carvalho de Melo 		return dccp_state_names[state];
1717c657876SArnaldo Carvalho de Melo }
1727c657876SArnaldo Carvalho de Melo 
1737c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name);
1747c657876SArnaldo Carvalho de Melo 
17572478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
1763e0fadc5SArnaldo Carvalho de Melo {
1773e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
178410e27a4SGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
1793e0fadc5SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1803e0fadc5SArnaldo Carvalho de Melo 
181410e27a4SGerrit Renker 	dccp_minisock_init(&dp->dccps_minisock);
182410e27a4SGerrit Renker 
183e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
184e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
185e18d7a98SArnaldo Carvalho de Melo 	sk->sk_state		= DCCP_CLOSED;
186e18d7a98SArnaldo Carvalho de Melo 	sk->sk_write_space	= dccp_write_space;
187e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss	= dccp_sync_mss;
188410e27a4SGerrit Renker 	dp->dccps_mss_cache	= 536;
189e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_rate_last	= jiffies;
190e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
191e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
192410e27a4SGerrit Renker 	dp->dccps_l_ack_ratio	= dp->dccps_r_ack_ratio = 1;
193e18d7a98SArnaldo Carvalho de Melo 
194e18d7a98SArnaldo Carvalho de Melo 	dccp_init_xmit_timers(sk);
195e18d7a98SArnaldo Carvalho de Melo 
196ac75773cSGerrit Renker 	INIT_LIST_HEAD(&dp->dccps_featneg);
197410e27a4SGerrit Renker 	/*
198410e27a4SGerrit Renker 	 * FIXME: We're hardcoding the CCID, and doing this at this point makes
199410e27a4SGerrit Renker 	 * the listening (master) sock get CCID control blocks, which is not
200410e27a4SGerrit Renker 	 * necessary, but for now, to not mess with the test userspace apps,
201410e27a4SGerrit Renker 	 * lets leave it here, later the real solution is to do this in a
202410e27a4SGerrit Renker 	 * setsockopt(CCIDs-I-want/accept). -acme
203410e27a4SGerrit Renker 	 */
204410e27a4SGerrit Renker 	if (likely(ctl_sock_initialized)) {
205e8ef967aSGerrit Renker 		int rc = dccp_feat_init(sk);
206410e27a4SGerrit Renker 
207410e27a4SGerrit Renker 		if (rc)
208410e27a4SGerrit Renker 			return rc;
209410e27a4SGerrit Renker 
210410e27a4SGerrit Renker 		if (dmsk->dccpms_send_ack_vector) {
211410e27a4SGerrit Renker 			dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
212410e27a4SGerrit Renker 			if (dp->dccps_hc_rx_ackvec == NULL)
213410e27a4SGerrit Renker 				return -ENOMEM;
214410e27a4SGerrit Renker 		}
215410e27a4SGerrit Renker 		dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
216410e27a4SGerrit Renker 						      sk, GFP_KERNEL);
217410e27a4SGerrit Renker 		dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
218410e27a4SGerrit Renker 						      sk, GFP_KERNEL);
219410e27a4SGerrit Renker 		if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
220410e27a4SGerrit Renker 			     dp->dccps_hc_tx_ccid == NULL)) {
221410e27a4SGerrit Renker 			ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
222410e27a4SGerrit Renker 			ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
223410e27a4SGerrit Renker 			if (dmsk->dccpms_send_ack_vector) {
224410e27a4SGerrit Renker 				dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
225410e27a4SGerrit Renker 				dp->dccps_hc_rx_ackvec = NULL;
226410e27a4SGerrit Renker 			}
227410e27a4SGerrit Renker 			dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
228410e27a4SGerrit Renker 			return -ENOMEM;
229410e27a4SGerrit Renker 		}
230410e27a4SGerrit Renker 	} else {
2313e0fadc5SArnaldo Carvalho de Melo 		/* control socket doesn't need feat nego */
232410e27a4SGerrit Renker 		INIT_LIST_HEAD(&dmsk->dccpms_pending);
233410e27a4SGerrit Renker 		INIT_LIST_HEAD(&dmsk->dccpms_conf);
234410e27a4SGerrit Renker 	}
235410e27a4SGerrit Renker 
2363e0fadc5SArnaldo Carvalho de Melo 	return 0;
2373e0fadc5SArnaldo Carvalho de Melo }
2383e0fadc5SArnaldo Carvalho de Melo 
2393e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock);
2403e0fadc5SArnaldo Carvalho de Melo 
2417d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk)
2423e0fadc5SArnaldo Carvalho de Melo {
2433e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
244410e27a4SGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
2453e0fadc5SArnaldo Carvalho de Melo 
2463e0fadc5SArnaldo Carvalho de Melo 	/*
2473e0fadc5SArnaldo Carvalho de Melo 	 * DCCP doesn't use sk_write_queue, just sk_send_head
2483e0fadc5SArnaldo Carvalho de Melo 	 * for retransmissions
2493e0fadc5SArnaldo Carvalho de Melo 	 */
2503e0fadc5SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
2513e0fadc5SArnaldo Carvalho de Melo 		kfree_skb(sk->sk_send_head);
2523e0fadc5SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
2533e0fadc5SArnaldo Carvalho de Melo 	}
2543e0fadc5SArnaldo Carvalho de Melo 
2553e0fadc5SArnaldo Carvalho de Melo 	/* Clean up a referenced DCCP bind bucket. */
2563e0fadc5SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_bind_hash != NULL)
257ab1e0a13SArnaldo Carvalho de Melo 		inet_put_port(sk);
2583e0fadc5SArnaldo Carvalho de Melo 
2593e0fadc5SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
2603e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_service_list = NULL;
2613e0fadc5SArnaldo Carvalho de Melo 
262410e27a4SGerrit Renker 	if (dmsk->dccpms_send_ack_vector) {
2633e0fadc5SArnaldo Carvalho de Melo 		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
2643e0fadc5SArnaldo Carvalho de Melo 		dp->dccps_hc_rx_ackvec = NULL;
2653e0fadc5SArnaldo Carvalho de Melo 	}
2663e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
2673e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
2683e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
2693e0fadc5SArnaldo Carvalho de Melo 
2703e0fadc5SArnaldo Carvalho de Melo 	/* clean up feature negotiation state */
271d99a7bd2SGerrit Renker 	dccp_feat_list_purge(&dp->dccps_featneg);
2723e0fadc5SArnaldo Carvalho de Melo }
2733e0fadc5SArnaldo Carvalho de Melo 
2743e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock);
2753e0fadc5SArnaldo Carvalho de Melo 
27672a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog)
2777c657876SArnaldo Carvalho de Melo {
27867e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
27967e6b629SArnaldo Carvalho de Melo 
28067e6b629SArnaldo Carvalho de Melo 	dp->dccps_role = DCCP_ROLE_LISTEN;
2819eca0a47SGerrit Renker 	/* do not start to listen if feature negotiation setup fails */
2829eca0a47SGerrit Renker 	if (dccp_feat_finalise_settings(dp))
2839eca0a47SGerrit Renker 		return -EPROTO;
28472a3effaSEric Dumazet 	return inet_csk_listen_start(sk, backlog);
2857c657876SArnaldo Carvalho de Melo }
2867c657876SArnaldo Carvalho de Melo 
287ce865a61SGerrit Renker static inline int dccp_need_reset(int state)
288ce865a61SGerrit Renker {
289ce865a61SGerrit Renker 	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
290ce865a61SGerrit Renker 	       state != DCCP_REQUESTING;
291ce865a61SGerrit Renker }
292ce865a61SGerrit Renker 
2937c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags)
2947c657876SArnaldo Carvalho de Melo {
2957c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2967c657876SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
2977c657876SArnaldo Carvalho de Melo 	int err = 0;
2987c657876SArnaldo Carvalho de Melo 	const int old_state = sk->sk_state;
2997c657876SArnaldo Carvalho de Melo 
3007c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_CLOSED)
3017c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
3027c657876SArnaldo Carvalho de Melo 
303ce865a61SGerrit Renker 	/*
304ce865a61SGerrit Renker 	 * This corresponds to the ABORT function of RFC793, sec. 3.8
305ce865a61SGerrit Renker 	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
306ce865a61SGerrit Renker 	 */
3077c657876SArnaldo Carvalho de Melo 	if (old_state == DCCP_LISTEN) {
3087c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
309ce865a61SGerrit Renker 	} else if (dccp_need_reset(old_state)) {
310ce865a61SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
311ce865a61SGerrit Renker 		sk->sk_err = ECONNRESET;
3127c657876SArnaldo Carvalho de Melo 	} else if (old_state == DCCP_REQUESTING)
3137c657876SArnaldo Carvalho de Melo 		sk->sk_err = ECONNRESET;
3147c657876SArnaldo Carvalho de Melo 
3157c657876SArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
31648816322SGerrit Renker 
3177c657876SArnaldo Carvalho de Melo 	__skb_queue_purge(&sk->sk_receive_queue);
31848816322SGerrit Renker 	__skb_queue_purge(&sk->sk_write_queue);
3197c657876SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
3207c657876SArnaldo Carvalho de Melo 		__kfree_skb(sk->sk_send_head);
3217c657876SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
3227c657876SArnaldo Carvalho de Melo 	}
3237c657876SArnaldo Carvalho de Melo 
3247c657876SArnaldo Carvalho de Melo 	inet->dport = 0;
3257c657876SArnaldo Carvalho de Melo 
3267c657876SArnaldo Carvalho de Melo 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
3277c657876SArnaldo Carvalho de Melo 		inet_reset_saddr(sk);
3287c657876SArnaldo Carvalho de Melo 
3297c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = 0;
3307c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
3317c657876SArnaldo Carvalho de Melo 
3327c657876SArnaldo Carvalho de Melo 	icsk->icsk_backoff = 0;
3337c657876SArnaldo Carvalho de Melo 	inet_csk_delack_init(sk);
3347c657876SArnaldo Carvalho de Melo 	__sk_dst_reset(sk);
3357c657876SArnaldo Carvalho de Melo 
336547b792cSIlpo Järvinen 	WARN_ON(inet->num && !icsk->icsk_bind_hash);
3377c657876SArnaldo Carvalho de Melo 
3387c657876SArnaldo Carvalho de Melo 	sk->sk_error_report(sk);
3397c657876SArnaldo Carvalho de Melo 	return err;
3407c657876SArnaldo Carvalho de Melo }
3417c657876SArnaldo Carvalho de Melo 
342f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect);
343f21e68caSArnaldo Carvalho de Melo 
344331968bdSArnaldo Carvalho de Melo /*
345331968bdSArnaldo Carvalho de Melo  *	Wait for a DCCP event.
346331968bdSArnaldo Carvalho de Melo  *
347331968bdSArnaldo Carvalho de Melo  *	Note that we don't need to lock the socket, as the upper poll layers
348331968bdSArnaldo Carvalho de Melo  *	take care of normal races (between the test and the event) and we don't
349331968bdSArnaldo Carvalho de Melo  *	go look at any of the socket buffers directly.
350331968bdSArnaldo Carvalho de Melo  */
351f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock,
352331968bdSArnaldo Carvalho de Melo 		       poll_table *wait)
353331968bdSArnaldo Carvalho de Melo {
354331968bdSArnaldo Carvalho de Melo 	unsigned int mask;
355331968bdSArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
356331968bdSArnaldo Carvalho de Melo 
357331968bdSArnaldo Carvalho de Melo 	poll_wait(file, sk->sk_sleep, wait);
358331968bdSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
359331968bdSArnaldo Carvalho de Melo 		return inet_csk_listen_poll(sk);
360331968bdSArnaldo Carvalho de Melo 
361331968bdSArnaldo Carvalho de Melo 	/* Socket is not locked. We are protected from async events
362331968bdSArnaldo Carvalho de Melo 	   by poll logic and correct handling of state changes
363331968bdSArnaldo Carvalho de Melo 	   made by another threads is impossible in any case.
364331968bdSArnaldo Carvalho de Melo 	 */
365331968bdSArnaldo Carvalho de Melo 
366331968bdSArnaldo Carvalho de Melo 	mask = 0;
367331968bdSArnaldo Carvalho de Melo 	if (sk->sk_err)
368331968bdSArnaldo Carvalho de Melo 		mask = POLLERR;
369331968bdSArnaldo Carvalho de Melo 
370331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
371331968bdSArnaldo Carvalho de Melo 		mask |= POLLHUP;
372331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown & RCV_SHUTDOWN)
373f348d70aSDavide Libenzi 		mask |= POLLIN | POLLRDNORM | POLLRDHUP;
374331968bdSArnaldo Carvalho de Melo 
375331968bdSArnaldo Carvalho de Melo 	/* Connected? */
376331968bdSArnaldo Carvalho de Melo 	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
377331968bdSArnaldo Carvalho de Melo 		if (atomic_read(&sk->sk_rmem_alloc) > 0)
378331968bdSArnaldo Carvalho de Melo 			mask |= POLLIN | POLLRDNORM;
379331968bdSArnaldo Carvalho de Melo 
380331968bdSArnaldo Carvalho de Melo 		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
381331968bdSArnaldo Carvalho de Melo 			if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
382331968bdSArnaldo Carvalho de Melo 				mask |= POLLOUT | POLLWRNORM;
383331968bdSArnaldo Carvalho de Melo 			} else {  /* send SIGIO later */
384331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_ASYNC_NOSPACE,
385331968bdSArnaldo Carvalho de Melo 					&sk->sk_socket->flags);
386331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
387331968bdSArnaldo Carvalho de Melo 
388331968bdSArnaldo Carvalho de Melo 				/* Race breaker. If space is freed after
389331968bdSArnaldo Carvalho de Melo 				 * wspace test but before the flags are set,
390331968bdSArnaldo Carvalho de Melo 				 * IO signal will be lost.
391331968bdSArnaldo Carvalho de Melo 				 */
392331968bdSArnaldo Carvalho de Melo 				if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
393331968bdSArnaldo Carvalho de Melo 					mask |= POLLOUT | POLLWRNORM;
394331968bdSArnaldo Carvalho de Melo 			}
395331968bdSArnaldo Carvalho de Melo 		}
396331968bdSArnaldo Carvalho de Melo 	}
397331968bdSArnaldo Carvalho de Melo 	return mask;
398331968bdSArnaldo Carvalho de Melo }
399331968bdSArnaldo Carvalho de Melo 
400f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll);
401f21e68caSArnaldo Carvalho de Melo 
4027c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4037c657876SArnaldo Carvalho de Melo {
4046273172eSArnaldo Carvalho de Melo 	int rc = -ENOTCONN;
4056273172eSArnaldo Carvalho de Melo 
4066273172eSArnaldo Carvalho de Melo 	lock_sock(sk);
4076273172eSArnaldo Carvalho de Melo 
4086273172eSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
4096273172eSArnaldo Carvalho de Melo 		goto out;
4106273172eSArnaldo Carvalho de Melo 
4116273172eSArnaldo Carvalho de Melo 	switch (cmd) {
4126273172eSArnaldo Carvalho de Melo 	case SIOCINQ: {
4136273172eSArnaldo Carvalho de Melo 		struct sk_buff *skb;
4146273172eSArnaldo Carvalho de Melo 		unsigned long amount = 0;
4156273172eSArnaldo Carvalho de Melo 
4166273172eSArnaldo Carvalho de Melo 		skb = skb_peek(&sk->sk_receive_queue);
4176273172eSArnaldo Carvalho de Melo 		if (skb != NULL) {
4186273172eSArnaldo Carvalho de Melo 			/*
4196273172eSArnaldo Carvalho de Melo 			 * We will only return the amount of this packet since
4206273172eSArnaldo Carvalho de Melo 			 * that is all that will be read.
4216273172eSArnaldo Carvalho de Melo 			 */
4226273172eSArnaldo Carvalho de Melo 			amount = skb->len;
4236273172eSArnaldo Carvalho de Melo 		}
4246273172eSArnaldo Carvalho de Melo 		rc = put_user(amount, (int __user *)arg);
4256273172eSArnaldo Carvalho de Melo 	}
4266273172eSArnaldo Carvalho de Melo 		break;
4276273172eSArnaldo Carvalho de Melo 	default:
4286273172eSArnaldo Carvalho de Melo 		rc = -ENOIOCTLCMD;
4296273172eSArnaldo Carvalho de Melo 		break;
4306273172eSArnaldo Carvalho de Melo 	}
4316273172eSArnaldo Carvalho de Melo out:
4326273172eSArnaldo Carvalho de Melo 	release_sock(sk);
4336273172eSArnaldo Carvalho de Melo 	return rc;
4347c657876SArnaldo Carvalho de Melo }
4357c657876SArnaldo Carvalho de Melo 
436f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl);
437f21e68caSArnaldo Carvalho de Melo 
43860fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
43967e6b629SArnaldo Carvalho de Melo 				   char __user *optval, int optlen)
44067e6b629SArnaldo Carvalho de Melo {
44167e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
44267e6b629SArnaldo Carvalho de Melo 	struct dccp_service_list *sl = NULL;
44367e6b629SArnaldo Carvalho de Melo 
44467e6b629SArnaldo Carvalho de Melo 	if (service == DCCP_SERVICE_INVALID_VALUE ||
44567e6b629SArnaldo Carvalho de Melo 	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
44667e6b629SArnaldo Carvalho de Melo 		return -EINVAL;
44767e6b629SArnaldo Carvalho de Melo 
44867e6b629SArnaldo Carvalho de Melo 	if (optlen > sizeof(service)) {
44967e6b629SArnaldo Carvalho de Melo 		sl = kmalloc(optlen, GFP_KERNEL);
45067e6b629SArnaldo Carvalho de Melo 		if (sl == NULL)
45167e6b629SArnaldo Carvalho de Melo 			return -ENOMEM;
45267e6b629SArnaldo Carvalho de Melo 
45367e6b629SArnaldo Carvalho de Melo 		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
45467e6b629SArnaldo Carvalho de Melo 		if (copy_from_user(sl->dccpsl_list,
45567e6b629SArnaldo Carvalho de Melo 				   optval + sizeof(service),
45667e6b629SArnaldo Carvalho de Melo 				   optlen - sizeof(service)) ||
45767e6b629SArnaldo Carvalho de Melo 		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
45867e6b629SArnaldo Carvalho de Melo 			kfree(sl);
45967e6b629SArnaldo Carvalho de Melo 			return -EFAULT;
46067e6b629SArnaldo Carvalho de Melo 		}
46167e6b629SArnaldo Carvalho de Melo 	}
46267e6b629SArnaldo Carvalho de Melo 
46367e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
46467e6b629SArnaldo Carvalho de Melo 	dp->dccps_service = service;
46567e6b629SArnaldo Carvalho de Melo 
46667e6b629SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
46767e6b629SArnaldo Carvalho de Melo 
46867e6b629SArnaldo Carvalho de Melo 	dp->dccps_service_list = sl;
46967e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
47067e6b629SArnaldo Carvalho de Melo 	return 0;
47167e6b629SArnaldo Carvalho de Melo }
47267e6b629SArnaldo Carvalho de Melo 
473410e27a4SGerrit Renker /* byte 1 is feature.  the rest is the preference list */
474410e27a4SGerrit Renker static int dccp_setsockopt_change(struct sock *sk, int type,
475410e27a4SGerrit Renker 				  struct dccp_so_feat __user *optval)
47620f41eeeSGerrit Renker {
477410e27a4SGerrit Renker 	struct dccp_so_feat opt;
478fade756fSGerrit Renker 	u8 *val;
479410e27a4SGerrit Renker 	int rc;
480fade756fSGerrit Renker 
481410e27a4SGerrit Renker 	if (copy_from_user(&opt, optval, sizeof(opt)))
482410e27a4SGerrit Renker 		return -EFAULT;
483410e27a4SGerrit Renker 	/*
484410e27a4SGerrit Renker 	 * rfc4340: 6.1. Change Options
485410e27a4SGerrit Renker 	 */
486410e27a4SGerrit Renker 	if (opt.dccpsf_len < 1)
487fade756fSGerrit Renker 		return -EINVAL;
488fade756fSGerrit Renker 
489410e27a4SGerrit Renker 	val = kmalloc(opt.dccpsf_len, GFP_KERNEL);
490410e27a4SGerrit Renker 	if (!val)
491fade756fSGerrit Renker 		return -ENOMEM;
492fade756fSGerrit Renker 
493410e27a4SGerrit Renker 	if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) {
494410e27a4SGerrit Renker 		rc = -EFAULT;
495410e27a4SGerrit Renker 		goto out_free_val;
496fade756fSGerrit Renker 	}
497fade756fSGerrit Renker 
498410e27a4SGerrit Renker 	rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat,
499410e27a4SGerrit Renker 			      val, opt.dccpsf_len, GFP_KERNEL);
500410e27a4SGerrit Renker 	if (rc)
501410e27a4SGerrit Renker 		goto out_free_val;
502fade756fSGerrit Renker 
503410e27a4SGerrit Renker out:
504fade756fSGerrit Renker 	return rc;
505410e27a4SGerrit Renker 
506410e27a4SGerrit Renker out_free_val:
507410e27a4SGerrit Renker 	kfree(val);
508410e27a4SGerrit Renker 	goto out;
509fade756fSGerrit Renker }
510fade756fSGerrit Renker 
5113fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
512a1d3a355SArnaldo Carvalho de Melo 		char __user *optval, int optlen)
5137c657876SArnaldo Carvalho de Melo {
51409dbc389SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
51509dbc389SGerrit Renker 	int val, err = 0;
5167c657876SArnaldo Carvalho de Melo 
517410e27a4SGerrit Renker 	if (optlen < sizeof(int))
518a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
519a84ffe43SArnaldo Carvalho de Melo 
520a84ffe43SArnaldo Carvalho de Melo 	if (get_user(val, (int __user *)optval))
521a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
522a84ffe43SArnaldo Carvalho de Melo 
52367e6b629SArnaldo Carvalho de Melo 	if (optname == DCCP_SOCKOPT_SERVICE)
52467e6b629SArnaldo Carvalho de Melo 		return dccp_setsockopt_service(sk, val, optval, optlen);
525a84ffe43SArnaldo Carvalho de Melo 
52667e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
527a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
528410e27a4SGerrit Renker 	case DCCP_SOCKOPT_PACKET_SIZE:
529410e27a4SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
530410e27a4SGerrit Renker 		err = 0;
531410e27a4SGerrit Renker 		break;
532410e27a4SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_L:
533410e27a4SGerrit Renker 		if (optlen != sizeof(struct dccp_so_feat))
534410e27a4SGerrit Renker 			err = -EINVAL;
535410e27a4SGerrit Renker 		else
536410e27a4SGerrit Renker 			err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L,
537410e27a4SGerrit Renker 						     (struct dccp_so_feat __user *)
538410e27a4SGerrit Renker 						     optval);
539410e27a4SGerrit Renker 		break;
540410e27a4SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_R:
541410e27a4SGerrit Renker 		if (optlen != sizeof(struct dccp_so_feat))
542410e27a4SGerrit Renker 			err = -EINVAL;
543410e27a4SGerrit Renker 		else
544410e27a4SGerrit Renker 			err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R,
545410e27a4SGerrit Renker 						     (struct dccp_so_feat __user *)
546410e27a4SGerrit Renker 						     optval);
547410e27a4SGerrit Renker 		break;
548b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
549b8599d20SGerrit Renker 		if (dp->dccps_role != DCCP_ROLE_SERVER)
550b8599d20SGerrit Renker 			err = -EOPNOTSUPP;
551b8599d20SGerrit Renker 		else
552b8599d20SGerrit Renker 			dp->dccps_server_timewait = (val != 0);
553b8599d20SGerrit Renker 		break;
554410e27a4SGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:	/* sender side, RFC 4340, sec. 9.2 */
555410e27a4SGerrit Renker 		if (val < 0 || val > 15)
556d6da3511STomasz Grobelny 			err = -EINVAL;
557d6da3511STomasz Grobelny 		else
558410e27a4SGerrit Renker 			dp->dccps_pcslen = val;
559d6da3511STomasz Grobelny 		break;
560410e27a4SGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:	/* receiver side, RFC 4340 sec. 9.2.1 */
561410e27a4SGerrit Renker 		if (val < 0 || val > 15)
562d6da3511STomasz Grobelny 			err = -EINVAL;
563410e27a4SGerrit Renker 		else {
564410e27a4SGerrit Renker 			dp->dccps_pcrlen = val;
565410e27a4SGerrit Renker 			/* FIXME: add feature negotiation,
566410e27a4SGerrit Renker 			 * ChangeL(MinimumChecksumCoverage, val) */
567410e27a4SGerrit Renker 		}
568d6da3511STomasz Grobelny 		break;
569a84ffe43SArnaldo Carvalho de Melo 	default:
570a84ffe43SArnaldo Carvalho de Melo 		err = -ENOPROTOOPT;
571a84ffe43SArnaldo Carvalho de Melo 		break;
572a84ffe43SArnaldo Carvalho de Melo 	}
57373bbe095SGerrit Renker 
574410e27a4SGerrit Renker 	release_sock(sk);
575a84ffe43SArnaldo Carvalho de Melo 	return err;
5767c657876SArnaldo Carvalho de Melo }
5777c657876SArnaldo Carvalho de Melo 
5783fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname,
5793fdadf7dSDmitry Mishin 		    char __user *optval, int optlen)
5803fdadf7dSDmitry Mishin {
5813fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
5823fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
5833fdadf7dSDmitry Mishin 							     optname, optval,
5843fdadf7dSDmitry Mishin 							     optlen);
5853fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5863fdadf7dSDmitry Mishin }
587543d9cfeSArnaldo Carvalho de Melo 
588f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt);
589f21e68caSArnaldo Carvalho de Melo 
5903fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5913fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
5923fdadf7dSDmitry Mishin 			   char __user *optval, int optlen)
5933fdadf7dSDmitry Mishin {
594dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
595dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_setsockopt(sk, level, optname,
596dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
5973fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5983fdadf7dSDmitry Mishin }
599543d9cfeSArnaldo Carvalho de Melo 
6003fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
6013fdadf7dSDmitry Mishin #endif
6023fdadf7dSDmitry Mishin 
60367e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len,
60460fe62e7SAndrea Bittau 				   __be32 __user *optval,
60567e6b629SArnaldo Carvalho de Melo 				   int __user *optlen)
60667e6b629SArnaldo Carvalho de Melo {
60767e6b629SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
60867e6b629SArnaldo Carvalho de Melo 	const struct dccp_service_list *sl;
60967e6b629SArnaldo Carvalho de Melo 	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
61067e6b629SArnaldo Carvalho de Melo 
61167e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
61267e6b629SArnaldo Carvalho de Melo 	if ((sl = dp->dccps_service_list) != NULL) {
61367e6b629SArnaldo Carvalho de Melo 		slen = sl->dccpsl_nr * sizeof(u32);
61467e6b629SArnaldo Carvalho de Melo 		total_len += slen;
61567e6b629SArnaldo Carvalho de Melo 	}
61667e6b629SArnaldo Carvalho de Melo 
61767e6b629SArnaldo Carvalho de Melo 	err = -EINVAL;
61867e6b629SArnaldo Carvalho de Melo 	if (total_len > len)
61967e6b629SArnaldo Carvalho de Melo 		goto out;
62067e6b629SArnaldo Carvalho de Melo 
62167e6b629SArnaldo Carvalho de Melo 	err = 0;
62267e6b629SArnaldo Carvalho de Melo 	if (put_user(total_len, optlen) ||
62367e6b629SArnaldo Carvalho de Melo 	    put_user(dp->dccps_service, optval) ||
62467e6b629SArnaldo Carvalho de Melo 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
62567e6b629SArnaldo Carvalho de Melo 		err = -EFAULT;
62667e6b629SArnaldo Carvalho de Melo out:
62767e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
62867e6b629SArnaldo Carvalho de Melo 	return err;
62967e6b629SArnaldo Carvalho de Melo }
63067e6b629SArnaldo Carvalho de Melo 
6313fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
632a1d3a355SArnaldo Carvalho de Melo 		    char __user *optval, int __user *optlen)
6337c657876SArnaldo Carvalho de Melo {
634a84ffe43SArnaldo Carvalho de Melo 	struct dccp_sock *dp;
635a84ffe43SArnaldo Carvalho de Melo 	int val, len;
6367c657876SArnaldo Carvalho de Melo 
637a84ffe43SArnaldo Carvalho de Melo 	if (get_user(len, optlen))
638a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
639a84ffe43SArnaldo Carvalho de Melo 
64039ebc027SArnaldo Carvalho de Melo 	if (len < (int)sizeof(int))
641a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
642a84ffe43SArnaldo Carvalho de Melo 
643a84ffe43SArnaldo Carvalho de Melo 	dp = dccp_sk(sk);
644a84ffe43SArnaldo Carvalho de Melo 
645a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
646a84ffe43SArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_PACKET_SIZE:
6475aed3243SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
648841bac1dSArnaldo Carvalho de Melo 		return 0;
64988f964dbSArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_SERVICE:
65088f964dbSArnaldo Carvalho de Melo 		return dccp_getsockopt_service(sk, len,
65160fe62e7SAndrea Bittau 					       (__be32 __user *)optval, optlen);
6527c559a9eSGerrit Renker 	case DCCP_SOCKOPT_GET_CUR_MPS:
6537c559a9eSGerrit Renker 		val = dp->dccps_mss_cache;
6547c559a9eSGerrit Renker 		break;
655d90ebcbfSGerrit Renker 	case DCCP_SOCKOPT_AVAILABLE_CCIDS:
656d90ebcbfSGerrit Renker 		return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
657b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
658b8599d20SGerrit Renker 		val = dp->dccps_server_timewait;
659b8599d20SGerrit Renker 		break;
6606f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
6616f4e5fffSGerrit Renker 		val = dp->dccps_pcslen;
6626f4e5fffSGerrit Renker 		break;
6636f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
6646f4e5fffSGerrit Renker 		val = dp->dccps_pcrlen;
6656f4e5fffSGerrit Renker 		break;
66688f964dbSArnaldo Carvalho de Melo 	case 128 ... 191:
66788f964dbSArnaldo Carvalho de Melo 		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
66888f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
66988f964dbSArnaldo Carvalho de Melo 	case 192 ... 255:
67088f964dbSArnaldo Carvalho de Melo 		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
67188f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
672a84ffe43SArnaldo Carvalho de Melo 	default:
673a84ffe43SArnaldo Carvalho de Melo 		return -ENOPROTOOPT;
674a84ffe43SArnaldo Carvalho de Melo 	}
675a84ffe43SArnaldo Carvalho de Melo 
67679133506SGerrit Renker 	len = sizeof(val);
677a84ffe43SArnaldo Carvalho de Melo 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
678a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
679a84ffe43SArnaldo Carvalho de Melo 
680a84ffe43SArnaldo Carvalho de Melo 	return 0;
6817c657876SArnaldo Carvalho de Melo }
6827c657876SArnaldo Carvalho de Melo 
6833fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname,
6843fdadf7dSDmitry Mishin 		    char __user *optval, int __user *optlen)
6853fdadf7dSDmitry Mishin {
6863fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
6873fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
6883fdadf7dSDmitry Mishin 							     optname, optval,
6893fdadf7dSDmitry Mishin 							     optlen);
6903fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
6913fdadf7dSDmitry Mishin }
692543d9cfeSArnaldo Carvalho de Melo 
693f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt);
694f21e68caSArnaldo Carvalho de Melo 
6953fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
6963fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
6973fdadf7dSDmitry Mishin 			   char __user *optval, int __user *optlen)
6983fdadf7dSDmitry Mishin {
699dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
700dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_getsockopt(sk, level, optname,
701dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
7023fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
7033fdadf7dSDmitry Mishin }
704543d9cfeSArnaldo Carvalho de Melo 
7053fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
7063fdadf7dSDmitry Mishin #endif
7073fdadf7dSDmitry Mishin 
7087c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7097c657876SArnaldo Carvalho de Melo 		 size_t len)
7107c657876SArnaldo Carvalho de Melo {
7117c657876SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
7127c657876SArnaldo Carvalho de Melo 	const int flags = msg->msg_flags;
7137c657876SArnaldo Carvalho de Melo 	const int noblock = flags & MSG_DONTWAIT;
7147c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
7157c657876SArnaldo Carvalho de Melo 	int rc, size;
7167c657876SArnaldo Carvalho de Melo 	long timeo;
7177c657876SArnaldo Carvalho de Melo 
7187c657876SArnaldo Carvalho de Melo 	if (len > dp->dccps_mss_cache)
7197c657876SArnaldo Carvalho de Melo 		return -EMSGSIZE;
7207c657876SArnaldo Carvalho de Melo 
7217c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
722b1308dc0SIan McDonald 
723410e27a4SGerrit Renker 	if (sysctl_dccp_tx_qlen &&
724410e27a4SGerrit Renker 	    (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
725b1308dc0SIan McDonald 		rc = -EAGAIN;
726b1308dc0SIan McDonald 		goto out_release;
727b1308dc0SIan McDonald 	}
728b1308dc0SIan McDonald 
72927258ee5SArnaldo Carvalho de Melo 	timeo = sock_sndtimeo(sk, noblock);
7307c657876SArnaldo Carvalho de Melo 
7317c657876SArnaldo Carvalho de Melo 	/*
7327c657876SArnaldo Carvalho de Melo 	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
7337c657876SArnaldo Carvalho de Melo 	 * so that the trick in dccp_rcv_request_sent_state_process.
7347c657876SArnaldo Carvalho de Melo 	 */
7357c657876SArnaldo Carvalho de Melo 	/* Wait for a connection to finish. */
736cecd8d0eSGerrit Renker 	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7377c657876SArnaldo Carvalho de Melo 		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
73827258ee5SArnaldo Carvalho de Melo 			goto out_release;
7397c657876SArnaldo Carvalho de Melo 
7407c657876SArnaldo Carvalho de Melo 	size = sk->sk_prot->max_header + len;
7417c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7427c657876SArnaldo Carvalho de Melo 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
7437c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7447c657876SArnaldo Carvalho de Melo 	if (skb == NULL)
7457c657876SArnaldo Carvalho de Melo 		goto out_release;
7467c657876SArnaldo Carvalho de Melo 
7477c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
7487c657876SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
74927258ee5SArnaldo Carvalho de Melo 	if (rc != 0)
75027258ee5SArnaldo Carvalho de Melo 		goto out_discard;
7517c657876SArnaldo Carvalho de Melo 
752410e27a4SGerrit Renker 	skb_queue_tail(&sk->sk_write_queue, skb);
753410e27a4SGerrit Renker 	dccp_write_xmit(sk,0);
7547c657876SArnaldo Carvalho de Melo out_release:
7557c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7567c657876SArnaldo Carvalho de Melo 	return rc ? : len;
75727258ee5SArnaldo Carvalho de Melo out_discard:
75827258ee5SArnaldo Carvalho de Melo 	kfree_skb(skb);
7597c657876SArnaldo Carvalho de Melo 	goto out_release;
7607c657876SArnaldo Carvalho de Melo }
7617c657876SArnaldo Carvalho de Melo 
762f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg);
763f21e68caSArnaldo Carvalho de Melo 
7647c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7657c657876SArnaldo Carvalho de Melo 		 size_t len, int nonblock, int flags, int *addr_len)
7667c657876SArnaldo Carvalho de Melo {
7677c657876SArnaldo Carvalho de Melo 	const struct dccp_hdr *dh;
7687c657876SArnaldo Carvalho de Melo 	long timeo;
7697c657876SArnaldo Carvalho de Melo 
7707c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7717c657876SArnaldo Carvalho de Melo 
772531669a0SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
773531669a0SArnaldo Carvalho de Melo 		len = -ENOTCONN;
7747c657876SArnaldo Carvalho de Melo 		goto out;
775531669a0SArnaldo Carvalho de Melo 	}
7767c657876SArnaldo Carvalho de Melo 
7777c657876SArnaldo Carvalho de Melo 	timeo = sock_rcvtimeo(sk, nonblock);
7787c657876SArnaldo Carvalho de Melo 
7797c657876SArnaldo Carvalho de Melo 	do {
780531669a0SArnaldo Carvalho de Melo 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
7817c657876SArnaldo Carvalho de Melo 
782531669a0SArnaldo Carvalho de Melo 		if (skb == NULL)
783531669a0SArnaldo Carvalho de Melo 			goto verify_sock_status;
7847c657876SArnaldo Carvalho de Melo 
7857c657876SArnaldo Carvalho de Melo 		dh = dccp_hdr(skb);
7867c657876SArnaldo Carvalho de Melo 
7870c869620SGerrit Renker 		switch (dh->dccph_type) {
7880c869620SGerrit Renker 		case DCCP_PKT_DATA:
7890c869620SGerrit Renker 		case DCCP_PKT_DATAACK:
7907c657876SArnaldo Carvalho de Melo 			goto found_ok_skb;
7917c657876SArnaldo Carvalho de Melo 
7920c869620SGerrit Renker 		case DCCP_PKT_CLOSE:
7930c869620SGerrit Renker 		case DCCP_PKT_CLOSEREQ:
7940c869620SGerrit Renker 			if (!(flags & MSG_PEEK))
7950c869620SGerrit Renker 				dccp_finish_passive_close(sk);
7960c869620SGerrit Renker 			/* fall through */
7970c869620SGerrit Renker 		case DCCP_PKT_RESET:
7980c869620SGerrit Renker 			dccp_pr_debug("found fin (%s) ok!\n",
7990c869620SGerrit Renker 				      dccp_packet_name(dh->dccph_type));
800531669a0SArnaldo Carvalho de Melo 			len = 0;
8017c657876SArnaldo Carvalho de Melo 			goto found_fin_ok;
8020c869620SGerrit Renker 		default:
8037690af3fSArnaldo Carvalho de Melo 			dccp_pr_debug("packet_type=%s\n",
8047690af3fSArnaldo Carvalho de Melo 				      dccp_packet_name(dh->dccph_type));
805624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8060c869620SGerrit Renker 		}
807531669a0SArnaldo Carvalho de Melo verify_sock_status:
808531669a0SArnaldo Carvalho de Melo 		if (sock_flag(sk, SOCK_DONE)) {
809531669a0SArnaldo Carvalho de Melo 			len = 0;
8107c657876SArnaldo Carvalho de Melo 			break;
8117c657876SArnaldo Carvalho de Melo 		}
8127c657876SArnaldo Carvalho de Melo 
813531669a0SArnaldo Carvalho de Melo 		if (sk->sk_err) {
814531669a0SArnaldo Carvalho de Melo 			len = sock_error(sk);
8157c657876SArnaldo Carvalho de Melo 			break;
816531669a0SArnaldo Carvalho de Melo 		}
817531669a0SArnaldo Carvalho de Melo 
818531669a0SArnaldo Carvalho de Melo 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
819531669a0SArnaldo Carvalho de Melo 			len = 0;
820531669a0SArnaldo Carvalho de Melo 			break;
821531669a0SArnaldo Carvalho de Melo 		}
8227c657876SArnaldo Carvalho de Melo 
8237c657876SArnaldo Carvalho de Melo 		if (sk->sk_state == DCCP_CLOSED) {
8247c657876SArnaldo Carvalho de Melo 			if (!sock_flag(sk, SOCK_DONE)) {
8257c657876SArnaldo Carvalho de Melo 				/* This occurs when user tries to read
8267c657876SArnaldo Carvalho de Melo 				 * from never connected socket.
8277c657876SArnaldo Carvalho de Melo 				 */
828531669a0SArnaldo Carvalho de Melo 				len = -ENOTCONN;
8297c657876SArnaldo Carvalho de Melo 				break;
8307c657876SArnaldo Carvalho de Melo 			}
831531669a0SArnaldo Carvalho de Melo 			len = 0;
8327c657876SArnaldo Carvalho de Melo 			break;
8337c657876SArnaldo Carvalho de Melo 		}
8347c657876SArnaldo Carvalho de Melo 
8357c657876SArnaldo Carvalho de Melo 		if (!timeo) {
836531669a0SArnaldo Carvalho de Melo 			len = -EAGAIN;
8377c657876SArnaldo Carvalho de Melo 			break;
8387c657876SArnaldo Carvalho de Melo 		}
8397c657876SArnaldo Carvalho de Melo 
8407c657876SArnaldo Carvalho de Melo 		if (signal_pending(current)) {
841531669a0SArnaldo Carvalho de Melo 			len = sock_intr_errno(timeo);
8427c657876SArnaldo Carvalho de Melo 			break;
8437c657876SArnaldo Carvalho de Melo 		}
8447c657876SArnaldo Carvalho de Melo 
8457c657876SArnaldo Carvalho de Melo 		sk_wait_data(sk, &timeo);
8467c657876SArnaldo Carvalho de Melo 		continue;
8477c657876SArnaldo Carvalho de Melo 	found_ok_skb:
848531669a0SArnaldo Carvalho de Melo 		if (len > skb->len)
849531669a0SArnaldo Carvalho de Melo 			len = skb->len;
850531669a0SArnaldo Carvalho de Melo 		else if (len < skb->len)
851531669a0SArnaldo Carvalho de Melo 			msg->msg_flags |= MSG_TRUNC;
8527c657876SArnaldo Carvalho de Melo 
853531669a0SArnaldo Carvalho de Melo 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
8547c657876SArnaldo Carvalho de Melo 			/* Exception. Bailout! */
855531669a0SArnaldo Carvalho de Melo 			len = -EFAULT;
8567c657876SArnaldo Carvalho de Melo 			break;
8577c657876SArnaldo Carvalho de Melo 		}
8587c657876SArnaldo Carvalho de Melo 	found_fin_ok:
8597c657876SArnaldo Carvalho de Melo 		if (!(flags & MSG_PEEK))
860624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8617c657876SArnaldo Carvalho de Melo 		break;
862531669a0SArnaldo Carvalho de Melo 	} while (1);
8637c657876SArnaldo Carvalho de Melo out:
8647c657876SArnaldo Carvalho de Melo 	release_sock(sk);
865531669a0SArnaldo Carvalho de Melo 	return len;
8667c657876SArnaldo Carvalho de Melo }
8677c657876SArnaldo Carvalho de Melo 
868f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg);
869f21e68caSArnaldo Carvalho de Melo 
870f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog)
8717c657876SArnaldo Carvalho de Melo {
8727c657876SArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
8737c657876SArnaldo Carvalho de Melo 	unsigned char old_state;
8747c657876SArnaldo Carvalho de Melo 	int err;
8757c657876SArnaldo Carvalho de Melo 
8767c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
8777c657876SArnaldo Carvalho de Melo 
8787c657876SArnaldo Carvalho de Melo 	err = -EINVAL;
8797c657876SArnaldo Carvalho de Melo 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
8807c657876SArnaldo Carvalho de Melo 		goto out;
8817c657876SArnaldo Carvalho de Melo 
8827c657876SArnaldo Carvalho de Melo 	old_state = sk->sk_state;
8837c657876SArnaldo Carvalho de Melo 	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
8847c657876SArnaldo Carvalho de Melo 		goto out;
8857c657876SArnaldo Carvalho de Melo 
8867c657876SArnaldo Carvalho de Melo 	/* Really, if the socket is already in listen state
8877c657876SArnaldo Carvalho de Melo 	 * we can only allow the backlog to be adjusted.
8887c657876SArnaldo Carvalho de Melo 	 */
8897c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_LISTEN) {
8907c657876SArnaldo Carvalho de Melo 		/*
8917c657876SArnaldo Carvalho de Melo 		 * FIXME: here it probably should be sk->sk_prot->listen_start
8927c657876SArnaldo Carvalho de Melo 		 * see tcp_listen_start
8937c657876SArnaldo Carvalho de Melo 		 */
89472a3effaSEric Dumazet 		err = dccp_listen_start(sk, backlog);
8957c657876SArnaldo Carvalho de Melo 		if (err)
8967c657876SArnaldo Carvalho de Melo 			goto out;
8977c657876SArnaldo Carvalho de Melo 	}
8987c657876SArnaldo Carvalho de Melo 	sk->sk_max_ack_backlog = backlog;
8997c657876SArnaldo Carvalho de Melo 	err = 0;
9007c657876SArnaldo Carvalho de Melo 
9017c657876SArnaldo Carvalho de Melo out:
9027c657876SArnaldo Carvalho de Melo 	release_sock(sk);
9037c657876SArnaldo Carvalho de Melo 	return err;
9047c657876SArnaldo Carvalho de Melo }
9057c657876SArnaldo Carvalho de Melo 
906f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen);
907f21e68caSArnaldo Carvalho de Melo 
9080c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk)
9097c657876SArnaldo Carvalho de Melo {
9100c869620SGerrit Renker 	u8 next_state = DCCP_CLOSED;
9117c657876SArnaldo Carvalho de Melo 
9120c869620SGerrit Renker 	switch (sk->sk_state) {
9130c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
9140c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
9150c869620SGerrit Renker 		dccp_finish_passive_close(sk);
9160c869620SGerrit Renker 		break;
9170c869620SGerrit Renker 	case DCCP_PARTOPEN:
9180c869620SGerrit Renker 		dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
9190c869620SGerrit Renker 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
9200c869620SGerrit Renker 		/* fall through */
9210c869620SGerrit Renker 	case DCCP_OPEN:
9220c869620SGerrit Renker 		dccp_send_close(sk, 1);
9237c657876SArnaldo Carvalho de Melo 
924b8599d20SGerrit Renker 		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
925b8599d20SGerrit Renker 		    !dccp_sk(sk)->dccps_server_timewait)
9260c869620SGerrit Renker 			next_state = DCCP_ACTIVE_CLOSEREQ;
9270c869620SGerrit Renker 		else
9280c869620SGerrit Renker 			next_state = DCCP_CLOSING;
9290c869620SGerrit Renker 		/* fall through */
9300c869620SGerrit Renker 	default:
9310c869620SGerrit Renker 		dccp_set_state(sk, next_state);
9320c869620SGerrit Renker 	}
9337c657876SArnaldo Carvalho de Melo }
9347c657876SArnaldo Carvalho de Melo 
9357c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout)
9367c657876SArnaldo Carvalho de Melo {
93797e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
9387c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
939d83bd95bSGerrit Renker 	u32 data_was_unread = 0;
940134af346SHerbert Xu 	int state;
9417c657876SArnaldo Carvalho de Melo 
9427c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
9437c657876SArnaldo Carvalho de Melo 
9447c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
9457c657876SArnaldo Carvalho de Melo 
9467c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
9477c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
9487c657876SArnaldo Carvalho de Melo 
9497c657876SArnaldo Carvalho de Melo 		/* Special case. */
9507c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
9517c657876SArnaldo Carvalho de Melo 
9527c657876SArnaldo Carvalho de Melo 		goto adjudge_to_death;
9537c657876SArnaldo Carvalho de Melo 	}
9547c657876SArnaldo Carvalho de Melo 
95597e5848dSIan McDonald 	sk_stop_timer(sk, &dp->dccps_xmit_timer);
95697e5848dSIan McDonald 
9577c657876SArnaldo Carvalho de Melo 	/*
9587c657876SArnaldo Carvalho de Melo 	 * We need to flush the recv. buffs.  We do this only on the
9597c657876SArnaldo Carvalho de Melo 	 * descriptor close, not protocol-sourced closes, because the
9607c657876SArnaldo Carvalho de Melo 	  *reader process may not have drained the data yet!
9617c657876SArnaldo Carvalho de Melo 	 */
9627c657876SArnaldo Carvalho de Melo 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
963d83bd95bSGerrit Renker 		data_was_unread += skb->len;
9647c657876SArnaldo Carvalho de Melo 		__kfree_skb(skb);
9657c657876SArnaldo Carvalho de Melo 	}
9667c657876SArnaldo Carvalho de Melo 
967d83bd95bSGerrit Renker 	if (data_was_unread) {
968d83bd95bSGerrit Renker 		/* Unread data was tossed, send an appropriate Reset Code */
969d83bd95bSGerrit Renker 		DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
970d83bd95bSGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
971d83bd95bSGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
972d83bd95bSGerrit Renker 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
9737c657876SArnaldo Carvalho de Melo 		/* Check zero linger _after_ checking for unread data. */
9747c657876SArnaldo Carvalho de Melo 		sk->sk_prot->disconnect(sk, 0);
9750c869620SGerrit Renker 	} else if (sk->sk_state != DCCP_CLOSED) {
9760c869620SGerrit Renker 		dccp_terminate_connection(sk);
9777c657876SArnaldo Carvalho de Melo 	}
9787c657876SArnaldo Carvalho de Melo 
9797c657876SArnaldo Carvalho de Melo 	sk_stream_wait_close(sk, timeout);
9807c657876SArnaldo Carvalho de Melo 
9817c657876SArnaldo Carvalho de Melo adjudge_to_death:
982134af346SHerbert Xu 	state = sk->sk_state;
983134af346SHerbert Xu 	sock_hold(sk);
984134af346SHerbert Xu 	sock_orphan(sk);
985134af346SHerbert Xu 	atomic_inc(sk->sk_prot->orphan_count);
986134af346SHerbert Xu 
9877ad07e7cSArnaldo Carvalho de Melo 	/*
9887ad07e7cSArnaldo Carvalho de Melo 	 * It is the last release_sock in its life. It will remove backlog.
9897ad07e7cSArnaldo Carvalho de Melo 	 */
9907c657876SArnaldo Carvalho de Melo 	release_sock(sk);
9917c657876SArnaldo Carvalho de Melo 	/*
9927c657876SArnaldo Carvalho de Melo 	 * Now socket is owned by kernel and we acquire BH lock
9937c657876SArnaldo Carvalho de Melo 	 * to finish close. No need to check for user refs.
9947c657876SArnaldo Carvalho de Melo 	 */
9957c657876SArnaldo Carvalho de Melo 	local_bh_disable();
9967c657876SArnaldo Carvalho de Melo 	bh_lock_sock(sk);
997547b792cSIlpo Järvinen 	WARN_ON(sock_owned_by_user(sk));
9987c657876SArnaldo Carvalho de Melo 
999134af346SHerbert Xu 	/* Have we already been destroyed by a softirq or backlog? */
1000134af346SHerbert Xu 	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1001134af346SHerbert Xu 		goto out;
10027c657876SArnaldo Carvalho de Melo 
10037c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_CLOSED)
10047c657876SArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
10057c657876SArnaldo Carvalho de Melo 
10067c657876SArnaldo Carvalho de Melo 	/* Otherwise, socket is reprieved until protocol close. */
10077c657876SArnaldo Carvalho de Melo 
1008134af346SHerbert Xu out:
10097c657876SArnaldo Carvalho de Melo 	bh_unlock_sock(sk);
10107c657876SArnaldo Carvalho de Melo 	local_bh_enable();
10117c657876SArnaldo Carvalho de Melo 	sock_put(sk);
10127c657876SArnaldo Carvalho de Melo }
10137c657876SArnaldo Carvalho de Melo 
1014f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close);
1015f21e68caSArnaldo Carvalho de Melo 
10167c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how)
10177c657876SArnaldo Carvalho de Melo {
10188e8c71f1SGerrit Renker 	dccp_pr_debug("called shutdown(%x)\n", how);
10197c657876SArnaldo Carvalho de Melo }
10207c657876SArnaldo Carvalho de Melo 
1021f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown);
1022f21e68caSArnaldo Carvalho de Melo 
102324e8b7e4SYOSHIFUJI Hideaki static inline int dccp_mib_init(void)
10247c657876SArnaldo Carvalho de Melo {
102524e8b7e4SYOSHIFUJI Hideaki 	return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
10267c657876SArnaldo Carvalho de Melo }
10277c657876SArnaldo Carvalho de Melo 
102824e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void)
102946f09ffaSArnaldo Carvalho de Melo {
103024e8b7e4SYOSHIFUJI Hideaki 	snmp_mib_free((void**)dccp_statistics);
103146f09ffaSArnaldo Carvalho de Melo }
103246f09ffaSArnaldo Carvalho de Melo 
10337c657876SArnaldo Carvalho de Melo static int thash_entries;
10347c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444);
10357c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
10367c657876SArnaldo Carvalho de Melo 
1037a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG
10387c657876SArnaldo Carvalho de Melo int dccp_debug;
103943264991SGerrit Renker module_param(dccp_debug, bool, 0644);
10407c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1041f21e68caSArnaldo Carvalho de Melo 
1042f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug);
1043a1d3a355SArnaldo Carvalho de Melo #endif
10447c657876SArnaldo Carvalho de Melo 
10457c657876SArnaldo Carvalho de Melo static int __init dccp_init(void)
10467c657876SArnaldo Carvalho de Melo {
10477c657876SArnaldo Carvalho de Melo 	unsigned long goal;
10487c657876SArnaldo Carvalho de Melo 	int ehash_order, bhash_order, i;
1049b61fafc4SArnaldo Carvalho de Melo 	int rc = -ENOBUFS;
10507c657876SArnaldo Carvalho de Melo 
1051028b0275SPatrick McHardy 	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1052028b0275SPatrick McHardy 		     FIELD_SIZEOF(struct sk_buff, cb));
1053028b0275SPatrick McHardy 
10547690af3fSArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep =
10557690af3fSArnaldo Carvalho de Melo 		kmem_cache_create("dccp_bind_bucket",
10567690af3fSArnaldo Carvalho de Melo 				  sizeof(struct inet_bind_bucket), 0,
105720c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN, NULL);
10587c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bind_bucket_cachep)
1059b61fafc4SArnaldo Carvalho de Melo 		goto out;
10607c657876SArnaldo Carvalho de Melo 
10617c657876SArnaldo Carvalho de Melo 	/*
10627c657876SArnaldo Carvalho de Melo 	 * Size and allocate the main established and bind bucket
10637c657876SArnaldo Carvalho de Melo 	 * hash tables.
10647c657876SArnaldo Carvalho de Melo 	 *
10657c657876SArnaldo Carvalho de Melo 	 * The methodology is similar to that of the buffer cache.
10667c657876SArnaldo Carvalho de Melo 	 */
10677c657876SArnaldo Carvalho de Melo 	if (num_physpages >= (128 * 1024))
10687c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (21 - PAGE_SHIFT);
10697c657876SArnaldo Carvalho de Melo 	else
10707c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (23 - PAGE_SHIFT);
10717c657876SArnaldo Carvalho de Melo 
10727c657876SArnaldo Carvalho de Melo 	if (thash_entries)
10737690af3fSArnaldo Carvalho de Melo 		goal = (thash_entries *
10747690af3fSArnaldo Carvalho de Melo 			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
10757c657876SArnaldo Carvalho de Melo 	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
10767c657876SArnaldo Carvalho de Melo 		;
10777c657876SArnaldo Carvalho de Melo 	do {
10787c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
10797c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_ehash_bucket);
10807690af3fSArnaldo Carvalho de Melo 		while (dccp_hashinfo.ehash_size &
10817690af3fSArnaldo Carvalho de Melo 		       (dccp_hashinfo.ehash_size - 1))
10827c657876SArnaldo Carvalho de Melo 			dccp_hashinfo.ehash_size--;
10837c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
10847c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, ehash_order);
10857c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
10867c657876SArnaldo Carvalho de Melo 
10877c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.ehash) {
108859348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP established hash table");
10897c657876SArnaldo Carvalho de Melo 		goto out_free_bind_bucket_cachep;
10907c657876SArnaldo Carvalho de Melo 	}
10917c657876SArnaldo Carvalho de Melo 
1092dbca9b27SEric Dumazet 	for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
10933ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
10943ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
10957c657876SArnaldo Carvalho de Melo 	}
10967c657876SArnaldo Carvalho de Melo 
1097230140cfSEric Dumazet 	if (inet_ehash_locks_alloc(&dccp_hashinfo))
1098230140cfSEric Dumazet 			goto out_free_dccp_ehash;
1099230140cfSEric Dumazet 
11007c657876SArnaldo Carvalho de Melo 	bhash_order = ehash_order;
11017c657876SArnaldo Carvalho de Melo 
11027c657876SArnaldo Carvalho de Melo 	do {
11037c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
11047c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_bind_hashbucket);
11057690af3fSArnaldo Carvalho de Melo 		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
11067690af3fSArnaldo Carvalho de Melo 		    bhash_order > 0)
11077c657876SArnaldo Carvalho de Melo 			continue;
11087c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
11097c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, bhash_order);
11107c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
11117c657876SArnaldo Carvalho de Melo 
11127c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bhash) {
111359348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP bind hash table");
1114230140cfSEric Dumazet 		goto out_free_dccp_locks;
11157c657876SArnaldo Carvalho de Melo 	}
11167c657876SArnaldo Carvalho de Melo 
11177c657876SArnaldo Carvalho de Melo 	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
11187c657876SArnaldo Carvalho de Melo 		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
11197c657876SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
11207c657876SArnaldo Carvalho de Melo 	}
11217c657876SArnaldo Carvalho de Melo 
112246f09ffaSArnaldo Carvalho de Melo 	rc = dccp_mib_init();
1123fa23e2ecSArnaldo Carvalho de Melo 	if (rc)
11247c657876SArnaldo Carvalho de Melo 		goto out_free_dccp_bhash;
11257c657876SArnaldo Carvalho de Melo 
11269b07ef5dSArnaldo Carvalho de Melo 	rc = dccp_ackvec_init();
11277c657876SArnaldo Carvalho de Melo 	if (rc)
1128b61fafc4SArnaldo Carvalho de Melo 		goto out_free_dccp_mib;
11299b07ef5dSArnaldo Carvalho de Melo 
1130e55d912fSArnaldo Carvalho de Melo 	rc = dccp_sysctl_init();
11319b07ef5dSArnaldo Carvalho de Melo 	if (rc)
11329b07ef5dSArnaldo Carvalho de Melo 		goto out_ackvec_exit;
11334c70f383SGerrit Renker 
11344c70f383SGerrit Renker 	dccp_timestamping_init();
11357c657876SArnaldo Carvalho de Melo out:
11367c657876SArnaldo Carvalho de Melo 	return rc;
11379b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit:
11389b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1139b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib:
114046f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
11417c657876SArnaldo Carvalho de Melo out_free_dccp_bhash:
11427c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
11437c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bhash = NULL;
1144230140cfSEric Dumazet out_free_dccp_locks:
1145230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11467c657876SArnaldo Carvalho de Melo out_free_dccp_ehash:
11477c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
11487c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.ehash = NULL;
11497c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep:
11507c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11517c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep = NULL;
11527c657876SArnaldo Carvalho de Melo 	goto out;
11537c657876SArnaldo Carvalho de Melo }
11547c657876SArnaldo Carvalho de Melo 
11557c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void)
11567c657876SArnaldo Carvalho de Melo {
115746f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
1158725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash,
1159725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.bhash_size *
1160725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_bind_hashbucket)));
1161725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash,
1162725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.ehash_size *
1163725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_ehash_bucket)));
1164230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11657c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11669b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1167e55d912fSArnaldo Carvalho de Melo 	dccp_sysctl_exit();
11687c657876SArnaldo Carvalho de Melo }
11697c657876SArnaldo Carvalho de Melo 
11707c657876SArnaldo Carvalho de Melo module_init(dccp_init);
11717c657876SArnaldo Carvalho de Melo module_exit(dccp_fini);
11727c657876SArnaldo Carvalho de Melo 
11737c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
11747c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
11757c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1176