blob: 0d420790b795180cdfefed7d3cbfe42cbce083a3 [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
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070043atomic_t dccp_orphan_count = ATOMIC_INIT(0);
44
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080045EXPORT_SYMBOL_GPL(dccp_orphan_count);
46
Arnaldo Carvalho de Melo075ae862006-03-20 21:24:19 -080047struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
48 .lhash_lock = RW_LOCK_UNLOCKED,
49 .lhash_users = ATOMIC_INIT(0),
50 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
51};
52
53EXPORT_SYMBOL_GPL(dccp_hashinfo);
54
Ian McDonaldb1308dc2006-11-20 18:30:17 -020055/* the maximum queue length for tx in packets. 0 is no limit */
56int sysctl_dccp_tx_qlen __read_mostly = 5;
57
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080058void dccp_set_state(struct sock *sk, const int state)
59{
60 const int oldstate = sk->sk_state;
61
Gerrit Renkerf11135a2007-11-28 11:34:53 -020062 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080063 dccp_state_name(oldstate), dccp_state_name(state));
64 WARN_ON(state == oldstate);
65
66 switch (state) {
67 case DCCP_OPEN:
68 if (oldstate != DCCP_OPEN)
69 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
Gerrit Renker3a53a9a2008-09-04 07:30:19 +020070 /* Client retransmits all Confirm options until entering OPEN */
71 if (oldstate == DCCP_PARTOPEN)
72 dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080073 break;
74
75 case DCCP_CLOSED:
Gerrit Renker0c869622007-11-28 11:59:48 -020076 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
77 oldstate == DCCP_CLOSING)
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080078 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
79
80 sk->sk_prot->unhash(sk);
81 if (inet_csk(sk)->icsk_bind_hash != NULL &&
82 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -080083 inet_put_port(sk);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080084 /* fall through */
85 default:
86 if (oldstate == DCCP_OPEN)
87 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
88 }
89
90 /* Change state AFTER socket is unhashed to avoid closed
91 * socket sitting in hash tables.
92 */
93 sk->sk_state = state;
94}
95
96EXPORT_SYMBOL_GPL(dccp_set_state);
97
Gerrit Renker0c869622007-11-28 11:59:48 -020098static void dccp_finish_passive_close(struct sock *sk)
99{
100 switch (sk->sk_state) {
101 case DCCP_PASSIVE_CLOSE:
102 /* Node (client or server) has received Close packet. */
103 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
104 dccp_set_state(sk, DCCP_CLOSED);
105 break;
106 case DCCP_PASSIVE_CLOSEREQ:
107 /*
108 * Client received CloseReq. We set the `active' flag so that
109 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
110 */
111 dccp_send_close(sk, 1);
112 dccp_set_state(sk, DCCP_CLOSING);
113 }
114}
115
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800116void dccp_done(struct sock *sk)
117{
118 dccp_set_state(sk, DCCP_CLOSED);
119 dccp_clear_xmit_timers(sk);
120
121 sk->sk_shutdown = SHUTDOWN_MASK;
122
123 if (!sock_flag(sk, SOCK_DEAD))
124 sk->sk_state_change(sk);
125 else
126 inet_csk_destroy_sock(sk);
127}
128
129EXPORT_SYMBOL_GPL(dccp_done);
130
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700131const char *dccp_packet_name(const int type)
132{
133 static const char *dccp_packet_names[] = {
134 [DCCP_PKT_REQUEST] = "REQUEST",
135 [DCCP_PKT_RESPONSE] = "RESPONSE",
136 [DCCP_PKT_DATA] = "DATA",
137 [DCCP_PKT_ACK] = "ACK",
138 [DCCP_PKT_DATAACK] = "DATAACK",
139 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
140 [DCCP_PKT_CLOSE] = "CLOSE",
141 [DCCP_PKT_RESET] = "RESET",
142 [DCCP_PKT_SYNC] = "SYNC",
143 [DCCP_PKT_SYNCACK] = "SYNCACK",
144 };
145
146 if (type >= DCCP_NR_PKT_TYPES)
147 return "INVALID";
148 else
149 return dccp_packet_names[type];
150}
151
152EXPORT_SYMBOL_GPL(dccp_packet_name);
153
154const char *dccp_state_name(const int state)
155{
156 static char *dccp_state_names[] = {
Gerrit Renkerf11135a2007-11-28 11:34:53 -0200157 [DCCP_OPEN] = "OPEN",
158 [DCCP_REQUESTING] = "REQUESTING",
159 [DCCP_PARTOPEN] = "PARTOPEN",
160 [DCCP_LISTEN] = "LISTEN",
161 [DCCP_RESPOND] = "RESPOND",
162 [DCCP_CLOSING] = "CLOSING",
163 [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
164 [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
165 [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
166 [DCCP_TIME_WAIT] = "TIME_WAIT",
167 [DCCP_CLOSED] = "CLOSED",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700168 };
169
170 if (state >= DCCP_MAX_STATES)
171 return "INVALID STATE!";
172 else
173 return dccp_state_names[state];
174}
175
176EXPORT_SYMBOL_GPL(dccp_state_name);
177
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800178int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800179{
180 struct dccp_sock *dp = dccp_sk(sk);
181 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800182
Arnaldo Carvalho de Meloa4bf3902006-03-20 22:50:58 -0800183 dccp_minisock_init(&dp->dccps_minisock);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800184
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200185 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
186 icsk->icsk_syn_retries = sysctl_dccp_request_retries;
187 sk->sk_state = DCCP_CLOSED;
188 sk->sk_write_space = dccp_write_space;
189 icsk->icsk_sync_mss = dccp_sync_mss;
190 dp->dccps_mss_cache = 536;
191 dp->dccps_rate_last = jiffies;
192 dp->dccps_role = DCCP_ROLE_UNDEFINED;
193 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
194 dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1;
195
196 dccp_init_xmit_timers(sk);
197
Gerrit Renker828755c2008-09-04 07:30:19 +0200198 INIT_LIST_HEAD(&dp->dccps_featneg);
Gerrit Renker3a53a9a2008-09-04 07:30:19 +0200199 /* control socket doesn't need feat nego */
200 if (likely(ctl_sock_initialized))
201 return dccp_feat_init(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800202 return 0;
203}
204
205EXPORT_SYMBOL_GPL(dccp_init_sock);
206
Brian Haley7d06b2e2008-06-14 17:04:49 -0700207void dccp_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800208{
209 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800210 struct dccp_minisock *dmsk = dccp_msk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800211
212 /*
213 * DCCP doesn't use sk_write_queue, just sk_send_head
214 * for retransmissions
215 */
216 if (sk->sk_send_head != NULL) {
217 kfree_skb(sk->sk_send_head);
218 sk->sk_send_head = NULL;
219 }
220
221 /* Clean up a referenced DCCP bind bucket. */
222 if (inet_csk(sk)->icsk_bind_hash != NULL)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800223 inet_put_port(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800224
225 kfree(dp->dccps_service_list);
226 dp->dccps_service_list = NULL;
227
Arnaldo Carvalho de Melo8ca0d172006-03-20 22:51:53 -0800228 if (dmsk->dccpms_send_ack_vector) {
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800229 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
230 dp->dccps_hc_rx_ackvec = NULL;
231 }
232 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
233 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
234 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
235
236 /* clean up feature negotiation state */
Gerrit Renker70208382008-09-04 07:30:19 +0200237 dccp_feat_list_purge(&dp->dccps_featneg);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800238}
239
240EXPORT_SYMBOL_GPL(dccp_destroy_sock);
241
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800242static inline int dccp_listen_start(struct sock *sk, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700243{
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700244 struct dccp_sock *dp = dccp_sk(sk);
245
246 dp->dccps_role = DCCP_ROLE_LISTEN;
Gerrit Renker093e1f42008-09-04 07:30:19 +0200247 /* do not start to listen if feature negotiation setup fails */
248 if (dccp_feat_finalise_settings(dp))
249 return -EPROTO;
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800250 return inet_csk_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700251}
252
Gerrit Renkerce865a62007-11-24 22:14:15 -0200253static inline int dccp_need_reset(int state)
254{
255 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
256 state != DCCP_REQUESTING;
257}
258
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700259int dccp_disconnect(struct sock *sk, int flags)
260{
261 struct inet_connection_sock *icsk = inet_csk(sk);
262 struct inet_sock *inet = inet_sk(sk);
263 int err = 0;
264 const int old_state = sk->sk_state;
265
266 if (old_state != DCCP_CLOSED)
267 dccp_set_state(sk, DCCP_CLOSED);
268
Gerrit Renkerce865a62007-11-24 22:14:15 -0200269 /*
270 * This corresponds to the ABORT function of RFC793, sec. 3.8
271 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
272 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700273 if (old_state == DCCP_LISTEN) {
274 inet_csk_listen_stop(sk);
Gerrit Renkerce865a62007-11-24 22:14:15 -0200275 } else if (dccp_need_reset(old_state)) {
276 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
277 sk->sk_err = ECONNRESET;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700278 } else if (old_state == DCCP_REQUESTING)
279 sk->sk_err = ECONNRESET;
280
281 dccp_clear_xmit_timers(sk);
Gerrit Renker48816322008-08-23 13:28:27 +0200282
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700283 __skb_queue_purge(&sk->sk_receive_queue);
Gerrit Renker48816322008-08-23 13:28:27 +0200284 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700285 if (sk->sk_send_head != NULL) {
286 __kfree_skb(sk->sk_send_head);
287 sk->sk_send_head = NULL;
288 }
289
290 inet->dport = 0;
291
292 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
293 inet_reset_saddr(sk);
294
295 sk->sk_shutdown = 0;
296 sock_reset_flag(sk, SOCK_DONE);
297
298 icsk->icsk_backoff = 0;
299 inet_csk_delack_init(sk);
300 __sk_dst_reset(sk);
301
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700302 WARN_ON(inet->num && !icsk->icsk_bind_hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700303
304 sk->sk_error_report(sk);
305 return err;
306}
307
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800308EXPORT_SYMBOL_GPL(dccp_disconnect);
309
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700310/*
311 * Wait for a DCCP event.
312 *
313 * Note that we don't need to lock the socket, as the upper poll layers
314 * take care of normal races (between the test and the event) and we don't
315 * go look at any of the socket buffers directly.
316 */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800317unsigned int dccp_poll(struct file *file, struct socket *sock,
318 poll_table *wait)
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700319{
320 unsigned int mask;
321 struct sock *sk = sock->sk;
322
323 poll_wait(file, sk->sk_sleep, wait);
324 if (sk->sk_state == DCCP_LISTEN)
325 return inet_csk_listen_poll(sk);
326
327 /* Socket is not locked. We are protected from async events
328 by poll logic and correct handling of state changes
329 made by another threads is impossible in any case.
330 */
331
332 mask = 0;
333 if (sk->sk_err)
334 mask = POLLERR;
335
336 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
337 mask |= POLLHUP;
338 if (sk->sk_shutdown & RCV_SHUTDOWN)
Davide Libenzif348d702006-03-25 03:07:39 -0800339 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700340
341 /* Connected? */
342 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
343 if (atomic_read(&sk->sk_rmem_alloc) > 0)
344 mask |= POLLIN | POLLRDNORM;
345
346 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
347 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
348 mask |= POLLOUT | POLLWRNORM;
349 } else { /* send SIGIO later */
350 set_bit(SOCK_ASYNC_NOSPACE,
351 &sk->sk_socket->flags);
352 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
353
354 /* Race breaker. If space is freed after
355 * wspace test but before the flags are set,
356 * IO signal will be lost.
357 */
358 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
359 mask |= POLLOUT | POLLWRNORM;
360 }
361 }
362 }
363 return mask;
364}
365
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800366EXPORT_SYMBOL_GPL(dccp_poll);
367
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700368int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
369{
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700370 int rc = -ENOTCONN;
371
372 lock_sock(sk);
373
374 if (sk->sk_state == DCCP_LISTEN)
375 goto out;
376
377 switch (cmd) {
378 case SIOCINQ: {
379 struct sk_buff *skb;
380 unsigned long amount = 0;
381
382 skb = skb_peek(&sk->sk_receive_queue);
383 if (skb != NULL) {
384 /*
385 * We will only return the amount of this packet since
386 * that is all that will be read.
387 */
388 amount = skb->len;
389 }
390 rc = put_user(amount, (int __user *)arg);
391 }
392 break;
393 default:
394 rc = -ENOIOCTLCMD;
395 break;
396 }
397out:
398 release_sock(sk);
399 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700400}
401
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800402EXPORT_SYMBOL_GPL(dccp_ioctl);
403
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800404static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700405 char __user *optval, int optlen)
406{
407 struct dccp_sock *dp = dccp_sk(sk);
408 struct dccp_service_list *sl = NULL;
409
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200410 if (service == DCCP_SERVICE_INVALID_VALUE ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700411 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
412 return -EINVAL;
413
414 if (optlen > sizeof(service)) {
415 sl = kmalloc(optlen, GFP_KERNEL);
416 if (sl == NULL)
417 return -ENOMEM;
418
419 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
420 if (copy_from_user(sl->dccpsl_list,
421 optval + sizeof(service),
422 optlen - sizeof(service)) ||
423 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
424 kfree(sl);
425 return -EFAULT;
426 }
427 }
428
429 lock_sock(sk);
430 dp->dccps_service = service;
431
Jesper Juhla51482b2005-11-08 09:41:34 -0800432 kfree(dp->dccps_service_list);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700433
434 dp->dccps_service_list = sl;
435 release_sock(sk);
436 return 0;
437}
438
Gerrit Renker20f41ee2008-09-04 07:30:19 +0200439static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
440{
441 u8 *list, len;
442 int i, rc;
443
444 if (cscov < 0 || cscov > 15)
445 return -EINVAL;
446 /*
447 * Populate a list of permissible values, in the range cscov...15. This
448 * is necessary since feature negotiation of single values only works if
449 * both sides incidentally choose the same value. Since the list starts
450 * lowest-value first, negotiation will pick the smallest shared value.
451 */
452 if (cscov == 0)
453 return 0;
454 len = 16 - cscov;
455
456 list = kmalloc(len, GFP_KERNEL);
457 if (list == NULL)
458 return -ENOBUFS;
459
460 for (i = 0; i < len; i++)
461 list[i] = cscov++;
462
463 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
464
465 if (rc == 0) {
466 if (rx)
467 dccp_sk(sk)->dccps_pcrlen = cscov;
468 else
469 dccp_sk(sk)->dccps_pcslen = cscov;
470 }
471 kfree(list);
472 return rc;
473}
474
Gerrit Renkerfade7562008-09-04 07:30:19 +0200475static int dccp_setsockopt_ccid(struct sock *sk, int type,
476 char __user *optval, int optlen)
477{
478 u8 *val;
479 int rc = 0;
480
481 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
482 return -EINVAL;
483
484 val = kmalloc(optlen, GFP_KERNEL);
485 if (val == NULL)
486 return -ENOMEM;
487
488 if (copy_from_user(val, optval, optlen)) {
489 kfree(val);
490 return -EFAULT;
491 }
492
493 lock_sock(sk);
494 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
495 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
496
497 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
498 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
499 release_sock(sk);
500
501 kfree(val);
502 return rc;
503}
504
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800505static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
506 char __user *optval, int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700507{
Gerrit Renker09dbc382006-11-14 12:57:34 -0200508 struct dccp_sock *dp = dccp_sk(sk);
509 int val, err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700510
Gerrit Renker73bbe092008-09-04 07:30:19 +0200511 switch (optname) {
512 case DCCP_SOCKOPT_PACKET_SIZE:
513 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
514 return 0;
515 case DCCP_SOCKOPT_CHANGE_L:
516 case DCCP_SOCKOPT_CHANGE_R:
517 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
518 return 0;
Gerrit Renkerfade7562008-09-04 07:30:19 +0200519 case DCCP_SOCKOPT_CCID:
520 case DCCP_SOCKOPT_RX_CCID:
521 case DCCP_SOCKOPT_TX_CCID:
522 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
Gerrit Renker73bbe092008-09-04 07:30:19 +0200523 }
524
525 if (optlen < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300526 return -EINVAL;
527
528 if (get_user(val, (int __user *)optval))
529 return -EFAULT;
530
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700531 if (optname == DCCP_SOCKOPT_SERVICE)
532 return dccp_setsockopt_service(sk, val, optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300533
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700534 lock_sock(sk);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300535 switch (optname) {
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200536 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
537 if (dp->dccps_role != DCCP_ROLE_SERVER)
538 err = -EOPNOTSUPP;
539 else
540 dp->dccps_server_timewait = (val != 0);
541 break;
Gerrit Renker20f41ee2008-09-04 07:30:19 +0200542 case DCCP_SOCKOPT_SEND_CSCOV:
543 err = dccp_setsockopt_cscov(sk, val, false);
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200544 break;
Gerrit Renker20f41ee2008-09-04 07:30:19 +0200545 case DCCP_SOCKOPT_RECV_CSCOV:
546 err = dccp_setsockopt_cscov(sk, val, true);
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200547 break;
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300548 default:
549 err = -ENOPROTOOPT;
550 break;
551 }
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300552 release_sock(sk);
Gerrit Renker73bbe092008-09-04 07:30:19 +0200553
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300554 return err;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700555}
556
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800557int dccp_setsockopt(struct sock *sk, int level, int optname,
558 char __user *optval, int optlen)
559{
560 if (level != SOL_DCCP)
561 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
562 optname, optval,
563 optlen);
564 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
565}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800566
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800567EXPORT_SYMBOL_GPL(dccp_setsockopt);
568
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800569#ifdef CONFIG_COMPAT
570int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800571 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800572{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800573 if (level != SOL_DCCP)
574 return inet_csk_compat_setsockopt(sk, level, optname,
575 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800576 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
577}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800578
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800579EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
580#endif
581
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700582static int dccp_getsockopt_service(struct sock *sk, int len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800583 __be32 __user *optval,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700584 int __user *optlen)
585{
586 const struct dccp_sock *dp = dccp_sk(sk);
587 const struct dccp_service_list *sl;
588 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
589
590 lock_sock(sk);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700591 if ((sl = dp->dccps_service_list) != NULL) {
592 slen = sl->dccpsl_nr * sizeof(u32);
593 total_len += slen;
594 }
595
596 err = -EINVAL;
597 if (total_len > len)
598 goto out;
599
600 err = 0;
601 if (put_user(total_len, optlen) ||
602 put_user(dp->dccps_service, optval) ||
603 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
604 err = -EFAULT;
605out:
606 release_sock(sk);
607 return err;
608}
609
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800610static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300611 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700612{
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300613 struct dccp_sock *dp;
614 int val, len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700615
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300616 if (get_user(len, optlen))
617 return -EFAULT;
618
Arnaldo Carvalho de Melo39ebc022007-03-28 11:54:32 -0700619 if (len < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300620 return -EINVAL;
621
622 dp = dccp_sk(sk);
623
624 switch (optname) {
625 case DCCP_SOCKOPT_PACKET_SIZE:
Gerrit Renker5aed3242006-11-28 19:33:36 -0200626 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
Arnaldo Carvalho de Melo841bac12006-11-28 19:42:03 -0200627 return 0;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700628 case DCCP_SOCKOPT_SERVICE:
629 return dccp_getsockopt_service(sk, len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800630 (__be32 __user *)optval, optlen);
Gerrit Renker7c559a92007-10-04 14:39:22 -0700631 case DCCP_SOCKOPT_GET_CUR_MPS:
632 val = dp->dccps_mss_cache;
Gerrit Renker7c559a92007-10-04 14:39:22 -0700633 break;
Gerrit Renker71bb4952008-09-04 07:30:19 +0200634 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
635 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
Gerrit Renkerc8041e22008-09-04 07:30:19 +0200636 case DCCP_SOCKOPT_TX_CCID:
637 val = ccid_get_current_tx_ccid(dp);
638 if (val < 0)
639 return -ENOPROTOOPT;
640 break;
641 case DCCP_SOCKOPT_RX_CCID:
642 val = ccid_get_current_rx_ccid(dp);
643 if (val < 0)
644 return -ENOPROTOOPT;
645 break;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200646 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
647 val = dp->dccps_server_timewait;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200648 break;
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200649 case DCCP_SOCKOPT_SEND_CSCOV:
650 val = dp->dccps_pcslen;
651 break;
652 case DCCP_SOCKOPT_RECV_CSCOV:
653 val = dp->dccps_pcrlen;
654 break;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700655 case 128 ... 191:
656 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
657 len, (u32 __user *)optval, optlen);
658 case 192 ... 255:
659 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
660 len, (u32 __user *)optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300661 default:
662 return -ENOPROTOOPT;
663 }
664
Gerrit Renker79133502007-12-13 12:27:14 -0200665 len = sizeof(val);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300666 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
667 return -EFAULT;
668
669 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700670}
671
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800672int dccp_getsockopt(struct sock *sk, int level, int optname,
673 char __user *optval, int __user *optlen)
674{
675 if (level != SOL_DCCP)
676 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
677 optname, optval,
678 optlen);
679 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
680}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800681
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800682EXPORT_SYMBOL_GPL(dccp_getsockopt);
683
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800684#ifdef CONFIG_COMPAT
685int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800686 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800687{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800688 if (level != SOL_DCCP)
689 return inet_csk_compat_getsockopt(sk, level, optname,
690 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800691 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
692}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800693
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800694EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
695#endif
696
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700697int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
698 size_t len)
699{
700 const struct dccp_sock *dp = dccp_sk(sk);
701 const int flags = msg->msg_flags;
702 const int noblock = flags & MSG_DONTWAIT;
703 struct sk_buff *skb;
704 int rc, size;
705 long timeo;
706
707 if (len > dp->dccps_mss_cache)
708 return -EMSGSIZE;
709
710 lock_sock(sk);
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200711
712 if (sysctl_dccp_tx_qlen &&
713 (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
714 rc = -EAGAIN;
715 goto out_release;
716 }
717
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700718 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700719
720 /*
721 * We have to use sk_stream_wait_connect here to set sk_write_pending,
722 * so that the trick in dccp_rcv_request_sent_state_process.
723 */
724 /* Wait for a connection to finish. */
Gerrit Renkercecd8d02007-09-26 19:36:08 -0300725 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700726 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700727 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700728
729 size = sk->sk_prot->max_header + len;
730 release_sock(sk);
731 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
732 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700733 if (skb == NULL)
734 goto out_release;
735
736 skb_reserve(skb, sk->sk_prot->max_header);
737 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700738 if (rc != 0)
739 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700740
Ian McDonald97e58482006-08-26 19:16:45 -0700741 skb_queue_tail(&sk->sk_write_queue, skb);
742 dccp_write_xmit(sk,0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700743out_release:
744 release_sock(sk);
745 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700746out_discard:
747 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700748 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700749}
750
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800751EXPORT_SYMBOL_GPL(dccp_sendmsg);
752
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700753int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
754 size_t len, int nonblock, int flags, int *addr_len)
755{
756 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700757 long timeo;
758
759 lock_sock(sk);
760
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300761 if (sk->sk_state == DCCP_LISTEN) {
762 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700763 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300764 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700765
766 timeo = sock_rcvtimeo(sk, nonblock);
767
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700768 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300769 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700770
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300771 if (skb == NULL)
772 goto verify_sock_status;
773
774 dh = dccp_hdr(skb);
775
Gerrit Renker0c869622007-11-28 11:59:48 -0200776 switch (dh->dccph_type) {
777 case DCCP_PKT_DATA:
778 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300779 goto found_ok_skb;
780
Gerrit Renker0c869622007-11-28 11:59:48 -0200781 case DCCP_PKT_CLOSE:
782 case DCCP_PKT_CLOSEREQ:
783 if (!(flags & MSG_PEEK))
784 dccp_finish_passive_close(sk);
785 /* fall through */
786 case DCCP_PKT_RESET:
787 dccp_pr_debug("found fin (%s) ok!\n",
788 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300789 len = 0;
790 goto found_fin_ok;
Gerrit Renker0c869622007-11-28 11:59:48 -0200791 default:
792 dccp_pr_debug("packet_type=%s\n",
793 dccp_packet_name(dh->dccph_type));
794 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700795 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300796verify_sock_status:
797 if (sock_flag(sk, SOCK_DONE)) {
798 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700799 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700800 }
801
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300802 if (sk->sk_err) {
803 len = sock_error(sk);
804 break;
805 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700806
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300807 if (sk->sk_shutdown & RCV_SHUTDOWN) {
808 len = 0;
809 break;
810 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700811
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300812 if (sk->sk_state == DCCP_CLOSED) {
813 if (!sock_flag(sk, SOCK_DONE)) {
814 /* This occurs when user tries to read
815 * from never connected socket.
816 */
817 len = -ENOTCONN;
818 break;
819 }
820 len = 0;
821 break;
822 }
823
824 if (!timeo) {
825 len = -EAGAIN;
826 break;
827 }
828
829 if (signal_pending(current)) {
830 len = sock_intr_errno(timeo);
831 break;
832 }
833
834 sk_wait_data(sk, &timeo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700836 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300837 if (len > skb->len)
838 len = skb->len;
839 else if (len < skb->len)
840 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700841
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300842 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
843 /* Exception. Bailout! */
844 len = -EFAULT;
845 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700846 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847 found_fin_ok:
848 if (!(flags & MSG_PEEK))
Chris Leech624d1162006-05-23 18:01:28 -0700849 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700850 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300851 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700852out:
853 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300854 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700855}
856
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800857EXPORT_SYMBOL_GPL(dccp_recvmsg);
858
859int inet_dccp_listen(struct socket *sock, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700860{
861 struct sock *sk = sock->sk;
862 unsigned char old_state;
863 int err;
864
865 lock_sock(sk);
866
867 err = -EINVAL;
868 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
869 goto out;
870
871 old_state = sk->sk_state;
872 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
873 goto out;
874
875 /* Really, if the socket is already in listen state
876 * we can only allow the backlog to be adjusted.
877 */
878 if (old_state != DCCP_LISTEN) {
879 /*
880 * FIXME: here it probably should be sk->sk_prot->listen_start
881 * see tcp_listen_start
882 */
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800883 err = dccp_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700884 if (err)
885 goto out;
886 }
887 sk->sk_max_ack_backlog = backlog;
888 err = 0;
889
890out:
891 release_sock(sk);
892 return err;
893}
894
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800895EXPORT_SYMBOL_GPL(inet_dccp_listen);
896
Gerrit Renker0c869622007-11-28 11:59:48 -0200897static void dccp_terminate_connection(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700898{
Gerrit Renker0c869622007-11-28 11:59:48 -0200899 u8 next_state = DCCP_CLOSED;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700900
Gerrit Renker0c869622007-11-28 11:59:48 -0200901 switch (sk->sk_state) {
902 case DCCP_PASSIVE_CLOSE:
903 case DCCP_PASSIVE_CLOSEREQ:
904 dccp_finish_passive_close(sk);
905 break;
906 case DCCP_PARTOPEN:
907 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
908 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
909 /* fall through */
910 case DCCP_OPEN:
911 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700912
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200913 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
914 !dccp_sk(sk)->dccps_server_timewait)
Gerrit Renker0c869622007-11-28 11:59:48 -0200915 next_state = DCCP_ACTIVE_CLOSEREQ;
916 else
917 next_state = DCCP_CLOSING;
918 /* fall through */
919 default:
920 dccp_set_state(sk, next_state);
921 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700922}
923
924void dccp_close(struct sock *sk, long timeout)
925{
Ian McDonald97e58482006-08-26 19:16:45 -0700926 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700927 struct sk_buff *skb;
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800928 u32 data_was_unread = 0;
Herbert Xu134af342006-05-05 17:09:13 -0700929 int state;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700930
931 lock_sock(sk);
932
933 sk->sk_shutdown = SHUTDOWN_MASK;
934
935 if (sk->sk_state == DCCP_LISTEN) {
936 dccp_set_state(sk, DCCP_CLOSED);
937
938 /* Special case. */
939 inet_csk_listen_stop(sk);
940
941 goto adjudge_to_death;
942 }
943
Ian McDonald97e58482006-08-26 19:16:45 -0700944 sk_stop_timer(sk, &dp->dccps_xmit_timer);
945
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700946 /*
947 * We need to flush the recv. buffs. We do this only on the
948 * descriptor close, not protocol-sourced closes, because the
949 *reader process may not have drained the data yet!
950 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700951 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800952 data_was_unread += skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700953 __kfree_skb(skb);
954 }
955
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800956 if (data_was_unread) {
957 /* Unread data was tossed, send an appropriate Reset Code */
958 DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
959 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
960 dccp_set_state(sk, DCCP_CLOSED);
961 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700962 /* Check zero linger _after_ checking for unread data. */
963 sk->sk_prot->disconnect(sk, 0);
Gerrit Renker0c869622007-11-28 11:59:48 -0200964 } else if (sk->sk_state != DCCP_CLOSED) {
965 dccp_terminate_connection(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700966 }
967
968 sk_stream_wait_close(sk, timeout);
969
970adjudge_to_death:
Herbert Xu134af342006-05-05 17:09:13 -0700971 state = sk->sk_state;
972 sock_hold(sk);
973 sock_orphan(sk);
974 atomic_inc(sk->sk_prot->orphan_count);
975
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700976 /*
977 * It is the last release_sock in its life. It will remove backlog.
978 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700979 release_sock(sk);
980 /*
981 * Now socket is owned by kernel and we acquire BH lock
982 * to finish close. No need to check for user refs.
983 */
984 local_bh_disable();
985 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700986 WARN_ON(sock_owned_by_user(sk));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700987
Herbert Xu134af342006-05-05 17:09:13 -0700988 /* Have we already been destroyed by a softirq or backlog? */
989 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
990 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700991
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700992 if (sk->sk_state == DCCP_CLOSED)
993 inet_csk_destroy_sock(sk);
994
995 /* Otherwise, socket is reprieved until protocol close. */
996
Herbert Xu134af342006-05-05 17:09:13 -0700997out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700998 bh_unlock_sock(sk);
999 local_bh_enable();
1000 sock_put(sk);
1001}
1002
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001003EXPORT_SYMBOL_GPL(dccp_close);
1004
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001005void dccp_shutdown(struct sock *sk, int how)
1006{
Gerrit Renker8e8c71f2007-11-21 09:56:48 -02001007 dccp_pr_debug("called shutdown(%x)\n", how);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001008}
1009
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001010EXPORT_SYMBOL_GPL(dccp_shutdown);
1011
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001012static inline int dccp_mib_init(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001013{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001014 return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001015}
1016
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001017static inline void dccp_mib_exit(void)
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001018{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001019 snmp_mib_free((void**)dccp_statistics);
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001020}
1021
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001022static int thash_entries;
1023module_param(thash_entries, int, 0444);
1024MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1025
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001026#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001027int dccp_debug;
Gerrit Renker43264992008-08-23 13:28:27 +02001028module_param(dccp_debug, bool, 0644);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001029MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001030
1031EXPORT_SYMBOL_GPL(dccp_debug);
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001032#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001033
1034static int __init dccp_init(void)
1035{
1036 unsigned long goal;
1037 int ehash_order, bhash_order, i;
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001038 int rc = -ENOBUFS;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001039
Patrick McHardy028b0272008-04-12 18:35:41 -07001040 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1041 FIELD_SIZEOF(struct sk_buff, cb));
1042
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001043 dccp_hashinfo.bind_bucket_cachep =
1044 kmem_cache_create("dccp_bind_bucket",
1045 sizeof(struct inet_bind_bucket), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001046 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001047 if (!dccp_hashinfo.bind_bucket_cachep)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001048 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001049
1050 /*
1051 * Size and allocate the main established and bind bucket
1052 * hash tables.
1053 *
1054 * The methodology is similar to that of the buffer cache.
1055 */
1056 if (num_physpages >= (128 * 1024))
1057 goal = num_physpages >> (21 - PAGE_SHIFT);
1058 else
1059 goal = num_physpages >> (23 - PAGE_SHIFT);
1060
1061 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001062 goal = (thash_entries *
1063 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001064 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1065 ;
1066 do {
1067 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1068 sizeof(struct inet_ehash_bucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001069 while (dccp_hashinfo.ehash_size &
1070 (dccp_hashinfo.ehash_size - 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001071 dccp_hashinfo.ehash_size--;
1072 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1073 __get_free_pages(GFP_ATOMIC, ehash_order);
1074 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1075
1076 if (!dccp_hashinfo.ehash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001077 DCCP_CRIT("Failed to allocate DCCP established hash table");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001078 goto out_free_bind_bucket_cachep;
1079 }
1080
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001081 for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001082 INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001083 INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].twchain);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001084 }
1085
Eric Dumazet230140c2007-11-07 02:40:20 -08001086 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1087 goto out_free_dccp_ehash;
1088
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001089 bhash_order = ehash_order;
1090
1091 do {
1092 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1093 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001094 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1095 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001096 continue;
1097 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1098 __get_free_pages(GFP_ATOMIC, bhash_order);
1099 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1100
1101 if (!dccp_hashinfo.bhash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001102 DCCP_CRIT("Failed to allocate DCCP bind hash table");
Eric Dumazet230140c2007-11-07 02:40:20 -08001103 goto out_free_dccp_locks;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001104 }
1105
1106 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1107 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1108 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1109 }
1110
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001111 rc = dccp_mib_init();
Arnaldo Carvalho de Melofa23e2e2006-03-20 17:16:01 -08001112 if (rc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001113 goto out_free_dccp_bhash;
1114
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001115 rc = dccp_ackvec_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001116 if (rc)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001117 goto out_free_dccp_mib;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001118
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001119 rc = dccp_sysctl_init();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001120 if (rc)
1121 goto out_ackvec_exit;
Gerrit Renker4c70f382007-09-25 22:40:13 -07001122
1123 dccp_timestamping_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001124out:
1125 return rc;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001126out_ackvec_exit:
1127 dccp_ackvec_exit();
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001128out_free_dccp_mib:
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001129 dccp_mib_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001130out_free_dccp_bhash:
1131 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1132 dccp_hashinfo.bhash = NULL;
Eric Dumazet230140c2007-11-07 02:40:20 -08001133out_free_dccp_locks:
1134 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001135out_free_dccp_ehash:
1136 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1137 dccp_hashinfo.ehash = NULL;
1138out_free_bind_bucket_cachep:
1139 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1140 dccp_hashinfo.bind_bucket_cachep = NULL;
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");