Blame view

net/dccp/proto.c 29.9 KB
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1
2
3
4
5
6
7
8
9
10
  /*
   *  net/dccp/proto.c
   *
   *  An implementation of the DCCP protocol
   *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
   *
   *	This program is free software; you can redistribute it and/or modify it
   *	under the terms of the GNU General Public License version 2 as
   *	published by the Free Software Foundation.
   */
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
11
12
13
14
15
16
17
18
19
20
21
  #include <linux/dccp.h>
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/sched.h>
  #include <linux/kernel.h>
  #include <linux/skbuff.h>
  #include <linux/netdevice.h>
  #include <linux/in.h>
  #include <linux/if_arp.h>
  #include <linux/init.h>
  #include <linux/random.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
23
  #include <net/checksum.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
24
  #include <net/inet_sock.h>
120e9daba   Eric Dumazet   dccp: defer ccid_...
25
  #include <net/inet_common.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
26
27
  #include <net/sock.h>
  #include <net/xfrm.h>
6273172e1   Arnaldo Carvalho de Melo   [DCCP]: Implement...
28
  #include <asm/ioctls.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
29
30
31
32
  #include <linux/spinlock.h>
  #include <linux/timer.h>
  #include <linux/delay.h>
  #include <linux/poll.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
33
34
35
  
  #include "ccid.h"
  #include "dccp.h"
afe00251d   Andrea Bittau   [DCCP]: Initial f...
36
  #include "feat.h"
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
37

ee549be6f   Masami Hiramatsu   net: dccp: Add DC...
38
39
  #define CREATE_TRACE_POINTS
  #include "trace.h"
ba89966c1   Eric Dumazet   [NET]: use __read...
40
  DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
41

f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
42
  EXPORT_SYMBOL_GPL(dccp_statistics);
dd24c0019   Eric Dumazet   net: Use a percpu...
43
  struct percpu_counter dccp_orphan_count;
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
44
  EXPORT_SYMBOL_GPL(dccp_orphan_count);
5caea4ea7   Eric Dumazet   net: listening_ha...
45
  struct inet_hashinfo dccp_hashinfo;
075ae8661   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
46
  EXPORT_SYMBOL_GPL(dccp_hashinfo);
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
47
48
  /* the maximum queue length for tx in packets. 0 is no limit */
  int sysctl_dccp_tx_qlen __read_mostly = 5;
1f4f0f645   stephen hemminger   dccp: Kill dead c...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #ifdef CONFIG_IP_DCCP_DEBUG
  static const char *dccp_state_name(const int state)
  {
  	static const char *const dccp_state_names[] = {
  	[DCCP_OPEN]		= "OPEN",
  	[DCCP_REQUESTING]	= "REQUESTING",
  	[DCCP_PARTOPEN]		= "PARTOPEN",
  	[DCCP_LISTEN]		= "LISTEN",
  	[DCCP_RESPOND]		= "RESPOND",
  	[DCCP_CLOSING]		= "CLOSING",
  	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
  	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
  	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
  	[DCCP_TIME_WAIT]	= "TIME_WAIT",
  	[DCCP_CLOSED]		= "CLOSED",
  	};
  
  	if (state >= DCCP_MAX_STATES)
  		return "INVALID STATE!";
  	else
  		return dccp_state_names[state];
  }
  #endif
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
72
73
74
  void dccp_set_state(struct sock *sk, const int state)
  {
  	const int oldstate = sk->sk_state;
f11135a34   Gerrit Renker   [DCCP]: Dedicated...
75
76
  	dccp_pr_debug("%s(%p)  %s  -->  %s
  ", dccp_role(sk), sk,
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
77
78
79
80
81
82
83
  		      dccp_state_name(oldstate), dccp_state_name(state));
  	WARN_ON(state == oldstate);
  
  	switch (state) {
  	case DCCP_OPEN:
  		if (oldstate != DCCP_OPEN)
  			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
6eb55d172   Gerrit Renker   dccp: Integration...
84
85
86
  		/* Client retransmits all Confirm options until entering OPEN */
  		if (oldstate == DCCP_PARTOPEN)
  			dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
87
88
89
  		break;
  
  	case DCCP_CLOSED:
0c8696207   Gerrit Renker   [DCCP]: Integrate...
90
91
  		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
  		    oldstate == DCCP_CLOSING)
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
92
93
94
95
96
  			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
  
  		sk->sk_prot->unhash(sk);
  		if (inet_csk(sk)->icsk_bind_hash != NULL &&
  		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
ab1e0a13d   Arnaldo Carvalho de Melo   [SOCK] proto: Add...
97
  			inet_put_port(sk);
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
98
99
100
101
102
103
104
105
106
  		/* fall through */
  	default:
  		if (oldstate == DCCP_OPEN)
  			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
  	}
  
  	/* Change state AFTER socket is unhashed to avoid closed
  	 * socket sitting in hash tables.
  	 */
b0832e300   Yafang Shao   net: tracepoint: ...
107
  	inet_sk_set_state(sk, state);
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
108
109
110
  }
  
  EXPORT_SYMBOL_GPL(dccp_set_state);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  static void dccp_finish_passive_close(struct sock *sk)
  {
  	switch (sk->sk_state) {
  	case DCCP_PASSIVE_CLOSE:
  		/* Node (client or server) has received Close packet. */
  		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
  		dccp_set_state(sk, DCCP_CLOSED);
  		break;
  	case DCCP_PASSIVE_CLOSEREQ:
  		/*
  		 * Client received CloseReq. We set the `active' flag so that
  		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
  		 */
  		dccp_send_close(sk, 1);
  		dccp_set_state(sk, DCCP_CLOSING);
  	}
  }
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  void dccp_done(struct sock *sk)
  {
  	dccp_set_state(sk, DCCP_CLOSED);
  	dccp_clear_xmit_timers(sk);
  
  	sk->sk_shutdown = SHUTDOWN_MASK;
  
  	if (!sock_flag(sk, SOCK_DEAD))
  		sk->sk_state_change(sk);
  	else
  		inet_csk_destroy_sock(sk);
  }
  
  EXPORT_SYMBOL_GPL(dccp_done);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
142
143
  const char *dccp_packet_name(const int type)
  {
36cbd3dcc   Jan Engelhardt   net: mark read-on...
144
  	static const char *const dccp_packet_names[] = {
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  		[DCCP_PKT_REQUEST]  = "REQUEST",
  		[DCCP_PKT_RESPONSE] = "RESPONSE",
  		[DCCP_PKT_DATA]	    = "DATA",
  		[DCCP_PKT_ACK]	    = "ACK",
  		[DCCP_PKT_DATAACK]  = "DATAACK",
  		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
  		[DCCP_PKT_CLOSE]    = "CLOSE",
  		[DCCP_PKT_RESET]    = "RESET",
  		[DCCP_PKT_SYNC]	    = "SYNC",
  		[DCCP_PKT_SYNCACK]  = "SYNCACK",
  	};
  
  	if (type >= DCCP_NR_PKT_TYPES)
  		return "INVALID";
  	else
  		return dccp_packet_names[type];
  }
  
  EXPORT_SYMBOL_GPL(dccp_packet_name);
120e9daba   Eric Dumazet   dccp: defer ccid_...
164
165
166
167
168
169
170
171
  static void dccp_sk_destruct(struct sock *sk)
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  
  	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  	dp->dccps_hc_tx_ccid = NULL;
  	inet_sock_destruct(sk);
  }
724788735   Arnaldo Carvalho de Melo   [DCCP] ipv6: Add ...
172
  int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
173
174
175
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  	struct inet_connection_sock *icsk = inet_csk(sk);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
176

e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
177
178
179
180
  	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
  	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
  	sk->sk_state		= DCCP_CLOSED;
  	sk->sk_write_space	= dccp_write_space;
120e9daba   Eric Dumazet   dccp: defer ccid_...
181
  	sk->sk_destruct		= dccp_sk_destruct;
e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
182
  	icsk->icsk_sync_mss	= dccp_sync_mss;
410e27a49   Gerrit Renker   This reverts "Mer...
183
  	dp->dccps_mss_cache	= 536;
e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
184
185
186
  	dp->dccps_rate_last	= jiffies;
  	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
  	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
187
  	dp->dccps_tx_qlen	= sysctl_dccp_tx_qlen;
e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
188
189
  
  	dccp_init_xmit_timers(sk);
ac75773c2   Gerrit Renker   dccp: Per-socket ...
190
  	INIT_LIST_HEAD(&dp->dccps_featneg);
6eb55d172   Gerrit Renker   dccp: Integration...
191
192
193
  	/* control socket doesn't need feat nego */
  	if (likely(ctl_sock_initialized))
  		return dccp_feat_init(sk);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
194
195
196
197
  	return 0;
  }
  
  EXPORT_SYMBOL_GPL(dccp_init_sock);
7d06b2e05   Brian Haley   net: change proto...
198
  void dccp_destroy_sock(struct sock *sk)
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
199
200
  {
  	struct dccp_sock *dp = dccp_sk(sk);
7749d4ff8   Eric Dumazet   dccp: purge write...
201
  	__skb_queue_purge(&sk->sk_write_queue);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
202
203
204
205
206
207
208
  	if (sk->sk_send_head != NULL) {
  		kfree_skb(sk->sk_send_head);
  		sk->sk_send_head = NULL;
  	}
  
  	/* Clean up a referenced DCCP bind bucket. */
  	if (inet_csk(sk)->icsk_bind_hash != NULL)
ab1e0a13d   Arnaldo Carvalho de Melo   [SOCK] proto: Add...
209
  		inet_put_port(sk);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
210
211
212
  
  	kfree(dp->dccps_service_list);
  	dp->dccps_service_list = NULL;
6fdd34d43   Gerrit Renker   dccp ccid-2: Phas...
213
  	if (dp->dccps_hc_rx_ackvec != NULL) {
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
214
215
216
217
  		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  		dp->dccps_hc_rx_ackvec = NULL;
  	}
  	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
120e9daba   Eric Dumazet   dccp: defer ccid_...
218
  	dp->dccps_hc_rx_ccid = NULL;
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
219
220
  
  	/* clean up feature negotiation state */
d99a7bd21   Gerrit Renker   dccp: Cleanup rou...
221
  	dccp_feat_list_purge(&dp->dccps_featneg);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
222
223
224
  }
  
  EXPORT_SYMBOL_GPL(dccp_destroy_sock);
72a3effaf   Eric Dumazet   [NET]: Size liste...
225
  static inline int dccp_listen_start(struct sock *sk, int backlog)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
226
  {
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
227
228
229
  	struct dccp_sock *dp = dccp_sk(sk);
  
  	dp->dccps_role = DCCP_ROLE_LISTEN;
9eca0a47d   Gerrit Renker   dccp: Resolve dep...
230
231
232
  	/* do not start to listen if feature negotiation setup fails */
  	if (dccp_feat_finalise_settings(dp))
  		return -EPROTO;
72a3effaf   Eric Dumazet   [NET]: Size liste...
233
  	return inet_csk_listen_start(sk, backlog);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
234
  }
ce865a61c   Gerrit Renker   [DCCP]: Add suppo...
235
236
237
238
239
  static inline int dccp_need_reset(int state)
  {
  	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
  	       state != DCCP_REQUESTING;
  }
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
240
241
242
243
  int dccp_disconnect(struct sock *sk, int flags)
  {
  	struct inet_connection_sock *icsk = inet_csk(sk);
  	struct inet_sock *inet = inet_sk(sk);
69c64866c   Mohamed Ghannam   dccp: CVE-2017-88...
244
  	struct dccp_sock *dp = dccp_sk(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
245
246
247
248
249
  	int err = 0;
  	const int old_state = sk->sk_state;
  
  	if (old_state != DCCP_CLOSED)
  		dccp_set_state(sk, DCCP_CLOSED);
ce865a61c   Gerrit Renker   [DCCP]: Add suppo...
250
251
252
253
  	/*
  	 * This corresponds to the ABORT function of RFC793, sec. 3.8
  	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
  	 */
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
254
255
  	if (old_state == DCCP_LISTEN) {
  		inet_csk_listen_stop(sk);
ce865a61c   Gerrit Renker   [DCCP]: Add suppo...
256
257
258
  	} else if (dccp_need_reset(old_state)) {
  		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  		sk->sk_err = ECONNRESET;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
259
260
261
262
  	} else if (old_state == DCCP_REQUESTING)
  		sk->sk_err = ECONNRESET;
  
  	dccp_clear_xmit_timers(sk);
69c64866c   Mohamed Ghannam   dccp: CVE-2017-88...
263
  	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
69c64866c   Mohamed Ghannam   dccp: CVE-2017-88...
264
  	dp->dccps_hc_rx_ccid = NULL;
48816322a   Gerrit Renker   dccp: Empty the w...
265

7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
266
  	__skb_queue_purge(&sk->sk_receive_queue);
48816322a   Gerrit Renker   dccp: Empty the w...
267
  	__skb_queue_purge(&sk->sk_write_queue);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
268
269
270
271
  	if (sk->sk_send_head != NULL) {
  		__kfree_skb(sk->sk_send_head);
  		sk->sk_send_head = NULL;
  	}
c720c7e83   Eric Dumazet   inet: rename some...
272
  	inet->inet_dport = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
273
274
275
276
277
278
279
280
281
282
  
  	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  		inet_reset_saddr(sk);
  
  	sk->sk_shutdown = 0;
  	sock_reset_flag(sk, SOCK_DONE);
  
  	icsk->icsk_backoff = 0;
  	inet_csk_delack_init(sk);
  	__sk_dst_reset(sk);
c720c7e83   Eric Dumazet   inet: rename some...
283
  	WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
284
285
286
287
  
  	sk->sk_error_report(sk);
  	return err;
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
288
  EXPORT_SYMBOL_GPL(dccp_disconnect);
f4335f52b   Christoph Hellwig   net/dccp: convert...
289
  __poll_t dccp_poll_mask(struct socket *sock, __poll_t events)
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
290
  {
ade994f4f   Al Viro   net: annotate ->p...
291
  	__poll_t mask;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
292
  	struct sock *sk = sock->sk;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
293
294
295
296
297
298
299
300
301
302
  	if (sk->sk_state == DCCP_LISTEN)
  		return inet_csk_listen_poll(sk);
  
  	/* Socket is not locked. We are protected from async events
  	   by poll logic and correct handling of state changes
  	   made by another threads is impossible in any case.
  	 */
  
  	mask = 0;
  	if (sk->sk_err)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
303
  		mask = EPOLLERR;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
304
305
  
  	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
306
  		mask |= EPOLLHUP;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
307
  	if (sk->sk_shutdown & RCV_SHUTDOWN)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
308
  		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
309
310
311
312
  
  	/* Connected? */
  	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
  		if (atomic_read(&sk->sk_rmem_alloc) > 0)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
313
  			mask |= EPOLLIN | EPOLLRDNORM;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
314
315
  
  		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
64dc61306   Eric Dumazet   net: add sk_strea...
316
  			if (sk_stream_is_writeable(sk)) {
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
317
  				mask |= EPOLLOUT | EPOLLWRNORM;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
318
  			} else {  /* send SIGIO later */
9cd3e072b   Eric Dumazet   net: rename SOCK_...
319
  				sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
320
321
322
323
324
325
  				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  
  				/* Race breaker. If space is freed after
  				 * wspace test but before the flags are set,
  				 * IO signal will be lost.
  				 */
64dc61306   Eric Dumazet   net: add sk_strea...
326
  				if (sk_stream_is_writeable(sk))
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
327
  					mask |= EPOLLOUT | EPOLLWRNORM;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
328
329
330
331
332
  			}
  		}
  	}
  	return mask;
  }
f4335f52b   Christoph Hellwig   net/dccp: convert...
333
  EXPORT_SYMBOL_GPL(dccp_poll_mask);
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
334

7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
335
336
  int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  {
6273172e1   Arnaldo Carvalho de Melo   [DCCP]: Implement...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  	int rc = -ENOTCONN;
  
  	lock_sock(sk);
  
  	if (sk->sk_state == DCCP_LISTEN)
  		goto out;
  
  	switch (cmd) {
  	case SIOCINQ: {
  		struct sk_buff *skb;
  		unsigned long amount = 0;
  
  		skb = skb_peek(&sk->sk_receive_queue);
  		if (skb != NULL) {
  			/*
  			 * We will only return the amount of this packet since
  			 * that is all that will be read.
  			 */
  			amount = skb->len;
  		}
  		rc = put_user(amount, (int __user *)arg);
  	}
  		break;
  	default:
  		rc = -ENOIOCTLCMD;
  		break;
  	}
  out:
  	release_sock(sk);
  	return rc;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
367
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
368
  EXPORT_SYMBOL_GPL(dccp_ioctl);
60fe62e78   Andrea Bittau   [DCCP]: sparse en...
369
  static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
b7058842c   David S. Miller   net: Make setsock...
370
  				   char __user *optval, unsigned int optlen)
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
371
372
373
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  	struct dccp_service_list *sl = NULL;
8109b02b5   Arnaldo Carvalho de Melo   [DCCP]: Whitespac...
374
  	if (service == DCCP_SERVICE_INVALID_VALUE ||
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
  		return -EINVAL;
  
  	if (optlen > sizeof(service)) {
  		sl = kmalloc(optlen, GFP_KERNEL);
  		if (sl == NULL)
  			return -ENOMEM;
  
  		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
  		if (copy_from_user(sl->dccpsl_list,
  				   optval + sizeof(service),
  				   optlen - sizeof(service)) ||
  		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
  			kfree(sl);
  			return -EFAULT;
  		}
  	}
  
  	lock_sock(sk);
  	dp->dccps_service = service;
a51482bde   Jesper Juhl   [NET]: kfree cleanup
395
  	kfree(dp->dccps_service_list);
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
396
397
398
399
400
  
  	dp->dccps_service_list = sl;
  	release_sock(sk);
  	return 0;
  }
294505598   Gerrit Renker   dccp: Feature neg...
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
  {
  	u8 *list, len;
  	int i, rc;
  
  	if (cscov < 0 || cscov > 15)
  		return -EINVAL;
  	/*
  	 * Populate a list of permissible values, in the range cscov...15. This
  	 * is necessary since feature negotiation of single values only works if
  	 * both sides incidentally choose the same value. Since the list starts
  	 * lowest-value first, negotiation will pick the smallest shared value.
  	 */
  	if (cscov == 0)
  		return 0;
  	len = 16 - cscov;
  
  	list = kmalloc(len, GFP_KERNEL);
  	if (list == NULL)
  		return -ENOBUFS;
  
  	for (i = 0; i < len; i++)
  		list[i] = cscov++;
  
  	rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
  
  	if (rc == 0) {
  		if (rx)
  			dccp_sk(sk)->dccps_pcrlen = cscov;
  		else
  			dccp_sk(sk)->dccps_pcslen = cscov;
  	}
  	kfree(list);
  	return rc;
  }
b20a9c24d   Gerrit Renker   dccp: Set per-con...
436
  static int dccp_setsockopt_ccid(struct sock *sk, int type,
b7058842c   David S. Miller   net: Make setsock...
437
  				char __user *optval, unsigned int optlen)
b20a9c24d   Gerrit Renker   dccp: Set per-con...
438
439
440
441
442
443
  {
  	u8 *val;
  	int rc = 0;
  
  	if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
  		return -EINVAL;
042604d2a   Julia Lawall   net/dccp: Use mem...
444
445
446
  	val = memdup_user(optval, optlen);
  	if (IS_ERR(val))
  		return PTR_ERR(val);
b20a9c24d   Gerrit Renker   dccp: Set per-con...
447
448
449
450
451
452
453
454
455
456
457
458
  
  	lock_sock(sk);
  	if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
  		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
  
  	if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
  		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
  	release_sock(sk);
  
  	kfree(val);
  	return rc;
  }
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
459
  static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
b7058842c   David S. Miller   net: Make setsock...
460
  		char __user *optval, unsigned int optlen)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
461
  {
09dbc3895   Gerrit Renker   [DCCP]: Miscellan...
462
463
  	struct dccp_sock *dp = dccp_sk(sk);
  	int val, err = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
464

191029963   Gerrit Renker   dccp: Tidy up set...
465
466
467
468
469
470
471
472
473
474
  	switch (optname) {
  	case DCCP_SOCKOPT_PACKET_SIZE:
  		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app
  ");
  		return 0;
  	case DCCP_SOCKOPT_CHANGE_L:
  	case DCCP_SOCKOPT_CHANGE_R:
  		DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app
  ");
  		return 0;
b20a9c24d   Gerrit Renker   dccp: Set per-con...
475
476
477
478
  	case DCCP_SOCKOPT_CCID:
  	case DCCP_SOCKOPT_RX_CCID:
  	case DCCP_SOCKOPT_TX_CCID:
  		return dccp_setsockopt_ccid(sk, optname, optval, optlen);
191029963   Gerrit Renker   dccp: Tidy up set...
479
480
481
  	}
  
  	if (optlen < (int)sizeof(int))
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
482
483
484
485
  		return -EINVAL;
  
  	if (get_user(val, (int __user *)optval))
  		return -EFAULT;
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
486
487
  	if (optname == DCCP_SOCKOPT_SERVICE)
  		return dccp_setsockopt_service(sk, val, optval, optlen);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
488

67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
489
  	lock_sock(sk);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
490
  	switch (optname) {
b8599d207   Gerrit Renker   [DCCP]: Support f...
491
492
493
494
495
496
  	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
  		if (dp->dccps_role != DCCP_ROLE_SERVER)
  			err = -EOPNOTSUPP;
  		else
  			dp->dccps_server_timewait = (val != 0);
  		break;
294505598   Gerrit Renker   dccp: Feature neg...
497
498
  	case DCCP_SOCKOPT_SEND_CSCOV:
  		err = dccp_setsockopt_cscov(sk, val, false);
d6da3511d   Tomasz Grobelny   dccp: Policy-base...
499
  		break;
294505598   Gerrit Renker   dccp: Feature neg...
500
501
  	case DCCP_SOCKOPT_RECV_CSCOV:
  		err = dccp_setsockopt_cscov(sk, val, true);
d6da3511d   Tomasz Grobelny   dccp: Policy-base...
502
  		break;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  	case DCCP_SOCKOPT_QPOLICY_ID:
  		if (sk->sk_state != DCCP_CLOSED)
  			err = -EISCONN;
  		else if (val < 0 || val >= DCCPQ_POLICY_MAX)
  			err = -EINVAL;
  		else
  			dp->dccps_qpolicy = val;
  		break;
  	case DCCP_SOCKOPT_QPOLICY_TXQLEN:
  		if (val < 0)
  			err = -EINVAL;
  		else
  			dp->dccps_tx_qlen = val;
  		break;
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
517
518
519
520
  	default:
  		err = -ENOPROTOOPT;
  		break;
  	}
410e27a49   Gerrit Renker   This reverts "Mer...
521
  	release_sock(sk);
191029963   Gerrit Renker   dccp: Tidy up set...
522

a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
523
  	return err;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
524
  }
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
525
  int dccp_setsockopt(struct sock *sk, int level, int optname,
b7058842c   David S. Miller   net: Make setsock...
526
  		    char __user *optval, unsigned int optlen)
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
527
528
529
530
531
532
533
  {
  	if (level != SOL_DCCP)
  		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
  							     optname, optval,
  							     optlen);
  	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
  }
543d9cfee   Arnaldo Carvalho de Melo   [NET]: Identation...
534

f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
535
  EXPORT_SYMBOL_GPL(dccp_setsockopt);
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
536
537
  #ifdef CONFIG_COMPAT
  int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
b7058842c   David S. Miller   net: Make setsock...
538
  			   char __user *optval, unsigned int optlen)
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
539
  {
dec73ff02   Arnaldo Carvalho de Melo   [ICSK] compat: In...
540
541
542
  	if (level != SOL_DCCP)
  		return inet_csk_compat_setsockopt(sk, level, optname,
  						  optval, optlen);
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
543
544
  	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
  }
543d9cfee   Arnaldo Carvalho de Melo   [NET]: Identation...
545

3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
546
547
  EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
  #endif
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
548
  static int dccp_getsockopt_service(struct sock *sk, int len,
60fe62e78   Andrea Bittau   [DCCP]: sparse en...
549
  				   __be32 __user *optval,
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
550
551
552
553
554
555
556
  				   int __user *optlen)
  {
  	const struct dccp_sock *dp = dccp_sk(sk);
  	const struct dccp_service_list *sl;
  	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
  
  	lock_sock(sk);
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  	if ((sl = dp->dccps_service_list) != NULL) {
  		slen = sl->dccpsl_nr * sizeof(u32);
  		total_len += slen;
  	}
  
  	err = -EINVAL;
  	if (total_len > len)
  		goto out;
  
  	err = 0;
  	if (put_user(total_len, optlen) ||
  	    put_user(dp->dccps_service, optval) ||
  	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
  		err = -EFAULT;
  out:
  	release_sock(sk);
  	return err;
  }
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
575
  static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
a1d3a3551   Arnaldo Carvalho de Melo   [DCCP]: Fix spars...
576
  		    char __user *optval, int __user *optlen)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
577
  {
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
578
579
  	struct dccp_sock *dp;
  	int val, len;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
580

a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
581
582
  	if (get_user(len, optlen))
  		return -EFAULT;
39ebc0276   Arnaldo Carvalho de Melo   [DCCP] getsockopt...
583
  	if (len < (int)sizeof(int))
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
584
585
586
587
588
589
  		return -EINVAL;
  
  	dp = dccp_sk(sk);
  
  	switch (optname) {
  	case DCCP_SOCKOPT_PACKET_SIZE:
5aed32436   Gerrit Renker   [DCCP]: Tidy up u...
590
591
  		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app
  ");
841bac1d6   Arnaldo Carvalho de Melo   [DCCP]: Make {set...
592
  		return 0;
88f964db6   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
593
594
  	case DCCP_SOCKOPT_SERVICE:
  		return dccp_getsockopt_service(sk, len,
60fe62e78   Andrea Bittau   [DCCP]: sparse en...
595
  					       (__be32 __user *)optval, optlen);
7c559a9e4   Gerrit Renker   [DCCP]: Add socke...
596
597
  	case DCCP_SOCKOPT_GET_CUR_MPS:
  		val = dp->dccps_mss_cache;
7c559a9e4   Gerrit Renker   [DCCP]: Add socke...
598
  		break;
d90ebcbfa   Gerrit Renker   dccp: Query suppo...
599
600
  	case DCCP_SOCKOPT_AVAILABLE_CCIDS:
  		return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
71c262a3d   Gerrit Renker   dccp: API to quer...
601
602
603
604
605
606
607
608
609
610
  	case DCCP_SOCKOPT_TX_CCID:
  		val = ccid_get_current_tx_ccid(dp);
  		if (val < 0)
  			return -ENOPROTOOPT;
  		break;
  	case DCCP_SOCKOPT_RX_CCID:
  		val = ccid_get_current_rx_ccid(dp);
  		if (val < 0)
  			return -ENOPROTOOPT;
  		break;
b8599d207   Gerrit Renker   [DCCP]: Support f...
611
612
  	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
  		val = dp->dccps_server_timewait;
b8599d207   Gerrit Renker   [DCCP]: Support f...
613
  		break;
6f4e5fff1   Gerrit Renker   [DCCP]: Support f...
614
615
616
617
618
619
  	case DCCP_SOCKOPT_SEND_CSCOV:
  		val = dp->dccps_pcslen;
  		break;
  	case DCCP_SOCKOPT_RECV_CSCOV:
  		val = dp->dccps_pcrlen;
  		break;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
620
621
622
623
624
625
  	case DCCP_SOCKOPT_QPOLICY_ID:
  		val = dp->dccps_qpolicy;
  		break;
  	case DCCP_SOCKOPT_QPOLICY_TXQLEN:
  		val = dp->dccps_tx_qlen;
  		break;
88f964db6   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
626
627
628
629
630
631
  	case 128 ... 191:
  		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
  					     len, (u32 __user *)optval, optlen);
  	case 192 ... 255:
  		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
  					     len, (u32 __user *)optval, optlen);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
632
633
634
  	default:
  		return -ENOPROTOOPT;
  	}
791335066   Gerrit Renker   [DCCP]: Collapse ...
635
  	len = sizeof(val);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
636
637
638
639
  	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  		return -EFAULT;
  
  	return 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
640
  }
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
641
642
643
644
645
646
647
648
649
  int dccp_getsockopt(struct sock *sk, int level, int optname,
  		    char __user *optval, int __user *optlen)
  {
  	if (level != SOL_DCCP)
  		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
  							     optname, optval,
  							     optlen);
  	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
  }
543d9cfee   Arnaldo Carvalho de Melo   [NET]: Identation...
650

f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
651
  EXPORT_SYMBOL_GPL(dccp_getsockopt);
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
652
653
  #ifdef CONFIG_COMPAT
  int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
543d9cfee   Arnaldo Carvalho de Melo   [NET]: Identation...
654
  			   char __user *optval, int __user *optlen)
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
655
  {
dec73ff02   Arnaldo Carvalho de Melo   [ICSK] compat: In...
656
657
658
  	if (level != SOL_DCCP)
  		return inet_csk_compat_getsockopt(sk, level, optname,
  						  optval, optlen);
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
659
660
  	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
  }
543d9cfee   Arnaldo Carvalho de Melo   [NET]: Identation...
661

3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
662
663
  EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
  #endif
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
664
665
  static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
  {
f95b414ed   Gu Zheng   net: introduce he...
666
  	struct cmsghdr *cmsg;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
667
668
669
670
671
672
673
674
675
676
677
678
  
  	/*
  	 * Assign an (opaque) qpolicy priority value to skb->priority.
  	 *
  	 * We are overloading this skb field for use with the qpolicy subystem.
  	 * The skb->priority is normally used for the SO_PRIORITY option, which
  	 * is initialised from sk_priority. Since the assignment of sk_priority
  	 * to skb->priority happens later (on layer 3), we overload this field
  	 * for use with queueing priorities as long as the skb is on layer 4.
  	 * The default priority value (if nothing is set) is 0.
  	 */
  	skb->priority = 0;
f95b414ed   Gu Zheng   net: introduce he...
679
  	for_each_cmsghdr(cmsg, msg) {
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
680
681
682
683
684
  		if (!CMSG_OK(msg, cmsg))
  			return -EINVAL;
  
  		if (cmsg->cmsg_level != SOL_DCCP)
  			continue;
049102650   Tomasz Grobelny   dccp qpolicy: Par...
685
686
687
  		if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX &&
  		    !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type))
  			return -EINVAL;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
688
689
690
691
692
693
694
695
696
697
698
699
  		switch (cmsg->cmsg_type) {
  		case DCCP_SCM_PRIORITY:
  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32)))
  				return -EINVAL;
  			skb->priority = *(__u32 *)CMSG_DATA(cmsg);
  			break;
  		default:
  			return -EINVAL;
  		}
  	}
  	return 0;
  }
1b7841404   Ying Xue   net: Remove iocb ...
700
  int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
701
702
703
704
705
706
707
  {
  	const struct dccp_sock *dp = dccp_sk(sk);
  	const int flags = msg->msg_flags;
  	const int noblock = flags & MSG_DONTWAIT;
  	struct sk_buff *skb;
  	int rc, size;
  	long timeo;
ee549be6f   Masami Hiramatsu   net: dccp: Add DC...
708
  	trace_dccp_probe(sk, len);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
709
710
711
712
  	if (len > dp->dccps_mss_cache)
  		return -EMSGSIZE;
  
  	lock_sock(sk);
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
713

871a2c16c   Tomasz Grobelny   dccp: Policy-base...
714
  	if (dccp_qpolicy_full(sk)) {
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
715
716
717
  		rc = -EAGAIN;
  		goto out_release;
  	}
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
718
  	timeo = sock_sndtimeo(sk, noblock);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
719
720
721
722
723
724
  
  	/*
  	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
  	 * so that the trick in dccp_rcv_request_sent_state_process.
  	 */
  	/* Wait for a connection to finish. */
cecd8d0ec   Gerrit Renker   [DCCP]: Reduce th...
725
  	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
726
  		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
727
  			goto out_release;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
728
729
730
731
732
  
  	size = sk->sk_prot->max_header + len;
  	release_sock(sk);
  	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
  	lock_sock(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
733
734
  	if (skb == NULL)
  		goto out_release;
67f93df79   Alexey Kodanev   dccp: check sk fo...
735
736
737
738
  	if (sk->sk_state == DCCP_CLOSED) {
  		rc = -ENOTCONN;
  		goto out_discard;
  	}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
739
  	skb_reserve(skb, sk->sk_prot->max_header);
6ce8e9ce5   Al Viro   new helper: memcp...
740
  	rc = memcpy_from_msg(skb_put(skb, len), msg, len);
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
741
742
  	if (rc != 0)
  		goto out_discard;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
743
744
745
746
747
  	rc = dccp_msghdr_parse(msg, skb);
  	if (rc != 0)
  		goto out_discard;
  
  	dccp_qpolicy_push(sk, skb);
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
748
749
750
751
752
753
754
  	/*
  	 * The xmit_timer is set if the TX CCID is rate-based and will expire
  	 * when congestion control permits to release further packets into the
  	 * network. Window-based CCIDs do not use this timer.
  	 */
  	if (!timer_pending(&dp->dccps_xmit_timer))
  		dccp_write_xmit(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
755
756
757
  out_release:
  	release_sock(sk);
  	return rc ? : len;
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
758
759
  out_discard:
  	kfree_skb(skb);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
760
  	goto out_release;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
761
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
762
  EXPORT_SYMBOL_GPL(dccp_sendmsg);
1b7841404   Ying Xue   net: Remove iocb ...
763
764
  int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  		 int flags, int *addr_len)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
765
766
  {
  	const struct dccp_hdr *dh;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
767
768
769
  	long timeo;
  
  	lock_sock(sk);
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
770
771
  	if (sk->sk_state == DCCP_LISTEN) {
  		len = -ENOTCONN;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
772
  		goto out;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
773
  	}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
774

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
775
  	timeo = sock_rcvtimeo(sk, nonblock);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
776
777
  
  	do {
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
778
  		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
779

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
780
781
  		if (skb == NULL)
  			goto verify_sock_status;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
782

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
783
  		dh = dccp_hdr(skb);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
784

0c8696207   Gerrit Renker   [DCCP]: Integrate...
785
786
787
  		switch (dh->dccph_type) {
  		case DCCP_PKT_DATA:
  		case DCCP_PKT_DATAACK:
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
788
  			goto found_ok_skb;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
789

0c8696207   Gerrit Renker   [DCCP]: Integrate...
790
791
792
793
794
795
796
797
798
  		case DCCP_PKT_CLOSE:
  		case DCCP_PKT_CLOSEREQ:
  			if (!(flags & MSG_PEEK))
  				dccp_finish_passive_close(sk);
  			/* fall through */
  		case DCCP_PKT_RESET:
  			dccp_pr_debug("found fin (%s) ok!
  ",
  				      dccp_packet_name(dh->dccph_type));
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
799
800
  			len = 0;
  			goto found_fin_ok;
0c8696207   Gerrit Renker   [DCCP]: Integrate...
801
802
803
804
  		default:
  			dccp_pr_debug("packet_type=%s
  ",
  				      dccp_packet_name(dh->dccph_type));
7bced3975   Dan Williams   net_dma: simple r...
805
  			sk_eat_skb(sk, skb);
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
806
  		}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
807
808
809
  verify_sock_status:
  		if (sock_flag(sk, SOCK_DONE)) {
  			len = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
810
  			break;
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
811
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
812

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
813
814
815
816
  		if (sk->sk_err) {
  			len = sock_error(sk);
  			break;
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
817

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
818
819
820
821
  		if (sk->sk_shutdown & RCV_SHUTDOWN) {
  			len = 0;
  			break;
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
822

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
823
824
825
826
827
828
  		if (sk->sk_state == DCCP_CLOSED) {
  			if (!sock_flag(sk, SOCK_DONE)) {
  				/* This occurs when user tries to read
  				 * from never connected socket.
  				 */
  				len = -ENOTCONN;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
829
830
  				break;
  			}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
831
832
  			len = 0;
  			break;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
833
  		}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
834
835
836
837
  		if (!timeo) {
  			len = -EAGAIN;
  			break;
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
838

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
839
840
841
842
  		if (signal_pending(current)) {
  			len = sock_intr_errno(timeo);
  			break;
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
843

dfbafc995   Sabrina Dubroca   tcp: fix recv wit...
844
  		sk_wait_data(sk, &timeo, NULL);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
845
  		continue;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
846
  	found_ok_skb:
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
847
848
849
850
  		if (len > skb->len)
  			len = skb->len;
  		else if (len < skb->len)
  			msg->msg_flags |= MSG_TRUNC;
51f3d02b9   David S. Miller   net: Add and use ...
851
  		if (skb_copy_datagram_msg(skb, 0, msg, len)) {
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
852
853
854
  			/* Exception. Bailout! */
  			len = -EFAULT;
  			break;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
855
  		}
55d955902   Gerrit Renker   dccp: support for...
856
857
  		if (flags & MSG_TRUNC)
  			len = skb->len;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
858
859
  	found_fin_ok:
  		if (!(flags & MSG_PEEK))
7bced3975   Dan Williams   net_dma: simple r...
860
  			sk_eat_skb(sk, skb);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
861
  		break;
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
862
  	} while (1);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
863
864
  out:
  	release_sock(sk);
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
865
  	return len;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
866
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
867
868
869
  EXPORT_SYMBOL_GPL(dccp_recvmsg);
  
  int inet_dccp_listen(struct socket *sock, int backlog)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  {
  	struct sock *sk = sock->sk;
  	unsigned char old_state;
  	int err;
  
  	lock_sock(sk);
  
  	err = -EINVAL;
  	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
  		goto out;
  
  	old_state = sk->sk_state;
  	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
  		goto out;
  
  	/* Really, if the socket is already in listen state
  	 * we can only allow the backlog to be adjusted.
  	 */
  	if (old_state != DCCP_LISTEN) {
  		/*
  		 * FIXME: here it probably should be sk->sk_prot->listen_start
  		 * see tcp_listen_start
  		 */
72a3effaf   Eric Dumazet   [NET]: Size liste...
893
  		err = dccp_listen_start(sk, backlog);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
894
895
896
897
898
899
900
901
902
903
  		if (err)
  			goto out;
  	}
  	sk->sk_max_ack_backlog = backlog;
  	err = 0;
  
  out:
  	release_sock(sk);
  	return err;
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
904
  EXPORT_SYMBOL_GPL(inet_dccp_listen);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
905
  static void dccp_terminate_connection(struct sock *sk)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
906
  {
0c8696207   Gerrit Renker   [DCCP]: Integrate...
907
  	u8 next_state = DCCP_CLOSED;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
908

0c8696207   Gerrit Renker   [DCCP]: Integrate...
909
910
911
912
913
914
915
916
917
918
919
920
  	switch (sk->sk_state) {
  	case DCCP_PASSIVE_CLOSE:
  	case DCCP_PASSIVE_CLOSEREQ:
  		dccp_finish_passive_close(sk);
  		break;
  	case DCCP_PARTOPEN:
  		dccp_pr_debug("Stop PARTOPEN timer (%p)
  ", sk);
  		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  		/* fall through */
  	case DCCP_OPEN:
  		dccp_send_close(sk, 1);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
921

b8599d207   Gerrit Renker   [DCCP]: Support f...
922
923
  		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
  		    !dccp_sk(sk)->dccps_server_timewait)
0c8696207   Gerrit Renker   [DCCP]: Integrate...
924
925
926
927
928
929
930
  			next_state = DCCP_ACTIVE_CLOSEREQ;
  		else
  			next_state = DCCP_CLOSING;
  		/* fall through */
  	default:
  		dccp_set_state(sk, next_state);
  	}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
931
932
933
934
  }
  
  void dccp_close(struct sock *sk, long timeout)
  {
97e5848dd   Ian McDonald   [DCCP]: Introduce...
935
  	struct dccp_sock *dp = dccp_sk(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
936
  	struct sk_buff *skb;
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
937
  	u32 data_was_unread = 0;
134af3463   Herbert Xu   [DCCP]: Fix sock_...
938
  	int state;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
939
940
941
942
943
944
945
946
947
948
949
950
951
  
  	lock_sock(sk);
  
  	sk->sk_shutdown = SHUTDOWN_MASK;
  
  	if (sk->sk_state == DCCP_LISTEN) {
  		dccp_set_state(sk, DCCP_CLOSED);
  
  		/* Special case. */
  		inet_csk_listen_stop(sk);
  
  		goto adjudge_to_death;
  	}
97e5848dd   Ian McDonald   [DCCP]: Introduce...
952
  	sk_stop_timer(sk, &dp->dccps_xmit_timer);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
953
954
955
956
957
  	/*
  	 * We need to flush the recv. buffs.  We do this only on the
  	 * descriptor close, not protocol-sourced closes, because the
  	  *reader process may not have drained the data yet!
  	 */
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
958
  	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
959
  		data_was_unread += skb->len;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
960
961
  		__kfree_skb(skb);
  	}
346da62cc   Eric Dumazet   dccp: do not send...
962
963
964
  	/* If socket has been already reset kill it. */
  	if (sk->sk_state == DCCP_CLOSED)
  		goto adjudge_to_death;
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
965
966
  	if (data_was_unread) {
  		/* Unread data was tossed, send an appropriate Reset Code */
2f34b3297   Gerrit Renker   dccp: cosmetics -...
967
968
  		DCCP_WARN("ABORT with %u bytes unread
  ", data_was_unread);
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
969
970
971
  		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  		dccp_set_state(sk, DCCP_CLOSED);
  	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
972
973
  		/* Check zero linger _after_ checking for unread data. */
  		sk->sk_prot->disconnect(sk, 0);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
974
  	} else if (sk->sk_state != DCCP_CLOSED) {
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
975
976
977
978
979
  		/*
  		 * Normal connection termination. May need to wait if there are
  		 * still packets in the TX queue that are delayed by the CCID.
  		 */
  		dccp_flush_write_queue(sk, &timeout);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
980
  		dccp_terminate_connection(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
981
  	}
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
982
983
984
985
986
987
988
  	/*
  	 * Flush write queue. This may be necessary in several cases:
  	 * - we have been closed by the peer but still have application data;
  	 * - abortive termination (unread data or zero linger time),
  	 * - normal termination but queue could not be flushed within time limit
  	 */
  	__skb_queue_purge(&sk->sk_write_queue);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
989
990
991
  	sk_stream_wait_close(sk, timeout);
  
  adjudge_to_death:
134af3463   Herbert Xu   [DCCP]: Fix sock_...
992
993
994
  	state = sk->sk_state;
  	sock_hold(sk);
  	sock_orphan(sk);
134af3463   Herbert Xu   [DCCP]: Fix sock_...
995

7ad07e7cf   Arnaldo Carvalho de Melo   [DCCP]: Implement...
996
997
998
  	/*
  	 * It is the last release_sock in its life. It will remove backlog.
  	 */
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
999
1000
1001
1002
1003
1004
1005
  	release_sock(sk);
  	/*
  	 * Now socket is owned by kernel and we acquire BH lock
  	 * to finish close. No need to check for user refs.
  	 */
  	local_bh_disable();
  	bh_lock_sock(sk);
547b792ca   Ilpo Järvinen   net: convert BUG_...
1006
  	WARN_ON(sock_owned_by_user(sk));
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1007

eb4dea585   Herbert Xu   net: Fix percpu c...
1008
  	percpu_counter_inc(sk->sk_prot->orphan_count);
134af3463   Herbert Xu   [DCCP]: Fix sock_...
1009
1010
1011
  	/* Have we already been destroyed by a softirq or backlog? */
  	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
  		goto out;
7ad07e7cf   Arnaldo Carvalho de Melo   [DCCP]: Implement...
1012

7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1013
1014
1015
1016
  	if (sk->sk_state == DCCP_CLOSED)
  		inet_csk_destroy_sock(sk);
  
  	/* Otherwise, socket is reprieved until protocol close. */
134af3463   Herbert Xu   [DCCP]: Fix sock_...
1017
  out:
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1018
1019
1020
1021
  	bh_unlock_sock(sk);
  	local_bh_enable();
  	sock_put(sk);
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1022
  EXPORT_SYMBOL_GPL(dccp_close);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1023
1024
  void dccp_shutdown(struct sock *sk, int how)
  {
8e8c71f1a   Gerrit Renker   [DCCP]: Honour an...
1025
1026
  	dccp_pr_debug("called shutdown(%x)
  ", how);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1027
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1028
  EXPORT_SYMBOL_GPL(dccp_shutdown);
0c5b8a462   Fabian Frederick   net/dccp/proto.c:...
1029
  static inline int __init dccp_mib_init(void)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1030
  {
698365fa1   WANG Cong   net: clean up snm...
1031
1032
1033
1034
  	dccp_statistics = alloc_percpu(struct dccp_mib);
  	if (!dccp_statistics)
  		return -ENOMEM;
  	return 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1035
  }
24e8b7e48   YOSHIFUJI Hideaki   [DCCP]: Use snmp_...
1036
  static inline void dccp_mib_exit(void)
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1037
  {
698365fa1   WANG Cong   net: clean up snm...
1038
  	free_percpu(dccp_statistics);
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1039
  }
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1040
1041
1042
  static int thash_entries;
  module_param(thash_entries, int, 0444);
  MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
a1d3a3551   Arnaldo Carvalho de Melo   [DCCP]: Fix spars...
1043
  #ifdef CONFIG_IP_DCCP_DEBUG
eb9399220   Rusty Russell   module_param: mak...
1044
  bool dccp_debug;
432649916   Gerrit Renker   dccp: Toggle debu...
1045
  module_param(dccp_debug, bool, 0644);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1046
  MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1047
1048
  
  EXPORT_SYMBOL_GPL(dccp_debug);
a1d3a3551   Arnaldo Carvalho de Melo   [DCCP]: Fix spars...
1049
  #endif
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1050
1051
1052
1053
1054
  
  static int __init dccp_init(void)
  {
  	unsigned long goal;
  	int ehash_order, bhash_order, i;
dd24c0019   Eric Dumazet   net: Use a percpu...
1055
  	int rc;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1056

028b02752   Patrick McHardy   [DCCP]: Fix skb->...
1057
1058
  	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
  		     FIELD_SIZEOF(struct sk_buff, cb));
908c7f194   Tejun Heo   percpu_counter: a...
1059
  	rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL);
dd24c0019   Eric Dumazet   net: Use a percpu...
1060
  	if (rc)
d14a0ebda   Gerrit Renker   net-2.6 [Bug-Fix]...
1061
  		goto out_fail;
dd24c0019   Eric Dumazet   net: Use a percpu...
1062
  	rc = -ENOBUFS;
5caea4ea7   Eric Dumazet   net: listening_ha...
1063
  	inet_hashinfo_init(&dccp_hashinfo);
7690af3ff   Arnaldo Carvalho de Melo   [DCCP]: Just refl...
1064
1065
1066
  	dccp_hashinfo.bind_bucket_cachep =
  		kmem_cache_create("dccp_bind_bucket",
  				  sizeof(struct inet_bind_bucket), 0,
20c2df83d   Paul Mundt   mm: Remove slab d...
1067
  				  SLAB_HWCACHE_ALIGN, NULL);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1068
  	if (!dccp_hashinfo.bind_bucket_cachep)
dd24c0019   Eric Dumazet   net: Use a percpu...
1069
  		goto out_free_percpu;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1070
1071
1072
1073
1074
1075
1076
  
  	/*
  	 * Size and allocate the main established and bind bucket
  	 * hash tables.
  	 *
  	 * The methodology is similar to that of the buffer cache.
  	 */
4481374ce   Jan Beulich   mm: replace vario...
1077
1078
  	if (totalram_pages >= (128 * 1024))
  		goal = totalram_pages >> (21 - PAGE_SHIFT);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1079
  	else
4481374ce   Jan Beulich   mm: replace vario...
1080
  		goal = totalram_pages >> (23 - PAGE_SHIFT);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1081
1082
  
  	if (thash_entries)
7690af3ff   Arnaldo Carvalho de Melo   [DCCP]: Just refl...
1083
1084
  		goal = (thash_entries *
  			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1085
1086
1087
  	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
  		;
  	do {
f373b53b5   Eric Dumazet   tcp: replace ehas...
1088
  		unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1089
  					sizeof(struct inet_ehash_bucket);
f373b53b5   Eric Dumazet   tcp: replace ehas...
1090
1091
1092
1093
  
  		while (hash_size & (hash_size - 1))
  			hash_size--;
  		dccp_hashinfo.ehash_mask = hash_size - 1;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1094
  		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1c29b3ff4   Mel Gorman   net-dccp: suppres...
1095
  			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1096
1097
1098
  	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
  
  	if (!dccp_hashinfo.ehash) {
59348b19e   Gerrit Renker   [DCCP]: Simplifie...
1099
  		DCCP_CRIT("Failed to allocate DCCP established hash table");
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1100
1101
  		goto out_free_bind_bucket_cachep;
  	}
05dbc7b59   Eric Dumazet   tcp/dccp: remove ...
1102
  	for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
3ab5aee7f   Eric Dumazet   net: Convert TCP ...
1103
  		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1104

230140cff   Eric Dumazet   [INET]: Remove pe...
1105
1106
  	if (inet_ehash_locks_alloc(&dccp_hashinfo))
  			goto out_free_dccp_ehash;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1107
1108
1109
1110
1111
  	bhash_order = ehash_order;
  
  	do {
  		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
  					sizeof(struct inet_bind_hashbucket);
7690af3ff   Arnaldo Carvalho de Melo   [DCCP]: Just refl...
1112
1113
  		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
  		    bhash_order > 0)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1114
1115
  			continue;
  		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1c29b3ff4   Mel Gorman   net-dccp: suppres...
1116
  			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1117
1118
1119
  	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
  
  	if (!dccp_hashinfo.bhash) {
59348b19e   Gerrit Renker   [DCCP]: Simplifie...
1120
  		DCCP_CRIT("Failed to allocate DCCP bind hash table");
230140cff   Eric Dumazet   [INET]: Remove pe...
1121
  		goto out_free_dccp_locks;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1122
1123
1124
1125
1126
1127
  	}
  
  	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
  		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
  		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
  	}
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1128
  	rc = dccp_mib_init();
fa23e2ecd   Arnaldo Carvalho de Melo   [DCCP]: Fix error...
1129
  	if (rc)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1130
  		goto out_free_dccp_bhash;
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1131
  	rc = dccp_ackvec_init();
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1132
  	if (rc)
b61fafc4e   Arnaldo Carvalho de Melo   [DCCP]: Move the ...
1133
  		goto out_free_dccp_mib;
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1134

e55d912f5   Arnaldo Carvalho de Melo   [DCCP] feat: Intr...
1135
  	rc = dccp_sysctl_init();
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1136
1137
  	if (rc)
  		goto out_ackvec_exit;
4c70f383e   Gerrit Renker   [DCCP]: Provide 1...
1138

ddebc973c   Gerrit Renker   dccp: Lockless in...
1139
1140
1141
  	rc = ccid_initialize_builtins();
  	if (rc)
  		goto out_sysctl_exit;
4c70f383e   Gerrit Renker   [DCCP]: Provide 1...
1142
  	dccp_timestamping_init();
d14a0ebda   Gerrit Renker   net-2.6 [Bug-Fix]...
1143
1144
  
  	return 0;
ddebc973c   Gerrit Renker   dccp: Lockless in...
1145
1146
  out_sysctl_exit:
  	dccp_sysctl_exit();
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1147
1148
  out_ackvec_exit:
  	dccp_ackvec_exit();
b61fafc4e   Arnaldo Carvalho de Melo   [DCCP]: Move the ...
1149
  out_free_dccp_mib:
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1150
  	dccp_mib_exit();
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1151
1152
  out_free_dccp_bhash:
  	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
230140cff   Eric Dumazet   [INET]: Remove pe...
1153
1154
  out_free_dccp_locks:
  	inet_ehash_locks_free(&dccp_hashinfo);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1155
1156
  out_free_dccp_ehash:
  	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1157
1158
  out_free_bind_bucket_cachep:
  	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
dd24c0019   Eric Dumazet   net: Use a percpu...
1159
1160
  out_free_percpu:
  	percpu_counter_destroy(&dccp_orphan_count);
d14a0ebda   Gerrit Renker   net-2.6 [Bug-Fix]...
1161
1162
1163
1164
1165
  out_fail:
  	dccp_hashinfo.bhash = NULL;
  	dccp_hashinfo.ehash = NULL;
  	dccp_hashinfo.bind_bucket_cachep = NULL;
  	return rc;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1166
  }
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1167
1168
  static void __exit dccp_fini(void)
  {
ddebc973c   Gerrit Renker   dccp: Lockless in...
1169
  	ccid_cleanup_builtins();
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1170
  	dccp_mib_exit();
725ba8eee   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
1171
1172
1173
1174
  	free_pages((unsigned long)dccp_hashinfo.bhash,
  		   get_order(dccp_hashinfo.bhash_size *
  			     sizeof(struct inet_bind_hashbucket)));
  	free_pages((unsigned long)dccp_hashinfo.ehash,
f373b53b5   Eric Dumazet   tcp: replace ehas...
1175
  		   get_order((dccp_hashinfo.ehash_mask + 1) *
725ba8eee   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
1176
  			     sizeof(struct inet_ehash_bucket)));
230140cff   Eric Dumazet   [INET]: Remove pe...
1177
  	inet_ehash_locks_free(&dccp_hashinfo);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1178
  	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1179
  	dccp_ackvec_exit();
e55d912f5   Arnaldo Carvalho de Melo   [DCCP] feat: Intr...
1180
  	dccp_sysctl_exit();
476181cb0   Wei Yongjun   dccp: missing des...
1181
  	percpu_counter_destroy(&dccp_orphan_count);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1182
1183
1184
1185
  }
  
  module_init(dccp_init);
  module_exit(dccp_fini);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1186
1187
1188
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
  MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");