xref: /openbmc/linux/net/dccp/proto.c (revision 55d95590)
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 
43dd24c001SEric Dumazet struct percpu_counter dccp_orphan_count;
44f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count);
45f21e68caSArnaldo Carvalho de Melo 
465caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo;
47075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo);
48075ae866SArnaldo Carvalho de Melo 
49b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */
50b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5;
51b1308dc0SIan McDonald 
52c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state)
53c25a18baSArnaldo Carvalho de Melo {
54c25a18baSArnaldo Carvalho de Melo 	const int oldstate = sk->sk_state;
55c25a18baSArnaldo Carvalho de Melo 
56f11135a3SGerrit Renker 	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
57c25a18baSArnaldo Carvalho de Melo 		      dccp_state_name(oldstate), dccp_state_name(state));
58c25a18baSArnaldo Carvalho de Melo 	WARN_ON(state == oldstate);
59c25a18baSArnaldo Carvalho de Melo 
60c25a18baSArnaldo Carvalho de Melo 	switch (state) {
61c25a18baSArnaldo Carvalho de Melo 	case DCCP_OPEN:
62c25a18baSArnaldo Carvalho de Melo 		if (oldstate != DCCP_OPEN)
63c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
646eb55d17SGerrit Renker 		/* Client retransmits all Confirm options until entering OPEN */
656eb55d17SGerrit Renker 		if (oldstate == DCCP_PARTOPEN)
666eb55d17SGerrit Renker 			dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
67c25a18baSArnaldo Carvalho de Melo 		break;
68c25a18baSArnaldo Carvalho de Melo 
69c25a18baSArnaldo Carvalho de Melo 	case DCCP_CLOSED:
700c869620SGerrit Renker 		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
710c869620SGerrit Renker 		    oldstate == DCCP_CLOSING)
72c25a18baSArnaldo Carvalho de Melo 			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
73c25a18baSArnaldo Carvalho de Melo 
74c25a18baSArnaldo Carvalho de Melo 		sk->sk_prot->unhash(sk);
75c25a18baSArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash != NULL &&
76c25a18baSArnaldo Carvalho de Melo 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
77ab1e0a13SArnaldo Carvalho de Melo 			inet_put_port(sk);
78c25a18baSArnaldo Carvalho de Melo 		/* fall through */
79c25a18baSArnaldo Carvalho de Melo 	default:
80c25a18baSArnaldo Carvalho de Melo 		if (oldstate == DCCP_OPEN)
81c25a18baSArnaldo Carvalho de Melo 			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
82c25a18baSArnaldo Carvalho de Melo 	}
83c25a18baSArnaldo Carvalho de Melo 
84c25a18baSArnaldo Carvalho de Melo 	/* Change state AFTER socket is unhashed to avoid closed
85c25a18baSArnaldo Carvalho de Melo 	 * socket sitting in hash tables.
86c25a18baSArnaldo Carvalho de Melo 	 */
87c25a18baSArnaldo Carvalho de Melo 	sk->sk_state = state;
88c25a18baSArnaldo Carvalho de Melo }
89c25a18baSArnaldo Carvalho de Melo 
90c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state);
91c25a18baSArnaldo Carvalho de Melo 
920c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk)
930c869620SGerrit Renker {
940c869620SGerrit Renker 	switch (sk->sk_state) {
950c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
960c869620SGerrit Renker 		/* Node (client or server) has received Close packet. */
970c869620SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
980c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
990c869620SGerrit Renker 		break;
1000c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
1010c869620SGerrit Renker 		/*
1020c869620SGerrit Renker 		 * Client received CloseReq. We set the `active' flag so that
1030c869620SGerrit Renker 		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
1040c869620SGerrit Renker 		 */
1050c869620SGerrit Renker 		dccp_send_close(sk, 1);
1060c869620SGerrit Renker 		dccp_set_state(sk, DCCP_CLOSING);
1070c869620SGerrit Renker 	}
1080c869620SGerrit Renker }
1090c869620SGerrit Renker 
110c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk)
111c25a18baSArnaldo Carvalho de Melo {
112c25a18baSArnaldo Carvalho de Melo 	dccp_set_state(sk, DCCP_CLOSED);
113c25a18baSArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
114c25a18baSArnaldo Carvalho de Melo 
115c25a18baSArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
116c25a18baSArnaldo Carvalho de Melo 
117c25a18baSArnaldo Carvalho de Melo 	if (!sock_flag(sk, SOCK_DEAD))
118c25a18baSArnaldo Carvalho de Melo 		sk->sk_state_change(sk);
119c25a18baSArnaldo Carvalho de Melo 	else
120c25a18baSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
121c25a18baSArnaldo Carvalho de Melo }
122c25a18baSArnaldo Carvalho de Melo 
123c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done);
124c25a18baSArnaldo Carvalho de Melo 
1257c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type)
1267c657876SArnaldo Carvalho de Melo {
12736cbd3dcSJan Engelhardt 	static const char *const dccp_packet_names[] = {
1287c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_REQUEST]  = "REQUEST",
1297c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESPONSE] = "RESPONSE",
1307c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATA]	    = "DATA",
1317c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_ACK]	    = "ACK",
1327c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_DATAACK]  = "DATAACK",
1337c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
1347c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_CLOSE]    = "CLOSE",
1357c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_RESET]    = "RESET",
1367c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNC]	    = "SYNC",
1377c657876SArnaldo Carvalho de Melo 		[DCCP_PKT_SYNCACK]  = "SYNCACK",
1387c657876SArnaldo Carvalho de Melo 	};
1397c657876SArnaldo Carvalho de Melo 
1407c657876SArnaldo Carvalho de Melo 	if (type >= DCCP_NR_PKT_TYPES)
1417c657876SArnaldo Carvalho de Melo 		return "INVALID";
1427c657876SArnaldo Carvalho de Melo 	else
1437c657876SArnaldo Carvalho de Melo 		return dccp_packet_names[type];
1447c657876SArnaldo Carvalho de Melo }
1457c657876SArnaldo Carvalho de Melo 
1467c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name);
1477c657876SArnaldo Carvalho de Melo 
1487c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state)
1497c657876SArnaldo Carvalho de Melo {
15036cbd3dcSJan Engelhardt 	static const char *const dccp_state_names[] = {
1517c657876SArnaldo Carvalho de Melo 	[DCCP_OPEN]		= "OPEN",
1527c657876SArnaldo Carvalho de Melo 	[DCCP_REQUESTING]	= "REQUESTING",
1537c657876SArnaldo Carvalho de Melo 	[DCCP_PARTOPEN]		= "PARTOPEN",
1547c657876SArnaldo Carvalho de Melo 	[DCCP_LISTEN]		= "LISTEN",
1557c657876SArnaldo Carvalho de Melo 	[DCCP_RESPOND]		= "RESPOND",
1567c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSING]		= "CLOSING",
157f11135a3SGerrit Renker 	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
158f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
159f11135a3SGerrit Renker 	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
1607c657876SArnaldo Carvalho de Melo 	[DCCP_TIME_WAIT]	= "TIME_WAIT",
1617c657876SArnaldo Carvalho de Melo 	[DCCP_CLOSED]		= "CLOSED",
1627c657876SArnaldo Carvalho de Melo 	};
1637c657876SArnaldo Carvalho de Melo 
1647c657876SArnaldo Carvalho de Melo 	if (state >= DCCP_MAX_STATES)
1657c657876SArnaldo Carvalho de Melo 		return "INVALID STATE!";
1667c657876SArnaldo Carvalho de Melo 	else
1677c657876SArnaldo Carvalho de Melo 		return dccp_state_names[state];
1687c657876SArnaldo Carvalho de Melo }
1697c657876SArnaldo Carvalho de Melo 
1707c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name);
1717c657876SArnaldo Carvalho de Melo 
17272478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
1733e0fadc5SArnaldo Carvalho de Melo {
1743e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
1753e0fadc5SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1763e0fadc5SArnaldo Carvalho de Melo 
177e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
178e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
179e18d7a98SArnaldo Carvalho de Melo 	sk->sk_state		= DCCP_CLOSED;
180e18d7a98SArnaldo Carvalho de Melo 	sk->sk_write_space	= dccp_write_space;
181e18d7a98SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss	= dccp_sync_mss;
182410e27a4SGerrit Renker 	dp->dccps_mss_cache	= 536;
183e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_rate_last	= jiffies;
184e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
185e18d7a98SArnaldo Carvalho de Melo 	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
186410e27a4SGerrit Renker 	dp->dccps_l_ack_ratio	= dp->dccps_r_ack_ratio = 1;
187e18d7a98SArnaldo Carvalho de Melo 
188e18d7a98SArnaldo Carvalho de Melo 	dccp_init_xmit_timers(sk);
189e18d7a98SArnaldo Carvalho de Melo 
190ac75773cSGerrit Renker 	INIT_LIST_HEAD(&dp->dccps_featneg);
1913e0fadc5SArnaldo Carvalho de Melo 	/* control socket doesn't need feat nego */
1926eb55d17SGerrit Renker 	if (likely(ctl_sock_initialized))
1936eb55d17SGerrit Renker 		return dccp_feat_init(sk);
1943e0fadc5SArnaldo Carvalho de Melo 	return 0;
1953e0fadc5SArnaldo Carvalho de Melo }
1963e0fadc5SArnaldo Carvalho de Melo 
1973e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock);
1983e0fadc5SArnaldo Carvalho de Melo 
1997d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk)
2003e0fadc5SArnaldo Carvalho de Melo {
2013e0fadc5SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
2023e0fadc5SArnaldo Carvalho de Melo 
2033e0fadc5SArnaldo Carvalho de Melo 	/*
2043e0fadc5SArnaldo Carvalho de Melo 	 * DCCP doesn't use sk_write_queue, just sk_send_head
2053e0fadc5SArnaldo Carvalho de Melo 	 * for retransmissions
2063e0fadc5SArnaldo Carvalho de Melo 	 */
2073e0fadc5SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
2083e0fadc5SArnaldo Carvalho de Melo 		kfree_skb(sk->sk_send_head);
2093e0fadc5SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
2103e0fadc5SArnaldo Carvalho de Melo 	}
2113e0fadc5SArnaldo Carvalho de Melo 
2123e0fadc5SArnaldo Carvalho de Melo 	/* Clean up a referenced DCCP bind bucket. */
2133e0fadc5SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_bind_hash != NULL)
214ab1e0a13SArnaldo Carvalho de Melo 		inet_put_port(sk);
2153e0fadc5SArnaldo Carvalho de Melo 
2163e0fadc5SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
2173e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_service_list = NULL;
2183e0fadc5SArnaldo Carvalho de Melo 
2196fdd34d4SGerrit Renker 	if (dp->dccps_hc_rx_ackvec != NULL) {
2203e0fadc5SArnaldo Carvalho de Melo 		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
2213e0fadc5SArnaldo Carvalho de Melo 		dp->dccps_hc_rx_ackvec = NULL;
2223e0fadc5SArnaldo Carvalho de Melo 	}
2233e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
2243e0fadc5SArnaldo Carvalho de Melo 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
2253e0fadc5SArnaldo Carvalho de Melo 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
2263e0fadc5SArnaldo Carvalho de Melo 
2273e0fadc5SArnaldo Carvalho de Melo 	/* clean up feature negotiation state */
228d99a7bd2SGerrit Renker 	dccp_feat_list_purge(&dp->dccps_featneg);
2293e0fadc5SArnaldo Carvalho de Melo }
2303e0fadc5SArnaldo Carvalho de Melo 
2313e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock);
2323e0fadc5SArnaldo Carvalho de Melo 
23372a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog)
2347c657876SArnaldo Carvalho de Melo {
23567e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
23667e6b629SArnaldo Carvalho de Melo 
23767e6b629SArnaldo Carvalho de Melo 	dp->dccps_role = DCCP_ROLE_LISTEN;
2389eca0a47SGerrit Renker 	/* do not start to listen if feature negotiation setup fails */
2399eca0a47SGerrit Renker 	if (dccp_feat_finalise_settings(dp))
2409eca0a47SGerrit Renker 		return -EPROTO;
24172a3effaSEric Dumazet 	return inet_csk_listen_start(sk, backlog);
2427c657876SArnaldo Carvalho de Melo }
2437c657876SArnaldo Carvalho de Melo 
244ce865a61SGerrit Renker static inline int dccp_need_reset(int state)
245ce865a61SGerrit Renker {
246ce865a61SGerrit Renker 	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
247ce865a61SGerrit Renker 	       state != DCCP_REQUESTING;
248ce865a61SGerrit Renker }
249ce865a61SGerrit Renker 
2507c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags)
2517c657876SArnaldo Carvalho de Melo {
2527c657876SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
2537c657876SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
2547c657876SArnaldo Carvalho de Melo 	int err = 0;
2557c657876SArnaldo Carvalho de Melo 	const int old_state = sk->sk_state;
2567c657876SArnaldo Carvalho de Melo 
2577c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_CLOSED)
2587c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
2597c657876SArnaldo Carvalho de Melo 
260ce865a61SGerrit Renker 	/*
261ce865a61SGerrit Renker 	 * This corresponds to the ABORT function of RFC793, sec. 3.8
262ce865a61SGerrit Renker 	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
263ce865a61SGerrit Renker 	 */
2647c657876SArnaldo Carvalho de Melo 	if (old_state == DCCP_LISTEN) {
2657c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
266ce865a61SGerrit Renker 	} else if (dccp_need_reset(old_state)) {
267ce865a61SGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
268ce865a61SGerrit Renker 		sk->sk_err = ECONNRESET;
2697c657876SArnaldo Carvalho de Melo 	} else if (old_state == DCCP_REQUESTING)
2707c657876SArnaldo Carvalho de Melo 		sk->sk_err = ECONNRESET;
2717c657876SArnaldo Carvalho de Melo 
2727c657876SArnaldo Carvalho de Melo 	dccp_clear_xmit_timers(sk);
27348816322SGerrit Renker 
2747c657876SArnaldo Carvalho de Melo 	__skb_queue_purge(&sk->sk_receive_queue);
27548816322SGerrit Renker 	__skb_queue_purge(&sk->sk_write_queue);
2767c657876SArnaldo Carvalho de Melo 	if (sk->sk_send_head != NULL) {
2777c657876SArnaldo Carvalho de Melo 		__kfree_skb(sk->sk_send_head);
2787c657876SArnaldo Carvalho de Melo 		sk->sk_send_head = NULL;
2797c657876SArnaldo Carvalho de Melo 	}
2807c657876SArnaldo Carvalho de Melo 
281c720c7e8SEric Dumazet 	inet->inet_dport = 0;
2827c657876SArnaldo Carvalho de Melo 
2837c657876SArnaldo Carvalho de Melo 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
2847c657876SArnaldo Carvalho de Melo 		inet_reset_saddr(sk);
2857c657876SArnaldo Carvalho de Melo 
2867c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = 0;
2877c657876SArnaldo Carvalho de Melo 	sock_reset_flag(sk, SOCK_DONE);
2887c657876SArnaldo Carvalho de Melo 
2897c657876SArnaldo Carvalho de Melo 	icsk->icsk_backoff = 0;
2907c657876SArnaldo Carvalho de Melo 	inet_csk_delack_init(sk);
2917c657876SArnaldo Carvalho de Melo 	__sk_dst_reset(sk);
2927c657876SArnaldo Carvalho de Melo 
293c720c7e8SEric Dumazet 	WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
2947c657876SArnaldo Carvalho de Melo 
2957c657876SArnaldo Carvalho de Melo 	sk->sk_error_report(sk);
2967c657876SArnaldo Carvalho de Melo 	return err;
2977c657876SArnaldo Carvalho de Melo }
2987c657876SArnaldo Carvalho de Melo 
299f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect);
300f21e68caSArnaldo Carvalho de Melo 
301331968bdSArnaldo Carvalho de Melo /*
302331968bdSArnaldo Carvalho de Melo  *	Wait for a DCCP event.
303331968bdSArnaldo Carvalho de Melo  *
304331968bdSArnaldo Carvalho de Melo  *	Note that we don't need to lock the socket, as the upper poll layers
305331968bdSArnaldo Carvalho de Melo  *	take care of normal races (between the test and the event) and we don't
306331968bdSArnaldo Carvalho de Melo  *	go look at any of the socket buffers directly.
307331968bdSArnaldo Carvalho de Melo  */
308f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock,
309331968bdSArnaldo Carvalho de Melo 		       poll_table *wait)
310331968bdSArnaldo Carvalho de Melo {
311331968bdSArnaldo Carvalho de Melo 	unsigned int mask;
312331968bdSArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
313331968bdSArnaldo Carvalho de Melo 
314a57de0b4SJiri Olsa 	sock_poll_wait(file, sk->sk_sleep, wait);
315331968bdSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
316331968bdSArnaldo Carvalho de Melo 		return inet_csk_listen_poll(sk);
317331968bdSArnaldo Carvalho de Melo 
318331968bdSArnaldo Carvalho de Melo 	/* Socket is not locked. We are protected from async events
319331968bdSArnaldo Carvalho de Melo 	   by poll logic and correct handling of state changes
320331968bdSArnaldo Carvalho de Melo 	   made by another threads is impossible in any case.
321331968bdSArnaldo Carvalho de Melo 	 */
322331968bdSArnaldo Carvalho de Melo 
323331968bdSArnaldo Carvalho de Melo 	mask = 0;
324331968bdSArnaldo Carvalho de Melo 	if (sk->sk_err)
325331968bdSArnaldo Carvalho de Melo 		mask = POLLERR;
326331968bdSArnaldo Carvalho de Melo 
327331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
328331968bdSArnaldo Carvalho de Melo 		mask |= POLLHUP;
329331968bdSArnaldo Carvalho de Melo 	if (sk->sk_shutdown & RCV_SHUTDOWN)
330f348d70aSDavide Libenzi 		mask |= POLLIN | POLLRDNORM | POLLRDHUP;
331331968bdSArnaldo Carvalho de Melo 
332331968bdSArnaldo Carvalho de Melo 	/* Connected? */
333331968bdSArnaldo Carvalho de Melo 	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
334331968bdSArnaldo Carvalho de Melo 		if (atomic_read(&sk->sk_rmem_alloc) > 0)
335331968bdSArnaldo Carvalho de Melo 			mask |= POLLIN | POLLRDNORM;
336331968bdSArnaldo Carvalho de Melo 
337331968bdSArnaldo Carvalho de Melo 		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
338331968bdSArnaldo Carvalho de Melo 			if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
339331968bdSArnaldo Carvalho de Melo 				mask |= POLLOUT | POLLWRNORM;
340331968bdSArnaldo Carvalho de Melo 			} else {  /* send SIGIO later */
341331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_ASYNC_NOSPACE,
342331968bdSArnaldo Carvalho de Melo 					&sk->sk_socket->flags);
343331968bdSArnaldo Carvalho de Melo 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
344331968bdSArnaldo Carvalho de Melo 
345331968bdSArnaldo Carvalho de Melo 				/* Race breaker. If space is freed after
346331968bdSArnaldo Carvalho de Melo 				 * wspace test but before the flags are set,
347331968bdSArnaldo Carvalho de Melo 				 * IO signal will be lost.
348331968bdSArnaldo Carvalho de Melo 				 */
349331968bdSArnaldo Carvalho de Melo 				if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
350331968bdSArnaldo Carvalho de Melo 					mask |= POLLOUT | POLLWRNORM;
351331968bdSArnaldo Carvalho de Melo 			}
352331968bdSArnaldo Carvalho de Melo 		}
353331968bdSArnaldo Carvalho de Melo 	}
354331968bdSArnaldo Carvalho de Melo 	return mask;
355331968bdSArnaldo Carvalho de Melo }
356331968bdSArnaldo Carvalho de Melo 
357f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll);
358f21e68caSArnaldo Carvalho de Melo 
3597c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3607c657876SArnaldo Carvalho de Melo {
3616273172eSArnaldo Carvalho de Melo 	int rc = -ENOTCONN;
3626273172eSArnaldo Carvalho de Melo 
3636273172eSArnaldo Carvalho de Melo 	lock_sock(sk);
3646273172eSArnaldo Carvalho de Melo 
3656273172eSArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN)
3666273172eSArnaldo Carvalho de Melo 		goto out;
3676273172eSArnaldo Carvalho de Melo 
3686273172eSArnaldo Carvalho de Melo 	switch (cmd) {
3696273172eSArnaldo Carvalho de Melo 	case SIOCINQ: {
3706273172eSArnaldo Carvalho de Melo 		struct sk_buff *skb;
3716273172eSArnaldo Carvalho de Melo 		unsigned long amount = 0;
3726273172eSArnaldo Carvalho de Melo 
3736273172eSArnaldo Carvalho de Melo 		skb = skb_peek(&sk->sk_receive_queue);
3746273172eSArnaldo Carvalho de Melo 		if (skb != NULL) {
3756273172eSArnaldo Carvalho de Melo 			/*
3766273172eSArnaldo Carvalho de Melo 			 * We will only return the amount of this packet since
3776273172eSArnaldo Carvalho de Melo 			 * that is all that will be read.
3786273172eSArnaldo Carvalho de Melo 			 */
3796273172eSArnaldo Carvalho de Melo 			amount = skb->len;
3806273172eSArnaldo Carvalho de Melo 		}
3816273172eSArnaldo Carvalho de Melo 		rc = put_user(amount, (int __user *)arg);
3826273172eSArnaldo Carvalho de Melo 	}
3836273172eSArnaldo Carvalho de Melo 		break;
3846273172eSArnaldo Carvalho de Melo 	default:
3856273172eSArnaldo Carvalho de Melo 		rc = -ENOIOCTLCMD;
3866273172eSArnaldo Carvalho de Melo 		break;
3876273172eSArnaldo Carvalho de Melo 	}
3886273172eSArnaldo Carvalho de Melo out:
3896273172eSArnaldo Carvalho de Melo 	release_sock(sk);
3906273172eSArnaldo Carvalho de Melo 	return rc;
3917c657876SArnaldo Carvalho de Melo }
3927c657876SArnaldo Carvalho de Melo 
393f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl);
394f21e68caSArnaldo Carvalho de Melo 
39560fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
396b7058842SDavid S. Miller 				   char __user *optval, unsigned int optlen)
39767e6b629SArnaldo Carvalho de Melo {
39867e6b629SArnaldo Carvalho de Melo 	struct dccp_sock *dp = dccp_sk(sk);
39967e6b629SArnaldo Carvalho de Melo 	struct dccp_service_list *sl = NULL;
40067e6b629SArnaldo Carvalho de Melo 
40167e6b629SArnaldo Carvalho de Melo 	if (service == DCCP_SERVICE_INVALID_VALUE ||
40267e6b629SArnaldo Carvalho de Melo 	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
40367e6b629SArnaldo Carvalho de Melo 		return -EINVAL;
40467e6b629SArnaldo Carvalho de Melo 
40567e6b629SArnaldo Carvalho de Melo 	if (optlen > sizeof(service)) {
40667e6b629SArnaldo Carvalho de Melo 		sl = kmalloc(optlen, GFP_KERNEL);
40767e6b629SArnaldo Carvalho de Melo 		if (sl == NULL)
40867e6b629SArnaldo Carvalho de Melo 			return -ENOMEM;
40967e6b629SArnaldo Carvalho de Melo 
41067e6b629SArnaldo Carvalho de Melo 		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
41167e6b629SArnaldo Carvalho de Melo 		if (copy_from_user(sl->dccpsl_list,
41267e6b629SArnaldo Carvalho de Melo 				   optval + sizeof(service),
41367e6b629SArnaldo Carvalho de Melo 				   optlen - sizeof(service)) ||
41467e6b629SArnaldo Carvalho de Melo 		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
41567e6b629SArnaldo Carvalho de Melo 			kfree(sl);
41667e6b629SArnaldo Carvalho de Melo 			return -EFAULT;
41767e6b629SArnaldo Carvalho de Melo 		}
41867e6b629SArnaldo Carvalho de Melo 	}
41967e6b629SArnaldo Carvalho de Melo 
42067e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
42167e6b629SArnaldo Carvalho de Melo 	dp->dccps_service = service;
42267e6b629SArnaldo Carvalho de Melo 
42367e6b629SArnaldo Carvalho de Melo 	kfree(dp->dccps_service_list);
42467e6b629SArnaldo Carvalho de Melo 
42567e6b629SArnaldo Carvalho de Melo 	dp->dccps_service_list = sl;
42667e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
42767e6b629SArnaldo Carvalho de Melo 	return 0;
42867e6b629SArnaldo Carvalho de Melo }
42967e6b629SArnaldo Carvalho de Melo 
43029450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
43129450559SGerrit Renker {
43229450559SGerrit Renker 	u8 *list, len;
43329450559SGerrit Renker 	int i, rc;
43429450559SGerrit Renker 
43529450559SGerrit Renker 	if (cscov < 0 || cscov > 15)
43629450559SGerrit Renker 		return -EINVAL;
43729450559SGerrit Renker 	/*
43829450559SGerrit Renker 	 * Populate a list of permissible values, in the range cscov...15. This
43929450559SGerrit Renker 	 * is necessary since feature negotiation of single values only works if
44029450559SGerrit Renker 	 * both sides incidentally choose the same value. Since the list starts
44129450559SGerrit Renker 	 * lowest-value first, negotiation will pick the smallest shared value.
44229450559SGerrit Renker 	 */
44329450559SGerrit Renker 	if (cscov == 0)
44429450559SGerrit Renker 		return 0;
44529450559SGerrit Renker 	len = 16 - cscov;
44629450559SGerrit Renker 
44729450559SGerrit Renker 	list = kmalloc(len, GFP_KERNEL);
44829450559SGerrit Renker 	if (list == NULL)
44929450559SGerrit Renker 		return -ENOBUFS;
45029450559SGerrit Renker 
45129450559SGerrit Renker 	for (i = 0; i < len; i++)
45229450559SGerrit Renker 		list[i] = cscov++;
45329450559SGerrit Renker 
45429450559SGerrit Renker 	rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
45529450559SGerrit Renker 
45629450559SGerrit Renker 	if (rc == 0) {
45729450559SGerrit Renker 		if (rx)
45829450559SGerrit Renker 			dccp_sk(sk)->dccps_pcrlen = cscov;
45929450559SGerrit Renker 		else
46029450559SGerrit Renker 			dccp_sk(sk)->dccps_pcslen = cscov;
46129450559SGerrit Renker 	}
46229450559SGerrit Renker 	kfree(list);
46329450559SGerrit Renker 	return rc;
46429450559SGerrit Renker }
46529450559SGerrit Renker 
466b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type,
467b7058842SDavid S. Miller 				char __user *optval, unsigned int optlen)
468b20a9c24SGerrit Renker {
469b20a9c24SGerrit Renker 	u8 *val;
470b20a9c24SGerrit Renker 	int rc = 0;
471b20a9c24SGerrit Renker 
472b20a9c24SGerrit Renker 	if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
473b20a9c24SGerrit Renker 		return -EINVAL;
474b20a9c24SGerrit Renker 
475b20a9c24SGerrit Renker 	val = kmalloc(optlen, GFP_KERNEL);
476b20a9c24SGerrit Renker 	if (val == NULL)
477b20a9c24SGerrit Renker 		return -ENOMEM;
478b20a9c24SGerrit Renker 
479b20a9c24SGerrit Renker 	if (copy_from_user(val, optval, optlen)) {
480b20a9c24SGerrit Renker 		kfree(val);
481b20a9c24SGerrit Renker 		return -EFAULT;
482b20a9c24SGerrit Renker 	}
483b20a9c24SGerrit Renker 
484b20a9c24SGerrit Renker 	lock_sock(sk);
485b20a9c24SGerrit Renker 	if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
486b20a9c24SGerrit Renker 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
487b20a9c24SGerrit Renker 
488b20a9c24SGerrit Renker 	if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
489b20a9c24SGerrit Renker 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
490b20a9c24SGerrit Renker 	release_sock(sk);
491b20a9c24SGerrit Renker 
492b20a9c24SGerrit Renker 	kfree(val);
493b20a9c24SGerrit Renker 	return rc;
494b20a9c24SGerrit Renker }
495b20a9c24SGerrit Renker 
4963fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
497b7058842SDavid S. Miller 		char __user *optval, unsigned int optlen)
4987c657876SArnaldo Carvalho de Melo {
49909dbc389SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
50009dbc389SGerrit Renker 	int val, err = 0;
5017c657876SArnaldo Carvalho de Melo 
50219102996SGerrit Renker 	switch (optname) {
50319102996SGerrit Renker 	case DCCP_SOCKOPT_PACKET_SIZE:
50419102996SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
50519102996SGerrit Renker 		return 0;
50619102996SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_L:
50719102996SGerrit Renker 	case DCCP_SOCKOPT_CHANGE_R:
50819102996SGerrit Renker 		DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
50919102996SGerrit Renker 		return 0;
510b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_CCID:
511b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_RX_CCID:
512b20a9c24SGerrit Renker 	case DCCP_SOCKOPT_TX_CCID:
513b20a9c24SGerrit Renker 		return dccp_setsockopt_ccid(sk, optname, optval, optlen);
51419102996SGerrit Renker 	}
51519102996SGerrit Renker 
51619102996SGerrit Renker 	if (optlen < (int)sizeof(int))
517a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
518a84ffe43SArnaldo Carvalho de Melo 
519a84ffe43SArnaldo Carvalho de Melo 	if (get_user(val, (int __user *)optval))
520a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
521a84ffe43SArnaldo Carvalho de Melo 
52267e6b629SArnaldo Carvalho de Melo 	if (optname == DCCP_SOCKOPT_SERVICE)
52367e6b629SArnaldo Carvalho de Melo 		return dccp_setsockopt_service(sk, val, optval, optlen);
524a84ffe43SArnaldo Carvalho de Melo 
52567e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
526a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
527b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
528b8599d20SGerrit Renker 		if (dp->dccps_role != DCCP_ROLE_SERVER)
529b8599d20SGerrit Renker 			err = -EOPNOTSUPP;
530b8599d20SGerrit Renker 		else
531b8599d20SGerrit Renker 			dp->dccps_server_timewait = (val != 0);
532b8599d20SGerrit Renker 		break;
53329450559SGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
53429450559SGerrit Renker 		err = dccp_setsockopt_cscov(sk, val, false);
535d6da3511STomasz Grobelny 		break;
53629450559SGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
53729450559SGerrit Renker 		err = dccp_setsockopt_cscov(sk, val, true);
538d6da3511STomasz Grobelny 		break;
539a84ffe43SArnaldo Carvalho de Melo 	default:
540a84ffe43SArnaldo Carvalho de Melo 		err = -ENOPROTOOPT;
541a84ffe43SArnaldo Carvalho de Melo 		break;
542a84ffe43SArnaldo Carvalho de Melo 	}
543410e27a4SGerrit Renker 	release_sock(sk);
54419102996SGerrit Renker 
545a84ffe43SArnaldo Carvalho de Melo 	return err;
5467c657876SArnaldo Carvalho de Melo }
5477c657876SArnaldo Carvalho de Melo 
5483fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname,
549b7058842SDavid S. Miller 		    char __user *optval, unsigned int optlen)
5503fdadf7dSDmitry Mishin {
5513fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
5523fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
5533fdadf7dSDmitry Mishin 							     optname, optval,
5543fdadf7dSDmitry Mishin 							     optlen);
5553fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5563fdadf7dSDmitry Mishin }
557543d9cfeSArnaldo Carvalho de Melo 
558f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt);
559f21e68caSArnaldo Carvalho de Melo 
5603fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5613fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
562b7058842SDavid S. Miller 			   char __user *optval, unsigned int optlen)
5633fdadf7dSDmitry Mishin {
564dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
565dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_setsockopt(sk, level, optname,
566dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
5673fdadf7dSDmitry Mishin 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
5683fdadf7dSDmitry Mishin }
569543d9cfeSArnaldo Carvalho de Melo 
5703fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
5713fdadf7dSDmitry Mishin #endif
5723fdadf7dSDmitry Mishin 
57367e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len,
57460fe62e7SAndrea Bittau 				   __be32 __user *optval,
57567e6b629SArnaldo Carvalho de Melo 				   int __user *optlen)
57667e6b629SArnaldo Carvalho de Melo {
57767e6b629SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
57867e6b629SArnaldo Carvalho de Melo 	const struct dccp_service_list *sl;
57967e6b629SArnaldo Carvalho de Melo 	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
58067e6b629SArnaldo Carvalho de Melo 
58167e6b629SArnaldo Carvalho de Melo 	lock_sock(sk);
58267e6b629SArnaldo Carvalho de Melo 	if ((sl = dp->dccps_service_list) != NULL) {
58367e6b629SArnaldo Carvalho de Melo 		slen = sl->dccpsl_nr * sizeof(u32);
58467e6b629SArnaldo Carvalho de Melo 		total_len += slen;
58567e6b629SArnaldo Carvalho de Melo 	}
58667e6b629SArnaldo Carvalho de Melo 
58767e6b629SArnaldo Carvalho de Melo 	err = -EINVAL;
58867e6b629SArnaldo Carvalho de Melo 	if (total_len > len)
58967e6b629SArnaldo Carvalho de Melo 		goto out;
59067e6b629SArnaldo Carvalho de Melo 
59167e6b629SArnaldo Carvalho de Melo 	err = 0;
59267e6b629SArnaldo Carvalho de Melo 	if (put_user(total_len, optlen) ||
59367e6b629SArnaldo Carvalho de Melo 	    put_user(dp->dccps_service, optval) ||
59467e6b629SArnaldo Carvalho de Melo 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
59567e6b629SArnaldo Carvalho de Melo 		err = -EFAULT;
59667e6b629SArnaldo Carvalho de Melo out:
59767e6b629SArnaldo Carvalho de Melo 	release_sock(sk);
59867e6b629SArnaldo Carvalho de Melo 	return err;
59967e6b629SArnaldo Carvalho de Melo }
60067e6b629SArnaldo Carvalho de Melo 
6013fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
602a1d3a355SArnaldo Carvalho de Melo 		    char __user *optval, int __user *optlen)
6037c657876SArnaldo Carvalho de Melo {
604a84ffe43SArnaldo Carvalho de Melo 	struct dccp_sock *dp;
605a84ffe43SArnaldo Carvalho de Melo 	int val, len;
6067c657876SArnaldo Carvalho de Melo 
607a84ffe43SArnaldo Carvalho de Melo 	if (get_user(len, optlen))
608a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
609a84ffe43SArnaldo Carvalho de Melo 
61039ebc027SArnaldo Carvalho de Melo 	if (len < (int)sizeof(int))
611a84ffe43SArnaldo Carvalho de Melo 		return -EINVAL;
612a84ffe43SArnaldo Carvalho de Melo 
613a84ffe43SArnaldo Carvalho de Melo 	dp = dccp_sk(sk);
614a84ffe43SArnaldo Carvalho de Melo 
615a84ffe43SArnaldo Carvalho de Melo 	switch (optname) {
616a84ffe43SArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_PACKET_SIZE:
6175aed3243SGerrit Renker 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
618841bac1dSArnaldo Carvalho de Melo 		return 0;
61988f964dbSArnaldo Carvalho de Melo 	case DCCP_SOCKOPT_SERVICE:
62088f964dbSArnaldo Carvalho de Melo 		return dccp_getsockopt_service(sk, len,
62160fe62e7SAndrea Bittau 					       (__be32 __user *)optval, optlen);
6227c559a9eSGerrit Renker 	case DCCP_SOCKOPT_GET_CUR_MPS:
6237c559a9eSGerrit Renker 		val = dp->dccps_mss_cache;
6247c559a9eSGerrit Renker 		break;
625d90ebcbfSGerrit Renker 	case DCCP_SOCKOPT_AVAILABLE_CCIDS:
626d90ebcbfSGerrit Renker 		return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
62771c262a3SGerrit Renker 	case DCCP_SOCKOPT_TX_CCID:
62871c262a3SGerrit Renker 		val = ccid_get_current_tx_ccid(dp);
62971c262a3SGerrit Renker 		if (val < 0)
63071c262a3SGerrit Renker 			return -ENOPROTOOPT;
63171c262a3SGerrit Renker 		break;
63271c262a3SGerrit Renker 	case DCCP_SOCKOPT_RX_CCID:
63371c262a3SGerrit Renker 		val = ccid_get_current_rx_ccid(dp);
63471c262a3SGerrit Renker 		if (val < 0)
63571c262a3SGerrit Renker 			return -ENOPROTOOPT;
63671c262a3SGerrit Renker 		break;
637b8599d20SGerrit Renker 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
638b8599d20SGerrit Renker 		val = dp->dccps_server_timewait;
639b8599d20SGerrit Renker 		break;
6406f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_SEND_CSCOV:
6416f4e5fffSGerrit Renker 		val = dp->dccps_pcslen;
6426f4e5fffSGerrit Renker 		break;
6436f4e5fffSGerrit Renker 	case DCCP_SOCKOPT_RECV_CSCOV:
6446f4e5fffSGerrit Renker 		val = dp->dccps_pcrlen;
6456f4e5fffSGerrit Renker 		break;
64688f964dbSArnaldo Carvalho de Melo 	case 128 ... 191:
64788f964dbSArnaldo Carvalho de Melo 		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
64888f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
64988f964dbSArnaldo Carvalho de Melo 	case 192 ... 255:
65088f964dbSArnaldo Carvalho de Melo 		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
65188f964dbSArnaldo Carvalho de Melo 					     len, (u32 __user *)optval, optlen);
652a84ffe43SArnaldo Carvalho de Melo 	default:
653a84ffe43SArnaldo Carvalho de Melo 		return -ENOPROTOOPT;
654a84ffe43SArnaldo Carvalho de Melo 	}
655a84ffe43SArnaldo Carvalho de Melo 
65679133506SGerrit Renker 	len = sizeof(val);
657a84ffe43SArnaldo Carvalho de Melo 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
658a84ffe43SArnaldo Carvalho de Melo 		return -EFAULT;
659a84ffe43SArnaldo Carvalho de Melo 
660a84ffe43SArnaldo Carvalho de Melo 	return 0;
6617c657876SArnaldo Carvalho de Melo }
6627c657876SArnaldo Carvalho de Melo 
6633fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname,
6643fdadf7dSDmitry Mishin 		    char __user *optval, int __user *optlen)
6653fdadf7dSDmitry Mishin {
6663fdadf7dSDmitry Mishin 	if (level != SOL_DCCP)
6673fdadf7dSDmitry Mishin 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
6683fdadf7dSDmitry Mishin 							     optname, optval,
6693fdadf7dSDmitry Mishin 							     optlen);
6703fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
6713fdadf7dSDmitry Mishin }
672543d9cfeSArnaldo Carvalho de Melo 
673f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt);
674f21e68caSArnaldo Carvalho de Melo 
6753fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
6763fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
6773fdadf7dSDmitry Mishin 			   char __user *optval, int __user *optlen)
6783fdadf7dSDmitry Mishin {
679dec73ff0SArnaldo Carvalho de Melo 	if (level != SOL_DCCP)
680dec73ff0SArnaldo Carvalho de Melo 		return inet_csk_compat_getsockopt(sk, level, optname,
681dec73ff0SArnaldo Carvalho de Melo 						  optval, optlen);
6823fdadf7dSDmitry Mishin 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
6833fdadf7dSDmitry Mishin }
684543d9cfeSArnaldo Carvalho de Melo 
6853fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
6863fdadf7dSDmitry Mishin #endif
6873fdadf7dSDmitry Mishin 
6887c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
6897c657876SArnaldo Carvalho de Melo 		 size_t len)
6907c657876SArnaldo Carvalho de Melo {
6917c657876SArnaldo Carvalho de Melo 	const struct dccp_sock *dp = dccp_sk(sk);
6927c657876SArnaldo Carvalho de Melo 	const int flags = msg->msg_flags;
6937c657876SArnaldo Carvalho de Melo 	const int noblock = flags & MSG_DONTWAIT;
6947c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
6957c657876SArnaldo Carvalho de Melo 	int rc, size;
6967c657876SArnaldo Carvalho de Melo 	long timeo;
6977c657876SArnaldo Carvalho de Melo 
6987c657876SArnaldo Carvalho de Melo 	if (len > dp->dccps_mss_cache)
6997c657876SArnaldo Carvalho de Melo 		return -EMSGSIZE;
7007c657876SArnaldo Carvalho de Melo 
7017c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
702b1308dc0SIan McDonald 
703410e27a4SGerrit Renker 	if (sysctl_dccp_tx_qlen &&
704410e27a4SGerrit Renker 	    (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
705b1308dc0SIan McDonald 		rc = -EAGAIN;
706b1308dc0SIan McDonald 		goto out_release;
707b1308dc0SIan McDonald 	}
708b1308dc0SIan McDonald 
70927258ee5SArnaldo Carvalho de Melo 	timeo = sock_sndtimeo(sk, noblock);
7107c657876SArnaldo Carvalho de Melo 
7117c657876SArnaldo Carvalho de Melo 	/*
7127c657876SArnaldo Carvalho de Melo 	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
7137c657876SArnaldo Carvalho de Melo 	 * so that the trick in dccp_rcv_request_sent_state_process.
7147c657876SArnaldo Carvalho de Melo 	 */
7157c657876SArnaldo Carvalho de Melo 	/* Wait for a connection to finish. */
716cecd8d0eSGerrit Renker 	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7177c657876SArnaldo Carvalho de Melo 		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
71827258ee5SArnaldo Carvalho de Melo 			goto out_release;
7197c657876SArnaldo Carvalho de Melo 
7207c657876SArnaldo Carvalho de Melo 	size = sk->sk_prot->max_header + len;
7217c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7227c657876SArnaldo Carvalho de Melo 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
7237c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7247c657876SArnaldo Carvalho de Melo 	if (skb == NULL)
7257c657876SArnaldo Carvalho de Melo 		goto out_release;
7267c657876SArnaldo Carvalho de Melo 
7277c657876SArnaldo Carvalho de Melo 	skb_reserve(skb, sk->sk_prot->max_header);
7287c657876SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
72927258ee5SArnaldo Carvalho de Melo 	if (rc != 0)
73027258ee5SArnaldo Carvalho de Melo 		goto out_discard;
7317c657876SArnaldo Carvalho de Melo 
732410e27a4SGerrit Renker 	skb_queue_tail(&sk->sk_write_queue, skb);
733410e27a4SGerrit Renker 	dccp_write_xmit(sk,0);
7347c657876SArnaldo Carvalho de Melo out_release:
7357c657876SArnaldo Carvalho de Melo 	release_sock(sk);
7367c657876SArnaldo Carvalho de Melo 	return rc ? : len;
73727258ee5SArnaldo Carvalho de Melo out_discard:
73827258ee5SArnaldo Carvalho de Melo 	kfree_skb(skb);
7397c657876SArnaldo Carvalho de Melo 	goto out_release;
7407c657876SArnaldo Carvalho de Melo }
7417c657876SArnaldo Carvalho de Melo 
742f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg);
743f21e68caSArnaldo Carvalho de Melo 
7447c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7457c657876SArnaldo Carvalho de Melo 		 size_t len, int nonblock, int flags, int *addr_len)
7467c657876SArnaldo Carvalho de Melo {
7477c657876SArnaldo Carvalho de Melo 	const struct dccp_hdr *dh;
7487c657876SArnaldo Carvalho de Melo 	long timeo;
7497c657876SArnaldo Carvalho de Melo 
7507c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
7517c657876SArnaldo Carvalho de Melo 
752531669a0SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
753531669a0SArnaldo Carvalho de Melo 		len = -ENOTCONN;
7547c657876SArnaldo Carvalho de Melo 		goto out;
755531669a0SArnaldo Carvalho de Melo 	}
7567c657876SArnaldo Carvalho de Melo 
7577c657876SArnaldo Carvalho de Melo 	timeo = sock_rcvtimeo(sk, nonblock);
7587c657876SArnaldo Carvalho de Melo 
7597c657876SArnaldo Carvalho de Melo 	do {
760531669a0SArnaldo Carvalho de Melo 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
7617c657876SArnaldo Carvalho de Melo 
762531669a0SArnaldo Carvalho de Melo 		if (skb == NULL)
763531669a0SArnaldo Carvalho de Melo 			goto verify_sock_status;
7647c657876SArnaldo Carvalho de Melo 
7657c657876SArnaldo Carvalho de Melo 		dh = dccp_hdr(skb);
7667c657876SArnaldo Carvalho de Melo 
7670c869620SGerrit Renker 		switch (dh->dccph_type) {
7680c869620SGerrit Renker 		case DCCP_PKT_DATA:
7690c869620SGerrit Renker 		case DCCP_PKT_DATAACK:
7707c657876SArnaldo Carvalho de Melo 			goto found_ok_skb;
7717c657876SArnaldo Carvalho de Melo 
7720c869620SGerrit Renker 		case DCCP_PKT_CLOSE:
7730c869620SGerrit Renker 		case DCCP_PKT_CLOSEREQ:
7740c869620SGerrit Renker 			if (!(flags & MSG_PEEK))
7750c869620SGerrit Renker 				dccp_finish_passive_close(sk);
7760c869620SGerrit Renker 			/* fall through */
7770c869620SGerrit Renker 		case DCCP_PKT_RESET:
7780c869620SGerrit Renker 			dccp_pr_debug("found fin (%s) ok!\n",
7790c869620SGerrit Renker 				      dccp_packet_name(dh->dccph_type));
780531669a0SArnaldo Carvalho de Melo 			len = 0;
7817c657876SArnaldo Carvalho de Melo 			goto found_fin_ok;
7820c869620SGerrit Renker 		default:
7837690af3fSArnaldo Carvalho de Melo 			dccp_pr_debug("packet_type=%s\n",
7847690af3fSArnaldo Carvalho de Melo 				      dccp_packet_name(dh->dccph_type));
785624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
7860c869620SGerrit Renker 		}
787531669a0SArnaldo Carvalho de Melo verify_sock_status:
788531669a0SArnaldo Carvalho de Melo 		if (sock_flag(sk, SOCK_DONE)) {
789531669a0SArnaldo Carvalho de Melo 			len = 0;
7907c657876SArnaldo Carvalho de Melo 			break;
7917c657876SArnaldo Carvalho de Melo 		}
7927c657876SArnaldo Carvalho de Melo 
793531669a0SArnaldo Carvalho de Melo 		if (sk->sk_err) {
794531669a0SArnaldo Carvalho de Melo 			len = sock_error(sk);
7957c657876SArnaldo Carvalho de Melo 			break;
796531669a0SArnaldo Carvalho de Melo 		}
797531669a0SArnaldo Carvalho de Melo 
798531669a0SArnaldo Carvalho de Melo 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
799531669a0SArnaldo Carvalho de Melo 			len = 0;
800531669a0SArnaldo Carvalho de Melo 			break;
801531669a0SArnaldo Carvalho de Melo 		}
8027c657876SArnaldo Carvalho de Melo 
8037c657876SArnaldo Carvalho de Melo 		if (sk->sk_state == DCCP_CLOSED) {
8047c657876SArnaldo Carvalho de Melo 			if (!sock_flag(sk, SOCK_DONE)) {
8057c657876SArnaldo Carvalho de Melo 				/* This occurs when user tries to read
8067c657876SArnaldo Carvalho de Melo 				 * from never connected socket.
8077c657876SArnaldo Carvalho de Melo 				 */
808531669a0SArnaldo Carvalho de Melo 				len = -ENOTCONN;
8097c657876SArnaldo Carvalho de Melo 				break;
8107c657876SArnaldo Carvalho de Melo 			}
811531669a0SArnaldo Carvalho de Melo 			len = 0;
8127c657876SArnaldo Carvalho de Melo 			break;
8137c657876SArnaldo Carvalho de Melo 		}
8147c657876SArnaldo Carvalho de Melo 
8157c657876SArnaldo Carvalho de Melo 		if (!timeo) {
816531669a0SArnaldo Carvalho de Melo 			len = -EAGAIN;
8177c657876SArnaldo Carvalho de Melo 			break;
8187c657876SArnaldo Carvalho de Melo 		}
8197c657876SArnaldo Carvalho de Melo 
8207c657876SArnaldo Carvalho de Melo 		if (signal_pending(current)) {
821531669a0SArnaldo Carvalho de Melo 			len = sock_intr_errno(timeo);
8227c657876SArnaldo Carvalho de Melo 			break;
8237c657876SArnaldo Carvalho de Melo 		}
8247c657876SArnaldo Carvalho de Melo 
8257c657876SArnaldo Carvalho de Melo 		sk_wait_data(sk, &timeo);
8267c657876SArnaldo Carvalho de Melo 		continue;
8277c657876SArnaldo Carvalho de Melo 	found_ok_skb:
828531669a0SArnaldo Carvalho de Melo 		if (len > skb->len)
829531669a0SArnaldo Carvalho de Melo 			len = skb->len;
830531669a0SArnaldo Carvalho de Melo 		else if (len < skb->len)
831531669a0SArnaldo Carvalho de Melo 			msg->msg_flags |= MSG_TRUNC;
8327c657876SArnaldo Carvalho de Melo 
833531669a0SArnaldo Carvalho de Melo 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
8347c657876SArnaldo Carvalho de Melo 			/* Exception. Bailout! */
835531669a0SArnaldo Carvalho de Melo 			len = -EFAULT;
8367c657876SArnaldo Carvalho de Melo 			break;
8377c657876SArnaldo Carvalho de Melo 		}
83855d95590SGerrit Renker 		if (flags & MSG_TRUNC)
83955d95590SGerrit Renker 			len = skb->len;
8407c657876SArnaldo Carvalho de Melo 	found_fin_ok:
8417c657876SArnaldo Carvalho de Melo 		if (!(flags & MSG_PEEK))
842624d1164SChris Leech 			sk_eat_skb(sk, skb, 0);
8437c657876SArnaldo Carvalho de Melo 		break;
844531669a0SArnaldo Carvalho de Melo 	} while (1);
8457c657876SArnaldo Carvalho de Melo out:
8467c657876SArnaldo Carvalho de Melo 	release_sock(sk);
847531669a0SArnaldo Carvalho de Melo 	return len;
8487c657876SArnaldo Carvalho de Melo }
8497c657876SArnaldo Carvalho de Melo 
850f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg);
851f21e68caSArnaldo Carvalho de Melo 
852f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog)
8537c657876SArnaldo Carvalho de Melo {
8547c657876SArnaldo Carvalho de Melo 	struct sock *sk = sock->sk;
8557c657876SArnaldo Carvalho de Melo 	unsigned char old_state;
8567c657876SArnaldo Carvalho de Melo 	int err;
8577c657876SArnaldo Carvalho de Melo 
8587c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
8597c657876SArnaldo Carvalho de Melo 
8607c657876SArnaldo Carvalho de Melo 	err = -EINVAL;
8617c657876SArnaldo Carvalho de Melo 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
8627c657876SArnaldo Carvalho de Melo 		goto out;
8637c657876SArnaldo Carvalho de Melo 
8647c657876SArnaldo Carvalho de Melo 	old_state = sk->sk_state;
8657c657876SArnaldo Carvalho de Melo 	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
8667c657876SArnaldo Carvalho de Melo 		goto out;
8677c657876SArnaldo Carvalho de Melo 
8687c657876SArnaldo Carvalho de Melo 	/* Really, if the socket is already in listen state
8697c657876SArnaldo Carvalho de Melo 	 * we can only allow the backlog to be adjusted.
8707c657876SArnaldo Carvalho de Melo 	 */
8717c657876SArnaldo Carvalho de Melo 	if (old_state != DCCP_LISTEN) {
8727c657876SArnaldo Carvalho de Melo 		/*
8737c657876SArnaldo Carvalho de Melo 		 * FIXME: here it probably should be sk->sk_prot->listen_start
8747c657876SArnaldo Carvalho de Melo 		 * see tcp_listen_start
8757c657876SArnaldo Carvalho de Melo 		 */
87672a3effaSEric Dumazet 		err = dccp_listen_start(sk, backlog);
8777c657876SArnaldo Carvalho de Melo 		if (err)
8787c657876SArnaldo Carvalho de Melo 			goto out;
8797c657876SArnaldo Carvalho de Melo 	}
8807c657876SArnaldo Carvalho de Melo 	sk->sk_max_ack_backlog = backlog;
8817c657876SArnaldo Carvalho de Melo 	err = 0;
8827c657876SArnaldo Carvalho de Melo 
8837c657876SArnaldo Carvalho de Melo out:
8847c657876SArnaldo Carvalho de Melo 	release_sock(sk);
8857c657876SArnaldo Carvalho de Melo 	return err;
8867c657876SArnaldo Carvalho de Melo }
8877c657876SArnaldo Carvalho de Melo 
888f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen);
889f21e68caSArnaldo Carvalho de Melo 
8900c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk)
8917c657876SArnaldo Carvalho de Melo {
8920c869620SGerrit Renker 	u8 next_state = DCCP_CLOSED;
8937c657876SArnaldo Carvalho de Melo 
8940c869620SGerrit Renker 	switch (sk->sk_state) {
8950c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSE:
8960c869620SGerrit Renker 	case DCCP_PASSIVE_CLOSEREQ:
8970c869620SGerrit Renker 		dccp_finish_passive_close(sk);
8980c869620SGerrit Renker 		break;
8990c869620SGerrit Renker 	case DCCP_PARTOPEN:
9000c869620SGerrit Renker 		dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
9010c869620SGerrit Renker 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
9020c869620SGerrit Renker 		/* fall through */
9030c869620SGerrit Renker 	case DCCP_OPEN:
9040c869620SGerrit Renker 		dccp_send_close(sk, 1);
9057c657876SArnaldo Carvalho de Melo 
906b8599d20SGerrit Renker 		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
907b8599d20SGerrit Renker 		    !dccp_sk(sk)->dccps_server_timewait)
9080c869620SGerrit Renker 			next_state = DCCP_ACTIVE_CLOSEREQ;
9090c869620SGerrit Renker 		else
9100c869620SGerrit Renker 			next_state = DCCP_CLOSING;
9110c869620SGerrit Renker 		/* fall through */
9120c869620SGerrit Renker 	default:
9130c869620SGerrit Renker 		dccp_set_state(sk, next_state);
9140c869620SGerrit Renker 	}
9157c657876SArnaldo Carvalho de Melo }
9167c657876SArnaldo Carvalho de Melo 
9177c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout)
9187c657876SArnaldo Carvalho de Melo {
91997e5848dSIan McDonald 	struct dccp_sock *dp = dccp_sk(sk);
9207c657876SArnaldo Carvalho de Melo 	struct sk_buff *skb;
921d83bd95bSGerrit Renker 	u32 data_was_unread = 0;
922134af346SHerbert Xu 	int state;
9237c657876SArnaldo Carvalho de Melo 
9247c657876SArnaldo Carvalho de Melo 	lock_sock(sk);
9257c657876SArnaldo Carvalho de Melo 
9267c657876SArnaldo Carvalho de Melo 	sk->sk_shutdown = SHUTDOWN_MASK;
9277c657876SArnaldo Carvalho de Melo 
9287c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_LISTEN) {
9297c657876SArnaldo Carvalho de Melo 		dccp_set_state(sk, DCCP_CLOSED);
9307c657876SArnaldo Carvalho de Melo 
9317c657876SArnaldo Carvalho de Melo 		/* Special case. */
9327c657876SArnaldo Carvalho de Melo 		inet_csk_listen_stop(sk);
9337c657876SArnaldo Carvalho de Melo 
9347c657876SArnaldo Carvalho de Melo 		goto adjudge_to_death;
9357c657876SArnaldo Carvalho de Melo 	}
9367c657876SArnaldo Carvalho de Melo 
93797e5848dSIan McDonald 	sk_stop_timer(sk, &dp->dccps_xmit_timer);
93897e5848dSIan McDonald 
9397c657876SArnaldo Carvalho de Melo 	/*
9407c657876SArnaldo Carvalho de Melo 	 * We need to flush the recv. buffs.  We do this only on the
9417c657876SArnaldo Carvalho de Melo 	 * descriptor close, not protocol-sourced closes, because the
9427c657876SArnaldo Carvalho de Melo 	  *reader process may not have drained the data yet!
9437c657876SArnaldo Carvalho de Melo 	 */
9447c657876SArnaldo Carvalho de Melo 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
945d83bd95bSGerrit Renker 		data_was_unread += skb->len;
9467c657876SArnaldo Carvalho de Melo 		__kfree_skb(skb);
9477c657876SArnaldo Carvalho de Melo 	}
9487c657876SArnaldo Carvalho de Melo 
949d83bd95bSGerrit Renker 	if (data_was_unread) {
950d83bd95bSGerrit Renker 		/* Unread data was tossed, send an appropriate Reset Code */
951d83bd95bSGerrit Renker 		DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
952d83bd95bSGerrit Renker 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
953d83bd95bSGerrit Renker 		dccp_set_state(sk, DCCP_CLOSED);
954d83bd95bSGerrit Renker 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
9557c657876SArnaldo Carvalho de Melo 		/* Check zero linger _after_ checking for unread data. */
9567c657876SArnaldo Carvalho de Melo 		sk->sk_prot->disconnect(sk, 0);
9570c869620SGerrit Renker 	} else if (sk->sk_state != DCCP_CLOSED) {
9580c869620SGerrit Renker 		dccp_terminate_connection(sk);
9597c657876SArnaldo Carvalho de Melo 	}
9607c657876SArnaldo Carvalho de Melo 
9617c657876SArnaldo Carvalho de Melo 	sk_stream_wait_close(sk, timeout);
9627c657876SArnaldo Carvalho de Melo 
9637c657876SArnaldo Carvalho de Melo adjudge_to_death:
964134af346SHerbert Xu 	state = sk->sk_state;
965134af346SHerbert Xu 	sock_hold(sk);
966134af346SHerbert Xu 	sock_orphan(sk);
967134af346SHerbert Xu 
9687ad07e7cSArnaldo Carvalho de Melo 	/*
9697ad07e7cSArnaldo Carvalho de Melo 	 * It is the last release_sock in its life. It will remove backlog.
9707ad07e7cSArnaldo Carvalho de Melo 	 */
9717c657876SArnaldo Carvalho de Melo 	release_sock(sk);
9727c657876SArnaldo Carvalho de Melo 	/*
9737c657876SArnaldo Carvalho de Melo 	 * Now socket is owned by kernel and we acquire BH lock
9747c657876SArnaldo Carvalho de Melo 	 * to finish close. No need to check for user refs.
9757c657876SArnaldo Carvalho de Melo 	 */
9767c657876SArnaldo Carvalho de Melo 	local_bh_disable();
9777c657876SArnaldo Carvalho de Melo 	bh_lock_sock(sk);
978547b792cSIlpo Järvinen 	WARN_ON(sock_owned_by_user(sk));
9797c657876SArnaldo Carvalho de Melo 
980eb4dea58SHerbert Xu 	percpu_counter_inc(sk->sk_prot->orphan_count);
981eb4dea58SHerbert Xu 
982134af346SHerbert Xu 	/* Have we already been destroyed by a softirq or backlog? */
983134af346SHerbert Xu 	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
984134af346SHerbert Xu 		goto out;
9857c657876SArnaldo Carvalho de Melo 
9867c657876SArnaldo Carvalho de Melo 	if (sk->sk_state == DCCP_CLOSED)
9877c657876SArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
9887c657876SArnaldo Carvalho de Melo 
9897c657876SArnaldo Carvalho de Melo 	/* Otherwise, socket is reprieved until protocol close. */
9907c657876SArnaldo Carvalho de Melo 
991134af346SHerbert Xu out:
9927c657876SArnaldo Carvalho de Melo 	bh_unlock_sock(sk);
9937c657876SArnaldo Carvalho de Melo 	local_bh_enable();
9947c657876SArnaldo Carvalho de Melo 	sock_put(sk);
9957c657876SArnaldo Carvalho de Melo }
9967c657876SArnaldo Carvalho de Melo 
997f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close);
998f21e68caSArnaldo Carvalho de Melo 
9997c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how)
10007c657876SArnaldo Carvalho de Melo {
10018e8c71f1SGerrit Renker 	dccp_pr_debug("called shutdown(%x)\n", how);
10027c657876SArnaldo Carvalho de Melo }
10037c657876SArnaldo Carvalho de Melo 
1004f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown);
1005f21e68caSArnaldo Carvalho de Melo 
100624e8b7e4SYOSHIFUJI Hideaki static inline int dccp_mib_init(void)
10077c657876SArnaldo Carvalho de Melo {
100824e8b7e4SYOSHIFUJI Hideaki 	return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
10097c657876SArnaldo Carvalho de Melo }
10107c657876SArnaldo Carvalho de Melo 
101124e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void)
101246f09ffaSArnaldo Carvalho de Melo {
101324e8b7e4SYOSHIFUJI Hideaki 	snmp_mib_free((void**)dccp_statistics);
101446f09ffaSArnaldo Carvalho de Melo }
101546f09ffaSArnaldo Carvalho de Melo 
10167c657876SArnaldo Carvalho de Melo static int thash_entries;
10177c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444);
10187c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
10197c657876SArnaldo Carvalho de Melo 
1020a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG
10217c657876SArnaldo Carvalho de Melo int dccp_debug;
102243264991SGerrit Renker module_param(dccp_debug, bool, 0644);
10237c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1024f21e68caSArnaldo Carvalho de Melo 
1025f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug);
1026a1d3a355SArnaldo Carvalho de Melo #endif
10277c657876SArnaldo Carvalho de Melo 
10287c657876SArnaldo Carvalho de Melo static int __init dccp_init(void)
10297c657876SArnaldo Carvalho de Melo {
10307c657876SArnaldo Carvalho de Melo 	unsigned long goal;
10317c657876SArnaldo Carvalho de Melo 	int ehash_order, bhash_order, i;
1032dd24c001SEric Dumazet 	int rc;
10337c657876SArnaldo Carvalho de Melo 
1034028b0275SPatrick McHardy 	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1035028b0275SPatrick McHardy 		     FIELD_SIZEOF(struct sk_buff, cb));
1036dd24c001SEric Dumazet 	rc = percpu_counter_init(&dccp_orphan_count, 0);
1037dd24c001SEric Dumazet 	if (rc)
1038dd24c001SEric Dumazet 		goto out;
1039dd24c001SEric Dumazet 	rc = -ENOBUFS;
10405caea4eaSEric Dumazet 	inet_hashinfo_init(&dccp_hashinfo);
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)
1046dd24c001SEric Dumazet 		goto out_free_percpu;
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 	 */
10544481374cSJan Beulich 	if (totalram_pages >= (128 * 1024))
10554481374cSJan Beulich 		goal = totalram_pages >> (21 - PAGE_SHIFT);
10567c657876SArnaldo Carvalho de Melo 	else
10574481374cSJan Beulich 		goal = totalram_pages >> (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 {
1065f373b53bSEric Dumazet 		unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
10667c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_ehash_bucket);
1067f373b53bSEric Dumazet 
1068f373b53bSEric Dumazet 		while (hash_size & (hash_size - 1))
1069f373b53bSEric Dumazet 			hash_size--;
1070f373b53bSEric Dumazet 		dccp_hashinfo.ehash_mask = hash_size - 1;
10717c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
10721c29b3ffSMel Gorman 			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
10737c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
10747c657876SArnaldo Carvalho de Melo 
10757c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.ehash) {
107659348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP established hash table");
10777c657876SArnaldo Carvalho de Melo 		goto out_free_bind_bucket_cachep;
10787c657876SArnaldo Carvalho de Melo 	}
10797c657876SArnaldo Carvalho de Melo 
1080f373b53bSEric Dumazet 	for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) {
10813ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
10823ab5aee7SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
10837c657876SArnaldo Carvalho de Melo 	}
10847c657876SArnaldo Carvalho de Melo 
1085230140cfSEric Dumazet 	if (inet_ehash_locks_alloc(&dccp_hashinfo))
1086230140cfSEric Dumazet 			goto out_free_dccp_ehash;
1087230140cfSEric Dumazet 
10887c657876SArnaldo Carvalho de Melo 	bhash_order = ehash_order;
10897c657876SArnaldo Carvalho de Melo 
10907c657876SArnaldo Carvalho de Melo 	do {
10917c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
10927c657876SArnaldo Carvalho de Melo 					sizeof(struct inet_bind_hashbucket);
10937690af3fSArnaldo Carvalho de Melo 		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
10947690af3fSArnaldo Carvalho de Melo 		    bhash_order > 0)
10957c657876SArnaldo Carvalho de Melo 			continue;
10967c657876SArnaldo Carvalho de Melo 		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
10971c29b3ffSMel Gorman 			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
10987c657876SArnaldo Carvalho de Melo 	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
10997c657876SArnaldo Carvalho de Melo 
11007c657876SArnaldo Carvalho de Melo 	if (!dccp_hashinfo.bhash) {
110159348b19SGerrit Renker 		DCCP_CRIT("Failed to allocate DCCP bind hash table");
1102230140cfSEric Dumazet 		goto out_free_dccp_locks;
11037c657876SArnaldo Carvalho de Melo 	}
11047c657876SArnaldo Carvalho de Melo 
11057c657876SArnaldo Carvalho de Melo 	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
11067c657876SArnaldo Carvalho de Melo 		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
11077c657876SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
11087c657876SArnaldo Carvalho de Melo 	}
11097c657876SArnaldo Carvalho de Melo 
111046f09ffaSArnaldo Carvalho de Melo 	rc = dccp_mib_init();
1111fa23e2ecSArnaldo Carvalho de Melo 	if (rc)
11127c657876SArnaldo Carvalho de Melo 		goto out_free_dccp_bhash;
11137c657876SArnaldo Carvalho de Melo 
11149b07ef5dSArnaldo Carvalho de Melo 	rc = dccp_ackvec_init();
11157c657876SArnaldo Carvalho de Melo 	if (rc)
1116b61fafc4SArnaldo Carvalho de Melo 		goto out_free_dccp_mib;
11179b07ef5dSArnaldo Carvalho de Melo 
1118e55d912fSArnaldo Carvalho de Melo 	rc = dccp_sysctl_init();
11199b07ef5dSArnaldo Carvalho de Melo 	if (rc)
11209b07ef5dSArnaldo Carvalho de Melo 		goto out_ackvec_exit;
11214c70f383SGerrit Renker 
1122ddebc973SGerrit Renker 	rc = ccid_initialize_builtins();
1123ddebc973SGerrit Renker 	if (rc)
1124ddebc973SGerrit Renker 		goto out_sysctl_exit;
1125ddebc973SGerrit Renker 
11264c70f383SGerrit Renker 	dccp_timestamping_init();
11277c657876SArnaldo Carvalho de Melo out:
11287c657876SArnaldo Carvalho de Melo 	return rc;
1129ddebc973SGerrit Renker out_sysctl_exit:
1130ddebc973SGerrit Renker 	dccp_sysctl_exit();
11319b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit:
11329b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1133b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib:
113446f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
11357c657876SArnaldo Carvalho de Melo out_free_dccp_bhash:
11367c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
11377c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bhash = NULL;
1138230140cfSEric Dumazet out_free_dccp_locks:
1139230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11407c657876SArnaldo Carvalho de Melo out_free_dccp_ehash:
11417c657876SArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
11427c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.ehash = NULL;
11437c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep:
11447c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11457c657876SArnaldo Carvalho de Melo 	dccp_hashinfo.bind_bucket_cachep = NULL;
1146dd24c001SEric Dumazet out_free_percpu:
1147dd24c001SEric Dumazet 	percpu_counter_destroy(&dccp_orphan_count);
11487c657876SArnaldo Carvalho de Melo 	goto out;
11497c657876SArnaldo Carvalho de Melo }
11507c657876SArnaldo Carvalho de Melo 
11517c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void)
11527c657876SArnaldo Carvalho de Melo {
1153ddebc973SGerrit Renker 	ccid_cleanup_builtins();
115446f09ffaSArnaldo Carvalho de Melo 	dccp_mib_exit();
1155725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.bhash,
1156725ba8eeSArnaldo Carvalho de Melo 		   get_order(dccp_hashinfo.bhash_size *
1157725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_bind_hashbucket)));
1158725ba8eeSArnaldo Carvalho de Melo 	free_pages((unsigned long)dccp_hashinfo.ehash,
1159f373b53bSEric Dumazet 		   get_order((dccp_hashinfo.ehash_mask + 1) *
1160725ba8eeSArnaldo Carvalho de Melo 			     sizeof(struct inet_ehash_bucket)));
1161230140cfSEric Dumazet 	inet_ehash_locks_free(&dccp_hashinfo);
11627c657876SArnaldo Carvalho de Melo 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11639b07ef5dSArnaldo Carvalho de Melo 	dccp_ackvec_exit();
1164e55d912fSArnaldo Carvalho de Melo 	dccp_sysctl_exit();
1165476181cbSWei Yongjun 	percpu_counter_destroy(&dccp_orphan_count);
11667c657876SArnaldo Carvalho de Melo }
11677c657876SArnaldo Carvalho de Melo 
11687c657876SArnaldo Carvalho de Melo module_init(dccp_init);
11697c657876SArnaldo Carvalho de Melo module_exit(dccp_fini);
11707c657876SArnaldo Carvalho de Melo 
11717c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
11727c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
11737c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1174