blob: ea85c423cdbdb6e672cea8d8935709faacae6ef9 [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
Eric Dumazet5caea4e2008-11-20 00:40:07 -080047struct inet_hashinfo dccp_hashinfo;
Arnaldo Carvalho de Melo075ae862006-03-20 21:24:19 -080048EXPORT_SYMBOL_GPL(dccp_hashinfo);
49
Ian McDonaldb1308dc2006-11-20 18:30:17 -020050/* the maximum queue length for tx in packets. 0 is no limit */
51int sysctl_dccp_tx_qlen __read_mostly = 5;
52
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080053void dccp_set_state(struct sock *sk, const int state)
54{
55 const int oldstate = sk->sk_state;
56
Gerrit Renkerf11135a2007-11-28 11:34:53 -020057 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080058 dccp_state_name(oldstate), dccp_state_name(state));
59 WARN_ON(state == oldstate);
60
61 switch (state) {
62 case DCCP_OPEN:
63 if (oldstate != DCCP_OPEN)
64 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
65 break;
66
67 case DCCP_CLOSED:
Gerrit Renker0c869622007-11-28 11:59:48 -020068 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
69 oldstate == DCCP_CLOSING)
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080070 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
71
72 sk->sk_prot->unhash(sk);
73 if (inet_csk(sk)->icsk_bind_hash != NULL &&
74 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -080075 inet_put_port(sk);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080076 /* fall through */
77 default:
78 if (oldstate == DCCP_OPEN)
79 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
80 }
81
82 /* Change state AFTER socket is unhashed to avoid closed
83 * socket sitting in hash tables.
84 */
85 sk->sk_state = state;
86}
87
88EXPORT_SYMBOL_GPL(dccp_set_state);
89
Gerrit Renker0c869622007-11-28 11:59:48 -020090static void dccp_finish_passive_close(struct sock *sk)
91{
92 switch (sk->sk_state) {
93 case DCCP_PASSIVE_CLOSE:
94 /* Node (client or server) has received Close packet. */
95 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
96 dccp_set_state(sk, DCCP_CLOSED);
97 break;
98 case DCCP_PASSIVE_CLOSEREQ:
99 /*
100 * Client received CloseReq. We set the `active' flag so that
101 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
102 */
103 dccp_send_close(sk, 1);
104 dccp_set_state(sk, DCCP_CLOSING);
105 }
106}
107
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800108void dccp_done(struct sock *sk)
109{
110 dccp_set_state(sk, DCCP_CLOSED);
111 dccp_clear_xmit_timers(sk);
112
113 sk->sk_shutdown = SHUTDOWN_MASK;
114
115 if (!sock_flag(sk, SOCK_DEAD))
116 sk->sk_state_change(sk);
117 else
118 inet_csk_destroy_sock(sk);
119}
120
121EXPORT_SYMBOL_GPL(dccp_done);
122
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700123const char *dccp_packet_name(const int type)
124{
125 static const char *dccp_packet_names[] = {
126 [DCCP_PKT_REQUEST] = "REQUEST",
127 [DCCP_PKT_RESPONSE] = "RESPONSE",
128 [DCCP_PKT_DATA] = "DATA",
129 [DCCP_PKT_ACK] = "ACK",
130 [DCCP_PKT_DATAACK] = "DATAACK",
131 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
132 [DCCP_PKT_CLOSE] = "CLOSE",
133 [DCCP_PKT_RESET] = "RESET",
134 [DCCP_PKT_SYNC] = "SYNC",
135 [DCCP_PKT_SYNCACK] = "SYNCACK",
136 };
137
138 if (type >= DCCP_NR_PKT_TYPES)
139 return "INVALID";
140 else
141 return dccp_packet_names[type];
142}
143
144EXPORT_SYMBOL_GPL(dccp_packet_name);
145
146const char *dccp_state_name(const int state)
147{
148 static char *dccp_state_names[] = {
Gerrit Renkerf11135a2007-11-28 11:34:53 -0200149 [DCCP_OPEN] = "OPEN",
150 [DCCP_REQUESTING] = "REQUESTING",
151 [DCCP_PARTOPEN] = "PARTOPEN",
152 [DCCP_LISTEN] = "LISTEN",
153 [DCCP_RESPOND] = "RESPOND",
154 [DCCP_CLOSING] = "CLOSING",
155 [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
156 [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
157 [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
158 [DCCP_TIME_WAIT] = "TIME_WAIT",
159 [DCCP_CLOSED] = "CLOSED",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700160 };
161
162 if (state >= DCCP_MAX_STATES)
163 return "INVALID STATE!";
164 else
165 return dccp_state_names[state];
166}
167
168EXPORT_SYMBOL_GPL(dccp_state_name);
169
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800170int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800171{
172 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200173 struct dccp_minisock *dmsk = dccp_msk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800174 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800175
Gerrit Renker410e27a2008-09-09 13:27:22 +0200176 dccp_minisock_init(&dp->dccps_minisock);
177
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200178 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
179 icsk->icsk_syn_retries = sysctl_dccp_request_retries;
180 sk->sk_state = DCCP_CLOSED;
181 sk->sk_write_space = dccp_write_space;
182 icsk->icsk_sync_mss = dccp_sync_mss;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200183 dp->dccps_mss_cache = 536;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200184 dp->dccps_rate_last = jiffies;
185 dp->dccps_role = DCCP_ROLE_UNDEFINED;
186 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200187 dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200188
189 dccp_init_xmit_timers(sk);
190
Gerrit Renkerac757732008-11-04 23:55:49 -0800191 INIT_LIST_HEAD(&dp->dccps_featneg);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200192 /*
193 * FIXME: We're hardcoding the CCID, and doing this at this point makes
194 * the listening (master) sock get CCID control blocks, which is not
195 * necessary, but for now, to not mess with the test userspace apps,
196 * lets leave it here, later the real solution is to do this in a
197 * setsockopt(CCIDs-I-want/accept). -acme
198 */
199 if (likely(ctl_sock_initialized)) {
Gerrit Renkere8ef9672008-11-12 00:43:40 -0800200 int rc = dccp_feat_init(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200201
202 if (rc)
203 return rc;
204
205 if (dmsk->dccpms_send_ack_vector) {
206 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
207 if (dp->dccps_hc_rx_ackvec == NULL)
208 return -ENOMEM;
209 }
210 dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
211 sk, GFP_KERNEL);
212 dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
213 sk, GFP_KERNEL);
214 if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
215 dp->dccps_hc_tx_ccid == NULL)) {
216 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
217 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
218 if (dmsk->dccpms_send_ack_vector) {
219 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
220 dp->dccps_hc_rx_ackvec = NULL;
221 }
222 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
223 return -ENOMEM;
224 }
225 } else {
226 /* control socket doesn't need feat nego */
227 INIT_LIST_HEAD(&dmsk->dccpms_pending);
228 INIT_LIST_HEAD(&dmsk->dccpms_conf);
229 }
230
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800231 return 0;
232}
233
234EXPORT_SYMBOL_GPL(dccp_init_sock);
235
Brian Haley7d06b2e2008-06-14 17:04:49 -0700236void dccp_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800237{
238 struct dccp_sock *dp = dccp_sk(sk);
Gerrit Renker410e27a2008-09-09 13:27:22 +0200239 struct dccp_minisock *dmsk = dccp_msk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800240
241 /*
242 * DCCP doesn't use sk_write_queue, just sk_send_head
243 * for retransmissions
244 */
245 if (sk->sk_send_head != NULL) {
246 kfree_skb(sk->sk_send_head);
247 sk->sk_send_head = NULL;
248 }
249
250 /* Clean up a referenced DCCP bind bucket. */
251 if (inet_csk(sk)->icsk_bind_hash != NULL)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800252 inet_put_port(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800253
254 kfree(dp->dccps_service_list);
255 dp->dccps_service_list = NULL;
256
Gerrit Renker410e27a2008-09-09 13:27:22 +0200257 if (dmsk->dccpms_send_ack_vector) {
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800258 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
259 dp->dccps_hc_rx_ackvec = NULL;
260 }
261 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
262 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
263 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
264
265 /* clean up feature negotiation state */
Gerrit Renkerd99a7bd2008-11-04 23:56:30 -0800266 dccp_feat_list_purge(&dp->dccps_featneg);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800267}
268
269EXPORT_SYMBOL_GPL(dccp_destroy_sock);
270
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800271static inline int dccp_listen_start(struct sock *sk, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700272{
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700273 struct dccp_sock *dp = dccp_sk(sk);
274
275 dp->dccps_role = DCCP_ROLE_LISTEN;
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800276 /* do not start to listen if feature negotiation setup fails */
277 if (dccp_feat_finalise_settings(dp))
278 return -EPROTO;
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800279 return inet_csk_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700280}
281
Gerrit Renkerce865a62007-11-24 22:14:15 -0200282static inline int dccp_need_reset(int state)
283{
284 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
285 state != DCCP_REQUESTING;
286}
287
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700288int dccp_disconnect(struct sock *sk, int flags)
289{
290 struct inet_connection_sock *icsk = inet_csk(sk);
291 struct inet_sock *inet = inet_sk(sk);
292 int err = 0;
293 const int old_state = sk->sk_state;
294
295 if (old_state != DCCP_CLOSED)
296 dccp_set_state(sk, DCCP_CLOSED);
297
Gerrit Renkerce865a62007-11-24 22:14:15 -0200298 /*
299 * This corresponds to the ABORT function of RFC793, sec. 3.8
300 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
301 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700302 if (old_state == DCCP_LISTEN) {
303 inet_csk_listen_stop(sk);
Gerrit Renkerce865a62007-11-24 22:14:15 -0200304 } else if (dccp_need_reset(old_state)) {
305 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
306 sk->sk_err = ECONNRESET;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307 } else if (old_state == DCCP_REQUESTING)
308 sk->sk_err = ECONNRESET;
309
310 dccp_clear_xmit_timers(sk);
Gerrit Renker48816322008-08-23 13:28:27 +0200311
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700312 __skb_queue_purge(&sk->sk_receive_queue);
Gerrit Renker48816322008-08-23 13:28:27 +0200313 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700314 if (sk->sk_send_head != NULL) {
315 __kfree_skb(sk->sk_send_head);
316 sk->sk_send_head = NULL;
317 }
318
319 inet->dport = 0;
320
321 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
322 inet_reset_saddr(sk);
323
324 sk->sk_shutdown = 0;
325 sock_reset_flag(sk, SOCK_DONE);
326
327 icsk->icsk_backoff = 0;
328 inet_csk_delack_init(sk);
329 __sk_dst_reset(sk);
330
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700331 WARN_ON(inet->num && !icsk->icsk_bind_hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700332
333 sk->sk_error_report(sk);
334 return err;
335}
336
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800337EXPORT_SYMBOL_GPL(dccp_disconnect);
338
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700339/*
340 * Wait for a DCCP event.
341 *
342 * Note that we don't need to lock the socket, as the upper poll layers
343 * take care of normal races (between the test and the event) and we don't
344 * go look at any of the socket buffers directly.
345 */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800346unsigned int dccp_poll(struct file *file, struct socket *sock,
347 poll_table *wait)
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700348{
349 unsigned int mask;
350 struct sock *sk = sock->sk;
351
352 poll_wait(file, sk->sk_sleep, wait);
353 if (sk->sk_state == DCCP_LISTEN)
354 return inet_csk_listen_poll(sk);
355
356 /* Socket is not locked. We are protected from async events
357 by poll logic and correct handling of state changes
358 made by another threads is impossible in any case.
359 */
360
361 mask = 0;
362 if (sk->sk_err)
363 mask = POLLERR;
364
365 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
366 mask |= POLLHUP;
367 if (sk->sk_shutdown & RCV_SHUTDOWN)
Davide Libenzif348d702006-03-25 03:07:39 -0800368 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700369
370 /* Connected? */
371 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
372 if (atomic_read(&sk->sk_rmem_alloc) > 0)
373 mask |= POLLIN | POLLRDNORM;
374
375 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
376 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
377 mask |= POLLOUT | POLLWRNORM;
378 } else { /* send SIGIO later */
379 set_bit(SOCK_ASYNC_NOSPACE,
380 &sk->sk_socket->flags);
381 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
382
383 /* Race breaker. If space is freed after
384 * wspace test but before the flags are set,
385 * IO signal will be lost.
386 */
387 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
388 mask |= POLLOUT | POLLWRNORM;
389 }
390 }
391 }
392 return mask;
393}
394
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800395EXPORT_SYMBOL_GPL(dccp_poll);
396
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700397int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
398{
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700399 int rc = -ENOTCONN;
400
401 lock_sock(sk);
402
403 if (sk->sk_state == DCCP_LISTEN)
404 goto out;
405
406 switch (cmd) {
407 case SIOCINQ: {
408 struct sk_buff *skb;
409 unsigned long amount = 0;
410
411 skb = skb_peek(&sk->sk_receive_queue);
412 if (skb != NULL) {
413 /*
414 * We will only return the amount of this packet since
415 * that is all that will be read.
416 */
417 amount = skb->len;
418 }
419 rc = put_user(amount, (int __user *)arg);
420 }
421 break;
422 default:
423 rc = -ENOIOCTLCMD;
424 break;
425 }
426out:
427 release_sock(sk);
428 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700429}
430
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800431EXPORT_SYMBOL_GPL(dccp_ioctl);
432
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800433static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700434 char __user *optval, int optlen)
435{
436 struct dccp_sock *dp = dccp_sk(sk);
437 struct dccp_service_list *sl = NULL;
438
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200439 if (service == DCCP_SERVICE_INVALID_VALUE ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700440 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
441 return -EINVAL;
442
443 if (optlen > sizeof(service)) {
444 sl = kmalloc(optlen, GFP_KERNEL);
445 if (sl == NULL)
446 return -ENOMEM;
447
448 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
449 if (copy_from_user(sl->dccpsl_list,
450 optval + sizeof(service),
451 optlen - sizeof(service)) ||
452 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
453 kfree(sl);
454 return -EFAULT;
455 }
456 }
457
458 lock_sock(sk);
459 dp->dccps_service = service;
460
Jesper Juhla51482b2005-11-08 09:41:34 -0800461 kfree(dp->dccps_service_list);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700462
463 dp->dccps_service_list = sl;
464 release_sock(sk);
465 return 0;
466}
467
Gerrit Renker29450552008-11-16 22:53:48 -0800468static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
469{
470 u8 *list, len;
471 int i, rc;
472
473 if (cscov < 0 || cscov > 15)
474 return -EINVAL;
475 /*
476 * Populate a list of permissible values, in the range cscov...15. This
477 * is necessary since feature negotiation of single values only works if
478 * both sides incidentally choose the same value. Since the list starts
479 * lowest-value first, negotiation will pick the smallest shared value.
480 */
481 if (cscov == 0)
482 return 0;
483 len = 16 - cscov;
484
485 list = kmalloc(len, GFP_KERNEL);
486 if (list == NULL)
487 return -ENOBUFS;
488
489 for (i = 0; i < len; i++)
490 list[i] = cscov++;
491
492 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
493
494 if (rc == 0) {
495 if (rx)
496 dccp_sk(sk)->dccps_pcrlen = cscov;
497 else
498 dccp_sk(sk)->dccps_pcslen = cscov;
499 }
500 kfree(list);
501 return rc;
502}
503
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800504static int dccp_setsockopt_ccid(struct sock *sk, int type,
505 char __user *optval, int optlen)
506{
507 u8 *val;
508 int rc = 0;
509
510 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
511 return -EINVAL;
512
513 val = kmalloc(optlen, GFP_KERNEL);
514 if (val == NULL)
515 return -ENOMEM;
516
517 if (copy_from_user(val, optval, optlen)) {
518 kfree(val);
519 return -EFAULT;
520 }
521
522 lock_sock(sk);
523 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
524 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
525
526 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
527 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
528 release_sock(sk);
529
530 kfree(val);
531 return rc;
532}
533
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800534static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
535 char __user *optval, int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700536{
Gerrit Renker09dbc382006-11-14 12:57:34 -0200537 struct dccp_sock *dp = dccp_sk(sk);
538 int val, err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700539
Gerrit Renker19102992008-11-16 22:56:55 -0800540 switch (optname) {
541 case DCCP_SOCKOPT_PACKET_SIZE:
542 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
543 return 0;
544 case DCCP_SOCKOPT_CHANGE_L:
545 case DCCP_SOCKOPT_CHANGE_R:
546 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
547 return 0;
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800548 case DCCP_SOCKOPT_CCID:
549 case DCCP_SOCKOPT_RX_CCID:
550 case DCCP_SOCKOPT_TX_CCID:
551 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
Gerrit Renker19102992008-11-16 22:56:55 -0800552 }
553
554 if (optlen < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300555 return -EINVAL;
556
557 if (get_user(val, (int __user *)optval))
558 return -EFAULT;
559
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700560 if (optname == DCCP_SOCKOPT_SERVICE)
561 return dccp_setsockopt_service(sk, val, optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300562
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700563 lock_sock(sk);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300564 switch (optname) {
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200565 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
566 if (dp->dccps_role != DCCP_ROLE_SERVER)
567 err = -EOPNOTSUPP;
568 else
569 dp->dccps_server_timewait = (val != 0);
570 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800571 case DCCP_SOCKOPT_SEND_CSCOV:
572 err = dccp_setsockopt_cscov(sk, val, false);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200573 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800574 case DCCP_SOCKOPT_RECV_CSCOV:
575 err = dccp_setsockopt_cscov(sk, val, true);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200576 break;
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300577 default:
578 err = -ENOPROTOOPT;
579 break;
580 }
Gerrit Renker410e27a2008-09-09 13:27:22 +0200581 release_sock(sk);
Gerrit Renker19102992008-11-16 22:56:55 -0800582
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300583 return err;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700584}
585
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800586int dccp_setsockopt(struct sock *sk, int level, int optname,
587 char __user *optval, int optlen)
588{
589 if (level != SOL_DCCP)
590 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
591 optname, optval,
592 optlen);
593 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
594}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800595
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800596EXPORT_SYMBOL_GPL(dccp_setsockopt);
597
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800598#ifdef CONFIG_COMPAT
599int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800600 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800601{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800602 if (level != SOL_DCCP)
603 return inet_csk_compat_setsockopt(sk, level, optname,
604 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800605 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
606}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800607
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800608EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
609#endif
610
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700611static int dccp_getsockopt_service(struct sock *sk, int len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800612 __be32 __user *optval,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700613 int __user *optlen)
614{
615 const struct dccp_sock *dp = dccp_sk(sk);
616 const struct dccp_service_list *sl;
617 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
618
619 lock_sock(sk);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700620 if ((sl = dp->dccps_service_list) != NULL) {
621 slen = sl->dccpsl_nr * sizeof(u32);
622 total_len += slen;
623 }
624
625 err = -EINVAL;
626 if (total_len > len)
627 goto out;
628
629 err = 0;
630 if (put_user(total_len, optlen) ||
631 put_user(dp->dccps_service, optval) ||
632 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
633 err = -EFAULT;
634out:
635 release_sock(sk);
636 return err;
637}
638
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800639static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300640 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700641{
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300642 struct dccp_sock *dp;
643 int val, len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700644
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300645 if (get_user(len, optlen))
646 return -EFAULT;
647
Arnaldo Carvalho de Melo39ebc022007-03-28 11:54:32 -0700648 if (len < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300649 return -EINVAL;
650
651 dp = dccp_sk(sk);
652
653 switch (optname) {
654 case DCCP_SOCKOPT_PACKET_SIZE:
Gerrit Renker5aed3242006-11-28 19:33:36 -0200655 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
Arnaldo Carvalho de Melo841bac12006-11-28 19:42:03 -0200656 return 0;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700657 case DCCP_SOCKOPT_SERVICE:
658 return dccp_getsockopt_service(sk, len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800659 (__be32 __user *)optval, optlen);
Gerrit Renker7c559a92007-10-04 14:39:22 -0700660 case DCCP_SOCKOPT_GET_CUR_MPS:
661 val = dp->dccps_mss_cache;
Gerrit Renker7c559a92007-10-04 14:39:22 -0700662 break;
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800663 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
664 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
Gerrit Renker71c262a2008-11-23 16:04:59 -0800665 case DCCP_SOCKOPT_TX_CCID:
666 val = ccid_get_current_tx_ccid(dp);
667 if (val < 0)
668 return -ENOPROTOOPT;
669 break;
670 case DCCP_SOCKOPT_RX_CCID:
671 val = ccid_get_current_rx_ccid(dp);
672 if (val < 0)
673 return -ENOPROTOOPT;
674 break;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200675 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
676 val = dp->dccps_server_timewait;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200677 break;
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200678 case DCCP_SOCKOPT_SEND_CSCOV:
679 val = dp->dccps_pcslen;
680 break;
681 case DCCP_SOCKOPT_RECV_CSCOV:
682 val = dp->dccps_pcrlen;
683 break;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700684 case 128 ... 191:
685 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
686 len, (u32 __user *)optval, optlen);
687 case 192 ... 255:
688 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
689 len, (u32 __user *)optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300690 default:
691 return -ENOPROTOOPT;
692 }
693
Gerrit Renker79133502007-12-13 12:27:14 -0200694 len = sizeof(val);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300695 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
696 return -EFAULT;
697
698 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700699}
700
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800701int dccp_getsockopt(struct sock *sk, int level, int optname,
702 char __user *optval, int __user *optlen)
703{
704 if (level != SOL_DCCP)
705 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
706 optname, optval,
707 optlen);
708 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
709}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800710
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800711EXPORT_SYMBOL_GPL(dccp_getsockopt);
712
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800713#ifdef CONFIG_COMPAT
714int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800715 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800716{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800717 if (level != SOL_DCCP)
718 return inet_csk_compat_getsockopt(sk, level, optname,
719 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800720 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
721}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800722
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800723EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
724#endif
725
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700726int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
727 size_t len)
728{
729 const struct dccp_sock *dp = dccp_sk(sk);
730 const int flags = msg->msg_flags;
731 const int noblock = flags & MSG_DONTWAIT;
732 struct sk_buff *skb;
733 int rc, size;
734 long timeo;
735
736 if (len > dp->dccps_mss_cache)
737 return -EMSGSIZE;
738
739 lock_sock(sk);
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200740
Gerrit Renker410e27a2008-09-09 13:27:22 +0200741 if (sysctl_dccp_tx_qlen &&
742 (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200743 rc = -EAGAIN;
744 goto out_release;
745 }
746
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700747 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700748
749 /*
750 * We have to use sk_stream_wait_connect here to set sk_write_pending,
751 * so that the trick in dccp_rcv_request_sent_state_process.
752 */
753 /* Wait for a connection to finish. */
Gerrit Renkercecd8d02007-09-26 19:36:08 -0300754 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700755 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700756 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700757
758 size = sk->sk_prot->max_header + len;
759 release_sock(sk);
760 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
761 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700762 if (skb == NULL)
763 goto out_release;
764
765 skb_reserve(skb, sk->sk_prot->max_header);
766 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700767 if (rc != 0)
768 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700769
Gerrit Renker410e27a2008-09-09 13:27:22 +0200770 skb_queue_tail(&sk->sk_write_queue, skb);
771 dccp_write_xmit(sk,0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700772out_release:
773 release_sock(sk);
774 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700775out_discard:
776 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700777 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700778}
779
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800780EXPORT_SYMBOL_GPL(dccp_sendmsg);
781
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700782int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
783 size_t len, int nonblock, int flags, int *addr_len)
784{
785 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700786 long timeo;
787
788 lock_sock(sk);
789
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300790 if (sk->sk_state == DCCP_LISTEN) {
791 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700792 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300793 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700794
795 timeo = sock_rcvtimeo(sk, nonblock);
796
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700797 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300798 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700799
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300800 if (skb == NULL)
801 goto verify_sock_status;
802
803 dh = dccp_hdr(skb);
804
Gerrit Renker0c869622007-11-28 11:59:48 -0200805 switch (dh->dccph_type) {
806 case DCCP_PKT_DATA:
807 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300808 goto found_ok_skb;
809
Gerrit Renker0c869622007-11-28 11:59:48 -0200810 case DCCP_PKT_CLOSE:
811 case DCCP_PKT_CLOSEREQ:
812 if (!(flags & MSG_PEEK))
813 dccp_finish_passive_close(sk);
814 /* fall through */
815 case DCCP_PKT_RESET:
816 dccp_pr_debug("found fin (%s) ok!\n",
817 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300818 len = 0;
819 goto found_fin_ok;
Gerrit Renker0c869622007-11-28 11:59:48 -0200820 default:
821 dccp_pr_debug("packet_type=%s\n",
822 dccp_packet_name(dh->dccph_type));
823 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700824 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300825verify_sock_status:
826 if (sock_flag(sk, SOCK_DONE)) {
827 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700828 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700829 }
830
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300831 if (sk->sk_err) {
832 len = sock_error(sk);
833 break;
834 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300836 if (sk->sk_shutdown & RCV_SHUTDOWN) {
837 len = 0;
838 break;
839 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300841 if (sk->sk_state == DCCP_CLOSED) {
842 if (!sock_flag(sk, SOCK_DONE)) {
843 /* This occurs when user tries to read
844 * from never connected socket.
845 */
846 len = -ENOTCONN;
847 break;
848 }
849 len = 0;
850 break;
851 }
852
853 if (!timeo) {
854 len = -EAGAIN;
855 break;
856 }
857
858 if (signal_pending(current)) {
859 len = sock_intr_errno(timeo);
860 break;
861 }
862
863 sk_wait_data(sk, &timeo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700864 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700865 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300866 if (len > skb->len)
867 len = skb->len;
868 else if (len < skb->len)
869 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700870
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300871 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
872 /* Exception. Bailout! */
873 len = -EFAULT;
874 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700875 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700876 found_fin_ok:
877 if (!(flags & MSG_PEEK))
Chris Leech624d1162006-05-23 18:01:28 -0700878 sk_eat_skb(sk, skb, 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700879 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300880 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700881out:
882 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300883 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700884}
885
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800886EXPORT_SYMBOL_GPL(dccp_recvmsg);
887
888int inet_dccp_listen(struct socket *sock, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700889{
890 struct sock *sk = sock->sk;
891 unsigned char old_state;
892 int err;
893
894 lock_sock(sk);
895
896 err = -EINVAL;
897 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
898 goto out;
899
900 old_state = sk->sk_state;
901 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
902 goto out;
903
904 /* Really, if the socket is already in listen state
905 * we can only allow the backlog to be adjusted.
906 */
907 if (old_state != DCCP_LISTEN) {
908 /*
909 * FIXME: here it probably should be sk->sk_prot->listen_start
910 * see tcp_listen_start
911 */
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800912 err = dccp_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700913 if (err)
914 goto out;
915 }
916 sk->sk_max_ack_backlog = backlog;
917 err = 0;
918
919out:
920 release_sock(sk);
921 return err;
922}
923
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800924EXPORT_SYMBOL_GPL(inet_dccp_listen);
925
Gerrit Renker0c869622007-11-28 11:59:48 -0200926static void dccp_terminate_connection(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700927{
Gerrit Renker0c869622007-11-28 11:59:48 -0200928 u8 next_state = DCCP_CLOSED;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700929
Gerrit Renker0c869622007-11-28 11:59:48 -0200930 switch (sk->sk_state) {
931 case DCCP_PASSIVE_CLOSE:
932 case DCCP_PASSIVE_CLOSEREQ:
933 dccp_finish_passive_close(sk);
934 break;
935 case DCCP_PARTOPEN:
936 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
937 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
938 /* fall through */
939 case DCCP_OPEN:
940 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700941
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200942 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
943 !dccp_sk(sk)->dccps_server_timewait)
Gerrit Renker0c869622007-11-28 11:59:48 -0200944 next_state = DCCP_ACTIVE_CLOSEREQ;
945 else
946 next_state = DCCP_CLOSING;
947 /* fall through */
948 default:
949 dccp_set_state(sk, next_state);
950 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700951}
952
953void dccp_close(struct sock *sk, long timeout)
954{
Ian McDonald97e58482006-08-26 19:16:45 -0700955 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700956 struct sk_buff *skb;
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800957 u32 data_was_unread = 0;
Herbert Xu134af342006-05-05 17:09:13 -0700958 int state;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700959
960 lock_sock(sk);
961
962 sk->sk_shutdown = SHUTDOWN_MASK;
963
964 if (sk->sk_state == DCCP_LISTEN) {
965 dccp_set_state(sk, DCCP_CLOSED);
966
967 /* Special case. */
968 inet_csk_listen_stop(sk);
969
970 goto adjudge_to_death;
971 }
972
Ian McDonald97e58482006-08-26 19:16:45 -0700973 sk_stop_timer(sk, &dp->dccps_xmit_timer);
974
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700975 /*
976 * We need to flush the recv. buffs. We do this only on the
977 * descriptor close, not protocol-sourced closes, because the
978 *reader process may not have drained the data yet!
979 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700980 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800981 data_was_unread += skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700982 __kfree_skb(skb);
983 }
984
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800985 if (data_was_unread) {
986 /* Unread data was tossed, send an appropriate Reset Code */
987 DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
988 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
989 dccp_set_state(sk, DCCP_CLOSED);
990 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700991 /* Check zero linger _after_ checking for unread data. */
992 sk->sk_prot->disconnect(sk, 0);
Gerrit Renker0c869622007-11-28 11:59:48 -0200993 } else if (sk->sk_state != DCCP_CLOSED) {
994 dccp_terminate_connection(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700995 }
996
997 sk_stream_wait_close(sk, timeout);
998
999adjudge_to_death:
Herbert Xu134af342006-05-05 17:09:13 -07001000 state = sk->sk_state;
1001 sock_hold(sk);
1002 sock_orphan(sk);
1003 atomic_inc(sk->sk_prot->orphan_count);
1004
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -07001005 /*
1006 * It is the last release_sock in its life. It will remove backlog.
1007 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001008 release_sock(sk);
1009 /*
1010 * Now socket is owned by kernel and we acquire BH lock
1011 * to finish close. No need to check for user refs.
1012 */
1013 local_bh_disable();
1014 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001015 WARN_ON(sock_owned_by_user(sk));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001016
Herbert Xu134af342006-05-05 17:09:13 -07001017 /* Have we already been destroyed by a softirq or backlog? */
1018 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1019 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001020
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001021 if (sk->sk_state == DCCP_CLOSED)
1022 inet_csk_destroy_sock(sk);
1023
1024 /* Otherwise, socket is reprieved until protocol close. */
1025
Herbert Xu134af342006-05-05 17:09:13 -07001026out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001027 bh_unlock_sock(sk);
1028 local_bh_enable();
1029 sock_put(sk);
1030}
1031
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001032EXPORT_SYMBOL_GPL(dccp_close);
1033
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001034void dccp_shutdown(struct sock *sk, int how)
1035{
Gerrit Renker8e8c71f2007-11-21 09:56:48 -02001036 dccp_pr_debug("called shutdown(%x)\n", how);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001037}
1038
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001039EXPORT_SYMBOL_GPL(dccp_shutdown);
1040
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001041static inline int dccp_mib_init(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001042{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001043 return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001044}
1045
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001046static inline void dccp_mib_exit(void)
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001047{
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001048 snmp_mib_free((void**)dccp_statistics);
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001049}
1050
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001051static int thash_entries;
1052module_param(thash_entries, int, 0444);
1053MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1054
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001055#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001056int dccp_debug;
Gerrit Renker43264992008-08-23 13:28:27 +02001057module_param(dccp_debug, bool, 0644);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001058MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001059
1060EXPORT_SYMBOL_GPL(dccp_debug);
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001061#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001062
1063static int __init dccp_init(void)
1064{
1065 unsigned long goal;
1066 int ehash_order, bhash_order, i;
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001067 int rc = -ENOBUFS;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001068
Patrick McHardy028b0272008-04-12 18:35:41 -07001069 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1070 FIELD_SIZEOF(struct sk_buff, cb));
1071
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001072 inet_hashinfo_init(&dccp_hashinfo);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001073 dccp_hashinfo.bind_bucket_cachep =
1074 kmem_cache_create("dccp_bind_bucket",
1075 sizeof(struct inet_bind_bucket), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001076 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001077 if (!dccp_hashinfo.bind_bucket_cachep)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001078 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001079
1080 /*
1081 * Size and allocate the main established and bind bucket
1082 * hash tables.
1083 *
1084 * The methodology is similar to that of the buffer cache.
1085 */
1086 if (num_physpages >= (128 * 1024))
1087 goal = num_physpages >> (21 - PAGE_SHIFT);
1088 else
1089 goal = num_physpages >> (23 - PAGE_SHIFT);
1090
1091 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001092 goal = (thash_entries *
1093 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001094 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1095 ;
1096 do {
1097 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1098 sizeof(struct inet_ehash_bucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001099 while (dccp_hashinfo.ehash_size &
1100 (dccp_hashinfo.ehash_size - 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001101 dccp_hashinfo.ehash_size--;
1102 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1103 __get_free_pages(GFP_ATOMIC, ehash_order);
1104 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1105
1106 if (!dccp_hashinfo.ehash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001107 DCCP_CRIT("Failed to allocate DCCP established hash table");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001108 goto out_free_bind_bucket_cachep;
1109 }
1110
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001111 for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001112 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
1113 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001114 }
1115
Eric Dumazet230140c2007-11-07 02:40:20 -08001116 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1117 goto out_free_dccp_ehash;
1118
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001119 bhash_order = ehash_order;
1120
1121 do {
1122 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1123 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001124 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1125 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001126 continue;
1127 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1128 __get_free_pages(GFP_ATOMIC, bhash_order);
1129 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1130
1131 if (!dccp_hashinfo.bhash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001132 DCCP_CRIT("Failed to allocate DCCP bind hash table");
Eric Dumazet230140c2007-11-07 02:40:20 -08001133 goto out_free_dccp_locks;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001134 }
1135
1136 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1137 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1138 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1139 }
1140
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001141 rc = dccp_mib_init();
Arnaldo Carvalho de Melofa23e2e2006-03-20 17:16:01 -08001142 if (rc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001143 goto out_free_dccp_bhash;
1144
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001145 rc = dccp_ackvec_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001146 if (rc)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001147 goto out_free_dccp_mib;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001148
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001149 rc = dccp_sysctl_init();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001150 if (rc)
1151 goto out_ackvec_exit;
Gerrit Renker4c70f382007-09-25 22:40:13 -07001152
1153 dccp_timestamping_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001154out:
1155 return rc;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001156out_ackvec_exit:
1157 dccp_ackvec_exit();
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001158out_free_dccp_mib:
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001159 dccp_mib_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001160out_free_dccp_bhash:
1161 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1162 dccp_hashinfo.bhash = NULL;
Eric Dumazet230140c2007-11-07 02:40:20 -08001163out_free_dccp_locks:
1164 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001165out_free_dccp_ehash:
1166 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1167 dccp_hashinfo.ehash = NULL;
1168out_free_bind_bucket_cachep:
1169 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1170 dccp_hashinfo.bind_bucket_cachep = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001171 goto out;
1172}
1173
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001174static void __exit dccp_fini(void)
1175{
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001176 dccp_mib_exit();
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001177 free_pages((unsigned long)dccp_hashinfo.bhash,
1178 get_order(dccp_hashinfo.bhash_size *
1179 sizeof(struct inet_bind_hashbucket)));
1180 free_pages((unsigned long)dccp_hashinfo.ehash,
1181 get_order(dccp_hashinfo.ehash_size *
1182 sizeof(struct inet_ehash_bucket)));
Eric Dumazet230140c2007-11-07 02:40:20 -08001183 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001184 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001185 dccp_ackvec_exit();
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001186 dccp_sysctl_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001187}
1188
1189module_init(dccp_init);
1190module_exit(dccp_fini);
1191
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001192MODULE_LICENSE("GPL");
1193MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1194MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");