Blame view

net/dccp/proto.c 30 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>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
25
26
  #include <net/sock.h>
  #include <net/xfrm.h>
6273172e1   Arnaldo Carvalho de Melo   [DCCP]: Implement...
27
  #include <asm/ioctls.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
28
29
30
31
  #include <linux/spinlock.h>
  #include <linux/timer.h>
  #include <linux/delay.h>
  #include <linux/poll.h>
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
32
33
34
  
  #include "ccid.h"
  #include "dccp.h"
afe00251d   Andrea Bittau   [DCCP]: Initial f...
35
  #include "feat.h"
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
36

ba89966c1   Eric Dumazet   [NET]: use __read...
37
  DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
38

f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
39
  EXPORT_SYMBOL_GPL(dccp_statistics);
dd24c0019   Eric Dumazet   net: Use a percpu...
40
  struct percpu_counter dccp_orphan_count;
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
41
  EXPORT_SYMBOL_GPL(dccp_orphan_count);
5caea4ea7   Eric Dumazet   net: listening_ha...
42
  struct inet_hashinfo dccp_hashinfo;
075ae8661   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
43
  EXPORT_SYMBOL_GPL(dccp_hashinfo);
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
44
45
  /* 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...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #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 ...
69
70
71
  void dccp_set_state(struct sock *sk, const int state)
  {
  	const int oldstate = sk->sk_state;
f11135a34   Gerrit Renker   [DCCP]: Dedicated...
72
73
  	dccp_pr_debug("%s(%p)  %s  -->  %s
  ", dccp_role(sk), sk,
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
74
75
76
77
78
79
80
  		      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...
81
82
83
  		/* 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 ...
84
85
86
  		break;
  
  	case DCCP_CLOSED:
0c8696207   Gerrit Renker   [DCCP]: Integrate...
87
88
  		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
  		    oldstate == DCCP_CLOSING)
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
89
90
91
92
93
  			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...
94
  			inet_put_port(sk);
c25a18ba3   Arnaldo Carvalho de Melo   [DCCP]: Uninline ...
95
96
97
98
99
100
101
102
103
104
105
106
107
  		/* 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.
  	 */
  	sk->sk_state = state;
  }
  
  EXPORT_SYMBOL_GPL(dccp_set_state);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  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 ...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  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...
139
140
  const char *dccp_packet_name(const int type)
  {
36cbd3dcc   Jan Engelhardt   net: mark read-on...
141
  	static const char *const dccp_packet_names[] = {
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  		[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);
724788735   Arnaldo Carvalho de Melo   [DCCP] ipv6: Add ...
161
  int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
162
163
164
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  	struct inet_connection_sock *icsk = inet_csk(sk);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
165

e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
166
167
168
169
170
  	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;
  	icsk->icsk_sync_mss	= dccp_sync_mss;
410e27a49   Gerrit Renker   This reverts "Mer...
171
  	dp->dccps_mss_cache	= 536;
e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
172
173
174
  	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...
175
  	dp->dccps_tx_qlen	= sysctl_dccp_tx_qlen;
e18d7a985   Arnaldo Carvalho de Melo   [DCCP]: Initializ...
176
177
  
  	dccp_init_xmit_timers(sk);
ac75773c2   Gerrit Renker   dccp: Per-socket ...
178
  	INIT_LIST_HEAD(&dp->dccps_featneg);
6eb55d172   Gerrit Renker   dccp: Integration...
179
180
181
  	/* 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...
182
183
184
185
  	return 0;
  }
  
  EXPORT_SYMBOL_GPL(dccp_init_sock);
7d06b2e05   Brian Haley   net: change proto...
186
  void dccp_destroy_sock(struct sock *sk)
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  
  	/*
  	 * DCCP doesn't use sk_write_queue, just sk_send_head
  	 * for retransmissions
  	 */
  	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...
201
  		inet_put_port(sk);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
202
203
204
  
  	kfree(dp->dccps_service_list);
  	dp->dccps_service_list = NULL;
6fdd34d43   Gerrit Renker   dccp ccid-2: Phas...
205
  	if (dp->dccps_hc_rx_ackvec != NULL) {
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
206
207
208
209
210
211
212
213
  		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  		dp->dccps_hc_rx_ackvec = NULL;
  	}
  	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  
  	/* clean up feature negotiation state */
d99a7bd21   Gerrit Renker   dccp: Cleanup rou...
214
  	dccp_feat_list_purge(&dp->dccps_featneg);
3e0fadc51   Arnaldo Carvalho de Melo   [DCCP]: Move dccp...
215
216
217
  }
  
  EXPORT_SYMBOL_GPL(dccp_destroy_sock);
72a3effaf   Eric Dumazet   [NET]: Size liste...
218
  static inline int dccp_listen_start(struct sock *sk, int backlog)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
219
  {
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
220
221
222
  	struct dccp_sock *dp = dccp_sk(sk);
  
  	dp->dccps_role = DCCP_ROLE_LISTEN;
9eca0a47d   Gerrit Renker   dccp: Resolve dep...
223
224
225
  	/* do not start to listen if feature negotiation setup fails */
  	if (dccp_feat_finalise_settings(dp))
  		return -EPROTO;
72a3effaf   Eric Dumazet   [NET]: Size liste...
226
  	return inet_csk_listen_start(sk, backlog);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
227
  }
ce865a61c   Gerrit Renker   [DCCP]: Add suppo...
228
229
230
231
232
  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...
233
234
235
236
237
238
239
240
241
  int dccp_disconnect(struct sock *sk, int flags)
  {
  	struct inet_connection_sock *icsk = inet_csk(sk);
  	struct inet_sock *inet = inet_sk(sk);
  	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...
242
243
244
245
  	/*
  	 * 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...
246
247
  	if (old_state == DCCP_LISTEN) {
  		inet_csk_listen_stop(sk);
ce865a61c   Gerrit Renker   [DCCP]: Add suppo...
248
249
250
  	} 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...
251
252
253
254
  	} else if (old_state == DCCP_REQUESTING)
  		sk->sk_err = ECONNRESET;
  
  	dccp_clear_xmit_timers(sk);
48816322a   Gerrit Renker   dccp: Empty the w...
255

7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
256
  	__skb_queue_purge(&sk->sk_receive_queue);
48816322a   Gerrit Renker   dccp: Empty the w...
257
  	__skb_queue_purge(&sk->sk_write_queue);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
258
259
260
261
  	if (sk->sk_send_head != NULL) {
  		__kfree_skb(sk->sk_send_head);
  		sk->sk_send_head = NULL;
  	}
c720c7e83   Eric Dumazet   inet: rename some...
262
  	inet->inet_dport = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
263
264
265
266
267
268
269
270
271
272
  
  	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...
273
  	WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
274
275
276
277
  
  	sk->sk_error_report(sk);
  	return err;
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
278
  EXPORT_SYMBOL_GPL(dccp_disconnect);
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
279
280
281
282
283
284
285
  /*
   *	Wait for a DCCP event.
   *
   *	Note that we don't need to lock the socket, as the upper poll layers
   *	take care of normal races (between the test and the event) and we don't
   *	go look at any of the socket buffers directly.
   */
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
286
287
  unsigned int dccp_poll(struct file *file, struct socket *sock,
  		       poll_table *wait)
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
288
289
290
  {
  	unsigned int mask;
  	struct sock *sk = sock->sk;
aa3951451   Eric Dumazet   net: sk_sleep() h...
291
  	sock_poll_wait(file, sk_sleep(sk), wait);
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  	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)
  		mask = POLLERR;
  
  	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
  		mask |= POLLHUP;
  	if (sk->sk_shutdown & RCV_SHUTDOWN)
f348d70a3   Davide Libenzi   [PATCH] POLLRDHUP...
307
  		mask |= POLLIN | POLLRDNORM | POLLRDHUP;
331968bd0   Arnaldo Carvalho de Melo   [DCCP]: Initial d...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  
  	/* Connected? */
  	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
  		if (atomic_read(&sk->sk_rmem_alloc) > 0)
  			mask |= POLLIN | POLLRDNORM;
  
  		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  			if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
  				mask |= POLLOUT | POLLWRNORM;
  			} else {  /* send SIGIO later */
  				set_bit(SOCK_ASYNC_NOSPACE,
  					&sk->sk_socket->flags);
  				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.
  				 */
  				if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
  					mask |= POLLOUT | POLLWRNORM;
  			}
  		}
  	}
  	return mask;
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
333
  EXPORT_SYMBOL_GPL(dccp_poll);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
334
335
  int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  {
6273172e1   Arnaldo Carvalho de Melo   [DCCP]: Implement...
336
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
  	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...
366
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
367
  EXPORT_SYMBOL_GPL(dccp_ioctl);
60fe62e78   Andrea Bittau   [DCCP]: sparse en...
368
  static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
b7058842c   David S. Miller   net: Make setsock...
369
  				   char __user *optval, unsigned int optlen)
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
370
371
372
  {
  	struct dccp_sock *dp = dccp_sk(sk);
  	struct dccp_service_list *sl = NULL;
8109b02b5   Arnaldo Carvalho de Melo   [DCCP]: Whitespac...
373
  	if (service == DCCP_SERVICE_INVALID_VALUE ||
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  	    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
394
  	kfree(dp->dccps_service_list);
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
395
396
397
398
399
  
  	dp->dccps_service_list = sl;
  	release_sock(sk);
  	return 0;
  }
294505598   Gerrit Renker   dccp: Feature neg...
400
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
  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...
435
  static int dccp_setsockopt_ccid(struct sock *sk, int type,
b7058842c   David S. Miller   net: Make setsock...
436
  				char __user *optval, unsigned int optlen)
b20a9c24d   Gerrit Renker   dccp: Set per-con...
437
438
439
440
441
442
  {
  	u8 *val;
  	int rc = 0;
  
  	if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
  		return -EINVAL;
042604d2a   Julia Lawall   net/dccp: Use mem...
443
444
445
  	val = memdup_user(optval, optlen);
  	if (IS_ERR(val))
  		return PTR_ERR(val);
b20a9c24d   Gerrit Renker   dccp: Set per-con...
446
447
448
449
450
451
452
453
454
455
456
457
  
  	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...
458
  static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
b7058842c   David S. Miller   net: Make setsock...
459
  		char __user *optval, unsigned int optlen)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
460
  {
09dbc3895   Gerrit Renker   [DCCP]: Miscellan...
461
462
  	struct dccp_sock *dp = dccp_sk(sk);
  	int val, err = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
463

191029963   Gerrit Renker   dccp: Tidy up set...
464
465
466
467
468
469
470
471
472
473
  	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...
474
475
476
477
  	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...
478
479
480
  	}
  
  	if (optlen < (int)sizeof(int))
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
481
482
483
484
  		return -EINVAL;
  
  	if (get_user(val, (int __user *)optval))
  		return -EFAULT;
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
485
486
  	if (optname == DCCP_SOCKOPT_SERVICE)
  		return dccp_setsockopt_service(sk, val, optval, optlen);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
487

67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
488
  	lock_sock(sk);
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
489
  	switch (optname) {
b8599d207   Gerrit Renker   [DCCP]: Support f...
490
491
492
493
494
495
  	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...
496
497
  	case DCCP_SOCKOPT_SEND_CSCOV:
  		err = dccp_setsockopt_cscov(sk, val, false);
d6da3511d   Tomasz Grobelny   dccp: Policy-base...
498
  		break;
294505598   Gerrit Renker   dccp: Feature neg...
499
500
  	case DCCP_SOCKOPT_RECV_CSCOV:
  		err = dccp_setsockopt_cscov(sk, val, true);
d6da3511d   Tomasz Grobelny   dccp: Policy-base...
501
  		break;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	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...
516
517
518
519
  	default:
  		err = -ENOPROTOOPT;
  		break;
  	}
410e27a49   Gerrit Renker   This reverts "Mer...
520
  	release_sock(sk);
191029963   Gerrit Renker   dccp: Tidy up set...
521

a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
522
  	return err;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
523
  }
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
524
  int dccp_setsockopt(struct sock *sk, int level, int optname,
b7058842c   David S. Miller   net: Make setsock...
525
  		    char __user *optval, unsigned int optlen)
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
526
527
528
529
530
531
532
  {
  	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...
533

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

3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
545
546
  EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
  #endif
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
547
  static int dccp_getsockopt_service(struct sock *sk, int len,
60fe62e78   Andrea Bittau   [DCCP]: sparse en...
548
  				   __be32 __user *optval,
67e6b6292   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
549
550
551
552
553
554
555
  				   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...
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  	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...
574
  static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
a1d3a3551   Arnaldo Carvalho de Melo   [DCCP]: Fix spars...
575
  		    char __user *optval, int __user *optlen)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
576
  {
a84ffe430   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
577
578
  	struct dccp_sock *dp;
  	int val, len;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
579

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

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

3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
661
662
  EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
  #endif
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
  static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
  {
  	struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
  
  	/*
  	 * 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;
  
  	for (; cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  
  		if (!CMSG_OK(msg, cmsg))
  			return -EINVAL;
  
  		if (cmsg->cmsg_level != SOL_DCCP)
  			continue;
049102650   Tomasz Grobelny   dccp qpolicy: Par...
686
687
688
  		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...
689
690
691
692
693
694
695
696
697
698
699
700
  		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;
  }
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  		 size_t len)
  {
  	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;
  
  	if (len > dp->dccps_mss_cache)
  		return -EMSGSIZE;
  
  	lock_sock(sk);
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
715

871a2c16c   Tomasz Grobelny   dccp: Policy-base...
716
  	if (dccp_qpolicy_full(sk)) {
b1308dc01   Ian McDonald   [DCCP]: Set TX Qu...
717
718
719
  		rc = -EAGAIN;
  		goto out_release;
  	}
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
720
  	timeo = sock_sndtimeo(sk, noblock);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
721
722
723
724
725
726
  
  	/*
  	 * 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...
727
  	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
728
  		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
729
  			goto out_release;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
730
731
732
733
734
  
  	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...
735
736
737
738
739
  	if (skb == NULL)
  		goto out_release;
  
  	skb_reserve(skb, sk->sk_prot->max_header);
  	rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
740
741
  	if (rc != 0)
  		goto out_discard;
871a2c16c   Tomasz Grobelny   dccp: Policy-base...
742
743
744
745
746
  	rc = dccp_msghdr_parse(msg, skb);
  	if (rc != 0)
  		goto out_discard;
  
  	dccp_qpolicy_push(sk, skb);
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
747
748
749
750
751
752
753
  	/*
  	 * 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...
754
755
756
  out_release:
  	release_sock(sk);
  	return rc ? : len;
27258ee54   Arnaldo Carvalho de Melo   [DCCP]: Introduce...
757
758
  out_discard:
  	kfree_skb(skb);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
759
  	goto out_release;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
760
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
761
  EXPORT_SYMBOL_GPL(dccp_sendmsg);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
762
763
764
765
  int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  		 size_t len, int nonblock, int flags, int *addr_len)
  {
  	const struct dccp_hdr *dh;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
766
767
768
  	long timeo;
  
  	lock_sock(sk);
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
769
770
  	if (sk->sk_state == DCCP_LISTEN) {
  		len = -ENOTCONN;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
771
  		goto out;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
772
  	}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
773

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

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

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

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

0c8696207   Gerrit Renker   [DCCP]: Integrate...
789
790
791
792
793
794
795
796
797
  		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...
798
799
  			len = 0;
  			goto found_fin_ok;
0c8696207   Gerrit Renker   [DCCP]: Integrate...
800
801
802
803
  		default:
  			dccp_pr_debug("packet_type=%s
  ",
  				      dccp_packet_name(dh->dccph_type));
dc6b9b782   Eric Dumazet   net: include/net/...
804
  			sk_eat_skb(sk, skb, false);
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
805
  		}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
806
807
808
  verify_sock_status:
  		if (sock_flag(sk, SOCK_DONE)) {
  			len = 0;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
809
  			break;
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
810
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
811

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

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

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
822
823
824
825
826
827
  		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...
828
829
  				break;
  			}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
830
831
  			len = 0;
  			break;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
832
  		}
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
833
834
835
836
  		if (!timeo) {
  			len = -EAGAIN;
  			break;
  		}
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
837

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

531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
843
  		sk_wait_data(sk, &timeo);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
844
  		continue;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
845
  	found_ok_skb:
531669a0a   Arnaldo Carvalho de Melo   [DCCP]: Rewrite d...
846
847
848
849
850
851
852
853
854
  		if (len > skb->len)
  			len = skb->len;
  		else if (len < skb->len)
  			msg->msg_flags |= MSG_TRUNC;
  
  		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
  			/* 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))
dc6b9b782   Eric Dumazet   net: include/net/...
860
  			sk_eat_skb(sk, skb, false);
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);
  	}
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
962
963
  	if (data_was_unread) {
  		/* Unread data was tossed, send an appropriate Reset Code */
2f34b3297   Gerrit Renker   dccp: cosmetics -...
964
965
  		DCCP_WARN("ABORT with %u bytes unread
  ", data_was_unread);
d83bd95bf   Gerrit Renker   [DCCP]: Check for...
966
967
968
  		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...
969
970
  		/* Check zero linger _after_ checking for unread data. */
  		sk->sk_prot->disconnect(sk, 0);
0c8696207   Gerrit Renker   [DCCP]: Integrate...
971
  	} else if (sk->sk_state != DCCP_CLOSED) {
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
972
973
974
975
976
  		/*
  		 * 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...
977
  		dccp_terminate_connection(sk);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
978
  	}
b1fcf55ee   Gerrit Renker   dccp: Refine the ...
979
980
981
982
983
984
985
  	/*
  	 * 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...
986
987
988
  	sk_stream_wait_close(sk, timeout);
  
  adjudge_to_death:
134af3463   Herbert Xu   [DCCP]: Fix sock_...
989
990
991
  	state = sk->sk_state;
  	sock_hold(sk);
  	sock_orphan(sk);
134af3463   Herbert Xu   [DCCP]: Fix sock_...
992

7ad07e7cf   Arnaldo Carvalho de Melo   [DCCP]: Implement...
993
994
995
  	/*
  	 * It is the last release_sock in its life. It will remove backlog.
  	 */
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
996
997
998
999
1000
1001
1002
  	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_...
1003
  	WARN_ON(sock_owned_by_user(sk));
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1004

eb4dea585   Herbert Xu   net: Fix percpu c...
1005
  	percpu_counter_inc(sk->sk_prot->orphan_count);
134af3463   Herbert Xu   [DCCP]: Fix sock_...
1006
1007
1008
  	/* 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...
1009

7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1010
1011
1012
1013
  	if (sk->sk_state == DCCP_CLOSED)
  		inet_csk_destroy_sock(sk);
  
  	/* Otherwise, socket is reprieved until protocol close. */
134af3463   Herbert Xu   [DCCP]: Fix sock_...
1014
  out:
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1015
1016
1017
1018
  	bh_unlock_sock(sk);
  	local_bh_enable();
  	sock_put(sk);
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1019
  EXPORT_SYMBOL_GPL(dccp_close);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1020
1021
  void dccp_shutdown(struct sock *sk, int how)
  {
8e8c71f1a   Gerrit Renker   [DCCP]: Honour an...
1022
1023
  	dccp_pr_debug("called shutdown(%x)
  ", how);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1024
  }
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1025
  EXPORT_SYMBOL_GPL(dccp_shutdown);
24e8b7e48   YOSHIFUJI Hideaki   [DCCP]: Use snmp_...
1026
  static inline int dccp_mib_init(void)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1027
  {
7d720c3e4   Tejun Heo   percpu: add __per...
1028
  	return snmp_mib_init((void __percpu **)dccp_statistics,
1823e4c80   Eric Dumazet   snmp: add align p...
1029
1030
  			     sizeof(struct dccp_mib),
  			     __alignof__(struct dccp_mib));
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1031
  }
24e8b7e48   YOSHIFUJI Hideaki   [DCCP]: Use snmp_...
1032
  static inline void dccp_mib_exit(void)
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1033
  {
7d720c3e4   Tejun Heo   percpu: add __per...
1034
  	snmp_mib_free((void __percpu **)dccp_statistics);
46f09ffa7   Arnaldo Carvalho de Melo   [DCCP]: Rename in...
1035
  }
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1036
1037
1038
  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...
1039
  #ifdef CONFIG_IP_DCCP_DEBUG
eb9399220   Rusty Russell   module_param: mak...
1040
  bool dccp_debug;
432649916   Gerrit Renker   dccp: Toggle debu...
1041
  module_param(dccp_debug, bool, 0644);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1042
  MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
f21e68caa   Arnaldo Carvalho de Melo   [DCCP]: Prepare t...
1043
1044
  
  EXPORT_SYMBOL_GPL(dccp_debug);
a1d3a3551   Arnaldo Carvalho de Melo   [DCCP]: Fix spars...
1045
  #endif
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1046
1047
1048
1049
1050
  
  static int __init dccp_init(void)
  {
  	unsigned long goal;
  	int ehash_order, bhash_order, i;
dd24c0019   Eric Dumazet   net: Use a percpu...
1051
  	int rc;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1052

028b02752   Patrick McHardy   [DCCP]: Fix skb->...
1053
1054
  	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
  		     FIELD_SIZEOF(struct sk_buff, cb));
dd24c0019   Eric Dumazet   net: Use a percpu...
1055
1056
  	rc = percpu_counter_init(&dccp_orphan_count, 0);
  	if (rc)
d14a0ebda   Gerrit Renker   net-2.6 [Bug-Fix]...
1057
  		goto out_fail;
dd24c0019   Eric Dumazet   net: Use a percpu...
1058
  	rc = -ENOBUFS;
5caea4ea7   Eric Dumazet   net: listening_ha...
1059
  	inet_hashinfo_init(&dccp_hashinfo);
7690af3ff   Arnaldo Carvalho de Melo   [DCCP]: Just refl...
1060
1061
1062
  	dccp_hashinfo.bind_bucket_cachep =
  		kmem_cache_create("dccp_bind_bucket",
  				  sizeof(struct inet_bind_bucket), 0,
20c2df83d   Paul Mundt   mm: Remove slab d...
1063
  				  SLAB_HWCACHE_ALIGN, NULL);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1064
  	if (!dccp_hashinfo.bind_bucket_cachep)
dd24c0019   Eric Dumazet   net: Use a percpu...
1065
  		goto out_free_percpu;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1066
1067
1068
1069
1070
1071
1072
  
  	/*
  	 * 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...
1073
1074
  	if (totalram_pages >= (128 * 1024))
  		goal = totalram_pages >> (21 - PAGE_SHIFT);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1075
  	else
4481374ce   Jan Beulich   mm: replace vario...
1076
  		goal = totalram_pages >> (23 - PAGE_SHIFT);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1077
1078
  
  	if (thash_entries)
7690af3ff   Arnaldo Carvalho de Melo   [DCCP]: Just refl...
1079
1080
  		goal = (thash_entries *
  			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1081
1082
1083
  	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
  		;
  	do {
f373b53b5   Eric Dumazet   tcp: replace ehas...
1084
  		unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1085
  					sizeof(struct inet_ehash_bucket);
f373b53b5   Eric Dumazet   tcp: replace ehas...
1086
1087
1088
1089
  
  		while (hash_size & (hash_size - 1))
  			hash_size--;
  		dccp_hashinfo.ehash_mask = hash_size - 1;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1090
  		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1c29b3ff4   Mel Gorman   net-dccp: suppres...
1091
  			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1092
1093
1094
  	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
  
  	if (!dccp_hashinfo.ehash) {
59348b19e   Gerrit Renker   [DCCP]: Simplifie...
1095
  		DCCP_CRIT("Failed to allocate DCCP established hash table");
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1096
1097
  		goto out_free_bind_bucket_cachep;
  	}
f373b53b5   Eric Dumazet   tcp: replace ehas...
1098
  	for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) {
3ab5aee7f   Eric Dumazet   net: Convert TCP ...
1099
1100
  		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
  		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1101
  	}
230140cff   Eric Dumazet   [INET]: Remove pe...
1102
1103
  	if (inet_ehash_locks_alloc(&dccp_hashinfo))
  			goto out_free_dccp_ehash;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1104
1105
1106
1107
1108
  	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...
1109
1110
  		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
  		    bhash_order > 0)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1111
1112
  			continue;
  		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1c29b3ff4   Mel Gorman   net-dccp: suppres...
1113
  			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1114
1115
1116
  	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
  
  	if (!dccp_hashinfo.bhash) {
59348b19e   Gerrit Renker   [DCCP]: Simplifie...
1117
  		DCCP_CRIT("Failed to allocate DCCP bind hash table");
230140cff   Eric Dumazet   [INET]: Remove pe...
1118
  		goto out_free_dccp_locks;
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1119
1120
1121
1122
1123
1124
  	}
  
  	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...
1125
  	rc = dccp_mib_init();
fa23e2ecd   Arnaldo Carvalho de Melo   [DCCP]: Fix error...
1126
  	if (rc)
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1127
  		goto out_free_dccp_bhash;
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1128
  	rc = dccp_ackvec_init();
7c657876b   Arnaldo Carvalho de Melo   [DCCP]: Initial i...
1129
  	if (rc)
b61fafc4e   Arnaldo Carvalho de Melo   [DCCP]: Move the ...
1130
  		goto out_free_dccp_mib;
9b07ef5dd   Arnaldo Carvalho de Melo   [DCCP] ackvec: In...
1131

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

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