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 296273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h> 307c657876SArnaldo Carvalho de Melo #include <asm/semaphore.h> 317c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 327c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 337c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 347c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 357c657876SArnaldo Carvalho de Melo 367c657876SArnaldo Carvalho de Melo #include "ccid.h" 377c657876SArnaldo Carvalho de Melo #include "dccp.h" 38afe00251SAndrea Bittau #include "feat.h" 397c657876SArnaldo Carvalho de Melo 40ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 417c657876SArnaldo Carvalho de Melo 42f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 43f21e68caSArnaldo Carvalho de Melo 447c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 457c657876SArnaldo Carvalho de Melo 46f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 47f21e68caSArnaldo Carvalho de Melo 48075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 49075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 50075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 51075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 52075ae866SArnaldo Carvalho de Melo }; 53075ae866SArnaldo Carvalho de Melo 54075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 55075ae866SArnaldo Carvalho de Melo 56b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 57b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 58b1308dc0SIan McDonald 59c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 60c25a18baSArnaldo Carvalho de Melo { 61c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 62c25a18baSArnaldo Carvalho de Melo 63c25a18baSArnaldo Carvalho de Melo dccp_pr_debug("%s(%p) %-10.10s -> %s\n", 64c25a18baSArnaldo Carvalho de Melo dccp_role(sk), sk, 65c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 66c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 67c25a18baSArnaldo Carvalho de Melo 68c25a18baSArnaldo Carvalho de Melo switch (state) { 69c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 70c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 71c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 72c25a18baSArnaldo Carvalho de Melo break; 73c25a18baSArnaldo Carvalho de Melo 74c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 75c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) 76c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 77c25a18baSArnaldo Carvalho de Melo 78c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 79c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 80c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 81c25a18baSArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 82c25a18baSArnaldo Carvalho de Melo /* fall through */ 83c25a18baSArnaldo Carvalho de Melo default: 84c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 85c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 86c25a18baSArnaldo Carvalho de Melo } 87c25a18baSArnaldo Carvalho de Melo 88c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 89c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 90c25a18baSArnaldo Carvalho de Melo */ 91c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 92c25a18baSArnaldo Carvalho de Melo } 93c25a18baSArnaldo Carvalho de Melo 94c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 95c25a18baSArnaldo Carvalho de Melo 96c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 97c25a18baSArnaldo Carvalho de Melo { 98c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 99c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 100c25a18baSArnaldo Carvalho de Melo 101c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 102c25a18baSArnaldo Carvalho de Melo 103c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 104c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 105c25a18baSArnaldo Carvalho de Melo else 106c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 107c25a18baSArnaldo Carvalho de Melo } 108c25a18baSArnaldo Carvalho de Melo 109c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 110c25a18baSArnaldo Carvalho de Melo 1117c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1127c657876SArnaldo Carvalho de Melo { 1137c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1147c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1157c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1167c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1177c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1187c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1197c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1207c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1217c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1227c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1237c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1247c657876SArnaldo Carvalho de Melo }; 1257c657876SArnaldo Carvalho de Melo 1267c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1277c657876SArnaldo Carvalho de Melo return "INVALID"; 1287c657876SArnaldo Carvalho de Melo else 1297c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1307c657876SArnaldo Carvalho de Melo } 1317c657876SArnaldo Carvalho de Melo 1327c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1337c657876SArnaldo Carvalho de Melo 1347c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1357c657876SArnaldo Carvalho de Melo { 1367c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1377c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1387c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1397c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1407c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1417c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1427c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 1437c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1447c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1457c657876SArnaldo Carvalho de Melo }; 1467c657876SArnaldo Carvalho de Melo 1477c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1487c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1497c657876SArnaldo Carvalho de Melo else 1507c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1517c657876SArnaldo Carvalho de Melo } 1527c657876SArnaldo Carvalho de Melo 1537c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1547c657876SArnaldo Carvalho de Melo 155c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 156c985ed70SArnaldo Carvalho de Melo { 157c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 158c985ed70SArnaldo Carvalho de Melo } 159c985ed70SArnaldo Carvalho de Melo 160c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 161c985ed70SArnaldo Carvalho de Melo 162c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 163c985ed70SArnaldo Carvalho de Melo { 164c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 165c985ed70SArnaldo Carvalho de Melo } 166c985ed70SArnaldo Carvalho de Melo 167c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 168c985ed70SArnaldo Carvalho de Melo 16972478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1703e0fadc5SArnaldo Carvalho de Melo { 1713e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 172a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 1733e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1743e0fadc5SArnaldo Carvalho de Melo 175a4bf3902SArnaldo Carvalho de Melo dccp_minisock_init(&dp->dccps_minisock); 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; 223a94f0f97SGerrit Renker dp->dccps_rate_last = jiffies; 2243e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 22500e4d116SGerrit Renker dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 2263e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 2273e0fadc5SArnaldo Carvalho de Melo 2283e0fadc5SArnaldo Carvalho de Melo return 0; 2293e0fadc5SArnaldo Carvalho de Melo } 2303e0fadc5SArnaldo Carvalho de Melo 2313e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2323e0fadc5SArnaldo Carvalho de Melo 2333e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 2343e0fadc5SArnaldo Carvalho de Melo { 2353e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2368ca0d17bSArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 2373e0fadc5SArnaldo Carvalho de Melo 2383e0fadc5SArnaldo Carvalho de Melo /* 2393e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2403e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2413e0fadc5SArnaldo Carvalho de Melo */ 2423e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2433e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2443e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2453e0fadc5SArnaldo Carvalho de Melo } 2463e0fadc5SArnaldo Carvalho de Melo 2473e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2483e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2493e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2503e0fadc5SArnaldo Carvalho de Melo 2513e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2523e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2533e0fadc5SArnaldo Carvalho de Melo 2548ca0d17bSArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2553e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2563e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2573e0fadc5SArnaldo Carvalho de Melo } 2583e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2593e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2603e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2613e0fadc5SArnaldo Carvalho de Melo 2623e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2638ca0d17bSArnaldo Carvalho de Melo dccp_feat_clean(dmsk); 2643e0fadc5SArnaldo Carvalho de Melo 2653e0fadc5SArnaldo Carvalho de Melo return 0; 2663e0fadc5SArnaldo Carvalho de Melo } 2673e0fadc5SArnaldo Carvalho de Melo 2683e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2693e0fadc5SArnaldo Carvalho de Melo 27072a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2717c657876SArnaldo Carvalho de Melo { 27267e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 27367e6b629SArnaldo Carvalho de Melo 27467e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 27572a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2767c657876SArnaldo Carvalho de Melo } 2777c657876SArnaldo Carvalho de Melo 2787c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2797c657876SArnaldo Carvalho de Melo { 2807c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2817c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2827c657876SArnaldo Carvalho de Melo int err = 0; 2837c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2847c657876SArnaldo Carvalho de Melo 2857c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2867c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2877c657876SArnaldo Carvalho de Melo 2887c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2897c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2907c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2917c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2927c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2937c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2947c657876SArnaldo Carvalho de Melo 2957c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2967c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2977c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2987c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2997c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 3007c657876SArnaldo Carvalho de Melo } 3017c657876SArnaldo Carvalho de Melo 3027c657876SArnaldo Carvalho de Melo inet->dport = 0; 3037c657876SArnaldo Carvalho de Melo 3047c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 3057c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 3067c657876SArnaldo Carvalho de Melo 3077c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3087c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3097c657876SArnaldo Carvalho de Melo 3107c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3117c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3127c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3137c657876SArnaldo Carvalho de Melo 3147c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 3157c657876SArnaldo Carvalho de Melo 3167c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3177c657876SArnaldo Carvalho de Melo return err; 3187c657876SArnaldo Carvalho de Melo } 3197c657876SArnaldo Carvalho de Melo 320f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 321f21e68caSArnaldo Carvalho de Melo 322331968bdSArnaldo Carvalho de Melo /* 323331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 324331968bdSArnaldo Carvalho de Melo * 325331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 326331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 327331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 328331968bdSArnaldo Carvalho de Melo */ 329f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 330331968bdSArnaldo Carvalho de Melo poll_table *wait) 331331968bdSArnaldo Carvalho de Melo { 332331968bdSArnaldo Carvalho de Melo unsigned int mask; 333331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 334331968bdSArnaldo Carvalho de Melo 335331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 336331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 337331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 338331968bdSArnaldo Carvalho de Melo 339331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 340331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 341331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 342331968bdSArnaldo Carvalho de Melo */ 343331968bdSArnaldo Carvalho de Melo 344331968bdSArnaldo Carvalho de Melo mask = 0; 345331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 346331968bdSArnaldo Carvalho de Melo mask = POLLERR; 347331968bdSArnaldo Carvalho de Melo 348331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 349331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 350331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 351f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 352331968bdSArnaldo Carvalho de Melo 353331968bdSArnaldo Carvalho de Melo /* Connected? */ 354331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 355331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 356331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 357331968bdSArnaldo Carvalho de Melo 358331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 359331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 360331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 361331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 362331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 363331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 364331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 365331968bdSArnaldo Carvalho de Melo 366331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 367331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 368331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 369331968bdSArnaldo Carvalho de Melo */ 370331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 371331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 372331968bdSArnaldo Carvalho de Melo } 373331968bdSArnaldo Carvalho de Melo } 374331968bdSArnaldo Carvalho de Melo } 375331968bdSArnaldo Carvalho de Melo return mask; 376331968bdSArnaldo Carvalho de Melo } 377331968bdSArnaldo Carvalho de Melo 378f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 379f21e68caSArnaldo Carvalho de Melo 3807c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3817c657876SArnaldo Carvalho de Melo { 3826273172eSArnaldo Carvalho de Melo int rc = -ENOTCONN; 3836273172eSArnaldo Carvalho de Melo 3846273172eSArnaldo Carvalho de Melo lock_sock(sk); 3856273172eSArnaldo Carvalho de Melo 3866273172eSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 3876273172eSArnaldo Carvalho de Melo goto out; 3886273172eSArnaldo Carvalho de Melo 3896273172eSArnaldo Carvalho de Melo switch (cmd) { 3906273172eSArnaldo Carvalho de Melo case SIOCINQ: { 3916273172eSArnaldo Carvalho de Melo struct sk_buff *skb; 3926273172eSArnaldo Carvalho de Melo unsigned long amount = 0; 3936273172eSArnaldo Carvalho de Melo 3946273172eSArnaldo Carvalho de Melo skb = skb_peek(&sk->sk_receive_queue); 3956273172eSArnaldo Carvalho de Melo if (skb != NULL) { 3966273172eSArnaldo Carvalho de Melo /* 3976273172eSArnaldo Carvalho de Melo * We will only return the amount of this packet since 3986273172eSArnaldo Carvalho de Melo * that is all that will be read. 3996273172eSArnaldo Carvalho de Melo */ 4006273172eSArnaldo Carvalho de Melo amount = skb->len; 4016273172eSArnaldo Carvalho de Melo } 4026273172eSArnaldo Carvalho de Melo rc = put_user(amount, (int __user *)arg); 4036273172eSArnaldo Carvalho de Melo } 4046273172eSArnaldo Carvalho de Melo break; 4056273172eSArnaldo Carvalho de Melo default: 4066273172eSArnaldo Carvalho de Melo rc = -ENOIOCTLCMD; 4076273172eSArnaldo Carvalho de Melo break; 4086273172eSArnaldo Carvalho de Melo } 4096273172eSArnaldo Carvalho de Melo out: 4106273172eSArnaldo Carvalho de Melo release_sock(sk); 4116273172eSArnaldo Carvalho de Melo return rc; 4127c657876SArnaldo Carvalho de Melo } 4137c657876SArnaldo Carvalho de Melo 414f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 415f21e68caSArnaldo Carvalho de Melo 41660fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 41767e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 41867e6b629SArnaldo Carvalho de Melo { 41967e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 42067e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 42167e6b629SArnaldo Carvalho de Melo 42267e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 42367e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 42467e6b629SArnaldo Carvalho de Melo return -EINVAL; 42567e6b629SArnaldo Carvalho de Melo 42667e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 42767e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 42867e6b629SArnaldo Carvalho de Melo if (sl == NULL) 42967e6b629SArnaldo Carvalho de Melo return -ENOMEM; 43067e6b629SArnaldo Carvalho de Melo 43167e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 43267e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 43367e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 43467e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 43567e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 43667e6b629SArnaldo Carvalho de Melo kfree(sl); 43767e6b629SArnaldo Carvalho de Melo return -EFAULT; 43867e6b629SArnaldo Carvalho de Melo } 43967e6b629SArnaldo Carvalho de Melo } 44067e6b629SArnaldo Carvalho de Melo 44167e6b629SArnaldo Carvalho de Melo lock_sock(sk); 44267e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 44367e6b629SArnaldo Carvalho de Melo 44467e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 44567e6b629SArnaldo Carvalho de Melo 44667e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 44767e6b629SArnaldo Carvalho de Melo release_sock(sk); 44867e6b629SArnaldo Carvalho de Melo return 0; 44967e6b629SArnaldo Carvalho de Melo } 45067e6b629SArnaldo Carvalho de Melo 451afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 452afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 453afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 454afe00251SAndrea Bittau { 455afe00251SAndrea Bittau struct dccp_so_feat opt; 456afe00251SAndrea Bittau u8 *val; 457afe00251SAndrea Bittau int rc; 458afe00251SAndrea Bittau 459afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 460afe00251SAndrea Bittau return -EFAULT; 461afe00251SAndrea Bittau 462afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 463afe00251SAndrea Bittau if (!val) 464afe00251SAndrea Bittau return -ENOMEM; 465afe00251SAndrea Bittau 466afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 467afe00251SAndrea Bittau rc = -EFAULT; 468afe00251SAndrea Bittau goto out_free_val; 469afe00251SAndrea Bittau } 470afe00251SAndrea Bittau 4718ca0d17bSArnaldo Carvalho de Melo rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat, 4728ca0d17bSArnaldo Carvalho de Melo val, opt.dccpsf_len, GFP_KERNEL); 473afe00251SAndrea Bittau if (rc) 474afe00251SAndrea Bittau goto out_free_val; 475afe00251SAndrea Bittau 476afe00251SAndrea Bittau out: 477afe00251SAndrea Bittau return rc; 478afe00251SAndrea Bittau 479afe00251SAndrea Bittau out_free_val: 480afe00251SAndrea Bittau kfree(val); 481afe00251SAndrea Bittau goto out; 482afe00251SAndrea Bittau } 483afe00251SAndrea Bittau 4843fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 485a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4867c657876SArnaldo Carvalho de Melo { 48709dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 48809dbc389SGerrit Renker int val, err = 0; 4897c657876SArnaldo Carvalho de Melo 490a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 491a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 492a84ffe43SArnaldo Carvalho de Melo 493a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 494a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 495a84ffe43SArnaldo Carvalho de Melo 49667e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 49767e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 498a84ffe43SArnaldo Carvalho de Melo 49967e6b629SArnaldo Carvalho de Melo lock_sock(sk); 500a84ffe43SArnaldo Carvalho de Melo switch (optname) { 501a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 5025aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 503841bac1dSArnaldo Carvalho de Melo err = 0; 504a84ffe43SArnaldo Carvalho de Melo break; 505afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 506afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 507afe00251SAndrea Bittau err = -EINVAL; 508afe00251SAndrea Bittau else 509afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L, 5109faefb6dSAlan Cox (struct dccp_so_feat __user *) 511afe00251SAndrea Bittau optval); 512afe00251SAndrea Bittau break; 513afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 514afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 515afe00251SAndrea Bittau err = -EINVAL; 516afe00251SAndrea Bittau else 517afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 5189faefb6dSAlan Cox (struct dccp_so_feat __user *) 519afe00251SAndrea Bittau optval); 520afe00251SAndrea Bittau break; 5216f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: /* sender side, RFC 4340, sec. 9.2 */ 5226f4e5fffSGerrit Renker if (val < 0 || val > 15) 5236f4e5fffSGerrit Renker err = -EINVAL; 5246f4e5fffSGerrit Renker else 5256f4e5fffSGerrit Renker dp->dccps_pcslen = val; 5266f4e5fffSGerrit Renker break; 5276f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: /* receiver side, RFC 4340 sec. 9.2.1 */ 5286f4e5fffSGerrit Renker if (val < 0 || val > 15) 5296f4e5fffSGerrit Renker err = -EINVAL; 5306f4e5fffSGerrit Renker else { 5316f4e5fffSGerrit Renker dp->dccps_pcrlen = val; 5326f4e5fffSGerrit Renker /* FIXME: add feature negotiation, 5336f4e5fffSGerrit Renker * ChangeL(MinimumChecksumCoverage, val) */ 5346f4e5fffSGerrit Renker } 5356f4e5fffSGerrit Renker break; 536a84ffe43SArnaldo Carvalho de Melo default: 537a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 538a84ffe43SArnaldo Carvalho de Melo break; 539a84ffe43SArnaldo Carvalho de Melo } 540a84ffe43SArnaldo Carvalho de Melo 541a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 542a84ffe43SArnaldo Carvalho de Melo return err; 5437c657876SArnaldo Carvalho de Melo } 5447c657876SArnaldo Carvalho de Melo 5453fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5463fdadf7dSDmitry Mishin char __user *optval, int optlen) 5473fdadf7dSDmitry Mishin { 5483fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5493fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5503fdadf7dSDmitry Mishin optname, optval, 5513fdadf7dSDmitry Mishin optlen); 5523fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5533fdadf7dSDmitry Mishin } 554543d9cfeSArnaldo Carvalho de Melo 555f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 556f21e68caSArnaldo Carvalho de Melo 5573fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5583fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5593fdadf7dSDmitry Mishin char __user *optval, int optlen) 5603fdadf7dSDmitry Mishin { 561dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 562dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 563dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5643fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5653fdadf7dSDmitry Mishin } 566543d9cfeSArnaldo Carvalho de Melo 5673fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5683fdadf7dSDmitry Mishin #endif 5693fdadf7dSDmitry Mishin 57067e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 57160fe62e7SAndrea Bittau __be32 __user *optval, 57267e6b629SArnaldo Carvalho de Melo int __user *optlen) 57367e6b629SArnaldo Carvalho de Melo { 57467e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 57567e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 57667e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 57767e6b629SArnaldo Carvalho de Melo 57867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 57967e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 58067e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 58167e6b629SArnaldo Carvalho de Melo total_len += slen; 58267e6b629SArnaldo Carvalho de Melo } 58367e6b629SArnaldo Carvalho de Melo 58467e6b629SArnaldo Carvalho de Melo err = -EINVAL; 58567e6b629SArnaldo Carvalho de Melo if (total_len > len) 58667e6b629SArnaldo Carvalho de Melo goto out; 58767e6b629SArnaldo Carvalho de Melo 58867e6b629SArnaldo Carvalho de Melo err = 0; 58967e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 59067e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 59167e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 59267e6b629SArnaldo Carvalho de Melo err = -EFAULT; 59367e6b629SArnaldo Carvalho de Melo out: 59467e6b629SArnaldo Carvalho de Melo release_sock(sk); 59567e6b629SArnaldo Carvalho de Melo return err; 59667e6b629SArnaldo Carvalho de Melo } 59767e6b629SArnaldo Carvalho de Melo 5983fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 599a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 6007c657876SArnaldo Carvalho de Melo { 601a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 602a84ffe43SArnaldo Carvalho de Melo int val, len; 6037c657876SArnaldo Carvalho de Melo 604a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 605a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 606a84ffe43SArnaldo Carvalho de Melo 60739ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 608a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 609a84ffe43SArnaldo Carvalho de Melo 610a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 611a84ffe43SArnaldo Carvalho de Melo 612a84ffe43SArnaldo Carvalho de Melo switch (optname) { 613a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 6145aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 615841bac1dSArnaldo Carvalho de Melo return 0; 61688f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 61788f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 61860fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 6197c559a9eSGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS: 6207c559a9eSGerrit Renker val = dp->dccps_mss_cache; 6217c559a9eSGerrit Renker len = sizeof(val); 6227c559a9eSGerrit Renker break; 6236f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 6246f4e5fffSGerrit Renker val = dp->dccps_pcslen; 62539ebc027SArnaldo Carvalho de Melo len = sizeof(val); 6266f4e5fffSGerrit Renker break; 6276f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 6286f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 62939ebc027SArnaldo Carvalho de Melo len = sizeof(val); 6306f4e5fffSGerrit Renker break; 63188f964dbSArnaldo Carvalho de Melo case 128 ... 191: 63288f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 63388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 63488f964dbSArnaldo Carvalho de Melo case 192 ... 255: 63588f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 63688f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 637a84ffe43SArnaldo Carvalho de Melo default: 638a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 639a84ffe43SArnaldo Carvalho de Melo } 640a84ffe43SArnaldo Carvalho de Melo 641a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 642a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 643a84ffe43SArnaldo Carvalho de Melo 644a84ffe43SArnaldo Carvalho de Melo return 0; 6457c657876SArnaldo Carvalho de Melo } 6467c657876SArnaldo Carvalho de Melo 6473fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6483fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6493fdadf7dSDmitry Mishin { 6503fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6513fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6523fdadf7dSDmitry Mishin optname, optval, 6533fdadf7dSDmitry Mishin optlen); 6543fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6553fdadf7dSDmitry Mishin } 656543d9cfeSArnaldo Carvalho de Melo 657f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 658f21e68caSArnaldo Carvalho de Melo 6593fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6603fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6613fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6623fdadf7dSDmitry Mishin { 663dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 664dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 665dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6663fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6673fdadf7dSDmitry Mishin } 668543d9cfeSArnaldo Carvalho de Melo 6693fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6703fdadf7dSDmitry Mishin #endif 6713fdadf7dSDmitry Mishin 6727c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6737c657876SArnaldo Carvalho de Melo size_t len) 6747c657876SArnaldo Carvalho de Melo { 6757c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 6767c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 6777c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 6787c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 6797c657876SArnaldo Carvalho de Melo int rc, size; 6807c657876SArnaldo Carvalho de Melo long timeo; 6817c657876SArnaldo Carvalho de Melo 6827c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 6837c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 6847c657876SArnaldo Carvalho de Melo 6857c657876SArnaldo Carvalho de Melo lock_sock(sk); 686b1308dc0SIan McDonald 687b1308dc0SIan McDonald if (sysctl_dccp_tx_qlen && 688b1308dc0SIan McDonald (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) { 689b1308dc0SIan McDonald rc = -EAGAIN; 690b1308dc0SIan McDonald goto out_release; 691b1308dc0SIan McDonald } 692b1308dc0SIan McDonald 69327258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6947c657876SArnaldo Carvalho de Melo 6957c657876SArnaldo Carvalho de Melo /* 6967c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6977c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6987c657876SArnaldo Carvalho de Melo */ 6997c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 700cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 7017c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 70227258ee5SArnaldo Carvalho de Melo goto out_release; 7037c657876SArnaldo Carvalho de Melo 7047c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 7057c657876SArnaldo Carvalho de Melo release_sock(sk); 7067c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 7077c657876SArnaldo Carvalho de Melo lock_sock(sk); 7087c657876SArnaldo Carvalho de Melo if (skb == NULL) 7097c657876SArnaldo Carvalho de Melo goto out_release; 7107c657876SArnaldo Carvalho de Melo 7117c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 7127c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 71327258ee5SArnaldo Carvalho de Melo if (rc != 0) 71427258ee5SArnaldo Carvalho de Melo goto out_discard; 7157c657876SArnaldo Carvalho de Melo 71697e5848dSIan McDonald skb_queue_tail(&sk->sk_write_queue, skb); 71797e5848dSIan McDonald dccp_write_xmit(sk,0); 7187c657876SArnaldo Carvalho de Melo out_release: 7197c657876SArnaldo Carvalho de Melo release_sock(sk); 7207c657876SArnaldo Carvalho de Melo return rc ? : len; 72127258ee5SArnaldo Carvalho de Melo out_discard: 72227258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 7237c657876SArnaldo Carvalho de Melo goto out_release; 7247c657876SArnaldo Carvalho de Melo } 7257c657876SArnaldo Carvalho de Melo 726f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 727f21e68caSArnaldo Carvalho de Melo 7287c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 7297c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 7307c657876SArnaldo Carvalho de Melo { 7317c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 7327c657876SArnaldo Carvalho de Melo long timeo; 7337c657876SArnaldo Carvalho de Melo 7347c657876SArnaldo Carvalho de Melo lock_sock(sk); 7357c657876SArnaldo Carvalho de Melo 736531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 737531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7387c657876SArnaldo Carvalho de Melo goto out; 739531669a0SArnaldo Carvalho de Melo } 7407c657876SArnaldo Carvalho de Melo 7417c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 7427c657876SArnaldo Carvalho de Melo 7437c657876SArnaldo Carvalho de Melo do { 744531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 7457c657876SArnaldo Carvalho de Melo 746531669a0SArnaldo Carvalho de Melo if (skb == NULL) 747531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 7487c657876SArnaldo Carvalho de Melo 7497c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 7507c657876SArnaldo Carvalho de Melo 7517c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 7527c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 7537c657876SArnaldo Carvalho de Melo goto found_ok_skb; 7547c657876SArnaldo Carvalho de Melo 7557c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 7567c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 7577c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 758531669a0SArnaldo Carvalho de Melo len = 0; 7597c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7607c657876SArnaldo Carvalho de Melo } 7617690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7627690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 763624d1164SChris Leech sk_eat_skb(sk, skb, 0); 764531669a0SArnaldo Carvalho de Melo verify_sock_status: 765531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 766531669a0SArnaldo Carvalho de Melo len = 0; 7677c657876SArnaldo Carvalho de Melo break; 7687c657876SArnaldo Carvalho de Melo } 7697c657876SArnaldo Carvalho de Melo 770531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 771531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7727c657876SArnaldo Carvalho de Melo break; 773531669a0SArnaldo Carvalho de Melo } 774531669a0SArnaldo Carvalho de Melo 775531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 776531669a0SArnaldo Carvalho de Melo len = 0; 777531669a0SArnaldo Carvalho de Melo break; 778531669a0SArnaldo Carvalho de Melo } 7797c657876SArnaldo Carvalho de Melo 7807c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7817c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7827c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7837c657876SArnaldo Carvalho de Melo * from never connected socket. 7847c657876SArnaldo Carvalho de Melo */ 785531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7867c657876SArnaldo Carvalho de Melo break; 7877c657876SArnaldo Carvalho de Melo } 788531669a0SArnaldo Carvalho de Melo len = 0; 7897c657876SArnaldo Carvalho de Melo break; 7907c657876SArnaldo Carvalho de Melo } 7917c657876SArnaldo Carvalho de Melo 7927c657876SArnaldo Carvalho de Melo if (!timeo) { 793531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7947c657876SArnaldo Carvalho de Melo break; 7957c657876SArnaldo Carvalho de Melo } 7967c657876SArnaldo Carvalho de Melo 7977c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 798531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7997c657876SArnaldo Carvalho de Melo break; 8007c657876SArnaldo Carvalho de Melo } 8017c657876SArnaldo Carvalho de Melo 8027c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 8037c657876SArnaldo Carvalho de Melo continue; 8047c657876SArnaldo Carvalho de Melo found_ok_skb: 805531669a0SArnaldo Carvalho de Melo if (len > skb->len) 806531669a0SArnaldo Carvalho de Melo len = skb->len; 807531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 808531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 8097c657876SArnaldo Carvalho de Melo 810531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 8117c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 812531669a0SArnaldo Carvalho de Melo len = -EFAULT; 8137c657876SArnaldo Carvalho de Melo break; 8147c657876SArnaldo Carvalho de Melo } 8157c657876SArnaldo Carvalho de Melo found_fin_ok: 8167c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 817624d1164SChris Leech sk_eat_skb(sk, skb, 0); 8187c657876SArnaldo Carvalho de Melo break; 819531669a0SArnaldo Carvalho de Melo } while (1); 8207c657876SArnaldo Carvalho de Melo out: 8217c657876SArnaldo Carvalho de Melo release_sock(sk); 822531669a0SArnaldo Carvalho de Melo return len; 8237c657876SArnaldo Carvalho de Melo } 8247c657876SArnaldo Carvalho de Melo 825f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 826f21e68caSArnaldo Carvalho de Melo 827f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 8287c657876SArnaldo Carvalho de Melo { 8297c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 8307c657876SArnaldo Carvalho de Melo unsigned char old_state; 8317c657876SArnaldo Carvalho de Melo int err; 8327c657876SArnaldo Carvalho de Melo 8337c657876SArnaldo Carvalho de Melo lock_sock(sk); 8347c657876SArnaldo Carvalho de Melo 8357c657876SArnaldo Carvalho de Melo err = -EINVAL; 8367c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 8377c657876SArnaldo Carvalho de Melo goto out; 8387c657876SArnaldo Carvalho de Melo 8397c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 8407c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 8417c657876SArnaldo Carvalho de Melo goto out; 8427c657876SArnaldo Carvalho de Melo 8437c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 8447c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 8457c657876SArnaldo Carvalho de Melo */ 8467c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 8477c657876SArnaldo Carvalho de Melo /* 8487c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 8497c657876SArnaldo Carvalho de Melo * see tcp_listen_start 8507c657876SArnaldo Carvalho de Melo */ 85172a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 8527c657876SArnaldo Carvalho de Melo if (err) 8537c657876SArnaldo Carvalho de Melo goto out; 8547c657876SArnaldo Carvalho de Melo } 8557c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 8567c657876SArnaldo Carvalho de Melo err = 0; 8577c657876SArnaldo Carvalho de Melo 8587c657876SArnaldo Carvalho de Melo out: 8597c657876SArnaldo Carvalho de Melo release_sock(sk); 8607c657876SArnaldo Carvalho de Melo return err; 8617c657876SArnaldo Carvalho de Melo } 8627c657876SArnaldo Carvalho de Melo 863f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 864f21e68caSArnaldo Carvalho de Melo 8657c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8667c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8677c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8687c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8697c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8707c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8717c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8727c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8737c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8747c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8757c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8767c657876SArnaldo Carvalho de Melo }; 8777c657876SArnaldo Carvalho de Melo 8787c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8797c657876SArnaldo Carvalho de Melo { 8807c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8817c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8827c657876SArnaldo Carvalho de Melo 8837c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8847c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8857c657876SArnaldo Carvalho de Melo 8867c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8877c657876SArnaldo Carvalho de Melo } 8887c657876SArnaldo Carvalho de Melo 8897c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8907c657876SArnaldo Carvalho de Melo { 89197e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 8927c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 893134af346SHerbert Xu int state; 8947c657876SArnaldo Carvalho de Melo 8957c657876SArnaldo Carvalho de Melo lock_sock(sk); 8967c657876SArnaldo Carvalho de Melo 8977c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8987c657876SArnaldo Carvalho de Melo 8997c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 9007c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9017c657876SArnaldo Carvalho de Melo 9027c657876SArnaldo Carvalho de Melo /* Special case. */ 9037c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 9047c657876SArnaldo Carvalho de Melo 9057c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 9067c657876SArnaldo Carvalho de Melo } 9077c657876SArnaldo Carvalho de Melo 90897e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 90997e5848dSIan McDonald 9107c657876SArnaldo Carvalho de Melo /* 9117c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 9127c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 9137c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 9147c657876SArnaldo Carvalho de Melo */ 9157c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 9167c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 9177c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 9187c657876SArnaldo Carvalho de Melo } 9197c657876SArnaldo Carvalho de Melo 9207c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 9217c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 9227c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 9237c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 9247ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 9257c657876SArnaldo Carvalho de Melo } 9267c657876SArnaldo Carvalho de Melo 9277c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 9287c657876SArnaldo Carvalho de Melo 9297c657876SArnaldo Carvalho de Melo adjudge_to_death: 930134af346SHerbert Xu state = sk->sk_state; 931134af346SHerbert Xu sock_hold(sk); 932134af346SHerbert Xu sock_orphan(sk); 933134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 934134af346SHerbert Xu 9357ad07e7cSArnaldo Carvalho de Melo /* 9367ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 9377ad07e7cSArnaldo Carvalho de Melo */ 9387c657876SArnaldo Carvalho de Melo release_sock(sk); 9397c657876SArnaldo Carvalho de Melo /* 9407c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 9417c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 9427c657876SArnaldo Carvalho de Melo */ 9437c657876SArnaldo Carvalho de Melo local_bh_disable(); 9447c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 9457c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 9467c657876SArnaldo Carvalho de Melo 947134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 948134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 949134af346SHerbert Xu goto out; 9507c657876SArnaldo Carvalho de Melo 9517ad07e7cSArnaldo Carvalho de Melo /* 9527ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 9537ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 9547ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 9557ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 9567ad07e7cSArnaldo Carvalho de Melo */ 9577ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 9587ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 9597ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9607ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9617ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9627ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9637ad07e7cSArnaldo Carvalho de Melo #if 0 9647ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9657c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9667ad07e7cSArnaldo Carvalho de Melo #endif 9677ad07e7cSArnaldo Carvalho de Melo } 9687c657876SArnaldo Carvalho de Melo 9697c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9707c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9717c657876SArnaldo Carvalho de Melo 9727c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9737c657876SArnaldo Carvalho de Melo 974134af346SHerbert Xu out: 9757c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9767c657876SArnaldo Carvalho de Melo local_bh_enable(); 9777c657876SArnaldo Carvalho de Melo sock_put(sk); 9787c657876SArnaldo Carvalho de Melo } 9797c657876SArnaldo Carvalho de Melo 980f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 981f21e68caSArnaldo Carvalho de Melo 9827c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9837c657876SArnaldo Carvalho de Melo { 9847c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9857c657876SArnaldo Carvalho de Melo } 9867c657876SArnaldo Carvalho de Melo 987f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 988f21e68caSArnaldo Carvalho de Melo 98946f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9907c657876SArnaldo Carvalho de Melo { 9917c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9927c657876SArnaldo Carvalho de Melo 9937c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9947c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9957c657876SArnaldo Carvalho de Melo goto out; 9967c657876SArnaldo Carvalho de Melo 9977c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9987c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9997c657876SArnaldo Carvalho de Melo goto out_free_one; 10007c657876SArnaldo Carvalho de Melo 10017c657876SArnaldo Carvalho de Melo rc = 0; 10027c657876SArnaldo Carvalho de Melo out: 10037c657876SArnaldo Carvalho de Melo return rc; 10047c657876SArnaldo Carvalho de Melo out_free_one: 10057c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 10067c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 10077c657876SArnaldo Carvalho de Melo goto out; 10087c657876SArnaldo Carvalho de Melo 10097c657876SArnaldo Carvalho de Melo } 10107c657876SArnaldo Carvalho de Melo 1011b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 101246f09ffaSArnaldo Carvalho de Melo { 101346f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 101446f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 101546f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 101646f09ffaSArnaldo Carvalho de Melo } 101746f09ffaSArnaldo Carvalho de Melo 10187c657876SArnaldo Carvalho de Melo static int thash_entries; 10197c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 10207c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 10217c657876SArnaldo Carvalho de Melo 1022a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 10237c657876SArnaldo Carvalho de Melo int dccp_debug; 1024042d18f9SGerrit Renker module_param(dccp_debug, bool, 0444); 10257c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 1026f21e68caSArnaldo Carvalho de Melo 1027f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 1028a1d3a355SArnaldo Carvalho de Melo #endif 10297c657876SArnaldo Carvalho de Melo 10307c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 10317c657876SArnaldo Carvalho de Melo { 10327c657876SArnaldo Carvalho de Melo unsigned long goal; 10337c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1034b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 10357c657876SArnaldo Carvalho de Melo 10367690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 10377690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 10387690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 103920c2df83SPaul Mundt SLAB_HWCACHE_ALIGN, NULL); 10407c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1041b61fafc4SArnaldo Carvalho de Melo goto out; 10427c657876SArnaldo Carvalho de Melo 10437c657876SArnaldo Carvalho de Melo /* 10447c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 10457c657876SArnaldo Carvalho de Melo * hash tables. 10467c657876SArnaldo Carvalho de Melo * 10477c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 10487c657876SArnaldo Carvalho de Melo */ 10497c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 10507c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10517c657876SArnaldo Carvalho de Melo else 10527c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10537c657876SArnaldo Carvalho de Melo 10547c657876SArnaldo Carvalho de Melo if (thash_entries) 10557690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10567690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10577c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10587c657876SArnaldo Carvalho de Melo ; 10597c657876SArnaldo Carvalho de Melo do { 10607c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10617c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10627690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10637690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10647c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10657c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10667c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10677c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10687c657876SArnaldo Carvalho de Melo 10697c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 107059348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 10717c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10727c657876SArnaldo Carvalho de Melo } 10737c657876SArnaldo Carvalho de Melo 1074dbca9b27SEric Dumazet for (i = 0; i < dccp_hashinfo.ehash_size; i++) { 10757c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10767c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 1077dbca9b27SEric Dumazet INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].twchain); 10787c657876SArnaldo Carvalho de Melo } 10797c657876SArnaldo Carvalho de Melo 10807c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10817c657876SArnaldo Carvalho de Melo 10827c657876SArnaldo Carvalho de Melo do { 10837c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10847c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10857690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10867690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10877c657876SArnaldo Carvalho de Melo continue; 10887c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10897c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10907c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10917c657876SArnaldo Carvalho de Melo 10927c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 109359348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 10947c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10957c657876SArnaldo Carvalho de Melo } 10967c657876SArnaldo Carvalho de Melo 10977c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10987c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10997c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 11007c657876SArnaldo Carvalho de Melo } 11017c657876SArnaldo Carvalho de Melo 110246f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1103fa23e2ecSArnaldo Carvalho de Melo if (rc) 11047c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 11057c657876SArnaldo Carvalho de Melo 11069b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 11077c657876SArnaldo Carvalho de Melo if (rc) 1108b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 11099b07ef5dSArnaldo Carvalho de Melo 1110e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 11119b07ef5dSArnaldo Carvalho de Melo if (rc) 11129b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 11134c70f383SGerrit Renker 11144c70f383SGerrit Renker dccp_timestamping_init(); 11157c657876SArnaldo Carvalho de Melo out: 11167c657876SArnaldo Carvalho de Melo return rc; 11179b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 11189b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1119b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 112046f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 11217c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 11227c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 11237c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 11247c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 11257c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 11267c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 11277c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 11287c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 11297c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 11307c657876SArnaldo Carvalho de Melo goto out; 11317c657876SArnaldo Carvalho de Melo } 11327c657876SArnaldo Carvalho de Melo 11337c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 11347c657876SArnaldo Carvalho de Melo { 113546f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1136725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1137725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1138725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1139725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1140725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1141725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 11427c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 11439b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1144e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 11457c657876SArnaldo Carvalho de Melo } 11467c657876SArnaldo Carvalho de Melo 11477c657876SArnaldo Carvalho de Melo module_init(dccp_init); 11487c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 11497c657876SArnaldo Carvalho de Melo 11507c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 11517c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11527c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1153