blob: 0941f8fe1675bb5160a5bbaf91d3accc7f3347be [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/proto.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070012#include <linux/dccp.h>
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/skbuff.h>
18#include <linux/netdevice.h>
19#include <linux/in.h>
20#include <linux/if_arp.h>
21#include <linux/init.h>
22#include <linux/random.h>
23#include <net/checksum.h>
24
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <net/inet_sock.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070026#include <net/sock.h>
27#include <net/xfrm.h>
28
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -070029#include <asm/ioctls.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070030#include <linux/spinlock.h>
31#include <linux/timer.h>
32#include <linux/delay.h>
33#include <linux/poll.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070034
35#include "ccid.h"
36#include "dccp.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080037#include "feat.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070038
Eric Dumazetba899662005-08-26 12:05:31 -070039DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070040
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080041EXPORT_SYMBOL_GPL(dccp_statistics);
42
Eric Dumazetdd24c002008-11-25 21:17:14 -080043struct percpu_counter dccp_orphan_count;
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080044EXPORT_SYMBOL_GPL(dccp_orphan_count);
45
Eric Dumazet5caea4e2008-11-20 00:40:07 -080046struct inet_hashinfo dccp_hashinfo;
Arnaldo Carvalho de Melo075ae862006-03-20 21:24:19 -080047EXPORT_SYMBOL_GPL(dccp_hashinfo);
48
Ian McDonaldb1308dc2006-11-20 18:30:17 -020049/* the maximum queue length for tx in packets. 0 is no limit */
50int sysctl_dccp_tx_qlen __read_mostly = 5;
51
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080052void dccp_set_state(struct sock *sk, const int state)
53{
54 const int oldstate = sk->sk_state;
55
Gerrit Renkerf11135a2007-11-28 11:34:53 -020056 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080057 dccp_state_name(oldstate), dccp_state_name(state));
58 WARN_ON(state == oldstate);
59
60 switch (state) {
61 case DCCP_OPEN:
62 if (oldstate != DCCP_OPEN)
63 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
Gerrit Renker6eb55d12008-12-08 01:15:26 -080064 /* Client retransmits all Confirm options until entering OPEN */
65 if (oldstate == DCCP_PARTOPEN)
66 dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080067 break;
68
69 case DCCP_CLOSED:
Gerrit Renker0c869622007-11-28 11:59:48 -020070 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
71 oldstate == DCCP_CLOSING)
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080072 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
73
74 sk->sk_prot->unhash(sk);
75 if (inet_csk(sk)->icsk_bind_hash != NULL &&
76 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -080077 inet_put_port(sk);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080078 /* fall through */
79 default:
80 if (oldstate == DCCP_OPEN)
81 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
82 }
83
84 /* Change state AFTER socket is unhashed to avoid closed
85 * socket sitting in hash tables.
86 */
87 sk->sk_state = state;
88}
89
90EXPORT_SYMBOL_GPL(dccp_set_state);
91
Gerrit Renker0c869622007-11-28 11:59:48 -020092static void dccp_finish_passive_close(struct sock *sk)
93{
94 switch (sk->sk_state) {
95 case DCCP_PASSIVE_CLOSE:
96 /* Node (client or server) has received Close packet. */
97 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
98 dccp_set_state(sk, DCCP_CLOSED);
99 break;
100 case DCCP_PASSIVE_CLOSEREQ:
101 /*
102 * Client received CloseReq. We set the `active' flag so that
103 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
104 */
105 dccp_send_close(sk, 1);
106 dccp_set_state(sk, DCCP_CLOSING);
107 }
108}
109
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800110void dccp_done(struct sock *sk)
111{
112 dccp_set_state(sk, DCCP_CLOSED);
113 dccp_clear_xmit_timers(sk);
114
115 sk->sk_shutdown = SHUTDOWN_MASK;
116
117 if (!sock_flag(sk, SOCK_DEAD))
118 sk->sk_state_change(sk);
119 else
120 inet_csk_destroy_sock(sk);
121}
122
123EXPORT_SYMBOL_GPL(dccp_done);
124
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700125const char *dccp_packet_name(const int type)
126{
127 static const char *dccp_packet_names[] = {
128 [DCCP_PKT_REQUEST] = "REQUEST",
129 [DCCP_PKT_RESPONSE] = "RESPONSE",
130 [DCCP_PKT_DATA] = "DATA",
131 [DCCP_PKT_ACK] = "ACK",
132 [DCCP_PKT_DATAACK] = "DATAACK",
133 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
134 [DCCP_PKT_CLOSE] = "CLOSE",
135 [DCCP_PKT_RESET] = "RESET",
136 [DCCP_PKT_SYNC] = "SYNC",
137 [DCCP_PKT_SYNCACK] = "SYNCACK",
138 };
139
140 if (type >= DCCP_NR_PKT_TYPES)
141 return "INVALID";
142 else
143 return dccp_packet_names[type];
144}
145
146EXPORT_SYMBOL_GPL(dccp_packet_name);
147
148const char *dccp_state_name(const int state)
149{
150 static char *dccp_state_names[] = {
Gerrit Renkerf11135a2007-11-28 11:34:53 -0200151 [DCCP_OPEN] = "OPEN",
152 [DCCP_REQUESTING] = "REQUESTING",
153 [DCCP_PARTOPEN] = "PARTOPEN",
154 [DCCP_LISTEN] = "LISTEN",
155 [DCCP_RESPOND] = "RESPOND",
156 [DCCP_CLOSING] = "CLOSING",
157 [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
158 [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
159 [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
160 [DCCP_TIME_WAIT] = "TIME_WAIT",
161 [DCCP_CLOSED] = "CLOSED",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700162 };
163
164 if (state >= DCCP_MAX_STATES)
165 return "INVALID STATE!";
166 else
167 return dccp_state_names[state];
168}
169
170EXPORT_SYMBOL_GPL(dccp_state_name);
171
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800172int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800173{
174 struct dccp_sock *dp = dccp_sk(sk);
175 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800176
Gerrit Renker410e27a2008-09-09 13:27:22 +0200177 dccp_minisock_init(&dp->dccps_minisock);
178
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200179 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
180 icsk->icsk_syn_retries = sysctl_dccp_request_retries;
181 sk->sk_state = DCCP_CLOSED;
182 sk->sk_write_space = dccp_write_space;
183 icsk->icsk_sync_mss = dccp_sync_mss;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200184 dp->dccps_mss_cache = 536;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200185 dp->dccps_rate_last = jiffies;
186 dp->dccps_role = DCCP_ROLE_UNDEFINED;
187 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200188 dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200189
190 dccp_init_xmit_timers(sk);
191
Gerrit Renkerac757732008-11-04 23:55:49 -0800192 INIT_LIST_HEAD(&dp->dccps_featneg);
Gerrit Renker6eb55d12008-12-08 01:15:26 -0800193 /* control socket doesn't need feat nego */
194 if (likely(ctl_sock_initialized))
195 return dccp_feat_init(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800196 return 0;
197}
198
199EXPORT_SYMBOL_GPL(dccp_init_sock);
200
Brian Haley7d06b2e2008-06-14 17:04:49 -0700201void dccp_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800202{
203 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200204 struct dccp_minisock *dmsk = dccp_msk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800205
206 /*
207 * DCCP doesn't use sk_write_queue, just sk_send_head
208 * for retransmissions
209 */
210 if (sk->sk_send_head != NULL) {
211 kfree_skb(sk->sk_send_head);
212 sk->sk_send_head = NULL;
213 }
214
215 /* Clean up a referenced DCCP bind bucket. */
216 if (inet_csk(sk)->icsk_bind_hash != NULL)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800217 inet_put_port(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800218
219 kfree(dp->dccps_service_list);
220 dp->dccps_service_list = NULL;
221
Gerrit Renker410e27a2008-09-09 13:27:22 +0200222 if (dmsk->dccpms_send_ack_vector) {
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800223 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
224 dp->dccps_hc_rx_ackvec = NULL;
225 }
226 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
227 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
228 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
229
230 /* clean up feature negotiation state */
Gerrit Renkerd99a7bd2008-11-04 23:56:30 -0800231 dccp_feat_list_purge(&dp->dccps_featneg);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800232}
233
234EXPORT_SYMBOL_GPL(dccp_destroy_sock);
235
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800236static inline int dccp_listen_start(struct sock *sk, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700237{
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700238 struct dccp_sock *dp = dccp_sk(sk);
239
240 dp->dccps_role = DCCP_ROLE_LISTEN;
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800241 /* do not start to listen if feature negotiation setup fails */
242 if (dccp_feat_finalise_settings(dp))
243 return -EPROTO;
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800244 return inet_csk_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700245}
246
Gerrit Renkerce865a62007-11-24 22:14:15 -0200247static inline int dccp_need_reset(int state)
248{
249 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
250 state != DCCP_REQUESTING;
251}
252
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700253int dccp_disconnect(struct sock *sk, int flags)
254{
255 struct inet_connection_sock *icsk = inet_csk(sk);
256 struct inet_sock *inet = inet_sk(sk);
257 int err = 0;
258 const int old_state = sk->sk_state;
259
260 if (old_state != DCCP_CLOSED)
261 dccp_set_state(sk, DCCP_CLOSED);
262
Gerrit Renkerce865a62007-11-24 22:14:15 -0200263 /*
264 * This corresponds to the ABORT function of RFC793, sec. 3.8
265 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
266 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700267 if (old_state == DCCP_LISTEN) {
268 inet_csk_listen_stop(sk);
Gerrit Renkerce865a62007-11-24 22:14:15 -0200269 } else if (dccp_need_reset(old_state)) {
270 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
271 sk->sk_err = ECONNRESET;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700272 } else if (old_state == DCCP_REQUESTING)
273 sk->sk_err = ECONNRESET;
274
275 dccp_clear_xmit_timers(sk);
Gerrit Renker48816322008-08-23 13:28:27 +0200276
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700277 __skb_queue_purge(&sk->sk_receive_queue);
Gerrit Renker48816322008-08-23 13:28:27 +0200278 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700279 if (sk->sk_send_head != NULL) {
280 __kfree_skb(sk->sk_send_head);
281 sk->sk_send_head = NULL;
282 }
283
284 inet->dport = 0;
285
286 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
287 inet_reset_saddr(sk);
288
289 sk->sk_shutdown = 0;
290 sock_reset_flag(sk, SOCK_DONE);
291
292 icsk->icsk_backoff = 0;
293 inet_csk_delack_init(sk);
294 __sk_dst_reset(sk);
295
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700296 WARN_ON(inet->num && !icsk->icsk_bind_hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700297
298 sk->sk_error_report(sk);
299 return err;
300}
301
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800302EXPORT_SYMBOL_GPL(dccp_disconnect);
303
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700304/*
305 * Wait for a DCCP event.
306 *
307 * Note that we don't need to lock the socket, as the upper poll layers
308 * take care of normal races (between the test and the event) and we don't
309 * go look at any of the socket buffers directly.
310 */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800311unsigned int dccp_poll(struct file *file, struct socket *sock,
312 poll_table *wait)
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700313{
314 unsigned int mask;
315 struct sock *sk = sock->sk;
316
317 poll_wait(file, sk->sk_sleep, wait);
318 if (sk->sk_state == DCCP_LISTEN)
319 return inet_csk_listen_poll(sk);
320
321 /* Socket is not locked. We are protected from async events
322 by poll logic and correct handling of state changes
323 made by another threads is impossible in any case.
324 */
325
326 mask = 0;
327 if (sk->sk_err)
328 mask = POLLERR;
329
330 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
331 mask |= POLLHUP;
332 if (sk->sk_shutdown & RCV_SHUTDOWN)
Davide Libenzif348d702006-03-25 03:07:39 -0800333 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700334
335 /* Connected? */
336 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
337 if (atomic_read(&sk->sk_rmem_alloc) > 0)
338 mask |= POLLIN | POLLRDNORM;
339
340 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
341 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
342 mask |= POLLOUT | POLLWRNORM;
343 } else { /* send SIGIO later */
344 set_bit(SOCK_ASYNC_NOSPACE,
345 &sk->sk_socket->flags);
346 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
347
348 /* Race breaker. If space is freed after
349 * wspace test but before the flags are set,
350 * IO signal will be lost.
351 */
352 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
353 mask |= POLLOUT | POLLWRNORM;
354 }
355 }
356 }
357 return mask;
358}
359
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800360EXPORT_SYMBOL_GPL(dccp_poll);
361
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700362int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
363{
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700364 int rc = -ENOTCONN;
365
366 lock_sock(sk);
367
368 if (sk->sk_state == DCCP_LISTEN)
369 goto out;
370
371 switch (cmd) {
372 case SIOCINQ: {
373 struct sk_buff *skb;
374 unsigned long amount = 0;
375
376 skb = skb_peek(&sk->sk_receive_queue);
377 if (skb != NULL) {
378 /*
379 * We will only return the amount of this packet since
380 * that is all that will be read.
381 */
382 amount = skb->len;
383 }
384 rc = put_user(amount, (int __user *)arg);
385 }
386 break;
387 default:
388 rc = -ENOIOCTLCMD;
389 break;
390 }
391out:
392 release_sock(sk);
393 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700394}
395
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800396EXPORT_SYMBOL_GPL(dccp_ioctl);
397
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800398static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700399 char __user *optval, int optlen)
400{
401 struct dccp_sock *dp = dccp_sk(sk);
402 struct dccp_service_list *sl = NULL;
403
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200404 if (service == DCCP_SERVICE_INVALID_VALUE ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700405 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
406 return -EINVAL;
407
408 if (optlen > sizeof(service)) {
409 sl = kmalloc(optlen, GFP_KERNEL);
410 if (sl == NULL)
411 return -ENOMEM;
412
413 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
414 if (copy_from_user(sl->dccpsl_list,
415 optval + sizeof(service),
416 optlen - sizeof(service)) ||
417 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
418 kfree(sl);
419 return -EFAULT;
420 }
421 }
422
423 lock_sock(sk);
424 dp->dccps_service = service;
425
Jesper Juhla51482b2005-11-08 09:41:34 -0800426 kfree(dp->dccps_service_list);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700427
428 dp->dccps_service_list = sl;
429 release_sock(sk);
430 return 0;
431}
432
Gerrit Renker29450552008-11-16 22:53:48 -0800433static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
434{
435 u8 *list, len;
436 int i, rc;
437
438 if (cscov < 0 || cscov > 15)
439 return -EINVAL;
440 /*
441 * Populate a list of permissible values, in the range cscov...15. This
442 * is necessary since feature negotiation of single values only works if
443 * both sides incidentally choose the same value. Since the list starts
444 * lowest-value first, negotiation will pick the smallest shared value.
445 */
446 if (cscov == 0)
447 return 0;
448 len = 16 - cscov;
449
450 list = kmalloc(len, GFP_KERNEL);
451 if (list == NULL)
452 return -ENOBUFS;
453
454 for (i = 0; i < len; i++)
455 list[i] = cscov++;
456
457 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
458
459 if (rc == 0) {
460 if (rx)
461 dccp_sk(sk)->dccps_pcrlen = cscov;
462 else
463 dccp_sk(sk)->dccps_pcslen = cscov;
464 }
465 kfree(list);
466 return rc;
467}
468
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800469static int dccp_setsockopt_ccid(struct sock *sk, int type,
470 char __user *optval, int optlen)
471{
472 u8 *val;
473 int rc = 0;
474
475 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
476 return -EINVAL;
477
478 val = kmalloc(optlen, GFP_KERNEL);
479 if (val == NULL)
480 return -ENOMEM;
481
482 if (copy_from_user(val, optval, optlen)) {
483 kfree(val);
484 return -EFAULT;
485 }
486
487 lock_sock(sk);
488 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
489 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
490
491 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
492 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
493 release_sock(sk);
494
495 kfree(val);
496 return rc;
497}
498
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800499static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
500 char __user *optval, int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700501{
Gerrit Renker09dbc382006-11-14 12:57:34 -0200502 struct dccp_sock *dp = dccp_sk(sk);
503 int val, err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504
Gerrit Renker19102992008-11-16 22:56:55 -0800505 switch (optname) {
506 case DCCP_SOCKOPT_PACKET_SIZE:
507 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
508 return 0;
509 case DCCP_SOCKOPT_CHANGE_L:
510 case DCCP_SOCKOPT_CHANGE_R:
511 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
512 return 0;
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800513 case DCCP_SOCKOPT_CCID:
514 case DCCP_SOCKOPT_RX_CCID:
515 case DCCP_SOCKOPT_TX_CCID:
516 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
Gerrit Renker19102992008-11-16 22:56:55 -0800517 }
518
519 if (optlen < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300520 return -EINVAL;
521
522 if (get_user(val, (int __user *)optval))
523 return -EFAULT;
524
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700525 if (optname == DCCP_SOCKOPT_SERVICE)
526 return dccp_setsockopt_service(sk, val, optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300527
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700528 lock_sock(sk);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300529 switch (optname) {
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200530 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
531 if (dp->dccps_role != DCCP_ROLE_SERVER)
532 err = -EOPNOTSUPP;
533 else
534 dp->dccps_server_timewait = (val != 0);
535 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800536 case DCCP_SOCKOPT_SEND_CSCOV:
537 err = dccp_setsockopt_cscov(sk, val, false);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200538 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800539 case DCCP_SOCKOPT_RECV_CSCOV:
540 err = dccp_setsockopt_cscov(sk, val, true);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200541 break;
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300542 default:
543 err = -ENOPROTOOPT;
544 break;
545 }
Gerrit Renker410e27a2008-09-09 13:27:22 +0200546 release_sock(sk);
Gerrit Renker19102992008-11-16 22:56:55 -0800547
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300548 return err;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700549}
550
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800551int dccp_setsockopt(struct sock *sk, int level, int optname,
552 char __user *optval, int optlen)
553{
554 if (level != SOL_DCCP)
555 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
556 optname, optval,
557 optlen);
558 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
559}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800560
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800561EXPORT_SYMBOL_GPL(dccp_setsockopt);
562
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800563#ifdef CONFIG_COMPAT
564int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800565 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800566{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800567 if (level != SOL_DCCP)
568 return inet_csk_compat_setsockopt(sk, level, optname,
569 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800570 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
571}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800572
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800573EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
574#endif
575
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700576static int dccp_getsockopt_service(struct sock *sk, int len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800577 __be32 __user *optval,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700578 int __user *optlen)
579{
580 const struct dccp_sock *dp = dccp_sk(sk);
581 const struct dccp_service_list *sl;
582 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
583
584 lock_sock(sk);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700585 if ((sl = dp->dccps_service_list) != NULL) {
586 slen = sl->dccpsl_nr * sizeof(u32);
587 total_len += slen;
588 }
589
590 err = -EINVAL;
591 if (total_len > len)
592 goto out;
593
594 err = 0;
595 if (put_user(total_len, optlen) ||
596 put_user(dp->dccps_service, optval) ||
597 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
598 err = -EFAULT;
599out:
600 release_sock(sk);
601 return err;
602}
603
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800604static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300605 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700606{
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300607 struct dccp_sock *dp;
608 int val, len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700609
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300610 if (get_user(len, optlen))
611 return -EFAULT;
612
Arnaldo Carvalho de Melo39ebc022007-03-28 11:54:32 -0700613 if (len < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300614 return -EINVAL;
615
616 dp = dccp_sk(sk);
617
618 switch (optname) {
619 case DCCP_SOCKOPT_PACKET_SIZE:
Gerrit Renker5aed3242006-11-28 19:33:36 -0200620 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
Arnaldo Carvalho de Melo841bac12006-11-28 19:42:03 -0200621 return 0;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700622 case DCCP_SOCKOPT_SERVICE:
623 return dccp_getsockopt_service(sk, len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800624 (__be32 __user *)optval, optlen);
Gerrit Renker7c559a92007-10-04 14:39:22 -0700625 case DCCP_SOCKOPT_GET_CUR_MPS:
626 val = dp->dccps_mss_cache;
Gerrit Renker7c559a92007-10-04 14:39:22 -0700627 break;
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800628 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
629 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
Gerrit Renker71c262a2008-11-23 16:04:59 -0800630 case DCCP_SOCKOPT_TX_CCID:
631 val = ccid_get_current_tx_ccid(dp);
632 if (val < 0)
633 return -ENOPROTOOPT;
634 break;
635 case DCCP_SOCKOPT_RX_CCID:
636 val = ccid_get_current_rx_ccid(dp);
637 if (val < 0)
638 return -ENOPROTOOPT;
639 break;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200640 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
641 val = dp->dccps_server_timewait;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200642 break;
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200643 case DCCP_SOCKOPT_SEND_CSCOV:
644 val = dp->dccps_pcslen;
645 break;
646 case DCCP_SOCKOPT_RECV_CSCOV:
647 val = dp->dccps_pcrlen;
648 break;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700649 case 128 ... 191:
650 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
651 len, (u32 __user *)optval, optlen);
652 case 192 ... 255:
653 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
654 len, (u32 __user *)optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300655 default:
656 return -ENOPROTOOPT;
657 }
658
Gerrit Renker79133502007-12-13 12:27:14 -0200659 len = sizeof(val);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300660 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
661 return -EFAULT;
662
663 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700664}
665
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800666int dccp_getsockopt(struct sock *sk, int level, int optname,
667 char __user *optval, int __user *optlen)
668{
669 if (level != SOL_DCCP)
670 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
671 optname, optval,
672 optlen);
673 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
674}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800675
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800676EXPORT_SYMBOL_GPL(dccp_getsockopt);
677
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800678#ifdef CONFIG_COMPAT
679int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800680 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800681{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800682 if (level != SOL_DCCP)
683 return inet_csk_compat_getsockopt(sk, level, optname,
684 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800685 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
686}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800687
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800688EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
689#endif
690
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700691int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
692 size_t len)
693{
694 const struct dccp_sock *dp = dccp_sk(sk);
695 const int flags = msg->msg_flags;
696 const int noblock = flags & MSG_DONTWAIT;
697 struct sk_buff *skb;
698 int rc, size;
699 long timeo;
700
701 if (len > dp->dccps_mss_cache)
702 return -EMSGSIZE;
703
704 lock_sock(sk);
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200705
Gerrit Renker410e27a2008-09-09 13:27:22 +0200706 if (sysctl_dccp_tx_qlen &&
707 (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200708 rc = -EAGAIN;
709 goto out_release;
710 }
711
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700712 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700713
714 /*
715 * We have to use sk_stream_wait_connect here to set sk_write_pending,
716 * so that the trick in dccp_rcv_request_sent_state_process.
717 */
718 /* Wait for a connection to finish. */
Gerrit Renkercecd8d02007-09-26 19:36:08 -0300719 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700720 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700721 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700722
723 size = sk->sk_prot->max_header + len;
724 release_sock(sk);
725 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
726 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700727 if (skb == NULL)
728 goto out_release;
729
730 skb_reserve(skb, sk->sk_prot->max_header);
731 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700732 if (rc != 0)
733 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700734
Gerrit Renker410e27a2008-09-09 13:27:22 +0200735 skb_queue_tail(&sk->sk_write_queue, skb);
736 dccp_write_xmit(sk,0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700737out_release:
738 release_sock(sk);
739 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700740out_discard:
741 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700742 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700743}
744
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800745EXPORT_SYMBOL_GPL(dccp_sendmsg);
746
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700747int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
748 size_t len, int nonblock, int flags, int *addr_len)
749{
750 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700751 long timeo;
752
753 lock_sock(sk);
754
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300755 if (sk->sk_state == DCCP_LISTEN) {
756 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700757 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300758 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700759
760 timeo = sock_rcvtimeo(sk, nonblock);
761
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700762 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300763 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700764
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300765 if (skb == NULL)
766 goto verify_sock_status;
767
768 dh = dccp_hdr(skb);
769
Gerrit Renker0c869622007-11-28 11:59:48 -0200770 switch (dh->dccph_type) {
771 case DCCP_PKT_DATA:
772 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300773 goto found_ok_skb;
774
Gerrit Renker0c869622007-11-28 11:59:48 -0200775 case DCCP_PKT_CLOSE:
776 case DCCP_PKT_CLOSEREQ:
777 if (!(flags & MSG_PEEK))
778 dccp_finish_passive_close(sk);
779 /* fall through */
780 case DCCP_PKT_RESET:
781 dccp_pr_debug("found fin (%s) ok!\n",
782 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300783 len = 0;
784 goto found_fin_ok;
Gerrit Renker0c869622007-11-28 11:59:48 -0200785 default:
786 dccp_pr_debug("packet_type=%s\n",
787 dccp_packet_name(dh->dccph_type));
788 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700789 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300790verify_sock_status:
791 if (sock_flag(sk, SOCK_DONE)) {
792 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700793 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700794 }
795
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300796 if (sk->sk_err) {
797 len = sock_error(sk);
798 break;
799 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700800
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300801 if (sk->sk_shutdown & RCV_SHUTDOWN) {
802 len = 0;
803 break;
804 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700805
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300806 if (sk->sk_state == DCCP_CLOSED) {
807 if (!sock_flag(sk, SOCK_DONE)) {
808 /* This occurs when user tries to read
809 * from never connected socket.
810 */
811 len = -ENOTCONN;
812 break;
813 }
814 len = 0;
815 break;
816 }
817
818 if (!timeo) {
819 len = -EAGAIN;
820 break;
821 }
822
823 if (signal_pending(current)) {
824 len = sock_intr_errno(timeo);
825 break;
826 }
827
828 sk_wait_data(sk, &timeo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700829 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700830 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300831 if (len > skb->len)
832 len = skb->len;
833 else if (len < skb->len)
834 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300836 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
837 /* Exception. Bailout! */
838 len = -EFAULT;
839 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700841 found_fin_ok:
842 if (!(flags & MSG_PEEK))
Chris Leech624d1162006-05-23 18:01:28 -0700843 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700844 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300845 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700846out:
847 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300848 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700849}
850
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800851EXPORT_SYMBOL_GPL(dccp_recvmsg);
852
853int inet_dccp_listen(struct socket *sock, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700854{
855 struct sock *sk = sock->sk;
856 unsigned char old_state;
857 int err;
858
859 lock_sock(sk);
860
861 err = -EINVAL;
862 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
863 goto out;
864
865 old_state = sk->sk_state;
866 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
867 goto out;
868
869 /* Really, if the socket is already in listen state
870 * we can only allow the backlog to be adjusted.
871 */
872 if (old_state != DCCP_LISTEN) {
873 /*
874 * FIXME: here it probably should be sk->sk_prot->listen_start
875 * see tcp_listen_start
876 */
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800877 err = dccp_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700878 if (err)
879 goto out;
880 }
881 sk->sk_max_ack_backlog = backlog;
882 err = 0;
883
884out:
885 release_sock(sk);
886 return err;
887}
888
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800889EXPORT_SYMBOL_GPL(inet_dccp_listen);
890
Gerrit Renker0c869622007-11-28 11:59:48 -0200891static void dccp_terminate_connection(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700892{
Gerrit Renker0c869622007-11-28 11:59:48 -0200893 u8 next_state = DCCP_CLOSED;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700894
Gerrit Renker0c869622007-11-28 11:59:48 -0200895 switch (sk->sk_state) {
896 case DCCP_PASSIVE_CLOSE:
897 case DCCP_PASSIVE_CLOSEREQ:
898 dccp_finish_passive_close(sk);
899 break;
900 case DCCP_PARTOPEN:
901 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
902 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
903 /* fall through */
904 case DCCP_OPEN:
905 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700906
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200907 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
908 !dccp_sk(sk)->dccps_server_timewait)
Gerrit Renker0c869622007-11-28 11:59:48 -0200909 next_state = DCCP_ACTIVE_CLOSEREQ;
910 else
911 next_state = DCCP_CLOSING;
912 /* fall through */
913 default:
914 dccp_set_state(sk, next_state);
915 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700916}
917
918void dccp_close(struct sock *sk, long timeout)
919{
Ian McDonald97e58482006-08-26 19:16:45 -0700920 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700921 struct sk_buff *skb;
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800922 u32 data_was_unread = 0;
Herbert Xu134af342006-05-05 17:09:13 -0700923 int state;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700924
925 lock_sock(sk);
926
927 sk->sk_shutdown = SHUTDOWN_MASK;
928
929 if (sk->sk_state == DCCP_LISTEN) {
930 dccp_set_state(sk, DCCP_CLOSED);
931
932 /* Special case. */
933 inet_csk_listen_stop(sk);
934
935 goto adjudge_to_death;
936 }
937
Ian McDonald97e58482006-08-26 19:16:45 -0700938 sk_stop_timer(sk, &dp->dccps_xmit_timer);
939
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700940 /*
941 * We need to flush the recv. buffs. We do this only on the
942 * descriptor close, not protocol-sourced closes, because the
943 *reader process may not have drained the data yet!
944 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700945 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800946 data_was_unread += skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700947 __kfree_skb(skb);
948 }
949
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800950 if (data_was_unread) {
951 /* Unread data was tossed, send an appropriate Reset Code */
952 DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
953 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
954 dccp_set_state(sk, DCCP_CLOSED);
955 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700956 /* Check zero linger _after_ checking for unread data. */
957 sk->sk_prot->disconnect(sk, 0);
Gerrit Renker0c869622007-11-28 11:59:48 -0200958 } else if (sk->sk_state != DCCP_CLOSED) {
959 dccp_terminate_connection(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700960 }
961
962 sk_stream_wait_close(sk, timeout);
963
964adjudge_to_death:
Herbert Xu134af342006-05-05 17:09:13 -0700965 state = sk->sk_state;
966 sock_hold(sk);
967 sock_orphan(sk);
Eric Dumazetdd24c002008-11-25 21:17:14 -0800968 percpu_counter_inc(sk->sk_prot->orphan_count);
Herbert Xu134af342006-05-05 17:09:13 -0700969
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700970 /*
971 * It is the last release_sock in its life. It will remove backlog.
972 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700973 release_sock(sk);
974 /*
975 * Now socket is owned by kernel and we acquire BH lock
976 * to finish close. No need to check for user refs.
977 */
978 local_bh_disable();
979 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700980 WARN_ON(sock_owned_by_user(sk));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700981
Herbert Xu134af342006-05-05 17:09:13 -0700982 /* Have we already been destroyed by a softirq or backlog? */
983 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
984 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700985
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700986 if (sk->sk_state == DCCP_CLOSED)
987 inet_csk_destroy_sock(sk);
988
989 /* Otherwise, socket is reprieved until protocol close. */
990
Herbert Xu134af342006-05-05 17:09:13 -0700991out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700992 bh_unlock_sock(sk);
993 local_bh_enable();
994 sock_put(sk);
995}
996
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800997EXPORT_SYMBOL_GPL(dccp_close);
998
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700999void dccp_shutdown(struct sock *sk, int how)
1000{
Gerrit Renker8e8c71f2007-11-21 09:56:48 -02001001 dccp_pr_debug("called shutdown(%x)\n", how);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001002}
1003
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001004EXPORT_SYMBOL_GPL(dccp_shutdown);
1005
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001006static inline int dccp_mib_init(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001007{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001008 return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001009}
1010
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001011static inline void dccp_mib_exit(void)
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001012{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001013 snmp_mib_free((void**)dccp_statistics);
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001014}
1015
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001016static int thash_entries;
1017module_param(thash_entries, int, 0444);
1018MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1019
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001020#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001021int dccp_debug;
Gerrit Renker43264992008-08-23 13:28:27 +02001022module_param(dccp_debug, bool, 0644);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001023MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001024
1025EXPORT_SYMBOL_GPL(dccp_debug);
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001026#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001027
1028static int __init dccp_init(void)
1029{
1030 unsigned long goal;
1031 int ehash_order, bhash_order, i;
Eric Dumazetdd24c002008-11-25 21:17:14 -08001032 int rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001033
Patrick McHardy028b0272008-04-12 18:35:41 -07001034 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1035 FIELD_SIZEOF(struct sk_buff, cb));
Eric Dumazetdd24c002008-11-25 21:17:14 -08001036 rc = percpu_counter_init(&dccp_orphan_count, 0);
1037 if (rc)
1038 goto out;
1039 rc = -ENOBUFS;
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001040 inet_hashinfo_init(&dccp_hashinfo);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001041 dccp_hashinfo.bind_bucket_cachep =
1042 kmem_cache_create("dccp_bind_bucket",
1043 sizeof(struct inet_bind_bucket), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001044 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001045 if (!dccp_hashinfo.bind_bucket_cachep)
Eric Dumazetdd24c002008-11-25 21:17:14 -08001046 goto out_free_percpu;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001047
1048 /*
1049 * Size and allocate the main established and bind bucket
1050 * hash tables.
1051 *
1052 * The methodology is similar to that of the buffer cache.
1053 */
1054 if (num_physpages >= (128 * 1024))
1055 goal = num_physpages >> (21 - PAGE_SHIFT);
1056 else
1057 goal = num_physpages >> (23 - PAGE_SHIFT);
1058
1059 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001060 goal = (thash_entries *
1061 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001062 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1063 ;
1064 do {
1065 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1066 sizeof(struct inet_ehash_bucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001067 while (dccp_hashinfo.ehash_size &
1068 (dccp_hashinfo.ehash_size - 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001069 dccp_hashinfo.ehash_size--;
1070 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1071 __get_free_pages(GFP_ATOMIC, ehash_order);
1072 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1073
1074 if (!dccp_hashinfo.ehash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001075 DCCP_CRIT("Failed to allocate DCCP established hash table");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001076 goto out_free_bind_bucket_cachep;
1077 }
1078
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001079 for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001080 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
1081 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001082 }
1083
Eric Dumazet230140c2007-11-07 02:40:20 -08001084 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1085 goto out_free_dccp_ehash;
1086
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001087 bhash_order = ehash_order;
1088
1089 do {
1090 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1091 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001092 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1093 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001094 continue;
1095 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1096 __get_free_pages(GFP_ATOMIC, bhash_order);
1097 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1098
1099 if (!dccp_hashinfo.bhash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001100 DCCP_CRIT("Failed to allocate DCCP bind hash table");
Eric Dumazet230140c2007-11-07 02:40:20 -08001101 goto out_free_dccp_locks;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001102 }
1103
1104 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1105 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1106 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1107 }
1108
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001109 rc = dccp_mib_init();
Arnaldo Carvalho de Melofa23e2e2006-03-20 17:16:01 -08001110 if (rc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001111 goto out_free_dccp_bhash;
1112
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001113 rc = dccp_ackvec_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001114 if (rc)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001115 goto out_free_dccp_mib;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001116
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001117 rc = dccp_sysctl_init();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001118 if (rc)
1119 goto out_ackvec_exit;
Gerrit Renker4c70f382007-09-25 22:40:13 -07001120
1121 dccp_timestamping_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001122out:
1123 return rc;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001124out_ackvec_exit:
1125 dccp_ackvec_exit();
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001126out_free_dccp_mib:
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001127 dccp_mib_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001128out_free_dccp_bhash:
1129 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1130 dccp_hashinfo.bhash = NULL;
Eric Dumazet230140c2007-11-07 02:40:20 -08001131out_free_dccp_locks:
1132 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001133out_free_dccp_ehash:
1134 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1135 dccp_hashinfo.ehash = NULL;
1136out_free_bind_bucket_cachep:
1137 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1138 dccp_hashinfo.bind_bucket_cachep = NULL;
Eric Dumazetdd24c002008-11-25 21:17:14 -08001139out_free_percpu:
1140 percpu_counter_destroy(&dccp_orphan_count);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001141 goto out;
1142}
1143
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001144static void __exit dccp_fini(void)
1145{
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001146 dccp_mib_exit();
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001147 free_pages((unsigned long)dccp_hashinfo.bhash,
1148 get_order(dccp_hashinfo.bhash_size *
1149 sizeof(struct inet_bind_hashbucket)));
1150 free_pages((unsigned long)dccp_hashinfo.ehash,
1151 get_order(dccp_hashinfo.ehash_size *
1152 sizeof(struct inet_ehash_bucket)));
Eric Dumazet230140c2007-11-07 02:40:20 -08001153 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001154 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001155 dccp_ackvec_exit();
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001156 dccp_sysctl_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001157}
1158
1159module_init(dccp_init);
1160module_exit(dccp_fini);
1161
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001162MODULE_LICENSE("GPL");
1163MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1164MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");