17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/proto.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 57c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 67c657876SArnaldo Carvalho de Melo * 77c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or modify it 87c657876SArnaldo Carvalho de Melo * under the terms of the GNU General Public License version 2 as 97c657876SArnaldo Carvalho de Melo * published by the Free Software Foundation. 107c657876SArnaldo Carvalho de Melo */ 117c657876SArnaldo Carvalho de Melo 127c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 137c657876SArnaldo Carvalho de Melo #include <linux/module.h> 147c657876SArnaldo Carvalho de Melo #include <linux/types.h> 157c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 167c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 177c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 187c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 197c657876SArnaldo Carvalho de Melo #include <linux/in.h> 207c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 217c657876SArnaldo Carvalho de Melo #include <linux/init.h> 227c657876SArnaldo Carvalho de Melo #include <linux/random.h> 237c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 247c657876SArnaldo Carvalho de Melo 2514c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 267c657876SArnaldo Carvalho de Melo #include <net/sock.h> 277c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 287c657876SArnaldo Carvalho de Melo 297c657876SArnaldo Carvalho de Melo #include <asm/semaphore.h> 307c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 317c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 327c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 337c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 347c657876SArnaldo Carvalho de Melo 357c657876SArnaldo Carvalho de Melo #include "ccid.h" 367c657876SArnaldo Carvalho de Melo #include "dccp.h" 37afe00251SAndrea Bittau #include "feat.h" 387c657876SArnaldo Carvalho de Melo 39ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 407c657876SArnaldo Carvalho de Melo 41f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 42f21e68caSArnaldo Carvalho de Melo 437c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 447c657876SArnaldo Carvalho de Melo 45f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 46f21e68caSArnaldo Carvalho de Melo 47075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 48075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 49075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 50075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 51075ae866SArnaldo Carvalho de Melo }; 52075ae866SArnaldo Carvalho de Melo 53075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 54075ae866SArnaldo Carvalho de Melo 55c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 56c25a18baSArnaldo Carvalho de Melo { 57c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 58c25a18baSArnaldo Carvalho de Melo 59c25a18baSArnaldo Carvalho de Melo dccp_pr_debug("%s(%p) %-10.10s -> %s\n", 60c25a18baSArnaldo Carvalho de Melo dccp_role(sk), sk, 61c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 62c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 63c25a18baSArnaldo Carvalho de Melo 64c25a18baSArnaldo Carvalho de Melo switch (state) { 65c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 66c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 67c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 68c25a18baSArnaldo Carvalho de Melo break; 69c25a18baSArnaldo Carvalho de Melo 70c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 71c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) 72c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 73c25a18baSArnaldo Carvalho de Melo 74c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 75c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 76c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 77c25a18baSArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 78c25a18baSArnaldo Carvalho de Melo /* fall through */ 79c25a18baSArnaldo Carvalho de Melo default: 80c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 81c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 82c25a18baSArnaldo Carvalho de Melo } 83c25a18baSArnaldo Carvalho de Melo 84c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 85c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 86c25a18baSArnaldo Carvalho de Melo */ 87c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 88c25a18baSArnaldo Carvalho de Melo } 89c25a18baSArnaldo Carvalho de Melo 90c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 91c25a18baSArnaldo Carvalho de Melo 92c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 93c25a18baSArnaldo Carvalho de Melo { 94c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 95c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 96c25a18baSArnaldo Carvalho de Melo 97c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 98c25a18baSArnaldo Carvalho de Melo 99c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 100c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 101c25a18baSArnaldo Carvalho de Melo else 102c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 103c25a18baSArnaldo Carvalho de Melo } 104c25a18baSArnaldo Carvalho de Melo 105c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 106c25a18baSArnaldo Carvalho de Melo 1077c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1087c657876SArnaldo Carvalho de Melo { 1097c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1107c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1117c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1127c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1137c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1147c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1157c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1167c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1177c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1187c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1197c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1207c657876SArnaldo Carvalho de Melo }; 1217c657876SArnaldo Carvalho de Melo 1227c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1237c657876SArnaldo Carvalho de Melo return "INVALID"; 1247c657876SArnaldo Carvalho de Melo else 1257c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1267c657876SArnaldo Carvalho de Melo } 1277c657876SArnaldo Carvalho de Melo 1287c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1297c657876SArnaldo Carvalho de Melo 1307c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1317c657876SArnaldo Carvalho de Melo { 1327c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1337c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1347c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1357c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1367c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1377c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1387c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 1397c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1407c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1417c657876SArnaldo Carvalho de Melo }; 1427c657876SArnaldo Carvalho de Melo 1437c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1447c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1457c657876SArnaldo Carvalho de Melo else 1467c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1477c657876SArnaldo Carvalho de Melo } 1487c657876SArnaldo Carvalho de Melo 1497c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1507c657876SArnaldo Carvalho de Melo 151c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 152c985ed70SArnaldo Carvalho de Melo { 153c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 154c985ed70SArnaldo Carvalho de Melo } 155c985ed70SArnaldo Carvalho de Melo 156c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 157c985ed70SArnaldo Carvalho de Melo 158c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 159c985ed70SArnaldo Carvalho de Melo { 160c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 161c985ed70SArnaldo Carvalho de Melo } 162c985ed70SArnaldo Carvalho de Melo 163c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 164c985ed70SArnaldo Carvalho de Melo 16572478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1663e0fadc5SArnaldo Carvalho de Melo { 1673e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 168a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 1693e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1703e0fadc5SArnaldo Carvalho de Melo 171a4bf3902SArnaldo Carvalho de Melo dccp_minisock_init(&dp->dccps_minisock); 1723e0fadc5SArnaldo Carvalho de Melo do_gettimeofday(&dp->dccps_epoch); 1733e0fadc5SArnaldo Carvalho de Melo 1743e0fadc5SArnaldo Carvalho de Melo /* 1753e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1763e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1773e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1783e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1793e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1803e0fadc5SArnaldo Carvalho de Melo */ 18172478873SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized)) { 1828ca0d17bSArnaldo Carvalho de Melo int rc = dccp_feat_init(dmsk); 1833e0fadc5SArnaldo Carvalho de Melo 1843e0fadc5SArnaldo Carvalho de Melo if (rc) 1853e0fadc5SArnaldo Carvalho de Melo return rc; 1863e0fadc5SArnaldo Carvalho de Melo 187a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 1883e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1893e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1903e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1913e0fadc5SArnaldo Carvalho de Melo } 192a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid, 1933e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 194a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid, 1953e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1963e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1973e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 1983e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 1993e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 200a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2013e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2023e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2033e0fadc5SArnaldo Carvalho de Melo } 2043e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2053e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 2063e0fadc5SArnaldo Carvalho de Melo } 2073e0fadc5SArnaldo Carvalho de Melo } else { 2083e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 209a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_pending); 210a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 2113e0fadc5SArnaldo Carvalho de Melo } 2123e0fadc5SArnaldo Carvalho de Melo 2133e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 2143e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 2153e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 2163e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 2173e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 2183e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 2193e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 22000e4d116SGerrit Renker dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 2213e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 2223e0fadc5SArnaldo Carvalho de Melo 2233e0fadc5SArnaldo Carvalho de Melo return 0; 2243e0fadc5SArnaldo Carvalho de Melo } 2253e0fadc5SArnaldo Carvalho de Melo 2263e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2273e0fadc5SArnaldo Carvalho de Melo 2283e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 2293e0fadc5SArnaldo Carvalho de Melo { 2303e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2318ca0d17bSArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 2323e0fadc5SArnaldo Carvalho de Melo 2333e0fadc5SArnaldo Carvalho de Melo /* 2343e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2353e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2363e0fadc5SArnaldo Carvalho de Melo */ 2373e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2383e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2393e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2403e0fadc5SArnaldo Carvalho de Melo } 2413e0fadc5SArnaldo Carvalho de Melo 2423e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2433e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2443e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2453e0fadc5SArnaldo Carvalho de Melo 2463e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2473e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2483e0fadc5SArnaldo Carvalho de Melo 2498ca0d17bSArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2503e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2513e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2523e0fadc5SArnaldo Carvalho de Melo } 2533e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2543e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2553e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2563e0fadc5SArnaldo Carvalho de Melo 2573e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2588ca0d17bSArnaldo Carvalho de Melo dccp_feat_clean(dmsk); 2593e0fadc5SArnaldo Carvalho de Melo 2603e0fadc5SArnaldo Carvalho de Melo return 0; 2613e0fadc5SArnaldo Carvalho de Melo } 2623e0fadc5SArnaldo Carvalho de Melo 2633e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2643e0fadc5SArnaldo Carvalho de Melo 2657c657876SArnaldo Carvalho de Melo static inline int dccp_listen_start(struct sock *sk) 2667c657876SArnaldo Carvalho de Melo { 26767e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 26867e6b629SArnaldo Carvalho de Melo 26967e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 2707c657876SArnaldo Carvalho de Melo return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE); 2717c657876SArnaldo Carvalho de Melo } 2727c657876SArnaldo Carvalho de Melo 2737c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2747c657876SArnaldo Carvalho de Melo { 2757c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2767c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2777c657876SArnaldo Carvalho de Melo int err = 0; 2787c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2797c657876SArnaldo Carvalho de Melo 2807c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2817c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2827c657876SArnaldo Carvalho de Melo 2837c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2847c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2857c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2867c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2877c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2887c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2897c657876SArnaldo Carvalho de Melo 2907c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2917c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2927c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2937c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2947c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2957c657876SArnaldo Carvalho de Melo } 2967c657876SArnaldo Carvalho de Melo 2977c657876SArnaldo Carvalho de Melo inet->dport = 0; 2987c657876SArnaldo Carvalho de Melo 2997c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 3007c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 3017c657876SArnaldo Carvalho de Melo 3027c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3037c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3047c657876SArnaldo Carvalho de Melo 3057c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3067c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3077c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3087c657876SArnaldo Carvalho de Melo 3097c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 3107c657876SArnaldo Carvalho de Melo 3117c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3127c657876SArnaldo Carvalho de Melo return err; 3137c657876SArnaldo Carvalho de Melo } 3147c657876SArnaldo Carvalho de Melo 315f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 316f21e68caSArnaldo Carvalho de Melo 317331968bdSArnaldo Carvalho de Melo /* 318331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 319331968bdSArnaldo Carvalho de Melo * 320331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 321331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 322331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 323331968bdSArnaldo Carvalho de Melo */ 324f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 325331968bdSArnaldo Carvalho de Melo poll_table *wait) 326331968bdSArnaldo Carvalho de Melo { 327331968bdSArnaldo Carvalho de Melo unsigned int mask; 328331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 329331968bdSArnaldo Carvalho de Melo 330331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 331331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 332331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 333331968bdSArnaldo Carvalho de Melo 334331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 335331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 336331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 337331968bdSArnaldo Carvalho de Melo */ 338331968bdSArnaldo Carvalho de Melo 339331968bdSArnaldo Carvalho de Melo mask = 0; 340331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 341331968bdSArnaldo Carvalho de Melo mask = POLLERR; 342331968bdSArnaldo Carvalho de Melo 343331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 344331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 345331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 346f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 347331968bdSArnaldo Carvalho de Melo 348331968bdSArnaldo Carvalho de Melo /* Connected? */ 349331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 350331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 351331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 352331968bdSArnaldo Carvalho de Melo 353331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 354331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 355331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 356331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 357331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 358331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 359331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 360331968bdSArnaldo Carvalho de Melo 361331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 362331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 363331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 364331968bdSArnaldo Carvalho de Melo */ 365331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 366331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 367331968bdSArnaldo Carvalho de Melo } 368331968bdSArnaldo Carvalho de Melo } 369331968bdSArnaldo Carvalho de Melo } 370331968bdSArnaldo Carvalho de Melo return mask; 371331968bdSArnaldo Carvalho de Melo } 372331968bdSArnaldo Carvalho de Melo 373f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 374f21e68caSArnaldo Carvalho de Melo 3757c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3767c657876SArnaldo Carvalho de Melo { 3777c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3787c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3797c657876SArnaldo Carvalho de Melo } 3807c657876SArnaldo Carvalho de Melo 381f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 382f21e68caSArnaldo Carvalho de Melo 38360fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 38467e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 38567e6b629SArnaldo Carvalho de Melo { 38667e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 38767e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 38867e6b629SArnaldo Carvalho de Melo 38967e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 39067e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 39167e6b629SArnaldo Carvalho de Melo return -EINVAL; 39267e6b629SArnaldo Carvalho de Melo 39367e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 39467e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 39567e6b629SArnaldo Carvalho de Melo if (sl == NULL) 39667e6b629SArnaldo Carvalho de Melo return -ENOMEM; 39767e6b629SArnaldo Carvalho de Melo 39867e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 39967e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 40067e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 40167e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 40267e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 40367e6b629SArnaldo Carvalho de Melo kfree(sl); 40467e6b629SArnaldo Carvalho de Melo return -EFAULT; 40567e6b629SArnaldo Carvalho de Melo } 40667e6b629SArnaldo Carvalho de Melo } 40767e6b629SArnaldo Carvalho de Melo 40867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 40967e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 41067e6b629SArnaldo Carvalho de Melo 41167e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 41267e6b629SArnaldo Carvalho de Melo 41367e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 41467e6b629SArnaldo Carvalho de Melo release_sock(sk); 41567e6b629SArnaldo Carvalho de Melo return 0; 41667e6b629SArnaldo Carvalho de Melo } 41767e6b629SArnaldo Carvalho de Melo 418afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 419afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 420afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 421afe00251SAndrea Bittau { 422afe00251SAndrea Bittau struct dccp_so_feat opt; 423afe00251SAndrea Bittau u8 *val; 424afe00251SAndrea Bittau int rc; 425afe00251SAndrea Bittau 426afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 427afe00251SAndrea Bittau return -EFAULT; 428afe00251SAndrea Bittau 429afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 430afe00251SAndrea Bittau if (!val) 431afe00251SAndrea Bittau return -ENOMEM; 432afe00251SAndrea Bittau 433afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 434afe00251SAndrea Bittau rc = -EFAULT; 435afe00251SAndrea Bittau goto out_free_val; 436afe00251SAndrea Bittau } 437afe00251SAndrea Bittau 4388ca0d17bSArnaldo Carvalho de Melo rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat, 4398ca0d17bSArnaldo Carvalho de Melo val, opt.dccpsf_len, GFP_KERNEL); 440afe00251SAndrea Bittau if (rc) 441afe00251SAndrea Bittau goto out_free_val; 442afe00251SAndrea Bittau 443afe00251SAndrea Bittau out: 444afe00251SAndrea Bittau return rc; 445afe00251SAndrea Bittau 446afe00251SAndrea Bittau out_free_val: 447afe00251SAndrea Bittau kfree(val); 448afe00251SAndrea Bittau goto out; 449afe00251SAndrea Bittau } 450afe00251SAndrea Bittau 4513fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 452a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4537c657876SArnaldo Carvalho de Melo { 454a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 455a84ffe43SArnaldo Carvalho de Melo int err; 456a84ffe43SArnaldo Carvalho de Melo int val; 4577c657876SArnaldo Carvalho de Melo 458a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 459a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 460a84ffe43SArnaldo Carvalho de Melo 461a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 462a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 463a84ffe43SArnaldo Carvalho de Melo 46467e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 46567e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 466a84ffe43SArnaldo Carvalho de Melo 46767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 468a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 469a84ffe43SArnaldo Carvalho de Melo err = 0; 470a84ffe43SArnaldo Carvalho de Melo 471a84ffe43SArnaldo Carvalho de Melo switch (optname) { 472a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 473a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 474a84ffe43SArnaldo Carvalho de Melo break; 475afe00251SAndrea Bittau 476afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 477afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 478afe00251SAndrea Bittau err = -EINVAL; 479afe00251SAndrea Bittau else 480afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L, 4819faefb6dSAlan Cox (struct dccp_so_feat __user *) 482afe00251SAndrea Bittau optval); 483afe00251SAndrea Bittau break; 484afe00251SAndrea Bittau 485afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 486afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 487afe00251SAndrea Bittau err = -EINVAL; 488afe00251SAndrea Bittau else 489afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 4909faefb6dSAlan Cox (struct dccp_so_feat __user *) 491afe00251SAndrea Bittau optval); 492afe00251SAndrea Bittau break; 493afe00251SAndrea Bittau 494a84ffe43SArnaldo Carvalho de Melo default: 495a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 496a84ffe43SArnaldo Carvalho de Melo break; 497a84ffe43SArnaldo Carvalho de Melo } 498a84ffe43SArnaldo Carvalho de Melo 499a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 500a84ffe43SArnaldo Carvalho de Melo return err; 5017c657876SArnaldo Carvalho de Melo } 5027c657876SArnaldo Carvalho de Melo 5033fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5043fdadf7dSDmitry Mishin char __user *optval, int optlen) 5053fdadf7dSDmitry Mishin { 5063fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5073fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5083fdadf7dSDmitry Mishin optname, optval, 5093fdadf7dSDmitry Mishin optlen); 5103fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5113fdadf7dSDmitry Mishin } 512543d9cfeSArnaldo Carvalho de Melo 513f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 514f21e68caSArnaldo Carvalho de Melo 5153fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5163fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5173fdadf7dSDmitry Mishin char __user *optval, int optlen) 5183fdadf7dSDmitry Mishin { 519dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 520dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 521dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5223fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5233fdadf7dSDmitry Mishin } 524543d9cfeSArnaldo Carvalho de Melo 5253fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5263fdadf7dSDmitry Mishin #endif 5273fdadf7dSDmitry Mishin 52867e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 52960fe62e7SAndrea Bittau __be32 __user *optval, 53067e6b629SArnaldo Carvalho de Melo int __user *optlen) 53167e6b629SArnaldo Carvalho de Melo { 53267e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 53367e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 53467e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 53567e6b629SArnaldo Carvalho de Melo 53667e6b629SArnaldo Carvalho de Melo lock_sock(sk); 53767e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 53867e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 53967e6b629SArnaldo Carvalho de Melo total_len += slen; 54067e6b629SArnaldo Carvalho de Melo } 54167e6b629SArnaldo Carvalho de Melo 54267e6b629SArnaldo Carvalho de Melo err = -EINVAL; 54367e6b629SArnaldo Carvalho de Melo if (total_len > len) 54467e6b629SArnaldo Carvalho de Melo goto out; 54567e6b629SArnaldo Carvalho de Melo 54667e6b629SArnaldo Carvalho de Melo err = 0; 54767e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 54867e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 54967e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 55067e6b629SArnaldo Carvalho de Melo err = -EFAULT; 55167e6b629SArnaldo Carvalho de Melo out: 55267e6b629SArnaldo Carvalho de Melo release_sock(sk); 55367e6b629SArnaldo Carvalho de Melo return err; 55467e6b629SArnaldo Carvalho de Melo } 55567e6b629SArnaldo Carvalho de Melo 5563fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 557a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5587c657876SArnaldo Carvalho de Melo { 559a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 560a84ffe43SArnaldo Carvalho de Melo int val, len; 5617c657876SArnaldo Carvalho de Melo 562a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 563a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 564a84ffe43SArnaldo Carvalho de Melo 56588f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 566a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 567a84ffe43SArnaldo Carvalho de Melo 568a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 569a84ffe43SArnaldo Carvalho de Melo 570a84ffe43SArnaldo Carvalho de Melo switch (optname) { 571a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 572a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 57388f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 574a84ffe43SArnaldo Carvalho de Melo break; 57588f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 57688f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 57760fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 57888f964dbSArnaldo Carvalho de Melo case 128 ... 191: 57988f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 58088f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 58188f964dbSArnaldo Carvalho de Melo case 192 ... 255: 58288f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 58388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 584a84ffe43SArnaldo Carvalho de Melo default: 585a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 586a84ffe43SArnaldo Carvalho de Melo } 587a84ffe43SArnaldo Carvalho de Melo 588a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 589a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 590a84ffe43SArnaldo Carvalho de Melo 591a84ffe43SArnaldo Carvalho de Melo return 0; 5927c657876SArnaldo Carvalho de Melo } 5937c657876SArnaldo Carvalho de Melo 5943fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 5953fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 5963fdadf7dSDmitry Mishin { 5973fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5983fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 5993fdadf7dSDmitry Mishin optname, optval, 6003fdadf7dSDmitry Mishin optlen); 6013fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6023fdadf7dSDmitry Mishin } 603543d9cfeSArnaldo Carvalho de Melo 604f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 605f21e68caSArnaldo Carvalho de Melo 6063fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6073fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6083fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6093fdadf7dSDmitry Mishin { 610dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 611dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 612dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6133fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6143fdadf7dSDmitry Mishin } 615543d9cfeSArnaldo Carvalho de Melo 6163fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6173fdadf7dSDmitry Mishin #endif 6183fdadf7dSDmitry Mishin 6197c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6207c657876SArnaldo Carvalho de Melo size_t len) 6217c657876SArnaldo Carvalho de Melo { 6227c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 6237c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 6247c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 6257c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 6267c657876SArnaldo Carvalho de Melo int rc, size; 6277c657876SArnaldo Carvalho de Melo long timeo; 6287c657876SArnaldo Carvalho de Melo 6297c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 6307c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 6317c657876SArnaldo Carvalho de Melo 6327c657876SArnaldo Carvalho de Melo lock_sock(sk); 63327258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6347c657876SArnaldo Carvalho de Melo 6357c657876SArnaldo Carvalho de Melo /* 6367c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6377c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6387c657876SArnaldo Carvalho de Melo */ 6397c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 6407c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 6417c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 64227258ee5SArnaldo Carvalho de Melo goto out_release; 6437c657876SArnaldo Carvalho de Melo 6447c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 6457c657876SArnaldo Carvalho de Melo release_sock(sk); 6467c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 6477c657876SArnaldo Carvalho de Melo lock_sock(sk); 6487c657876SArnaldo Carvalho de Melo if (skb == NULL) 6497c657876SArnaldo Carvalho de Melo goto out_release; 6507c657876SArnaldo Carvalho de Melo 6517c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 6527c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 65327258ee5SArnaldo Carvalho de Melo if (rc != 0) 65427258ee5SArnaldo Carvalho de Melo goto out_discard; 6557c657876SArnaldo Carvalho de Melo 65697e5848dSIan McDonald skb_queue_tail(&sk->sk_write_queue, skb); 65797e5848dSIan McDonald dccp_write_xmit(sk,0); 6587c657876SArnaldo Carvalho de Melo out_release: 6597c657876SArnaldo Carvalho de Melo release_sock(sk); 6607c657876SArnaldo Carvalho de Melo return rc ? : len; 66127258ee5SArnaldo Carvalho de Melo out_discard: 66227258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6637c657876SArnaldo Carvalho de Melo goto out_release; 6647c657876SArnaldo Carvalho de Melo } 6657c657876SArnaldo Carvalho de Melo 666f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 667f21e68caSArnaldo Carvalho de Melo 6687c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6697c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6707c657876SArnaldo Carvalho de Melo { 6717c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6727c657876SArnaldo Carvalho de Melo long timeo; 6737c657876SArnaldo Carvalho de Melo 6747c657876SArnaldo Carvalho de Melo lock_sock(sk); 6757c657876SArnaldo Carvalho de Melo 676531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 677531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6787c657876SArnaldo Carvalho de Melo goto out; 679531669a0SArnaldo Carvalho de Melo } 6807c657876SArnaldo Carvalho de Melo 6817c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 6827c657876SArnaldo Carvalho de Melo 6837c657876SArnaldo Carvalho de Melo do { 684531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 6857c657876SArnaldo Carvalho de Melo 686531669a0SArnaldo Carvalho de Melo if (skb == NULL) 687531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 6887c657876SArnaldo Carvalho de Melo 6897c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 6907c657876SArnaldo Carvalho de Melo 6917c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 6927c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 6937c657876SArnaldo Carvalho de Melo goto found_ok_skb; 6947c657876SArnaldo Carvalho de Melo 6957c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 6967c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 6977c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 698531669a0SArnaldo Carvalho de Melo len = 0; 6997c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7007c657876SArnaldo Carvalho de Melo } 7017690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7027690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 703624d1164SChris Leech sk_eat_skb(sk, skb, 0); 704531669a0SArnaldo Carvalho de Melo verify_sock_status: 705531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 706531669a0SArnaldo Carvalho de Melo len = 0; 7077c657876SArnaldo Carvalho de Melo break; 7087c657876SArnaldo Carvalho de Melo } 7097c657876SArnaldo Carvalho de Melo 710531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 711531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7127c657876SArnaldo Carvalho de Melo break; 713531669a0SArnaldo Carvalho de Melo } 714531669a0SArnaldo Carvalho de Melo 715531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 716531669a0SArnaldo Carvalho de Melo len = 0; 717531669a0SArnaldo Carvalho de Melo break; 718531669a0SArnaldo Carvalho de Melo } 7197c657876SArnaldo Carvalho de Melo 7207c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7217c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7227c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7237c657876SArnaldo Carvalho de Melo * from never connected socket. 7247c657876SArnaldo Carvalho de Melo */ 725531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7267c657876SArnaldo Carvalho de Melo break; 7277c657876SArnaldo Carvalho de Melo } 728531669a0SArnaldo Carvalho de Melo len = 0; 7297c657876SArnaldo Carvalho de Melo break; 7307c657876SArnaldo Carvalho de Melo } 7317c657876SArnaldo Carvalho de Melo 7327c657876SArnaldo Carvalho de Melo if (!timeo) { 733531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7347c657876SArnaldo Carvalho de Melo break; 7357c657876SArnaldo Carvalho de Melo } 7367c657876SArnaldo Carvalho de Melo 7377c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 738531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7397c657876SArnaldo Carvalho de Melo break; 7407c657876SArnaldo Carvalho de Melo } 7417c657876SArnaldo Carvalho de Melo 7427c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 7437c657876SArnaldo Carvalho de Melo continue; 7447c657876SArnaldo Carvalho de Melo found_ok_skb: 745531669a0SArnaldo Carvalho de Melo if (len > skb->len) 746531669a0SArnaldo Carvalho de Melo len = skb->len; 747531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 748531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 7497c657876SArnaldo Carvalho de Melo 750531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 7517c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 752531669a0SArnaldo Carvalho de Melo len = -EFAULT; 7537c657876SArnaldo Carvalho de Melo break; 7547c657876SArnaldo Carvalho de Melo } 7557c657876SArnaldo Carvalho de Melo found_fin_ok: 7567c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 757624d1164SChris Leech sk_eat_skb(sk, skb, 0); 7587c657876SArnaldo Carvalho de Melo break; 759531669a0SArnaldo Carvalho de Melo } while (1); 7607c657876SArnaldo Carvalho de Melo out: 7617c657876SArnaldo Carvalho de Melo release_sock(sk); 762531669a0SArnaldo Carvalho de Melo return len; 7637c657876SArnaldo Carvalho de Melo } 7647c657876SArnaldo Carvalho de Melo 765f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 766f21e68caSArnaldo Carvalho de Melo 767f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7687c657876SArnaldo Carvalho de Melo { 7697c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7707c657876SArnaldo Carvalho de Melo unsigned char old_state; 7717c657876SArnaldo Carvalho de Melo int err; 7727c657876SArnaldo Carvalho de Melo 7737c657876SArnaldo Carvalho de Melo lock_sock(sk); 7747c657876SArnaldo Carvalho de Melo 7757c657876SArnaldo Carvalho de Melo err = -EINVAL; 7767c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 7777c657876SArnaldo Carvalho de Melo goto out; 7787c657876SArnaldo Carvalho de Melo 7797c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 7807c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 7817c657876SArnaldo Carvalho de Melo goto out; 7827c657876SArnaldo Carvalho de Melo 7837c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 7847c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 7857c657876SArnaldo Carvalho de Melo */ 7867c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 7877c657876SArnaldo Carvalho de Melo /* 7887c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 7897c657876SArnaldo Carvalho de Melo * see tcp_listen_start 7907c657876SArnaldo Carvalho de Melo */ 7917c657876SArnaldo Carvalho de Melo err = dccp_listen_start(sk); 7927c657876SArnaldo Carvalho de Melo if (err) 7937c657876SArnaldo Carvalho de Melo goto out; 7947c657876SArnaldo Carvalho de Melo } 7957c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 7967c657876SArnaldo Carvalho de Melo err = 0; 7977c657876SArnaldo Carvalho de Melo 7987c657876SArnaldo Carvalho de Melo out: 7997c657876SArnaldo Carvalho de Melo release_sock(sk); 8007c657876SArnaldo Carvalho de Melo return err; 8017c657876SArnaldo Carvalho de Melo } 8027c657876SArnaldo Carvalho de Melo 803f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 804f21e68caSArnaldo Carvalho de Melo 8057c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8067c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8077c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8087c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8097c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8107c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8117c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8127c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8137c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8147c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8157c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8167c657876SArnaldo Carvalho de Melo }; 8177c657876SArnaldo Carvalho de Melo 8187c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8197c657876SArnaldo Carvalho de Melo { 8207c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8217c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8227c657876SArnaldo Carvalho de Melo 8237c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8247c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8257c657876SArnaldo Carvalho de Melo 8267c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8277c657876SArnaldo Carvalho de Melo } 8287c657876SArnaldo Carvalho de Melo 8297c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8307c657876SArnaldo Carvalho de Melo { 83197e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 8327c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 833134af346SHerbert Xu int state; 8347c657876SArnaldo Carvalho de Melo 8357c657876SArnaldo Carvalho de Melo lock_sock(sk); 8367c657876SArnaldo Carvalho de Melo 8377c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8387c657876SArnaldo Carvalho de Melo 8397c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 8407c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8417c657876SArnaldo Carvalho de Melo 8427c657876SArnaldo Carvalho de Melo /* Special case. */ 8437c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 8447c657876SArnaldo Carvalho de Melo 8457c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 8467c657876SArnaldo Carvalho de Melo } 8477c657876SArnaldo Carvalho de Melo 84897e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 84997e5848dSIan McDonald 8507c657876SArnaldo Carvalho de Melo /* 8517c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 8527c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 8537c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 8547c657876SArnaldo Carvalho de Melo */ 8557c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 8567c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 8577c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 8587c657876SArnaldo Carvalho de Melo } 8597c657876SArnaldo Carvalho de Melo 8607c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 8617c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 8627c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 8637c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8647ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8657c657876SArnaldo Carvalho de Melo } 8667c657876SArnaldo Carvalho de Melo 8677c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8687c657876SArnaldo Carvalho de Melo 8697c657876SArnaldo Carvalho de Melo adjudge_to_death: 870134af346SHerbert Xu state = sk->sk_state; 871134af346SHerbert Xu sock_hold(sk); 872134af346SHerbert Xu sock_orphan(sk); 873134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 874134af346SHerbert Xu 8757ad07e7cSArnaldo Carvalho de Melo /* 8767ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 8777ad07e7cSArnaldo Carvalho de Melo */ 8787c657876SArnaldo Carvalho de Melo release_sock(sk); 8797c657876SArnaldo Carvalho de Melo /* 8807c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 8817c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 8827c657876SArnaldo Carvalho de Melo */ 8837c657876SArnaldo Carvalho de Melo local_bh_disable(); 8847c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 8857c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 8867c657876SArnaldo Carvalho de Melo 887134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 888134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 889134af346SHerbert Xu goto out; 8907c657876SArnaldo Carvalho de Melo 8917ad07e7cSArnaldo Carvalho de Melo /* 8927ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 8937ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 8947ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 8957ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 8967ad07e7cSArnaldo Carvalho de Melo */ 8977ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 8987ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 8997ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9007ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9017ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9027ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9037ad07e7cSArnaldo Carvalho de Melo #if 0 9047ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9057c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9067ad07e7cSArnaldo Carvalho de Melo #endif 9077ad07e7cSArnaldo Carvalho de Melo } 9087c657876SArnaldo Carvalho de Melo 9097c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9107c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9117c657876SArnaldo Carvalho de Melo 9127c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9137c657876SArnaldo Carvalho de Melo 914134af346SHerbert Xu out: 9157c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9167c657876SArnaldo Carvalho de Melo local_bh_enable(); 9177c657876SArnaldo Carvalho de Melo sock_put(sk); 9187c657876SArnaldo Carvalho de Melo } 9197c657876SArnaldo Carvalho de Melo 920f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 921f21e68caSArnaldo Carvalho de Melo 9227c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9237c657876SArnaldo Carvalho de Melo { 9247c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9257c657876SArnaldo Carvalho de Melo } 9267c657876SArnaldo Carvalho de Melo 927f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 928f21e68caSArnaldo Carvalho de Melo 92946f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9307c657876SArnaldo Carvalho de Melo { 9317c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9327c657876SArnaldo Carvalho de Melo 9337c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9347c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9357c657876SArnaldo Carvalho de Melo goto out; 9367c657876SArnaldo Carvalho de Melo 9377c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9387c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9397c657876SArnaldo Carvalho de Melo goto out_free_one; 9407c657876SArnaldo Carvalho de Melo 9417c657876SArnaldo Carvalho de Melo rc = 0; 9427c657876SArnaldo Carvalho de Melo out: 9437c657876SArnaldo Carvalho de Melo return rc; 9447c657876SArnaldo Carvalho de Melo out_free_one: 9457c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9467c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9477c657876SArnaldo Carvalho de Melo goto out; 9487c657876SArnaldo Carvalho de Melo 9497c657876SArnaldo Carvalho de Melo } 9507c657876SArnaldo Carvalho de Melo 951b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 95246f09ffaSArnaldo Carvalho de Melo { 95346f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 95446f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 95546f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 95646f09ffaSArnaldo Carvalho de Melo } 95746f09ffaSArnaldo Carvalho de Melo 9587c657876SArnaldo Carvalho de Melo static int thash_entries; 9597c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9607c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9617c657876SArnaldo Carvalho de Melo 962a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9637c657876SArnaldo Carvalho de Melo int dccp_debug; 9647c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9657c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 966f21e68caSArnaldo Carvalho de Melo 967f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 968a1d3a355SArnaldo Carvalho de Melo #endif 9697c657876SArnaldo Carvalho de Melo 9707c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9717c657876SArnaldo Carvalho de Melo { 9727c657876SArnaldo Carvalho de Melo unsigned long goal; 9737c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 974b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 9757c657876SArnaldo Carvalho de Melo 9767690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 9777690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 9787690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 9797690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 9807c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 981b61fafc4SArnaldo Carvalho de Melo goto out; 9827c657876SArnaldo Carvalho de Melo 9837c657876SArnaldo Carvalho de Melo /* 9847c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 9857c657876SArnaldo Carvalho de Melo * hash tables. 9867c657876SArnaldo Carvalho de Melo * 9877c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 9887c657876SArnaldo Carvalho de Melo */ 9897c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 9907c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 9917c657876SArnaldo Carvalho de Melo else 9927c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 9937c657876SArnaldo Carvalho de Melo 9947c657876SArnaldo Carvalho de Melo if (thash_entries) 9957690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 9967690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 9977c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 9987c657876SArnaldo Carvalho de Melo ; 9997c657876SArnaldo Carvalho de Melo do { 10007c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10017c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10027c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 10037690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10047690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10057c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10067c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10077c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10087c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10097c657876SArnaldo Carvalho de Melo 10107c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 10117c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 10127c657876SArnaldo Carvalho de Melo "established hash table\n"); 10137c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10147c657876SArnaldo Carvalho de Melo } 10157c657876SArnaldo Carvalho de Melo 10167c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 10177c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10187c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 10197c657876SArnaldo Carvalho de Melo } 10207c657876SArnaldo Carvalho de Melo 10217c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10227c657876SArnaldo Carvalho de Melo 10237c657876SArnaldo Carvalho de Melo do { 10247c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10257c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10267690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10277690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10287c657876SArnaldo Carvalho de Melo continue; 10297c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10307c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10317c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10327c657876SArnaldo Carvalho de Melo 10337c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 10347c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 10357c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10367c657876SArnaldo Carvalho de Melo } 10377c657876SArnaldo Carvalho de Melo 10387c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10397c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10407c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10417c657876SArnaldo Carvalho de Melo } 10427c657876SArnaldo Carvalho de Melo 104346f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1044fa23e2ecSArnaldo Carvalho de Melo if (rc) 10457c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10467c657876SArnaldo Carvalho de Melo 10479b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10487c657876SArnaldo Carvalho de Melo if (rc) 1049b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 10509b07ef5dSArnaldo Carvalho de Melo 1051e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10529b07ef5dSArnaldo Carvalho de Melo if (rc) 10539b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 10547c657876SArnaldo Carvalho de Melo out: 10557c657876SArnaldo Carvalho de Melo return rc; 10569b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10579b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1058b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 105946f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10607c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10617c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10627c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10637c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10647c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10657c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10667c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10677c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10687c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10697c657876SArnaldo Carvalho de Melo goto out; 10707c657876SArnaldo Carvalho de Melo } 10717c657876SArnaldo Carvalho de Melo 10727c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 10737c657876SArnaldo Carvalho de Melo { 107446f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1075725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1076725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1077725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1078725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1079725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1080725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 10817c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10829b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1083e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 10847c657876SArnaldo Carvalho de Melo } 10857c657876SArnaldo Carvalho de Melo 10867c657876SArnaldo Carvalho de Melo module_init(dccp_init); 10877c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 10887c657876SArnaldo Carvalho de Melo 10897c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 10907c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 10917c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1092