17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/proto.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 57c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 67c657876SArnaldo Carvalho de Melo * 77c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or modify it 87c657876SArnaldo Carvalho de Melo * under the terms of the GNU General Public License version 2 as 97c657876SArnaldo Carvalho de Melo * published by the Free Software Foundation. 107c657876SArnaldo Carvalho de Melo */ 117c657876SArnaldo Carvalho de Melo 127c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 137c657876SArnaldo Carvalho de Melo #include <linux/module.h> 147c657876SArnaldo Carvalho de Melo #include <linux/types.h> 157c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 167c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 177c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 187c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 197c657876SArnaldo Carvalho de Melo #include <linux/in.h> 207c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 217c657876SArnaldo Carvalho de Melo #include <linux/init.h> 227c657876SArnaldo Carvalho de Melo #include <linux/random.h> 237c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 247c657876SArnaldo Carvalho de Melo 2514c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 267c657876SArnaldo Carvalho de Melo #include <net/sock.h> 277c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 287c657876SArnaldo Carvalho de Melo 296273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h> 307c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 317c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 327c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 337c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 347c657876SArnaldo Carvalho de Melo 357c657876SArnaldo Carvalho de Melo #include "ccid.h" 367c657876SArnaldo Carvalho de Melo #include "dccp.h" 37afe00251SAndrea Bittau #include "feat.h" 387c657876SArnaldo Carvalho de Melo 39ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 407c657876SArnaldo Carvalho de Melo 41f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 42f21e68caSArnaldo Carvalho de Melo 437c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 447c657876SArnaldo Carvalho de Melo 45f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 46f21e68caSArnaldo Carvalho de Melo 47075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 48075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 49075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 50075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 51075ae866SArnaldo Carvalho de Melo }; 52075ae866SArnaldo Carvalho de Melo 53075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 54075ae866SArnaldo Carvalho de Melo 55b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 56b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 57b1308dc0SIan McDonald 58c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 59c25a18baSArnaldo Carvalho de Melo { 60c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 61c25a18baSArnaldo Carvalho de Melo 62f11135a3SGerrit Renker dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk, 63c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 64c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 65c25a18baSArnaldo Carvalho de Melo 66c25a18baSArnaldo Carvalho de Melo switch (state) { 67c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 68c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 69c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 703a53a9adSGerrit Renker /* Client retransmits all Confirm options until entering OPEN */ 713a53a9adSGerrit Renker if (oldstate == DCCP_PARTOPEN) 723a53a9adSGerrit Renker dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg); 73c25a18baSArnaldo Carvalho de Melo break; 74c25a18baSArnaldo Carvalho de Melo 75c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 760c869620SGerrit Renker if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ || 770c869620SGerrit Renker oldstate == DCCP_CLOSING) 78c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 79c25a18baSArnaldo Carvalho de Melo 80c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 81c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 82c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 83ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 84c25a18baSArnaldo Carvalho de Melo /* fall through */ 85c25a18baSArnaldo Carvalho de Melo default: 86c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 87c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 88c25a18baSArnaldo Carvalho de Melo } 89c25a18baSArnaldo Carvalho de Melo 90c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 91c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 92c25a18baSArnaldo Carvalho de Melo */ 93c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 94c25a18baSArnaldo Carvalho de Melo } 95c25a18baSArnaldo Carvalho de Melo 96c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 97c25a18baSArnaldo Carvalho de Melo 980c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk) 990c869620SGerrit Renker { 1000c869620SGerrit Renker switch (sk->sk_state) { 1010c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 1020c869620SGerrit Renker /* Node (client or server) has received Close packet. */ 1030c869620SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED); 1040c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1050c869620SGerrit Renker break; 1060c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 1070c869620SGerrit Renker /* 1080c869620SGerrit Renker * Client received CloseReq. We set the `active' flag so that 1090c869620SGerrit Renker * dccp_send_close() retransmits the Close as per RFC 4340, 8.3. 1100c869620SGerrit Renker */ 1110c869620SGerrit Renker dccp_send_close(sk, 1); 1120c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSING); 1130c869620SGerrit Renker } 1140c869620SGerrit Renker } 1150c869620SGerrit Renker 116c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 117c25a18baSArnaldo Carvalho de Melo { 118c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 119c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 120c25a18baSArnaldo Carvalho de Melo 121c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 122c25a18baSArnaldo Carvalho de Melo 123c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 124c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 125c25a18baSArnaldo Carvalho de Melo else 126c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 127c25a18baSArnaldo Carvalho de Melo } 128c25a18baSArnaldo Carvalho de Melo 129c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 130c25a18baSArnaldo Carvalho de Melo 1317c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1327c657876SArnaldo Carvalho de Melo { 1337c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 1347c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1357c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1367c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1377c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1387c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1397c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1407c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1417c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1427c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1437c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1447c657876SArnaldo Carvalho de Melo }; 1457c657876SArnaldo Carvalho de Melo 1467c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1477c657876SArnaldo Carvalho de Melo return "INVALID"; 1487c657876SArnaldo Carvalho de Melo else 1497c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1507c657876SArnaldo Carvalho de Melo } 1517c657876SArnaldo Carvalho de Melo 1527c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1537c657876SArnaldo Carvalho de Melo 1547c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 1557c657876SArnaldo Carvalho de Melo { 1567c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 1577c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 1587c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 1597c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 1607c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 1617c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 1627c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 163f11135a3SGerrit Renker [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ", 164f11135a3SGerrit Renker [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE", 165f11135a3SGerrit Renker [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ", 1667c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 1677c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 1687c657876SArnaldo Carvalho de Melo }; 1697c657876SArnaldo Carvalho de Melo 1707c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1717c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1727c657876SArnaldo Carvalho de Melo else 1737c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1747c657876SArnaldo Carvalho de Melo } 1757c657876SArnaldo Carvalho de Melo 1767c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1777c657876SArnaldo Carvalho de Melo 17872478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1793e0fadc5SArnaldo Carvalho de Melo { 1803e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1813e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1823e0fadc5SArnaldo Carvalho de Melo 183e18d7a98SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 184e18d7a98SArnaldo Carvalho de Melo icsk->icsk_syn_retries = sysctl_dccp_request_retries; 185e18d7a98SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 186e18d7a98SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 187e18d7a98SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 188e18d7a98SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 189e18d7a98SArnaldo Carvalho de Melo dp->dccps_rate_last = jiffies; 190e18d7a98SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 191e18d7a98SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 192d6da3511STomasz Grobelny dp->dccps_tx_qlen = sysctl_dccp_tx_qlen; 193e18d7a98SArnaldo Carvalho de Melo 194e18d7a98SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 195e18d7a98SArnaldo Carvalho de Melo 196828755ceSGerrit Renker INIT_LIST_HEAD(&dp->dccps_featneg); 1973e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 1983a53a9adSGerrit Renker if (likely(ctl_sock_initialized)) 1993a53a9adSGerrit Renker return dccp_feat_init(sk); 2003e0fadc5SArnaldo Carvalho de Melo return 0; 2013e0fadc5SArnaldo Carvalho de Melo } 2023e0fadc5SArnaldo Carvalho de Melo 2033e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2043e0fadc5SArnaldo Carvalho de Melo 2057d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk) 2063e0fadc5SArnaldo Carvalho de Melo { 2073e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2083e0fadc5SArnaldo Carvalho de Melo 2093e0fadc5SArnaldo Carvalho de Melo /* 2103e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 2113e0fadc5SArnaldo Carvalho de Melo * for retransmissions 2123e0fadc5SArnaldo Carvalho de Melo */ 2133e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2143e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2153e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2163e0fadc5SArnaldo Carvalho de Melo } 2173e0fadc5SArnaldo Carvalho de Melo 2183e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2193e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 220ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 2213e0fadc5SArnaldo Carvalho de Melo 2223e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2233e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2243e0fadc5SArnaldo Carvalho de Melo 225b235dc4aSGerrit Renker if (dp->dccps_hc_rx_ackvec != NULL) { 2263e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2273e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2283e0fadc5SArnaldo Carvalho de Melo } 2293e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2303e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2313e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2323e0fadc5SArnaldo Carvalho de Melo 2333e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 23470208383SGerrit Renker dccp_feat_list_purge(&dp->dccps_featneg); 2353e0fadc5SArnaldo Carvalho de Melo } 2363e0fadc5SArnaldo Carvalho de Melo 2373e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2383e0fadc5SArnaldo Carvalho de Melo 23972a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2407c657876SArnaldo Carvalho de Melo { 24167e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 24267e6b629SArnaldo Carvalho de Melo 24367e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 244093e1f46SGerrit Renker /* do not start to listen if feature negotiation setup fails */ 245093e1f46SGerrit Renker if (dccp_feat_finalise_settings(dp)) 246093e1f46SGerrit Renker return -EPROTO; 24772a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2487c657876SArnaldo Carvalho de Melo } 2497c657876SArnaldo Carvalho de Melo 250ce865a61SGerrit Renker static inline int dccp_need_reset(int state) 251ce865a61SGerrit Renker { 252ce865a61SGerrit Renker return state != DCCP_CLOSED && state != DCCP_LISTEN && 253ce865a61SGerrit Renker state != DCCP_REQUESTING; 254ce865a61SGerrit Renker } 255ce865a61SGerrit Renker 2567c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2577c657876SArnaldo Carvalho de Melo { 2587c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2597c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2607c657876SArnaldo Carvalho de Melo int err = 0; 2617c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2627c657876SArnaldo Carvalho de Melo 2637c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2647c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2657c657876SArnaldo Carvalho de Melo 266ce865a61SGerrit Renker /* 267ce865a61SGerrit Renker * This corresponds to the ABORT function of RFC793, sec. 3.8 268ce865a61SGerrit Renker * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted". 269ce865a61SGerrit Renker */ 2707c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2717c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 272ce865a61SGerrit Renker } else if (dccp_need_reset(old_state)) { 273ce865a61SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 274ce865a61SGerrit Renker sk->sk_err = ECONNRESET; 2757c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2767c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2777c657876SArnaldo Carvalho de Melo 2787c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 27948816322SGerrit Renker 2807c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 28148816322SGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 2827c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2837c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2847c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2857c657876SArnaldo Carvalho de Melo } 2867c657876SArnaldo Carvalho de Melo 2877c657876SArnaldo Carvalho de Melo inet->dport = 0; 2887c657876SArnaldo Carvalho de Melo 2897c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 2907c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 2917c657876SArnaldo Carvalho de Melo 2927c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 2937c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 2947c657876SArnaldo Carvalho de Melo 2957c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 2967c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 2977c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 2987c657876SArnaldo Carvalho de Melo 299547b792cSIlpo Järvinen WARN_ON(inet->num && !icsk->icsk_bind_hash); 3007c657876SArnaldo Carvalho de Melo 3017c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3027c657876SArnaldo Carvalho de Melo return err; 3037c657876SArnaldo Carvalho de Melo } 3047c657876SArnaldo Carvalho de Melo 305f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 306f21e68caSArnaldo Carvalho de Melo 307331968bdSArnaldo Carvalho de Melo /* 308331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 309331968bdSArnaldo Carvalho de Melo * 310331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 311331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 312331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 313331968bdSArnaldo Carvalho de Melo */ 314f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 315331968bdSArnaldo Carvalho de Melo poll_table *wait) 316331968bdSArnaldo Carvalho de Melo { 317331968bdSArnaldo Carvalho de Melo unsigned int mask; 318331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 319331968bdSArnaldo Carvalho de Melo 320331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 321331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 322331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 323331968bdSArnaldo Carvalho de Melo 324331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 325331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 326331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 327331968bdSArnaldo Carvalho de Melo */ 328331968bdSArnaldo Carvalho de Melo 329331968bdSArnaldo Carvalho de Melo mask = 0; 330331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 331331968bdSArnaldo Carvalho de Melo mask = POLLERR; 332331968bdSArnaldo Carvalho de Melo 333331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 334331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 335331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 336f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 337331968bdSArnaldo Carvalho de Melo 338331968bdSArnaldo Carvalho de Melo /* Connected? */ 339331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 340331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 341331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 342331968bdSArnaldo Carvalho de Melo 343331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 344331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 345331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 346331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 347331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 348331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 349331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 350331968bdSArnaldo Carvalho de Melo 351331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 352331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 353331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 354331968bdSArnaldo Carvalho de Melo */ 355331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 356331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 357331968bdSArnaldo Carvalho de Melo } 358331968bdSArnaldo Carvalho de Melo } 359331968bdSArnaldo Carvalho de Melo } 360331968bdSArnaldo Carvalho de Melo return mask; 361331968bdSArnaldo Carvalho de Melo } 362331968bdSArnaldo Carvalho de Melo 363f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 364f21e68caSArnaldo Carvalho de Melo 3657c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3667c657876SArnaldo Carvalho de Melo { 3676273172eSArnaldo Carvalho de Melo int rc = -ENOTCONN; 3686273172eSArnaldo Carvalho de Melo 3696273172eSArnaldo Carvalho de Melo lock_sock(sk); 3706273172eSArnaldo Carvalho de Melo 3716273172eSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 3726273172eSArnaldo Carvalho de Melo goto out; 3736273172eSArnaldo Carvalho de Melo 3746273172eSArnaldo Carvalho de Melo switch (cmd) { 3756273172eSArnaldo Carvalho de Melo case SIOCINQ: { 3766273172eSArnaldo Carvalho de Melo struct sk_buff *skb; 3776273172eSArnaldo Carvalho de Melo unsigned long amount = 0; 3786273172eSArnaldo Carvalho de Melo 3796273172eSArnaldo Carvalho de Melo skb = skb_peek(&sk->sk_receive_queue); 3806273172eSArnaldo Carvalho de Melo if (skb != NULL) { 3816273172eSArnaldo Carvalho de Melo /* 3826273172eSArnaldo Carvalho de Melo * We will only return the amount of this packet since 3836273172eSArnaldo Carvalho de Melo * that is all that will be read. 3846273172eSArnaldo Carvalho de Melo */ 3856273172eSArnaldo Carvalho de Melo amount = skb->len; 3866273172eSArnaldo Carvalho de Melo } 3876273172eSArnaldo Carvalho de Melo rc = put_user(amount, (int __user *)arg); 3886273172eSArnaldo Carvalho de Melo } 3896273172eSArnaldo Carvalho de Melo break; 3906273172eSArnaldo Carvalho de Melo default: 3916273172eSArnaldo Carvalho de Melo rc = -ENOIOCTLCMD; 3926273172eSArnaldo Carvalho de Melo break; 3936273172eSArnaldo Carvalho de Melo } 3946273172eSArnaldo Carvalho de Melo out: 3956273172eSArnaldo Carvalho de Melo release_sock(sk); 3966273172eSArnaldo Carvalho de Melo return rc; 3977c657876SArnaldo Carvalho de Melo } 3987c657876SArnaldo Carvalho de Melo 399f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 400f21e68caSArnaldo Carvalho de Melo 40160fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 40267e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 40367e6b629SArnaldo Carvalho de Melo { 40467e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 40567e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 40667e6b629SArnaldo Carvalho de Melo 40767e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 40867e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 40967e6b629SArnaldo Carvalho de Melo return -EINVAL; 41067e6b629SArnaldo Carvalho de Melo 41167e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 41267e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 41367e6b629SArnaldo Carvalho de Melo if (sl == NULL) 41467e6b629SArnaldo Carvalho de Melo return -ENOMEM; 41567e6b629SArnaldo Carvalho de Melo 41667e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 41767e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 41867e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 41967e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 42067e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 42167e6b629SArnaldo Carvalho de Melo kfree(sl); 42267e6b629SArnaldo Carvalho de Melo return -EFAULT; 42367e6b629SArnaldo Carvalho de Melo } 42467e6b629SArnaldo Carvalho de Melo } 42567e6b629SArnaldo Carvalho de Melo 42667e6b629SArnaldo Carvalho de Melo lock_sock(sk); 42767e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 42867e6b629SArnaldo Carvalho de Melo 42967e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 43067e6b629SArnaldo Carvalho de Melo 43167e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 43267e6b629SArnaldo Carvalho de Melo release_sock(sk); 43367e6b629SArnaldo Carvalho de Melo return 0; 43467e6b629SArnaldo Carvalho de Melo } 43567e6b629SArnaldo Carvalho de Melo 43620f41eeeSGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx) 43720f41eeeSGerrit Renker { 43820f41eeeSGerrit Renker u8 *list, len; 43920f41eeeSGerrit Renker int i, rc; 44020f41eeeSGerrit Renker 44120f41eeeSGerrit Renker if (cscov < 0 || cscov > 15) 44220f41eeeSGerrit Renker return -EINVAL; 44320f41eeeSGerrit Renker /* 44420f41eeeSGerrit Renker * Populate a list of permissible values, in the range cscov...15. This 44520f41eeeSGerrit Renker * is necessary since feature negotiation of single values only works if 44620f41eeeSGerrit Renker * both sides incidentally choose the same value. Since the list starts 44720f41eeeSGerrit Renker * lowest-value first, negotiation will pick the smallest shared value. 44820f41eeeSGerrit Renker */ 44920f41eeeSGerrit Renker if (cscov == 0) 45020f41eeeSGerrit Renker return 0; 45120f41eeeSGerrit Renker len = 16 - cscov; 45220f41eeeSGerrit Renker 45320f41eeeSGerrit Renker list = kmalloc(len, GFP_KERNEL); 45420f41eeeSGerrit Renker if (list == NULL) 45520f41eeeSGerrit Renker return -ENOBUFS; 45620f41eeeSGerrit Renker 45720f41eeeSGerrit Renker for (i = 0; i < len; i++) 45820f41eeeSGerrit Renker list[i] = cscov++; 45920f41eeeSGerrit Renker 46020f41eeeSGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len); 46120f41eeeSGerrit Renker 46220f41eeeSGerrit Renker if (rc == 0) { 46320f41eeeSGerrit Renker if (rx) 46420f41eeeSGerrit Renker dccp_sk(sk)->dccps_pcrlen = cscov; 46520f41eeeSGerrit Renker else 46620f41eeeSGerrit Renker dccp_sk(sk)->dccps_pcslen = cscov; 46720f41eeeSGerrit Renker } 46820f41eeeSGerrit Renker kfree(list); 46920f41eeeSGerrit Renker return rc; 47020f41eeeSGerrit Renker } 47120f41eeeSGerrit Renker 472fade756fSGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type, 473fade756fSGerrit Renker char __user *optval, int optlen) 474fade756fSGerrit Renker { 475fade756fSGerrit Renker u8 *val; 476fade756fSGerrit Renker int rc = 0; 477fade756fSGerrit Renker 478fade756fSGerrit Renker if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS) 479fade756fSGerrit Renker return -EINVAL; 480fade756fSGerrit Renker 481fade756fSGerrit Renker val = kmalloc(optlen, GFP_KERNEL); 482fade756fSGerrit Renker if (val == NULL) 483fade756fSGerrit Renker return -ENOMEM; 484fade756fSGerrit Renker 485fade756fSGerrit Renker if (copy_from_user(val, optval, optlen)) { 486fade756fSGerrit Renker kfree(val); 487fade756fSGerrit Renker return -EFAULT; 488fade756fSGerrit Renker } 489fade756fSGerrit Renker 490fade756fSGerrit Renker lock_sock(sk); 491fade756fSGerrit Renker if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID) 492fade756fSGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen); 493fade756fSGerrit Renker 494fade756fSGerrit Renker if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID)) 495fade756fSGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen); 496fade756fSGerrit Renker release_sock(sk); 497fade756fSGerrit Renker 498fade756fSGerrit Renker kfree(val); 499fade756fSGerrit Renker return rc; 500fade756fSGerrit Renker } 501fade756fSGerrit Renker 5023fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 503a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 5047c657876SArnaldo Carvalho de Melo { 50509dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 50609dbc389SGerrit Renker int val, err = 0; 5077c657876SArnaldo Carvalho de Melo 50873bbe095SGerrit Renker switch (optname) { 50973bbe095SGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE: 51073bbe095SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 51173bbe095SGerrit Renker return 0; 51273bbe095SGerrit Renker case DCCP_SOCKOPT_CHANGE_L: 51373bbe095SGerrit Renker case DCCP_SOCKOPT_CHANGE_R: 51473bbe095SGerrit Renker DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n"); 51573bbe095SGerrit Renker return 0; 516fade756fSGerrit Renker case DCCP_SOCKOPT_CCID: 517fade756fSGerrit Renker case DCCP_SOCKOPT_RX_CCID: 518fade756fSGerrit Renker case DCCP_SOCKOPT_TX_CCID: 519fade756fSGerrit Renker return dccp_setsockopt_ccid(sk, optname, optval, optlen); 52073bbe095SGerrit Renker } 52173bbe095SGerrit Renker 52273bbe095SGerrit Renker if (optlen < (int)sizeof(int)) 523a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 524a84ffe43SArnaldo Carvalho de Melo 525a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 526a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 527a84ffe43SArnaldo Carvalho de Melo 52867e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 52967e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 530a84ffe43SArnaldo Carvalho de Melo 53167e6b629SArnaldo Carvalho de Melo lock_sock(sk); 532a84ffe43SArnaldo Carvalho de Melo switch (optname) { 533b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 534b8599d20SGerrit Renker if (dp->dccps_role != DCCP_ROLE_SERVER) 535b8599d20SGerrit Renker err = -EOPNOTSUPP; 536b8599d20SGerrit Renker else 537b8599d20SGerrit Renker dp->dccps_server_timewait = (val != 0); 538b8599d20SGerrit Renker break; 53920f41eeeSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 54020f41eeeSGerrit Renker err = dccp_setsockopt_cscov(sk, val, false); 5416f4e5fffSGerrit Renker break; 54220f41eeeSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 54320f41eeeSGerrit Renker err = dccp_setsockopt_cscov(sk, val, true); 5446f4e5fffSGerrit Renker break; 545d6da3511STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 546d6da3511STomasz Grobelny if (sk->sk_state != DCCP_CLOSED) 547d6da3511STomasz Grobelny err = -EISCONN; 548d6da3511STomasz Grobelny else if (val < 0 || val >= DCCPQ_POLICY_MAX) 549d6da3511STomasz Grobelny err = -EINVAL; 550d6da3511STomasz Grobelny else 551d6da3511STomasz Grobelny dp->dccps_qpolicy = val; 552d6da3511STomasz Grobelny break; 553d6da3511STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 554d6da3511STomasz Grobelny if (val < 0) 555d6da3511STomasz Grobelny err = -EINVAL; 556d6da3511STomasz Grobelny else 557d6da3511STomasz Grobelny dp->dccps_tx_qlen = val; 558d6da3511STomasz Grobelny break; 559a84ffe43SArnaldo Carvalho de Melo default: 560a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 561a84ffe43SArnaldo Carvalho de Melo break; 562a84ffe43SArnaldo Carvalho de Melo } 563a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 56473bbe095SGerrit Renker 565a84ffe43SArnaldo Carvalho de Melo return err; 5667c657876SArnaldo Carvalho de Melo } 5677c657876SArnaldo Carvalho de Melo 5683fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 5693fdadf7dSDmitry Mishin char __user *optval, int optlen) 5703fdadf7dSDmitry Mishin { 5713fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5723fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5733fdadf7dSDmitry Mishin optname, optval, 5743fdadf7dSDmitry Mishin optlen); 5753fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5763fdadf7dSDmitry Mishin } 577543d9cfeSArnaldo Carvalho de Melo 578f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 579f21e68caSArnaldo Carvalho de Melo 5803fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5813fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 5823fdadf7dSDmitry Mishin char __user *optval, int optlen) 5833fdadf7dSDmitry Mishin { 584dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 585dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 586dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5873fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5883fdadf7dSDmitry Mishin } 589543d9cfeSArnaldo Carvalho de Melo 5903fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5913fdadf7dSDmitry Mishin #endif 5923fdadf7dSDmitry Mishin 59367e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 59460fe62e7SAndrea Bittau __be32 __user *optval, 59567e6b629SArnaldo Carvalho de Melo int __user *optlen) 59667e6b629SArnaldo Carvalho de Melo { 59767e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 59867e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 59967e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 60067e6b629SArnaldo Carvalho de Melo 60167e6b629SArnaldo Carvalho de Melo lock_sock(sk); 60267e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 60367e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 60467e6b629SArnaldo Carvalho de Melo total_len += slen; 60567e6b629SArnaldo Carvalho de Melo } 60667e6b629SArnaldo Carvalho de Melo 60767e6b629SArnaldo Carvalho de Melo err = -EINVAL; 60867e6b629SArnaldo Carvalho de Melo if (total_len > len) 60967e6b629SArnaldo Carvalho de Melo goto out; 61067e6b629SArnaldo Carvalho de Melo 61167e6b629SArnaldo Carvalho de Melo err = 0; 61267e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 61367e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 61467e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 61567e6b629SArnaldo Carvalho de Melo err = -EFAULT; 61667e6b629SArnaldo Carvalho de Melo out: 61767e6b629SArnaldo Carvalho de Melo release_sock(sk); 61867e6b629SArnaldo Carvalho de Melo return err; 61967e6b629SArnaldo Carvalho de Melo } 62067e6b629SArnaldo Carvalho de Melo 6213fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 622a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 6237c657876SArnaldo Carvalho de Melo { 624a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 625a84ffe43SArnaldo Carvalho de Melo int val, len; 6267c657876SArnaldo Carvalho de Melo 627a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 628a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 629a84ffe43SArnaldo Carvalho de Melo 63039ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 631a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 632a84ffe43SArnaldo Carvalho de Melo 633a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 634a84ffe43SArnaldo Carvalho de Melo 635a84ffe43SArnaldo Carvalho de Melo switch (optname) { 636a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 6375aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 638841bac1dSArnaldo Carvalho de Melo return 0; 63988f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 64088f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 64160fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 6427c559a9eSGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS: 6437c559a9eSGerrit Renker val = dp->dccps_mss_cache; 6447c559a9eSGerrit Renker break; 64571bb4959SGerrit Renker case DCCP_SOCKOPT_AVAILABLE_CCIDS: 64671bb4959SGerrit Renker return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen); 647c8041e26SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 648c8041e26SGerrit Renker val = ccid_get_current_tx_ccid(dp); 649c8041e26SGerrit Renker if (val < 0) 650c8041e26SGerrit Renker return -ENOPROTOOPT; 651c8041e26SGerrit Renker break; 652c8041e26SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 653c8041e26SGerrit Renker val = ccid_get_current_rx_ccid(dp); 654c8041e26SGerrit Renker if (val < 0) 655c8041e26SGerrit Renker return -ENOPROTOOPT; 656c8041e26SGerrit Renker break; 657b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 658b8599d20SGerrit Renker val = dp->dccps_server_timewait; 659b8599d20SGerrit Renker break; 6606f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 6616f4e5fffSGerrit Renker val = dp->dccps_pcslen; 6626f4e5fffSGerrit Renker break; 6636f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 6646f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 6656f4e5fffSGerrit Renker break; 666d6da3511STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 667d6da3511STomasz Grobelny val = dp->dccps_qpolicy; 668d6da3511STomasz Grobelny break; 669d6da3511STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 670d6da3511STomasz Grobelny val = dp->dccps_tx_qlen; 671d6da3511STomasz Grobelny break; 67288f964dbSArnaldo Carvalho de Melo case 128 ... 191: 67388f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 67488f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 67588f964dbSArnaldo Carvalho de Melo case 192 ... 255: 67688f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 67788f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 678a84ffe43SArnaldo Carvalho de Melo default: 679a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 680a84ffe43SArnaldo Carvalho de Melo } 681a84ffe43SArnaldo Carvalho de Melo 68279133506SGerrit Renker len = sizeof(val); 683a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 684a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 685a84ffe43SArnaldo Carvalho de Melo 686a84ffe43SArnaldo Carvalho de Melo return 0; 6877c657876SArnaldo Carvalho de Melo } 6887c657876SArnaldo Carvalho de Melo 6893fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6903fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6913fdadf7dSDmitry Mishin { 6923fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6933fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6943fdadf7dSDmitry Mishin optname, optval, 6953fdadf7dSDmitry Mishin optlen); 6963fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6973fdadf7dSDmitry Mishin } 698543d9cfeSArnaldo Carvalho de Melo 699f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 700f21e68caSArnaldo Carvalho de Melo 7013fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7023fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 7033fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7043fdadf7dSDmitry Mishin { 705dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 706dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 707dec73ff0SArnaldo Carvalho de Melo optval, optlen); 7083fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 7093fdadf7dSDmitry Mishin } 710543d9cfeSArnaldo Carvalho de Melo 7113fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 7123fdadf7dSDmitry Mishin #endif 7133fdadf7dSDmitry Mishin 714d6da3511STomasz Grobelny static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb) 715d6da3511STomasz Grobelny { 716d6da3511STomasz Grobelny struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); 717d6da3511STomasz Grobelny 718d6da3511STomasz Grobelny /* 719d6da3511STomasz Grobelny * Assign an (opaque) qpolicy priority value to skb->priority. 720d6da3511STomasz Grobelny * 721d6da3511STomasz Grobelny * We are overloading this skb field for use with the qpolicy subystem. 722d6da3511STomasz Grobelny * The skb->priority is normally used for the SO_PRIORITY option, which 723d6da3511STomasz Grobelny * is initialised from sk_priority. Since the assignment of sk_priority 724d6da3511STomasz Grobelny * to skb->priority happens later (on layer 3), we overload this field 725d6da3511STomasz Grobelny * for use with queueing priorities as long as the skb is on layer 4. 726d6da3511STomasz Grobelny * The default priority value (if nothing is set) is 0. 727d6da3511STomasz Grobelny */ 728d6da3511STomasz Grobelny skb->priority = 0; 729d6da3511STomasz Grobelny 730d6da3511STomasz Grobelny for (; cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) { 731d6da3511STomasz Grobelny 732d6da3511STomasz Grobelny if (!CMSG_OK(msg, cmsg)) 733d6da3511STomasz Grobelny return -EINVAL; 734d6da3511STomasz Grobelny 735d6da3511STomasz Grobelny if (cmsg->cmsg_level != SOL_DCCP) 736d6da3511STomasz Grobelny continue; 737d6da3511STomasz Grobelny 738d6da3511STomasz Grobelny switch (cmsg->cmsg_type) { 739d6da3511STomasz Grobelny case DCCP_SCM_PRIORITY: 740d6da3511STomasz Grobelny if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32))) 741d6da3511STomasz Grobelny return -EINVAL; 742d6da3511STomasz Grobelny skb->priority = *(__u32 *)CMSG_DATA(cmsg); 743d6da3511STomasz Grobelny break; 744d6da3511STomasz Grobelny default: 745d6da3511STomasz Grobelny return -EINVAL; 746d6da3511STomasz Grobelny } 747d6da3511STomasz Grobelny } 748d6da3511STomasz Grobelny return 0; 749d6da3511STomasz Grobelny } 750d6da3511STomasz Grobelny 7517c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 7527c657876SArnaldo Carvalho de Melo size_t len) 7537c657876SArnaldo Carvalho de Melo { 7547c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 7557c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 7567c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 7577c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7587c657876SArnaldo Carvalho de Melo int rc, size; 7597c657876SArnaldo Carvalho de Melo long timeo; 7607c657876SArnaldo Carvalho de Melo 7617c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 7627c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 7637c657876SArnaldo Carvalho de Melo 7647c657876SArnaldo Carvalho de Melo lock_sock(sk); 765b1308dc0SIan McDonald 766d6da3511STomasz Grobelny if (dccp_qpolicy_full(sk)) { 767b1308dc0SIan McDonald rc = -EAGAIN; 768b1308dc0SIan McDonald goto out_release; 769b1308dc0SIan McDonald } 770b1308dc0SIan McDonald 77127258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 7727c657876SArnaldo Carvalho de Melo 7737c657876SArnaldo Carvalho de Melo /* 7747c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 7757c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 7767c657876SArnaldo Carvalho de Melo */ 7777c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 778cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 7797c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 78027258ee5SArnaldo Carvalho de Melo goto out_release; 7817c657876SArnaldo Carvalho de Melo 7827c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 7837c657876SArnaldo Carvalho de Melo release_sock(sk); 7847c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 7857c657876SArnaldo Carvalho de Melo lock_sock(sk); 7867c657876SArnaldo Carvalho de Melo if (skb == NULL) 7877c657876SArnaldo Carvalho de Melo goto out_release; 7887c657876SArnaldo Carvalho de Melo 7897c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 7907c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 79127258ee5SArnaldo Carvalho de Melo if (rc != 0) 79227258ee5SArnaldo Carvalho de Melo goto out_discard; 7937c657876SArnaldo Carvalho de Melo 794d6da3511STomasz Grobelny rc = dccp_msghdr_parse(msg, skb); 795d6da3511STomasz Grobelny if (rc != 0) 796d6da3511STomasz Grobelny goto out_discard; 797d6da3511STomasz Grobelny 798d6da3511STomasz Grobelny dccp_qpolicy_push(sk, skb); 799146993cfSGerrit Renker dccp_write_xmit(sk); 8007c657876SArnaldo Carvalho de Melo out_release: 8017c657876SArnaldo Carvalho de Melo release_sock(sk); 8027c657876SArnaldo Carvalho de Melo return rc ? : len; 80327258ee5SArnaldo Carvalho de Melo out_discard: 80427258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 8057c657876SArnaldo Carvalho de Melo goto out_release; 8067c657876SArnaldo Carvalho de Melo } 8077c657876SArnaldo Carvalho de Melo 808f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 809f21e68caSArnaldo Carvalho de Melo 8107c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 8117c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 8127c657876SArnaldo Carvalho de Melo { 8137c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 8147c657876SArnaldo Carvalho de Melo long timeo; 8157c657876SArnaldo Carvalho de Melo 8167c657876SArnaldo Carvalho de Melo lock_sock(sk); 8177c657876SArnaldo Carvalho de Melo 818531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 819531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8207c657876SArnaldo Carvalho de Melo goto out; 821531669a0SArnaldo Carvalho de Melo } 8227c657876SArnaldo Carvalho de Melo 8237c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 8247c657876SArnaldo Carvalho de Melo 8257c657876SArnaldo Carvalho de Melo do { 826531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 8277c657876SArnaldo Carvalho de Melo 828531669a0SArnaldo Carvalho de Melo if (skb == NULL) 829531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 8307c657876SArnaldo Carvalho de Melo 8317c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 8327c657876SArnaldo Carvalho de Melo 8330c869620SGerrit Renker switch (dh->dccph_type) { 8340c869620SGerrit Renker case DCCP_PKT_DATA: 8350c869620SGerrit Renker case DCCP_PKT_DATAACK: 8367c657876SArnaldo Carvalho de Melo goto found_ok_skb; 8377c657876SArnaldo Carvalho de Melo 8380c869620SGerrit Renker case DCCP_PKT_CLOSE: 8390c869620SGerrit Renker case DCCP_PKT_CLOSEREQ: 8400c869620SGerrit Renker if (!(flags & MSG_PEEK)) 8410c869620SGerrit Renker dccp_finish_passive_close(sk); 8420c869620SGerrit Renker /* fall through */ 8430c869620SGerrit Renker case DCCP_PKT_RESET: 8440c869620SGerrit Renker dccp_pr_debug("found fin (%s) ok!\n", 8450c869620SGerrit Renker dccp_packet_name(dh->dccph_type)); 846531669a0SArnaldo Carvalho de Melo len = 0; 8477c657876SArnaldo Carvalho de Melo goto found_fin_ok; 8480c869620SGerrit Renker default: 8497690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 8507690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 851624d1164SChris Leech sk_eat_skb(sk, skb, 0); 8520c869620SGerrit Renker } 853531669a0SArnaldo Carvalho de Melo verify_sock_status: 854531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 855531669a0SArnaldo Carvalho de Melo len = 0; 8567c657876SArnaldo Carvalho de Melo break; 8577c657876SArnaldo Carvalho de Melo } 8587c657876SArnaldo Carvalho de Melo 859531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 860531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 8617c657876SArnaldo Carvalho de Melo break; 862531669a0SArnaldo Carvalho de Melo } 863531669a0SArnaldo Carvalho de Melo 864531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 865531669a0SArnaldo Carvalho de Melo len = 0; 866531669a0SArnaldo Carvalho de Melo break; 867531669a0SArnaldo Carvalho de Melo } 8687c657876SArnaldo Carvalho de Melo 8697c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 8707c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 8717c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 8727c657876SArnaldo Carvalho de Melo * from never connected socket. 8737c657876SArnaldo Carvalho de Melo */ 874531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8757c657876SArnaldo Carvalho de Melo break; 8767c657876SArnaldo Carvalho de Melo } 877531669a0SArnaldo Carvalho de Melo len = 0; 8787c657876SArnaldo Carvalho de Melo break; 8797c657876SArnaldo Carvalho de Melo } 8807c657876SArnaldo Carvalho de Melo 8817c657876SArnaldo Carvalho de Melo if (!timeo) { 882531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 8837c657876SArnaldo Carvalho de Melo break; 8847c657876SArnaldo Carvalho de Melo } 8857c657876SArnaldo Carvalho de Melo 8867c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 887531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 8887c657876SArnaldo Carvalho de Melo break; 8897c657876SArnaldo Carvalho de Melo } 8907c657876SArnaldo Carvalho de Melo 8917c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 8927c657876SArnaldo Carvalho de Melo continue; 8937c657876SArnaldo Carvalho de Melo found_ok_skb: 894531669a0SArnaldo Carvalho de Melo if (len > skb->len) 895531669a0SArnaldo Carvalho de Melo len = skb->len; 896531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 897531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 8987c657876SArnaldo Carvalho de Melo 899531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 9007c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 901531669a0SArnaldo Carvalho de Melo len = -EFAULT; 9027c657876SArnaldo Carvalho de Melo break; 9037c657876SArnaldo Carvalho de Melo } 9047c657876SArnaldo Carvalho de Melo found_fin_ok: 9057c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 906624d1164SChris Leech sk_eat_skb(sk, skb, 0); 9077c657876SArnaldo Carvalho de Melo break; 908531669a0SArnaldo Carvalho de Melo } while (1); 9097c657876SArnaldo Carvalho de Melo out: 9107c657876SArnaldo Carvalho de Melo release_sock(sk); 911531669a0SArnaldo Carvalho de Melo return len; 9127c657876SArnaldo Carvalho de Melo } 9137c657876SArnaldo Carvalho de Melo 914f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 915f21e68caSArnaldo Carvalho de Melo 916f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 9177c657876SArnaldo Carvalho de Melo { 9187c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 9197c657876SArnaldo Carvalho de Melo unsigned char old_state; 9207c657876SArnaldo Carvalho de Melo int err; 9217c657876SArnaldo Carvalho de Melo 9227c657876SArnaldo Carvalho de Melo lock_sock(sk); 9237c657876SArnaldo Carvalho de Melo 9247c657876SArnaldo Carvalho de Melo err = -EINVAL; 9257c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 9267c657876SArnaldo Carvalho de Melo goto out; 9277c657876SArnaldo Carvalho de Melo 9287c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 9297c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 9307c657876SArnaldo Carvalho de Melo goto out; 9317c657876SArnaldo Carvalho de Melo 9327c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 9337c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 9347c657876SArnaldo Carvalho de Melo */ 9357c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 9367c657876SArnaldo Carvalho de Melo /* 9377c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 9387c657876SArnaldo Carvalho de Melo * see tcp_listen_start 9397c657876SArnaldo Carvalho de Melo */ 94072a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 9417c657876SArnaldo Carvalho de Melo if (err) 9427c657876SArnaldo Carvalho de Melo goto out; 9437c657876SArnaldo Carvalho de Melo } 9447c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 9457c657876SArnaldo Carvalho de Melo err = 0; 9467c657876SArnaldo Carvalho de Melo 9477c657876SArnaldo Carvalho de Melo out: 9487c657876SArnaldo Carvalho de Melo release_sock(sk); 9497c657876SArnaldo Carvalho de Melo return err; 9507c657876SArnaldo Carvalho de Melo } 9517c657876SArnaldo Carvalho de Melo 952f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 953f21e68caSArnaldo Carvalho de Melo 9540c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk) 9557c657876SArnaldo Carvalho de Melo { 9560c869620SGerrit Renker u8 next_state = DCCP_CLOSED; 9577c657876SArnaldo Carvalho de Melo 9580c869620SGerrit Renker switch (sk->sk_state) { 9590c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 9600c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 9610c869620SGerrit Renker dccp_finish_passive_close(sk); 9620c869620SGerrit Renker break; 9630c869620SGerrit Renker case DCCP_PARTOPEN: 9640c869620SGerrit Renker dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk); 9650c869620SGerrit Renker inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 9660c869620SGerrit Renker /* fall through */ 9670c869620SGerrit Renker case DCCP_OPEN: 9680c869620SGerrit Renker dccp_send_close(sk, 1); 9697c657876SArnaldo Carvalho de Melo 970b8599d20SGerrit Renker if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER && 971b8599d20SGerrit Renker !dccp_sk(sk)->dccps_server_timewait) 9720c869620SGerrit Renker next_state = DCCP_ACTIVE_CLOSEREQ; 9730c869620SGerrit Renker else 9740c869620SGerrit Renker next_state = DCCP_CLOSING; 9750c869620SGerrit Renker /* fall through */ 9760c869620SGerrit Renker default: 9770c869620SGerrit Renker dccp_set_state(sk, next_state); 9780c869620SGerrit Renker } 9797c657876SArnaldo Carvalho de Melo } 9807c657876SArnaldo Carvalho de Melo 9817c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 9827c657876SArnaldo Carvalho de Melo { 98397e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 9847c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 985d83bd95bSGerrit Renker u32 data_was_unread = 0; 986134af346SHerbert Xu int state; 9877c657876SArnaldo Carvalho de Melo 9887c657876SArnaldo Carvalho de Melo lock_sock(sk); 9897c657876SArnaldo Carvalho de Melo 9907c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 9917c657876SArnaldo Carvalho de Melo 9927c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 9937c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9947c657876SArnaldo Carvalho de Melo 9957c657876SArnaldo Carvalho de Melo /* Special case. */ 9967c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 9977c657876SArnaldo Carvalho de Melo 9987c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 9997c657876SArnaldo Carvalho de Melo } 10007c657876SArnaldo Carvalho de Melo 100197e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 100297e5848dSIan McDonald 10037c657876SArnaldo Carvalho de Melo /* 10047c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 10057c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 10067c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 10077c657876SArnaldo Carvalho de Melo */ 10087c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 1009d83bd95bSGerrit Renker data_was_unread += skb->len; 10107c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 10117c657876SArnaldo Carvalho de Melo } 10127c657876SArnaldo Carvalho de Melo 1013d83bd95bSGerrit Renker if (data_was_unread) { 1014d83bd95bSGerrit Renker /* Unread data was tossed, send an appropriate Reset Code */ 1015d83bd95bSGerrit Renker DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread); 1016d83bd95bSGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 1017d83bd95bSGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1018d83bd95bSGerrit Renker } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 10197c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 10207c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 10210c869620SGerrit Renker } else if (sk->sk_state != DCCP_CLOSED) { 1022146993cfSGerrit Renker /* 1023146993cfSGerrit Renker * Normal connection termination. May need to wait if there are 1024146993cfSGerrit Renker * still packets in the TX queue that are delayed by the CCID. 1025146993cfSGerrit Renker */ 1026146993cfSGerrit Renker dccp_flush_write_queue(sk, &timeout); 10270c869620SGerrit Renker dccp_terminate_connection(sk); 10287c657876SArnaldo Carvalho de Melo } 10297c657876SArnaldo Carvalho de Melo 1030146993cfSGerrit Renker /* 1031146993cfSGerrit Renker * Flush write queue. This may be necessary in several cases: 1032146993cfSGerrit Renker * - we have been closed by the peer but still have application data; 1033146993cfSGerrit Renker * - abortive termination (unread data or zero linger time), 1034146993cfSGerrit Renker * - normal termination but queue could not be flushed within time limit 1035146993cfSGerrit Renker */ 1036146993cfSGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 1037146993cfSGerrit Renker 10387c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 10397c657876SArnaldo Carvalho de Melo 10407c657876SArnaldo Carvalho de Melo adjudge_to_death: 1041134af346SHerbert Xu state = sk->sk_state; 1042134af346SHerbert Xu sock_hold(sk); 1043134af346SHerbert Xu sock_orphan(sk); 1044134af346SHerbert Xu atomic_inc(sk->sk_prot->orphan_count); 1045134af346SHerbert Xu 10467ad07e7cSArnaldo Carvalho de Melo /* 10477ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 10487ad07e7cSArnaldo Carvalho de Melo */ 10497c657876SArnaldo Carvalho de Melo release_sock(sk); 10507c657876SArnaldo Carvalho de Melo /* 10517c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 10527c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 10537c657876SArnaldo Carvalho de Melo */ 10547c657876SArnaldo Carvalho de Melo local_bh_disable(); 10557c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 1056547b792cSIlpo Järvinen WARN_ON(sock_owned_by_user(sk)); 10577c657876SArnaldo Carvalho de Melo 1058134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 1059134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 1060134af346SHerbert Xu goto out; 10617c657876SArnaldo Carvalho de Melo 10627c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 10637c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 10647c657876SArnaldo Carvalho de Melo 10657c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 10667c657876SArnaldo Carvalho de Melo 1067134af346SHerbert Xu out: 10687c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 10697c657876SArnaldo Carvalho de Melo local_bh_enable(); 10707c657876SArnaldo Carvalho de Melo sock_put(sk); 10717c657876SArnaldo Carvalho de Melo } 10727c657876SArnaldo Carvalho de Melo 1073f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 1074f21e68caSArnaldo Carvalho de Melo 10757c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 10767c657876SArnaldo Carvalho de Melo { 10778e8c71f1SGerrit Renker dccp_pr_debug("called shutdown(%x)\n", how); 10787c657876SArnaldo Carvalho de Melo } 10797c657876SArnaldo Carvalho de Melo 1080f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 1081f21e68caSArnaldo Carvalho de Melo 108224e8b7e4SYOSHIFUJI Hideaki static inline int dccp_mib_init(void) 10837c657876SArnaldo Carvalho de Melo { 108424e8b7e4SYOSHIFUJI Hideaki return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib)); 10857c657876SArnaldo Carvalho de Melo } 10867c657876SArnaldo Carvalho de Melo 108724e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void) 108846f09ffaSArnaldo Carvalho de Melo { 108924e8b7e4SYOSHIFUJI Hideaki snmp_mib_free((void**)dccp_statistics); 109046f09ffaSArnaldo Carvalho de Melo } 109146f09ffaSArnaldo Carvalho de Melo 10927c657876SArnaldo Carvalho de Melo static int thash_entries; 10937c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 10947c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 10957c657876SArnaldo Carvalho de Melo 1096a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 10977c657876SArnaldo Carvalho de Melo int dccp_debug; 109843264991SGerrit Renker module_param(dccp_debug, bool, 0644); 10997c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 1100f21e68caSArnaldo Carvalho de Melo 1101f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 1102a1d3a355SArnaldo Carvalho de Melo #endif 11037c657876SArnaldo Carvalho de Melo 11047c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 11057c657876SArnaldo Carvalho de Melo { 11067c657876SArnaldo Carvalho de Melo unsigned long goal; 11077c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1108b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 11097c657876SArnaldo Carvalho de Melo 1110028b0275SPatrick McHardy BUILD_BUG_ON(sizeof(struct dccp_skb_cb) > 1111028b0275SPatrick McHardy FIELD_SIZEOF(struct sk_buff, cb)); 1112028b0275SPatrick McHardy 11137690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 11147690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 11157690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 111620c2df83SPaul Mundt SLAB_HWCACHE_ALIGN, NULL); 11177c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1118b61fafc4SArnaldo Carvalho de Melo goto out; 11197c657876SArnaldo Carvalho de Melo 11207c657876SArnaldo Carvalho de Melo /* 11217c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 11227c657876SArnaldo Carvalho de Melo * hash tables. 11237c657876SArnaldo Carvalho de Melo * 11247c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 11257c657876SArnaldo Carvalho de Melo */ 11267c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 11277c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 11287c657876SArnaldo Carvalho de Melo else 11297c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 11307c657876SArnaldo Carvalho de Melo 11317c657876SArnaldo Carvalho de Melo if (thash_entries) 11327690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 11337690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 11347c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 11357c657876SArnaldo Carvalho de Melo ; 11367c657876SArnaldo Carvalho de Melo do { 11377c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 11387c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 11397690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 11407690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 11417c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 11427c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 11437c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 11447c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 11457c657876SArnaldo Carvalho de Melo 11467c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 114759348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 11487c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 11497c657876SArnaldo Carvalho de Melo } 11507c657876SArnaldo Carvalho de Melo 1151dbca9b27SEric Dumazet for (i = 0; i < dccp_hashinfo.ehash_size; i++) { 11527c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 1153dbca9b27SEric Dumazet INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].twchain); 11547c657876SArnaldo Carvalho de Melo } 11557c657876SArnaldo Carvalho de Melo 1156230140cfSEric Dumazet if (inet_ehash_locks_alloc(&dccp_hashinfo)) 1157230140cfSEric Dumazet goto out_free_dccp_ehash; 1158230140cfSEric Dumazet 11597c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 11607c657876SArnaldo Carvalho de Melo 11617c657876SArnaldo Carvalho de Melo do { 11627c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 11637c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 11647690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 11657690af3fSArnaldo Carvalho de Melo bhash_order > 0) 11667c657876SArnaldo Carvalho de Melo continue; 11677c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 11687c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 11697c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 11707c657876SArnaldo Carvalho de Melo 11717c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 117259348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 1173230140cfSEric Dumazet goto out_free_dccp_locks; 11747c657876SArnaldo Carvalho de Melo } 11757c657876SArnaldo Carvalho de Melo 11767c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 11777c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 11787c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 11797c657876SArnaldo Carvalho de Melo } 11807c657876SArnaldo Carvalho de Melo 118146f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1182fa23e2ecSArnaldo Carvalho de Melo if (rc) 11837c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 11847c657876SArnaldo Carvalho de Melo 11859b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 11867c657876SArnaldo Carvalho de Melo if (rc) 1187b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 11889b07ef5dSArnaldo Carvalho de Melo 1189e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 11909b07ef5dSArnaldo Carvalho de Melo if (rc) 11919b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 11924c70f383SGerrit Renker 11934c70f383SGerrit Renker dccp_timestamping_init(); 11947c657876SArnaldo Carvalho de Melo out: 11957c657876SArnaldo Carvalho de Melo return rc; 11969b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 11979b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1198b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 119946f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 12007c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 12017c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 12027c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 1203230140cfSEric Dumazet out_free_dccp_locks: 1204230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12057c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 12067c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 12077c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 12087c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 12097c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 12107c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 12117c657876SArnaldo Carvalho de Melo goto out; 12127c657876SArnaldo Carvalho de Melo } 12137c657876SArnaldo Carvalho de Melo 12147c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 12157c657876SArnaldo Carvalho de Melo { 121646f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1217725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1218725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1219725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1220725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1221725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1222725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 1223230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12247c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 12259b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1226e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 12277c657876SArnaldo Carvalho de Melo } 12287c657876SArnaldo Carvalho de Melo 12297c657876SArnaldo Carvalho de Melo module_init(dccp_init); 12307c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 12317c657876SArnaldo Carvalho de Melo 12327c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 12337c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 12347c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1235