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 55b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 56b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 57b1308dc0SIan McDonald 58c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 59c25a18baSArnaldo Carvalho de Melo { 60c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 61c25a18baSArnaldo Carvalho de Melo 62c25a18baSArnaldo Carvalho de Melo dccp_pr_debug("%s(%p) %-10.10s -> %s\n", 63c25a18baSArnaldo Carvalho de Melo dccp_role(sk), sk, 64c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 65c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 66c25a18baSArnaldo Carvalho de Melo 67c25a18baSArnaldo Carvalho de Melo switch (state) { 68c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 69c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 70c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 71c25a18baSArnaldo Carvalho de Melo break; 72c25a18baSArnaldo Carvalho de Melo 73c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 74c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) 75c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 76c25a18baSArnaldo Carvalho de Melo 77c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 78c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 79c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 80c25a18baSArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 81c25a18baSArnaldo Carvalho de Melo /* fall through */ 82c25a18baSArnaldo Carvalho de Melo default: 83c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 84c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 85c25a18baSArnaldo Carvalho de Melo } 86c25a18baSArnaldo Carvalho de Melo 87c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 88c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 89c25a18baSArnaldo Carvalho de Melo */ 90c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 91c25a18baSArnaldo Carvalho de Melo } 92c25a18baSArnaldo Carvalho de Melo 93c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 94c25a18baSArnaldo Carvalho de Melo 95c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 96c25a18baSArnaldo Carvalho de Melo { 97c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 98c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 99c25a18baSArnaldo Carvalho de Melo 100c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 101c25a18baSArnaldo Carvalho de Melo 102c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 103c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 104c25a18baSArnaldo Carvalho de Melo else 105c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 106c25a18baSArnaldo Carvalho de Melo } 107c25a18baSArnaldo Carvalho de Melo 108c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 109c25a18baSArnaldo Carvalho de Melo 1107c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1117c657876SArnaldo Carvalho de Melo { 1127c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1137c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1147c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1157c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1167c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1177c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1187c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1197c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1207c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1217c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1227c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1237c657876SArnaldo Carvalho de Melo }; 1247c657876SArnaldo Carvalho de Melo 1257c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1267c657876SArnaldo Carvalho de Melo return "INVALID"; 1277c657876SArnaldo Carvalho de Melo else 1287c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1297c657876SArnaldo Carvalho de Melo } 1307c657876SArnaldo Carvalho de Melo 1317c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1327c657876SArnaldo Carvalho de Melo 1337c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1347c657876SArnaldo Carvalho de Melo { 1357c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1367c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1377c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1387c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1397c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1407c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1417c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 1427c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1437c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1447c657876SArnaldo Carvalho de Melo }; 1457c657876SArnaldo Carvalho de Melo 1467c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1477c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1487c657876SArnaldo Carvalho de Melo else 1497c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1507c657876SArnaldo Carvalho de Melo } 1517c657876SArnaldo Carvalho de Melo 1527c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1537c657876SArnaldo Carvalho de Melo 154c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 155c985ed70SArnaldo Carvalho de Melo { 156c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 157c985ed70SArnaldo Carvalho de Melo } 158c985ed70SArnaldo Carvalho de Melo 159c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 160c985ed70SArnaldo Carvalho de Melo 161c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 162c985ed70SArnaldo Carvalho de Melo { 163c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 164c985ed70SArnaldo Carvalho de Melo } 165c985ed70SArnaldo Carvalho de Melo 166c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 167c985ed70SArnaldo Carvalho de Melo 16872478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1693e0fadc5SArnaldo Carvalho de Melo { 1703e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 171a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 1723e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1733e0fadc5SArnaldo Carvalho de Melo 174a4bf3902SArnaldo Carvalho de Melo dccp_minisock_init(&dp->dccps_minisock); 1753e0fadc5SArnaldo Carvalho de Melo 1763e0fadc5SArnaldo Carvalho de Melo /* 1773e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1783e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1793e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1803e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1813e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1823e0fadc5SArnaldo Carvalho de Melo */ 18372478873SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized)) { 1848ca0d17bSArnaldo Carvalho de Melo int rc = dccp_feat_init(dmsk); 1853e0fadc5SArnaldo Carvalho de Melo 1863e0fadc5SArnaldo Carvalho de Melo if (rc) 1873e0fadc5SArnaldo Carvalho de Melo return rc; 1883e0fadc5SArnaldo Carvalho de Melo 189a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 1903e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1913e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1923e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1933e0fadc5SArnaldo Carvalho de Melo } 194a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid, 1953e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 196a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid, 1973e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1983e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1993e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 2003e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2013e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 202a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2033e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2043e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2053e0fadc5SArnaldo Carvalho de Melo } 2063e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2073e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 2083e0fadc5SArnaldo Carvalho de Melo } 2093e0fadc5SArnaldo Carvalho de Melo } else { 2103e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 211a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_pending); 212a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 2133e0fadc5SArnaldo Carvalho de Melo } 2143e0fadc5SArnaldo Carvalho de Melo 2153e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 2163e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 2172e2e9e92SGerrit Renker icsk->icsk_syn_retries = sysctl_dccp_request_retries; 2183e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 2193e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 2203e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 2213e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 222a94f0f97SGerrit Renker dp->dccps_rate_last = jiffies; 2233e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 22400e4d116SGerrit Renker dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 2253e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 2263e0fadc5SArnaldo Carvalho de Melo 2273e0fadc5SArnaldo Carvalho de Melo return 0; 2283e0fadc5SArnaldo Carvalho de Melo } 2293e0fadc5SArnaldo Carvalho de Melo 2303e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2313e0fadc5SArnaldo Carvalho de Melo 2323e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 2333e0fadc5SArnaldo Carvalho de Melo { 2343e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2358ca0d17bSArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 2363e0fadc5SArnaldo Carvalho de Melo 2373e0fadc5SArnaldo Carvalho de Melo /* 2383e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2393e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2403e0fadc5SArnaldo Carvalho de Melo */ 2413e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2423e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2433e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2443e0fadc5SArnaldo Carvalho de Melo } 2453e0fadc5SArnaldo Carvalho de Melo 2463e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2473e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2483e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2493e0fadc5SArnaldo Carvalho de Melo 2503e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2513e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2523e0fadc5SArnaldo Carvalho de Melo 2538ca0d17bSArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2543e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2553e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2563e0fadc5SArnaldo Carvalho de Melo } 2573e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2583e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2593e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2603e0fadc5SArnaldo Carvalho de Melo 2613e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2628ca0d17bSArnaldo Carvalho de Melo dccp_feat_clean(dmsk); 2633e0fadc5SArnaldo Carvalho de Melo 2643e0fadc5SArnaldo Carvalho de Melo return 0; 2653e0fadc5SArnaldo Carvalho de Melo } 2663e0fadc5SArnaldo Carvalho de Melo 2673e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2683e0fadc5SArnaldo Carvalho de Melo 26972a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2707c657876SArnaldo Carvalho de Melo { 27167e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 27267e6b629SArnaldo Carvalho de Melo 27367e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 27472a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2757c657876SArnaldo Carvalho de Melo } 2767c657876SArnaldo Carvalho de Melo 2777c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2787c657876SArnaldo Carvalho de Melo { 2797c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2807c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2817c657876SArnaldo Carvalho de Melo int err = 0; 2827c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2837c657876SArnaldo Carvalho de Melo 2847c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2857c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2867c657876SArnaldo Carvalho de Melo 2877c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2887c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2897c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2907c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2917c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2927c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2937c657876SArnaldo Carvalho de Melo 2947c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2957c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2967c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2977c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2987c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2997c657876SArnaldo Carvalho de Melo } 3007c657876SArnaldo Carvalho de Melo 3017c657876SArnaldo Carvalho de Melo inet->dport = 0; 3027c657876SArnaldo Carvalho de Melo 3037c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 3047c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 3057c657876SArnaldo Carvalho de Melo 3067c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3077c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3087c657876SArnaldo Carvalho de Melo 3097c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3107c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3117c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3127c657876SArnaldo Carvalho de Melo 3137c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 3147c657876SArnaldo Carvalho de Melo 3157c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3167c657876SArnaldo Carvalho de Melo return err; 3177c657876SArnaldo Carvalho de Melo } 3187c657876SArnaldo Carvalho de Melo 319f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 320f21e68caSArnaldo Carvalho de Melo 321331968bdSArnaldo Carvalho de Melo /* 322331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 323331968bdSArnaldo Carvalho de Melo * 324331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 325331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 326331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 327331968bdSArnaldo Carvalho de Melo */ 328f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 329331968bdSArnaldo Carvalho de Melo poll_table *wait) 330331968bdSArnaldo Carvalho de Melo { 331331968bdSArnaldo Carvalho de Melo unsigned int mask; 332331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 333331968bdSArnaldo Carvalho de Melo 334331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 335331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 336331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 337331968bdSArnaldo Carvalho de Melo 338331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 339331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 340331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 341331968bdSArnaldo Carvalho de Melo */ 342331968bdSArnaldo Carvalho de Melo 343331968bdSArnaldo Carvalho de Melo mask = 0; 344331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 345331968bdSArnaldo Carvalho de Melo mask = POLLERR; 346331968bdSArnaldo Carvalho de Melo 347331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 348331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 349331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 350f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 351331968bdSArnaldo Carvalho de Melo 352331968bdSArnaldo Carvalho de Melo /* Connected? */ 353331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 354331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 355331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 356331968bdSArnaldo Carvalho de Melo 357331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 358331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 359331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 360331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 361331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 362331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 363331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 364331968bdSArnaldo Carvalho de Melo 365331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 366331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 367331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 368331968bdSArnaldo Carvalho de Melo */ 369331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 370331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 371331968bdSArnaldo Carvalho de Melo } 372331968bdSArnaldo Carvalho de Melo } 373331968bdSArnaldo Carvalho de Melo } 374331968bdSArnaldo Carvalho de Melo return mask; 375331968bdSArnaldo Carvalho de Melo } 376331968bdSArnaldo Carvalho de Melo 377f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 378f21e68caSArnaldo Carvalho de Melo 3797c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3807c657876SArnaldo Carvalho de Melo { 3817c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3827c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3837c657876SArnaldo Carvalho de Melo } 3847c657876SArnaldo Carvalho de Melo 385f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 386f21e68caSArnaldo Carvalho de Melo 38760fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 38867e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 38967e6b629SArnaldo Carvalho de Melo { 39067e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 39167e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 39267e6b629SArnaldo Carvalho de Melo 39367e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 39467e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 39567e6b629SArnaldo Carvalho de Melo return -EINVAL; 39667e6b629SArnaldo Carvalho de Melo 39767e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 39867e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 39967e6b629SArnaldo Carvalho de Melo if (sl == NULL) 40067e6b629SArnaldo Carvalho de Melo return -ENOMEM; 40167e6b629SArnaldo Carvalho de Melo 40267e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 40367e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 40467e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 40567e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 40667e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 40767e6b629SArnaldo Carvalho de Melo kfree(sl); 40867e6b629SArnaldo Carvalho de Melo return -EFAULT; 40967e6b629SArnaldo Carvalho de Melo } 41067e6b629SArnaldo Carvalho de Melo } 41167e6b629SArnaldo Carvalho de Melo 41267e6b629SArnaldo Carvalho de Melo lock_sock(sk); 41367e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 41467e6b629SArnaldo Carvalho de Melo 41567e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 41667e6b629SArnaldo Carvalho de Melo 41767e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 41867e6b629SArnaldo Carvalho de Melo release_sock(sk); 41967e6b629SArnaldo Carvalho de Melo return 0; 42067e6b629SArnaldo Carvalho de Melo } 42167e6b629SArnaldo Carvalho de Melo 422afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 423afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 424afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 425afe00251SAndrea Bittau { 426afe00251SAndrea Bittau struct dccp_so_feat opt; 427afe00251SAndrea Bittau u8 *val; 428afe00251SAndrea Bittau int rc; 429afe00251SAndrea Bittau 430afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 431afe00251SAndrea Bittau return -EFAULT; 432afe00251SAndrea Bittau 433afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 434afe00251SAndrea Bittau if (!val) 435afe00251SAndrea Bittau return -ENOMEM; 436afe00251SAndrea Bittau 437afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 438afe00251SAndrea Bittau rc = -EFAULT; 439afe00251SAndrea Bittau goto out_free_val; 440afe00251SAndrea Bittau } 441afe00251SAndrea Bittau 4428ca0d17bSArnaldo Carvalho de Melo rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat, 4438ca0d17bSArnaldo Carvalho de Melo val, opt.dccpsf_len, GFP_KERNEL); 444afe00251SAndrea Bittau if (rc) 445afe00251SAndrea Bittau goto out_free_val; 446afe00251SAndrea Bittau 447afe00251SAndrea Bittau out: 448afe00251SAndrea Bittau return rc; 449afe00251SAndrea Bittau 450afe00251SAndrea Bittau out_free_val: 451afe00251SAndrea Bittau kfree(val); 452afe00251SAndrea Bittau goto out; 453afe00251SAndrea Bittau } 454afe00251SAndrea Bittau 4553fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 456a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4577c657876SArnaldo Carvalho de Melo { 45809dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 45909dbc389SGerrit Renker int val, err = 0; 4607c657876SArnaldo Carvalho de Melo 461a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 462a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 463a84ffe43SArnaldo Carvalho de Melo 464a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 465a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 466a84ffe43SArnaldo Carvalho de Melo 46767e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 46867e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 469a84ffe43SArnaldo Carvalho de Melo 47067e6b629SArnaldo Carvalho de Melo lock_sock(sk); 471a84ffe43SArnaldo Carvalho de Melo switch (optname) { 472a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 4735aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 474841bac1dSArnaldo Carvalho de Melo err = 0; 475a84ffe43SArnaldo Carvalho de Melo break; 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 case DCCP_SOCKOPT_CHANGE_R: 485afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 486afe00251SAndrea Bittau err = -EINVAL; 487afe00251SAndrea Bittau else 488afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 4899faefb6dSAlan Cox (struct dccp_so_feat __user *) 490afe00251SAndrea Bittau optval); 491afe00251SAndrea Bittau break; 4926f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: /* sender side, RFC 4340, sec. 9.2 */ 4936f4e5fffSGerrit Renker if (val < 0 || val > 15) 4946f4e5fffSGerrit Renker err = -EINVAL; 4956f4e5fffSGerrit Renker else 4966f4e5fffSGerrit Renker dp->dccps_pcslen = val; 4976f4e5fffSGerrit Renker break; 4986f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: /* receiver side, RFC 4340 sec. 9.2.1 */ 4996f4e5fffSGerrit Renker if (val < 0 || val > 15) 5006f4e5fffSGerrit Renker err = -EINVAL; 5016f4e5fffSGerrit Renker else { 5026f4e5fffSGerrit Renker dp->dccps_pcrlen = val; 5036f4e5fffSGerrit Renker /* FIXME: add feature negotiation, 5046f4e5fffSGerrit Renker * ChangeL(MinimumChecksumCoverage, val) */ 5056f4e5fffSGerrit Renker } 5066f4e5fffSGerrit Renker break; 507a84ffe43SArnaldo Carvalho de Melo default: 508a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 509a84ffe43SArnaldo Carvalho de Melo break; 510a84ffe43SArnaldo Carvalho de Melo } 511a84ffe43SArnaldo Carvalho de Melo 512a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 513a84ffe43SArnaldo Carvalho de Melo return err; 5147c657876SArnaldo Carvalho de Melo } 5157c657876SArnaldo Carvalho de Melo 5163fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5173fdadf7dSDmitry Mishin char __user *optval, int optlen) 5183fdadf7dSDmitry Mishin { 5193fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5203fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5213fdadf7dSDmitry Mishin optname, optval, 5223fdadf7dSDmitry Mishin optlen); 5233fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5243fdadf7dSDmitry Mishin } 525543d9cfeSArnaldo Carvalho de Melo 526f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 527f21e68caSArnaldo Carvalho de Melo 5283fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5293fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5303fdadf7dSDmitry Mishin char __user *optval, int optlen) 5313fdadf7dSDmitry Mishin { 532dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 533dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 534dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5353fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5363fdadf7dSDmitry Mishin } 537543d9cfeSArnaldo Carvalho de Melo 5383fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5393fdadf7dSDmitry Mishin #endif 5403fdadf7dSDmitry Mishin 54167e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 54260fe62e7SAndrea Bittau __be32 __user *optval, 54367e6b629SArnaldo Carvalho de Melo int __user *optlen) 54467e6b629SArnaldo Carvalho de Melo { 54567e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 54667e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 54767e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 54867e6b629SArnaldo Carvalho de Melo 54967e6b629SArnaldo Carvalho de Melo lock_sock(sk); 55067e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 55167e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 55267e6b629SArnaldo Carvalho de Melo total_len += slen; 55367e6b629SArnaldo Carvalho de Melo } 55467e6b629SArnaldo Carvalho de Melo 55567e6b629SArnaldo Carvalho de Melo err = -EINVAL; 55667e6b629SArnaldo Carvalho de Melo if (total_len > len) 55767e6b629SArnaldo Carvalho de Melo goto out; 55867e6b629SArnaldo Carvalho de Melo 55967e6b629SArnaldo Carvalho de Melo err = 0; 56067e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 56167e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 56267e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 56367e6b629SArnaldo Carvalho de Melo err = -EFAULT; 56467e6b629SArnaldo Carvalho de Melo out: 56567e6b629SArnaldo Carvalho de Melo release_sock(sk); 56667e6b629SArnaldo Carvalho de Melo return err; 56767e6b629SArnaldo Carvalho de Melo } 56867e6b629SArnaldo Carvalho de Melo 5693fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 570a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5717c657876SArnaldo Carvalho de Melo { 572a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 573a84ffe43SArnaldo Carvalho de Melo int val, len; 5747c657876SArnaldo Carvalho de Melo 575a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 576a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 577a84ffe43SArnaldo Carvalho de Melo 57839ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 579a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 580a84ffe43SArnaldo Carvalho de Melo 581a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 582a84ffe43SArnaldo Carvalho de Melo 583a84ffe43SArnaldo Carvalho de Melo switch (optname) { 584a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 5855aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 586841bac1dSArnaldo Carvalho de Melo return 0; 58788f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 58888f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 58960fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 5906f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 5916f4e5fffSGerrit Renker val = dp->dccps_pcslen; 59239ebc027SArnaldo Carvalho de Melo len = sizeof(val); 5936f4e5fffSGerrit Renker break; 5946f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 5956f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 59639ebc027SArnaldo Carvalho de Melo len = sizeof(val); 5976f4e5fffSGerrit Renker break; 59888f964dbSArnaldo Carvalho de Melo case 128 ... 191: 59988f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 60088f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 60188f964dbSArnaldo Carvalho de Melo case 192 ... 255: 60288f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 60388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 604a84ffe43SArnaldo Carvalho de Melo default: 605a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 606a84ffe43SArnaldo Carvalho de Melo } 607a84ffe43SArnaldo Carvalho de Melo 608a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 609a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 610a84ffe43SArnaldo Carvalho de Melo 611a84ffe43SArnaldo Carvalho de Melo return 0; 6127c657876SArnaldo Carvalho de Melo } 6137c657876SArnaldo Carvalho de Melo 6143fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6153fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6163fdadf7dSDmitry Mishin { 6173fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6183fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6193fdadf7dSDmitry Mishin optname, optval, 6203fdadf7dSDmitry Mishin optlen); 6213fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6223fdadf7dSDmitry Mishin } 623543d9cfeSArnaldo Carvalho de Melo 624f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 625f21e68caSArnaldo Carvalho de Melo 6263fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6273fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6283fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6293fdadf7dSDmitry Mishin { 630dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 631dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 632dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6333fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6343fdadf7dSDmitry Mishin } 635543d9cfeSArnaldo Carvalho de Melo 6363fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6373fdadf7dSDmitry Mishin #endif 6383fdadf7dSDmitry Mishin 6397c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6407c657876SArnaldo Carvalho de Melo size_t len) 6417c657876SArnaldo Carvalho de Melo { 6427c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 6437c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 6447c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 6457c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 6467c657876SArnaldo Carvalho de Melo int rc, size; 6477c657876SArnaldo Carvalho de Melo long timeo; 6487c657876SArnaldo Carvalho de Melo 6497c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 6507c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 6517c657876SArnaldo Carvalho de Melo 6527c657876SArnaldo Carvalho de Melo lock_sock(sk); 653b1308dc0SIan McDonald 654b1308dc0SIan McDonald if (sysctl_dccp_tx_qlen && 655b1308dc0SIan McDonald (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) { 656b1308dc0SIan McDonald rc = -EAGAIN; 657b1308dc0SIan McDonald goto out_release; 658b1308dc0SIan McDonald } 659b1308dc0SIan McDonald 66027258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6617c657876SArnaldo Carvalho de Melo 6627c657876SArnaldo Carvalho de Melo /* 6637c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6647c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6657c657876SArnaldo Carvalho de Melo */ 6667c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 667cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 6687c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 66927258ee5SArnaldo Carvalho de Melo goto out_release; 6707c657876SArnaldo Carvalho de Melo 6717c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 6727c657876SArnaldo Carvalho de Melo release_sock(sk); 6737c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 6747c657876SArnaldo Carvalho de Melo lock_sock(sk); 6757c657876SArnaldo Carvalho de Melo if (skb == NULL) 6767c657876SArnaldo Carvalho de Melo goto out_release; 6777c657876SArnaldo Carvalho de Melo 6787c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 6797c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 68027258ee5SArnaldo Carvalho de Melo if (rc != 0) 68127258ee5SArnaldo Carvalho de Melo goto out_discard; 6827c657876SArnaldo Carvalho de Melo 68397e5848dSIan McDonald skb_queue_tail(&sk->sk_write_queue, skb); 68497e5848dSIan McDonald dccp_write_xmit(sk,0); 6857c657876SArnaldo Carvalho de Melo out_release: 6867c657876SArnaldo Carvalho de Melo release_sock(sk); 6877c657876SArnaldo Carvalho de Melo return rc ? : len; 68827258ee5SArnaldo Carvalho de Melo out_discard: 68927258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6907c657876SArnaldo Carvalho de Melo goto out_release; 6917c657876SArnaldo Carvalho de Melo } 6927c657876SArnaldo Carvalho de Melo 693f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 694f21e68caSArnaldo Carvalho de Melo 6957c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6967c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6977c657876SArnaldo Carvalho de Melo { 6987c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6997c657876SArnaldo Carvalho de Melo long timeo; 7007c657876SArnaldo Carvalho de Melo 7017c657876SArnaldo Carvalho de Melo lock_sock(sk); 7027c657876SArnaldo Carvalho de Melo 703531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 704531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7057c657876SArnaldo Carvalho de Melo goto out; 706531669a0SArnaldo Carvalho de Melo } 7077c657876SArnaldo Carvalho de Melo 7087c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 7097c657876SArnaldo Carvalho de Melo 7107c657876SArnaldo Carvalho de Melo do { 711531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 7127c657876SArnaldo Carvalho de Melo 713531669a0SArnaldo Carvalho de Melo if (skb == NULL) 714531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 7157c657876SArnaldo Carvalho de Melo 7167c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 7177c657876SArnaldo Carvalho de Melo 7187c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 7197c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 7207c657876SArnaldo Carvalho de Melo goto found_ok_skb; 7217c657876SArnaldo Carvalho de Melo 7227c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 7237c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 7247c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 725531669a0SArnaldo Carvalho de Melo len = 0; 7267c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7277c657876SArnaldo Carvalho de Melo } 7287690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7297690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 730624d1164SChris Leech sk_eat_skb(sk, skb, 0); 731531669a0SArnaldo Carvalho de Melo verify_sock_status: 732531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 733531669a0SArnaldo Carvalho de Melo len = 0; 7347c657876SArnaldo Carvalho de Melo break; 7357c657876SArnaldo Carvalho de Melo } 7367c657876SArnaldo Carvalho de Melo 737531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 738531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7397c657876SArnaldo Carvalho de Melo break; 740531669a0SArnaldo Carvalho de Melo } 741531669a0SArnaldo Carvalho de Melo 742531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 743531669a0SArnaldo Carvalho de Melo len = 0; 744531669a0SArnaldo Carvalho de Melo break; 745531669a0SArnaldo Carvalho de Melo } 7467c657876SArnaldo Carvalho de Melo 7477c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7487c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7497c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7507c657876SArnaldo Carvalho de Melo * from never connected socket. 7517c657876SArnaldo Carvalho de Melo */ 752531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7537c657876SArnaldo Carvalho de Melo break; 7547c657876SArnaldo Carvalho de Melo } 755531669a0SArnaldo Carvalho de Melo len = 0; 7567c657876SArnaldo Carvalho de Melo break; 7577c657876SArnaldo Carvalho de Melo } 7587c657876SArnaldo Carvalho de Melo 7597c657876SArnaldo Carvalho de Melo if (!timeo) { 760531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7617c657876SArnaldo Carvalho de Melo break; 7627c657876SArnaldo Carvalho de Melo } 7637c657876SArnaldo Carvalho de Melo 7647c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 765531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7667c657876SArnaldo Carvalho de Melo break; 7677c657876SArnaldo Carvalho de Melo } 7687c657876SArnaldo Carvalho de Melo 7697c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 7707c657876SArnaldo Carvalho de Melo continue; 7717c657876SArnaldo Carvalho de Melo found_ok_skb: 772531669a0SArnaldo Carvalho de Melo if (len > skb->len) 773531669a0SArnaldo Carvalho de Melo len = skb->len; 774531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 775531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 7767c657876SArnaldo Carvalho de Melo 777531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 7787c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 779531669a0SArnaldo Carvalho de Melo len = -EFAULT; 7807c657876SArnaldo Carvalho de Melo break; 7817c657876SArnaldo Carvalho de Melo } 7827c657876SArnaldo Carvalho de Melo found_fin_ok: 7837c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 784624d1164SChris Leech sk_eat_skb(sk, skb, 0); 7857c657876SArnaldo Carvalho de Melo break; 786531669a0SArnaldo Carvalho de Melo } while (1); 7877c657876SArnaldo Carvalho de Melo out: 7887c657876SArnaldo Carvalho de Melo release_sock(sk); 789531669a0SArnaldo Carvalho de Melo return len; 7907c657876SArnaldo Carvalho de Melo } 7917c657876SArnaldo Carvalho de Melo 792f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 793f21e68caSArnaldo Carvalho de Melo 794f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7957c657876SArnaldo Carvalho de Melo { 7967c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7977c657876SArnaldo Carvalho de Melo unsigned char old_state; 7987c657876SArnaldo Carvalho de Melo int err; 7997c657876SArnaldo Carvalho de Melo 8007c657876SArnaldo Carvalho de Melo lock_sock(sk); 8017c657876SArnaldo Carvalho de Melo 8027c657876SArnaldo Carvalho de Melo err = -EINVAL; 8037c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 8047c657876SArnaldo Carvalho de Melo goto out; 8057c657876SArnaldo Carvalho de Melo 8067c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 8077c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 8087c657876SArnaldo Carvalho de Melo goto out; 8097c657876SArnaldo Carvalho de Melo 8107c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 8117c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 8127c657876SArnaldo Carvalho de Melo */ 8137c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 8147c657876SArnaldo Carvalho de Melo /* 8157c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 8167c657876SArnaldo Carvalho de Melo * see tcp_listen_start 8177c657876SArnaldo Carvalho de Melo */ 81872a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 8197c657876SArnaldo Carvalho de Melo if (err) 8207c657876SArnaldo Carvalho de Melo goto out; 8217c657876SArnaldo Carvalho de Melo } 8227c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 8237c657876SArnaldo Carvalho de Melo err = 0; 8247c657876SArnaldo Carvalho de Melo 8257c657876SArnaldo Carvalho de Melo out: 8267c657876SArnaldo Carvalho de Melo release_sock(sk); 8277c657876SArnaldo Carvalho de Melo return err; 8287c657876SArnaldo Carvalho de Melo } 8297c657876SArnaldo Carvalho de Melo 830f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 831f21e68caSArnaldo Carvalho de Melo 8327c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8337c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8347c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8357c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8367c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8377c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8387c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8397c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8407c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8417c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8427c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8437c657876SArnaldo Carvalho de Melo }; 8447c657876SArnaldo Carvalho de Melo 8457c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8467c657876SArnaldo Carvalho de Melo { 8477c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8487c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8497c657876SArnaldo Carvalho de Melo 8507c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8517c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8527c657876SArnaldo Carvalho de Melo 8537c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8547c657876SArnaldo Carvalho de Melo } 8557c657876SArnaldo Carvalho de Melo 8567c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8577c657876SArnaldo Carvalho de Melo { 85897e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 8597c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 860134af346SHerbert Xu int state; 8617c657876SArnaldo Carvalho de Melo 8627c657876SArnaldo Carvalho de Melo lock_sock(sk); 8637c657876SArnaldo Carvalho de Melo 8647c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8657c657876SArnaldo Carvalho de Melo 8667c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 8677c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8687c657876SArnaldo Carvalho de Melo 8697c657876SArnaldo Carvalho de Melo /* Special case. */ 8707c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 8717c657876SArnaldo Carvalho de Melo 8727c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 8737c657876SArnaldo Carvalho de Melo } 8747c657876SArnaldo Carvalho de Melo 87597e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 87697e5848dSIan McDonald 8777c657876SArnaldo Carvalho de Melo /* 8787c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 8797c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 8807c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 8817c657876SArnaldo Carvalho de Melo */ 8827c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 8837c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 8847c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 8857c657876SArnaldo Carvalho de Melo } 8867c657876SArnaldo Carvalho de Melo 8877c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 8887c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 8897c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 8907c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8917ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8927c657876SArnaldo Carvalho de Melo } 8937c657876SArnaldo Carvalho de Melo 8947c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8957c657876SArnaldo Carvalho de Melo 8967c657876SArnaldo Carvalho de Melo adjudge_to_death: 897134af346SHerbert Xu state = sk->sk_state; 898134af346SHerbert Xu sock_hold(sk); 899134af346SHerbert Xu sock_orphan(sk); 900134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 901134af346SHerbert Xu 9027ad07e7cSArnaldo Carvalho de Melo /* 9037ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 9047ad07e7cSArnaldo Carvalho de Melo */ 9057c657876SArnaldo Carvalho de Melo release_sock(sk); 9067c657876SArnaldo Carvalho de Melo /* 9077c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 9087c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 9097c657876SArnaldo Carvalho de Melo */ 9107c657876SArnaldo Carvalho de Melo local_bh_disable(); 9117c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 9127c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 9137c657876SArnaldo Carvalho de Melo 914134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 915134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 916134af346SHerbert Xu goto out; 9177c657876SArnaldo Carvalho de Melo 9187ad07e7cSArnaldo Carvalho de Melo /* 9197ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 9207ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 9217ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 9227ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 9237ad07e7cSArnaldo Carvalho de Melo */ 9247ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 9257ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 9267ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9277ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9287ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9297ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9307ad07e7cSArnaldo Carvalho de Melo #if 0 9317ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9327c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9337ad07e7cSArnaldo Carvalho de Melo #endif 9347ad07e7cSArnaldo Carvalho de Melo } 9357c657876SArnaldo Carvalho de Melo 9367c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9377c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9387c657876SArnaldo Carvalho de Melo 9397c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9407c657876SArnaldo Carvalho de Melo 941134af346SHerbert Xu out: 9427c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9437c657876SArnaldo Carvalho de Melo local_bh_enable(); 9447c657876SArnaldo Carvalho de Melo sock_put(sk); 9457c657876SArnaldo Carvalho de Melo } 9467c657876SArnaldo Carvalho de Melo 947f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 948f21e68caSArnaldo Carvalho de Melo 9497c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9507c657876SArnaldo Carvalho de Melo { 9517c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9527c657876SArnaldo Carvalho de Melo } 9537c657876SArnaldo Carvalho de Melo 954f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 955f21e68caSArnaldo Carvalho de Melo 95646f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9577c657876SArnaldo Carvalho de Melo { 9587c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9597c657876SArnaldo Carvalho de Melo 9607c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9617c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9627c657876SArnaldo Carvalho de Melo goto out; 9637c657876SArnaldo Carvalho de Melo 9647c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9657c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9667c657876SArnaldo Carvalho de Melo goto out_free_one; 9677c657876SArnaldo Carvalho de Melo 9687c657876SArnaldo Carvalho de Melo rc = 0; 9697c657876SArnaldo Carvalho de Melo out: 9707c657876SArnaldo Carvalho de Melo return rc; 9717c657876SArnaldo Carvalho de Melo out_free_one: 9727c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9737c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9747c657876SArnaldo Carvalho de Melo goto out; 9757c657876SArnaldo Carvalho de Melo 9767c657876SArnaldo Carvalho de Melo } 9777c657876SArnaldo Carvalho de Melo 978b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 97946f09ffaSArnaldo Carvalho de Melo { 98046f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 98146f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 98246f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 98346f09ffaSArnaldo Carvalho de Melo } 98446f09ffaSArnaldo Carvalho de Melo 9857c657876SArnaldo Carvalho de Melo static int thash_entries; 9867c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9877c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9887c657876SArnaldo Carvalho de Melo 989a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9907c657876SArnaldo Carvalho de Melo int dccp_debug; 9917c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9927c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 993f21e68caSArnaldo Carvalho de Melo 994f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 995a1d3a355SArnaldo Carvalho de Melo #endif 9967c657876SArnaldo Carvalho de Melo 9977c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9987c657876SArnaldo Carvalho de Melo { 9997c657876SArnaldo Carvalho de Melo unsigned long goal; 10007c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1001b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 10027c657876SArnaldo Carvalho de Melo 10037690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 10047690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 10057690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 100620c2df83SPaul Mundt SLAB_HWCACHE_ALIGN, NULL); 10077c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1008b61fafc4SArnaldo Carvalho de Melo goto out; 10097c657876SArnaldo Carvalho de Melo 10107c657876SArnaldo Carvalho de Melo /* 10117c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 10127c657876SArnaldo Carvalho de Melo * hash tables. 10137c657876SArnaldo Carvalho de Melo * 10147c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 10157c657876SArnaldo Carvalho de Melo */ 10167c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 10177c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10187c657876SArnaldo Carvalho de Melo else 10197c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10207c657876SArnaldo Carvalho de Melo 10217c657876SArnaldo Carvalho de Melo if (thash_entries) 10227690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10237690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10247c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10257c657876SArnaldo Carvalho de Melo ; 10267c657876SArnaldo Carvalho de Melo do { 10277c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10287c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10297690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10307690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10317c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10327c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10337c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10347c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10357c657876SArnaldo Carvalho de Melo 10367c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 103759348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 10387c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10397c657876SArnaldo Carvalho de Melo } 10407c657876SArnaldo Carvalho de Melo 1041dbca9b27SEric Dumazet for (i = 0; i < dccp_hashinfo.ehash_size; i++) { 10427c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10437c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 1044dbca9b27SEric Dumazet INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].twchain); 10457c657876SArnaldo Carvalho de Melo } 10467c657876SArnaldo Carvalho de Melo 10477c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10487c657876SArnaldo Carvalho de Melo 10497c657876SArnaldo Carvalho de Melo do { 10507c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10517c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10527690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10537690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10547c657876SArnaldo Carvalho de Melo continue; 10557c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10567c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10577c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10587c657876SArnaldo Carvalho de Melo 10597c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 106059348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 10617c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10627c657876SArnaldo Carvalho de Melo } 10637c657876SArnaldo Carvalho de Melo 10647c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10657c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10667c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10677c657876SArnaldo Carvalho de Melo } 10687c657876SArnaldo Carvalho de Melo 106946f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1070fa23e2ecSArnaldo Carvalho de Melo if (rc) 10717c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10727c657876SArnaldo Carvalho de Melo 10739b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10747c657876SArnaldo Carvalho de Melo if (rc) 1075b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 10769b07ef5dSArnaldo Carvalho de Melo 1077e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10789b07ef5dSArnaldo Carvalho de Melo if (rc) 10799b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 10804c70f383SGerrit Renker 10814c70f383SGerrit Renker dccp_timestamping_init(); 10827c657876SArnaldo Carvalho de Melo out: 10837c657876SArnaldo Carvalho de Melo return rc; 10849b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10859b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1086b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 108746f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10887c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10897c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10907c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10917c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10927c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10937c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10947c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10957c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10967c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10977c657876SArnaldo Carvalho de Melo goto out; 10987c657876SArnaldo Carvalho de Melo } 10997c657876SArnaldo Carvalho de Melo 11007c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 11017c657876SArnaldo Carvalho de Melo { 110246f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1103725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1104725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1105725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1106725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1107725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1108725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 11097c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 11109b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1111e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 11127c657876SArnaldo Carvalho de Melo } 11137c657876SArnaldo Carvalho de Melo 11147c657876SArnaldo Carvalho de Melo module_init(dccp_init); 11157c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 11167c657876SArnaldo Carvalho de Melo 11177c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 11187c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11197c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1120