17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/proto.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 57c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 67c657876SArnaldo Carvalho de Melo * 77c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or modify it 87c657876SArnaldo Carvalho de Melo * under the terms of the GNU General Public License version 2 as 97c657876SArnaldo Carvalho de Melo * published by the Free Software Foundation. 107c657876SArnaldo Carvalho de Melo */ 117c657876SArnaldo Carvalho de Melo 127c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 137c657876SArnaldo Carvalho de Melo #include <linux/module.h> 147c657876SArnaldo Carvalho de Melo #include <linux/types.h> 157c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 167c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 177c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 187c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 197c657876SArnaldo Carvalho de Melo #include <linux/in.h> 207c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 217c657876SArnaldo Carvalho de Melo #include <linux/init.h> 227c657876SArnaldo Carvalho de Melo #include <linux/random.h> 237c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 247c657876SArnaldo Carvalho de Melo 2514c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 267c657876SArnaldo Carvalho de Melo #include <net/sock.h> 277c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 287c657876SArnaldo Carvalho de Melo 297c657876SArnaldo Carvalho de Melo #include <asm/semaphore.h> 307c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 317c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 327c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 337c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 347c657876SArnaldo Carvalho de Melo 357c657876SArnaldo Carvalho de Melo #include "ccid.h" 367c657876SArnaldo Carvalho de Melo #include "dccp.h" 37afe00251SAndrea Bittau #include "feat.h" 387c657876SArnaldo Carvalho de Melo 39ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 407c657876SArnaldo Carvalho de Melo 41f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 42f21e68caSArnaldo Carvalho de Melo 437c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 447c657876SArnaldo Carvalho de Melo 45f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 46f21e68caSArnaldo Carvalho de Melo 47075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 48075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 49075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 50075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 51075ae866SArnaldo Carvalho de Melo }; 52075ae866SArnaldo Carvalho de Melo 53075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 54075ae866SArnaldo Carvalho de Melo 55c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 56c25a18baSArnaldo Carvalho de Melo { 57c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 58c25a18baSArnaldo Carvalho de Melo 59c25a18baSArnaldo Carvalho de Melo dccp_pr_debug("%s(%p) %-10.10s -> %s\n", 60c25a18baSArnaldo Carvalho de Melo dccp_role(sk), sk, 61c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 62c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 63c25a18baSArnaldo Carvalho de Melo 64c25a18baSArnaldo Carvalho de Melo switch (state) { 65c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 66c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 67c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 68c25a18baSArnaldo Carvalho de Melo break; 69c25a18baSArnaldo Carvalho de Melo 70c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 71c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) 72c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 73c25a18baSArnaldo Carvalho de Melo 74c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 75c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 76c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 77c25a18baSArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 78c25a18baSArnaldo Carvalho de Melo /* fall through */ 79c25a18baSArnaldo Carvalho de Melo default: 80c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 81c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 82c25a18baSArnaldo Carvalho de Melo } 83c25a18baSArnaldo Carvalho de Melo 84c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 85c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 86c25a18baSArnaldo Carvalho de Melo */ 87c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 88c25a18baSArnaldo Carvalho de Melo } 89c25a18baSArnaldo Carvalho de Melo 90c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 91c25a18baSArnaldo Carvalho de Melo 92c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 93c25a18baSArnaldo Carvalho de Melo { 94c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 95c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 96c25a18baSArnaldo Carvalho de Melo 97c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 98c25a18baSArnaldo Carvalho de Melo 99c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 100c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 101c25a18baSArnaldo Carvalho de Melo else 102c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 103c25a18baSArnaldo Carvalho de Melo } 104c25a18baSArnaldo Carvalho de Melo 105c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 106c25a18baSArnaldo Carvalho de Melo 1077c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1087c657876SArnaldo Carvalho de Melo { 1097c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1107c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1117c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1127c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1137c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1147c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1157c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1167c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1177c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1187c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1197c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1207c657876SArnaldo Carvalho de Melo }; 1217c657876SArnaldo Carvalho de Melo 1227c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1237c657876SArnaldo Carvalho de Melo return "INVALID"; 1247c657876SArnaldo Carvalho de Melo else 1257c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1267c657876SArnaldo Carvalho de Melo } 1277c657876SArnaldo Carvalho de Melo 1287c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1297c657876SArnaldo Carvalho de Melo 1307c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1317c657876SArnaldo Carvalho de Melo { 1327c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1337c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1347c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1357c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1367c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1377c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1387c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 1397c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1407c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1417c657876SArnaldo Carvalho de Melo }; 1427c657876SArnaldo Carvalho de Melo 1437c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1447c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1457c657876SArnaldo Carvalho de Melo else 1467c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1477c657876SArnaldo Carvalho de Melo } 1487c657876SArnaldo Carvalho de Melo 1497c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1507c657876SArnaldo Carvalho de Melo 151c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 152c985ed70SArnaldo Carvalho de Melo { 153c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 154c985ed70SArnaldo Carvalho de Melo } 155c985ed70SArnaldo Carvalho de Melo 156c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 157c985ed70SArnaldo Carvalho de Melo 158c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 159c985ed70SArnaldo Carvalho de Melo { 160c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 161c985ed70SArnaldo Carvalho de Melo } 162c985ed70SArnaldo Carvalho de Melo 163c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 164c985ed70SArnaldo Carvalho de Melo 16572478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1663e0fadc5SArnaldo Carvalho de Melo { 1673e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 168a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 1693e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1703e0fadc5SArnaldo Carvalho de Melo 171a4bf3902SArnaldo Carvalho de Melo dccp_minisock_init(&dp->dccps_minisock); 1723e0fadc5SArnaldo Carvalho de Melo do_gettimeofday(&dp->dccps_epoch); 1733e0fadc5SArnaldo Carvalho de Melo 1743e0fadc5SArnaldo Carvalho de Melo /* 1753e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1763e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1773e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1783e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1793e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1803e0fadc5SArnaldo Carvalho de Melo */ 18172478873SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized)) { 1828ca0d17bSArnaldo Carvalho de Melo int rc = dccp_feat_init(dmsk); 1833e0fadc5SArnaldo Carvalho de Melo 1843e0fadc5SArnaldo Carvalho de Melo if (rc) 1853e0fadc5SArnaldo Carvalho de Melo return rc; 1863e0fadc5SArnaldo Carvalho de Melo 187a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 1883e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1893e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1903e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1913e0fadc5SArnaldo Carvalho de Melo } 192a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid, 1933e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 194a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid, 1953e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1963e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1973e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 1983e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 1993e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 200a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2013e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2023e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2033e0fadc5SArnaldo Carvalho de Melo } 2043e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2053e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 2063e0fadc5SArnaldo Carvalho de Melo } 2073e0fadc5SArnaldo Carvalho de Melo } else { 2083e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 209a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_pending); 210a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 2113e0fadc5SArnaldo Carvalho de Melo } 2123e0fadc5SArnaldo Carvalho de Melo 2133e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 2143e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 2153e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 2163e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 2173e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 2183e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 2193e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 22000e4d116SGerrit Renker dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 2213e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 2223e0fadc5SArnaldo Carvalho de Melo 2233e0fadc5SArnaldo Carvalho de Melo return 0; 2243e0fadc5SArnaldo Carvalho de Melo } 2253e0fadc5SArnaldo Carvalho de Melo 2263e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2273e0fadc5SArnaldo Carvalho de Melo 2283e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 2293e0fadc5SArnaldo Carvalho de Melo { 2303e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2318ca0d17bSArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 2323e0fadc5SArnaldo Carvalho de Melo 2333e0fadc5SArnaldo Carvalho de Melo /* 2343e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2353e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2363e0fadc5SArnaldo Carvalho de Melo */ 2373e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2383e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2393e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2403e0fadc5SArnaldo Carvalho de Melo } 2413e0fadc5SArnaldo Carvalho de Melo 2423e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2433e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2443e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2453e0fadc5SArnaldo Carvalho de Melo 2463e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2473e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2483e0fadc5SArnaldo Carvalho de Melo 2498ca0d17bSArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2503e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2513e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2523e0fadc5SArnaldo Carvalho de Melo } 2533e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2543e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2553e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2563e0fadc5SArnaldo Carvalho de Melo 2573e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2588ca0d17bSArnaldo Carvalho de Melo dccp_feat_clean(dmsk); 2593e0fadc5SArnaldo Carvalho de Melo 2603e0fadc5SArnaldo Carvalho de Melo return 0; 2613e0fadc5SArnaldo Carvalho de Melo } 2623e0fadc5SArnaldo Carvalho de Melo 2633e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2643e0fadc5SArnaldo Carvalho de Melo 26572a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2667c657876SArnaldo Carvalho de Melo { 26767e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 26867e6b629SArnaldo Carvalho de Melo 26967e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 27072a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2717c657876SArnaldo Carvalho de Melo } 2727c657876SArnaldo Carvalho de Melo 2737c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2747c657876SArnaldo Carvalho de Melo { 2757c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2767c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2777c657876SArnaldo Carvalho de Melo int err = 0; 2787c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2797c657876SArnaldo Carvalho de Melo 2807c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2817c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2827c657876SArnaldo Carvalho de Melo 2837c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2847c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2857c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2867c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2877c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2887c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2897c657876SArnaldo Carvalho de Melo 2907c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2917c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2927c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2937c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2947c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2957c657876SArnaldo Carvalho de Melo } 2967c657876SArnaldo Carvalho de Melo 2977c657876SArnaldo Carvalho de Melo inet->dport = 0; 2987c657876SArnaldo Carvalho de Melo 2997c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 3007c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 3017c657876SArnaldo Carvalho de Melo 3027c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3037c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3047c657876SArnaldo Carvalho de Melo 3057c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3067c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3077c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3087c657876SArnaldo Carvalho de Melo 3097c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 3107c657876SArnaldo Carvalho de Melo 3117c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3127c657876SArnaldo Carvalho de Melo return err; 3137c657876SArnaldo Carvalho de Melo } 3147c657876SArnaldo Carvalho de Melo 315f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 316f21e68caSArnaldo Carvalho de Melo 317331968bdSArnaldo Carvalho de Melo /* 318331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 319331968bdSArnaldo Carvalho de Melo * 320331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 321331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 322331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 323331968bdSArnaldo Carvalho de Melo */ 324f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 325331968bdSArnaldo Carvalho de Melo poll_table *wait) 326331968bdSArnaldo Carvalho de Melo { 327331968bdSArnaldo Carvalho de Melo unsigned int mask; 328331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 329331968bdSArnaldo Carvalho de Melo 330331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 331331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 332331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 333331968bdSArnaldo Carvalho de Melo 334331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 335331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 336331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 337331968bdSArnaldo Carvalho de Melo */ 338331968bdSArnaldo Carvalho de Melo 339331968bdSArnaldo Carvalho de Melo mask = 0; 340331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 341331968bdSArnaldo Carvalho de Melo mask = POLLERR; 342331968bdSArnaldo Carvalho de Melo 343331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 344331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 345331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 346f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 347331968bdSArnaldo Carvalho de Melo 348331968bdSArnaldo Carvalho de Melo /* Connected? */ 349331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 350331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 351331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 352331968bdSArnaldo Carvalho de Melo 353331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 354331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 355331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 356331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 357331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 358331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 359331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 360331968bdSArnaldo Carvalho de Melo 361331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 362331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 363331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 364331968bdSArnaldo Carvalho de Melo */ 365331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 366331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 367331968bdSArnaldo Carvalho de Melo } 368331968bdSArnaldo Carvalho de Melo } 369331968bdSArnaldo Carvalho de Melo } 370331968bdSArnaldo Carvalho de Melo return mask; 371331968bdSArnaldo Carvalho de Melo } 372331968bdSArnaldo Carvalho de Melo 373f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 374f21e68caSArnaldo Carvalho de Melo 3757c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3767c657876SArnaldo Carvalho de Melo { 3777c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3787c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3797c657876SArnaldo Carvalho de Melo } 3807c657876SArnaldo Carvalho de Melo 381f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 382f21e68caSArnaldo Carvalho de Melo 38360fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 38467e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 38567e6b629SArnaldo Carvalho de Melo { 38667e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 38767e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 38867e6b629SArnaldo Carvalho de Melo 38967e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 39067e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 39167e6b629SArnaldo Carvalho de Melo return -EINVAL; 39267e6b629SArnaldo Carvalho de Melo 39367e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 39467e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 39567e6b629SArnaldo Carvalho de Melo if (sl == NULL) 39667e6b629SArnaldo Carvalho de Melo return -ENOMEM; 39767e6b629SArnaldo Carvalho de Melo 39867e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 39967e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 40067e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 40167e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 40267e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 40367e6b629SArnaldo Carvalho de Melo kfree(sl); 40467e6b629SArnaldo Carvalho de Melo return -EFAULT; 40567e6b629SArnaldo Carvalho de Melo } 40667e6b629SArnaldo Carvalho de Melo } 40767e6b629SArnaldo Carvalho de Melo 40867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 40967e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 41067e6b629SArnaldo Carvalho de Melo 41167e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 41267e6b629SArnaldo Carvalho de Melo 41367e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 41467e6b629SArnaldo Carvalho de Melo release_sock(sk); 41567e6b629SArnaldo Carvalho de Melo return 0; 41667e6b629SArnaldo Carvalho de Melo } 41767e6b629SArnaldo Carvalho de Melo 418afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 419afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 420afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 421afe00251SAndrea Bittau { 422afe00251SAndrea Bittau struct dccp_so_feat opt; 423afe00251SAndrea Bittau u8 *val; 424afe00251SAndrea Bittau int rc; 425afe00251SAndrea Bittau 426afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 427afe00251SAndrea Bittau return -EFAULT; 428afe00251SAndrea Bittau 429afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 430afe00251SAndrea Bittau if (!val) 431afe00251SAndrea Bittau return -ENOMEM; 432afe00251SAndrea Bittau 433afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 434afe00251SAndrea Bittau rc = -EFAULT; 435afe00251SAndrea Bittau goto out_free_val; 436afe00251SAndrea Bittau } 437afe00251SAndrea Bittau 4388ca0d17bSArnaldo Carvalho de Melo rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat, 4398ca0d17bSArnaldo Carvalho de Melo val, opt.dccpsf_len, GFP_KERNEL); 440afe00251SAndrea Bittau if (rc) 441afe00251SAndrea Bittau goto out_free_val; 442afe00251SAndrea Bittau 443afe00251SAndrea Bittau out: 444afe00251SAndrea Bittau return rc; 445afe00251SAndrea Bittau 446afe00251SAndrea Bittau out_free_val: 447afe00251SAndrea Bittau kfree(val); 448afe00251SAndrea Bittau goto out; 449afe00251SAndrea Bittau } 450afe00251SAndrea Bittau 4513fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 452a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4537c657876SArnaldo Carvalho de Melo { 454a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 455a84ffe43SArnaldo Carvalho de Melo int err; 456a84ffe43SArnaldo Carvalho de Melo int val; 4577c657876SArnaldo Carvalho de Melo 458a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 459a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 460a84ffe43SArnaldo Carvalho de Melo 461a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 462a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 463a84ffe43SArnaldo Carvalho de Melo 46467e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 46567e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 466a84ffe43SArnaldo Carvalho de Melo 46767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 468a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 469a84ffe43SArnaldo Carvalho de Melo err = 0; 470a84ffe43SArnaldo Carvalho de Melo 471a84ffe43SArnaldo Carvalho de Melo switch (optname) { 472a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 473a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 474a84ffe43SArnaldo Carvalho de Melo break; 475afe00251SAndrea Bittau 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); 65127258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6527c657876SArnaldo Carvalho de Melo 6537c657876SArnaldo Carvalho de Melo /* 6547c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6557c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6567c657876SArnaldo Carvalho de Melo */ 6577c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 6587c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 6597c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 66027258ee5SArnaldo Carvalho de Melo goto out_release; 6617c657876SArnaldo Carvalho de Melo 6627c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 6637c657876SArnaldo Carvalho de Melo release_sock(sk); 6647c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 6657c657876SArnaldo Carvalho de Melo lock_sock(sk); 6667c657876SArnaldo Carvalho de Melo if (skb == NULL) 6677c657876SArnaldo Carvalho de Melo goto out_release; 6687c657876SArnaldo Carvalho de Melo 6697c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 6707c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 67127258ee5SArnaldo Carvalho de Melo if (rc != 0) 67227258ee5SArnaldo Carvalho de Melo goto out_discard; 6737c657876SArnaldo Carvalho de Melo 67497e5848dSIan McDonald skb_queue_tail(&sk->sk_write_queue, skb); 67597e5848dSIan McDonald dccp_write_xmit(sk,0); 6767c657876SArnaldo Carvalho de Melo out_release: 6777c657876SArnaldo Carvalho de Melo release_sock(sk); 6787c657876SArnaldo Carvalho de Melo return rc ? : len; 67927258ee5SArnaldo Carvalho de Melo out_discard: 68027258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6817c657876SArnaldo Carvalho de Melo goto out_release; 6827c657876SArnaldo Carvalho de Melo } 6837c657876SArnaldo Carvalho de Melo 684f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 685f21e68caSArnaldo Carvalho de Melo 6867c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6877c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6887c657876SArnaldo Carvalho de Melo { 6897c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6907c657876SArnaldo Carvalho de Melo long timeo; 6917c657876SArnaldo Carvalho de Melo 6927c657876SArnaldo Carvalho de Melo lock_sock(sk); 6937c657876SArnaldo Carvalho de Melo 694531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 695531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6967c657876SArnaldo Carvalho de Melo goto out; 697531669a0SArnaldo Carvalho de Melo } 6987c657876SArnaldo Carvalho de Melo 6997c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 7007c657876SArnaldo Carvalho de Melo 7017c657876SArnaldo Carvalho de Melo do { 702531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 7037c657876SArnaldo Carvalho de Melo 704531669a0SArnaldo Carvalho de Melo if (skb == NULL) 705531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 7067c657876SArnaldo Carvalho de Melo 7077c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 7087c657876SArnaldo Carvalho de Melo 7097c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 7107c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 7117c657876SArnaldo Carvalho de Melo goto found_ok_skb; 7127c657876SArnaldo Carvalho de Melo 7137c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 7147c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 7157c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 716531669a0SArnaldo Carvalho de Melo len = 0; 7177c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7187c657876SArnaldo Carvalho de Melo } 7197690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7207690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 721624d1164SChris Leech sk_eat_skb(sk, skb, 0); 722531669a0SArnaldo Carvalho de Melo verify_sock_status: 723531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 724531669a0SArnaldo Carvalho de Melo len = 0; 7257c657876SArnaldo Carvalho de Melo break; 7267c657876SArnaldo Carvalho de Melo } 7277c657876SArnaldo Carvalho de Melo 728531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 729531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7307c657876SArnaldo Carvalho de Melo break; 731531669a0SArnaldo Carvalho de Melo } 732531669a0SArnaldo Carvalho de Melo 733531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 734531669a0SArnaldo Carvalho de Melo len = 0; 735531669a0SArnaldo Carvalho de Melo break; 736531669a0SArnaldo Carvalho de Melo } 7377c657876SArnaldo Carvalho de Melo 7387c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7397c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7407c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7417c657876SArnaldo Carvalho de Melo * from never connected socket. 7427c657876SArnaldo Carvalho de Melo */ 743531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7447c657876SArnaldo Carvalho de Melo break; 7457c657876SArnaldo Carvalho de Melo } 746531669a0SArnaldo Carvalho de Melo len = 0; 7477c657876SArnaldo Carvalho de Melo break; 7487c657876SArnaldo Carvalho de Melo } 7497c657876SArnaldo Carvalho de Melo 7507c657876SArnaldo Carvalho de Melo if (!timeo) { 751531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7527c657876SArnaldo Carvalho de Melo break; 7537c657876SArnaldo Carvalho de Melo } 7547c657876SArnaldo Carvalho de Melo 7557c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 756531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7577c657876SArnaldo Carvalho de Melo break; 7587c657876SArnaldo Carvalho de Melo } 7597c657876SArnaldo Carvalho de Melo 7607c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 7617c657876SArnaldo Carvalho de Melo continue; 7627c657876SArnaldo Carvalho de Melo found_ok_skb: 763531669a0SArnaldo Carvalho de Melo if (len > skb->len) 764531669a0SArnaldo Carvalho de Melo len = skb->len; 765531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 766531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 7677c657876SArnaldo Carvalho de Melo 768531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 7697c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 770531669a0SArnaldo Carvalho de Melo len = -EFAULT; 7717c657876SArnaldo Carvalho de Melo break; 7727c657876SArnaldo Carvalho de Melo } 7737c657876SArnaldo Carvalho de Melo found_fin_ok: 7747c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 775624d1164SChris Leech sk_eat_skb(sk, skb, 0); 7767c657876SArnaldo Carvalho de Melo break; 777531669a0SArnaldo Carvalho de Melo } while (1); 7787c657876SArnaldo Carvalho de Melo out: 7797c657876SArnaldo Carvalho de Melo release_sock(sk); 780531669a0SArnaldo Carvalho de Melo return len; 7817c657876SArnaldo Carvalho de Melo } 7827c657876SArnaldo Carvalho de Melo 783f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 784f21e68caSArnaldo Carvalho de Melo 785f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7867c657876SArnaldo Carvalho de Melo { 7877c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7887c657876SArnaldo Carvalho de Melo unsigned char old_state; 7897c657876SArnaldo Carvalho de Melo int err; 7907c657876SArnaldo Carvalho de Melo 7917c657876SArnaldo Carvalho de Melo lock_sock(sk); 7927c657876SArnaldo Carvalho de Melo 7937c657876SArnaldo Carvalho de Melo err = -EINVAL; 7947c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 7957c657876SArnaldo Carvalho de Melo goto out; 7967c657876SArnaldo Carvalho de Melo 7977c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 7987c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 7997c657876SArnaldo Carvalho de Melo goto out; 8007c657876SArnaldo Carvalho de Melo 8017c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 8027c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 8037c657876SArnaldo Carvalho de Melo */ 8047c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 8057c657876SArnaldo Carvalho de Melo /* 8067c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 8077c657876SArnaldo Carvalho de Melo * see tcp_listen_start 8087c657876SArnaldo Carvalho de Melo */ 80972a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 8107c657876SArnaldo Carvalho de Melo if (err) 8117c657876SArnaldo Carvalho de Melo goto out; 8127c657876SArnaldo Carvalho de Melo } 8137c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 8147c657876SArnaldo Carvalho de Melo err = 0; 8157c657876SArnaldo Carvalho de Melo 8167c657876SArnaldo Carvalho de Melo out: 8177c657876SArnaldo Carvalho de Melo release_sock(sk); 8187c657876SArnaldo Carvalho de Melo return err; 8197c657876SArnaldo Carvalho de Melo } 8207c657876SArnaldo Carvalho de Melo 821f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 822f21e68caSArnaldo Carvalho de Melo 8237c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8247c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8257c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8267c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8277c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8287c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8297c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8307c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8317c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8327c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8337c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8347c657876SArnaldo Carvalho de Melo }; 8357c657876SArnaldo Carvalho de Melo 8367c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8377c657876SArnaldo Carvalho de Melo { 8387c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8397c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8407c657876SArnaldo Carvalho de Melo 8417c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8427c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8437c657876SArnaldo Carvalho de Melo 8447c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8457c657876SArnaldo Carvalho de Melo } 8467c657876SArnaldo Carvalho de Melo 8477c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8487c657876SArnaldo Carvalho de Melo { 84997e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 8507c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 851134af346SHerbert Xu int state; 8527c657876SArnaldo Carvalho de Melo 8537c657876SArnaldo Carvalho de Melo lock_sock(sk); 8547c657876SArnaldo Carvalho de Melo 8557c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8567c657876SArnaldo Carvalho de Melo 8577c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 8587c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8597c657876SArnaldo Carvalho de Melo 8607c657876SArnaldo Carvalho de Melo /* Special case. */ 8617c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 8627c657876SArnaldo Carvalho de Melo 8637c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 8647c657876SArnaldo Carvalho de Melo } 8657c657876SArnaldo Carvalho de Melo 86697e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 86797e5848dSIan McDonald 8687c657876SArnaldo Carvalho de Melo /* 8697c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 8707c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 8717c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 8727c657876SArnaldo Carvalho de Melo */ 8737c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 8747c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 8757c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 8767c657876SArnaldo Carvalho de Melo } 8777c657876SArnaldo Carvalho de Melo 8787c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 8797c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 8807c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 8817c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8827ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8837c657876SArnaldo Carvalho de Melo } 8847c657876SArnaldo Carvalho de Melo 8857c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8867c657876SArnaldo Carvalho de Melo 8877c657876SArnaldo Carvalho de Melo adjudge_to_death: 888134af346SHerbert Xu state = sk->sk_state; 889134af346SHerbert Xu sock_hold(sk); 890134af346SHerbert Xu sock_orphan(sk); 891134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 892134af346SHerbert Xu 8937ad07e7cSArnaldo Carvalho de Melo /* 8947ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 8957ad07e7cSArnaldo Carvalho de Melo */ 8967c657876SArnaldo Carvalho de Melo release_sock(sk); 8977c657876SArnaldo Carvalho de Melo /* 8987c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 8997c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 9007c657876SArnaldo Carvalho de Melo */ 9017c657876SArnaldo Carvalho de Melo local_bh_disable(); 9027c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 9037c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 9047c657876SArnaldo Carvalho de Melo 905134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 906134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 907134af346SHerbert Xu goto out; 9087c657876SArnaldo Carvalho de Melo 9097ad07e7cSArnaldo Carvalho de Melo /* 9107ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 9117ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 9127ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 9137ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 9147ad07e7cSArnaldo Carvalho de Melo */ 9157ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 9167ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 9177ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9187ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9197ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9207ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9217ad07e7cSArnaldo Carvalho de Melo #if 0 9227ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9237c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9247ad07e7cSArnaldo Carvalho de Melo #endif 9257ad07e7cSArnaldo Carvalho de Melo } 9267c657876SArnaldo Carvalho de Melo 9277c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9287c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9297c657876SArnaldo Carvalho de Melo 9307c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9317c657876SArnaldo Carvalho de Melo 932134af346SHerbert Xu out: 9337c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9347c657876SArnaldo Carvalho de Melo local_bh_enable(); 9357c657876SArnaldo Carvalho de Melo sock_put(sk); 9367c657876SArnaldo Carvalho de Melo } 9377c657876SArnaldo Carvalho de Melo 938f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 939f21e68caSArnaldo Carvalho de Melo 9407c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9417c657876SArnaldo Carvalho de Melo { 9427c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9437c657876SArnaldo Carvalho de Melo } 9447c657876SArnaldo Carvalho de Melo 945f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 946f21e68caSArnaldo Carvalho de Melo 94746f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9487c657876SArnaldo Carvalho de Melo { 9497c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9507c657876SArnaldo Carvalho de Melo 9517c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9527c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9537c657876SArnaldo Carvalho de Melo goto out; 9547c657876SArnaldo Carvalho de Melo 9557c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9567c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9577c657876SArnaldo Carvalho de Melo goto out_free_one; 9587c657876SArnaldo Carvalho de Melo 9597c657876SArnaldo Carvalho de Melo rc = 0; 9607c657876SArnaldo Carvalho de Melo out: 9617c657876SArnaldo Carvalho de Melo return rc; 9627c657876SArnaldo Carvalho de Melo out_free_one: 9637c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9647c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9657c657876SArnaldo Carvalho de Melo goto out; 9667c657876SArnaldo Carvalho de Melo 9677c657876SArnaldo Carvalho de Melo } 9687c657876SArnaldo Carvalho de Melo 969b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 97046f09ffaSArnaldo Carvalho de Melo { 97146f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 97246f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 97346f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 97446f09ffaSArnaldo Carvalho de Melo } 97546f09ffaSArnaldo Carvalho de Melo 9767c657876SArnaldo Carvalho de Melo static int thash_entries; 9777c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9787c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9797c657876SArnaldo Carvalho de Melo 980a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9817c657876SArnaldo Carvalho de Melo int dccp_debug; 9827c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9837c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 984f21e68caSArnaldo Carvalho de Melo 985f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 986a1d3a355SArnaldo Carvalho de Melo #endif 9877c657876SArnaldo Carvalho de Melo 9887c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9897c657876SArnaldo Carvalho de Melo { 9907c657876SArnaldo Carvalho de Melo unsigned long goal; 9917c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 992b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 9937c657876SArnaldo Carvalho de Melo 9947690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 9957690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 9967690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 9977690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 9987c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 999b61fafc4SArnaldo Carvalho de Melo goto out; 10007c657876SArnaldo Carvalho de Melo 10017c657876SArnaldo Carvalho de Melo /* 10027c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 10037c657876SArnaldo Carvalho de Melo * hash tables. 10047c657876SArnaldo Carvalho de Melo * 10057c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 10067c657876SArnaldo Carvalho de Melo */ 10077c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 10087c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10097c657876SArnaldo Carvalho de Melo else 10107c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10117c657876SArnaldo Carvalho de Melo 10127c657876SArnaldo Carvalho de Melo if (thash_entries) 10137690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10147690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10157c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10167c657876SArnaldo Carvalho de Melo ; 10177c657876SArnaldo Carvalho de Melo do { 10187c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10197c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10207c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 10217690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10227690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10237c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10247c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10257c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10267c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10277c657876SArnaldo Carvalho de Melo 10287c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 10297c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 10307c657876SArnaldo Carvalho de Melo "established hash table\n"); 10317c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10327c657876SArnaldo Carvalho de Melo } 10337c657876SArnaldo Carvalho de Melo 10347c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 10357c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10367c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 10377c657876SArnaldo Carvalho de Melo } 10387c657876SArnaldo Carvalho de Melo 10397c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10407c657876SArnaldo Carvalho de Melo 10417c657876SArnaldo Carvalho de Melo do { 10427c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10437c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10447690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10457690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10467c657876SArnaldo Carvalho de Melo continue; 10477c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10487c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10497c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10507c657876SArnaldo Carvalho de Melo 10517c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 10527c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 10537c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10547c657876SArnaldo Carvalho de Melo } 10557c657876SArnaldo Carvalho de Melo 10567c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10577c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10587c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10597c657876SArnaldo Carvalho de Melo } 10607c657876SArnaldo Carvalho de Melo 106146f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1062fa23e2ecSArnaldo Carvalho de Melo if (rc) 10637c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10647c657876SArnaldo Carvalho de Melo 10659b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10667c657876SArnaldo Carvalho de Melo if (rc) 1067b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 10689b07ef5dSArnaldo Carvalho de Melo 1069e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10709b07ef5dSArnaldo Carvalho de Melo if (rc) 10719b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 10727c657876SArnaldo Carvalho de Melo out: 10737c657876SArnaldo Carvalho de Melo return rc; 10749b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10759b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1076b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 107746f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10787c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10797c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10807c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10817c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10827c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10837c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10847c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10857c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10867c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10877c657876SArnaldo Carvalho de Melo goto out; 10887c657876SArnaldo Carvalho de Melo } 10897c657876SArnaldo Carvalho de Melo 10907c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 10917c657876SArnaldo Carvalho de Melo { 109246f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1093725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1094725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1095725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1096725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1097725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1098725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 10997c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 11009b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1101e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 11027c657876SArnaldo Carvalho de Melo } 11037c657876SArnaldo Carvalho de Melo 11047c657876SArnaldo Carvalho de Melo module_init(dccp_init); 11057c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 11067c657876SArnaldo Carvalho de Melo 11077c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 11087c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11097c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1110