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> 235a0e3ad6STejun Heo #include <linux/slab.h> 247c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 257c657876SArnaldo Carvalho de Melo 2614c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 27120e9dabSEric Dumazet #include <net/inet_common.h> 287c657876SArnaldo Carvalho de Melo #include <net/sock.h> 297c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 307c657876SArnaldo Carvalho de Melo 316273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h> 327c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 337c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 347c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 357c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 367c657876SArnaldo Carvalho de Melo 377c657876SArnaldo Carvalho de Melo #include "ccid.h" 387c657876SArnaldo Carvalho de Melo #include "dccp.h" 39afe00251SAndrea Bittau #include "feat.h" 407c657876SArnaldo Carvalho de Melo 41ee549be6SMasami Hiramatsu #define CREATE_TRACE_POINTS 42ee549be6SMasami Hiramatsu #include "trace.h" 43ee549be6SMasami Hiramatsu 44ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 457c657876SArnaldo Carvalho de Melo 46f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 47f21e68caSArnaldo Carvalho de Melo 48dd24c001SEric Dumazet struct percpu_counter dccp_orphan_count; 49f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 50f21e68caSArnaldo Carvalho de Melo 515caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo; 52075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 53075ae866SArnaldo Carvalho de Melo 54b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 55b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 56b1308dc0SIan McDonald 571f4f0f64Sstephen hemminger #ifdef CONFIG_IP_DCCP_DEBUG 581f4f0f64Sstephen hemminger static const char *dccp_state_name(const int state) 591f4f0f64Sstephen hemminger { 601f4f0f64Sstephen hemminger static const char *const dccp_state_names[] = { 611f4f0f64Sstephen hemminger [DCCP_OPEN] = "OPEN", 621f4f0f64Sstephen hemminger [DCCP_REQUESTING] = "REQUESTING", 631f4f0f64Sstephen hemminger [DCCP_PARTOPEN] = "PARTOPEN", 641f4f0f64Sstephen hemminger [DCCP_LISTEN] = "LISTEN", 651f4f0f64Sstephen hemminger [DCCP_RESPOND] = "RESPOND", 661f4f0f64Sstephen hemminger [DCCP_CLOSING] = "CLOSING", 671f4f0f64Sstephen hemminger [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ", 681f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE", 691f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ", 701f4f0f64Sstephen hemminger [DCCP_TIME_WAIT] = "TIME_WAIT", 711f4f0f64Sstephen hemminger [DCCP_CLOSED] = "CLOSED", 721f4f0f64Sstephen hemminger }; 731f4f0f64Sstephen hemminger 741f4f0f64Sstephen hemminger if (state >= DCCP_MAX_STATES) 751f4f0f64Sstephen hemminger return "INVALID STATE!"; 761f4f0f64Sstephen hemminger else 771f4f0f64Sstephen hemminger return dccp_state_names[state]; 781f4f0f64Sstephen hemminger } 791f4f0f64Sstephen hemminger #endif 801f4f0f64Sstephen hemminger 81c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 82c25a18baSArnaldo Carvalho de Melo { 83c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 84c25a18baSArnaldo Carvalho de Melo 85f11135a3SGerrit Renker dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk, 86c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 87c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 88c25a18baSArnaldo Carvalho de Melo 89c25a18baSArnaldo Carvalho de Melo switch (state) { 90c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 91c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 92c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 936eb55d17SGerrit Renker /* Client retransmits all Confirm options until entering OPEN */ 946eb55d17SGerrit Renker if (oldstate == DCCP_PARTOPEN) 956eb55d17SGerrit Renker dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg); 96c25a18baSArnaldo Carvalho de Melo break; 97c25a18baSArnaldo Carvalho de Melo 98c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 990c869620SGerrit Renker if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ || 1000c869620SGerrit Renker oldstate == DCCP_CLOSING) 101c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 102c25a18baSArnaldo Carvalho de Melo 103c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 104c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 105c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 106ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 107c25a18baSArnaldo Carvalho de Melo /* fall through */ 108c25a18baSArnaldo Carvalho de Melo default: 109c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 110c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 111c25a18baSArnaldo Carvalho de Melo } 112c25a18baSArnaldo Carvalho de Melo 113c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 114c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 115c25a18baSArnaldo Carvalho de Melo */ 116b0832e30SYafang Shao inet_sk_set_state(sk, state); 117c25a18baSArnaldo Carvalho de Melo } 118c25a18baSArnaldo Carvalho de Melo 119c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 120c25a18baSArnaldo Carvalho de Melo 1210c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk) 1220c869620SGerrit Renker { 1230c869620SGerrit Renker switch (sk->sk_state) { 1240c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 1250c869620SGerrit Renker /* Node (client or server) has received Close packet. */ 1260c869620SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED); 1270c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1280c869620SGerrit Renker break; 1290c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 1300c869620SGerrit Renker /* 1310c869620SGerrit Renker * Client received CloseReq. We set the `active' flag so that 1320c869620SGerrit Renker * dccp_send_close() retransmits the Close as per RFC 4340, 8.3. 1330c869620SGerrit Renker */ 1340c869620SGerrit Renker dccp_send_close(sk, 1); 1350c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSING); 1360c869620SGerrit Renker } 1370c869620SGerrit Renker } 1380c869620SGerrit Renker 139c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 140c25a18baSArnaldo Carvalho de Melo { 141c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 142c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 143c25a18baSArnaldo Carvalho de Melo 144c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 145c25a18baSArnaldo Carvalho de Melo 146c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 147c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 148c25a18baSArnaldo Carvalho de Melo else 149c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 150c25a18baSArnaldo Carvalho de Melo } 151c25a18baSArnaldo Carvalho de Melo 152c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 153c25a18baSArnaldo Carvalho de Melo 1547c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1557c657876SArnaldo Carvalho de Melo { 15636cbd3dcSJan Engelhardt static const char *const dccp_packet_names[] = { 1577c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1587c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1597c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1607c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1617c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1627c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1637c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1647c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1657c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1667c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1677c657876SArnaldo Carvalho de Melo }; 1687c657876SArnaldo Carvalho de Melo 1697c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1707c657876SArnaldo Carvalho de Melo return "INVALID"; 1717c657876SArnaldo Carvalho de Melo else 1727c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1737c657876SArnaldo Carvalho de Melo } 1747c657876SArnaldo Carvalho de Melo 1757c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1767c657876SArnaldo Carvalho de Melo 177120e9dabSEric Dumazet static void dccp_sk_destruct(struct sock *sk) 178120e9dabSEric Dumazet { 179120e9dabSEric Dumazet struct dccp_sock *dp = dccp_sk(sk); 180120e9dabSEric Dumazet 181120e9dabSEric Dumazet ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 182120e9dabSEric Dumazet dp->dccps_hc_tx_ccid = NULL; 183120e9dabSEric Dumazet inet_sock_destruct(sk); 184120e9dabSEric Dumazet } 185120e9dabSEric Dumazet 18672478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1873e0fadc5SArnaldo Carvalho de Melo { 1883e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1893e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1903e0fadc5SArnaldo Carvalho de Melo 191e18d7a98SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 192e18d7a98SArnaldo Carvalho de Melo icsk->icsk_syn_retries = sysctl_dccp_request_retries; 193e18d7a98SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 194e18d7a98SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 195120e9dabSEric Dumazet sk->sk_destruct = dccp_sk_destruct; 196e18d7a98SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 197410e27a4SGerrit Renker dp->dccps_mss_cache = 536; 198e18d7a98SArnaldo Carvalho de Melo dp->dccps_rate_last = jiffies; 199e18d7a98SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 200e18d7a98SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 201871a2c16STomasz Grobelny dp->dccps_tx_qlen = sysctl_dccp_tx_qlen; 202e18d7a98SArnaldo Carvalho de Melo 203e18d7a98SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 204e18d7a98SArnaldo Carvalho de Melo 205ac75773cSGerrit Renker INIT_LIST_HEAD(&dp->dccps_featneg); 2063e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 2076eb55d17SGerrit Renker if (likely(ctl_sock_initialized)) 2086eb55d17SGerrit Renker return dccp_feat_init(sk); 2093e0fadc5SArnaldo Carvalho de Melo return 0; 2103e0fadc5SArnaldo Carvalho de Melo } 2113e0fadc5SArnaldo Carvalho de Melo 2123e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2133e0fadc5SArnaldo Carvalho de Melo 2147d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk) 2153e0fadc5SArnaldo Carvalho de Melo { 2163e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2173e0fadc5SArnaldo Carvalho de Melo 2187749d4ffSEric Dumazet __skb_queue_purge(&sk->sk_write_queue); 2193e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2203e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2213e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2223e0fadc5SArnaldo Carvalho de Melo } 2233e0fadc5SArnaldo Carvalho de Melo 2243e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2253e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 226ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 2273e0fadc5SArnaldo Carvalho de Melo 2283e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2293e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2303e0fadc5SArnaldo Carvalho de Melo 2316fdd34d4SGerrit Renker if (dp->dccps_hc_rx_ackvec != NULL) { 2323e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2333e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2343e0fadc5SArnaldo Carvalho de Melo } 2353e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 236120e9dabSEric Dumazet dp->dccps_hc_rx_ccid = NULL; 2373e0fadc5SArnaldo Carvalho de Melo 2383e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 239d99a7bd2SGerrit Renker dccp_feat_list_purge(&dp->dccps_featneg); 2403e0fadc5SArnaldo Carvalho de Melo } 2413e0fadc5SArnaldo Carvalho de Melo 2423e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2433e0fadc5SArnaldo Carvalho de Melo 24472a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2457c657876SArnaldo Carvalho de Melo { 24667e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 24767e6b629SArnaldo Carvalho de Melo 24867e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 2499eca0a47SGerrit Renker /* do not start to listen if feature negotiation setup fails */ 2509eca0a47SGerrit Renker if (dccp_feat_finalise_settings(dp)) 2519eca0a47SGerrit Renker return -EPROTO; 25272a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2537c657876SArnaldo Carvalho de Melo } 2547c657876SArnaldo Carvalho de Melo 255ce865a61SGerrit Renker static inline int dccp_need_reset(int state) 256ce865a61SGerrit Renker { 257ce865a61SGerrit Renker return state != DCCP_CLOSED && state != DCCP_LISTEN && 258ce865a61SGerrit Renker state != DCCP_REQUESTING; 259ce865a61SGerrit Renker } 260ce865a61SGerrit Renker 2617c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2627c657876SArnaldo Carvalho de Melo { 2637c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2647c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 26569c64866SMohamed Ghannam struct dccp_sock *dp = dccp_sk(sk); 2667c657876SArnaldo Carvalho de Melo int err = 0; 2677c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2687c657876SArnaldo Carvalho de Melo 2697c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2707c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2717c657876SArnaldo Carvalho de Melo 272ce865a61SGerrit Renker /* 273ce865a61SGerrit Renker * This corresponds to the ABORT function of RFC793, sec. 3.8 274ce865a61SGerrit Renker * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted". 275ce865a61SGerrit Renker */ 2767c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2777c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 278ce865a61SGerrit Renker } else if (dccp_need_reset(old_state)) { 279ce865a61SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 280ce865a61SGerrit Renker sk->sk_err = ECONNRESET; 2817c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2827c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2837c657876SArnaldo Carvalho de Melo 2847c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 28569c64866SMohamed Ghannam ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 28669c64866SMohamed Ghannam dp->dccps_hc_rx_ccid = NULL; 28748816322SGerrit Renker 2887c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 28948816322SGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 2907c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2917c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2927c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2937c657876SArnaldo Carvalho de Melo } 2947c657876SArnaldo Carvalho de Melo 295c720c7e8SEric Dumazet inet->inet_dport = 0; 2967c657876SArnaldo Carvalho de Melo 2977c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 2987c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 2997c657876SArnaldo Carvalho de Melo 3007c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 3017c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 3027c657876SArnaldo Carvalho de Melo 3037c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3047c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 3057c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 3067c657876SArnaldo Carvalho de Melo 307c720c7e8SEric Dumazet WARN_ON(inet->inet_num && !icsk->icsk_bind_hash); 3087c657876SArnaldo Carvalho de Melo 3097c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 3107c657876SArnaldo Carvalho de Melo return err; 3117c657876SArnaldo Carvalho de Melo } 3127c657876SArnaldo Carvalho de Melo 313f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 314f21e68caSArnaldo Carvalho de Melo 315a11e1d43SLinus Torvalds /* 316a11e1d43SLinus Torvalds * Wait for a DCCP event. 317a11e1d43SLinus Torvalds * 318a11e1d43SLinus Torvalds * Note that we don't need to lock the socket, as the upper poll layers 319a11e1d43SLinus Torvalds * take care of normal races (between the test and the event) and we don't 320a11e1d43SLinus Torvalds * go look at any of the socket buffers directly. 321a11e1d43SLinus Torvalds */ 322a11e1d43SLinus Torvalds __poll_t dccp_poll(struct file *file, struct socket *sock, 323a11e1d43SLinus Torvalds poll_table *wait) 324331968bdSArnaldo Carvalho de Melo { 325ade994f4SAl Viro __poll_t mask; 326331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 327331968bdSArnaldo Carvalho de Melo 328a11e1d43SLinus Torvalds sock_poll_wait(file, sk_sleep(sk), wait); 329331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 330331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 331331968bdSArnaldo Carvalho de Melo 332331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 333331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 334331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 335331968bdSArnaldo Carvalho de Melo */ 336331968bdSArnaldo Carvalho de Melo 337331968bdSArnaldo Carvalho de Melo mask = 0; 338331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 339a9a08845SLinus Torvalds mask = EPOLLERR; 340331968bdSArnaldo Carvalho de Melo 341331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 342a9a08845SLinus Torvalds mask |= EPOLLHUP; 343331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 344a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 345331968bdSArnaldo Carvalho de Melo 346331968bdSArnaldo Carvalho de Melo /* Connected? */ 347331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 348331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 349a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 350331968bdSArnaldo Carvalho de Melo 351331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 35264dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) { 353a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 354331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 3559cd3e072SEric Dumazet sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 356331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 357331968bdSArnaldo Carvalho de Melo 358331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 359331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 360331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 361331968bdSArnaldo Carvalho de Melo */ 36264dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 363a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 364331968bdSArnaldo Carvalho de Melo } 365331968bdSArnaldo Carvalho de Melo } 366331968bdSArnaldo Carvalho de Melo } 367331968bdSArnaldo Carvalho de Melo return mask; 368331968bdSArnaldo Carvalho de Melo } 369331968bdSArnaldo Carvalho de Melo 370a11e1d43SLinus Torvalds EXPORT_SYMBOL_GPL(dccp_poll); 371f21e68caSArnaldo Carvalho de Melo 3727c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3737c657876SArnaldo Carvalho de Melo { 3746273172eSArnaldo Carvalho de Melo int rc = -ENOTCONN; 3756273172eSArnaldo Carvalho de Melo 3766273172eSArnaldo Carvalho de Melo lock_sock(sk); 3776273172eSArnaldo Carvalho de Melo 3786273172eSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 3796273172eSArnaldo Carvalho de Melo goto out; 3806273172eSArnaldo Carvalho de Melo 3816273172eSArnaldo Carvalho de Melo switch (cmd) { 3826273172eSArnaldo Carvalho de Melo case SIOCINQ: { 3836273172eSArnaldo Carvalho de Melo struct sk_buff *skb; 3846273172eSArnaldo Carvalho de Melo unsigned long amount = 0; 3856273172eSArnaldo Carvalho de Melo 3866273172eSArnaldo Carvalho de Melo skb = skb_peek(&sk->sk_receive_queue); 3876273172eSArnaldo Carvalho de Melo if (skb != NULL) { 3886273172eSArnaldo Carvalho de Melo /* 3896273172eSArnaldo Carvalho de Melo * We will only return the amount of this packet since 3906273172eSArnaldo Carvalho de Melo * that is all that will be read. 3916273172eSArnaldo Carvalho de Melo */ 3926273172eSArnaldo Carvalho de Melo amount = skb->len; 3936273172eSArnaldo Carvalho de Melo } 3946273172eSArnaldo Carvalho de Melo rc = put_user(amount, (int __user *)arg); 3956273172eSArnaldo Carvalho de Melo } 3966273172eSArnaldo Carvalho de Melo break; 3976273172eSArnaldo Carvalho de Melo default: 3986273172eSArnaldo Carvalho de Melo rc = -ENOIOCTLCMD; 3996273172eSArnaldo Carvalho de Melo break; 4006273172eSArnaldo Carvalho de Melo } 4016273172eSArnaldo Carvalho de Melo out: 4026273172eSArnaldo Carvalho de Melo release_sock(sk); 4036273172eSArnaldo Carvalho de Melo return rc; 4047c657876SArnaldo Carvalho de Melo } 4057c657876SArnaldo Carvalho de Melo 406f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 407f21e68caSArnaldo Carvalho de Melo 40860fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 409b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 41067e6b629SArnaldo Carvalho de Melo { 41167e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 41267e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 41367e6b629SArnaldo Carvalho de Melo 41467e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 41567e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 41667e6b629SArnaldo Carvalho de Melo return -EINVAL; 41767e6b629SArnaldo Carvalho de Melo 41867e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 41967e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 42067e6b629SArnaldo Carvalho de Melo if (sl == NULL) 42167e6b629SArnaldo Carvalho de Melo return -ENOMEM; 42267e6b629SArnaldo Carvalho de Melo 42367e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 42467e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 42567e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 42667e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 42767e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 42867e6b629SArnaldo Carvalho de Melo kfree(sl); 42967e6b629SArnaldo Carvalho de Melo return -EFAULT; 43067e6b629SArnaldo Carvalho de Melo } 43167e6b629SArnaldo Carvalho de Melo } 43267e6b629SArnaldo Carvalho de Melo 43367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 43467e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 43567e6b629SArnaldo Carvalho de Melo 43667e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 43767e6b629SArnaldo Carvalho de Melo 43867e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 43967e6b629SArnaldo Carvalho de Melo release_sock(sk); 44067e6b629SArnaldo Carvalho de Melo return 0; 44167e6b629SArnaldo Carvalho de Melo } 44267e6b629SArnaldo Carvalho de Melo 44329450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx) 44429450559SGerrit Renker { 44529450559SGerrit Renker u8 *list, len; 44629450559SGerrit Renker int i, rc; 44729450559SGerrit Renker 44829450559SGerrit Renker if (cscov < 0 || cscov > 15) 44929450559SGerrit Renker return -EINVAL; 45029450559SGerrit Renker /* 45129450559SGerrit Renker * Populate a list of permissible values, in the range cscov...15. This 45229450559SGerrit Renker * is necessary since feature negotiation of single values only works if 45329450559SGerrit Renker * both sides incidentally choose the same value. Since the list starts 45429450559SGerrit Renker * lowest-value first, negotiation will pick the smallest shared value. 45529450559SGerrit Renker */ 45629450559SGerrit Renker if (cscov == 0) 45729450559SGerrit Renker return 0; 45829450559SGerrit Renker len = 16 - cscov; 45929450559SGerrit Renker 46029450559SGerrit Renker list = kmalloc(len, GFP_KERNEL); 46129450559SGerrit Renker if (list == NULL) 46229450559SGerrit Renker return -ENOBUFS; 46329450559SGerrit Renker 46429450559SGerrit Renker for (i = 0; i < len; i++) 46529450559SGerrit Renker list[i] = cscov++; 46629450559SGerrit Renker 46729450559SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len); 46829450559SGerrit Renker 46929450559SGerrit Renker if (rc == 0) { 47029450559SGerrit Renker if (rx) 47129450559SGerrit Renker dccp_sk(sk)->dccps_pcrlen = cscov; 47229450559SGerrit Renker else 47329450559SGerrit Renker dccp_sk(sk)->dccps_pcslen = cscov; 47429450559SGerrit Renker } 47529450559SGerrit Renker kfree(list); 47629450559SGerrit Renker return rc; 47729450559SGerrit Renker } 47829450559SGerrit Renker 479b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type, 480b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 481b20a9c24SGerrit Renker { 482b20a9c24SGerrit Renker u8 *val; 483b20a9c24SGerrit Renker int rc = 0; 484b20a9c24SGerrit Renker 485b20a9c24SGerrit Renker if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS) 486b20a9c24SGerrit Renker return -EINVAL; 487b20a9c24SGerrit Renker 488042604d2SJulia Lawall val = memdup_user(optval, optlen); 489042604d2SJulia Lawall if (IS_ERR(val)) 490042604d2SJulia Lawall return PTR_ERR(val); 491b20a9c24SGerrit Renker 492b20a9c24SGerrit Renker lock_sock(sk); 493b20a9c24SGerrit Renker if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID) 494b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen); 495b20a9c24SGerrit Renker 496b20a9c24SGerrit Renker if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID)) 497b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen); 498b20a9c24SGerrit Renker release_sock(sk); 499b20a9c24SGerrit Renker 500b20a9c24SGerrit Renker kfree(val); 501b20a9c24SGerrit Renker return rc; 502b20a9c24SGerrit Renker } 503b20a9c24SGerrit Renker 5043fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 505b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 5067c657876SArnaldo Carvalho de Melo { 50709dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 50809dbc389SGerrit Renker int val, err = 0; 5097c657876SArnaldo Carvalho de Melo 51019102996SGerrit Renker switch (optname) { 51119102996SGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE: 51219102996SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 51319102996SGerrit Renker return 0; 51419102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_L: 51519102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_R: 51619102996SGerrit Renker DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n"); 51719102996SGerrit Renker return 0; 518b20a9c24SGerrit Renker case DCCP_SOCKOPT_CCID: 519b20a9c24SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 520b20a9c24SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 521b20a9c24SGerrit Renker return dccp_setsockopt_ccid(sk, optname, optval, optlen); 52219102996SGerrit Renker } 52319102996SGerrit Renker 52419102996SGerrit Renker if (optlen < (int)sizeof(int)) 525a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 526a84ffe43SArnaldo Carvalho de Melo 527a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 528a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 529a84ffe43SArnaldo Carvalho de Melo 53067e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 53167e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 532a84ffe43SArnaldo Carvalho de Melo 53367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 534a84ffe43SArnaldo Carvalho de Melo switch (optname) { 535b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 536b8599d20SGerrit Renker if (dp->dccps_role != DCCP_ROLE_SERVER) 537b8599d20SGerrit Renker err = -EOPNOTSUPP; 538b8599d20SGerrit Renker else 539b8599d20SGerrit Renker dp->dccps_server_timewait = (val != 0); 540b8599d20SGerrit Renker break; 54129450559SGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 54229450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, false); 543d6da3511STomasz Grobelny break; 54429450559SGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 54529450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, true); 546d6da3511STomasz Grobelny break; 547871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 548871a2c16STomasz Grobelny if (sk->sk_state != DCCP_CLOSED) 549871a2c16STomasz Grobelny err = -EISCONN; 550871a2c16STomasz Grobelny else if (val < 0 || val >= DCCPQ_POLICY_MAX) 551871a2c16STomasz Grobelny err = -EINVAL; 552871a2c16STomasz Grobelny else 553871a2c16STomasz Grobelny dp->dccps_qpolicy = val; 554871a2c16STomasz Grobelny break; 555871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 556871a2c16STomasz Grobelny if (val < 0) 557871a2c16STomasz Grobelny err = -EINVAL; 558871a2c16STomasz Grobelny else 559871a2c16STomasz Grobelny dp->dccps_tx_qlen = val; 560871a2c16STomasz Grobelny break; 561a84ffe43SArnaldo Carvalho de Melo default: 562a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 563a84ffe43SArnaldo Carvalho de Melo break; 564a84ffe43SArnaldo Carvalho de Melo } 565410e27a4SGerrit Renker release_sock(sk); 56619102996SGerrit Renker 567a84ffe43SArnaldo Carvalho de Melo return err; 5687c657876SArnaldo Carvalho de Melo } 5697c657876SArnaldo Carvalho de Melo 5703fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 571b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 5723fdadf7dSDmitry Mishin { 5733fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5743fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5753fdadf7dSDmitry Mishin optname, optval, 5763fdadf7dSDmitry Mishin optlen); 5773fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5783fdadf7dSDmitry Mishin } 579543d9cfeSArnaldo Carvalho de Melo 580f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 581f21e68caSArnaldo Carvalho de Melo 5823fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5833fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 584b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 5853fdadf7dSDmitry Mishin { 586dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 587dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 588dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5893fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5903fdadf7dSDmitry Mishin } 591543d9cfeSArnaldo Carvalho de Melo 5923fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5933fdadf7dSDmitry Mishin #endif 5943fdadf7dSDmitry Mishin 59567e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 59660fe62e7SAndrea Bittau __be32 __user *optval, 59767e6b629SArnaldo Carvalho de Melo int __user *optlen) 59867e6b629SArnaldo Carvalho de Melo { 59967e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 60067e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 60167e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 60267e6b629SArnaldo Carvalho de Melo 60367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 60467e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 60567e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 60667e6b629SArnaldo Carvalho de Melo total_len += slen; 60767e6b629SArnaldo Carvalho de Melo } 60867e6b629SArnaldo Carvalho de Melo 60967e6b629SArnaldo Carvalho de Melo err = -EINVAL; 61067e6b629SArnaldo Carvalho de Melo if (total_len > len) 61167e6b629SArnaldo Carvalho de Melo goto out; 61267e6b629SArnaldo Carvalho de Melo 61367e6b629SArnaldo Carvalho de Melo err = 0; 61467e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 61567e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 61667e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 61767e6b629SArnaldo Carvalho de Melo err = -EFAULT; 61867e6b629SArnaldo Carvalho de Melo out: 61967e6b629SArnaldo Carvalho de Melo release_sock(sk); 62067e6b629SArnaldo Carvalho de Melo return err; 62167e6b629SArnaldo Carvalho de Melo } 62267e6b629SArnaldo Carvalho de Melo 6233fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 624a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 6257c657876SArnaldo Carvalho de Melo { 626a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 627a84ffe43SArnaldo Carvalho de Melo int val, len; 6287c657876SArnaldo Carvalho de Melo 629a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 630a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 631a84ffe43SArnaldo Carvalho de Melo 63239ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 633a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 634a84ffe43SArnaldo Carvalho de Melo 635a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 636a84ffe43SArnaldo Carvalho de Melo 637a84ffe43SArnaldo Carvalho de Melo switch (optname) { 638a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 6395aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 640841bac1dSArnaldo Carvalho de Melo return 0; 64188f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 64288f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 64360fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 6447c559a9eSGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS: 6457c559a9eSGerrit Renker val = dp->dccps_mss_cache; 6467c559a9eSGerrit Renker break; 647d90ebcbfSGerrit Renker case DCCP_SOCKOPT_AVAILABLE_CCIDS: 648d90ebcbfSGerrit Renker return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen); 64971c262a3SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 65071c262a3SGerrit Renker val = ccid_get_current_tx_ccid(dp); 65171c262a3SGerrit Renker if (val < 0) 65271c262a3SGerrit Renker return -ENOPROTOOPT; 65371c262a3SGerrit Renker break; 65471c262a3SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 65571c262a3SGerrit Renker val = ccid_get_current_rx_ccid(dp); 65671c262a3SGerrit Renker if (val < 0) 65771c262a3SGerrit Renker return -ENOPROTOOPT; 65871c262a3SGerrit Renker break; 659b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 660b8599d20SGerrit Renker val = dp->dccps_server_timewait; 661b8599d20SGerrit Renker break; 6626f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 6636f4e5fffSGerrit Renker val = dp->dccps_pcslen; 6646f4e5fffSGerrit Renker break; 6656f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 6666f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 6676f4e5fffSGerrit Renker break; 668871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 669871a2c16STomasz Grobelny val = dp->dccps_qpolicy; 670871a2c16STomasz Grobelny break; 671871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 672871a2c16STomasz Grobelny val = dp->dccps_tx_qlen; 673871a2c16STomasz Grobelny break; 67488f964dbSArnaldo Carvalho de Melo case 128 ... 191: 67588f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 67688f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 67788f964dbSArnaldo Carvalho de Melo case 192 ... 255: 67888f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 67988f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 680a84ffe43SArnaldo Carvalho de Melo default: 681a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 682a84ffe43SArnaldo Carvalho de Melo } 683a84ffe43SArnaldo Carvalho de Melo 68479133506SGerrit Renker len = sizeof(val); 685a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 686a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 687a84ffe43SArnaldo Carvalho de Melo 688a84ffe43SArnaldo Carvalho de Melo return 0; 6897c657876SArnaldo Carvalho de Melo } 6907c657876SArnaldo Carvalho de Melo 6913fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6923fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6933fdadf7dSDmitry Mishin { 6943fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6953fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6963fdadf7dSDmitry Mishin optname, optval, 6973fdadf7dSDmitry Mishin optlen); 6983fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6993fdadf7dSDmitry Mishin } 700543d9cfeSArnaldo Carvalho de Melo 701f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 702f21e68caSArnaldo Carvalho de Melo 7033fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7043fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 7053fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7063fdadf7dSDmitry Mishin { 707dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 708dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 709dec73ff0SArnaldo Carvalho de Melo optval, optlen); 7103fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 7113fdadf7dSDmitry Mishin } 712543d9cfeSArnaldo Carvalho de Melo 7133fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 7143fdadf7dSDmitry Mishin #endif 7153fdadf7dSDmitry Mishin 716871a2c16STomasz Grobelny static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb) 717871a2c16STomasz Grobelny { 718f95b414eSGu Zheng struct cmsghdr *cmsg; 719871a2c16STomasz Grobelny 720871a2c16STomasz Grobelny /* 721871a2c16STomasz Grobelny * Assign an (opaque) qpolicy priority value to skb->priority. 722871a2c16STomasz Grobelny * 723871a2c16STomasz Grobelny * We are overloading this skb field for use with the qpolicy subystem. 724871a2c16STomasz Grobelny * The skb->priority is normally used for the SO_PRIORITY option, which 725871a2c16STomasz Grobelny * is initialised from sk_priority. Since the assignment of sk_priority 726871a2c16STomasz Grobelny * to skb->priority happens later (on layer 3), we overload this field 727871a2c16STomasz Grobelny * for use with queueing priorities as long as the skb is on layer 4. 728871a2c16STomasz Grobelny * The default priority value (if nothing is set) is 0. 729871a2c16STomasz Grobelny */ 730871a2c16STomasz Grobelny skb->priority = 0; 731871a2c16STomasz Grobelny 732f95b414eSGu Zheng for_each_cmsghdr(cmsg, msg) { 733871a2c16STomasz Grobelny if (!CMSG_OK(msg, cmsg)) 734871a2c16STomasz Grobelny return -EINVAL; 735871a2c16STomasz Grobelny 736871a2c16STomasz Grobelny if (cmsg->cmsg_level != SOL_DCCP) 737871a2c16STomasz Grobelny continue; 738871a2c16STomasz Grobelny 73904910265STomasz Grobelny if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX && 74004910265STomasz Grobelny !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type)) 74104910265STomasz Grobelny return -EINVAL; 74204910265STomasz Grobelny 743871a2c16STomasz Grobelny switch (cmsg->cmsg_type) { 744871a2c16STomasz Grobelny case DCCP_SCM_PRIORITY: 745871a2c16STomasz Grobelny if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32))) 746871a2c16STomasz Grobelny return -EINVAL; 747871a2c16STomasz Grobelny skb->priority = *(__u32 *)CMSG_DATA(cmsg); 748871a2c16STomasz Grobelny break; 749871a2c16STomasz Grobelny default: 750871a2c16STomasz Grobelny return -EINVAL; 751871a2c16STomasz Grobelny } 752871a2c16STomasz Grobelny } 753871a2c16STomasz Grobelny return 0; 754871a2c16STomasz Grobelny } 755871a2c16STomasz Grobelny 7561b784140SYing Xue int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 7577c657876SArnaldo Carvalho de Melo { 7587c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 7597c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 7607c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 7617c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7627c657876SArnaldo Carvalho de Melo int rc, size; 7637c657876SArnaldo Carvalho de Melo long timeo; 7647c657876SArnaldo Carvalho de Melo 765ee549be6SMasami Hiramatsu trace_dccp_probe(sk, len); 766ee549be6SMasami Hiramatsu 7677c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 7687c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 7697c657876SArnaldo Carvalho de Melo 7707c657876SArnaldo Carvalho de Melo lock_sock(sk); 771b1308dc0SIan McDonald 772871a2c16STomasz Grobelny if (dccp_qpolicy_full(sk)) { 773b1308dc0SIan McDonald rc = -EAGAIN; 774b1308dc0SIan McDonald goto out_release; 775b1308dc0SIan McDonald } 776b1308dc0SIan McDonald 77727258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 7787c657876SArnaldo Carvalho de Melo 7797c657876SArnaldo Carvalho de Melo /* 7807c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 7817c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 7827c657876SArnaldo Carvalho de Melo */ 7837c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 784cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 7857c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 78627258ee5SArnaldo Carvalho de Melo goto out_release; 7877c657876SArnaldo Carvalho de Melo 7887c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 7897c657876SArnaldo Carvalho de Melo release_sock(sk); 7907c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 7917c657876SArnaldo Carvalho de Melo lock_sock(sk); 7927c657876SArnaldo Carvalho de Melo if (skb == NULL) 7937c657876SArnaldo Carvalho de Melo goto out_release; 7947c657876SArnaldo Carvalho de Melo 79567f93df7SAlexey Kodanev if (sk->sk_state == DCCP_CLOSED) { 79667f93df7SAlexey Kodanev rc = -ENOTCONN; 79767f93df7SAlexey Kodanev goto out_discard; 79867f93df7SAlexey Kodanev } 79967f93df7SAlexey Kodanev 8007c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 8016ce8e9ceSAl Viro rc = memcpy_from_msg(skb_put(skb, len), msg, len); 80227258ee5SArnaldo Carvalho de Melo if (rc != 0) 80327258ee5SArnaldo Carvalho de Melo goto out_discard; 8047c657876SArnaldo Carvalho de Melo 805871a2c16STomasz Grobelny rc = dccp_msghdr_parse(msg, skb); 806871a2c16STomasz Grobelny if (rc != 0) 807871a2c16STomasz Grobelny goto out_discard; 808871a2c16STomasz Grobelny 809871a2c16STomasz Grobelny dccp_qpolicy_push(sk, skb); 810b1fcf55eSGerrit Renker /* 811b1fcf55eSGerrit Renker * The xmit_timer is set if the TX CCID is rate-based and will expire 812b1fcf55eSGerrit Renker * when congestion control permits to release further packets into the 813b1fcf55eSGerrit Renker * network. Window-based CCIDs do not use this timer. 814b1fcf55eSGerrit Renker */ 815b1fcf55eSGerrit Renker if (!timer_pending(&dp->dccps_xmit_timer)) 816b1fcf55eSGerrit Renker dccp_write_xmit(sk); 8177c657876SArnaldo Carvalho de Melo out_release: 8187c657876SArnaldo Carvalho de Melo release_sock(sk); 8197c657876SArnaldo Carvalho de Melo return rc ? : len; 82027258ee5SArnaldo Carvalho de Melo out_discard: 82127258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 8227c657876SArnaldo Carvalho de Melo goto out_release; 8237c657876SArnaldo Carvalho de Melo } 8247c657876SArnaldo Carvalho de Melo 825f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 826f21e68caSArnaldo Carvalho de Melo 8271b784140SYing Xue int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, 8281b784140SYing Xue int flags, int *addr_len) 8297c657876SArnaldo Carvalho de Melo { 8307c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 8317c657876SArnaldo Carvalho de Melo long timeo; 8327c657876SArnaldo Carvalho de Melo 8337c657876SArnaldo Carvalho de Melo lock_sock(sk); 8347c657876SArnaldo Carvalho de Melo 835531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 836531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8377c657876SArnaldo Carvalho de Melo goto out; 838531669a0SArnaldo Carvalho de Melo } 8397c657876SArnaldo Carvalho de Melo 8407c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 8417c657876SArnaldo Carvalho de Melo 8427c657876SArnaldo Carvalho de Melo do { 843531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 8447c657876SArnaldo Carvalho de Melo 845531669a0SArnaldo Carvalho de Melo if (skb == NULL) 846531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 8477c657876SArnaldo Carvalho de Melo 8487c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 8497c657876SArnaldo Carvalho de Melo 8500c869620SGerrit Renker switch (dh->dccph_type) { 8510c869620SGerrit Renker case DCCP_PKT_DATA: 8520c869620SGerrit Renker case DCCP_PKT_DATAACK: 8537c657876SArnaldo Carvalho de Melo goto found_ok_skb; 8547c657876SArnaldo Carvalho de Melo 8550c869620SGerrit Renker case DCCP_PKT_CLOSE: 8560c869620SGerrit Renker case DCCP_PKT_CLOSEREQ: 8570c869620SGerrit Renker if (!(flags & MSG_PEEK)) 8580c869620SGerrit Renker dccp_finish_passive_close(sk); 8590c869620SGerrit Renker /* fall through */ 8600c869620SGerrit Renker case DCCP_PKT_RESET: 8610c869620SGerrit Renker dccp_pr_debug("found fin (%s) ok!\n", 8620c869620SGerrit Renker dccp_packet_name(dh->dccph_type)); 863531669a0SArnaldo Carvalho de Melo len = 0; 8647c657876SArnaldo Carvalho de Melo goto found_fin_ok; 8650c869620SGerrit Renker default: 8667690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 8677690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 8687bced397SDan Williams sk_eat_skb(sk, skb); 8690c869620SGerrit Renker } 870531669a0SArnaldo Carvalho de Melo verify_sock_status: 871531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 872531669a0SArnaldo Carvalho de Melo len = 0; 8737c657876SArnaldo Carvalho de Melo break; 8747c657876SArnaldo Carvalho de Melo } 8757c657876SArnaldo Carvalho de Melo 876531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 877531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 8787c657876SArnaldo Carvalho de Melo break; 879531669a0SArnaldo Carvalho de Melo } 880531669a0SArnaldo Carvalho de Melo 881531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 882531669a0SArnaldo Carvalho de Melo len = 0; 883531669a0SArnaldo Carvalho de Melo break; 884531669a0SArnaldo Carvalho de Melo } 8857c657876SArnaldo Carvalho de Melo 8867c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 8877c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 8887c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 8897c657876SArnaldo Carvalho de Melo * from never connected socket. 8907c657876SArnaldo Carvalho de Melo */ 891531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8927c657876SArnaldo Carvalho de Melo break; 8937c657876SArnaldo Carvalho de Melo } 894531669a0SArnaldo Carvalho de Melo len = 0; 8957c657876SArnaldo Carvalho de Melo break; 8967c657876SArnaldo Carvalho de Melo } 8977c657876SArnaldo Carvalho de Melo 8987c657876SArnaldo Carvalho de Melo if (!timeo) { 899531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 9007c657876SArnaldo Carvalho de Melo break; 9017c657876SArnaldo Carvalho de Melo } 9027c657876SArnaldo Carvalho de Melo 9037c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 904531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 9057c657876SArnaldo Carvalho de Melo break; 9067c657876SArnaldo Carvalho de Melo } 9077c657876SArnaldo Carvalho de Melo 908dfbafc99SSabrina Dubroca sk_wait_data(sk, &timeo, NULL); 9097c657876SArnaldo Carvalho de Melo continue; 9107c657876SArnaldo Carvalho de Melo found_ok_skb: 911531669a0SArnaldo Carvalho de Melo if (len > skb->len) 912531669a0SArnaldo Carvalho de Melo len = skb->len; 913531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 914531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 9157c657876SArnaldo Carvalho de Melo 91651f3d02bSDavid S. Miller if (skb_copy_datagram_msg(skb, 0, msg, len)) { 9177c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 918531669a0SArnaldo Carvalho de Melo len = -EFAULT; 9197c657876SArnaldo Carvalho de Melo break; 9207c657876SArnaldo Carvalho de Melo } 92155d95590SGerrit Renker if (flags & MSG_TRUNC) 92255d95590SGerrit Renker len = skb->len; 9237c657876SArnaldo Carvalho de Melo found_fin_ok: 9247c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 9257bced397SDan Williams sk_eat_skb(sk, skb); 9267c657876SArnaldo Carvalho de Melo break; 927531669a0SArnaldo Carvalho de Melo } while (1); 9287c657876SArnaldo Carvalho de Melo out: 9297c657876SArnaldo Carvalho de Melo release_sock(sk); 930531669a0SArnaldo Carvalho de Melo return len; 9317c657876SArnaldo Carvalho de Melo } 9327c657876SArnaldo Carvalho de Melo 933f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 934f21e68caSArnaldo Carvalho de Melo 935f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 9367c657876SArnaldo Carvalho de Melo { 9377c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 9387c657876SArnaldo Carvalho de Melo unsigned char old_state; 9397c657876SArnaldo Carvalho de Melo int err; 9407c657876SArnaldo Carvalho de Melo 9417c657876SArnaldo Carvalho de Melo lock_sock(sk); 9427c657876SArnaldo Carvalho de Melo 9437c657876SArnaldo Carvalho de Melo err = -EINVAL; 9447c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 9457c657876SArnaldo Carvalho de Melo goto out; 9467c657876SArnaldo Carvalho de Melo 9477c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 9487c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 9497c657876SArnaldo Carvalho de Melo goto out; 9507c657876SArnaldo Carvalho de Melo 9517c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 9527c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 9537c657876SArnaldo Carvalho de Melo */ 9547c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 9557c657876SArnaldo Carvalho de Melo /* 9567c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 9577c657876SArnaldo Carvalho de Melo * see tcp_listen_start 9587c657876SArnaldo Carvalho de Melo */ 95972a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 9607c657876SArnaldo Carvalho de Melo if (err) 9617c657876SArnaldo Carvalho de Melo goto out; 9627c657876SArnaldo Carvalho de Melo } 9637c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 9647c657876SArnaldo Carvalho de Melo err = 0; 9657c657876SArnaldo Carvalho de Melo 9667c657876SArnaldo Carvalho de Melo out: 9677c657876SArnaldo Carvalho de Melo release_sock(sk); 9687c657876SArnaldo Carvalho de Melo return err; 9697c657876SArnaldo Carvalho de Melo } 9707c657876SArnaldo Carvalho de Melo 971f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 972f21e68caSArnaldo Carvalho de Melo 9730c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk) 9747c657876SArnaldo Carvalho de Melo { 9750c869620SGerrit Renker u8 next_state = DCCP_CLOSED; 9767c657876SArnaldo Carvalho de Melo 9770c869620SGerrit Renker switch (sk->sk_state) { 9780c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 9790c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 9800c869620SGerrit Renker dccp_finish_passive_close(sk); 9810c869620SGerrit Renker break; 9820c869620SGerrit Renker case DCCP_PARTOPEN: 9830c869620SGerrit Renker dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk); 9840c869620SGerrit Renker inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 9850c869620SGerrit Renker /* fall through */ 9860c869620SGerrit Renker case DCCP_OPEN: 9870c869620SGerrit Renker dccp_send_close(sk, 1); 9887c657876SArnaldo Carvalho de Melo 989b8599d20SGerrit Renker if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER && 990b8599d20SGerrit Renker !dccp_sk(sk)->dccps_server_timewait) 9910c869620SGerrit Renker next_state = DCCP_ACTIVE_CLOSEREQ; 9920c869620SGerrit Renker else 9930c869620SGerrit Renker next_state = DCCP_CLOSING; 9940c869620SGerrit Renker /* fall through */ 9950c869620SGerrit Renker default: 9960c869620SGerrit Renker dccp_set_state(sk, next_state); 9970c869620SGerrit Renker } 9987c657876SArnaldo Carvalho de Melo } 9997c657876SArnaldo Carvalho de Melo 10007c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 10017c657876SArnaldo Carvalho de Melo { 100297e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 10037c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 1004d83bd95bSGerrit Renker u32 data_was_unread = 0; 1005134af346SHerbert Xu int state; 10067c657876SArnaldo Carvalho de Melo 10077c657876SArnaldo Carvalho de Melo lock_sock(sk); 10087c657876SArnaldo Carvalho de Melo 10097c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 10107c657876SArnaldo Carvalho de Melo 10117c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 10127c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 10137c657876SArnaldo Carvalho de Melo 10147c657876SArnaldo Carvalho de Melo /* Special case. */ 10157c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 10167c657876SArnaldo Carvalho de Melo 10177c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 10187c657876SArnaldo Carvalho de Melo } 10197c657876SArnaldo Carvalho de Melo 102097e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 102197e5848dSIan McDonald 10227c657876SArnaldo Carvalho de Melo /* 10237c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 10247c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 10257c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 10267c657876SArnaldo Carvalho de Melo */ 10277c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 1028d83bd95bSGerrit Renker data_was_unread += skb->len; 10297c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 10307c657876SArnaldo Carvalho de Melo } 10317c657876SArnaldo Carvalho de Melo 1032346da62cSEric Dumazet /* If socket has been already reset kill it. */ 1033346da62cSEric Dumazet if (sk->sk_state == DCCP_CLOSED) 1034346da62cSEric Dumazet goto adjudge_to_death; 1035346da62cSEric Dumazet 1036d83bd95bSGerrit Renker if (data_was_unread) { 1037d83bd95bSGerrit Renker /* Unread data was tossed, send an appropriate Reset Code */ 10382f34b329SGerrit Renker DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread); 1039d83bd95bSGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 1040d83bd95bSGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1041d83bd95bSGerrit Renker } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 10427c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 10437c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 10440c869620SGerrit Renker } else if (sk->sk_state != DCCP_CLOSED) { 1045b1fcf55eSGerrit Renker /* 1046b1fcf55eSGerrit Renker * Normal connection termination. May need to wait if there are 1047b1fcf55eSGerrit Renker * still packets in the TX queue that are delayed by the CCID. 1048b1fcf55eSGerrit Renker */ 1049b1fcf55eSGerrit Renker dccp_flush_write_queue(sk, &timeout); 10500c869620SGerrit Renker dccp_terminate_connection(sk); 10517c657876SArnaldo Carvalho de Melo } 10527c657876SArnaldo Carvalho de Melo 1053b1fcf55eSGerrit Renker /* 1054b1fcf55eSGerrit Renker * Flush write queue. This may be necessary in several cases: 1055b1fcf55eSGerrit Renker * - we have been closed by the peer but still have application data; 1056b1fcf55eSGerrit Renker * - abortive termination (unread data or zero linger time), 1057b1fcf55eSGerrit Renker * - normal termination but queue could not be flushed within time limit 1058b1fcf55eSGerrit Renker */ 1059b1fcf55eSGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 1060b1fcf55eSGerrit Renker 10617c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 10627c657876SArnaldo Carvalho de Melo 10637c657876SArnaldo Carvalho de Melo adjudge_to_death: 1064134af346SHerbert Xu state = sk->sk_state; 1065134af346SHerbert Xu sock_hold(sk); 1066134af346SHerbert Xu sock_orphan(sk); 1067134af346SHerbert Xu 10687ad07e7cSArnaldo Carvalho de Melo /* 10697ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 10707ad07e7cSArnaldo Carvalho de Melo */ 10717c657876SArnaldo Carvalho de Melo release_sock(sk); 10727c657876SArnaldo Carvalho de Melo /* 10737c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 10747c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 10757c657876SArnaldo Carvalho de Melo */ 10767c657876SArnaldo Carvalho de Melo local_bh_disable(); 10777c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 1078547b792cSIlpo Järvinen WARN_ON(sock_owned_by_user(sk)); 10797c657876SArnaldo Carvalho de Melo 1080eb4dea58SHerbert Xu percpu_counter_inc(sk->sk_prot->orphan_count); 1081eb4dea58SHerbert Xu 1082134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 1083134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 1084134af346SHerbert Xu goto out; 10857c657876SArnaldo Carvalho de Melo 10867c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 10877c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 10887c657876SArnaldo Carvalho de Melo 10897c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 10907c657876SArnaldo Carvalho de Melo 1091134af346SHerbert Xu out: 10927c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 10937c657876SArnaldo Carvalho de Melo local_bh_enable(); 10947c657876SArnaldo Carvalho de Melo sock_put(sk); 10957c657876SArnaldo Carvalho de Melo } 10967c657876SArnaldo Carvalho de Melo 1097f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 1098f21e68caSArnaldo Carvalho de Melo 10997c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 11007c657876SArnaldo Carvalho de Melo { 11018e8c71f1SGerrit Renker dccp_pr_debug("called shutdown(%x)\n", how); 11027c657876SArnaldo Carvalho de Melo } 11037c657876SArnaldo Carvalho de Melo 1104f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 1105f21e68caSArnaldo Carvalho de Melo 11060c5b8a46SFabian Frederick static inline int __init dccp_mib_init(void) 11077c657876SArnaldo Carvalho de Melo { 1108698365faSWANG Cong dccp_statistics = alloc_percpu(struct dccp_mib); 1109698365faSWANG Cong if (!dccp_statistics) 1110698365faSWANG Cong return -ENOMEM; 1111698365faSWANG Cong return 0; 11127c657876SArnaldo Carvalho de Melo } 11137c657876SArnaldo Carvalho de Melo 111424e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void) 111546f09ffaSArnaldo Carvalho de Melo { 1116698365faSWANG Cong free_percpu(dccp_statistics); 111746f09ffaSArnaldo Carvalho de Melo } 111846f09ffaSArnaldo Carvalho de Melo 11197c657876SArnaldo Carvalho de Melo static int thash_entries; 11207c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 11217c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 11227c657876SArnaldo Carvalho de Melo 1123a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 1124eb939922SRusty Russell bool dccp_debug; 112543264991SGerrit Renker module_param(dccp_debug, bool, 0644); 11267c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 1127f21e68caSArnaldo Carvalho de Melo 1128f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 1129a1d3a355SArnaldo Carvalho de Melo #endif 11307c657876SArnaldo Carvalho de Melo 11317c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 11327c657876SArnaldo Carvalho de Melo { 11337c657876SArnaldo Carvalho de Melo unsigned long goal; 11347c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1135dd24c001SEric Dumazet int rc; 11367c657876SArnaldo Carvalho de Melo 1137028b0275SPatrick McHardy BUILD_BUG_ON(sizeof(struct dccp_skb_cb) > 1138028b0275SPatrick McHardy FIELD_SIZEOF(struct sk_buff, cb)); 1139908c7f19STejun Heo rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL); 1140dd24c001SEric Dumazet if (rc) 1141d14a0ebdSGerrit Renker goto out_fail; 1142dd24c001SEric Dumazet rc = -ENOBUFS; 11435caea4eaSEric Dumazet inet_hashinfo_init(&dccp_hashinfo); 11447690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 11457690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 11467690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 114720c2df83SPaul Mundt SLAB_HWCACHE_ALIGN, NULL); 11487c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1149dd24c001SEric Dumazet goto out_free_percpu; 11507c657876SArnaldo Carvalho de Melo 11517c657876SArnaldo Carvalho de Melo /* 11527c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 11537c657876SArnaldo Carvalho de Melo * hash tables. 11547c657876SArnaldo Carvalho de Melo * 11557c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 11567c657876SArnaldo Carvalho de Melo */ 11574481374cSJan Beulich if (totalram_pages >= (128 * 1024)) 11584481374cSJan Beulich goal = totalram_pages >> (21 - PAGE_SHIFT); 11597c657876SArnaldo Carvalho de Melo else 11604481374cSJan Beulich goal = totalram_pages >> (23 - PAGE_SHIFT); 11617c657876SArnaldo Carvalho de Melo 11627c657876SArnaldo Carvalho de Melo if (thash_entries) 11637690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 11647690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 11657c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 11667c657876SArnaldo Carvalho de Melo ; 11677c657876SArnaldo Carvalho de Melo do { 1168f373b53bSEric Dumazet unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE / 11697c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 1170f373b53bSEric Dumazet 1171f373b53bSEric Dumazet while (hash_size & (hash_size - 1)) 1172f373b53bSEric Dumazet hash_size--; 1173f373b53bSEric Dumazet dccp_hashinfo.ehash_mask = hash_size - 1; 11747c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 11751c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order); 11767c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 11777c657876SArnaldo Carvalho de Melo 11787c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 117959348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 11807c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 11817c657876SArnaldo Carvalho de Melo } 11827c657876SArnaldo Carvalho de Melo 118305dbc7b5SEric Dumazet for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) 11843ab5aee7SEric Dumazet INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i); 11857c657876SArnaldo Carvalho de Melo 1186230140cfSEric Dumazet if (inet_ehash_locks_alloc(&dccp_hashinfo)) 1187230140cfSEric Dumazet goto out_free_dccp_ehash; 1188230140cfSEric Dumazet 11897c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 11907c657876SArnaldo Carvalho de Melo 11917c657876SArnaldo Carvalho de Melo do { 11927c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 11937c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 11947690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 11957690af3fSArnaldo Carvalho de Melo bhash_order > 0) 11967c657876SArnaldo Carvalho de Melo continue; 11977c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 11981c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order); 11997c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 12007c657876SArnaldo Carvalho de Melo 12017c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 120259348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 1203230140cfSEric Dumazet goto out_free_dccp_locks; 12047c657876SArnaldo Carvalho de Melo } 12057c657876SArnaldo Carvalho de Melo 12067c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 12077c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 12087c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 12097c657876SArnaldo Carvalho de Melo } 12107c657876SArnaldo Carvalho de Melo 121146f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1212fa23e2ecSArnaldo Carvalho de Melo if (rc) 12137c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 12147c657876SArnaldo Carvalho de Melo 12159b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 12167c657876SArnaldo Carvalho de Melo if (rc) 1217b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 12189b07ef5dSArnaldo Carvalho de Melo 1219e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 12209b07ef5dSArnaldo Carvalho de Melo if (rc) 12219b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 12224c70f383SGerrit Renker 1223ddebc973SGerrit Renker rc = ccid_initialize_builtins(); 1224ddebc973SGerrit Renker if (rc) 1225ddebc973SGerrit Renker goto out_sysctl_exit; 1226ddebc973SGerrit Renker 12274c70f383SGerrit Renker dccp_timestamping_init(); 1228d14a0ebdSGerrit Renker 1229d14a0ebdSGerrit Renker return 0; 1230d14a0ebdSGerrit Renker 1231ddebc973SGerrit Renker out_sysctl_exit: 1232ddebc973SGerrit Renker dccp_sysctl_exit(); 12339b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 12349b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1235b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 123646f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 12377c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 12387c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 1239230140cfSEric Dumazet out_free_dccp_locks: 1240230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12417c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 12427c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 12437c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 12447c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 1245dd24c001SEric Dumazet out_free_percpu: 1246dd24c001SEric Dumazet percpu_counter_destroy(&dccp_orphan_count); 1247d14a0ebdSGerrit Renker out_fail: 1248d14a0ebdSGerrit Renker dccp_hashinfo.bhash = NULL; 1249d14a0ebdSGerrit Renker dccp_hashinfo.ehash = NULL; 1250d14a0ebdSGerrit Renker dccp_hashinfo.bind_bucket_cachep = NULL; 1251d14a0ebdSGerrit Renker return rc; 12527c657876SArnaldo Carvalho de Melo } 12537c657876SArnaldo Carvalho de Melo 12547c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 12557c657876SArnaldo Carvalho de Melo { 1256ddebc973SGerrit Renker ccid_cleanup_builtins(); 125746f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1258725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1259725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1260725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1261725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1262f373b53bSEric Dumazet get_order((dccp_hashinfo.ehash_mask + 1) * 1263725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 1264230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12657c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 12669b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1267e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 1268476181cbSWei Yongjun percpu_counter_destroy(&dccp_orphan_count); 12697c657876SArnaldo Carvalho de Melo } 12707c657876SArnaldo Carvalho de Melo 12717c657876SArnaldo Carvalho de Melo module_init(dccp_init); 12727c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 12737c657876SArnaldo Carvalho de Melo 12747c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 12757c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 12767c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1277