Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6] / net / dccp / proto.c
1 /*
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
12 #include <linux/config.h>
13 #include <linux/dccp.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/skbuff.h>
19 #include <linux/netdevice.h>
20 #include <linux/in.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <linux/random.h>
24 #include <net/checksum.h>
25
26 #include <net/inet_sock.h>
27 #include <net/sock.h>
28 #include <net/xfrm.h>
29
30 #include <asm/semaphore.h>
31 #include <linux/spinlock.h>
32 #include <linux/timer.h>
33 #include <linux/delay.h>
34 #include <linux/poll.h>
35
36 #include "ccid.h"
37 #include "dccp.h"
38 #include "feat.h"
39
40 DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
41
42 EXPORT_SYMBOL_GPL(dccp_statistics);
43
44 atomic_t dccp_orphan_count = ATOMIC_INIT(0);
45
46 EXPORT_SYMBOL_GPL(dccp_orphan_count);
47
48 struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
49         .lhash_lock     = RW_LOCK_UNLOCKED,
50         .lhash_users    = ATOMIC_INIT(0),
51         .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
52 };
53
54 EXPORT_SYMBOL_GPL(dccp_hashinfo);
55
56 void dccp_set_state(struct sock *sk, const int state)
57 {
58         const int oldstate = sk->sk_state;
59
60         dccp_pr_debug("%s(%p) %-10.10s -> %s\n",
61                       dccp_role(sk), sk,
62                       dccp_state_name(oldstate), dccp_state_name(state));
63         WARN_ON(state == oldstate);
64
65         switch (state) {
66         case DCCP_OPEN:
67                 if (oldstate != DCCP_OPEN)
68                         DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
69                 break;
70
71         case DCCP_CLOSED:
72                 if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN)
73                         DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
74
75                 sk->sk_prot->unhash(sk);
76                 if (inet_csk(sk)->icsk_bind_hash != NULL &&
77                     !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
78                         inet_put_port(&dccp_hashinfo, sk);
79                 /* fall through */
80         default:
81                 if (oldstate == DCCP_OPEN)
82                         DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
83         }
84
85         /* Change state AFTER socket is unhashed to avoid closed
86          * socket sitting in hash tables.
87          */
88         sk->sk_state = state;
89 }
90
91 EXPORT_SYMBOL_GPL(dccp_set_state);
92
93 void dccp_done(struct sock *sk)
94 {
95         dccp_set_state(sk, DCCP_CLOSED);
96         dccp_clear_xmit_timers(sk);
97
98         sk->sk_shutdown = SHUTDOWN_MASK;
99
100         if (!sock_flag(sk, SOCK_DEAD))
101                 sk->sk_state_change(sk);
102         else
103                 inet_csk_destroy_sock(sk);
104 }
105
106 EXPORT_SYMBOL_GPL(dccp_done);
107
108 const char *dccp_packet_name(const int type)
109 {
110         static const char *dccp_packet_names[] = {
111                 [DCCP_PKT_REQUEST]  = "REQUEST",
112                 [DCCP_PKT_RESPONSE] = "RESPONSE",
113                 [DCCP_PKT_DATA]     = "DATA",
114                 [DCCP_PKT_ACK]      = "ACK",
115                 [DCCP_PKT_DATAACK]  = "DATAACK",
116                 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
117                 [DCCP_PKT_CLOSE]    = "CLOSE",
118                 [DCCP_PKT_RESET]    = "RESET",
119                 [DCCP_PKT_SYNC]     = "SYNC",
120                 [DCCP_PKT_SYNCACK]  = "SYNCACK",
121         };
122
123         if (type >= DCCP_NR_PKT_TYPES)
124                 return "INVALID";
125         else
126                 return dccp_packet_names[type];
127 }
128
129 EXPORT_SYMBOL_GPL(dccp_packet_name);
130
131 const char *dccp_state_name(const int state)
132 {
133         static char *dccp_state_names[] = {
134         [DCCP_OPEN]       = "OPEN",
135         [DCCP_REQUESTING] = "REQUESTING",
136         [DCCP_PARTOPEN]   = "PARTOPEN",
137         [DCCP_LISTEN]     = "LISTEN",
138         [DCCP_RESPOND]    = "RESPOND",
139         [DCCP_CLOSING]    = "CLOSING",
140         [DCCP_TIME_WAIT]  = "TIME_WAIT",
141         [DCCP_CLOSED]     = "CLOSED",
142         };
143
144         if (state >= DCCP_MAX_STATES)
145                 return "INVALID STATE!";
146         else
147                 return dccp_state_names[state];
148 }
149
150 EXPORT_SYMBOL_GPL(dccp_state_name);
151
152 void dccp_hash(struct sock *sk)
153 {
154         inet_hash(&dccp_hashinfo, sk);
155 }
156
157 EXPORT_SYMBOL_GPL(dccp_hash);
158
159 void dccp_unhash(struct sock *sk)
160 {
161         inet_unhash(&dccp_hashinfo, sk);
162 }
163
164 EXPORT_SYMBOL_GPL(dccp_unhash);
165
166 int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
167 {
168         struct dccp_sock *dp = dccp_sk(sk);
169         struct dccp_minisock *dmsk = dccp_msk(sk);
170         struct inet_connection_sock *icsk = inet_csk(sk);
171
172         dccp_minisock_init(&dp->dccps_minisock);
173         do_gettimeofday(&dp->dccps_epoch);
174
175         /*
176          * FIXME: We're hardcoding the CCID, and doing this at this point makes
177          * the listening (master) sock get CCID control blocks, which is not
178          * necessary, but for now, to not mess with the test userspace apps,
179          * lets leave it here, later the real solution is to do this in a
180          * setsockopt(CCIDs-I-want/accept). -acme
181          */
182         if (likely(ctl_sock_initialized)) {
183                 int rc = dccp_feat_init(dmsk);
184
185                 if (rc)
186                         return rc;
187
188                 if (dmsk->dccpms_send_ack_vector) {
189                         dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
190                         if (dp->dccps_hc_rx_ackvec == NULL)
191                                 return -ENOMEM;
192                 }
193                 dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
194                                                       sk, GFP_KERNEL);
195                 dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
196                                                       sk, GFP_KERNEL);
197                 if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
198                              dp->dccps_hc_tx_ccid == NULL)) {
199                         ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
200                         ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
201                         if (dmsk->dccpms_send_ack_vector) {
202                                 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
203                                 dp->dccps_hc_rx_ackvec = NULL;
204                         }
205                         dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
206                         return -ENOMEM;
207                 }
208         } else {
209                 /* control socket doesn't need feat nego */
210                 INIT_LIST_HEAD(&dmsk->dccpms_pending);
211                 INIT_LIST_HEAD(&dmsk->dccpms_conf);
212         }
213
214         dccp_init_xmit_timers(sk);
215         icsk->icsk_rto          = DCCP_TIMEOUT_INIT;
216         sk->sk_state            = DCCP_CLOSED;
217         sk->sk_write_space      = dccp_write_space;
218         icsk->icsk_sync_mss     = dccp_sync_mss;
219         dp->dccps_mss_cache     = 536;
220         dp->dccps_role          = DCCP_ROLE_UNDEFINED;
221         dp->dccps_service       = DCCP_SERVICE_INVALID_VALUE;
222         dp->dccps_l_ack_ratio   = dp->dccps_r_ack_ratio = 1;
223
224         return 0;
225 }
226
227 EXPORT_SYMBOL_GPL(dccp_init_sock);
228
229 int dccp_destroy_sock(struct sock *sk)
230 {
231         struct dccp_sock *dp = dccp_sk(sk);
232         struct dccp_minisock *dmsk = dccp_msk(sk);
233
234         /*
235          * DCCP doesn't use sk_write_queue, just sk_send_head
236          * for retransmissions
237          */
238         if (sk->sk_send_head != NULL) {
239                 kfree_skb(sk->sk_send_head);
240                 sk->sk_send_head = NULL;
241         }
242
243         /* Clean up a referenced DCCP bind bucket. */
244         if (inet_csk(sk)->icsk_bind_hash != NULL)
245                 inet_put_port(&dccp_hashinfo, sk);
246
247         kfree(dp->dccps_service_list);
248         dp->dccps_service_list = NULL;
249
250         if (dmsk->dccpms_send_ack_vector) {
251                 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
252                 dp->dccps_hc_rx_ackvec = NULL;
253         }
254         ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
255         ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
256         dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
257
258         /* clean up feature negotiation state */
259         dccp_feat_clean(dmsk);
260
261         return 0;
262 }
263
264 EXPORT_SYMBOL_GPL(dccp_destroy_sock);
265
266 static inline int dccp_listen_start(struct sock *sk)
267 {
268         struct dccp_sock *dp = dccp_sk(sk);
269
270         dp->dccps_role = DCCP_ROLE_LISTEN;
271         /*
272          * Apps need to use setsockopt(DCCP_SOCKOPT_SERVICE)
273          * before calling listen()
274          */
275         if (dccp_service_not_initialized(sk))
276                 return -EPROTO;
277         return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
278 }
279
280 int dccp_disconnect(struct sock *sk, int flags)
281 {
282         struct inet_connection_sock *icsk = inet_csk(sk);
283         struct inet_sock *inet = inet_sk(sk);
284         int err = 0;
285         const int old_state = sk->sk_state;
286
287         if (old_state != DCCP_CLOSED)
288                 dccp_set_state(sk, DCCP_CLOSED);
289
290         /* ABORT function of RFC793 */
291         if (old_state == DCCP_LISTEN) {
292                 inet_csk_listen_stop(sk);
293         /* FIXME: do the active reset thing */
294         } else if (old_state == DCCP_REQUESTING)
295                 sk->sk_err = ECONNRESET;
296
297         dccp_clear_xmit_timers(sk);
298         __skb_queue_purge(&sk->sk_receive_queue);
299         if (sk->sk_send_head != NULL) {
300                 __kfree_skb(sk->sk_send_head);
301                 sk->sk_send_head = NULL;
302         }
303
304         inet->dport = 0;
305
306         if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
307                 inet_reset_saddr(sk);
308
309         sk->sk_shutdown = 0;
310         sock_reset_flag(sk, SOCK_DONE);
311
312         icsk->icsk_backoff = 0;
313         inet_csk_delack_init(sk);
314         __sk_dst_reset(sk);
315
316         BUG_TRAP(!inet->num || icsk->icsk_bind_hash);
317
318         sk->sk_error_report(sk);
319         return err;
320 }
321
322 EXPORT_SYMBOL_GPL(dccp_disconnect);
323
324 /*
325  *      Wait for a DCCP event.
326  *
327  *      Note that we don't need to lock the socket, as the upper poll layers
328  *      take care of normal races (between the test and the event) and we don't
329  *      go look at any of the socket buffers directly.
330  */
331 unsigned int dccp_poll(struct file *file, struct socket *sock,
332                        poll_table *wait)
333 {
334         unsigned int mask;
335         struct sock *sk = sock->sk;
336
337         poll_wait(file, sk->sk_sleep, wait);
338         if (sk->sk_state == DCCP_LISTEN)
339                 return inet_csk_listen_poll(sk);
340
341         /* Socket is not locked. We are protected from async events
342            by poll logic and correct handling of state changes
343            made by another threads is impossible in any case.
344          */
345
346         mask = 0;
347         if (sk->sk_err)
348                 mask = POLLERR;
349
350         if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
351                 mask |= POLLHUP;
352         if (sk->sk_shutdown & RCV_SHUTDOWN)
353                 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
354
355         /* Connected? */
356         if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
357                 if (atomic_read(&sk->sk_rmem_alloc) > 0)
358                         mask |= POLLIN | POLLRDNORM;
359
360                 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
361                         if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
362                                 mask |= POLLOUT | POLLWRNORM;
363                         } else {  /* send SIGIO later */
364                                 set_bit(SOCK_ASYNC_NOSPACE,
365                                         &sk->sk_socket->flags);
366                                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
367
368                                 /* Race breaker. If space is freed after
369                                  * wspace test but before the flags are set,
370                                  * IO signal will be lost.
371                                  */
372                                 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
373                                         mask |= POLLOUT | POLLWRNORM;
374                         }
375                 }
376         }
377         return mask;
378 }
379
380 EXPORT_SYMBOL_GPL(dccp_poll);
381
382 int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
383 {
384         dccp_pr_debug("entry\n");
385         return -ENOIOCTLCMD;
386 }
387
388 EXPORT_SYMBOL_GPL(dccp_ioctl);
389
390 static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
391                                    char __user *optval, int optlen)
392 {
393         struct dccp_sock *dp = dccp_sk(sk);
394         struct dccp_service_list *sl = NULL;
395
396         if (service == DCCP_SERVICE_INVALID_VALUE || 
397             optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
398                 return -EINVAL;
399
400         if (optlen > sizeof(service)) {
401                 sl = kmalloc(optlen, GFP_KERNEL);
402                 if (sl == NULL)
403                         return -ENOMEM;
404
405                 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
406                 if (copy_from_user(sl->dccpsl_list,
407                                    optval + sizeof(service),
408                                    optlen - sizeof(service)) ||
409                     dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
410                         kfree(sl);
411                         return -EFAULT;
412                 }
413         }
414
415         lock_sock(sk);
416         dp->dccps_service = service;
417
418         kfree(dp->dccps_service_list);
419
420         dp->dccps_service_list = sl;
421         release_sock(sk);
422         return 0;
423 }
424
425 /* byte 1 is feature.  the rest is the preference list */
426 static int dccp_setsockopt_change(struct sock *sk, int type,
427                                   struct dccp_so_feat __user *optval)
428 {
429         struct dccp_so_feat opt;
430         u8 *val;
431         int rc;
432
433         if (copy_from_user(&opt, optval, sizeof(opt)))
434                 return -EFAULT;
435
436         val = kmalloc(opt.dccpsf_len, GFP_KERNEL);
437         if (!val)
438                 return -ENOMEM;
439
440         if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) {
441                 rc = -EFAULT;
442                 goto out_free_val;
443         }
444
445         rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat,
446                               val, opt.dccpsf_len, GFP_KERNEL);
447         if (rc)
448                 goto out_free_val;
449
450 out:
451         return rc;
452
453 out_free_val:
454         kfree(val);
455         goto out;
456 }
457
458 static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
459                 char __user *optval, int optlen)
460 {
461         struct dccp_sock *dp;
462         int err;
463         int val;
464
465         if (optlen < sizeof(int))
466                 return -EINVAL;
467
468         if (get_user(val, (int __user *)optval))
469                 return -EFAULT;
470
471         if (optname == DCCP_SOCKOPT_SERVICE)
472                 return dccp_setsockopt_service(sk, val, optval, optlen);
473
474         lock_sock(sk);
475         dp = dccp_sk(sk);
476         err = 0;
477
478         switch (optname) {
479         case DCCP_SOCKOPT_PACKET_SIZE:
480                 dp->dccps_packet_size = val;
481                 break;
482
483         case DCCP_SOCKOPT_CHANGE_L:
484                 if (optlen != sizeof(struct dccp_so_feat))
485                         err = -EINVAL;
486                 else
487                         err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L,
488                                                      (struct dccp_so_feat *)
489                                                      optval);
490                 break;
491
492         case DCCP_SOCKOPT_CHANGE_R:
493                 if (optlen != sizeof(struct dccp_so_feat))
494                         err = -EINVAL;
495                 else
496                         err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R,
497                                                      (struct dccp_so_feat *)
498                                                      optval);
499                 break;
500
501         default:
502                 err = -ENOPROTOOPT;
503                 break;
504         }
505         
506         release_sock(sk);
507         return err;
508 }
509
510 int dccp_setsockopt(struct sock *sk, int level, int optname,
511                     char __user *optval, int optlen)
512 {
513         if (level != SOL_DCCP)
514                 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
515                                                              optname, optval,
516                                                              optlen);
517         return do_dccp_setsockopt(sk, level, optname, optval, optlen);
518 }
519
520 EXPORT_SYMBOL_GPL(dccp_setsockopt);
521
522 #ifdef CONFIG_COMPAT
523 int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
524                            char __user *optval, int optlen)
525 {
526         if (level != SOL_DCCP)
527                 return inet_csk_compat_setsockopt(sk, level, optname,
528                                                   optval, optlen);
529         return do_dccp_setsockopt(sk, level, optname, optval, optlen);
530 }
531
532 EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
533 #endif
534
535 static int dccp_getsockopt_service(struct sock *sk, int len,
536                                    __be32 __user *optval,
537                                    int __user *optlen)
538 {
539         const struct dccp_sock *dp = dccp_sk(sk);
540         const struct dccp_service_list *sl;
541         int err = -ENOENT, slen = 0, total_len = sizeof(u32);
542
543         lock_sock(sk);
544         if (dccp_service_not_initialized(sk))
545                 goto out;
546
547         if ((sl = dp->dccps_service_list) != NULL) {
548                 slen = sl->dccpsl_nr * sizeof(u32);
549                 total_len += slen;
550         }
551
552         err = -EINVAL;
553         if (total_len > len)
554                 goto out;
555
556         err = 0;
557         if (put_user(total_len, optlen) ||
558             put_user(dp->dccps_service, optval) ||
559             (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
560                 err = -EFAULT;
561 out:
562         release_sock(sk);
563         return err;
564 }
565
566 static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
567                     char __user *optval, int __user *optlen)
568 {
569         struct dccp_sock *dp;
570         int val, len;
571
572         if (get_user(len, optlen))
573                 return -EFAULT;
574
575         if (len < sizeof(int))
576                 return -EINVAL;
577
578         dp = dccp_sk(sk);
579
580         switch (optname) {
581         case DCCP_SOCKOPT_PACKET_SIZE:
582                 val = dp->dccps_packet_size;
583                 len = sizeof(dp->dccps_packet_size);
584                 break;
585         case DCCP_SOCKOPT_SERVICE:
586                 return dccp_getsockopt_service(sk, len,
587                                                (__be32 __user *)optval, optlen);
588         case 128 ... 191:
589                 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
590                                              len, (u32 __user *)optval, optlen);
591         case 192 ... 255:
592                 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
593                                              len, (u32 __user *)optval, optlen);
594         default:
595                 return -ENOPROTOOPT;
596         }
597
598         if (put_user(len, optlen) || copy_to_user(optval, &val, len))
599                 return -EFAULT;
600
601         return 0;
602 }
603
604 int dccp_getsockopt(struct sock *sk, int level, int optname,
605                     char __user *optval, int __user *optlen)
606 {
607         if (level != SOL_DCCP)
608                 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
609                                                              optname, optval,
610                                                              optlen);
611         return do_dccp_getsockopt(sk, level, optname, optval, optlen);
612 }
613
614 EXPORT_SYMBOL_GPL(dccp_getsockopt);
615
616 #ifdef CONFIG_COMPAT
617 int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
618                            char __user *optval, int __user *optlen)
619 {
620         if (level != SOL_DCCP)
621                 return inet_csk_compat_getsockopt(sk, level, optname,
622                                                   optval, optlen);
623         return do_dccp_getsockopt(sk, level, optname, optval, optlen);
624 }
625
626 EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
627 #endif
628
629 int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
630                  size_t len)
631 {
632         const struct dccp_sock *dp = dccp_sk(sk);
633         const int flags = msg->msg_flags;
634         const int noblock = flags & MSG_DONTWAIT;
635         struct sk_buff *skb;
636         int rc, size;
637         long timeo;
638
639         if (len > dp->dccps_mss_cache)
640                 return -EMSGSIZE;
641
642         lock_sock(sk);
643         timeo = sock_sndtimeo(sk, noblock);
644
645         /*
646          * We have to use sk_stream_wait_connect here to set sk_write_pending,
647          * so that the trick in dccp_rcv_request_sent_state_process.
648          */
649         /* Wait for a connection to finish. */
650         if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING))
651                 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
652                         goto out_release;
653
654         size = sk->sk_prot->max_header + len;
655         release_sock(sk);
656         skb = sock_alloc_send_skb(sk, size, noblock, &rc);
657         lock_sock(sk);
658         if (skb == NULL)
659                 goto out_release;
660
661         skb_reserve(skb, sk->sk_prot->max_header);
662         rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
663         if (rc != 0)
664                 goto out_discard;
665
666         rc = dccp_write_xmit(sk, skb, &timeo);
667         /*
668          * XXX we don't use sk_write_queue, so just discard the packet.
669          *     Current plan however is to _use_ sk_write_queue with
670          *     an algorith similar to tcp_sendmsg, where the main difference
671          *     is that in DCCP we have to respect packet boundaries, so
672          *     no coalescing of skbs.
673          *
674          *     This bug was _quickly_ found & fixed by just looking at an OSTRA
675          *     generated callgraph 8) -acme
676          */
677 out_release:
678         release_sock(sk);
679         return rc ? : len;
680 out_discard:
681         kfree_skb(skb);
682         goto out_release;
683 }
684
685 EXPORT_SYMBOL_GPL(dccp_sendmsg);
686
687 int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
688                  size_t len, int nonblock, int flags, int *addr_len)
689 {
690         const struct dccp_hdr *dh;
691         long timeo;
692
693         lock_sock(sk);
694
695         if (sk->sk_state == DCCP_LISTEN) {
696                 len = -ENOTCONN;
697                 goto out;
698         }
699
700         timeo = sock_rcvtimeo(sk, nonblock);
701
702         do {
703                 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
704
705                 if (skb == NULL)
706                         goto verify_sock_status;
707
708                 dh = dccp_hdr(skb);
709
710                 if (dh->dccph_type == DCCP_PKT_DATA ||
711                     dh->dccph_type == DCCP_PKT_DATAACK)
712                         goto found_ok_skb;
713
714                 if (dh->dccph_type == DCCP_PKT_RESET ||
715                     dh->dccph_type == DCCP_PKT_CLOSE) {
716                         dccp_pr_debug("found fin ok!\n");
717                         len = 0;
718                         goto found_fin_ok;
719                 }
720                 dccp_pr_debug("packet_type=%s\n",
721                               dccp_packet_name(dh->dccph_type));
722                 sk_eat_skb(sk, skb, 0);
723 verify_sock_status:
724                 if (sock_flag(sk, SOCK_DONE)) {
725                         len = 0;
726                         break;
727                 }
728
729                 if (sk->sk_err) {
730                         len = sock_error(sk);
731                         break;
732                 }
733
734                 if (sk->sk_shutdown & RCV_SHUTDOWN) {
735                         len = 0;
736                         break;
737                 }
738
739                 if (sk->sk_state == DCCP_CLOSED) {
740                         if (!sock_flag(sk, SOCK_DONE)) {
741                                 /* This occurs when user tries to read
742                                  * from never connected socket.
743                                  */
744                                 len = -ENOTCONN;
745                                 break;
746                         }
747                         len = 0;
748                         break;
749                 }
750
751                 if (!timeo) {
752                         len = -EAGAIN;
753                         break;
754                 }
755
756                 if (signal_pending(current)) {
757                         len = sock_intr_errno(timeo);
758                         break;
759                 }
760
761                 sk_wait_data(sk, &timeo);
762                 continue;
763         found_ok_skb:
764                 if (len > skb->len)
765                         len = skb->len;
766                 else if (len < skb->len)
767                         msg->msg_flags |= MSG_TRUNC;
768
769                 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
770                         /* Exception. Bailout! */
771                         len = -EFAULT;
772                         break;
773                 }
774         found_fin_ok:
775                 if (!(flags & MSG_PEEK))
776                         sk_eat_skb(sk, skb, 0);
777                 break;
778         } while (1);
779 out:
780         release_sock(sk);
781         return len;
782 }
783
784 EXPORT_SYMBOL_GPL(dccp_recvmsg);
785
786 int inet_dccp_listen(struct socket *sock, int backlog)
787 {
788         struct sock *sk = sock->sk;
789         unsigned char old_state;
790         int err;
791
792         lock_sock(sk);
793
794         err = -EINVAL;
795         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
796                 goto out;
797
798         old_state = sk->sk_state;
799         if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
800                 goto out;
801
802         /* Really, if the socket is already in listen state
803          * we can only allow the backlog to be adjusted.
804          */
805         if (old_state != DCCP_LISTEN) {
806                 /*
807                  * FIXME: here it probably should be sk->sk_prot->listen_start
808                  * see tcp_listen_start
809                  */
810                 err = dccp_listen_start(sk);
811                 if (err)
812                         goto out;
813         }
814         sk->sk_max_ack_backlog = backlog;
815         err = 0;
816
817 out:
818         release_sock(sk);
819         return err;
820 }
821
822 EXPORT_SYMBOL_GPL(inet_dccp_listen);
823
824 static const unsigned char dccp_new_state[] = {
825         /* current state:   new state:      action:     */
826         [0]               = DCCP_CLOSED,
827         [DCCP_OPEN]       = DCCP_CLOSING | DCCP_ACTION_FIN,
828         [DCCP_REQUESTING] = DCCP_CLOSED,
829         [DCCP_PARTOPEN]   = DCCP_CLOSING | DCCP_ACTION_FIN,
830         [DCCP_LISTEN]     = DCCP_CLOSED,
831         [DCCP_RESPOND]    = DCCP_CLOSED,
832         [DCCP_CLOSING]    = DCCP_CLOSED,
833         [DCCP_TIME_WAIT]  = DCCP_CLOSED,
834         [DCCP_CLOSED]     = DCCP_CLOSED,
835 };
836
837 static int dccp_close_state(struct sock *sk)
838 {
839         const int next = dccp_new_state[sk->sk_state];
840         const int ns = next & DCCP_STATE_MASK;
841
842         if (ns != sk->sk_state)
843                 dccp_set_state(sk, ns);
844
845         return next & DCCP_ACTION_FIN;
846 }
847
848 void dccp_close(struct sock *sk, long timeout)
849 {
850         struct sk_buff *skb;
851         int state;
852
853         lock_sock(sk);
854
855         sk->sk_shutdown = SHUTDOWN_MASK;
856
857         if (sk->sk_state == DCCP_LISTEN) {
858                 dccp_set_state(sk, DCCP_CLOSED);
859
860                 /* Special case. */
861                 inet_csk_listen_stop(sk);
862
863                 goto adjudge_to_death;
864         }
865
866         /*
867          * We need to flush the recv. buffs.  We do this only on the
868          * descriptor close, not protocol-sourced closes, because the
869           *reader process may not have drained the data yet!
870          */
871         /* FIXME: check for unread data */
872         while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
873                 __kfree_skb(skb);
874         }
875
876         if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
877                 /* Check zero linger _after_ checking for unread data. */
878                 sk->sk_prot->disconnect(sk, 0);
879         } else if (dccp_close_state(sk)) {
880                 dccp_send_close(sk, 1);
881         }
882
883         sk_stream_wait_close(sk, timeout);
884
885 adjudge_to_death:
886         state = sk->sk_state;
887         sock_hold(sk);
888         sock_orphan(sk);
889         atomic_inc(sk->sk_prot->orphan_count);
890
891         /*
892          * It is the last release_sock in its life. It will remove backlog.
893          */
894         release_sock(sk);
895         /*
896          * Now socket is owned by kernel and we acquire BH lock
897          * to finish close. No need to check for user refs.
898          */
899         local_bh_disable();
900         bh_lock_sock(sk);
901         BUG_TRAP(!sock_owned_by_user(sk));
902
903         /* Have we already been destroyed by a softirq or backlog? */
904         if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
905                 goto out;
906
907         /*
908          * The last release_sock may have processed the CLOSE or RESET
909          * packet moving sock to CLOSED state, if not we have to fire
910          * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination"
911          * in draft-ietf-dccp-spec-11. -acme
912          */
913         if (sk->sk_state == DCCP_CLOSING) {
914                 /* FIXME: should start at 2 * RTT */
915                 /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */
916                 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
917                                           inet_csk(sk)->icsk_rto,
918                                           DCCP_RTO_MAX);
919 #if 0
920                 /* Yeah, we should use sk->sk_prot->orphan_count, etc */
921                 dccp_set_state(sk, DCCP_CLOSED);
922 #endif
923         }
924
925         if (sk->sk_state == DCCP_CLOSED)
926                 inet_csk_destroy_sock(sk);
927
928         /* Otherwise, socket is reprieved until protocol close. */
929
930 out:
931         bh_unlock_sock(sk);
932         local_bh_enable();
933         sock_put(sk);
934 }
935
936 EXPORT_SYMBOL_GPL(dccp_close);
937
938 void dccp_shutdown(struct sock *sk, int how)
939 {
940         dccp_pr_debug("entry\n");
941 }
942
943 EXPORT_SYMBOL_GPL(dccp_shutdown);
944
945 static int __init dccp_mib_init(void)
946 {
947         int rc = -ENOMEM;
948
949         dccp_statistics[0] = alloc_percpu(struct dccp_mib);
950         if (dccp_statistics[0] == NULL)
951                 goto out;
952
953         dccp_statistics[1] = alloc_percpu(struct dccp_mib);
954         if (dccp_statistics[1] == NULL)
955                 goto out_free_one;
956
957         rc = 0;
958 out:
959         return rc;
960 out_free_one:
961         free_percpu(dccp_statistics[0]);
962         dccp_statistics[0] = NULL;
963         goto out;
964
965 }
966
967 static void dccp_mib_exit(void)
968 {
969         free_percpu(dccp_statistics[0]);
970         free_percpu(dccp_statistics[1]);
971         dccp_statistics[0] = dccp_statistics[1] = NULL;
972 }
973
974 static int thash_entries;
975 module_param(thash_entries, int, 0444);
976 MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
977
978 #ifdef CONFIG_IP_DCCP_DEBUG
979 int dccp_debug;
980 module_param(dccp_debug, int, 0444);
981 MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
982
983 EXPORT_SYMBOL_GPL(dccp_debug);
984 #endif
985
986 static int __init dccp_init(void)
987 {
988         unsigned long goal;
989         int ehash_order, bhash_order, i;
990         int rc = -ENOBUFS;
991
992         dccp_hashinfo.bind_bucket_cachep =
993                 kmem_cache_create("dccp_bind_bucket",
994                                   sizeof(struct inet_bind_bucket), 0,
995                                   SLAB_HWCACHE_ALIGN, NULL, NULL);
996         if (!dccp_hashinfo.bind_bucket_cachep)
997                 goto out;
998
999         /*
1000          * Size and allocate the main established and bind bucket
1001          * hash tables.
1002          *
1003          * The methodology is similar to that of the buffer cache.
1004          */
1005         if (num_physpages >= (128 * 1024))
1006                 goal = num_physpages >> (21 - PAGE_SHIFT);
1007         else
1008                 goal = num_physpages >> (23 - PAGE_SHIFT);
1009
1010         if (thash_entries)
1011                 goal = (thash_entries *
1012                         sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
1013         for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1014                 ;
1015         do {
1016                 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1017                                         sizeof(struct inet_ehash_bucket);
1018                 dccp_hashinfo.ehash_size >>= 1;
1019                 while (dccp_hashinfo.ehash_size &
1020                        (dccp_hashinfo.ehash_size - 1))
1021                         dccp_hashinfo.ehash_size--;
1022                 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1023                         __get_free_pages(GFP_ATOMIC, ehash_order);
1024         } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1025
1026         if (!dccp_hashinfo.ehash) {
1027                 printk(KERN_CRIT "Failed to allocate DCCP "
1028                                  "established hash table\n");
1029                 goto out_free_bind_bucket_cachep;
1030         }
1031
1032         for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) {
1033                 rwlock_init(&dccp_hashinfo.ehash[i].lock);
1034                 INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
1035         }
1036
1037         bhash_order = ehash_order;
1038
1039         do {
1040                 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1041                                         sizeof(struct inet_bind_hashbucket);
1042                 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1043                     bhash_order > 0)
1044                         continue;
1045                 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1046                         __get_free_pages(GFP_ATOMIC, bhash_order);
1047         } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1048
1049         if (!dccp_hashinfo.bhash) {
1050                 printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n");
1051                 goto out_free_dccp_ehash;
1052         }
1053
1054         for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1055                 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1056                 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1057         }
1058
1059         rc = dccp_mib_init();
1060         if (rc)
1061                 goto out_free_dccp_bhash;
1062
1063         rc = dccp_ackvec_init();
1064         if (rc)
1065                 goto out_free_dccp_mib;
1066
1067         rc = dccp_sysctl_init();
1068         if (rc)
1069                 goto out_ackvec_exit;
1070 out:
1071         return rc;
1072 out_ackvec_exit:
1073         dccp_ackvec_exit();
1074 out_free_dccp_mib:
1075         dccp_mib_exit();
1076 out_free_dccp_bhash:
1077         free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1078         dccp_hashinfo.bhash = NULL;
1079 out_free_dccp_ehash:
1080         free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1081         dccp_hashinfo.ehash = NULL;
1082 out_free_bind_bucket_cachep:
1083         kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1084         dccp_hashinfo.bind_bucket_cachep = NULL;
1085         goto out;
1086 }
1087
1088 static void __exit dccp_fini(void)
1089 {
1090         dccp_mib_exit();
1091         free_pages((unsigned long)dccp_hashinfo.bhash,
1092                    get_order(dccp_hashinfo.bhash_size *
1093                              sizeof(struct inet_bind_hashbucket)));
1094         free_pages((unsigned long)dccp_hashinfo.ehash,
1095                    get_order(dccp_hashinfo.ehash_size *
1096                              sizeof(struct inet_ehash_bucket)));
1097         kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1098         dccp_ackvec_exit();
1099         dccp_sysctl_exit();
1100 }
1101
1102 module_init(dccp_init);
1103 module_exit(dccp_fini);
1104
1105 MODULE_LICENSE("GPL");
1106 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1107 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");