xref: /openbmc/linux/net/dccp/proto.c (revision 24e8b7e4)
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 <asm/semaphore.h>
317c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h>
327c657876SArnaldo Carvalho de Melo #include <linux/timer.h>
337c657876SArnaldo Carvalho de Melo #include <linux/delay.h>
347c657876SArnaldo Carvalho de Melo #include <linux/poll.h>
357c657876SArnaldo Carvalho de Melo 
367c657876SArnaldo Carvalho de Melo #include "ccid.h"
377c657876SArnaldo Carvalho de Melo #include "dccp.h"
38afe00251SAndrea Bittau #include "feat.h"
397c657876SArnaldo Carvalho de Melo 
40ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
417c657876SArnaldo Carvalho de Melo 
42f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics);
43f21e68caSArnaldo Carvalho de Melo 
447c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0);
457c657876SArnaldo Carvalho de Melo 
46f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count);
47f21e68caSArnaldo Carvalho de Melo 
48075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
49075ae866SArnaldo Carvalho de Melo 	.lhash_lock	= RW_LOCK_UNLOCKED,
50075ae866SArnaldo Carvalho de Melo 	.lhash_users	= ATOMIC_INIT(0),
51075ae866SArnaldo Carvalho de Melo 	.lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
52075ae866SArnaldo Carvalho de Melo };
53075ae866SArnaldo Carvalho de Melo 
54075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo);
55075ae866SArnaldo Carvalho de Melo 
56b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */
57b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5;
58b1308dc0SIan McDonald 
59c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state)
60c25a18baSArnaldo Carvalho de Melo {
61c25a18baSArnaldo Carvalho de Melo 	const int oldstate = sk->sk_state;
62c25a18baSArnaldo Carvalho de Melo 
63f11135a3SGerrit Renker 	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
64c25a18baSArnaldo Carvalho de Melo 		      dccp_state_name(oldstate), dccp_state_name(state));
65c25a18baSArnaldo Carvalho de Melo 	WARN_ON(state == oldstate);
66c25a18baSArnaldo Carvalho de Melo 
67c25a18baSArnaldo Carvalho de Melo 	switch (state) {
68c25a18baSArnaldo Carvalho de Melo 	case DCCP_OPEN:
69c25a18baSArnaldo Carvalho de Melo 		if (oldstate != DCCP_OPEN)
70c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
71c25a18baSArnaldo Carvalho de Melo 		break;
72c25a18baSArnaldo Carvalho de Melo 
73c25a18baSArnaldo Carvalho de Melo 	case DCCP_CLOSED:
740c869620SGerrit Renker 		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
750c869620SGerrit Renker 		    oldstate == DCCP_CLOSING)
76c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
77c25a18baSArnaldo Carvalho de Melo 
78c25a18baSArnaldo Carvalho de Melo 		sk->sk_prot->unhash(sk);
79c25a18baSArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash != NULL &&
80c25a18baSArnaldo Carvalho de Melo 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
81ab1e0a13SArnaldo Carvalho de Melo 			inet_put_port(sk);
82c25a18baSArnaldo Carvalho de Melo 		/* fall through */
83c25a18baSArnaldo Carvalho de Melo 	default:
84c25a18baSArnaldo Carvalho de Melo 		if (oldstate == DCCP_OPEN)
85c25a18baSArnaldo Carvalho de Melo 			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
86c25a18baSArnaldo Carvalho de Melo 	}
87c25a18baSArnaldo Carvalho de Melo 
88c25a18baSArnaldo Carvalho de Melo 	/* Change state AFTER socket is unhashed to avoid closed
89c25a18baSArnaldo Carvalho de Melo 	 * socket sitting in hash tables.
90c25a18baSArnaldo Carvalho de Melo 	 */
91c25a18baSArnaldo Carvalho de Melo 	sk->sk_state = state;
92c25a18baSArnaldo Carvalho de Melo }
93c25a18baSArnaldo Carvalho de Melo 
94c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state);
95c25a18baSArnaldo Carvalho de Melo 
960c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk)
970c869620SGerrit Renker {
980c869620SGerrit Renker 	switch (sk->sk_state) {
990c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
1000c869620SGerrit Renker 		/* Node (client or server) has received Close packet. */
1010c869620SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
1020c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
1030c869620SGerrit Renker 		break;
1040c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
1050c869620SGerrit Renker 		/*
1060c869620SGerrit Renker 		 * Client received CloseReq. We set the `active' flag so that
1070c869620SGerrit Renker 		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
1080c869620SGerrit Renker 		 */
1090c869620SGerrit Renker 		dccp_send_close(sk, 1);
1100c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSING);
1110c869620SGerrit Renker 	}
1120c869620SGerrit Renker }
1130c869620SGerrit Renker 
114c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk)
115c25a18baSArnaldo Carvalho de Melo {
116c25a18baSArnaldo Carvalho de Melo 	dccp_set_state(sk, DCCP_CLOSED);
117c25a18baSArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
118c25a18baSArnaldo Carvalho de Melo 
119c25a18baSArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
120c25a18baSArnaldo Carvalho de Melo 
121c25a18baSArnaldo Carvalho de Melo 	if (!sock_flag(sk, SOCK_DEAD))
122c25a18baSArnaldo Carvalho de Melo 		sk->sk_state_change(sk);
123c25a18baSArnaldo Carvalho de Melo 	else
124c25a18baSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
125c25a18baSArnaldo Carvalho de Melo }
126c25a18baSArnaldo Carvalho de Melo 
127c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done);
128c25a18baSArnaldo Carvalho de Melo 
1297c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type)
1307c657876SArnaldo Carvalho de Melo {
1317c657876SArnaldo Carvalho de Melo 	static const char *dccp_packet_names[] = {
1327c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_REQUEST]  = "REQUEST",
1337c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESPONSE] = "RESPONSE",
1347c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATA]	    = "DATA",
1357c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_ACK]	    = "ACK",
1367c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATAACK]  = "DATAACK",
1377c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
1387c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSE]    = "CLOSE",
1397c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESET]    = "RESET",
1407c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNC]	    = "SYNC",
1417c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNCACK]  = "SYNCACK",
1427c657876SArnaldo Carvalho de Melo 	};
1437c657876SArnaldo Carvalho de Melo 
1447c657876SArnaldo Carvalho de Melo 	if (type >= DCCP_NR_PKT_TYPES)
1457c657876SArnaldo Carvalho de Melo 		return "INVALID";
1467c657876SArnaldo Carvalho de Melo 	else
1477c657876SArnaldo Carvalho de Melo 		return dccp_packet_names[type];
1487c657876SArnaldo Carvalho de Melo }
1497c657876SArnaldo Carvalho de Melo 
1507c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name);
1517c657876SArnaldo Carvalho de Melo 
1527c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state)
1537c657876SArnaldo Carvalho de Melo {
1547c657876SArnaldo Carvalho de Melo 	static char *dccp_state_names[] = {
1557c657876SArnaldo Carvalho de Melo 	[DCCP_OPEN]		= "OPEN",
1567c657876SArnaldo Carvalho de Melo 	[DCCP_REQUESTING]	= "REQUESTING",
1577c657876SArnaldo Carvalho de Melo 	[DCCP_PARTOPEN]		= "PARTOPEN",
1587c657876SArnaldo Carvalho de Melo 	[DCCP_LISTEN]		= "LISTEN",
1597c657876SArnaldo Carvalho de Melo 	[DCCP_RESPOND]		= "RESPOND",
1607c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSING]		= "CLOSING",
161f11135a3SGerrit Renker 	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
162f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
163f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
1647c657876SArnaldo Carvalho de Melo 	[DCCP_TIME_WAIT]	= "TIME_WAIT",
1657c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSED]		= "CLOSED",
1667c657876SArnaldo Carvalho de Melo 	};
1677c657876SArnaldo Carvalho de Melo 
1687c657876SArnaldo Carvalho de Melo 	if (state >= DCCP_MAX_STATES)
1697c657876SArnaldo Carvalho de Melo 		return "INVALID STATE!";
1707c657876SArnaldo Carvalho de Melo 	else
1717c657876SArnaldo Carvalho de Melo 		return dccp_state_names[state];
1727c657876SArnaldo Carvalho de Melo }
1737c657876SArnaldo Carvalho de Melo 
1747c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name);
1757c657876SArnaldo Carvalho de Melo 
17672478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
1773e0fadc5SArnaldo Carvalho de Melo {
1783e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
179a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
1803e0fadc5SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1813e0fadc5SArnaldo Carvalho de Melo 
182a4bf3902SArnaldo Carvalho de Melo 	dccp_minisock_init(&dp->dccps_minisock);
1833e0fadc5SArnaldo Carvalho de Melo 
184e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
185e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
186e18d7a98SArnaldo Carvalho de Melo 	sk->sk_state		= DCCP_CLOSED;
187e18d7a98SArnaldo Carvalho de Melo 	sk->sk_write_space	= dccp_write_space;
188e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss	= dccp_sync_mss;
189e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_mss_cache	= 536;
190e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_rate_last	= jiffies;
191e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
192e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
193e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_l_ack_ratio	= dp->dccps_r_ack_ratio = 1;
194e18d7a98SArnaldo Carvalho de Melo 
195e18d7a98SArnaldo Carvalho de Melo 	dccp_init_xmit_timers(sk);
196e18d7a98SArnaldo Carvalho de Melo 
1973e0fadc5SArnaldo Carvalho de Melo 	/*
1983e0fadc5SArnaldo Carvalho de Melo 	 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1993e0fadc5SArnaldo Carvalho de Melo 	 * the listening (master) sock get CCID control blocks, which is not
2003e0fadc5SArnaldo Carvalho de Melo 	 * necessary, but for now, to not mess with the test userspace apps,
2013e0fadc5SArnaldo Carvalho de Melo 	 * lets leave it here, later the real solution is to do this in a
2023e0fadc5SArnaldo Carvalho de Melo 	 * setsockopt(CCIDs-I-want/accept). -acme
2033e0fadc5SArnaldo Carvalho de Melo 	 */
20472478873SArnaldo Carvalho de Melo 	if (likely(ctl_sock_initialized)) {
2058ca0d17bSArnaldo Carvalho de Melo 		int rc = dccp_feat_init(dmsk);
2063e0fadc5SArnaldo Carvalho de Melo 
2073e0fadc5SArnaldo Carvalho de Melo 		if (rc)
2083e0fadc5SArnaldo Carvalho de Melo 			return rc;
2093e0fadc5SArnaldo Carvalho de Melo 
210a4bf3902SArnaldo Carvalho de Melo 		if (dmsk->dccpms_send_ack_vector) {
2113e0fadc5SArnaldo Carvalho de Melo 			dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
2123e0fadc5SArnaldo Carvalho de Melo 			if (dp->dccps_hc_rx_ackvec == NULL)
2133e0fadc5SArnaldo Carvalho de Melo 				return -ENOMEM;
2143e0fadc5SArnaldo Carvalho de Melo 		}
215a4bf3902SArnaldo Carvalho de Melo 		dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
2163e0fadc5SArnaldo Carvalho de Melo 						      sk, GFP_KERNEL);
217a4bf3902SArnaldo Carvalho de Melo 		dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
2183e0fadc5SArnaldo Carvalho de Melo 						      sk, GFP_KERNEL);
2193e0fadc5SArnaldo Carvalho de Melo 		if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
2203e0fadc5SArnaldo Carvalho de Melo 			     dp->dccps_hc_tx_ccid == NULL)) {
2213e0fadc5SArnaldo Carvalho de Melo 			ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
2223e0fadc5SArnaldo Carvalho de Melo 			ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
223a4bf3902SArnaldo Carvalho de Melo 			if (dmsk->dccpms_send_ack_vector) {
2243e0fadc5SArnaldo Carvalho de Melo 				dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
2253e0fadc5SArnaldo Carvalho de Melo 				dp->dccps_hc_rx_ackvec = NULL;
2263e0fadc5SArnaldo Carvalho de Melo 			}
2273e0fadc5SArnaldo Carvalho de Melo 			dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
2283e0fadc5SArnaldo Carvalho de Melo 			return -ENOMEM;
2293e0fadc5SArnaldo Carvalho de Melo 		}
2303e0fadc5SArnaldo Carvalho de Melo 	} else {
2313e0fadc5SArnaldo Carvalho de Melo 		/* control socket doesn't need feat nego */
232a4bf3902SArnaldo Carvalho de Melo 		INIT_LIST_HEAD(&dmsk->dccpms_pending);
233a4bf3902SArnaldo Carvalho de Melo 		INIT_LIST_HEAD(&dmsk->dccpms_conf);
2343e0fadc5SArnaldo Carvalho de Melo 	}
2353e0fadc5SArnaldo Carvalho de Melo 
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 
2413e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk)
2423e0fadc5SArnaldo Carvalho de Melo {
2433e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
2448ca0d17bSArnaldo Carvalho de Melo 	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 
2628ca0d17bSArnaldo Carvalho de Melo 	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 */
2718ca0d17bSArnaldo Carvalho de Melo 	dccp_feat_clean(dmsk);
2723e0fadc5SArnaldo Carvalho de Melo 
2733e0fadc5SArnaldo Carvalho de Melo 	return 0;
2743e0fadc5SArnaldo Carvalho de Melo }
2753e0fadc5SArnaldo Carvalho de Melo 
2763e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock);
2773e0fadc5SArnaldo Carvalho de Melo 
27872a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog)
2797c657876SArnaldo Carvalho de Melo {
28067e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
28167e6b629SArnaldo Carvalho de Melo 
28267e6b629SArnaldo Carvalho de Melo 	dp->dccps_role = DCCP_ROLE_LISTEN;
28372a3effaSEric Dumazet 	return inet_csk_listen_start(sk, backlog);
2847c657876SArnaldo Carvalho de Melo }
2857c657876SArnaldo Carvalho de Melo 
286ce865a61SGerrit Renker static inline int dccp_need_reset(int state)
287ce865a61SGerrit Renker {
288ce865a61SGerrit Renker 	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
289ce865a61SGerrit Renker 	       state != DCCP_REQUESTING;
290ce865a61SGerrit Renker }
291ce865a61SGerrit Renker 
2927c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags)
2937c657876SArnaldo Carvalho de Melo {
2947c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2957c657876SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
2967c657876SArnaldo Carvalho de Melo 	int err = 0;
2977c657876SArnaldo Carvalho de Melo 	const int old_state = sk->sk_state;
2987c657876SArnaldo Carvalho de Melo 
2997c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_CLOSED)
3007c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
3017c657876SArnaldo Carvalho de Melo 
302ce865a61SGerrit Renker 	/*
303ce865a61SGerrit Renker 	 * This corresponds to the ABORT function of RFC793, sec. 3.8
304ce865a61SGerrit Renker 	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
305ce865a61SGerrit Renker 	 */
3067c657876SArnaldo Carvalho de Melo 	if (old_state == DCCP_LISTEN) {
3077c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
308ce865a61SGerrit Renker 	} else if (dccp_need_reset(old_state)) {
309ce865a61SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
310ce865a61SGerrit Renker 		sk->sk_err = ECONNRESET;
3117c657876SArnaldo Carvalho de Melo 	} else if (old_state == DCCP_REQUESTING)
3127c657876SArnaldo Carvalho de Melo 		sk->sk_err = ECONNRESET;
3137c657876SArnaldo Carvalho de Melo 
3147c657876SArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
3157c657876SArnaldo Carvalho de Melo 	__skb_queue_purge(&sk->sk_receive_queue);
3167c657876SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
3177c657876SArnaldo Carvalho de Melo 		__kfree_skb(sk->sk_send_head);
3187c657876SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
3197c657876SArnaldo Carvalho de Melo 	}
3207c657876SArnaldo Carvalho de Melo 
3217c657876SArnaldo Carvalho de Melo 	inet->dport = 0;
3227c657876SArnaldo Carvalho de Melo 
3237c657876SArnaldo Carvalho de Melo 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
3247c657876SArnaldo Carvalho de Melo 		inet_reset_saddr(sk);
3257c657876SArnaldo Carvalho de Melo 
3267c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = 0;
3277c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
3287c657876SArnaldo Carvalho de Melo 
3297c657876SArnaldo Carvalho de Melo 	icsk->icsk_backoff = 0;
3307c657876SArnaldo Carvalho de Melo 	inet_csk_delack_init(sk);
3317c657876SArnaldo Carvalho de Melo 	__sk_dst_reset(sk);
3327c657876SArnaldo Carvalho de Melo 
3337c657876SArnaldo Carvalho de Melo 	BUG_TRAP(!inet->num || icsk->icsk_bind_hash);
3347c657876SArnaldo Carvalho de Melo 
3357c657876SArnaldo Carvalho de Melo 	sk->sk_error_report(sk);
3367c657876SArnaldo Carvalho de Melo 	return err;
3377c657876SArnaldo Carvalho de Melo }
3387c657876SArnaldo Carvalho de Melo 
339f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect);
340f21e68caSArnaldo Carvalho de Melo 
341331968bdSArnaldo Carvalho de Melo /*
342331968bdSArnaldo Carvalho de Melo  *	Wait for a DCCP event.
343331968bdSArnaldo Carvalho de Melo  *
344331968bdSArnaldo Carvalho de Melo  *	Note that we don't need to lock the socket, as the upper poll layers
345331968bdSArnaldo Carvalho de Melo  *	take care of normal races (between the test and the event) and we don't
346331968bdSArnaldo Carvalho de Melo  *	go look at any of the socket buffers directly.
347331968bdSArnaldo Carvalho de Melo  */
348f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock,
349331968bdSArnaldo Carvalho de Melo 		       poll_table *wait)
350331968bdSArnaldo Carvalho de Melo {
351331968bdSArnaldo Carvalho de Melo 	unsigned int mask;
352331968bdSArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
353331968bdSArnaldo Carvalho de Melo 
354331968bdSArnaldo Carvalho de Melo 	poll_wait(file, sk->sk_sleep, wait);
355331968bdSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
356331968bdSArnaldo Carvalho de Melo 		return inet_csk_listen_poll(sk);
357331968bdSArnaldo Carvalho de Melo 
358331968bdSArnaldo Carvalho de Melo 	/* Socket is not locked. We are protected from async events
359331968bdSArnaldo Carvalho de Melo 	   by poll logic and correct handling of state changes
360331968bdSArnaldo Carvalho de Melo 	   made by another threads is impossible in any case.
361331968bdSArnaldo Carvalho de Melo 	 */
362331968bdSArnaldo Carvalho de Melo 
363331968bdSArnaldo Carvalho de Melo 	mask = 0;
364331968bdSArnaldo Carvalho de Melo 	if (sk->sk_err)
365331968bdSArnaldo Carvalho de Melo 		mask = POLLERR;
366331968bdSArnaldo Carvalho de Melo 
367331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
368331968bdSArnaldo Carvalho de Melo 		mask |= POLLHUP;
369331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown & RCV_SHUTDOWN)
370f348d70aSDavide Libenzi 		mask |= POLLIN | POLLRDNORM | POLLRDHUP;
371331968bdSArnaldo Carvalho de Melo 
372331968bdSArnaldo Carvalho de Melo 	/* Connected? */
373331968bdSArnaldo Carvalho de Melo 	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
374331968bdSArnaldo Carvalho de Melo 		if (atomic_read(&sk->sk_rmem_alloc) > 0)
375331968bdSArnaldo Carvalho de Melo 			mask |= POLLIN | POLLRDNORM;
376331968bdSArnaldo Carvalho de Melo 
377331968bdSArnaldo Carvalho de Melo 		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
378331968bdSArnaldo Carvalho de Melo 			if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
379331968bdSArnaldo Carvalho de Melo 				mask |= POLLOUT | POLLWRNORM;
380331968bdSArnaldo Carvalho de Melo 			} else {  /* send SIGIO later */
381331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_ASYNC_NOSPACE,
382331968bdSArnaldo Carvalho de Melo 					&sk->sk_socket->flags);
383331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
384331968bdSArnaldo Carvalho de Melo 
385331968bdSArnaldo Carvalho de Melo 				/* Race breaker. If space is freed after
386331968bdSArnaldo Carvalho de Melo 				 * wspace test but before the flags are set,
387331968bdSArnaldo Carvalho de Melo 				 * IO signal will be lost.
388331968bdSArnaldo Carvalho de Melo 				 */
389331968bdSArnaldo Carvalho de Melo 				if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
390331968bdSArnaldo Carvalho de Melo 					mask |= POLLOUT | POLLWRNORM;
391331968bdSArnaldo Carvalho de Melo 			}
392331968bdSArnaldo Carvalho de Melo 		}
393331968bdSArnaldo Carvalho de Melo 	}
394331968bdSArnaldo Carvalho de Melo 	return mask;
395331968bdSArnaldo Carvalho de Melo }
396331968bdSArnaldo Carvalho de Melo 
397f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll);
398f21e68caSArnaldo Carvalho de Melo 
3997c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4007c657876SArnaldo Carvalho de Melo {
4016273172eSArnaldo Carvalho de Melo 	int rc = -ENOTCONN;
4026273172eSArnaldo Carvalho de Melo 
4036273172eSArnaldo Carvalho de Melo 	lock_sock(sk);
4046273172eSArnaldo Carvalho de Melo 
4056273172eSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
4066273172eSArnaldo Carvalho de Melo 		goto out;
4076273172eSArnaldo Carvalho de Melo 
4086273172eSArnaldo Carvalho de Melo 	switch (cmd) {
4096273172eSArnaldo Carvalho de Melo 	case SIOCINQ: {
4106273172eSArnaldo Carvalho de Melo 		struct sk_buff *skb;
4116273172eSArnaldo Carvalho de Melo 		unsigned long amount = 0;
4126273172eSArnaldo Carvalho de Melo 
4136273172eSArnaldo Carvalho de Melo 		skb = skb_peek(&sk->sk_receive_queue);
4146273172eSArnaldo Carvalho de Melo 		if (skb != NULL) {
4156273172eSArnaldo Carvalho de Melo 			/*
4166273172eSArnaldo Carvalho de Melo 			 * We will only return the amount of this packet since
4176273172eSArnaldo Carvalho de Melo 			 * that is all that will be read.
4186273172eSArnaldo Carvalho de Melo 			 */
4196273172eSArnaldo Carvalho de Melo 			amount = skb->len;
4206273172eSArnaldo Carvalho de Melo 		}
4216273172eSArnaldo Carvalho de Melo 		rc = put_user(amount, (int __user *)arg);
4226273172eSArnaldo Carvalho de Melo 	}
4236273172eSArnaldo Carvalho de Melo 		break;
4246273172eSArnaldo Carvalho de Melo 	default:
4256273172eSArnaldo Carvalho de Melo 		rc = -ENOIOCTLCMD;
4266273172eSArnaldo Carvalho de Melo 		break;
4276273172eSArnaldo Carvalho de Melo 	}
4286273172eSArnaldo Carvalho de Melo out:
4296273172eSArnaldo Carvalho de Melo 	release_sock(sk);
4306273172eSArnaldo Carvalho de Melo 	return rc;
4317c657876SArnaldo Carvalho de Melo }
4327c657876SArnaldo Carvalho de Melo 
433f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl);
434f21e68caSArnaldo Carvalho de Melo 
43560fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
43667e6b629SArnaldo Carvalho de Melo 				   char __user *optval, int optlen)
43767e6b629SArnaldo Carvalho de Melo {
43867e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
43967e6b629SArnaldo Carvalho de Melo 	struct dccp_service_list *sl = NULL;
44067e6b629SArnaldo Carvalho de Melo 
44167e6b629SArnaldo Carvalho de Melo 	if (service == DCCP_SERVICE_INVALID_VALUE ||
44267e6b629SArnaldo Carvalho de Melo 	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
44367e6b629SArnaldo Carvalho de Melo 		return -EINVAL;
44467e6b629SArnaldo Carvalho de Melo 
44567e6b629SArnaldo Carvalho de Melo 	if (optlen > sizeof(service)) {
44667e6b629SArnaldo Carvalho de Melo 		sl = kmalloc(optlen, GFP_KERNEL);
44767e6b629SArnaldo Carvalho de Melo 		if (sl == NULL)
44867e6b629SArnaldo Carvalho de Melo 			return -ENOMEM;
44967e6b629SArnaldo Carvalho de Melo 
45067e6b629SArnaldo Carvalho de Melo 		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
45167e6b629SArnaldo Carvalho de Melo 		if (copy_from_user(sl->dccpsl_list,
45267e6b629SArnaldo Carvalho de Melo 				   optval + sizeof(service),
45367e6b629SArnaldo Carvalho de Melo 				   optlen - sizeof(service)) ||
45467e6b629SArnaldo Carvalho de Melo 		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
45567e6b629SArnaldo Carvalho de Melo 			kfree(sl);
45667e6b629SArnaldo Carvalho de Melo 			return -EFAULT;
45767e6b629SArnaldo Carvalho de Melo 		}
45867e6b629SArnaldo Carvalho de Melo 	}
45967e6b629SArnaldo Carvalho de Melo 
46067e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
46167e6b629SArnaldo Carvalho de Melo 	dp->dccps_service = service;
46267e6b629SArnaldo Carvalho de Melo 
46367e6b629SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
46467e6b629SArnaldo Carvalho de Melo 
46567e6b629SArnaldo Carvalho de Melo 	dp->dccps_service_list = sl;
46667e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
46767e6b629SArnaldo Carvalho de Melo 	return 0;
46867e6b629SArnaldo Carvalho de Melo }
46967e6b629SArnaldo Carvalho de Melo 
470afe00251SAndrea Bittau /* byte 1 is feature.  the rest is the preference list */
471afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type,
472afe00251SAndrea Bittau 				  struct dccp_so_feat __user *optval)
473afe00251SAndrea Bittau {
474afe00251SAndrea Bittau 	struct dccp_so_feat opt;
475afe00251SAndrea Bittau 	u8 *val;
476afe00251SAndrea Bittau 	int rc;
477afe00251SAndrea Bittau 
478afe00251SAndrea Bittau 	if (copy_from_user(&opt, optval, sizeof(opt)))
479afe00251SAndrea Bittau 		return -EFAULT;
480afe00251SAndrea Bittau 
481afe00251SAndrea Bittau 	val = kmalloc(opt.dccpsf_len, GFP_KERNEL);
482afe00251SAndrea Bittau 	if (!val)
483afe00251SAndrea Bittau 		return -ENOMEM;
484afe00251SAndrea Bittau 
485afe00251SAndrea Bittau 	if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) {
486afe00251SAndrea Bittau 		rc = -EFAULT;
487afe00251SAndrea Bittau 		goto out_free_val;
488afe00251SAndrea Bittau 	}
489afe00251SAndrea Bittau 
4908ca0d17bSArnaldo Carvalho de Melo 	rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat,
4918ca0d17bSArnaldo Carvalho de Melo 			      val, opt.dccpsf_len, GFP_KERNEL);
492afe00251SAndrea Bittau 	if (rc)
493afe00251SAndrea Bittau 		goto out_free_val;
494afe00251SAndrea Bittau 
495afe00251SAndrea Bittau out:
496afe00251SAndrea Bittau 	return rc;
497afe00251SAndrea Bittau 
498afe00251SAndrea Bittau out_free_val:
499afe00251SAndrea Bittau 	kfree(val);
500afe00251SAndrea Bittau 	goto out;
501afe00251SAndrea Bittau }
502afe00251SAndrea Bittau 
5033fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
504a1d3a355SArnaldo Carvalho de Melo 		char __user *optval, int optlen)
5057c657876SArnaldo Carvalho de Melo {
50609dbc389SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
50709dbc389SGerrit Renker 	int val, err = 0;
5087c657876SArnaldo Carvalho de Melo 
509a84ffe43SArnaldo Carvalho de Melo 	if (optlen < sizeof(int))
510a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
511a84ffe43SArnaldo Carvalho de Melo 
512a84ffe43SArnaldo Carvalho de Melo 	if (get_user(val, (int __user *)optval))
513a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
514a84ffe43SArnaldo Carvalho de Melo 
51567e6b629SArnaldo Carvalho de Melo 	if (optname == DCCP_SOCKOPT_SERVICE)
51667e6b629SArnaldo Carvalho de Melo 		return dccp_setsockopt_service(sk, val, optval, optlen);
517a84ffe43SArnaldo Carvalho de Melo 
51867e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
519a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
520a84ffe43SArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_PACKET_SIZE:
5215aed3243SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
522841bac1dSArnaldo Carvalho de Melo 		err = 0;
523a84ffe43SArnaldo Carvalho de Melo 		break;
524afe00251SAndrea Bittau 	case DCCP_SOCKOPT_CHANGE_L:
525afe00251SAndrea Bittau 		if (optlen != sizeof(struct dccp_so_feat))
526afe00251SAndrea Bittau 			err = -EINVAL;
527afe00251SAndrea Bittau 		else
528afe00251SAndrea Bittau 			err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L,
5299faefb6dSAlan Cox 						     (struct dccp_so_feat __user *)
530afe00251SAndrea Bittau 						     optval);
531afe00251SAndrea Bittau 		break;
532afe00251SAndrea Bittau 	case DCCP_SOCKOPT_CHANGE_R:
533afe00251SAndrea Bittau 		if (optlen != sizeof(struct dccp_so_feat))
534afe00251SAndrea Bittau 			err = -EINVAL;
535afe00251SAndrea Bittau 		else
536afe00251SAndrea Bittau 			err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R,
5379faefb6dSAlan Cox 						     (struct dccp_so_feat __user *)
538afe00251SAndrea Bittau 						     optval);
539afe00251SAndrea Bittau 		break;
540b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
541b8599d20SGerrit Renker 		if (dp->dccps_role != DCCP_ROLE_SERVER)
542b8599d20SGerrit Renker 			err = -EOPNOTSUPP;
543b8599d20SGerrit Renker 		else
544b8599d20SGerrit Renker 			dp->dccps_server_timewait = (val != 0);
545b8599d20SGerrit Renker 		break;
5466f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:	/* sender side, RFC 4340, sec. 9.2 */
5476f4e5fffSGerrit Renker 		if (val < 0 || val > 15)
5486f4e5fffSGerrit Renker 			err = -EINVAL;
5496f4e5fffSGerrit Renker 		else
5506f4e5fffSGerrit Renker 			dp->dccps_pcslen = val;
5516f4e5fffSGerrit Renker 		break;
5526f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:	/* receiver side, RFC 4340 sec. 9.2.1 */
5536f4e5fffSGerrit Renker 		if (val < 0 || val > 15)
5546f4e5fffSGerrit Renker 			err = -EINVAL;
5556f4e5fffSGerrit Renker 		else {
5566f4e5fffSGerrit Renker 			dp->dccps_pcrlen = val;
5576f4e5fffSGerrit Renker 			/* FIXME: add feature negotiation,
5586f4e5fffSGerrit Renker 			 * ChangeL(MinimumChecksumCoverage, val) */
5596f4e5fffSGerrit Renker 		}
5606f4e5fffSGerrit Renker 		break;
561a84ffe43SArnaldo Carvalho de Melo 	default:
562a84ffe43SArnaldo Carvalho de Melo 		err = -ENOPROTOOPT;
563a84ffe43SArnaldo Carvalho de Melo 		break;
564a84ffe43SArnaldo Carvalho de Melo 	}
565a84ffe43SArnaldo Carvalho de Melo 
566a84ffe43SArnaldo Carvalho de Melo 	release_sock(sk);
567a84ffe43SArnaldo Carvalho de Melo 	return err;
5687c657876SArnaldo Carvalho de Melo }
5697c657876SArnaldo Carvalho de Melo 
5703fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname,
5713fdadf7dSDmitry Mishin 		    char __user *optval, int optlen)
5723fdadf7dSDmitry Mishin {
5733fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
5743fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
5753fdadf7dSDmitry Mishin 							     optname, optval,
5763fdadf7dSDmitry Mishin 							     optlen);
5773fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5783fdadf7dSDmitry Mishin }
579543d9cfeSArnaldo Carvalho de Melo 
580f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt);
581f21e68caSArnaldo Carvalho de Melo 
5823fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5833fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
5843fdadf7dSDmitry Mishin 			   char __user *optval, int optlen)
5853fdadf7dSDmitry Mishin {
586dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
587dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_setsockopt(sk, level, optname,
588dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
5893fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5903fdadf7dSDmitry Mishin }
591543d9cfeSArnaldo Carvalho de Melo 
5923fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
5933fdadf7dSDmitry Mishin #endif
5943fdadf7dSDmitry Mishin 
59567e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len,
59660fe62e7SAndrea Bittau 				   __be32 __user *optval,
59767e6b629SArnaldo Carvalho de Melo 				   int __user *optlen)
59867e6b629SArnaldo Carvalho de Melo {
59967e6b629SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
60067e6b629SArnaldo Carvalho de Melo 	const struct dccp_service_list *sl;
60167e6b629SArnaldo Carvalho de Melo 	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
60267e6b629SArnaldo Carvalho de Melo 
60367e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
60467e6b629SArnaldo Carvalho de Melo 	if ((sl = dp->dccps_service_list) != NULL) {
60567e6b629SArnaldo Carvalho de Melo 		slen = sl->dccpsl_nr * sizeof(u32);
60667e6b629SArnaldo Carvalho de Melo 		total_len += slen;
60767e6b629SArnaldo Carvalho de Melo 	}
60867e6b629SArnaldo Carvalho de Melo 
60967e6b629SArnaldo Carvalho de Melo 	err = -EINVAL;
61067e6b629SArnaldo Carvalho de Melo 	if (total_len > len)
61167e6b629SArnaldo Carvalho de Melo 		goto out;
61267e6b629SArnaldo Carvalho de Melo 
61367e6b629SArnaldo Carvalho de Melo 	err = 0;
61467e6b629SArnaldo Carvalho de Melo 	if (put_user(total_len, optlen) ||
61567e6b629SArnaldo Carvalho de Melo 	    put_user(dp->dccps_service, optval) ||
61667e6b629SArnaldo Carvalho de Melo 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
61767e6b629SArnaldo Carvalho de Melo 		err = -EFAULT;
61867e6b629SArnaldo Carvalho de Melo out:
61967e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
62067e6b629SArnaldo Carvalho de Melo 	return err;
62167e6b629SArnaldo Carvalho de Melo }
62267e6b629SArnaldo Carvalho de Melo 
6233fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
624a1d3a355SArnaldo Carvalho de Melo 		    char __user *optval, int __user *optlen)
6257c657876SArnaldo Carvalho de Melo {
626a84ffe43SArnaldo Carvalho de Melo 	struct dccp_sock *dp;
627a84ffe43SArnaldo Carvalho de Melo 	int val, len;
6287c657876SArnaldo Carvalho de Melo 
629a84ffe43SArnaldo Carvalho de Melo 	if (get_user(len, optlen))
630a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
631a84ffe43SArnaldo Carvalho de Melo 
63239ebc027SArnaldo Carvalho de Melo 	if (len < (int)sizeof(int))
633a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
634a84ffe43SArnaldo Carvalho de Melo 
635a84ffe43SArnaldo Carvalho de Melo 	dp = dccp_sk(sk);
636a84ffe43SArnaldo Carvalho de Melo 
637a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
638a84ffe43SArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_PACKET_SIZE:
6395aed3243SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
640841bac1dSArnaldo Carvalho de Melo 		return 0;
64188f964dbSArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_SERVICE:
64288f964dbSArnaldo Carvalho de Melo 		return dccp_getsockopt_service(sk, len,
64360fe62e7SAndrea Bittau 					       (__be32 __user *)optval, optlen);
6447c559a9eSGerrit Renker 	case DCCP_SOCKOPT_GET_CUR_MPS:
6457c559a9eSGerrit Renker 		val = dp->dccps_mss_cache;
6467c559a9eSGerrit Renker 		break;
647b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
648b8599d20SGerrit Renker 		val = dp->dccps_server_timewait;
649b8599d20SGerrit Renker 		break;
6506f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
6516f4e5fffSGerrit Renker 		val = dp->dccps_pcslen;
6526f4e5fffSGerrit Renker 		break;
6536f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
6546f4e5fffSGerrit Renker 		val = dp->dccps_pcrlen;
6556f4e5fffSGerrit Renker 		break;
65688f964dbSArnaldo Carvalho de Melo 	case 128 ... 191:
65788f964dbSArnaldo Carvalho de Melo 		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
65888f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
65988f964dbSArnaldo Carvalho de Melo 	case 192 ... 255:
66088f964dbSArnaldo Carvalho de Melo 		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
66188f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
662a84ffe43SArnaldo Carvalho de Melo 	default:
663a84ffe43SArnaldo Carvalho de Melo 		return -ENOPROTOOPT;
664a84ffe43SArnaldo Carvalho de Melo 	}
665a84ffe43SArnaldo Carvalho de Melo 
66679133506SGerrit Renker 	len = sizeof(val);
667a84ffe43SArnaldo Carvalho de Melo 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
668a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
669a84ffe43SArnaldo Carvalho de Melo 
670a84ffe43SArnaldo Carvalho de Melo 	return 0;
6717c657876SArnaldo Carvalho de Melo }
6727c657876SArnaldo Carvalho de Melo 
6733fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname,
6743fdadf7dSDmitry Mishin 		    char __user *optval, int __user *optlen)
6753fdadf7dSDmitry Mishin {
6763fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
6773fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
6783fdadf7dSDmitry Mishin 							     optname, optval,
6793fdadf7dSDmitry Mishin 							     optlen);
6803fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
6813fdadf7dSDmitry Mishin }
682543d9cfeSArnaldo Carvalho de Melo 
683f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt);
684f21e68caSArnaldo Carvalho de Melo 
6853fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
6863fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
6873fdadf7dSDmitry Mishin 			   char __user *optval, int __user *optlen)
6883fdadf7dSDmitry Mishin {
689dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
690dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_getsockopt(sk, level, optname,
691dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
6923fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
6933fdadf7dSDmitry Mishin }
694543d9cfeSArnaldo Carvalho de Melo 
6953fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
6963fdadf7dSDmitry Mishin #endif
6973fdadf7dSDmitry Mishin 
6987c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
6997c657876SArnaldo Carvalho de Melo 		 size_t len)
7007c657876SArnaldo Carvalho de Melo {
7017c657876SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
7027c657876SArnaldo Carvalho de Melo 	const int flags = msg->msg_flags;
7037c657876SArnaldo Carvalho de Melo 	const int noblock = flags & MSG_DONTWAIT;
7047c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
7057c657876SArnaldo Carvalho de Melo 	int rc, size;
7067c657876SArnaldo Carvalho de Melo 	long timeo;
7077c657876SArnaldo Carvalho de Melo 
7087c657876SArnaldo Carvalho de Melo 	if (len > dp->dccps_mss_cache)
7097c657876SArnaldo Carvalho de Melo 		return -EMSGSIZE;
7107c657876SArnaldo Carvalho de Melo 
7117c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
712b1308dc0SIan McDonald 
713b1308dc0SIan McDonald 	if (sysctl_dccp_tx_qlen &&
714b1308dc0SIan McDonald 	    (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
715b1308dc0SIan McDonald 		rc = -EAGAIN;
716b1308dc0SIan McDonald 		goto out_release;
717b1308dc0SIan McDonald 	}
718b1308dc0SIan McDonald 
71927258ee5SArnaldo Carvalho de Melo 	timeo = sock_sndtimeo(sk, noblock);
7207c657876SArnaldo Carvalho de Melo 
7217c657876SArnaldo Carvalho de Melo 	/*
7227c657876SArnaldo Carvalho de Melo 	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
7237c657876SArnaldo Carvalho de Melo 	 * so that the trick in dccp_rcv_request_sent_state_process.
7247c657876SArnaldo Carvalho de Melo 	 */
7257c657876SArnaldo Carvalho de Melo 	/* Wait for a connection to finish. */
726cecd8d0eSGerrit Renker 	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7277c657876SArnaldo Carvalho de Melo 		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
72827258ee5SArnaldo Carvalho de Melo 			goto out_release;
7297c657876SArnaldo Carvalho de Melo 
7307c657876SArnaldo Carvalho de Melo 	size = sk->sk_prot->max_header + len;
7317c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7327c657876SArnaldo Carvalho de Melo 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
7337c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7347c657876SArnaldo Carvalho de Melo 	if (skb == NULL)
7357c657876SArnaldo Carvalho de Melo 		goto out_release;
7367c657876SArnaldo Carvalho de Melo 
7377c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
7387c657876SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
73927258ee5SArnaldo Carvalho de Melo 	if (rc != 0)
74027258ee5SArnaldo Carvalho de Melo 		goto out_discard;
7417c657876SArnaldo Carvalho de Melo 
74297e5848dSIan McDonald 	skb_queue_tail(&sk->sk_write_queue, skb);
74397e5848dSIan McDonald 	dccp_write_xmit(sk,0);
7447c657876SArnaldo Carvalho de Melo out_release:
7457c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7467c657876SArnaldo Carvalho de Melo 	return rc ? : len;
74727258ee5SArnaldo Carvalho de Melo out_discard:
74827258ee5SArnaldo Carvalho de Melo 	kfree_skb(skb);
7497c657876SArnaldo Carvalho de Melo 	goto out_release;
7507c657876SArnaldo Carvalho de Melo }
7517c657876SArnaldo Carvalho de Melo 
752f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg);
753f21e68caSArnaldo Carvalho de Melo 
7547c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7557c657876SArnaldo Carvalho de Melo 		 size_t len, int nonblock, int flags, int *addr_len)
7567c657876SArnaldo Carvalho de Melo {
7577c657876SArnaldo Carvalho de Melo 	const struct dccp_hdr *dh;
7587c657876SArnaldo Carvalho de Melo 	long timeo;
7597c657876SArnaldo Carvalho de Melo 
7607c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7617c657876SArnaldo Carvalho de Melo 
762531669a0SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
763531669a0SArnaldo Carvalho de Melo 		len = -ENOTCONN;
7647c657876SArnaldo Carvalho de Melo 		goto out;
765531669a0SArnaldo Carvalho de Melo 	}
7667c657876SArnaldo Carvalho de Melo 
7677c657876SArnaldo Carvalho de Melo 	timeo = sock_rcvtimeo(sk, nonblock);
7687c657876SArnaldo Carvalho de Melo 
7697c657876SArnaldo Carvalho de Melo 	do {
770531669a0SArnaldo Carvalho de Melo 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
7717c657876SArnaldo Carvalho de Melo 
772531669a0SArnaldo Carvalho de Melo 		if (skb == NULL)
773531669a0SArnaldo Carvalho de Melo 			goto verify_sock_status;
7747c657876SArnaldo Carvalho de Melo 
7757c657876SArnaldo Carvalho de Melo 		dh = dccp_hdr(skb);
7767c657876SArnaldo Carvalho de Melo 
7770c869620SGerrit Renker 		switch (dh->dccph_type) {
7780c869620SGerrit Renker 		case DCCP_PKT_DATA:
7790c869620SGerrit Renker 		case DCCP_PKT_DATAACK:
7807c657876SArnaldo Carvalho de Melo 			goto found_ok_skb;
7817c657876SArnaldo Carvalho de Melo 
7820c869620SGerrit Renker 		case DCCP_PKT_CLOSE:
7830c869620SGerrit Renker 		case DCCP_PKT_CLOSEREQ:
7840c869620SGerrit Renker 			if (!(flags & MSG_PEEK))
7850c869620SGerrit Renker 				dccp_finish_passive_close(sk);
7860c869620SGerrit Renker 			/* fall through */
7870c869620SGerrit Renker 		case DCCP_PKT_RESET:
7880c869620SGerrit Renker 			dccp_pr_debug("found fin (%s) ok!\n",
7890c869620SGerrit Renker 				      dccp_packet_name(dh->dccph_type));
790531669a0SArnaldo Carvalho de Melo 			len = 0;
7917c657876SArnaldo Carvalho de Melo 			goto found_fin_ok;
7920c869620SGerrit Renker 		default:
7937690af3fSArnaldo Carvalho de Melo 			dccp_pr_debug("packet_type=%s\n",
7947690af3fSArnaldo Carvalho de Melo 				      dccp_packet_name(dh->dccph_type));
795624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
7960c869620SGerrit Renker 		}
797531669a0SArnaldo Carvalho de Melo verify_sock_status:
798531669a0SArnaldo Carvalho de Melo 		if (sock_flag(sk, SOCK_DONE)) {
799531669a0SArnaldo Carvalho de Melo 			len = 0;
8007c657876SArnaldo Carvalho de Melo 			break;
8017c657876SArnaldo Carvalho de Melo 		}
8027c657876SArnaldo Carvalho de Melo 
803531669a0SArnaldo Carvalho de Melo 		if (sk->sk_err) {
804531669a0SArnaldo Carvalho de Melo 			len = sock_error(sk);
8057c657876SArnaldo Carvalho de Melo 			break;
806531669a0SArnaldo Carvalho de Melo 		}
807531669a0SArnaldo Carvalho de Melo 
808531669a0SArnaldo Carvalho de Melo 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
809531669a0SArnaldo Carvalho de Melo 			len = 0;
810531669a0SArnaldo Carvalho de Melo 			break;
811531669a0SArnaldo Carvalho de Melo 		}
8127c657876SArnaldo Carvalho de Melo 
8137c657876SArnaldo Carvalho de Melo 		if (sk->sk_state == DCCP_CLOSED) {
8147c657876SArnaldo Carvalho de Melo 			if (!sock_flag(sk, SOCK_DONE)) {
8157c657876SArnaldo Carvalho de Melo 				/* This occurs when user tries to read
8167c657876SArnaldo Carvalho de Melo 				 * from never connected socket.
8177c657876SArnaldo Carvalho de Melo 				 */
818531669a0SArnaldo Carvalho de Melo 				len = -ENOTCONN;
8197c657876SArnaldo Carvalho de Melo 				break;
8207c657876SArnaldo Carvalho de Melo 			}
821531669a0SArnaldo Carvalho de Melo 			len = 0;
8227c657876SArnaldo Carvalho de Melo 			break;
8237c657876SArnaldo Carvalho de Melo 		}
8247c657876SArnaldo Carvalho de Melo 
8257c657876SArnaldo Carvalho de Melo 		if (!timeo) {
826531669a0SArnaldo Carvalho de Melo 			len = -EAGAIN;
8277c657876SArnaldo Carvalho de Melo 			break;
8287c657876SArnaldo Carvalho de Melo 		}
8297c657876SArnaldo Carvalho de Melo 
8307c657876SArnaldo Carvalho de Melo 		if (signal_pending(current)) {
831531669a0SArnaldo Carvalho de Melo 			len = sock_intr_errno(timeo);
8327c657876SArnaldo Carvalho de Melo 			break;
8337c657876SArnaldo Carvalho de Melo 		}
8347c657876SArnaldo Carvalho de Melo 
8357c657876SArnaldo Carvalho de Melo 		sk_wait_data(sk, &timeo);
8367c657876SArnaldo Carvalho de Melo 		continue;
8377c657876SArnaldo Carvalho de Melo 	found_ok_skb:
838531669a0SArnaldo Carvalho de Melo 		if (len > skb->len)
839531669a0SArnaldo Carvalho de Melo 			len = skb->len;
840531669a0SArnaldo Carvalho de Melo 		else if (len < skb->len)
841531669a0SArnaldo Carvalho de Melo 			msg->msg_flags |= MSG_TRUNC;
8427c657876SArnaldo Carvalho de Melo 
843531669a0SArnaldo Carvalho de Melo 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
8447c657876SArnaldo Carvalho de Melo 			/* Exception. Bailout! */
845531669a0SArnaldo Carvalho de Melo 			len = -EFAULT;
8467c657876SArnaldo Carvalho de Melo 			break;
8477c657876SArnaldo Carvalho de Melo 		}
8487c657876SArnaldo Carvalho de Melo 	found_fin_ok:
8497c657876SArnaldo Carvalho de Melo 		if (!(flags & MSG_PEEK))
850624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8517c657876SArnaldo Carvalho de Melo 		break;
852531669a0SArnaldo Carvalho de Melo 	} while (1);
8537c657876SArnaldo Carvalho de Melo out:
8547c657876SArnaldo Carvalho de Melo 	release_sock(sk);
855531669a0SArnaldo Carvalho de Melo 	return len;
8567c657876SArnaldo Carvalho de Melo }
8577c657876SArnaldo Carvalho de Melo 
858f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg);
859f21e68caSArnaldo Carvalho de Melo 
860f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog)
8617c657876SArnaldo Carvalho de Melo {
8627c657876SArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
8637c657876SArnaldo Carvalho de Melo 	unsigned char old_state;
8647c657876SArnaldo Carvalho de Melo 	int err;
8657c657876SArnaldo Carvalho de Melo 
8667c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
8677c657876SArnaldo Carvalho de Melo 
8687c657876SArnaldo Carvalho de Melo 	err = -EINVAL;
8697c657876SArnaldo Carvalho de Melo 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
8707c657876SArnaldo Carvalho de Melo 		goto out;
8717c657876SArnaldo Carvalho de Melo 
8727c657876SArnaldo Carvalho de Melo 	old_state = sk->sk_state;
8737c657876SArnaldo Carvalho de Melo 	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
8747c657876SArnaldo Carvalho de Melo 		goto out;
8757c657876SArnaldo Carvalho de Melo 
8767c657876SArnaldo Carvalho de Melo 	/* Really, if the socket is already in listen state
8777c657876SArnaldo Carvalho de Melo 	 * we can only allow the backlog to be adjusted.
8787c657876SArnaldo Carvalho de Melo 	 */
8797c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_LISTEN) {
8807c657876SArnaldo Carvalho de Melo 		/*
8817c657876SArnaldo Carvalho de Melo 		 * FIXME: here it probably should be sk->sk_prot->listen_start
8827c657876SArnaldo Carvalho de Melo 		 * see tcp_listen_start
8837c657876SArnaldo Carvalho de Melo 		 */
88472a3effaSEric Dumazet 		err = dccp_listen_start(sk, backlog);
8857c657876SArnaldo Carvalho de Melo 		if (err)
8867c657876SArnaldo Carvalho de Melo 			goto out;
8877c657876SArnaldo Carvalho de Melo 	}
8887c657876SArnaldo Carvalho de Melo 	sk->sk_max_ack_backlog = backlog;
8897c657876SArnaldo Carvalho de Melo 	err = 0;
8907c657876SArnaldo Carvalho de Melo 
8917c657876SArnaldo Carvalho de Melo out:
8927c657876SArnaldo Carvalho de Melo 	release_sock(sk);
8937c657876SArnaldo Carvalho de Melo 	return err;
8947c657876SArnaldo Carvalho de Melo }
8957c657876SArnaldo Carvalho de Melo 
896f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen);
897f21e68caSArnaldo Carvalho de Melo 
8980c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk)
8997c657876SArnaldo Carvalho de Melo {
9000c869620SGerrit Renker 	u8 next_state = DCCP_CLOSED;
9017c657876SArnaldo Carvalho de Melo 
9020c869620SGerrit Renker 	switch (sk->sk_state) {
9030c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
9040c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
9050c869620SGerrit Renker 		dccp_finish_passive_close(sk);
9060c869620SGerrit Renker 		break;
9070c869620SGerrit Renker 	case DCCP_PARTOPEN:
9080c869620SGerrit Renker 		dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
9090c869620SGerrit Renker 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
9100c869620SGerrit Renker 		/* fall through */
9110c869620SGerrit Renker 	case DCCP_OPEN:
9120c869620SGerrit Renker 		dccp_send_close(sk, 1);
9137c657876SArnaldo Carvalho de Melo 
914b8599d20SGerrit Renker 		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
915b8599d20SGerrit Renker 		    !dccp_sk(sk)->dccps_server_timewait)
9160c869620SGerrit Renker 			next_state = DCCP_ACTIVE_CLOSEREQ;
9170c869620SGerrit Renker 		else
9180c869620SGerrit Renker 			next_state = DCCP_CLOSING;
9190c869620SGerrit Renker 		/* fall through */
9200c869620SGerrit Renker 	default:
9210c869620SGerrit Renker 		dccp_set_state(sk, next_state);
9220c869620SGerrit Renker 	}
9237c657876SArnaldo Carvalho de Melo }
9247c657876SArnaldo Carvalho de Melo 
9257c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout)
9267c657876SArnaldo Carvalho de Melo {
92797e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
9287c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
929d83bd95bSGerrit Renker 	u32 data_was_unread = 0;
930134af346SHerbert Xu 	int state;
9317c657876SArnaldo Carvalho de Melo 
9327c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
9337c657876SArnaldo Carvalho de Melo 
9347c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
9357c657876SArnaldo Carvalho de Melo 
9367c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
9377c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
9387c657876SArnaldo Carvalho de Melo 
9397c657876SArnaldo Carvalho de Melo 		/* Special case. */
9407c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
9417c657876SArnaldo Carvalho de Melo 
9427c657876SArnaldo Carvalho de Melo 		goto adjudge_to_death;
9437c657876SArnaldo Carvalho de Melo 	}
9447c657876SArnaldo Carvalho de Melo 
94597e5848dSIan McDonald 	sk_stop_timer(sk, &dp->dccps_xmit_timer);
94697e5848dSIan McDonald 
9477c657876SArnaldo Carvalho de Melo 	/*
9487c657876SArnaldo Carvalho de Melo 	 * We need to flush the recv. buffs.  We do this only on the
9497c657876SArnaldo Carvalho de Melo 	 * descriptor close, not protocol-sourced closes, because the
9507c657876SArnaldo Carvalho de Melo 	  *reader process may not have drained the data yet!
9517c657876SArnaldo Carvalho de Melo 	 */
9527c657876SArnaldo Carvalho de Melo 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
953d83bd95bSGerrit Renker 		data_was_unread += skb->len;
9547c657876SArnaldo Carvalho de Melo 		__kfree_skb(skb);
9557c657876SArnaldo Carvalho de Melo 	}
9567c657876SArnaldo Carvalho de Melo 
957d83bd95bSGerrit Renker 	if (data_was_unread) {
958d83bd95bSGerrit Renker 		/* Unread data was tossed, send an appropriate Reset Code */
959d83bd95bSGerrit Renker 		DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
960d83bd95bSGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
961d83bd95bSGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
962d83bd95bSGerrit Renker 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
9637c657876SArnaldo Carvalho de Melo 		/* Check zero linger _after_ checking for unread data. */
9647c657876SArnaldo Carvalho de Melo 		sk->sk_prot->disconnect(sk, 0);
9650c869620SGerrit Renker 	} else if (sk->sk_state != DCCP_CLOSED) {
9660c869620SGerrit Renker 		dccp_terminate_connection(sk);
9677c657876SArnaldo Carvalho de Melo 	}
9687c657876SArnaldo Carvalho de Melo 
9697c657876SArnaldo Carvalho de Melo 	sk_stream_wait_close(sk, timeout);
9707c657876SArnaldo Carvalho de Melo 
9717c657876SArnaldo Carvalho de Melo adjudge_to_death:
972134af346SHerbert Xu 	state = sk->sk_state;
973134af346SHerbert Xu 	sock_hold(sk);
974134af346SHerbert Xu 	sock_orphan(sk);
975134af346SHerbert Xu 	atomic_inc(sk->sk_prot->orphan_count);
976134af346SHerbert Xu 
9777ad07e7cSArnaldo Carvalho de Melo 	/*
9787ad07e7cSArnaldo Carvalho de Melo 	 * It is the last release_sock in its life. It will remove backlog.
9797ad07e7cSArnaldo Carvalho de Melo 	 */
9807c657876SArnaldo Carvalho de Melo 	release_sock(sk);
9817c657876SArnaldo Carvalho de Melo 	/*
9827c657876SArnaldo Carvalho de Melo 	 * Now socket is owned by kernel and we acquire BH lock
9837c657876SArnaldo Carvalho de Melo 	 * to finish close. No need to check for user refs.
9847c657876SArnaldo Carvalho de Melo 	 */
9857c657876SArnaldo Carvalho de Melo 	local_bh_disable();
9867c657876SArnaldo Carvalho de Melo 	bh_lock_sock(sk);
9877c657876SArnaldo Carvalho de Melo 	BUG_TRAP(!sock_owned_by_user(sk));
9887c657876SArnaldo Carvalho de Melo 
989134af346SHerbert Xu 	/* Have we already been destroyed by a softirq or backlog? */
990134af346SHerbert Xu 	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
991134af346SHerbert Xu 		goto out;
9927c657876SArnaldo Carvalho de Melo 
9937c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_CLOSED)
9947c657876SArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
9957c657876SArnaldo Carvalho de Melo 
9967c657876SArnaldo Carvalho de Melo 	/* Otherwise, socket is reprieved until protocol close. */
9977c657876SArnaldo Carvalho de Melo 
998134af346SHerbert Xu out:
9997c657876SArnaldo Carvalho de Melo 	bh_unlock_sock(sk);
10007c657876SArnaldo Carvalho de Melo 	local_bh_enable();
10017c657876SArnaldo Carvalho de Melo 	sock_put(sk);
10027c657876SArnaldo Carvalho de Melo }
10037c657876SArnaldo Carvalho de Melo 
1004f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close);
1005f21e68caSArnaldo Carvalho de Melo 
10067c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how)
10077c657876SArnaldo Carvalho de Melo {
10088e8c71f1SGerrit Renker 	dccp_pr_debug("called shutdown(%x)\n", how);
10097c657876SArnaldo Carvalho de Melo }
10107c657876SArnaldo Carvalho de Melo 
1011f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown);
1012f21e68caSArnaldo Carvalho de Melo 
101324e8b7e4SYOSHIFUJI Hideaki static inline int dccp_mib_init(void)
10147c657876SArnaldo Carvalho de Melo {
101524e8b7e4SYOSHIFUJI Hideaki 	return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
10167c657876SArnaldo Carvalho de Melo }
10177c657876SArnaldo Carvalho de Melo 
101824e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void)
101946f09ffaSArnaldo Carvalho de Melo {
102024e8b7e4SYOSHIFUJI Hideaki 	snmp_mib_free((void**)dccp_statistics);
102146f09ffaSArnaldo Carvalho de Melo }
102246f09ffaSArnaldo Carvalho de Melo 
10237c657876SArnaldo Carvalho de Melo static int thash_entries;
10247c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444);
10257c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
10267c657876SArnaldo Carvalho de Melo 
1027a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG
10287c657876SArnaldo Carvalho de Melo int dccp_debug;
1029042d18f9SGerrit Renker module_param(dccp_debug, bool, 0444);
10307c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1031f21e68caSArnaldo Carvalho de Melo 
1032f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug);
1033a1d3a355SArnaldo Carvalho de Melo #endif
10347c657876SArnaldo Carvalho de Melo 
10357c657876SArnaldo Carvalho de Melo static int __init dccp_init(void)
10367c657876SArnaldo Carvalho de Melo {
10377c657876SArnaldo Carvalho de Melo 	unsigned long goal;
10387c657876SArnaldo Carvalho de Melo 	int ehash_order, bhash_order, i;
1039b61fafc4SArnaldo Carvalho de Melo 	int rc = -ENOBUFS;
10407c657876SArnaldo Carvalho de Melo 
10417690af3fSArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep =
10427690af3fSArnaldo Carvalho de Melo 		kmem_cache_create("dccp_bind_bucket",
10437690af3fSArnaldo Carvalho de Melo 				  sizeof(struct inet_bind_bucket), 0,
104420c2df83SPaul Mundt 				  SLAB_HWCACHE_ALIGN, NULL);
10457c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bind_bucket_cachep)
1046b61fafc4SArnaldo Carvalho de Melo 		goto out;
10477c657876SArnaldo Carvalho de Melo 
10487c657876SArnaldo Carvalho de Melo 	/*
10497c657876SArnaldo Carvalho de Melo 	 * Size and allocate the main established and bind bucket
10507c657876SArnaldo Carvalho de Melo 	 * hash tables.
10517c657876SArnaldo Carvalho de Melo 	 *
10527c657876SArnaldo Carvalho de Melo 	 * The methodology is similar to that of the buffer cache.
10537c657876SArnaldo Carvalho de Melo 	 */
10547c657876SArnaldo Carvalho de Melo 	if (num_physpages >= (128 * 1024))
10557c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (21 - PAGE_SHIFT);
10567c657876SArnaldo Carvalho de Melo 	else
10577c657876SArnaldo Carvalho de Melo 		goal = num_physpages >> (23 - PAGE_SHIFT);
10587c657876SArnaldo Carvalho de Melo 
10597c657876SArnaldo Carvalho de Melo 	if (thash_entries)
10607690af3fSArnaldo Carvalho de Melo 		goal = (thash_entries *
10617690af3fSArnaldo Carvalho de Melo 			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
10627c657876SArnaldo Carvalho de Melo 	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
10637c657876SArnaldo Carvalho de Melo 		;
10647c657876SArnaldo Carvalho de Melo 	do {
10657c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
10667c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_ehash_bucket);
10677690af3fSArnaldo Carvalho de Melo 		while (dccp_hashinfo.ehash_size &
10687690af3fSArnaldo Carvalho de Melo 		       (dccp_hashinfo.ehash_size - 1))
10697c657876SArnaldo Carvalho de Melo 			dccp_hashinfo.ehash_size--;
10707c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
10717c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, ehash_order);
10727c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
10737c657876SArnaldo Carvalho de Melo 
10747c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.ehash) {
107559348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP established hash table");
10767c657876SArnaldo Carvalho de Melo 		goto out_free_bind_bucket_cachep;
10777c657876SArnaldo Carvalho de Melo 	}
10787c657876SArnaldo Carvalho de Melo 
1079dbca9b27SEric Dumazet 	for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
10807c657876SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
1081dbca9b27SEric Dumazet 		INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].twchain);
10827c657876SArnaldo Carvalho de Melo 	}
10837c657876SArnaldo Carvalho de Melo 
1084230140cfSEric Dumazet 	if (inet_ehash_locks_alloc(&dccp_hashinfo))
1085230140cfSEric Dumazet 			goto out_free_dccp_ehash;
1086230140cfSEric Dumazet 
10877c657876SArnaldo Carvalho de Melo 	bhash_order = ehash_order;
10887c657876SArnaldo Carvalho de Melo 
10897c657876SArnaldo Carvalho de Melo 	do {
10907c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
10917c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_bind_hashbucket);
10927690af3fSArnaldo Carvalho de Melo 		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
10937690af3fSArnaldo Carvalho de Melo 		    bhash_order > 0)
10947c657876SArnaldo Carvalho de Melo 			continue;
10957c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
10967c657876SArnaldo Carvalho de Melo 			__get_free_pages(GFP_ATOMIC, bhash_order);
10977c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
10987c657876SArnaldo Carvalho de Melo 
10997c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bhash) {
110059348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP bind hash table");
1101230140cfSEric Dumazet 		goto out_free_dccp_locks;
11027c657876SArnaldo Carvalho de Melo 	}
11037c657876SArnaldo Carvalho de Melo 
11047c657876SArnaldo Carvalho de Melo 	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
11057c657876SArnaldo Carvalho de Melo 		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
11067c657876SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
11077c657876SArnaldo Carvalho de Melo 	}
11087c657876SArnaldo Carvalho de Melo 
110946f09ffaSArnaldo Carvalho de Melo 	rc = dccp_mib_init();
1110fa23e2ecSArnaldo Carvalho de Melo 	if (rc)
11117c657876SArnaldo Carvalho de Melo 		goto out_free_dccp_bhash;
11127c657876SArnaldo Carvalho de Melo 
11139b07ef5dSArnaldo Carvalho de Melo 	rc = dccp_ackvec_init();
11147c657876SArnaldo Carvalho de Melo 	if (rc)
1115b61fafc4SArnaldo Carvalho de Melo 		goto out_free_dccp_mib;
11169b07ef5dSArnaldo Carvalho de Melo 
1117e55d912fSArnaldo Carvalho de Melo 	rc = dccp_sysctl_init();
11189b07ef5dSArnaldo Carvalho de Melo 	if (rc)
11199b07ef5dSArnaldo Carvalho de Melo 		goto out_ackvec_exit;
11204c70f383SGerrit Renker 
11214c70f383SGerrit Renker 	dccp_timestamping_init();
11227c657876SArnaldo Carvalho de Melo out:
11237c657876SArnaldo Carvalho de Melo 	return rc;
11249b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit:
11259b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1126b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib:
112746f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
11287c657876SArnaldo Carvalho de Melo out_free_dccp_bhash:
11297c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
11307c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bhash = NULL;
1131230140cfSEric Dumazet out_free_dccp_locks:
1132230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11337c657876SArnaldo Carvalho de Melo out_free_dccp_ehash:
11347c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
11357c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.ehash = NULL;
11367c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep:
11377c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11387c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep = NULL;
11397c657876SArnaldo Carvalho de Melo 	goto out;
11407c657876SArnaldo Carvalho de Melo }
11417c657876SArnaldo Carvalho de Melo 
11427c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void)
11437c657876SArnaldo Carvalho de Melo {
114446f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
1145725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash,
1146725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.bhash_size *
1147725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_bind_hashbucket)));
1148725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash,
1149725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.ehash_size *
1150725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_ehash_bucket)));
1151230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11527c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11539b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1154e55d912fSArnaldo Carvalho de Melo 	dccp_sysctl_exit();
11557c657876SArnaldo Carvalho de Melo }
11567c657876SArnaldo Carvalho de Melo 
11577c657876SArnaldo Carvalho de Melo module_init(dccp_init);
11587c657876SArnaldo Carvalho de Melo module_exit(dccp_fini);
11597c657876SArnaldo Carvalho de Melo 
11607c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
11617c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
11627c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1163