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 do_gettimeofday(&dp->dccps_epoch); 1763e0fadc5SArnaldo Carvalho de Melo 1773e0fadc5SArnaldo Carvalho de Melo /* 1783e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1793e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1803e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1813e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1823e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1833e0fadc5SArnaldo Carvalho de Melo */ 18472478873SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized)) { 1858ca0d17bSArnaldo Carvalho de Melo int rc = dccp_feat_init(dmsk); 1863e0fadc5SArnaldo Carvalho de Melo 1873e0fadc5SArnaldo Carvalho de Melo if (rc) 1883e0fadc5SArnaldo Carvalho de Melo return rc; 1893e0fadc5SArnaldo Carvalho de Melo 190a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 1913e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1923e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1933e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1943e0fadc5SArnaldo Carvalho de Melo } 195a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid, 1963e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 197a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid, 1983e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1993e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 2003e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 2013e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2023e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 203a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2043e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2053e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2063e0fadc5SArnaldo Carvalho de Melo } 2073e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2083e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 2093e0fadc5SArnaldo Carvalho de Melo } 2103e0fadc5SArnaldo Carvalho de Melo } else { 2113e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 212a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_pending); 213a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 2143e0fadc5SArnaldo Carvalho de Melo } 2153e0fadc5SArnaldo Carvalho de Melo 2163e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 2173e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 2182e2e9e92SGerrit Renker icsk->icsk_syn_retries = sysctl_dccp_request_retries; 2193e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 2203e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 2213e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 2223e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 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: 473a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 474a84ffe43SArnaldo Carvalho de Melo break; 475afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 476afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 477afe00251SAndrea Bittau err = -EINVAL; 478afe00251SAndrea Bittau else 479afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L, 4809faefb6dSAlan Cox (struct dccp_so_feat __user *) 481afe00251SAndrea Bittau optval); 482afe00251SAndrea Bittau break; 483afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 484afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 485afe00251SAndrea Bittau err = -EINVAL; 486afe00251SAndrea Bittau else 487afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 4889faefb6dSAlan Cox (struct dccp_so_feat __user *) 489afe00251SAndrea Bittau optval); 490afe00251SAndrea Bittau break; 4916f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: /* sender side, RFC 4340, sec. 9.2 */ 4926f4e5fffSGerrit Renker if (val < 0 || val > 15) 4936f4e5fffSGerrit Renker err = -EINVAL; 4946f4e5fffSGerrit Renker else 4956f4e5fffSGerrit Renker dp->dccps_pcslen = val; 4966f4e5fffSGerrit Renker break; 4976f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: /* receiver side, RFC 4340 sec. 9.2.1 */ 4986f4e5fffSGerrit Renker if (val < 0 || val > 15) 4996f4e5fffSGerrit Renker err = -EINVAL; 5006f4e5fffSGerrit Renker else { 5016f4e5fffSGerrit Renker dp->dccps_pcrlen = val; 5026f4e5fffSGerrit Renker /* FIXME: add feature negotiation, 5036f4e5fffSGerrit Renker * ChangeL(MinimumChecksumCoverage, val) */ 5046f4e5fffSGerrit Renker } 5056f4e5fffSGerrit Renker break; 506a84ffe43SArnaldo Carvalho de Melo default: 507a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 508a84ffe43SArnaldo Carvalho de Melo break; 509a84ffe43SArnaldo Carvalho de Melo } 510a84ffe43SArnaldo Carvalho de Melo 511a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 512a84ffe43SArnaldo Carvalho de Melo return err; 5137c657876SArnaldo Carvalho de Melo } 5147c657876SArnaldo Carvalho de Melo 5153fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5163fdadf7dSDmitry Mishin char __user *optval, int optlen) 5173fdadf7dSDmitry Mishin { 5183fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5193fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5203fdadf7dSDmitry Mishin optname, optval, 5213fdadf7dSDmitry Mishin optlen); 5223fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5233fdadf7dSDmitry Mishin } 524543d9cfeSArnaldo Carvalho de Melo 525f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 526f21e68caSArnaldo Carvalho de Melo 5273fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5283fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5293fdadf7dSDmitry Mishin char __user *optval, int optlen) 5303fdadf7dSDmitry Mishin { 531dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 532dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 533dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5343fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5353fdadf7dSDmitry Mishin } 536543d9cfeSArnaldo Carvalho de Melo 5373fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5383fdadf7dSDmitry Mishin #endif 5393fdadf7dSDmitry Mishin 54067e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 54160fe62e7SAndrea Bittau __be32 __user *optval, 54267e6b629SArnaldo Carvalho de Melo int __user *optlen) 54367e6b629SArnaldo Carvalho de Melo { 54467e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 54567e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 54667e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 54767e6b629SArnaldo Carvalho de Melo 54867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 54967e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 55067e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 55167e6b629SArnaldo Carvalho de Melo total_len += slen; 55267e6b629SArnaldo Carvalho de Melo } 55367e6b629SArnaldo Carvalho de Melo 55467e6b629SArnaldo Carvalho de Melo err = -EINVAL; 55567e6b629SArnaldo Carvalho de Melo if (total_len > len) 55667e6b629SArnaldo Carvalho de Melo goto out; 55767e6b629SArnaldo Carvalho de Melo 55867e6b629SArnaldo Carvalho de Melo err = 0; 55967e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 56067e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 56167e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 56267e6b629SArnaldo Carvalho de Melo err = -EFAULT; 56367e6b629SArnaldo Carvalho de Melo out: 56467e6b629SArnaldo Carvalho de Melo release_sock(sk); 56567e6b629SArnaldo Carvalho de Melo return err; 56667e6b629SArnaldo Carvalho de Melo } 56767e6b629SArnaldo Carvalho de Melo 5683fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 569a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5707c657876SArnaldo Carvalho de Melo { 571a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 572a84ffe43SArnaldo Carvalho de Melo int val, len; 5737c657876SArnaldo Carvalho de Melo 574a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 575a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 576a84ffe43SArnaldo Carvalho de Melo 57788f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 578a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 579a84ffe43SArnaldo Carvalho de Melo 580a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 581a84ffe43SArnaldo Carvalho de Melo 582a84ffe43SArnaldo Carvalho de Melo switch (optname) { 583a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 584a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 58588f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 586a84ffe43SArnaldo Carvalho de Melo break; 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; 5926f4e5fffSGerrit Renker break; 5936f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 5946f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 5956f4e5fffSGerrit Renker break; 59688f964dbSArnaldo Carvalho de Melo case 128 ... 191: 59788f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 59888f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 59988f964dbSArnaldo Carvalho de Melo case 192 ... 255: 60088f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 60188f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 602a84ffe43SArnaldo Carvalho de Melo default: 603a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 604a84ffe43SArnaldo Carvalho de Melo } 605a84ffe43SArnaldo Carvalho de Melo 606a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 607a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 608a84ffe43SArnaldo Carvalho de Melo 609a84ffe43SArnaldo Carvalho de Melo return 0; 6107c657876SArnaldo Carvalho de Melo } 6117c657876SArnaldo Carvalho de Melo 6123fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6133fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6143fdadf7dSDmitry Mishin { 6153fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6163fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6173fdadf7dSDmitry Mishin optname, optval, 6183fdadf7dSDmitry Mishin optlen); 6193fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6203fdadf7dSDmitry Mishin } 621543d9cfeSArnaldo Carvalho de Melo 622f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 623f21e68caSArnaldo Carvalho de Melo 6243fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6253fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6263fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6273fdadf7dSDmitry Mishin { 628dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 629dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 630dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6313fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6323fdadf7dSDmitry Mishin } 633543d9cfeSArnaldo Carvalho de Melo 6343fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6353fdadf7dSDmitry Mishin #endif 6363fdadf7dSDmitry Mishin 6377c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6387c657876SArnaldo Carvalho de Melo size_t len) 6397c657876SArnaldo Carvalho de Melo { 6407c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 6417c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 6427c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 6437c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 6447c657876SArnaldo Carvalho de Melo int rc, size; 6457c657876SArnaldo Carvalho de Melo long timeo; 6467c657876SArnaldo Carvalho de Melo 6477c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 6487c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 6497c657876SArnaldo Carvalho de Melo 6507c657876SArnaldo Carvalho de Melo lock_sock(sk); 651b1308dc0SIan McDonald 652b1308dc0SIan McDonald if (sysctl_dccp_tx_qlen && 653b1308dc0SIan McDonald (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) { 654b1308dc0SIan McDonald rc = -EAGAIN; 655b1308dc0SIan McDonald goto out_release; 656b1308dc0SIan McDonald } 657b1308dc0SIan McDonald 65827258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6597c657876SArnaldo Carvalho de Melo 6607c657876SArnaldo Carvalho de Melo /* 6617c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6627c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6637c657876SArnaldo Carvalho de Melo */ 6647c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 6657c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 6667c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 66727258ee5SArnaldo Carvalho de Melo goto out_release; 6687c657876SArnaldo Carvalho de Melo 6697c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 6707c657876SArnaldo Carvalho de Melo release_sock(sk); 6717c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 6727c657876SArnaldo Carvalho de Melo lock_sock(sk); 6737c657876SArnaldo Carvalho de Melo if (skb == NULL) 6747c657876SArnaldo Carvalho de Melo goto out_release; 6757c657876SArnaldo Carvalho de Melo 6767c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 6777c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 67827258ee5SArnaldo Carvalho de Melo if (rc != 0) 67927258ee5SArnaldo Carvalho de Melo goto out_discard; 6807c657876SArnaldo Carvalho de Melo 68197e5848dSIan McDonald skb_queue_tail(&sk->sk_write_queue, skb); 68297e5848dSIan McDonald dccp_write_xmit(sk,0); 6837c657876SArnaldo Carvalho de Melo out_release: 6847c657876SArnaldo Carvalho de Melo release_sock(sk); 6857c657876SArnaldo Carvalho de Melo return rc ? : len; 68627258ee5SArnaldo Carvalho de Melo out_discard: 68727258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6887c657876SArnaldo Carvalho de Melo goto out_release; 6897c657876SArnaldo Carvalho de Melo } 6907c657876SArnaldo Carvalho de Melo 691f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 692f21e68caSArnaldo Carvalho de Melo 6937c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6947c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6957c657876SArnaldo Carvalho de Melo { 6967c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6977c657876SArnaldo Carvalho de Melo long timeo; 6987c657876SArnaldo Carvalho de Melo 6997c657876SArnaldo Carvalho de Melo lock_sock(sk); 7007c657876SArnaldo Carvalho de Melo 701531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 702531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7037c657876SArnaldo Carvalho de Melo goto out; 704531669a0SArnaldo Carvalho de Melo } 7057c657876SArnaldo Carvalho de Melo 7067c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 7077c657876SArnaldo Carvalho de Melo 7087c657876SArnaldo Carvalho de Melo do { 709531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 7107c657876SArnaldo Carvalho de Melo 711531669a0SArnaldo Carvalho de Melo if (skb == NULL) 712531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 7137c657876SArnaldo Carvalho de Melo 7147c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 7157c657876SArnaldo Carvalho de Melo 7167c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 7177c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 7187c657876SArnaldo Carvalho de Melo goto found_ok_skb; 7197c657876SArnaldo Carvalho de Melo 7207c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 7217c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 7227c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 723531669a0SArnaldo Carvalho de Melo len = 0; 7247c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7257c657876SArnaldo Carvalho de Melo } 7267690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7277690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 728624d1164SChris Leech sk_eat_skb(sk, skb, 0); 729531669a0SArnaldo Carvalho de Melo verify_sock_status: 730531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 731531669a0SArnaldo Carvalho de Melo len = 0; 7327c657876SArnaldo Carvalho de Melo break; 7337c657876SArnaldo Carvalho de Melo } 7347c657876SArnaldo Carvalho de Melo 735531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 736531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7377c657876SArnaldo Carvalho de Melo break; 738531669a0SArnaldo Carvalho de Melo } 739531669a0SArnaldo Carvalho de Melo 740531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 741531669a0SArnaldo Carvalho de Melo len = 0; 742531669a0SArnaldo Carvalho de Melo break; 743531669a0SArnaldo Carvalho de Melo } 7447c657876SArnaldo Carvalho de Melo 7457c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7467c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7477c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7487c657876SArnaldo Carvalho de Melo * from never connected socket. 7497c657876SArnaldo Carvalho de Melo */ 750531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7517c657876SArnaldo Carvalho de Melo break; 7527c657876SArnaldo Carvalho de Melo } 753531669a0SArnaldo Carvalho de Melo len = 0; 7547c657876SArnaldo Carvalho de Melo break; 7557c657876SArnaldo Carvalho de Melo } 7567c657876SArnaldo Carvalho de Melo 7577c657876SArnaldo Carvalho de Melo if (!timeo) { 758531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7597c657876SArnaldo Carvalho de Melo break; 7607c657876SArnaldo Carvalho de Melo } 7617c657876SArnaldo Carvalho de Melo 7627c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 763531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7647c657876SArnaldo Carvalho de Melo break; 7657c657876SArnaldo Carvalho de Melo } 7667c657876SArnaldo Carvalho de Melo 7677c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 7687c657876SArnaldo Carvalho de Melo continue; 7697c657876SArnaldo Carvalho de Melo found_ok_skb: 770531669a0SArnaldo Carvalho de Melo if (len > skb->len) 771531669a0SArnaldo Carvalho de Melo len = skb->len; 772531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 773531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 7747c657876SArnaldo Carvalho de Melo 775531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 7767c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 777531669a0SArnaldo Carvalho de Melo len = -EFAULT; 7787c657876SArnaldo Carvalho de Melo break; 7797c657876SArnaldo Carvalho de Melo } 7807c657876SArnaldo Carvalho de Melo found_fin_ok: 7817c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 782624d1164SChris Leech sk_eat_skb(sk, skb, 0); 7837c657876SArnaldo Carvalho de Melo break; 784531669a0SArnaldo Carvalho de Melo } while (1); 7857c657876SArnaldo Carvalho de Melo out: 7867c657876SArnaldo Carvalho de Melo release_sock(sk); 787531669a0SArnaldo Carvalho de Melo return len; 7887c657876SArnaldo Carvalho de Melo } 7897c657876SArnaldo Carvalho de Melo 790f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 791f21e68caSArnaldo Carvalho de Melo 792f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7937c657876SArnaldo Carvalho de Melo { 7947c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7957c657876SArnaldo Carvalho de Melo unsigned char old_state; 7967c657876SArnaldo Carvalho de Melo int err; 7977c657876SArnaldo Carvalho de Melo 7987c657876SArnaldo Carvalho de Melo lock_sock(sk); 7997c657876SArnaldo Carvalho de Melo 8007c657876SArnaldo Carvalho de Melo err = -EINVAL; 8017c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 8027c657876SArnaldo Carvalho de Melo goto out; 8037c657876SArnaldo Carvalho de Melo 8047c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 8057c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 8067c657876SArnaldo Carvalho de Melo goto out; 8077c657876SArnaldo Carvalho de Melo 8087c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 8097c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 8107c657876SArnaldo Carvalho de Melo */ 8117c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 8127c657876SArnaldo Carvalho de Melo /* 8137c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 8147c657876SArnaldo Carvalho de Melo * see tcp_listen_start 8157c657876SArnaldo Carvalho de Melo */ 81672a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 8177c657876SArnaldo Carvalho de Melo if (err) 8187c657876SArnaldo Carvalho de Melo goto out; 8197c657876SArnaldo Carvalho de Melo } 8207c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 8217c657876SArnaldo Carvalho de Melo err = 0; 8227c657876SArnaldo Carvalho de Melo 8237c657876SArnaldo Carvalho de Melo out: 8247c657876SArnaldo Carvalho de Melo release_sock(sk); 8257c657876SArnaldo Carvalho de Melo return err; 8267c657876SArnaldo Carvalho de Melo } 8277c657876SArnaldo Carvalho de Melo 828f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 829f21e68caSArnaldo Carvalho de Melo 8307c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8317c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8327c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8337c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8347c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8357c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8367c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8377c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8387c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8397c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8407c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8417c657876SArnaldo Carvalho de Melo }; 8427c657876SArnaldo Carvalho de Melo 8437c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8447c657876SArnaldo Carvalho de Melo { 8457c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8467c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8477c657876SArnaldo Carvalho de Melo 8487c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8497c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8507c657876SArnaldo Carvalho de Melo 8517c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8527c657876SArnaldo Carvalho de Melo } 8537c657876SArnaldo Carvalho de Melo 8547c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8557c657876SArnaldo Carvalho de Melo { 85697e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 8577c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 858134af346SHerbert Xu int state; 8597c657876SArnaldo Carvalho de Melo 8607c657876SArnaldo Carvalho de Melo lock_sock(sk); 8617c657876SArnaldo Carvalho de Melo 8627c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8637c657876SArnaldo Carvalho de Melo 8647c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 8657c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8667c657876SArnaldo Carvalho de Melo 8677c657876SArnaldo Carvalho de Melo /* Special case. */ 8687c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 8697c657876SArnaldo Carvalho de Melo 8707c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 8717c657876SArnaldo Carvalho de Melo } 8727c657876SArnaldo Carvalho de Melo 87397e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 87497e5848dSIan McDonald 8757c657876SArnaldo Carvalho de Melo /* 8767c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 8777c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 8787c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 8797c657876SArnaldo Carvalho de Melo */ 8807c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 8817c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 8827c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 8837c657876SArnaldo Carvalho de Melo } 8847c657876SArnaldo Carvalho de Melo 8857c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 8867c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 8877c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 8887c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8897ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8907c657876SArnaldo Carvalho de Melo } 8917c657876SArnaldo Carvalho de Melo 8927c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8937c657876SArnaldo Carvalho de Melo 8947c657876SArnaldo Carvalho de Melo adjudge_to_death: 895134af346SHerbert Xu state = sk->sk_state; 896134af346SHerbert Xu sock_hold(sk); 897134af346SHerbert Xu sock_orphan(sk); 898134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 899134af346SHerbert Xu 9007ad07e7cSArnaldo Carvalho de Melo /* 9017ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 9027ad07e7cSArnaldo Carvalho de Melo */ 9037c657876SArnaldo Carvalho de Melo release_sock(sk); 9047c657876SArnaldo Carvalho de Melo /* 9057c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 9067c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 9077c657876SArnaldo Carvalho de Melo */ 9087c657876SArnaldo Carvalho de Melo local_bh_disable(); 9097c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 9107c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 9117c657876SArnaldo Carvalho de Melo 912134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 913134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 914134af346SHerbert Xu goto out; 9157c657876SArnaldo Carvalho de Melo 9167ad07e7cSArnaldo Carvalho de Melo /* 9177ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 9187ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 9197ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 9207ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 9217ad07e7cSArnaldo Carvalho de Melo */ 9227ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 9237ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 9247ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9257ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9267ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9277ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9287ad07e7cSArnaldo Carvalho de Melo #if 0 9297ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9307c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9317ad07e7cSArnaldo Carvalho de Melo #endif 9327ad07e7cSArnaldo Carvalho de Melo } 9337c657876SArnaldo Carvalho de Melo 9347c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9357c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9367c657876SArnaldo Carvalho de Melo 9377c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9387c657876SArnaldo Carvalho de Melo 939134af346SHerbert Xu out: 9407c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9417c657876SArnaldo Carvalho de Melo local_bh_enable(); 9427c657876SArnaldo Carvalho de Melo sock_put(sk); 9437c657876SArnaldo Carvalho de Melo } 9447c657876SArnaldo Carvalho de Melo 945f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 946f21e68caSArnaldo Carvalho de Melo 9477c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9487c657876SArnaldo Carvalho de Melo { 9497c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9507c657876SArnaldo Carvalho de Melo } 9517c657876SArnaldo Carvalho de Melo 952f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 953f21e68caSArnaldo Carvalho de Melo 95446f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9557c657876SArnaldo Carvalho de Melo { 9567c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9577c657876SArnaldo Carvalho de Melo 9587c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9597c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9607c657876SArnaldo Carvalho de Melo goto out; 9617c657876SArnaldo Carvalho de Melo 9627c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9637c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9647c657876SArnaldo Carvalho de Melo goto out_free_one; 9657c657876SArnaldo Carvalho de Melo 9667c657876SArnaldo Carvalho de Melo rc = 0; 9677c657876SArnaldo Carvalho de Melo out: 9687c657876SArnaldo Carvalho de Melo return rc; 9697c657876SArnaldo Carvalho de Melo out_free_one: 9707c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9717c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9727c657876SArnaldo Carvalho de Melo goto out; 9737c657876SArnaldo Carvalho de Melo 9747c657876SArnaldo Carvalho de Melo } 9757c657876SArnaldo Carvalho de Melo 976b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 97746f09ffaSArnaldo Carvalho de Melo { 97846f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 97946f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 98046f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 98146f09ffaSArnaldo Carvalho de Melo } 98246f09ffaSArnaldo Carvalho de Melo 9837c657876SArnaldo Carvalho de Melo static int thash_entries; 9847c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9857c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9867c657876SArnaldo Carvalho de Melo 987a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9887c657876SArnaldo Carvalho de Melo int dccp_debug; 9897c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9907c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 991f21e68caSArnaldo Carvalho de Melo 992f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 993a1d3a355SArnaldo Carvalho de Melo #endif 9947c657876SArnaldo Carvalho de Melo 9957c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9967c657876SArnaldo Carvalho de Melo { 9977c657876SArnaldo Carvalho de Melo unsigned long goal; 9987c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 999b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 10007c657876SArnaldo Carvalho de Melo 10017690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 10027690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 10037690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 10047690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 10057c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1006b61fafc4SArnaldo Carvalho de Melo goto out; 10077c657876SArnaldo Carvalho de Melo 10087c657876SArnaldo Carvalho de Melo /* 10097c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 10107c657876SArnaldo Carvalho de Melo * hash tables. 10117c657876SArnaldo Carvalho de Melo * 10127c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 10137c657876SArnaldo Carvalho de Melo */ 10147c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 10157c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10167c657876SArnaldo Carvalho de Melo else 10177c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10187c657876SArnaldo Carvalho de Melo 10197c657876SArnaldo Carvalho de Melo if (thash_entries) 10207690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10217690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10227c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10237c657876SArnaldo Carvalho de Melo ; 10247c657876SArnaldo Carvalho de Melo do { 10257c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10267c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10277c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 10287690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10297690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10307c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10317c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10327c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10337c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10347c657876SArnaldo Carvalho de Melo 10357c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 10367c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 10377c657876SArnaldo Carvalho de Melo "established hash table\n"); 10387c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10397c657876SArnaldo Carvalho de Melo } 10407c657876SArnaldo Carvalho de Melo 10417c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 10427c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10437c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 10447c657876SArnaldo Carvalho de Melo } 10457c657876SArnaldo Carvalho de Melo 10467c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10477c657876SArnaldo Carvalho de Melo 10487c657876SArnaldo Carvalho de Melo do { 10497c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10507c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10517690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10527690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10537c657876SArnaldo Carvalho de Melo continue; 10547c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10557c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10567c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10577c657876SArnaldo Carvalho de Melo 10587c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 10597c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 10607c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10617c657876SArnaldo Carvalho de Melo } 10627c657876SArnaldo Carvalho de Melo 10637c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10647c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10657c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10667c657876SArnaldo Carvalho de Melo } 10677c657876SArnaldo Carvalho de Melo 106846f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1069fa23e2ecSArnaldo Carvalho de Melo if (rc) 10707c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10717c657876SArnaldo Carvalho de Melo 10729b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10737c657876SArnaldo Carvalho de Melo if (rc) 1074b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 10759b07ef5dSArnaldo Carvalho de Melo 1076e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10779b07ef5dSArnaldo Carvalho de Melo if (rc) 10789b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 10797c657876SArnaldo Carvalho de Melo out: 10807c657876SArnaldo Carvalho de Melo return rc; 10819b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10829b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1083b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 108446f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10857c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10867c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10877c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10887c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10897c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10907c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10917c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10927c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10937c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10947c657876SArnaldo Carvalho de Melo goto out; 10957c657876SArnaldo Carvalho de Melo } 10967c657876SArnaldo Carvalho de Melo 10977c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 10987c657876SArnaldo Carvalho de Melo { 109946f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1100725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1101725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1102725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1103725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1104725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1105725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 11067c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 11079b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1108e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 11097c657876SArnaldo Carvalho de Melo } 11107c657876SArnaldo Carvalho de Melo 11117c657876SArnaldo Carvalho de Melo module_init(dccp_init); 11127c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 11137c657876SArnaldo Carvalho de Melo 11147c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 11157c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11167c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1117