4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
12 #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>
20 #include <linux/if_arp.h>
21 #include <linux/init.h>
22 #include <linux/random.h>
23 #include <net/checksum.h>
25 #include <net/inet_sock.h>
29 #include <asm/ioctls.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/poll.h>
39 DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
41 EXPORT_SYMBOL_GPL(dccp_statistics);
43 atomic_t dccp_orphan_count = ATOMIC_INIT(0);
45 EXPORT_SYMBOL_GPL(dccp_orphan_count);
47 struct inet_hashinfo dccp_hashinfo;
48 EXPORT_SYMBOL_GPL(dccp_hashinfo);
50 /* the maximum queue length for tx in packets. 0 is no limit */
51 int sysctl_dccp_tx_qlen __read_mostly = 5;
53 void dccp_set_state(struct sock *sk, const int state)
55 const int oldstate = sk->sk_state;
57 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
58 dccp_state_name(oldstate), dccp_state_name(state));
59 WARN_ON(state == oldstate);
63 if (oldstate != DCCP_OPEN)
64 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
68 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
69 oldstate == DCCP_CLOSING)
70 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
72 sk->sk_prot->unhash(sk);
73 if (inet_csk(sk)->icsk_bind_hash != NULL &&
74 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
78 if (oldstate == DCCP_OPEN)
79 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
82 /* Change state AFTER socket is unhashed to avoid closed
83 * socket sitting in hash tables.
88 EXPORT_SYMBOL_GPL(dccp_set_state);
90 static void dccp_finish_passive_close(struct sock *sk)
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);
98 case DCCP_PASSIVE_CLOSEREQ:
100 * Client received CloseReq. We set the `active' flag so that
101 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
103 dccp_send_close(sk, 1);
104 dccp_set_state(sk, DCCP_CLOSING);
108 void dccp_done(struct sock *sk)
110 dccp_set_state(sk, DCCP_CLOSED);
111 dccp_clear_xmit_timers(sk);
113 sk->sk_shutdown = SHUTDOWN_MASK;
115 if (!sock_flag(sk, SOCK_DEAD))
116 sk->sk_state_change(sk);
118 inet_csk_destroy_sock(sk);
121 EXPORT_SYMBOL_GPL(dccp_done);
123 const char *dccp_packet_name(const int type)
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",
138 if (type >= DCCP_NR_PKT_TYPES)
141 return dccp_packet_names[type];
144 EXPORT_SYMBOL_GPL(dccp_packet_name);
146 const char *dccp_state_name(const int state)
148 static char *dccp_state_names[] = {
149 [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",
162 if (state >= DCCP_MAX_STATES)
163 return "INVALID STATE!";
165 return dccp_state_names[state];
168 EXPORT_SYMBOL_GPL(dccp_state_name);
170 int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
172 struct dccp_sock *dp = dccp_sk(sk);
173 struct dccp_minisock *dmsk = dccp_msk(sk);
174 struct inet_connection_sock *icsk = inet_csk(sk);
176 dccp_minisock_init(&dp->dccps_minisock);
178 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;
183 dp->dccps_mss_cache = 536;
184 dp->dccps_rate_last = jiffies;
185 dp->dccps_role = DCCP_ROLE_UNDEFINED;
186 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
187 dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1;
189 dccp_init_xmit_timers(sk);
191 INIT_LIST_HEAD(&dp->dccps_featneg);
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
199 if (likely(ctl_sock_initialized)) {
200 int rc = dccp_feat_init(sk);
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)
210 dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
212 dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
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;
222 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
226 /* control socket doesn't need feat nego */
227 INIT_LIST_HEAD(&dmsk->dccpms_pending);
228 INIT_LIST_HEAD(&dmsk->dccpms_conf);
234 EXPORT_SYMBOL_GPL(dccp_init_sock);
236 void dccp_destroy_sock(struct sock *sk)
238 struct dccp_sock *dp = dccp_sk(sk);
239 struct dccp_minisock *dmsk = dccp_msk(sk);
242 * DCCP doesn't use sk_write_queue, just sk_send_head
243 * for retransmissions
245 if (sk->sk_send_head != NULL) {
246 kfree_skb(sk->sk_send_head);
247 sk->sk_send_head = NULL;
250 /* Clean up a referenced DCCP bind bucket. */
251 if (inet_csk(sk)->icsk_bind_hash != NULL)
254 kfree(dp->dccps_service_list);
255 dp->dccps_service_list = NULL;
257 if (dmsk->dccpms_send_ack_vector) {
258 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
259 dp->dccps_hc_rx_ackvec = NULL;
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;
265 /* clean up feature negotiation state */
266 dccp_feat_list_purge(&dp->dccps_featneg);
269 EXPORT_SYMBOL_GPL(dccp_destroy_sock);
271 static inline int dccp_listen_start(struct sock *sk, int backlog)
273 struct dccp_sock *dp = dccp_sk(sk);
275 dp->dccps_role = DCCP_ROLE_LISTEN;
276 /* do not start to listen if feature negotiation setup fails */
277 if (dccp_feat_finalise_settings(dp))
279 return inet_csk_listen_start(sk, backlog);
282 static inline int dccp_need_reset(int state)
284 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
285 state != DCCP_REQUESTING;
288 int dccp_disconnect(struct sock *sk, int flags)
290 struct inet_connection_sock *icsk = inet_csk(sk);
291 struct inet_sock *inet = inet_sk(sk);
293 const int old_state = sk->sk_state;
295 if (old_state != DCCP_CLOSED)
296 dccp_set_state(sk, DCCP_CLOSED);
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".
302 if (old_state == DCCP_LISTEN) {
303 inet_csk_listen_stop(sk);
304 } else if (dccp_need_reset(old_state)) {
305 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
306 sk->sk_err = ECONNRESET;
307 } else if (old_state == DCCP_REQUESTING)
308 sk->sk_err = ECONNRESET;
310 dccp_clear_xmit_timers(sk);
312 __skb_queue_purge(&sk->sk_receive_queue);
313 __skb_queue_purge(&sk->sk_write_queue);
314 if (sk->sk_send_head != NULL) {
315 __kfree_skb(sk->sk_send_head);
316 sk->sk_send_head = NULL;
321 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
322 inet_reset_saddr(sk);
325 sock_reset_flag(sk, SOCK_DONE);
327 icsk->icsk_backoff = 0;
328 inet_csk_delack_init(sk);
331 WARN_ON(inet->num && !icsk->icsk_bind_hash);
333 sk->sk_error_report(sk);
337 EXPORT_SYMBOL_GPL(dccp_disconnect);
340 * Wait for a DCCP event.
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.
346 unsigned int dccp_poll(struct file *file, struct socket *sock,
350 struct sock *sk = sock->sk;
352 poll_wait(file, sk->sk_sleep, wait);
353 if (sk->sk_state == DCCP_LISTEN)
354 return inet_csk_listen_poll(sk);
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.
365 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
367 if (sk->sk_shutdown & RCV_SHUTDOWN)
368 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
371 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
372 if (atomic_read(&sk->sk_rmem_alloc) > 0)
373 mask |= POLLIN | POLLRDNORM;
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);
383 /* Race breaker. If space is freed after
384 * wspace test but before the flags are set,
385 * IO signal will be lost.
387 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
388 mask |= POLLOUT | POLLWRNORM;
395 EXPORT_SYMBOL_GPL(dccp_poll);
397 int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
403 if (sk->sk_state == DCCP_LISTEN)
409 unsigned long amount = 0;
411 skb = skb_peek(&sk->sk_receive_queue);
414 * We will only return the amount of this packet since
415 * that is all that will be read.
419 rc = put_user(amount, (int __user *)arg);
431 EXPORT_SYMBOL_GPL(dccp_ioctl);
433 static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
434 char __user *optval, int optlen)
436 struct dccp_sock *dp = dccp_sk(sk);
437 struct dccp_service_list *sl = NULL;
439 if (service == DCCP_SERVICE_INVALID_VALUE ||
440 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
443 if (optlen > sizeof(service)) {
444 sl = kmalloc(optlen, GFP_KERNEL);
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)) {
459 dp->dccps_service = service;
461 kfree(dp->dccps_service_list);
463 dp->dccps_service_list = sl;
468 static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
473 if (cscov < 0 || cscov > 15)
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.
485 list = kmalloc(len, GFP_KERNEL);
489 for (i = 0; i < len; i++)
492 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
496 dccp_sk(sk)->dccps_pcrlen = cscov;
498 dccp_sk(sk)->dccps_pcslen = cscov;
504 static int dccp_setsockopt_ccid(struct sock *sk, int type,
505 char __user *optval, int optlen)
510 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
513 val = kmalloc(optlen, GFP_KERNEL);
517 if (copy_from_user(val, optval, optlen)) {
523 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
524 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
526 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
527 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
534 static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
535 char __user *optval, int optlen)
537 struct dccp_sock *dp = dccp_sk(sk);
541 case DCCP_SOCKOPT_PACKET_SIZE:
542 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
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");
548 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);
554 if (optlen < (int)sizeof(int))
557 if (get_user(val, (int __user *)optval))
560 if (optname == DCCP_SOCKOPT_SERVICE)
561 return dccp_setsockopt_service(sk, val, optval, optlen);
565 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
566 if (dp->dccps_role != DCCP_ROLE_SERVER)
569 dp->dccps_server_timewait = (val != 0);
571 case DCCP_SOCKOPT_SEND_CSCOV:
572 err = dccp_setsockopt_cscov(sk, val, false);
574 case DCCP_SOCKOPT_RECV_CSCOV:
575 err = dccp_setsockopt_cscov(sk, val, true);
586 int dccp_setsockopt(struct sock *sk, int level, int optname,
587 char __user *optval, int optlen)
589 if (level != SOL_DCCP)
590 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
593 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
596 EXPORT_SYMBOL_GPL(dccp_setsockopt);
599 int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
600 char __user *optval, int optlen)
602 if (level != SOL_DCCP)
603 return inet_csk_compat_setsockopt(sk, level, optname,
605 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
608 EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
611 static int dccp_getsockopt_service(struct sock *sk, int len,
612 __be32 __user *optval,
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);
620 if ((sl = dp->dccps_service_list) != NULL) {
621 slen = sl->dccpsl_nr * sizeof(u32);
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)))
639 static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
640 char __user *optval, int __user *optlen)
642 struct dccp_sock *dp;
645 if (get_user(len, optlen))
648 if (len < (int)sizeof(int))
654 case DCCP_SOCKOPT_PACKET_SIZE:
655 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
657 case DCCP_SOCKOPT_SERVICE:
658 return dccp_getsockopt_service(sk, len,
659 (__be32 __user *)optval, optlen);
660 case DCCP_SOCKOPT_GET_CUR_MPS:
661 val = dp->dccps_mss_cache;
663 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
664 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
665 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
666 val = dp->dccps_server_timewait;
668 case DCCP_SOCKOPT_SEND_CSCOV:
669 val = dp->dccps_pcslen;
671 case DCCP_SOCKOPT_RECV_CSCOV:
672 val = dp->dccps_pcrlen;
675 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
676 len, (u32 __user *)optval, optlen);
678 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
679 len, (u32 __user *)optval, optlen);
685 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
691 int dccp_getsockopt(struct sock *sk, int level, int optname,
692 char __user *optval, int __user *optlen)
694 if (level != SOL_DCCP)
695 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
698 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
701 EXPORT_SYMBOL_GPL(dccp_getsockopt);
704 int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
705 char __user *optval, int __user *optlen)
707 if (level != SOL_DCCP)
708 return inet_csk_compat_getsockopt(sk, level, optname,
710 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
713 EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
716 int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
719 const struct dccp_sock *dp = dccp_sk(sk);
720 const int flags = msg->msg_flags;
721 const int noblock = flags & MSG_DONTWAIT;
726 if (len > dp->dccps_mss_cache)
731 if (sysctl_dccp_tx_qlen &&
732 (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
737 timeo = sock_sndtimeo(sk, noblock);
740 * We have to use sk_stream_wait_connect here to set sk_write_pending,
741 * so that the trick in dccp_rcv_request_sent_state_process.
743 /* Wait for a connection to finish. */
744 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
745 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
748 size = sk->sk_prot->max_header + len;
750 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
755 skb_reserve(skb, sk->sk_prot->max_header);
756 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
760 skb_queue_tail(&sk->sk_write_queue, skb);
761 dccp_write_xmit(sk,0);
770 EXPORT_SYMBOL_GPL(dccp_sendmsg);
772 int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
773 size_t len, int nonblock, int flags, int *addr_len)
775 const struct dccp_hdr *dh;
780 if (sk->sk_state == DCCP_LISTEN) {
785 timeo = sock_rcvtimeo(sk, nonblock);
788 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
791 goto verify_sock_status;
795 switch (dh->dccph_type) {
797 case DCCP_PKT_DATAACK:
801 case DCCP_PKT_CLOSEREQ:
802 if (!(flags & MSG_PEEK))
803 dccp_finish_passive_close(sk);
806 dccp_pr_debug("found fin (%s) ok!\n",
807 dccp_packet_name(dh->dccph_type));
811 dccp_pr_debug("packet_type=%s\n",
812 dccp_packet_name(dh->dccph_type));
813 sk_eat_skb(sk, skb, 0);
816 if (sock_flag(sk, SOCK_DONE)) {
822 len = sock_error(sk);
826 if (sk->sk_shutdown & RCV_SHUTDOWN) {
831 if (sk->sk_state == DCCP_CLOSED) {
832 if (!sock_flag(sk, SOCK_DONE)) {
833 /* This occurs when user tries to read
834 * from never connected socket.
848 if (signal_pending(current)) {
849 len = sock_intr_errno(timeo);
853 sk_wait_data(sk, &timeo);
858 else if (len < skb->len)
859 msg->msg_flags |= MSG_TRUNC;
861 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
862 /* Exception. Bailout! */
867 if (!(flags & MSG_PEEK))
868 sk_eat_skb(sk, skb, 0);
876 EXPORT_SYMBOL_GPL(dccp_recvmsg);
878 int inet_dccp_listen(struct socket *sock, int backlog)
880 struct sock *sk = sock->sk;
881 unsigned char old_state;
887 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
890 old_state = sk->sk_state;
891 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
894 /* Really, if the socket is already in listen state
895 * we can only allow the backlog to be adjusted.
897 if (old_state != DCCP_LISTEN) {
899 * FIXME: here it probably should be sk->sk_prot->listen_start
900 * see tcp_listen_start
902 err = dccp_listen_start(sk, backlog);
906 sk->sk_max_ack_backlog = backlog;
914 EXPORT_SYMBOL_GPL(inet_dccp_listen);
916 static void dccp_terminate_connection(struct sock *sk)
918 u8 next_state = DCCP_CLOSED;
920 switch (sk->sk_state) {
921 case DCCP_PASSIVE_CLOSE:
922 case DCCP_PASSIVE_CLOSEREQ:
923 dccp_finish_passive_close(sk);
926 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
927 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
930 dccp_send_close(sk, 1);
932 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
933 !dccp_sk(sk)->dccps_server_timewait)
934 next_state = DCCP_ACTIVE_CLOSEREQ;
936 next_state = DCCP_CLOSING;
939 dccp_set_state(sk, next_state);
943 void dccp_close(struct sock *sk, long timeout)
945 struct dccp_sock *dp = dccp_sk(sk);
947 u32 data_was_unread = 0;
952 sk->sk_shutdown = SHUTDOWN_MASK;
954 if (sk->sk_state == DCCP_LISTEN) {
955 dccp_set_state(sk, DCCP_CLOSED);
958 inet_csk_listen_stop(sk);
960 goto adjudge_to_death;
963 sk_stop_timer(sk, &dp->dccps_xmit_timer);
966 * We need to flush the recv. buffs. We do this only on the
967 * descriptor close, not protocol-sourced closes, because the
968 *reader process may not have drained the data yet!
970 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
971 data_was_unread += skb->len;
975 if (data_was_unread) {
976 /* Unread data was tossed, send an appropriate Reset Code */
977 DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
978 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
979 dccp_set_state(sk, DCCP_CLOSED);
980 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
981 /* Check zero linger _after_ checking for unread data. */
982 sk->sk_prot->disconnect(sk, 0);
983 } else if (sk->sk_state != DCCP_CLOSED) {
984 dccp_terminate_connection(sk);
987 sk_stream_wait_close(sk, timeout);
990 state = sk->sk_state;
993 atomic_inc(sk->sk_prot->orphan_count);
996 * It is the last release_sock in its life. It will remove backlog.
1000 * Now socket is owned by kernel and we acquire BH lock
1001 * to finish close. No need to check for user refs.
1005 WARN_ON(sock_owned_by_user(sk));
1007 /* Have we already been destroyed by a softirq or backlog? */
1008 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1011 if (sk->sk_state == DCCP_CLOSED)
1012 inet_csk_destroy_sock(sk);
1014 /* Otherwise, socket is reprieved until protocol close. */
1022 EXPORT_SYMBOL_GPL(dccp_close);
1024 void dccp_shutdown(struct sock *sk, int how)
1026 dccp_pr_debug("called shutdown(%x)\n", how);
1029 EXPORT_SYMBOL_GPL(dccp_shutdown);
1031 static inline int dccp_mib_init(void)
1033 return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
1036 static inline void dccp_mib_exit(void)
1038 snmp_mib_free((void**)dccp_statistics);
1041 static int thash_entries;
1042 module_param(thash_entries, int, 0444);
1043 MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1045 #ifdef CONFIG_IP_DCCP_DEBUG
1047 module_param(dccp_debug, bool, 0644);
1048 MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1050 EXPORT_SYMBOL_GPL(dccp_debug);
1053 static int __init dccp_init(void)
1056 int ehash_order, bhash_order, i;
1059 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1060 FIELD_SIZEOF(struct sk_buff, cb));
1062 inet_hashinfo_init(&dccp_hashinfo);
1063 dccp_hashinfo.bind_bucket_cachep =
1064 kmem_cache_create("dccp_bind_bucket",
1065 sizeof(struct inet_bind_bucket), 0,
1066 SLAB_HWCACHE_ALIGN, NULL);
1067 if (!dccp_hashinfo.bind_bucket_cachep)
1071 * Size and allocate the main established and bind bucket
1074 * The methodology is similar to that of the buffer cache.
1076 if (num_physpages >= (128 * 1024))
1077 goal = num_physpages >> (21 - PAGE_SHIFT);
1079 goal = num_physpages >> (23 - PAGE_SHIFT);
1082 goal = (thash_entries *
1083 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
1084 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1087 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1088 sizeof(struct inet_ehash_bucket);
1089 while (dccp_hashinfo.ehash_size &
1090 (dccp_hashinfo.ehash_size - 1))
1091 dccp_hashinfo.ehash_size--;
1092 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1093 __get_free_pages(GFP_ATOMIC, ehash_order);
1094 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1096 if (!dccp_hashinfo.ehash) {
1097 DCCP_CRIT("Failed to allocate DCCP established hash table");
1098 goto out_free_bind_bucket_cachep;
1101 for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
1102 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
1103 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
1106 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1107 goto out_free_dccp_ehash;
1109 bhash_order = ehash_order;
1112 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1113 sizeof(struct inet_bind_hashbucket);
1114 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1117 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1118 __get_free_pages(GFP_ATOMIC, bhash_order);
1119 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1121 if (!dccp_hashinfo.bhash) {
1122 DCCP_CRIT("Failed to allocate DCCP bind hash table");
1123 goto out_free_dccp_locks;
1126 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1127 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1128 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1131 rc = dccp_mib_init();
1133 goto out_free_dccp_bhash;
1135 rc = dccp_ackvec_init();
1137 goto out_free_dccp_mib;
1139 rc = dccp_sysctl_init();
1141 goto out_ackvec_exit;
1143 dccp_timestamping_init();
1150 out_free_dccp_bhash:
1151 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1152 dccp_hashinfo.bhash = NULL;
1153 out_free_dccp_locks:
1154 inet_ehash_locks_free(&dccp_hashinfo);
1155 out_free_dccp_ehash:
1156 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1157 dccp_hashinfo.ehash = NULL;
1158 out_free_bind_bucket_cachep:
1159 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1160 dccp_hashinfo.bind_bucket_cachep = NULL;
1164 static void __exit dccp_fini(void)
1167 free_pages((unsigned long)dccp_hashinfo.bhash,
1168 get_order(dccp_hashinfo.bhash_size *
1169 sizeof(struct inet_bind_hashbucket)));
1170 free_pages((unsigned long)dccp_hashinfo.ehash,
1171 get_order(dccp_hashinfo.ehash_size *
1172 sizeof(struct inet_ehash_bucket)));
1173 inet_ehash_locks_free(&dccp_hashinfo);
1174 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1179 module_init(dccp_init);
1180 module_exit(dccp_fini);
1182 MODULE_LICENSE("GPL");
1183 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1184 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");