1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27c657876SArnaldo Carvalho de Melo /* 37c657876SArnaldo Carvalho de Melo * net/dccp/proto.c 47c657876SArnaldo Carvalho de Melo * 57c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 67c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 77c657876SArnaldo Carvalho de Melo */ 87c657876SArnaldo Carvalho de Melo 97c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 107c657876SArnaldo Carvalho de Melo #include <linux/module.h> 117c657876SArnaldo Carvalho de Melo #include <linux/types.h> 127c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 137c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 147c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 157c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 167c657876SArnaldo Carvalho de Melo #include <linux/in.h> 177c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 187c657876SArnaldo Carvalho de Melo #include <linux/init.h> 197c657876SArnaldo Carvalho de Melo #include <linux/random.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 217c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 227c657876SArnaldo Carvalho de Melo 2314c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 24120e9dabSEric Dumazet #include <net/inet_common.h> 257c657876SArnaldo Carvalho de Melo #include <net/sock.h> 267c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 277c657876SArnaldo Carvalho de Melo 286273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h> 297c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 307c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 317c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 327c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 337c657876SArnaldo Carvalho de Melo 347c657876SArnaldo Carvalho de Melo #include "ccid.h" 357c657876SArnaldo Carvalho de Melo #include "dccp.h" 36afe00251SAndrea Bittau #include "feat.h" 377c657876SArnaldo Carvalho de Melo 38ee549be6SMasami Hiramatsu #define CREATE_TRACE_POINTS 39ee549be6SMasami Hiramatsu #include "trace.h" 40ee549be6SMasami Hiramatsu 41ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 427c657876SArnaldo Carvalho de Melo 43f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 44f21e68caSArnaldo Carvalho de Melo 4519757cebSEric Dumazet DEFINE_PER_CPU(unsigned int, dccp_orphan_count); 4619757cebSEric Dumazet EXPORT_PER_CPU_SYMBOL_GPL(dccp_orphan_count); 47f21e68caSArnaldo Carvalho de Melo 485caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo; 49075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 50075ae866SArnaldo Carvalho de Melo 51b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 52b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 53b1308dc0SIan McDonald 541f4f0f64Sstephen hemminger #ifdef CONFIG_IP_DCCP_DEBUG 551f4f0f64Sstephen hemminger static const char *dccp_state_name(const int state) 561f4f0f64Sstephen hemminger { 571f4f0f64Sstephen hemminger static const char *const dccp_state_names[] = { 581f4f0f64Sstephen hemminger [DCCP_OPEN] = "OPEN", 591f4f0f64Sstephen hemminger [DCCP_REQUESTING] = "REQUESTING", 601f4f0f64Sstephen hemminger [DCCP_PARTOPEN] = "PARTOPEN", 611f4f0f64Sstephen hemminger [DCCP_LISTEN] = "LISTEN", 621f4f0f64Sstephen hemminger [DCCP_RESPOND] = "RESPOND", 631f4f0f64Sstephen hemminger [DCCP_CLOSING] = "CLOSING", 641f4f0f64Sstephen hemminger [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ", 651f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE", 661f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ", 671f4f0f64Sstephen hemminger [DCCP_TIME_WAIT] = "TIME_WAIT", 681f4f0f64Sstephen hemminger [DCCP_CLOSED] = "CLOSED", 691f4f0f64Sstephen hemminger }; 701f4f0f64Sstephen hemminger 711f4f0f64Sstephen hemminger if (state >= DCCP_MAX_STATES) 721f4f0f64Sstephen hemminger return "INVALID STATE!"; 731f4f0f64Sstephen hemminger else 741f4f0f64Sstephen hemminger return dccp_state_names[state]; 751f4f0f64Sstephen hemminger } 761f4f0f64Sstephen hemminger #endif 771f4f0f64Sstephen hemminger 78c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 79c25a18baSArnaldo Carvalho de Melo { 80c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 81c25a18baSArnaldo Carvalho de Melo 82f11135a3SGerrit Renker dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk, 83c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 84c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 85c25a18baSArnaldo Carvalho de Melo 86c25a18baSArnaldo Carvalho de Melo switch (state) { 87c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 88c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 89c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 906eb55d17SGerrit Renker /* Client retransmits all Confirm options until entering OPEN */ 916eb55d17SGerrit Renker if (oldstate == DCCP_PARTOPEN) 926eb55d17SGerrit Renker dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg); 93c25a18baSArnaldo Carvalho de Melo break; 94c25a18baSArnaldo Carvalho de Melo 95c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 960c869620SGerrit Renker if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ || 970c869620SGerrit Renker oldstate == DCCP_CLOSING) 98c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 99c25a18baSArnaldo Carvalho de Melo 100c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 101c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 102c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 103ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 104df561f66SGustavo A. R. Silva fallthrough; 105c25a18baSArnaldo Carvalho de Melo default: 106c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 107c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 108c25a18baSArnaldo Carvalho de Melo } 109c25a18baSArnaldo Carvalho de Melo 110c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 111c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 112c25a18baSArnaldo Carvalho de Melo */ 113b0832e30SYafang Shao inet_sk_set_state(sk, state); 114c25a18baSArnaldo Carvalho de Melo } 115c25a18baSArnaldo Carvalho de Melo 116c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 117c25a18baSArnaldo Carvalho de Melo 1180c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk) 1190c869620SGerrit Renker { 1200c869620SGerrit Renker switch (sk->sk_state) { 1210c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 1220c869620SGerrit Renker /* Node (client or server) has received Close packet. */ 1230c869620SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED); 1240c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1250c869620SGerrit Renker break; 1260c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 1270c869620SGerrit Renker /* 1280c869620SGerrit Renker * Client received CloseReq. We set the `active' flag so that 1290c869620SGerrit Renker * dccp_send_close() retransmits the Close as per RFC 4340, 8.3. 1300c869620SGerrit Renker */ 1310c869620SGerrit Renker dccp_send_close(sk, 1); 1320c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSING); 1330c869620SGerrit Renker } 1340c869620SGerrit Renker } 1350c869620SGerrit Renker 136c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 137c25a18baSArnaldo Carvalho de Melo { 138c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 139c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 140c25a18baSArnaldo Carvalho de Melo 141c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 142c25a18baSArnaldo Carvalho de Melo 143c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 144c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 145c25a18baSArnaldo Carvalho de Melo else 146c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 147c25a18baSArnaldo Carvalho de Melo } 148c25a18baSArnaldo Carvalho de Melo 149c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 150c25a18baSArnaldo Carvalho de Melo 1517c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1527c657876SArnaldo Carvalho de Melo { 15336cbd3dcSJan Engelhardt static const char *const dccp_packet_names[] = { 1547c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1557c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1567c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1577c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1587c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1597c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1607c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1617c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1627c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1637c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1647c657876SArnaldo Carvalho de Melo }; 1657c657876SArnaldo Carvalho de Melo 1667c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1677c657876SArnaldo Carvalho de Melo return "INVALID"; 1687c657876SArnaldo Carvalho de Melo else 1697c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1707c657876SArnaldo Carvalho de Melo } 1717c657876SArnaldo Carvalho de Melo 1727c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1737c657876SArnaldo Carvalho de Melo 174120e9dabSEric Dumazet static void dccp_sk_destruct(struct sock *sk) 175120e9dabSEric Dumazet { 176120e9dabSEric Dumazet struct dccp_sock *dp = dccp_sk(sk); 177120e9dabSEric Dumazet 178120e9dabSEric Dumazet ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 179120e9dabSEric Dumazet dp->dccps_hc_tx_ccid = NULL; 180120e9dabSEric Dumazet inet_sock_destruct(sk); 181120e9dabSEric Dumazet } 182120e9dabSEric Dumazet 18372478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1843e0fadc5SArnaldo Carvalho de Melo { 1853e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1863e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1873e0fadc5SArnaldo Carvalho de Melo 188e18d7a98SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 189e18d7a98SArnaldo Carvalho de Melo icsk->icsk_syn_retries = sysctl_dccp_request_retries; 190e18d7a98SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 191e18d7a98SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 192120e9dabSEric Dumazet sk->sk_destruct = dccp_sk_destruct; 193e18d7a98SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 194410e27a4SGerrit Renker dp->dccps_mss_cache = 536; 195e18d7a98SArnaldo Carvalho de Melo dp->dccps_rate_last = jiffies; 196e18d7a98SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 197e18d7a98SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 198871a2c16STomasz Grobelny dp->dccps_tx_qlen = sysctl_dccp_tx_qlen; 199e18d7a98SArnaldo Carvalho de Melo 200e18d7a98SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 201e18d7a98SArnaldo Carvalho de Melo 202ac75773cSGerrit Renker INIT_LIST_HEAD(&dp->dccps_featneg); 2033e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 2046eb55d17SGerrit Renker if (likely(ctl_sock_initialized)) 2056eb55d17SGerrit Renker return dccp_feat_init(sk); 2063e0fadc5SArnaldo Carvalho de Melo return 0; 2073e0fadc5SArnaldo Carvalho de Melo } 2083e0fadc5SArnaldo Carvalho de Melo 2093e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 2103e0fadc5SArnaldo Carvalho de Melo 2117d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk) 2123e0fadc5SArnaldo Carvalho de Melo { 2133e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2143e0fadc5SArnaldo Carvalho de Melo 2157749d4ffSEric Dumazet __skb_queue_purge(&sk->sk_write_queue); 2163e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2173e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2183e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2193e0fadc5SArnaldo Carvalho de Melo } 2203e0fadc5SArnaldo Carvalho de Melo 2213e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2223e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 223ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 2243e0fadc5SArnaldo Carvalho de Melo 2253e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2263e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2273e0fadc5SArnaldo Carvalho de Melo 2286fdd34d4SGerrit Renker if (dp->dccps_hc_rx_ackvec != NULL) { 2293e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2303e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2313e0fadc5SArnaldo Carvalho de Melo } 2323e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 233120e9dabSEric Dumazet dp->dccps_hc_rx_ccid = NULL; 2343e0fadc5SArnaldo Carvalho de Melo 2353e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 236d99a7bd2SGerrit Renker dccp_feat_list_purge(&dp->dccps_featneg); 2373e0fadc5SArnaldo Carvalho de Melo } 2383e0fadc5SArnaldo Carvalho de Melo 2393e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2403e0fadc5SArnaldo Carvalho de Melo 241ce865a61SGerrit Renker static inline int dccp_need_reset(int state) 242ce865a61SGerrit Renker { 243ce865a61SGerrit Renker return state != DCCP_CLOSED && state != DCCP_LISTEN && 244ce865a61SGerrit Renker state != DCCP_REQUESTING; 245ce865a61SGerrit Renker } 246ce865a61SGerrit Renker 2477c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2487c657876SArnaldo Carvalho de Melo { 2497c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2507c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 25169c64866SMohamed Ghannam struct dccp_sock *dp = dccp_sk(sk); 2527c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2537c657876SArnaldo Carvalho de Melo 2547c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2557c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2567c657876SArnaldo Carvalho de Melo 257ce865a61SGerrit Renker /* 258ce865a61SGerrit Renker * This corresponds to the ABORT function of RFC793, sec. 3.8 259ce865a61SGerrit Renker * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted". 260ce865a61SGerrit Renker */ 2617c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2627c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 263ce865a61SGerrit Renker } else if (dccp_need_reset(old_state)) { 264ce865a61SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 265ce865a61SGerrit Renker sk->sk_err = ECONNRESET; 2667c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2677c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2687c657876SArnaldo Carvalho de Melo 2697c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 27069c64866SMohamed Ghannam ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 27169c64866SMohamed Ghannam dp->dccps_hc_rx_ccid = NULL; 27248816322SGerrit Renker 2737c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 27448816322SGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 2757c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2767c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2777c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2787c657876SArnaldo Carvalho de Melo } 2797c657876SArnaldo Carvalho de Melo 280c720c7e8SEric Dumazet inet->inet_dport = 0; 2817c657876SArnaldo Carvalho de Melo 282*e0833d1fSKuniyuki Iwashima inet_bhash2_reset_saddr(sk); 2837c657876SArnaldo Carvalho de Melo 2847c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 2857c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 2867c657876SArnaldo Carvalho de Melo 2877c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 2887c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 2897c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 2907c657876SArnaldo Carvalho de Melo 291c720c7e8SEric Dumazet WARN_ON(inet->inet_num && !icsk->icsk_bind_hash); 2927c657876SArnaldo Carvalho de Melo 293e3ae2365SAlexander Aring sk_error_report(sk); 2943285a9aaSHariprasad Kelam return 0; 2957c657876SArnaldo Carvalho de Melo } 2967c657876SArnaldo Carvalho de Melo 297f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 298f21e68caSArnaldo Carvalho de Melo 299a11e1d43SLinus Torvalds /* 300a11e1d43SLinus Torvalds * Wait for a DCCP event. 301a11e1d43SLinus Torvalds * 302a11e1d43SLinus Torvalds * Note that we don't need to lock the socket, as the upper poll layers 303a11e1d43SLinus Torvalds * take care of normal races (between the test and the event) and we don't 304a11e1d43SLinus Torvalds * go look at any of the socket buffers directly. 305a11e1d43SLinus Torvalds */ 306a11e1d43SLinus Torvalds __poll_t dccp_poll(struct file *file, struct socket *sock, 307a11e1d43SLinus Torvalds poll_table *wait) 308331968bdSArnaldo Carvalho de Melo { 309ade994f4SAl Viro __poll_t mask; 310331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 311331968bdSArnaldo Carvalho de Melo 31289ab066dSKarsten Graul sock_poll_wait(file, sock, wait); 313331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 314331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 315331968bdSArnaldo Carvalho de Melo 316331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 317331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 318331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 319331968bdSArnaldo Carvalho de Melo */ 320331968bdSArnaldo Carvalho de Melo 321331968bdSArnaldo Carvalho de Melo mask = 0; 322331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 323a9a08845SLinus Torvalds mask = EPOLLERR; 324331968bdSArnaldo Carvalho de Melo 325331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 326a9a08845SLinus Torvalds mask |= EPOLLHUP; 327331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 328a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 329331968bdSArnaldo Carvalho de Melo 330331968bdSArnaldo Carvalho de Melo /* Connected? */ 331331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 332331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 333a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 334331968bdSArnaldo Carvalho de Melo 335331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 33664dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) { 337a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 338331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 3399cd3e072SEric Dumazet sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 340331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 341331968bdSArnaldo Carvalho de Melo 342331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 343331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 344331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 345331968bdSArnaldo Carvalho de Melo */ 34664dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) 347a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 348331968bdSArnaldo Carvalho de Melo } 349331968bdSArnaldo Carvalho de Melo } 350331968bdSArnaldo Carvalho de Melo } 351331968bdSArnaldo Carvalho de Melo return mask; 352331968bdSArnaldo Carvalho de Melo } 353331968bdSArnaldo Carvalho de Melo 354a11e1d43SLinus Torvalds EXPORT_SYMBOL_GPL(dccp_poll); 355f21e68caSArnaldo Carvalho de Melo 3567c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3577c657876SArnaldo Carvalho de Melo { 3586273172eSArnaldo Carvalho de Melo int rc = -ENOTCONN; 3596273172eSArnaldo Carvalho de Melo 3606273172eSArnaldo Carvalho de Melo lock_sock(sk); 3616273172eSArnaldo Carvalho de Melo 3626273172eSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 3636273172eSArnaldo Carvalho de Melo goto out; 3646273172eSArnaldo Carvalho de Melo 3656273172eSArnaldo Carvalho de Melo switch (cmd) { 366749c08f8SRichard Sailer case SIOCOUTQ: { 367749c08f8SRichard Sailer int amount = sk_wmem_alloc_get(sk); 368749c08f8SRichard Sailer /* Using sk_wmem_alloc here because sk_wmem_queued is not used by DCCP and 369749c08f8SRichard Sailer * always 0, comparably to UDP. 370749c08f8SRichard Sailer */ 371749c08f8SRichard Sailer 372749c08f8SRichard Sailer rc = put_user(amount, (int __user *)arg); 373749c08f8SRichard Sailer } 374749c08f8SRichard Sailer break; 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, 402a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned 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; 417d3c48151SChristoph Hellwig if (copy_from_sockptr_offset(sl->dccpsl_list, optval, 418d3c48151SChristoph Hellwig sizeof(service), optlen - sizeof(service)) || 41967e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 42067e6b629SArnaldo Carvalho de Melo kfree(sl); 42167e6b629SArnaldo Carvalho de Melo return -EFAULT; 42267e6b629SArnaldo Carvalho de Melo } 42367e6b629SArnaldo Carvalho de Melo } 42467e6b629SArnaldo Carvalho de Melo 42567e6b629SArnaldo Carvalho de Melo lock_sock(sk); 42667e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 42767e6b629SArnaldo Carvalho de Melo 42867e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 42967e6b629SArnaldo Carvalho de Melo 43067e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 43167e6b629SArnaldo Carvalho de Melo release_sock(sk); 43267e6b629SArnaldo Carvalho de Melo return 0; 43367e6b629SArnaldo Carvalho de Melo } 43467e6b629SArnaldo Carvalho de Melo 43529450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx) 43629450559SGerrit Renker { 43729450559SGerrit Renker u8 *list, len; 43829450559SGerrit Renker int i, rc; 43929450559SGerrit Renker 44029450559SGerrit Renker if (cscov < 0 || cscov > 15) 44129450559SGerrit Renker return -EINVAL; 44229450559SGerrit Renker /* 44329450559SGerrit Renker * Populate a list of permissible values, in the range cscov...15. This 44429450559SGerrit Renker * is necessary since feature negotiation of single values only works if 44529450559SGerrit Renker * both sides incidentally choose the same value. Since the list starts 44629450559SGerrit Renker * lowest-value first, negotiation will pick the smallest shared value. 44729450559SGerrit Renker */ 44829450559SGerrit Renker if (cscov == 0) 44929450559SGerrit Renker return 0; 45029450559SGerrit Renker len = 16 - cscov; 45129450559SGerrit Renker 45229450559SGerrit Renker list = kmalloc(len, GFP_KERNEL); 45329450559SGerrit Renker if (list == NULL) 45429450559SGerrit Renker return -ENOBUFS; 45529450559SGerrit Renker 45629450559SGerrit Renker for (i = 0; i < len; i++) 45729450559SGerrit Renker list[i] = cscov++; 45829450559SGerrit Renker 45929450559SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len); 46029450559SGerrit Renker 46129450559SGerrit Renker if (rc == 0) { 46229450559SGerrit Renker if (rx) 46329450559SGerrit Renker dccp_sk(sk)->dccps_pcrlen = cscov; 46429450559SGerrit Renker else 46529450559SGerrit Renker dccp_sk(sk)->dccps_pcslen = cscov; 46629450559SGerrit Renker } 46729450559SGerrit Renker kfree(list); 46829450559SGerrit Renker return rc; 46929450559SGerrit Renker } 47029450559SGerrit Renker 471b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type, 472a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 473b20a9c24SGerrit Renker { 474b20a9c24SGerrit Renker u8 *val; 475b20a9c24SGerrit Renker int rc = 0; 476b20a9c24SGerrit Renker 477b20a9c24SGerrit Renker if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS) 478b20a9c24SGerrit Renker return -EINVAL; 479b20a9c24SGerrit Renker 480a7b75c5aSChristoph Hellwig val = memdup_sockptr(optval, optlen); 481042604d2SJulia Lawall if (IS_ERR(val)) 482042604d2SJulia Lawall return PTR_ERR(val); 483b20a9c24SGerrit Renker 484b20a9c24SGerrit Renker lock_sock(sk); 485b20a9c24SGerrit Renker if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID) 486b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen); 487b20a9c24SGerrit Renker 488b20a9c24SGerrit Renker if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID)) 489b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen); 490b20a9c24SGerrit Renker release_sock(sk); 491b20a9c24SGerrit Renker 492b20a9c24SGerrit Renker kfree(val); 493b20a9c24SGerrit Renker return rc; 494b20a9c24SGerrit Renker } 495b20a9c24SGerrit Renker 4963fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 497a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 4987c657876SArnaldo Carvalho de Melo { 49909dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 50009dbc389SGerrit Renker int val, err = 0; 5017c657876SArnaldo Carvalho de Melo 50219102996SGerrit Renker switch (optname) { 50319102996SGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE: 50419102996SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 50519102996SGerrit Renker return 0; 50619102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_L: 50719102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_R: 50819102996SGerrit Renker DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n"); 50919102996SGerrit Renker return 0; 510b20a9c24SGerrit Renker case DCCP_SOCKOPT_CCID: 511b20a9c24SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 512b20a9c24SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 513b20a9c24SGerrit Renker return dccp_setsockopt_ccid(sk, optname, optval, optlen); 51419102996SGerrit Renker } 51519102996SGerrit Renker 51619102996SGerrit Renker if (optlen < (int)sizeof(int)) 517a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 518a84ffe43SArnaldo Carvalho de Melo 519a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&val, optval, sizeof(int))) 520a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 521a84ffe43SArnaldo Carvalho de Melo 52267e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 52367e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 524a84ffe43SArnaldo Carvalho de Melo 52567e6b629SArnaldo Carvalho de Melo lock_sock(sk); 526a84ffe43SArnaldo Carvalho de Melo switch (optname) { 527b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 528b8599d20SGerrit Renker if (dp->dccps_role != DCCP_ROLE_SERVER) 529b8599d20SGerrit Renker err = -EOPNOTSUPP; 530b8599d20SGerrit Renker else 531b8599d20SGerrit Renker dp->dccps_server_timewait = (val != 0); 532b8599d20SGerrit Renker break; 53329450559SGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 53429450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, false); 535d6da3511STomasz Grobelny break; 53629450559SGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 53729450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, true); 538d6da3511STomasz Grobelny break; 539871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 540871a2c16STomasz Grobelny if (sk->sk_state != DCCP_CLOSED) 541871a2c16STomasz Grobelny err = -EISCONN; 542871a2c16STomasz Grobelny else if (val < 0 || val >= DCCPQ_POLICY_MAX) 543871a2c16STomasz Grobelny err = -EINVAL; 544871a2c16STomasz Grobelny else 545871a2c16STomasz Grobelny dp->dccps_qpolicy = val; 546871a2c16STomasz Grobelny break; 547871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 548871a2c16STomasz Grobelny if (val < 0) 549871a2c16STomasz Grobelny err = -EINVAL; 550871a2c16STomasz Grobelny else 551871a2c16STomasz Grobelny dp->dccps_tx_qlen = val; 552871a2c16STomasz Grobelny break; 553a84ffe43SArnaldo Carvalho de Melo default: 554a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 555a84ffe43SArnaldo Carvalho de Melo break; 556a84ffe43SArnaldo Carvalho de Melo } 557410e27a4SGerrit Renker release_sock(sk); 55819102996SGerrit Renker 559a84ffe43SArnaldo Carvalho de Melo return err; 5607c657876SArnaldo Carvalho de Melo } 5617c657876SArnaldo Carvalho de Melo 562a7b75c5aSChristoph Hellwig int dccp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 563a7b75c5aSChristoph Hellwig unsigned int optlen) 5643fdadf7dSDmitry Mishin { 5653fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5663fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5673fdadf7dSDmitry Mishin optname, optval, 5683fdadf7dSDmitry Mishin optlen); 5693fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5703fdadf7dSDmitry Mishin } 571543d9cfeSArnaldo Carvalho de Melo 572f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 573f21e68caSArnaldo Carvalho de Melo 57467e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 57560fe62e7SAndrea Bittau __be32 __user *optval, 57667e6b629SArnaldo Carvalho de Melo int __user *optlen) 57767e6b629SArnaldo Carvalho de Melo { 57867e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 57967e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 58067e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 58167e6b629SArnaldo Carvalho de Melo 58267e6b629SArnaldo Carvalho de Melo lock_sock(sk); 58367e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 58467e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 58567e6b629SArnaldo Carvalho de Melo total_len += slen; 58667e6b629SArnaldo Carvalho de Melo } 58767e6b629SArnaldo Carvalho de Melo 58867e6b629SArnaldo Carvalho de Melo err = -EINVAL; 58967e6b629SArnaldo Carvalho de Melo if (total_len > len) 59067e6b629SArnaldo Carvalho de Melo goto out; 59167e6b629SArnaldo Carvalho de Melo 59267e6b629SArnaldo Carvalho de Melo err = 0; 59367e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 59467e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 59567e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 59667e6b629SArnaldo Carvalho de Melo err = -EFAULT; 59767e6b629SArnaldo Carvalho de Melo out: 59867e6b629SArnaldo Carvalho de Melo release_sock(sk); 59967e6b629SArnaldo Carvalho de Melo return err; 60067e6b629SArnaldo Carvalho de Melo } 60167e6b629SArnaldo Carvalho de Melo 6023fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 603a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 6047c657876SArnaldo Carvalho de Melo { 605a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 606a84ffe43SArnaldo Carvalho de Melo int val, len; 6077c657876SArnaldo Carvalho de Melo 608a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 609a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 610a84ffe43SArnaldo Carvalho de Melo 61139ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 612a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 613a84ffe43SArnaldo Carvalho de Melo 614a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 615a84ffe43SArnaldo Carvalho de Melo 616a84ffe43SArnaldo Carvalho de Melo switch (optname) { 617a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 6185aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 619841bac1dSArnaldo Carvalho de Melo return 0; 62088f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 62188f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 62260fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 6237c559a9eSGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS: 6247c559a9eSGerrit Renker val = dp->dccps_mss_cache; 6257c559a9eSGerrit Renker break; 626d90ebcbfSGerrit Renker case DCCP_SOCKOPT_AVAILABLE_CCIDS: 627d90ebcbfSGerrit Renker return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen); 62871c262a3SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 62971c262a3SGerrit Renker val = ccid_get_current_tx_ccid(dp); 63071c262a3SGerrit Renker if (val < 0) 63171c262a3SGerrit Renker return -ENOPROTOOPT; 63271c262a3SGerrit Renker break; 63371c262a3SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 63471c262a3SGerrit Renker val = ccid_get_current_rx_ccid(dp); 63571c262a3SGerrit Renker if (val < 0) 63671c262a3SGerrit Renker return -ENOPROTOOPT; 63771c262a3SGerrit Renker break; 638b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 639b8599d20SGerrit Renker val = dp->dccps_server_timewait; 640b8599d20SGerrit Renker break; 6416f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 6426f4e5fffSGerrit Renker val = dp->dccps_pcslen; 6436f4e5fffSGerrit Renker break; 6446f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 6456f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 6466f4e5fffSGerrit Renker break; 647871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 648871a2c16STomasz Grobelny val = dp->dccps_qpolicy; 649871a2c16STomasz Grobelny break; 650871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 651871a2c16STomasz Grobelny val = dp->dccps_tx_qlen; 652871a2c16STomasz Grobelny break; 65388f964dbSArnaldo Carvalho de Melo case 128 ... 191: 65488f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 65588f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 65688f964dbSArnaldo Carvalho de Melo case 192 ... 255: 65788f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 65888f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 659a84ffe43SArnaldo Carvalho de Melo default: 660a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 661a84ffe43SArnaldo Carvalho de Melo } 662a84ffe43SArnaldo Carvalho de Melo 66379133506SGerrit Renker len = sizeof(val); 664a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 665a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 666a84ffe43SArnaldo Carvalho de Melo 667a84ffe43SArnaldo Carvalho de Melo return 0; 6687c657876SArnaldo Carvalho de Melo } 6697c657876SArnaldo Carvalho de Melo 6703fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6713fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6723fdadf7dSDmitry Mishin { 6733fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6743fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6753fdadf7dSDmitry Mishin optname, optval, 6763fdadf7dSDmitry Mishin optlen); 6773fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6783fdadf7dSDmitry Mishin } 679543d9cfeSArnaldo Carvalho de Melo 680f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 681f21e68caSArnaldo Carvalho de Melo 682871a2c16STomasz Grobelny static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb) 683871a2c16STomasz Grobelny { 684f95b414eSGu Zheng struct cmsghdr *cmsg; 685871a2c16STomasz Grobelny 686871a2c16STomasz Grobelny /* 687871a2c16STomasz Grobelny * Assign an (opaque) qpolicy priority value to skb->priority. 688871a2c16STomasz Grobelny * 689871a2c16STomasz Grobelny * We are overloading this skb field for use with the qpolicy subystem. 690871a2c16STomasz Grobelny * The skb->priority is normally used for the SO_PRIORITY option, which 691871a2c16STomasz Grobelny * is initialised from sk_priority. Since the assignment of sk_priority 692871a2c16STomasz Grobelny * to skb->priority happens later (on layer 3), we overload this field 693871a2c16STomasz Grobelny * for use with queueing priorities as long as the skb is on layer 4. 694871a2c16STomasz Grobelny * The default priority value (if nothing is set) is 0. 695871a2c16STomasz Grobelny */ 696871a2c16STomasz Grobelny skb->priority = 0; 697871a2c16STomasz Grobelny 698f95b414eSGu Zheng for_each_cmsghdr(cmsg, msg) { 699871a2c16STomasz Grobelny if (!CMSG_OK(msg, cmsg)) 700871a2c16STomasz Grobelny return -EINVAL; 701871a2c16STomasz Grobelny 702871a2c16STomasz Grobelny if (cmsg->cmsg_level != SOL_DCCP) 703871a2c16STomasz Grobelny continue; 704871a2c16STomasz Grobelny 70504910265STomasz Grobelny if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX && 70604910265STomasz Grobelny !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type)) 70704910265STomasz Grobelny return -EINVAL; 70804910265STomasz Grobelny 709871a2c16STomasz Grobelny switch (cmsg->cmsg_type) { 710871a2c16STomasz Grobelny case DCCP_SCM_PRIORITY: 711871a2c16STomasz Grobelny if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32))) 712871a2c16STomasz Grobelny return -EINVAL; 713871a2c16STomasz Grobelny skb->priority = *(__u32 *)CMSG_DATA(cmsg); 714871a2c16STomasz Grobelny break; 715871a2c16STomasz Grobelny default: 716871a2c16STomasz Grobelny return -EINVAL; 717871a2c16STomasz Grobelny } 718871a2c16STomasz Grobelny } 719871a2c16STomasz Grobelny return 0; 720871a2c16STomasz Grobelny } 721871a2c16STomasz Grobelny 7221b784140SYing Xue int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 7237c657876SArnaldo Carvalho de Melo { 7247c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 7257c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 7267c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 7277c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7287c657876SArnaldo Carvalho de Melo int rc, size; 7297c657876SArnaldo Carvalho de Melo long timeo; 7307c657876SArnaldo Carvalho de Melo 731ee549be6SMasami Hiramatsu trace_dccp_probe(sk, len); 732ee549be6SMasami Hiramatsu 7337c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 7347c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 7357c657876SArnaldo Carvalho de Melo 7367c657876SArnaldo Carvalho de Melo lock_sock(sk); 737b1308dc0SIan McDonald 73827258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 7397c657876SArnaldo Carvalho de Melo 7407c657876SArnaldo Carvalho de Melo /* 7417c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 7427c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 7437c657876SArnaldo Carvalho de Melo */ 7447c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 745cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 7467c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 74727258ee5SArnaldo Carvalho de Melo goto out_release; 7487c657876SArnaldo Carvalho de Melo 7497c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 7507c657876SArnaldo Carvalho de Melo release_sock(sk); 7517c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 7527c657876SArnaldo Carvalho de Melo lock_sock(sk); 7537c657876SArnaldo Carvalho de Melo if (skb == NULL) 7547c657876SArnaldo Carvalho de Melo goto out_release; 7557c657876SArnaldo Carvalho de Melo 756a41b17ffSHangyu Hua if (dccp_qpolicy_full(sk)) { 757a41b17ffSHangyu Hua rc = -EAGAIN; 758a41b17ffSHangyu Hua goto out_discard; 759a41b17ffSHangyu Hua } 760a41b17ffSHangyu Hua 76167f93df7SAlexey Kodanev if (sk->sk_state == DCCP_CLOSED) { 76267f93df7SAlexey Kodanev rc = -ENOTCONN; 76367f93df7SAlexey Kodanev goto out_discard; 76467f93df7SAlexey Kodanev } 76567f93df7SAlexey Kodanev 7667c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 7676ce8e9ceSAl Viro rc = memcpy_from_msg(skb_put(skb, len), msg, len); 76827258ee5SArnaldo Carvalho de Melo if (rc != 0) 76927258ee5SArnaldo Carvalho de Melo goto out_discard; 7707c657876SArnaldo Carvalho de Melo 771871a2c16STomasz Grobelny rc = dccp_msghdr_parse(msg, skb); 772871a2c16STomasz Grobelny if (rc != 0) 773871a2c16STomasz Grobelny goto out_discard; 774871a2c16STomasz Grobelny 775871a2c16STomasz Grobelny dccp_qpolicy_push(sk, skb); 776b1fcf55eSGerrit Renker /* 777b1fcf55eSGerrit Renker * The xmit_timer is set if the TX CCID is rate-based and will expire 778b1fcf55eSGerrit Renker * when congestion control permits to release further packets into the 779b1fcf55eSGerrit Renker * network. Window-based CCIDs do not use this timer. 780b1fcf55eSGerrit Renker */ 781b1fcf55eSGerrit Renker if (!timer_pending(&dp->dccps_xmit_timer)) 782b1fcf55eSGerrit Renker dccp_write_xmit(sk); 7837c657876SArnaldo Carvalho de Melo out_release: 7847c657876SArnaldo Carvalho de Melo release_sock(sk); 7857c657876SArnaldo Carvalho de Melo return rc ? : len; 78627258ee5SArnaldo Carvalho de Melo out_discard: 78727258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 7887c657876SArnaldo Carvalho de Melo goto out_release; 7897c657876SArnaldo Carvalho de Melo } 7907c657876SArnaldo Carvalho de Melo 791f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 792f21e68caSArnaldo Carvalho de Melo 793ec095263SOliver Hartkopp int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags, 794ec095263SOliver Hartkopp int *addr_len) 7957c657876SArnaldo Carvalho de Melo { 7967c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 7977c657876SArnaldo Carvalho de Melo long timeo; 7987c657876SArnaldo Carvalho de Melo 7997c657876SArnaldo Carvalho de Melo lock_sock(sk); 8007c657876SArnaldo Carvalho de Melo 801531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 802531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8037c657876SArnaldo Carvalho de Melo goto out; 804531669a0SArnaldo Carvalho de Melo } 8057c657876SArnaldo Carvalho de Melo 806ec095263SOliver Hartkopp timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 8077c657876SArnaldo Carvalho de Melo 8087c657876SArnaldo Carvalho de Melo do { 809531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 8107c657876SArnaldo Carvalho de Melo 811531669a0SArnaldo Carvalho de Melo if (skb == NULL) 812531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 8137c657876SArnaldo Carvalho de Melo 8147c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 8157c657876SArnaldo Carvalho de Melo 8160c869620SGerrit Renker switch (dh->dccph_type) { 8170c869620SGerrit Renker case DCCP_PKT_DATA: 8180c869620SGerrit Renker case DCCP_PKT_DATAACK: 8197c657876SArnaldo Carvalho de Melo goto found_ok_skb; 8207c657876SArnaldo Carvalho de Melo 8210c869620SGerrit Renker case DCCP_PKT_CLOSE: 8220c869620SGerrit Renker case DCCP_PKT_CLOSEREQ: 8230c869620SGerrit Renker if (!(flags & MSG_PEEK)) 8240c869620SGerrit Renker dccp_finish_passive_close(sk); 825df561f66SGustavo A. R. Silva fallthrough; 8260c869620SGerrit Renker case DCCP_PKT_RESET: 8270c869620SGerrit Renker dccp_pr_debug("found fin (%s) ok!\n", 8280c869620SGerrit Renker dccp_packet_name(dh->dccph_type)); 829531669a0SArnaldo Carvalho de Melo len = 0; 8307c657876SArnaldo Carvalho de Melo goto found_fin_ok; 8310c869620SGerrit Renker default: 8327690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 8337690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 8347bced397SDan Williams sk_eat_skb(sk, skb); 8350c869620SGerrit Renker } 836531669a0SArnaldo Carvalho de Melo verify_sock_status: 837531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 838531669a0SArnaldo Carvalho de Melo len = 0; 8397c657876SArnaldo Carvalho de Melo break; 8407c657876SArnaldo Carvalho de Melo } 8417c657876SArnaldo Carvalho de Melo 842531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 843531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 8447c657876SArnaldo Carvalho de Melo break; 845531669a0SArnaldo Carvalho de Melo } 846531669a0SArnaldo Carvalho de Melo 847531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 848531669a0SArnaldo Carvalho de Melo len = 0; 849531669a0SArnaldo Carvalho de Melo break; 850531669a0SArnaldo Carvalho de Melo } 8517c657876SArnaldo Carvalho de Melo 8527c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 8537c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 8547c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 8557c657876SArnaldo Carvalho de Melo * from never connected socket. 8567c657876SArnaldo Carvalho de Melo */ 857531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8587c657876SArnaldo Carvalho de Melo break; 8597c657876SArnaldo Carvalho de Melo } 860531669a0SArnaldo Carvalho de Melo len = 0; 8617c657876SArnaldo Carvalho de Melo break; 8627c657876SArnaldo Carvalho de Melo } 8637c657876SArnaldo Carvalho de Melo 8647c657876SArnaldo Carvalho de Melo if (!timeo) { 865531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 8667c657876SArnaldo Carvalho de Melo break; 8677c657876SArnaldo Carvalho de Melo } 8687c657876SArnaldo Carvalho de Melo 8697c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 870531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 8717c657876SArnaldo Carvalho de Melo break; 8727c657876SArnaldo Carvalho de Melo } 8737c657876SArnaldo Carvalho de Melo 874dfbafc99SSabrina Dubroca sk_wait_data(sk, &timeo, NULL); 8757c657876SArnaldo Carvalho de Melo continue; 8767c657876SArnaldo Carvalho de Melo found_ok_skb: 877531669a0SArnaldo Carvalho de Melo if (len > skb->len) 878531669a0SArnaldo Carvalho de Melo len = skb->len; 879531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 880531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 8817c657876SArnaldo Carvalho de Melo 88251f3d02bSDavid S. Miller if (skb_copy_datagram_msg(skb, 0, msg, len)) { 8837c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 884531669a0SArnaldo Carvalho de Melo len = -EFAULT; 8857c657876SArnaldo Carvalho de Melo break; 8867c657876SArnaldo Carvalho de Melo } 88755d95590SGerrit Renker if (flags & MSG_TRUNC) 88855d95590SGerrit Renker len = skb->len; 8897c657876SArnaldo Carvalho de Melo found_fin_ok: 8907c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 8917bced397SDan Williams sk_eat_skb(sk, skb); 8927c657876SArnaldo Carvalho de Melo break; 893531669a0SArnaldo Carvalho de Melo } while (1); 8947c657876SArnaldo Carvalho de Melo out: 8957c657876SArnaldo Carvalho de Melo release_sock(sk); 896531669a0SArnaldo Carvalho de Melo return len; 8977c657876SArnaldo Carvalho de Melo } 8987c657876SArnaldo Carvalho de Melo 899f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 900f21e68caSArnaldo Carvalho de Melo 901f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 9027c657876SArnaldo Carvalho de Melo { 9037c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 9047c657876SArnaldo Carvalho de Melo unsigned char old_state; 9057c657876SArnaldo Carvalho de Melo int err; 9067c657876SArnaldo Carvalho de Melo 9077c657876SArnaldo Carvalho de Melo lock_sock(sk); 9087c657876SArnaldo Carvalho de Melo 9097c657876SArnaldo Carvalho de Melo err = -EINVAL; 9107c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 9117c657876SArnaldo Carvalho de Melo goto out; 9127c657876SArnaldo Carvalho de Melo 9137c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 9147c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 9157c657876SArnaldo Carvalho de Melo goto out; 9167c657876SArnaldo Carvalho de Melo 917099ecf59SEric Dumazet WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 9187c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 9197c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 9207c657876SArnaldo Carvalho de Melo */ 9217c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 922b4a8e749SKuniyuki Iwashima struct dccp_sock *dp = dccp_sk(sk); 923b4a8e749SKuniyuki Iwashima 924b4a8e749SKuniyuki Iwashima dp->dccps_role = DCCP_ROLE_LISTEN; 925b4a8e749SKuniyuki Iwashima 926b4a8e749SKuniyuki Iwashima /* do not start to listen if feature negotiation setup fails */ 927b4a8e749SKuniyuki Iwashima if (dccp_feat_finalise_settings(dp)) { 928b4a8e749SKuniyuki Iwashima err = -EPROTO; 929b4a8e749SKuniyuki Iwashima goto out; 930b4a8e749SKuniyuki Iwashima } 931b4a8e749SKuniyuki Iwashima 932b4a8e749SKuniyuki Iwashima err = inet_csk_listen_start(sk); 9337c657876SArnaldo Carvalho de Melo if (err) 9347c657876SArnaldo Carvalho de Melo goto out; 9357c657876SArnaldo Carvalho de Melo } 9367c657876SArnaldo Carvalho de Melo err = 0; 9377c657876SArnaldo Carvalho de Melo 9387c657876SArnaldo Carvalho de Melo out: 9397c657876SArnaldo Carvalho de Melo release_sock(sk); 9407c657876SArnaldo Carvalho de Melo return err; 9417c657876SArnaldo Carvalho de Melo } 9427c657876SArnaldo Carvalho de Melo 943f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 944f21e68caSArnaldo Carvalho de Melo 9450c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk) 9467c657876SArnaldo Carvalho de Melo { 9470c869620SGerrit Renker u8 next_state = DCCP_CLOSED; 9487c657876SArnaldo Carvalho de Melo 9490c869620SGerrit Renker switch (sk->sk_state) { 9500c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 9510c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 9520c869620SGerrit Renker dccp_finish_passive_close(sk); 9530c869620SGerrit Renker break; 9540c869620SGerrit Renker case DCCP_PARTOPEN: 9550c869620SGerrit Renker dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk); 9560c869620SGerrit Renker inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 957df561f66SGustavo A. R. Silva fallthrough; 9580c869620SGerrit Renker case DCCP_OPEN: 9590c869620SGerrit Renker dccp_send_close(sk, 1); 9607c657876SArnaldo Carvalho de Melo 961b8599d20SGerrit Renker if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER && 962b8599d20SGerrit Renker !dccp_sk(sk)->dccps_server_timewait) 9630c869620SGerrit Renker next_state = DCCP_ACTIVE_CLOSEREQ; 9640c869620SGerrit Renker else 9650c869620SGerrit Renker next_state = DCCP_CLOSING; 966df561f66SGustavo A. R. Silva fallthrough; 9670c869620SGerrit Renker default: 9680c869620SGerrit Renker dccp_set_state(sk, next_state); 9690c869620SGerrit Renker } 9707c657876SArnaldo Carvalho de Melo } 9717c657876SArnaldo Carvalho de Melo 9727c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 9737c657876SArnaldo Carvalho de Melo { 97497e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 9757c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 976d83bd95bSGerrit Renker u32 data_was_unread = 0; 977134af346SHerbert Xu int state; 9787c657876SArnaldo Carvalho de Melo 9797c657876SArnaldo Carvalho de Melo lock_sock(sk); 9807c657876SArnaldo Carvalho de Melo 9817c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 9827c657876SArnaldo Carvalho de Melo 9837c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 9847c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9857c657876SArnaldo Carvalho de Melo 9867c657876SArnaldo Carvalho de Melo /* Special case. */ 9877c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 9887c657876SArnaldo Carvalho de Melo 9897c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 9907c657876SArnaldo Carvalho de Melo } 9917c657876SArnaldo Carvalho de Melo 99297e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 99397e5848dSIan McDonald 9947c657876SArnaldo Carvalho de Melo /* 9957c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 9967c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 9977c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 9987c657876SArnaldo Carvalho de Melo */ 9997c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 1000d83bd95bSGerrit Renker data_was_unread += skb->len; 10017c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 10027c657876SArnaldo Carvalho de Melo } 10037c657876SArnaldo Carvalho de Melo 1004346da62cSEric Dumazet /* If socket has been already reset kill it. */ 1005346da62cSEric Dumazet if (sk->sk_state == DCCP_CLOSED) 1006346da62cSEric Dumazet goto adjudge_to_death; 1007346da62cSEric Dumazet 1008d83bd95bSGerrit Renker if (data_was_unread) { 1009d83bd95bSGerrit Renker /* Unread data was tossed, send an appropriate Reset Code */ 10102f34b329SGerrit Renker DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread); 1011d83bd95bSGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 1012d83bd95bSGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1013d83bd95bSGerrit Renker } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 10147c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 10157c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 10160c869620SGerrit Renker } else if (sk->sk_state != DCCP_CLOSED) { 1017b1fcf55eSGerrit Renker /* 1018b1fcf55eSGerrit Renker * Normal connection termination. May need to wait if there are 1019b1fcf55eSGerrit Renker * still packets in the TX queue that are delayed by the CCID. 1020b1fcf55eSGerrit Renker */ 1021b1fcf55eSGerrit Renker dccp_flush_write_queue(sk, &timeout); 10220c869620SGerrit Renker dccp_terminate_connection(sk); 10237c657876SArnaldo Carvalho de Melo } 10247c657876SArnaldo Carvalho de Melo 1025b1fcf55eSGerrit Renker /* 1026b1fcf55eSGerrit Renker * Flush write queue. This may be necessary in several cases: 1027b1fcf55eSGerrit Renker * - we have been closed by the peer but still have application data; 1028b1fcf55eSGerrit Renker * - abortive termination (unread data or zero linger time), 1029b1fcf55eSGerrit Renker * - normal termination but queue could not be flushed within time limit 1030b1fcf55eSGerrit Renker */ 1031b1fcf55eSGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 1032b1fcf55eSGerrit Renker 10337c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 10347c657876SArnaldo Carvalho de Melo 10357c657876SArnaldo Carvalho de Melo adjudge_to_death: 1036134af346SHerbert Xu state = sk->sk_state; 1037134af346SHerbert Xu sock_hold(sk); 1038134af346SHerbert Xu sock_orphan(sk); 1039134af346SHerbert Xu 10407ad07e7cSArnaldo Carvalho de Melo /* 10417ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 10427ad07e7cSArnaldo Carvalho de Melo */ 10437c657876SArnaldo Carvalho de Melo release_sock(sk); 10447c657876SArnaldo Carvalho de Melo /* 10457c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 10467c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 10477c657876SArnaldo Carvalho de Melo */ 10487c657876SArnaldo Carvalho de Melo local_bh_disable(); 10497c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 1050547b792cSIlpo Järvinen WARN_ON(sock_owned_by_user(sk)); 10517c657876SArnaldo Carvalho de Melo 105219757cebSEric Dumazet this_cpu_inc(dccp_orphan_count); 1053eb4dea58SHerbert Xu 1054134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 1055134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 1056134af346SHerbert Xu goto out; 10577c657876SArnaldo Carvalho de Melo 10587c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 10597c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 10607c657876SArnaldo Carvalho de Melo 10617c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 10627c657876SArnaldo Carvalho de Melo 1063134af346SHerbert Xu out: 10647c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 10657c657876SArnaldo Carvalho de Melo local_bh_enable(); 10667c657876SArnaldo Carvalho de Melo sock_put(sk); 10677c657876SArnaldo Carvalho de Melo } 10687c657876SArnaldo Carvalho de Melo 1069f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 1070f21e68caSArnaldo Carvalho de Melo 10717c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 10727c657876SArnaldo Carvalho de Melo { 10738e8c71f1SGerrit Renker dccp_pr_debug("called shutdown(%x)\n", how); 10747c657876SArnaldo Carvalho de Melo } 10757c657876SArnaldo Carvalho de Melo 1076f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 1077f21e68caSArnaldo Carvalho de Melo 10780c5b8a46SFabian Frederick static inline int __init dccp_mib_init(void) 10797c657876SArnaldo Carvalho de Melo { 1080698365faSWANG Cong dccp_statistics = alloc_percpu(struct dccp_mib); 1081698365faSWANG Cong if (!dccp_statistics) 1082698365faSWANG Cong return -ENOMEM; 1083698365faSWANG Cong return 0; 10847c657876SArnaldo Carvalho de Melo } 10857c657876SArnaldo Carvalho de Melo 108624e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void) 108746f09ffaSArnaldo Carvalho de Melo { 1088698365faSWANG Cong free_percpu(dccp_statistics); 108946f09ffaSArnaldo Carvalho de Melo } 109046f09ffaSArnaldo Carvalho de Melo 10917c657876SArnaldo Carvalho de Melo static int thash_entries; 10927c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 10937c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 10947c657876SArnaldo Carvalho de Melo 1095a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 1096eb939922SRusty Russell bool dccp_debug; 109743264991SGerrit Renker module_param(dccp_debug, bool, 0644); 10987c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 1099f21e68caSArnaldo Carvalho de Melo 1100f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 1101a1d3a355SArnaldo Carvalho de Melo #endif 11027c657876SArnaldo Carvalho de Melo 11037c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 11047c657876SArnaldo Carvalho de Melo { 11057c657876SArnaldo Carvalho de Melo unsigned long goal; 1106ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 11077c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1108dd24c001SEric Dumazet int rc; 11097c657876SArnaldo Carvalho de Melo 1110028b0275SPatrick McHardy BUILD_BUG_ON(sizeof(struct dccp_skb_cb) > 1111c593642cSPankaj Bharadiya sizeof_field(struct sk_buff, cb)); 1112c92c81dfSPeter Oskolkov rc = inet_hashinfo2_init_mod(&dccp_hashinfo); 1113c92c81dfSPeter Oskolkov if (rc) 111419757cebSEric Dumazet goto out_fail; 1115c92c81dfSPeter Oskolkov rc = -ENOBUFS; 11167690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 11177690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 11187690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 1119990c74e3SVasily Averin SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL); 11207c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1121c96b6accSWang Hai goto out_free_hashinfo2; 112228044fc1SJoanne Koong dccp_hashinfo.bind2_bucket_cachep = 112328044fc1SJoanne Koong kmem_cache_create("dccp_bind2_bucket", 112428044fc1SJoanne Koong sizeof(struct inet_bind2_bucket), 0, 112528044fc1SJoanne Koong SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL); 112628044fc1SJoanne Koong if (!dccp_hashinfo.bind2_bucket_cachep) 112728044fc1SJoanne Koong goto out_free_bind_bucket_cachep; 11287c657876SArnaldo Carvalho de Melo 11297c657876SArnaldo Carvalho de Melo /* 11307c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 11317c657876SArnaldo Carvalho de Melo * hash tables. 11327c657876SArnaldo Carvalho de Melo * 11337c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 11347c657876SArnaldo Carvalho de Melo */ 11353d6357deSArun KS if (nr_pages >= (128 * 1024)) 11363d6357deSArun KS goal = nr_pages >> (21 - PAGE_SHIFT); 11377c657876SArnaldo Carvalho de Melo else 11383d6357deSArun KS goal = nr_pages >> (23 - PAGE_SHIFT); 11397c657876SArnaldo Carvalho de Melo 11407c657876SArnaldo Carvalho de Melo if (thash_entries) 11417690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 11427690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 11437c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 11447c657876SArnaldo Carvalho de Melo ; 11457c657876SArnaldo Carvalho de Melo do { 1146f373b53bSEric Dumazet unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE / 11477c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 1148f373b53bSEric Dumazet 1149f373b53bSEric Dumazet while (hash_size & (hash_size - 1)) 1150f373b53bSEric Dumazet hash_size--; 1151f373b53bSEric Dumazet dccp_hashinfo.ehash_mask = hash_size - 1; 11527c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 11531c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order); 11547c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 11557c657876SArnaldo Carvalho de Melo 11567c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 115759348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 115828044fc1SJoanne Koong goto out_free_bind2_bucket_cachep; 11597c657876SArnaldo Carvalho de Melo } 11607c657876SArnaldo Carvalho de Melo 116105dbc7b5SEric Dumazet for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) 11623ab5aee7SEric Dumazet INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i); 11637c657876SArnaldo Carvalho de Melo 1164230140cfSEric Dumazet if (inet_ehash_locks_alloc(&dccp_hashinfo)) 1165230140cfSEric Dumazet goto out_free_dccp_ehash; 1166230140cfSEric Dumazet 11677c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 11687c657876SArnaldo Carvalho de Melo 11697c657876SArnaldo Carvalho de Melo do { 11707c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 11717c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 11727690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 11737690af3fSArnaldo Carvalho de Melo bhash_order > 0) 11747c657876SArnaldo Carvalho de Melo continue; 11757c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 11761c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order); 11777c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 11787c657876SArnaldo Carvalho de Melo 11797c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 118059348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 1181230140cfSEric Dumazet goto out_free_dccp_locks; 11827c657876SArnaldo Carvalho de Melo } 11837c657876SArnaldo Carvalho de Melo 118428044fc1SJoanne Koong dccp_hashinfo.bhash2 = (struct inet_bind_hashbucket *) 118528044fc1SJoanne Koong __get_free_pages(GFP_ATOMIC | __GFP_NOWARN, bhash_order); 118628044fc1SJoanne Koong 118728044fc1SJoanne Koong if (!dccp_hashinfo.bhash2) { 118828044fc1SJoanne Koong DCCP_CRIT("Failed to allocate DCCP bind2 hash table"); 118928044fc1SJoanne Koong goto out_free_dccp_bhash; 119028044fc1SJoanne Koong } 119128044fc1SJoanne Koong 11927c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 11937c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 11947c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 119528044fc1SJoanne Koong spin_lock_init(&dccp_hashinfo.bhash2[i].lock); 119628044fc1SJoanne Koong INIT_HLIST_HEAD(&dccp_hashinfo.bhash2[i].chain); 11977c657876SArnaldo Carvalho de Melo } 11987c657876SArnaldo Carvalho de Melo 1199d1e5e640SKuniyuki Iwashima dccp_hashinfo.pernet = false; 1200d1e5e640SKuniyuki Iwashima 120146f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1202fa23e2ecSArnaldo Carvalho de Melo if (rc) 120328044fc1SJoanne Koong goto out_free_dccp_bhash2; 12047c657876SArnaldo Carvalho de Melo 12059b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 12067c657876SArnaldo Carvalho de Melo if (rc) 1207b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 12089b07ef5dSArnaldo Carvalho de Melo 1209e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 12109b07ef5dSArnaldo Carvalho de Melo if (rc) 12119b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 12124c70f383SGerrit Renker 1213ddebc973SGerrit Renker rc = ccid_initialize_builtins(); 1214ddebc973SGerrit Renker if (rc) 1215ddebc973SGerrit Renker goto out_sysctl_exit; 1216ddebc973SGerrit Renker 12174c70f383SGerrit Renker dccp_timestamping_init(); 1218d14a0ebdSGerrit Renker 1219d14a0ebdSGerrit Renker return 0; 1220d14a0ebdSGerrit Renker 1221ddebc973SGerrit Renker out_sysctl_exit: 1222ddebc973SGerrit Renker dccp_sysctl_exit(); 12239b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 12249b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1225b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 122646f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 122728044fc1SJoanne Koong out_free_dccp_bhash2: 122828044fc1SJoanne Koong free_pages((unsigned long)dccp_hashinfo.bhash2, bhash_order); 12297c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 12307c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 1231230140cfSEric Dumazet out_free_dccp_locks: 1232230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12337c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 12347c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 123528044fc1SJoanne Koong out_free_bind2_bucket_cachep: 123628044fc1SJoanne Koong kmem_cache_destroy(dccp_hashinfo.bind2_bucket_cachep); 12377c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 12387c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 1239c96b6accSWang Hai out_free_hashinfo2: 1240c96b6accSWang Hai inet_hashinfo2_free_mod(&dccp_hashinfo); 1241d14a0ebdSGerrit Renker out_fail: 1242d14a0ebdSGerrit Renker dccp_hashinfo.bhash = NULL; 124328044fc1SJoanne Koong dccp_hashinfo.bhash2 = NULL; 1244d14a0ebdSGerrit Renker dccp_hashinfo.ehash = NULL; 1245d14a0ebdSGerrit Renker dccp_hashinfo.bind_bucket_cachep = NULL; 124628044fc1SJoanne Koong dccp_hashinfo.bind2_bucket_cachep = NULL; 1247d14a0ebdSGerrit Renker return rc; 12487c657876SArnaldo Carvalho de Melo } 12497c657876SArnaldo Carvalho de Melo 12507c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 12517c657876SArnaldo Carvalho de Melo { 125228044fc1SJoanne Koong int bhash_order = get_order(dccp_hashinfo.bhash_size * 125328044fc1SJoanne Koong sizeof(struct inet_bind_hashbucket)); 125428044fc1SJoanne Koong 1255ddebc973SGerrit Renker ccid_cleanup_builtins(); 125646f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 125728044fc1SJoanne Koong free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 125828044fc1SJoanne Koong free_pages((unsigned long)dccp_hashinfo.bhash2, bhash_order); 1259725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1260f373b53bSEric Dumazet get_order((dccp_hashinfo.ehash_mask + 1) * 1261725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 1262230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12637c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 12649b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1265e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 1266c96b6accSWang Hai inet_hashinfo2_free_mod(&dccp_hashinfo); 12677c657876SArnaldo Carvalho de Melo } 12687c657876SArnaldo Carvalho de Melo 12697c657876SArnaldo Carvalho de Melo module_init(dccp_init); 12707c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 12717c657876SArnaldo Carvalho de Melo 12727c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 12737c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 12747c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1275