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/config.h> 137c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 147c657876SArnaldo Carvalho de Melo #include <linux/module.h> 157c657876SArnaldo Carvalho de Melo #include <linux/types.h> 167c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 177c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 187c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 197c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 207c657876SArnaldo Carvalho de Melo #include <linux/in.h> 217c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 227c657876SArnaldo Carvalho de Melo #include <linux/init.h> 237c657876SArnaldo Carvalho de Melo #include <linux/random.h> 247c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 257c657876SArnaldo Carvalho de Melo 2614c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 277c657876SArnaldo Carvalho de Melo #include <net/sock.h> 287c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 297c657876SArnaldo Carvalho de Melo 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 56c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 57c25a18baSArnaldo Carvalho de Melo { 58c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 59c25a18baSArnaldo Carvalho de Melo 60c25a18baSArnaldo Carvalho de Melo dccp_pr_debug("%s(%p) %-10.10s -> %s\n", 61c25a18baSArnaldo Carvalho de Melo dccp_role(sk), sk, 62c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 63c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 64c25a18baSArnaldo Carvalho de Melo 65c25a18baSArnaldo Carvalho de Melo switch (state) { 66c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 67c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 68c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 69c25a18baSArnaldo Carvalho de Melo break; 70c25a18baSArnaldo Carvalho de Melo 71c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 72c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) 73c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 74c25a18baSArnaldo Carvalho de Melo 75c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 76c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 77c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 78c25a18baSArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 79c25a18baSArnaldo Carvalho de Melo /* fall through */ 80c25a18baSArnaldo Carvalho de Melo default: 81c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 82c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 83c25a18baSArnaldo Carvalho de Melo } 84c25a18baSArnaldo Carvalho de Melo 85c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 86c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 87c25a18baSArnaldo Carvalho de Melo */ 88c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 89c25a18baSArnaldo Carvalho de Melo } 90c25a18baSArnaldo Carvalho de Melo 91c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 92c25a18baSArnaldo Carvalho de Melo 93c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 94c25a18baSArnaldo Carvalho de Melo { 95c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 96c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 97c25a18baSArnaldo Carvalho de Melo 98c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 99c25a18baSArnaldo Carvalho de Melo 100c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 101c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 102c25a18baSArnaldo Carvalho de Melo else 103c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 104c25a18baSArnaldo Carvalho de Melo } 105c25a18baSArnaldo Carvalho de Melo 106c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 107c25a18baSArnaldo Carvalho de Melo 1087c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1097c657876SArnaldo Carvalho de Melo { 1107c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1117c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1127c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1137c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1147c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1157c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1167c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1177c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1187c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1197c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1207c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1217c657876SArnaldo Carvalho de Melo }; 1227c657876SArnaldo Carvalho de Melo 1237c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1247c657876SArnaldo Carvalho de Melo return "INVALID"; 1257c657876SArnaldo Carvalho de Melo else 1267c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1277c657876SArnaldo Carvalho de Melo } 1287c657876SArnaldo Carvalho de Melo 1297c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1307c657876SArnaldo Carvalho de Melo 1317c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1327c657876SArnaldo Carvalho de Melo { 1337c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1347c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1357c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1367c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1377c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1387c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1397c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 1407c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1417c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1427c657876SArnaldo Carvalho de Melo }; 1437c657876SArnaldo Carvalho de Melo 1447c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1457c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1467c657876SArnaldo Carvalho de Melo else 1477c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1487c657876SArnaldo Carvalho de Melo } 1497c657876SArnaldo Carvalho de Melo 1507c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1517c657876SArnaldo Carvalho de Melo 152c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 153c985ed70SArnaldo Carvalho de Melo { 154c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 155c985ed70SArnaldo Carvalho de Melo } 156c985ed70SArnaldo Carvalho de Melo 157c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 158c985ed70SArnaldo Carvalho de Melo 159c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 160c985ed70SArnaldo Carvalho de Melo { 161c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 162c985ed70SArnaldo Carvalho de Melo } 163c985ed70SArnaldo Carvalho de Melo 164c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 165c985ed70SArnaldo Carvalho de Melo 16672478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1673e0fadc5SArnaldo Carvalho de Melo { 1683e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 169a4bf3902SArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 1703e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1713e0fadc5SArnaldo Carvalho de Melo 172a4bf3902SArnaldo Carvalho de Melo dccp_minisock_init(&dp->dccps_minisock); 1733e0fadc5SArnaldo Carvalho de Melo do_gettimeofday(&dp->dccps_epoch); 1743e0fadc5SArnaldo Carvalho de Melo 1753e0fadc5SArnaldo Carvalho de Melo /* 1763e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1773e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1783e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1793e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1803e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1813e0fadc5SArnaldo Carvalho de Melo */ 18272478873SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized)) { 1838ca0d17bSArnaldo Carvalho de Melo int rc = dccp_feat_init(dmsk); 1843e0fadc5SArnaldo Carvalho de Melo 1853e0fadc5SArnaldo Carvalho de Melo if (rc) 1863e0fadc5SArnaldo Carvalho de Melo return rc; 1873e0fadc5SArnaldo Carvalho de Melo 188a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 1893e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1903e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1913e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1923e0fadc5SArnaldo Carvalho de Melo } 193a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid, 1943e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 195a4bf3902SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid, 1963e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1973e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1983e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 1993e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2003e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 201a4bf3902SArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2023e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2033e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2043e0fadc5SArnaldo Carvalho de Melo } 2053e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2063e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 2073e0fadc5SArnaldo Carvalho de Melo } 2083e0fadc5SArnaldo Carvalho de Melo } else { 2093e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 210a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_pending); 211a4bf3902SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dmsk->dccpms_conf); 2123e0fadc5SArnaldo Carvalho de Melo } 2133e0fadc5SArnaldo Carvalho de Melo 2143e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 2153e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 2163e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 2173e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 2183e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 2193e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 2203e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 2213e0fadc5SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_INVALID_VALUE; 2223e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 2233e0fadc5SArnaldo Carvalho de Melo 2243e0fadc5SArnaldo Carvalho de Melo return 0; 2253e0fadc5SArnaldo Carvalho de Melo } 2263e0fadc5SArnaldo Carvalho de Melo 2273e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2283e0fadc5SArnaldo Carvalho de Melo 2293e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 2303e0fadc5SArnaldo Carvalho de Melo { 2313e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2328ca0d17bSArnaldo Carvalho de Melo struct dccp_minisock *dmsk = dccp_msk(sk); 2333e0fadc5SArnaldo Carvalho de Melo 2343e0fadc5SArnaldo Carvalho de Melo /* 2353e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2363e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2373e0fadc5SArnaldo Carvalho de Melo */ 2383e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2393e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2403e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2413e0fadc5SArnaldo Carvalho de Melo } 2423e0fadc5SArnaldo Carvalho de Melo 2433e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2443e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2453e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2463e0fadc5SArnaldo Carvalho de Melo 2473e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2483e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2493e0fadc5SArnaldo Carvalho de Melo 2508ca0d17bSArnaldo Carvalho de Melo if (dmsk->dccpms_send_ack_vector) { 2513e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2523e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2533e0fadc5SArnaldo Carvalho de Melo } 2543e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2553e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2563e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2573e0fadc5SArnaldo Carvalho de Melo 2583e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2598ca0d17bSArnaldo Carvalho de Melo dccp_feat_clean(dmsk); 2603e0fadc5SArnaldo Carvalho de Melo 2613e0fadc5SArnaldo Carvalho de Melo return 0; 2623e0fadc5SArnaldo Carvalho de Melo } 2633e0fadc5SArnaldo Carvalho de Melo 2643e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2653e0fadc5SArnaldo Carvalho de Melo 2667c657876SArnaldo Carvalho de Melo static inline int dccp_listen_start(struct sock *sk) 2677c657876SArnaldo Carvalho de Melo { 26867e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 26967e6b629SArnaldo Carvalho de Melo 27067e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 27167e6b629SArnaldo Carvalho de Melo /* 27267e6b629SArnaldo Carvalho de Melo * Apps need to use setsockopt(DCCP_SOCKOPT_SERVICE) 27367e6b629SArnaldo Carvalho de Melo * before calling listen() 27467e6b629SArnaldo Carvalho de Melo */ 27567e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 27667e6b629SArnaldo Carvalho de Melo return -EPROTO; 2777c657876SArnaldo Carvalho de Melo return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE); 2787c657876SArnaldo Carvalho de Melo } 2797c657876SArnaldo Carvalho de Melo 2807c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2817c657876SArnaldo Carvalho de Melo { 2827c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2837c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2847c657876SArnaldo Carvalho de Melo int err = 0; 2857c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2867c657876SArnaldo Carvalho de Melo 2877c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2887c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2897c657876SArnaldo Carvalho de Melo 2907c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2917c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2927c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2937c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2947c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2957c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2967c657876SArnaldo Carvalho de Melo 2977c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2987c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2997c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 3007c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 3017c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 3027c657876SArnaldo Carvalho de Melo } 3037c657876SArnaldo Carvalho de Melo 3047c657876SArnaldo Carvalho de Melo inet->dport = 0; 3057c657876SArnaldo Carvalho de Melo 3067c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 3077c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 3087c657876SArnaldo Carvalho de Melo 3097c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3107c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3117c657876SArnaldo Carvalho de Melo 3127c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3137c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3147c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3157c657876SArnaldo Carvalho de Melo 3167c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 3177c657876SArnaldo Carvalho de Melo 3187c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3197c657876SArnaldo Carvalho de Melo return err; 3207c657876SArnaldo Carvalho de Melo } 3217c657876SArnaldo Carvalho de Melo 322f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 323f21e68caSArnaldo Carvalho de Melo 324331968bdSArnaldo Carvalho de Melo /* 325331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 326331968bdSArnaldo Carvalho de Melo * 327331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 328331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 329331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 330331968bdSArnaldo Carvalho de Melo */ 331f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 332331968bdSArnaldo Carvalho de Melo poll_table *wait) 333331968bdSArnaldo Carvalho de Melo { 334331968bdSArnaldo Carvalho de Melo unsigned int mask; 335331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 336331968bdSArnaldo Carvalho de Melo 337331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 338331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 339331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 340331968bdSArnaldo Carvalho de Melo 341331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 342331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 343331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 344331968bdSArnaldo Carvalho de Melo */ 345331968bdSArnaldo Carvalho de Melo 346331968bdSArnaldo Carvalho de Melo mask = 0; 347331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 348331968bdSArnaldo Carvalho de Melo mask = POLLERR; 349331968bdSArnaldo Carvalho de Melo 350331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 351331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 352331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 353331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 354331968bdSArnaldo Carvalho de Melo 355331968bdSArnaldo Carvalho de Melo /* Connected? */ 356331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 357331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 358331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 359331968bdSArnaldo Carvalho de Melo 360331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 361331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 362331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 363331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 364331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 365331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 366331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 367331968bdSArnaldo Carvalho de Melo 368331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 369331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 370331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 371331968bdSArnaldo Carvalho de Melo */ 372331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 373331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 374331968bdSArnaldo Carvalho de Melo } 375331968bdSArnaldo Carvalho de Melo } 376331968bdSArnaldo Carvalho de Melo } 377331968bdSArnaldo Carvalho de Melo return mask; 378331968bdSArnaldo Carvalho de Melo } 379331968bdSArnaldo Carvalho de Melo 380f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 381f21e68caSArnaldo Carvalho de Melo 3827c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3837c657876SArnaldo Carvalho de Melo { 3847c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3857c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3867c657876SArnaldo Carvalho de Melo } 3877c657876SArnaldo Carvalho de Melo 388f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 389f21e68caSArnaldo Carvalho de Melo 39060fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 39167e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 39267e6b629SArnaldo Carvalho de Melo { 39367e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 39467e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 39567e6b629SArnaldo Carvalho de Melo 39667e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 39767e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 39867e6b629SArnaldo Carvalho de Melo return -EINVAL; 39967e6b629SArnaldo Carvalho de Melo 40067e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 40167e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 40267e6b629SArnaldo Carvalho de Melo if (sl == NULL) 40367e6b629SArnaldo Carvalho de Melo return -ENOMEM; 40467e6b629SArnaldo Carvalho de Melo 40567e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 40667e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 40767e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 40867e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 40967e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 41067e6b629SArnaldo Carvalho de Melo kfree(sl); 41167e6b629SArnaldo Carvalho de Melo return -EFAULT; 41267e6b629SArnaldo Carvalho de Melo } 41367e6b629SArnaldo Carvalho de Melo } 41467e6b629SArnaldo Carvalho de Melo 41567e6b629SArnaldo Carvalho de Melo lock_sock(sk); 41667e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 41767e6b629SArnaldo Carvalho de Melo 41867e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 41967e6b629SArnaldo Carvalho de Melo 42067e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 42167e6b629SArnaldo Carvalho de Melo release_sock(sk); 42267e6b629SArnaldo Carvalho de Melo return 0; 42367e6b629SArnaldo Carvalho de Melo } 42467e6b629SArnaldo Carvalho de Melo 425afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 426afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 427afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 428afe00251SAndrea Bittau { 429afe00251SAndrea Bittau struct dccp_so_feat opt; 430afe00251SAndrea Bittau u8 *val; 431afe00251SAndrea Bittau int rc; 432afe00251SAndrea Bittau 433afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 434afe00251SAndrea Bittau return -EFAULT; 435afe00251SAndrea Bittau 436afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 437afe00251SAndrea Bittau if (!val) 438afe00251SAndrea Bittau return -ENOMEM; 439afe00251SAndrea Bittau 440afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 441afe00251SAndrea Bittau rc = -EFAULT; 442afe00251SAndrea Bittau goto out_free_val; 443afe00251SAndrea Bittau } 444afe00251SAndrea Bittau 4458ca0d17bSArnaldo Carvalho de Melo rc = dccp_feat_change(dccp_msk(sk), type, opt.dccpsf_feat, 4468ca0d17bSArnaldo Carvalho de Melo val, opt.dccpsf_len, GFP_KERNEL); 447afe00251SAndrea Bittau if (rc) 448afe00251SAndrea Bittau goto out_free_val; 449afe00251SAndrea Bittau 450afe00251SAndrea Bittau out: 451afe00251SAndrea Bittau return rc; 452afe00251SAndrea Bittau 453afe00251SAndrea Bittau out_free_val: 454afe00251SAndrea Bittau kfree(val); 455afe00251SAndrea Bittau goto out; 456afe00251SAndrea Bittau } 457afe00251SAndrea Bittau 4583fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 459a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4607c657876SArnaldo Carvalho de Melo { 461a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 462a84ffe43SArnaldo Carvalho de Melo int err; 463a84ffe43SArnaldo Carvalho de Melo int val; 4647c657876SArnaldo Carvalho de Melo 465a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 466a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 467a84ffe43SArnaldo Carvalho de Melo 468a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 469a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 470a84ffe43SArnaldo Carvalho de Melo 47167e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 47267e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 473a84ffe43SArnaldo Carvalho de Melo 47467e6b629SArnaldo Carvalho de Melo lock_sock(sk); 475a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 476a84ffe43SArnaldo Carvalho de Melo err = 0; 477a84ffe43SArnaldo Carvalho de Melo 478a84ffe43SArnaldo Carvalho de Melo switch (optname) { 479a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 480a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 481a84ffe43SArnaldo Carvalho de Melo break; 482afe00251SAndrea Bittau 483afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 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_L, 488afe00251SAndrea Bittau (struct dccp_so_feat *) 489afe00251SAndrea Bittau optval); 490afe00251SAndrea Bittau break; 491afe00251SAndrea Bittau 492afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 493afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 494afe00251SAndrea Bittau err = -EINVAL; 495afe00251SAndrea Bittau else 496afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 497afe00251SAndrea Bittau (struct dccp_so_feat *) 498afe00251SAndrea Bittau optval); 499afe00251SAndrea Bittau break; 500afe00251SAndrea Bittau 501a84ffe43SArnaldo Carvalho de Melo default: 502a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 503a84ffe43SArnaldo Carvalho de Melo break; 504a84ffe43SArnaldo Carvalho de Melo } 505a84ffe43SArnaldo Carvalho de Melo 506a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 507a84ffe43SArnaldo Carvalho de Melo return err; 5087c657876SArnaldo Carvalho de Melo } 5097c657876SArnaldo Carvalho de Melo 5103fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5113fdadf7dSDmitry Mishin char __user *optval, int optlen) 5123fdadf7dSDmitry Mishin { 5133fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5143fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5153fdadf7dSDmitry Mishin optname, optval, 5163fdadf7dSDmitry Mishin optlen); 5173fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5183fdadf7dSDmitry Mishin } 519543d9cfeSArnaldo Carvalho de Melo 520f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 521f21e68caSArnaldo Carvalho de Melo 5223fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5233fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5243fdadf7dSDmitry Mishin char __user *optval, int optlen) 5253fdadf7dSDmitry Mishin { 526dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 527dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 528dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5293fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5303fdadf7dSDmitry Mishin } 531543d9cfeSArnaldo Carvalho de Melo 5323fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5333fdadf7dSDmitry Mishin #endif 5343fdadf7dSDmitry Mishin 53567e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 53660fe62e7SAndrea Bittau __be32 __user *optval, 53767e6b629SArnaldo Carvalho de Melo int __user *optlen) 53867e6b629SArnaldo Carvalho de Melo { 53967e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 54067e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 54167e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 54267e6b629SArnaldo Carvalho de Melo 54367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 54467e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 54567e6b629SArnaldo Carvalho de Melo goto out; 54667e6b629SArnaldo Carvalho de Melo 54767e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 54867e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 54967e6b629SArnaldo Carvalho de Melo total_len += slen; 55067e6b629SArnaldo Carvalho de Melo } 55167e6b629SArnaldo Carvalho de Melo 55267e6b629SArnaldo Carvalho de Melo err = -EINVAL; 55367e6b629SArnaldo Carvalho de Melo if (total_len > len) 55467e6b629SArnaldo Carvalho de Melo goto out; 55567e6b629SArnaldo Carvalho de Melo 55667e6b629SArnaldo Carvalho de Melo err = 0; 55767e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 55867e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 55967e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 56067e6b629SArnaldo Carvalho de Melo err = -EFAULT; 56167e6b629SArnaldo Carvalho de Melo out: 56267e6b629SArnaldo Carvalho de Melo release_sock(sk); 56367e6b629SArnaldo Carvalho de Melo return err; 56467e6b629SArnaldo Carvalho de Melo } 56567e6b629SArnaldo Carvalho de Melo 5663fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 567a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5687c657876SArnaldo Carvalho de Melo { 569a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 570a84ffe43SArnaldo Carvalho de Melo int val, len; 5717c657876SArnaldo Carvalho de Melo 572a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 573a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 574a84ffe43SArnaldo Carvalho de Melo 57588f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 576a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 577a84ffe43SArnaldo Carvalho de Melo 578a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 579a84ffe43SArnaldo Carvalho de Melo 580a84ffe43SArnaldo Carvalho de Melo switch (optname) { 581a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 582a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 58388f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 584a84ffe43SArnaldo Carvalho de Melo break; 58588f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 58688f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 58760fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 58888f964dbSArnaldo Carvalho de Melo case 128 ... 191: 58988f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 59088f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 59188f964dbSArnaldo Carvalho de Melo case 192 ... 255: 59288f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 59388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 594a84ffe43SArnaldo Carvalho de Melo default: 595a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 596a84ffe43SArnaldo Carvalho de Melo } 597a84ffe43SArnaldo Carvalho de Melo 598a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 599a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 600a84ffe43SArnaldo Carvalho de Melo 601a84ffe43SArnaldo Carvalho de Melo return 0; 6027c657876SArnaldo Carvalho de Melo } 6037c657876SArnaldo Carvalho de Melo 6043fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6053fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6063fdadf7dSDmitry Mishin { 6073fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6083fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6093fdadf7dSDmitry Mishin optname, optval, 6103fdadf7dSDmitry Mishin optlen); 6113fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6123fdadf7dSDmitry Mishin } 613543d9cfeSArnaldo Carvalho de Melo 614f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 615f21e68caSArnaldo Carvalho de Melo 6163fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6173fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6183fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6193fdadf7dSDmitry Mishin { 620dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 621dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 622dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6233fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6243fdadf7dSDmitry Mishin } 625543d9cfeSArnaldo Carvalho de Melo 6263fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6273fdadf7dSDmitry Mishin #endif 6283fdadf7dSDmitry Mishin 6297c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6307c657876SArnaldo Carvalho de Melo size_t len) 6317c657876SArnaldo Carvalho de Melo { 6327c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 6337c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 6347c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 6357c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 6367c657876SArnaldo Carvalho de Melo int rc, size; 6377c657876SArnaldo Carvalho de Melo long timeo; 6387c657876SArnaldo Carvalho de Melo 6397c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 6407c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 6417c657876SArnaldo Carvalho de Melo 6427c657876SArnaldo Carvalho de Melo lock_sock(sk); 64327258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 6447c657876SArnaldo Carvalho de Melo 6457c657876SArnaldo Carvalho de Melo /* 6467c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 6477c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 6487c657876SArnaldo Carvalho de Melo */ 6497c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 6507c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 6517c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 65227258ee5SArnaldo Carvalho de Melo goto out_release; 6537c657876SArnaldo Carvalho de Melo 6547c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 6557c657876SArnaldo Carvalho de Melo release_sock(sk); 6567c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 6577c657876SArnaldo Carvalho de Melo lock_sock(sk); 6587c657876SArnaldo Carvalho de Melo if (skb == NULL) 6597c657876SArnaldo Carvalho de Melo goto out_release; 6607c657876SArnaldo Carvalho de Melo 6617c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 6627c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 66327258ee5SArnaldo Carvalho de Melo if (rc != 0) 66427258ee5SArnaldo Carvalho de Melo goto out_discard; 6657c657876SArnaldo Carvalho de Melo 666d6809c12SArnaldo Carvalho de Melo rc = dccp_write_xmit(sk, skb, &timeo); 66720472af9SArnaldo Carvalho de Melo /* 66820472af9SArnaldo Carvalho de Melo * XXX we don't use sk_write_queue, so just discard the packet. 66920472af9SArnaldo Carvalho de Melo * Current plan however is to _use_ sk_write_queue with 67020472af9SArnaldo Carvalho de Melo * an algorith similar to tcp_sendmsg, where the main difference 67120472af9SArnaldo Carvalho de Melo * is that in DCCP we have to respect packet boundaries, so 67220472af9SArnaldo Carvalho de Melo * no coalescing of skbs. 67320472af9SArnaldo Carvalho de Melo * 67420472af9SArnaldo Carvalho de Melo * This bug was _quickly_ found & fixed by just looking at an OSTRA 67520472af9SArnaldo Carvalho de Melo * generated callgraph 8) -acme 67620472af9SArnaldo Carvalho de Melo */ 6777c657876SArnaldo Carvalho de Melo out_release: 6787c657876SArnaldo Carvalho de Melo release_sock(sk); 6797c657876SArnaldo Carvalho de Melo return rc ? : len; 68027258ee5SArnaldo Carvalho de Melo out_discard: 68127258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6827c657876SArnaldo Carvalho de Melo goto out_release; 6837c657876SArnaldo Carvalho de Melo } 6847c657876SArnaldo Carvalho de Melo 685f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 686f21e68caSArnaldo Carvalho de Melo 6877c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6887c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6897c657876SArnaldo Carvalho de Melo { 6907c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6917c657876SArnaldo Carvalho de Melo long timeo; 6927c657876SArnaldo Carvalho de Melo 6937c657876SArnaldo Carvalho de Melo lock_sock(sk); 6947c657876SArnaldo Carvalho de Melo 695531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 696531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6977c657876SArnaldo Carvalho de Melo goto out; 698531669a0SArnaldo Carvalho de Melo } 6997c657876SArnaldo Carvalho de Melo 7007c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 7017c657876SArnaldo Carvalho de Melo 7027c657876SArnaldo Carvalho de Melo do { 703531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 7047c657876SArnaldo Carvalho de Melo 705531669a0SArnaldo Carvalho de Melo if (skb == NULL) 706531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 7077c657876SArnaldo Carvalho de Melo 7087c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 7097c657876SArnaldo Carvalho de Melo 7107c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 7117c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 7127c657876SArnaldo Carvalho de Melo goto found_ok_skb; 7137c657876SArnaldo Carvalho de Melo 7147c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 7157c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 7167c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 717531669a0SArnaldo Carvalho de Melo len = 0; 7187c657876SArnaldo Carvalho de Melo goto found_fin_ok; 7197c657876SArnaldo Carvalho de Melo } 7207690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 7217690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 722531669a0SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 723531669a0SArnaldo Carvalho de Melo verify_sock_status: 724531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 725531669a0SArnaldo Carvalho de Melo len = 0; 7267c657876SArnaldo Carvalho de Melo break; 7277c657876SArnaldo Carvalho de Melo } 7287c657876SArnaldo Carvalho de Melo 729531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 730531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 7317c657876SArnaldo Carvalho de Melo break; 732531669a0SArnaldo Carvalho de Melo } 733531669a0SArnaldo Carvalho de Melo 734531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 735531669a0SArnaldo Carvalho de Melo len = 0; 736531669a0SArnaldo Carvalho de Melo break; 737531669a0SArnaldo Carvalho de Melo } 7387c657876SArnaldo Carvalho de Melo 7397c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 7407c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 7417c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 7427c657876SArnaldo Carvalho de Melo * from never connected socket. 7437c657876SArnaldo Carvalho de Melo */ 744531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 7457c657876SArnaldo Carvalho de Melo break; 7467c657876SArnaldo Carvalho de Melo } 747531669a0SArnaldo Carvalho de Melo len = 0; 7487c657876SArnaldo Carvalho de Melo break; 7497c657876SArnaldo Carvalho de Melo } 7507c657876SArnaldo Carvalho de Melo 7517c657876SArnaldo Carvalho de Melo if (!timeo) { 752531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 7537c657876SArnaldo Carvalho de Melo break; 7547c657876SArnaldo Carvalho de Melo } 7557c657876SArnaldo Carvalho de Melo 7567c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 757531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 7587c657876SArnaldo Carvalho de Melo break; 7597c657876SArnaldo Carvalho de Melo } 7607c657876SArnaldo Carvalho de Melo 7617c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 7627c657876SArnaldo Carvalho de Melo continue; 7637c657876SArnaldo Carvalho de Melo found_ok_skb: 764531669a0SArnaldo Carvalho de Melo if (len > skb->len) 765531669a0SArnaldo Carvalho de Melo len = skb->len; 766531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 767531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 7687c657876SArnaldo Carvalho de Melo 769531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 7707c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 771531669a0SArnaldo Carvalho de Melo len = -EFAULT; 7727c657876SArnaldo Carvalho de Melo break; 7737c657876SArnaldo Carvalho de Melo } 7747c657876SArnaldo Carvalho de Melo found_fin_ok: 7757c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 7767c657876SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 7777c657876SArnaldo Carvalho de Melo break; 778531669a0SArnaldo Carvalho de Melo } while (1); 7797c657876SArnaldo Carvalho de Melo out: 7807c657876SArnaldo Carvalho de Melo release_sock(sk); 781531669a0SArnaldo Carvalho de Melo return len; 7827c657876SArnaldo Carvalho de Melo } 7837c657876SArnaldo Carvalho de Melo 784f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 785f21e68caSArnaldo Carvalho de Melo 786f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7877c657876SArnaldo Carvalho de Melo { 7887c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7897c657876SArnaldo Carvalho de Melo unsigned char old_state; 7907c657876SArnaldo Carvalho de Melo int err; 7917c657876SArnaldo Carvalho de Melo 7927c657876SArnaldo Carvalho de Melo lock_sock(sk); 7937c657876SArnaldo Carvalho de Melo 7947c657876SArnaldo Carvalho de Melo err = -EINVAL; 7957c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 7967c657876SArnaldo Carvalho de Melo goto out; 7977c657876SArnaldo Carvalho de Melo 7987c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 7997c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 8007c657876SArnaldo Carvalho de Melo goto out; 8017c657876SArnaldo Carvalho de Melo 8027c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 8037c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 8047c657876SArnaldo Carvalho de Melo */ 8057c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 8067c657876SArnaldo Carvalho de Melo /* 8077c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 8087c657876SArnaldo Carvalho de Melo * see tcp_listen_start 8097c657876SArnaldo Carvalho de Melo */ 8107c657876SArnaldo Carvalho de Melo err = dccp_listen_start(sk); 8117c657876SArnaldo Carvalho de Melo if (err) 8127c657876SArnaldo Carvalho de Melo goto out; 8137c657876SArnaldo Carvalho de Melo } 8147c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 8157c657876SArnaldo Carvalho de Melo err = 0; 8167c657876SArnaldo Carvalho de Melo 8177c657876SArnaldo Carvalho de Melo out: 8187c657876SArnaldo Carvalho de Melo release_sock(sk); 8197c657876SArnaldo Carvalho de Melo return err; 8207c657876SArnaldo Carvalho de Melo } 8217c657876SArnaldo Carvalho de Melo 822f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 823f21e68caSArnaldo Carvalho de Melo 8247c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 8257c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 8267c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 8277c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8287c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 8297c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 8307c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 8317c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 8327c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 8337c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 8347c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 8357c657876SArnaldo Carvalho de Melo }; 8367c657876SArnaldo Carvalho de Melo 8377c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 8387c657876SArnaldo Carvalho de Melo { 8397c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 8407c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 8417c657876SArnaldo Carvalho de Melo 8427c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 8437c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 8447c657876SArnaldo Carvalho de Melo 8457c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 8467c657876SArnaldo Carvalho de Melo } 8477c657876SArnaldo Carvalho de Melo 8487c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 8497c657876SArnaldo Carvalho de Melo { 8507c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 8517c657876SArnaldo Carvalho de Melo 8527c657876SArnaldo Carvalho de Melo lock_sock(sk); 8537c657876SArnaldo Carvalho de Melo 8547c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 8557c657876SArnaldo Carvalho de Melo 8567c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 8577c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8587c657876SArnaldo Carvalho de Melo 8597c657876SArnaldo Carvalho de Melo /* Special case. */ 8607c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 8617c657876SArnaldo Carvalho de Melo 8627c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 8637c657876SArnaldo Carvalho de Melo } 8647c657876SArnaldo Carvalho de Melo 8657c657876SArnaldo Carvalho de Melo /* 8667c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 8677c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 8687c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 8697c657876SArnaldo Carvalho de Melo */ 8707c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 8717c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 8727c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 8737c657876SArnaldo Carvalho de Melo } 8747c657876SArnaldo Carvalho de Melo 8757c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 8767c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 8777c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 8787c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8797ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8807c657876SArnaldo Carvalho de Melo } 8817c657876SArnaldo Carvalho de Melo 8827c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8837c657876SArnaldo Carvalho de Melo 8847c657876SArnaldo Carvalho de Melo adjudge_to_death: 8857ad07e7cSArnaldo Carvalho de Melo /* 8867ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 8877ad07e7cSArnaldo Carvalho de Melo */ 8887c657876SArnaldo Carvalho de Melo release_sock(sk); 8897c657876SArnaldo Carvalho de Melo /* 8907c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 8917c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 8927c657876SArnaldo Carvalho de Melo */ 8937c657876SArnaldo Carvalho de Melo local_bh_disable(); 8947c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 8957c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 8967c657876SArnaldo Carvalho de Melo 8977c657876SArnaldo Carvalho de Melo sock_hold(sk); 8987c657876SArnaldo Carvalho de Melo sock_orphan(sk); 8997c657876SArnaldo Carvalho de Melo 9007ad07e7cSArnaldo Carvalho de Melo /* 9017ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 9027ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 9037ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 9047ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 9057ad07e7cSArnaldo Carvalho de Melo */ 9067ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 9077ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 9087ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 9097ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 9107ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 9117ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 9127ad07e7cSArnaldo Carvalho de Melo #if 0 9137ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 9147c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9157ad07e7cSArnaldo Carvalho de Melo #endif 9167ad07e7cSArnaldo Carvalho de Melo } 9177c657876SArnaldo Carvalho de Melo 9187ad07e7cSArnaldo Carvalho de Melo atomic_inc(sk->sk_prot->orphan_count); 9197c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 9207c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 9217c657876SArnaldo Carvalho de Melo 9227c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 9237c657876SArnaldo Carvalho de Melo 9247c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 9257c657876SArnaldo Carvalho de Melo local_bh_enable(); 9267c657876SArnaldo Carvalho de Melo sock_put(sk); 9277c657876SArnaldo Carvalho de Melo } 9287c657876SArnaldo Carvalho de Melo 929f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 930f21e68caSArnaldo Carvalho de Melo 9317c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 9327c657876SArnaldo Carvalho de Melo { 9337c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 9347c657876SArnaldo Carvalho de Melo } 9357c657876SArnaldo Carvalho de Melo 936f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 937f21e68caSArnaldo Carvalho de Melo 93846f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9397c657876SArnaldo Carvalho de Melo { 9407c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9417c657876SArnaldo Carvalho de Melo 9427c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9437c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9447c657876SArnaldo Carvalho de Melo goto out; 9457c657876SArnaldo Carvalho de Melo 9467c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9477c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9487c657876SArnaldo Carvalho de Melo goto out_free_one; 9497c657876SArnaldo Carvalho de Melo 9507c657876SArnaldo Carvalho de Melo rc = 0; 9517c657876SArnaldo Carvalho de Melo out: 9527c657876SArnaldo Carvalho de Melo return rc; 9537c657876SArnaldo Carvalho de Melo out_free_one: 9547c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9557c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9567c657876SArnaldo Carvalho de Melo goto out; 9577c657876SArnaldo Carvalho de Melo 9587c657876SArnaldo Carvalho de Melo } 9597c657876SArnaldo Carvalho de Melo 960b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 96146f09ffaSArnaldo Carvalho de Melo { 96246f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 96346f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 96446f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 96546f09ffaSArnaldo Carvalho de Melo } 96646f09ffaSArnaldo Carvalho de Melo 9677c657876SArnaldo Carvalho de Melo static int thash_entries; 9687c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9697c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9707c657876SArnaldo Carvalho de Melo 971a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9727c657876SArnaldo Carvalho de Melo int dccp_debug; 9737c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9747c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 975f21e68caSArnaldo Carvalho de Melo 976f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 977a1d3a355SArnaldo Carvalho de Melo #endif 9787c657876SArnaldo Carvalho de Melo 9797c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9807c657876SArnaldo Carvalho de Melo { 9817c657876SArnaldo Carvalho de Melo unsigned long goal; 9827c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 983b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 9847c657876SArnaldo Carvalho de Melo 9857690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 9867690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 9877690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 9887690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 9897c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 990b61fafc4SArnaldo Carvalho de Melo goto out; 9917c657876SArnaldo Carvalho de Melo 9927c657876SArnaldo Carvalho de Melo /* 9937c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 9947c657876SArnaldo Carvalho de Melo * hash tables. 9957c657876SArnaldo Carvalho de Melo * 9967c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 9977c657876SArnaldo Carvalho de Melo */ 9987c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 9997c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10007c657876SArnaldo Carvalho de Melo else 10017c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10027c657876SArnaldo Carvalho de Melo 10037c657876SArnaldo Carvalho de Melo if (thash_entries) 10047690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10057690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10067c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10077c657876SArnaldo Carvalho de Melo ; 10087c657876SArnaldo Carvalho de Melo do { 10097c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10107c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10117c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 10127690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10137690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10147c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10157c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10167c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10177c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10187c657876SArnaldo Carvalho de Melo 10197c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 10207c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 10217c657876SArnaldo Carvalho de Melo "established hash table\n"); 10227c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10237c657876SArnaldo Carvalho de Melo } 10247c657876SArnaldo Carvalho de Melo 10257c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 10267c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10277c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 10287c657876SArnaldo Carvalho de Melo } 10297c657876SArnaldo Carvalho de Melo 10307c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10317c657876SArnaldo Carvalho de Melo 10327c657876SArnaldo Carvalho de Melo do { 10337c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10347c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10357690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10367690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10377c657876SArnaldo Carvalho de Melo continue; 10387c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10397c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10407c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10417c657876SArnaldo Carvalho de Melo 10427c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 10437c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 10447c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10457c657876SArnaldo Carvalho de Melo } 10467c657876SArnaldo Carvalho de Melo 10477c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10487c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10497c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10507c657876SArnaldo Carvalho de Melo } 10517c657876SArnaldo Carvalho de Melo 105246f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1053fa23e2ecSArnaldo Carvalho de Melo if (rc) 10547c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10557c657876SArnaldo Carvalho de Melo 10569b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10577c657876SArnaldo Carvalho de Melo if (rc) 1058b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 10599b07ef5dSArnaldo Carvalho de Melo 1060e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10619b07ef5dSArnaldo Carvalho de Melo if (rc) 10629b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 10637c657876SArnaldo Carvalho de Melo out: 10647c657876SArnaldo Carvalho de Melo return rc; 10659b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10669b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1067b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 106846f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10697c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10707c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10717c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10727c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10737c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10747c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10757c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10767c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10777c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10787c657876SArnaldo Carvalho de Melo goto out; 10797c657876SArnaldo Carvalho de Melo } 10807c657876SArnaldo Carvalho de Melo 10817c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 10827c657876SArnaldo Carvalho de Melo { 108346f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1084725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1085725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1086725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1087725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1088725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1089725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 10907c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10919b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1092e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 10937c657876SArnaldo Carvalho de Melo } 10947c657876SArnaldo Carvalho de Melo 10957c657876SArnaldo Carvalho de Melo module_init(dccp_init); 10967c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 10977c657876SArnaldo Carvalho de Melo 10987c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 10997c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11007c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1101