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> 277c657876SArnaldo Carvalho de Melo #include <net/sock.h> 287c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 297c657876SArnaldo Carvalho de Melo 306273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h> 317c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 327c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 337c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 347c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 357c657876SArnaldo Carvalho de Melo 367c657876SArnaldo Carvalho de Melo #include "ccid.h" 377c657876SArnaldo Carvalho de Melo #include "dccp.h" 38afe00251SAndrea Bittau #include "feat.h" 397c657876SArnaldo Carvalho de Melo 40ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 417c657876SArnaldo Carvalho de Melo 42f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 43f21e68caSArnaldo Carvalho de Melo 44dd24c001SEric Dumazet struct percpu_counter dccp_orphan_count; 45f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 46f21e68caSArnaldo Carvalho de Melo 475caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo; 48075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 49075ae866SArnaldo Carvalho de Melo 50b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */ 51b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5; 52b1308dc0SIan McDonald 531f4f0f64Sstephen hemminger #ifdef CONFIG_IP_DCCP_DEBUG 541f4f0f64Sstephen hemminger static const char *dccp_state_name(const int state) 551f4f0f64Sstephen hemminger { 561f4f0f64Sstephen hemminger static const char *const dccp_state_names[] = { 571f4f0f64Sstephen hemminger [DCCP_OPEN] = "OPEN", 581f4f0f64Sstephen hemminger [DCCP_REQUESTING] = "REQUESTING", 591f4f0f64Sstephen hemminger [DCCP_PARTOPEN] = "PARTOPEN", 601f4f0f64Sstephen hemminger [DCCP_LISTEN] = "LISTEN", 611f4f0f64Sstephen hemminger [DCCP_RESPOND] = "RESPOND", 621f4f0f64Sstephen hemminger [DCCP_CLOSING] = "CLOSING", 631f4f0f64Sstephen hemminger [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ", 641f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE", 651f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ", 661f4f0f64Sstephen hemminger [DCCP_TIME_WAIT] = "TIME_WAIT", 671f4f0f64Sstephen hemminger [DCCP_CLOSED] = "CLOSED", 681f4f0f64Sstephen hemminger }; 691f4f0f64Sstephen hemminger 701f4f0f64Sstephen hemminger if (state >= DCCP_MAX_STATES) 711f4f0f64Sstephen hemminger return "INVALID STATE!"; 721f4f0f64Sstephen hemminger else 731f4f0f64Sstephen hemminger return dccp_state_names[state]; 741f4f0f64Sstephen hemminger } 751f4f0f64Sstephen hemminger #endif 761f4f0f64Sstephen hemminger 77c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state) 78c25a18baSArnaldo Carvalho de Melo { 79c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state; 80c25a18baSArnaldo Carvalho de Melo 81f11135a3SGerrit Renker dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk, 82c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state)); 83c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate); 84c25a18baSArnaldo Carvalho de Melo 85c25a18baSArnaldo Carvalho de Melo switch (state) { 86c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN: 87c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN) 88c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB); 896eb55d17SGerrit Renker /* Client retransmits all Confirm options until entering OPEN */ 906eb55d17SGerrit Renker if (oldstate == DCCP_PARTOPEN) 916eb55d17SGerrit Renker dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg); 92c25a18baSArnaldo Carvalho de Melo break; 93c25a18baSArnaldo Carvalho de Melo 94c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED: 950c869620SGerrit Renker if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ || 960c869620SGerrit Renker oldstate == DCCP_CLOSING) 97c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); 98c25a18baSArnaldo Carvalho de Melo 99c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk); 100c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL && 101c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) 102ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 103c25a18baSArnaldo Carvalho de Melo /* fall through */ 104c25a18baSArnaldo Carvalho de Melo default: 105c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN) 106c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB); 107c25a18baSArnaldo Carvalho de Melo } 108c25a18baSArnaldo Carvalho de Melo 109c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed 110c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables. 111c25a18baSArnaldo Carvalho de Melo */ 112c25a18baSArnaldo Carvalho de Melo sk->sk_state = state; 113c25a18baSArnaldo Carvalho de Melo } 114c25a18baSArnaldo Carvalho de Melo 115c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state); 116c25a18baSArnaldo Carvalho de Melo 1170c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk) 1180c869620SGerrit Renker { 1190c869620SGerrit Renker switch (sk->sk_state) { 1200c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 1210c869620SGerrit Renker /* Node (client or server) has received Close packet. */ 1220c869620SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED); 1230c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1240c869620SGerrit Renker break; 1250c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 1260c869620SGerrit Renker /* 1270c869620SGerrit Renker * Client received CloseReq. We set the `active' flag so that 1280c869620SGerrit Renker * dccp_send_close() retransmits the Close as per RFC 4340, 8.3. 1290c869620SGerrit Renker */ 1300c869620SGerrit Renker dccp_send_close(sk, 1); 1310c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSING); 1320c869620SGerrit Renker } 1330c869620SGerrit Renker } 1340c869620SGerrit Renker 135c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk) 136c25a18baSArnaldo Carvalho de Melo { 137c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 138c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 139c25a18baSArnaldo Carvalho de Melo 140c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 141c25a18baSArnaldo Carvalho de Melo 142c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD)) 143c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk); 144c25a18baSArnaldo Carvalho de Melo else 145c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 146c25a18baSArnaldo Carvalho de Melo } 147c25a18baSArnaldo Carvalho de Melo 148c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done); 149c25a18baSArnaldo Carvalho de Melo 1507c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 1517c657876SArnaldo Carvalho de Melo { 15236cbd3dcSJan Engelhardt static const char *const dccp_packet_names[] = { 1537c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 1547c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 1557c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 1567c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 1577c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 1587c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 1597c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 1607c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 1617c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 1627c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 1637c657876SArnaldo Carvalho de Melo }; 1647c657876SArnaldo Carvalho de Melo 1657c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 1667c657876SArnaldo Carvalho de Melo return "INVALID"; 1677c657876SArnaldo Carvalho de Melo else 1687c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 1697c657876SArnaldo Carvalho de Melo } 1707c657876SArnaldo Carvalho de Melo 1717c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 1727c657876SArnaldo Carvalho de Melo 17372478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized) 1743e0fadc5SArnaldo Carvalho de Melo { 1753e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1763e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1773e0fadc5SArnaldo Carvalho de Melo 178e18d7a98SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 179e18d7a98SArnaldo Carvalho de Melo icsk->icsk_syn_retries = sysctl_dccp_request_retries; 180e18d7a98SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 181e18d7a98SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 182e18d7a98SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 183410e27a4SGerrit Renker dp->dccps_mss_cache = 536; 184e18d7a98SArnaldo Carvalho de Melo dp->dccps_rate_last = jiffies; 185e18d7a98SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 186e18d7a98SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT; 187871a2c16STomasz Grobelny dp->dccps_tx_qlen = sysctl_dccp_tx_qlen; 188e18d7a98SArnaldo Carvalho de Melo 189e18d7a98SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 190e18d7a98SArnaldo Carvalho de Melo 191ac75773cSGerrit Renker INIT_LIST_HEAD(&dp->dccps_featneg); 1923e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 1936eb55d17SGerrit Renker if (likely(ctl_sock_initialized)) 1946eb55d17SGerrit Renker return dccp_feat_init(sk); 1953e0fadc5SArnaldo Carvalho de Melo return 0; 1963e0fadc5SArnaldo Carvalho de Melo } 1973e0fadc5SArnaldo Carvalho de Melo 1983e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 1993e0fadc5SArnaldo Carvalho de Melo 2007d06b2e0SBrian Haley void dccp_destroy_sock(struct sock *sk) 2013e0fadc5SArnaldo Carvalho de Melo { 2023e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 2033e0fadc5SArnaldo Carvalho de Melo 2047749d4ffSEric Dumazet __skb_queue_purge(&sk->sk_write_queue); 2053e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2063e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 2073e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2083e0fadc5SArnaldo Carvalho de Melo } 2093e0fadc5SArnaldo Carvalho de Melo 2103e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2113e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 212ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk); 2133e0fadc5SArnaldo Carvalho de Melo 2143e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2153e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2163e0fadc5SArnaldo Carvalho de Melo 2176fdd34d4SGerrit Renker if (dp->dccps_hc_rx_ackvec != NULL) { 2183e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2193e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2203e0fadc5SArnaldo Carvalho de Melo } 2213e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2223e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2233e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2243e0fadc5SArnaldo Carvalho de Melo 2253e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 226d99a7bd2SGerrit Renker dccp_feat_list_purge(&dp->dccps_featneg); 2273e0fadc5SArnaldo Carvalho de Melo } 2283e0fadc5SArnaldo Carvalho de Melo 2293e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2303e0fadc5SArnaldo Carvalho de Melo 23172a3effaSEric Dumazet static inline int dccp_listen_start(struct sock *sk, int backlog) 2327c657876SArnaldo Carvalho de Melo { 23367e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 23467e6b629SArnaldo Carvalho de Melo 23567e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 2369eca0a47SGerrit Renker /* do not start to listen if feature negotiation setup fails */ 2379eca0a47SGerrit Renker if (dccp_feat_finalise_settings(dp)) 2389eca0a47SGerrit Renker return -EPROTO; 23972a3effaSEric Dumazet return inet_csk_listen_start(sk, backlog); 2407c657876SArnaldo Carvalho de Melo } 2417c657876SArnaldo Carvalho de Melo 242ce865a61SGerrit Renker static inline int dccp_need_reset(int state) 243ce865a61SGerrit Renker { 244ce865a61SGerrit Renker return state != DCCP_CLOSED && state != DCCP_LISTEN && 245ce865a61SGerrit Renker state != DCCP_REQUESTING; 246ce865a61SGerrit Renker } 247ce865a61SGerrit Renker 2487c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2497c657876SArnaldo Carvalho de Melo { 2507c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2517c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2527c657876SArnaldo Carvalho de Melo int err = 0; 2537c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2547c657876SArnaldo Carvalho de Melo 2557c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2567c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2577c657876SArnaldo Carvalho de Melo 258ce865a61SGerrit Renker /* 259ce865a61SGerrit Renker * This corresponds to the ABORT function of RFC793, sec. 3.8 260ce865a61SGerrit Renker * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted". 261ce865a61SGerrit Renker */ 2627c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2637c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 264ce865a61SGerrit Renker } else if (dccp_need_reset(old_state)) { 265ce865a61SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 266ce865a61SGerrit Renker sk->sk_err = ECONNRESET; 2677c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2687c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2697c657876SArnaldo Carvalho de Melo 2707c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 27148816322SGerrit Renker 2727c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 27348816322SGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 2747c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2757c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2767c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2777c657876SArnaldo Carvalho de Melo } 2787c657876SArnaldo Carvalho de Melo 279c720c7e8SEric Dumazet inet->inet_dport = 0; 2807c657876SArnaldo Carvalho de Melo 2817c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 2827c657876SArnaldo Carvalho de Melo inet_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 2937c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 2947c657876SArnaldo Carvalho de Melo return err; 2957c657876SArnaldo Carvalho de Melo } 2967c657876SArnaldo Carvalho de Melo 297f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 298f21e68caSArnaldo Carvalho de Melo 299331968bdSArnaldo Carvalho de Melo /* 300331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 301331968bdSArnaldo Carvalho de Melo * 302331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 303331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 304331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 305331968bdSArnaldo Carvalho de Melo */ 306f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 307331968bdSArnaldo Carvalho de Melo poll_table *wait) 308331968bdSArnaldo Carvalho de Melo { 309331968bdSArnaldo Carvalho de Melo unsigned int mask; 310331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 311331968bdSArnaldo Carvalho de Melo 312aa395145SEric Dumazet sock_poll_wait(file, sk_sleep(sk), 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) 323331968bdSArnaldo Carvalho de Melo mask = POLLERR; 324331968bdSArnaldo Carvalho de Melo 325331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 326331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 327331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 328f348d70aSDavide Libenzi mask |= POLLIN | POLLRDNORM | POLLRDHUP; 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) 333331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 334331968bdSArnaldo Carvalho de Melo 335331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 33664dc6130SEric Dumazet if (sk_stream_is_writeable(sk)) { 337331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 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)) 347331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 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 354f21e68caSArnaldo Carvalho de Melo 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) { 3666273172eSArnaldo Carvalho de Melo case SIOCINQ: { 3676273172eSArnaldo Carvalho de Melo struct sk_buff *skb; 3686273172eSArnaldo Carvalho de Melo unsigned long amount = 0; 3696273172eSArnaldo Carvalho de Melo 3706273172eSArnaldo Carvalho de Melo skb = skb_peek(&sk->sk_receive_queue); 3716273172eSArnaldo Carvalho de Melo if (skb != NULL) { 3726273172eSArnaldo Carvalho de Melo /* 3736273172eSArnaldo Carvalho de Melo * We will only return the amount of this packet since 3746273172eSArnaldo Carvalho de Melo * that is all that will be read. 3756273172eSArnaldo Carvalho de Melo */ 3766273172eSArnaldo Carvalho de Melo amount = skb->len; 3776273172eSArnaldo Carvalho de Melo } 3786273172eSArnaldo Carvalho de Melo rc = put_user(amount, (int __user *)arg); 3796273172eSArnaldo Carvalho de Melo } 3806273172eSArnaldo Carvalho de Melo break; 3816273172eSArnaldo Carvalho de Melo default: 3826273172eSArnaldo Carvalho de Melo rc = -ENOIOCTLCMD; 3836273172eSArnaldo Carvalho de Melo break; 3846273172eSArnaldo Carvalho de Melo } 3856273172eSArnaldo Carvalho de Melo out: 3866273172eSArnaldo Carvalho de Melo release_sock(sk); 3876273172eSArnaldo Carvalho de Melo return rc; 3887c657876SArnaldo Carvalho de Melo } 3897c657876SArnaldo Carvalho de Melo 390f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 391f21e68caSArnaldo Carvalho de Melo 39260fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 393b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 39467e6b629SArnaldo Carvalho de Melo { 39567e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 39667e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 39767e6b629SArnaldo Carvalho de Melo 39867e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 39967e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 40067e6b629SArnaldo Carvalho de Melo return -EINVAL; 40167e6b629SArnaldo Carvalho de Melo 40267e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 40367e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 40467e6b629SArnaldo Carvalho de Melo if (sl == NULL) 40567e6b629SArnaldo Carvalho de Melo return -ENOMEM; 40667e6b629SArnaldo Carvalho de Melo 40767e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 40867e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 40967e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 41067e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 41167e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 41267e6b629SArnaldo Carvalho de Melo kfree(sl); 41367e6b629SArnaldo Carvalho de Melo return -EFAULT; 41467e6b629SArnaldo Carvalho de Melo } 41567e6b629SArnaldo Carvalho de Melo } 41667e6b629SArnaldo Carvalho de Melo 41767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 41867e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 41967e6b629SArnaldo Carvalho de Melo 42067e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 42167e6b629SArnaldo Carvalho de Melo 42267e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 42367e6b629SArnaldo Carvalho de Melo release_sock(sk); 42467e6b629SArnaldo Carvalho de Melo return 0; 42567e6b629SArnaldo Carvalho de Melo } 42667e6b629SArnaldo Carvalho de Melo 42729450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx) 42829450559SGerrit Renker { 42929450559SGerrit Renker u8 *list, len; 43029450559SGerrit Renker int i, rc; 43129450559SGerrit Renker 43229450559SGerrit Renker if (cscov < 0 || cscov > 15) 43329450559SGerrit Renker return -EINVAL; 43429450559SGerrit Renker /* 43529450559SGerrit Renker * Populate a list of permissible values, in the range cscov...15. This 43629450559SGerrit Renker * is necessary since feature negotiation of single values only works if 43729450559SGerrit Renker * both sides incidentally choose the same value. Since the list starts 43829450559SGerrit Renker * lowest-value first, negotiation will pick the smallest shared value. 43929450559SGerrit Renker */ 44029450559SGerrit Renker if (cscov == 0) 44129450559SGerrit Renker return 0; 44229450559SGerrit Renker len = 16 - cscov; 44329450559SGerrit Renker 44429450559SGerrit Renker list = kmalloc(len, GFP_KERNEL); 44529450559SGerrit Renker if (list == NULL) 44629450559SGerrit Renker return -ENOBUFS; 44729450559SGerrit Renker 44829450559SGerrit Renker for (i = 0; i < len; i++) 44929450559SGerrit Renker list[i] = cscov++; 45029450559SGerrit Renker 45129450559SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len); 45229450559SGerrit Renker 45329450559SGerrit Renker if (rc == 0) { 45429450559SGerrit Renker if (rx) 45529450559SGerrit Renker dccp_sk(sk)->dccps_pcrlen = cscov; 45629450559SGerrit Renker else 45729450559SGerrit Renker dccp_sk(sk)->dccps_pcslen = cscov; 45829450559SGerrit Renker } 45929450559SGerrit Renker kfree(list); 46029450559SGerrit Renker return rc; 46129450559SGerrit Renker } 46229450559SGerrit Renker 463b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type, 464b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 465b20a9c24SGerrit Renker { 466b20a9c24SGerrit Renker u8 *val; 467b20a9c24SGerrit Renker int rc = 0; 468b20a9c24SGerrit Renker 469b20a9c24SGerrit Renker if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS) 470b20a9c24SGerrit Renker return -EINVAL; 471b20a9c24SGerrit Renker 472042604d2SJulia Lawall val = memdup_user(optval, optlen); 473042604d2SJulia Lawall if (IS_ERR(val)) 474042604d2SJulia Lawall return PTR_ERR(val); 475b20a9c24SGerrit Renker 476b20a9c24SGerrit Renker lock_sock(sk); 477b20a9c24SGerrit Renker if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID) 478b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen); 479b20a9c24SGerrit Renker 480b20a9c24SGerrit Renker if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID)) 481b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen); 482b20a9c24SGerrit Renker release_sock(sk); 483b20a9c24SGerrit Renker 484b20a9c24SGerrit Renker kfree(val); 485b20a9c24SGerrit Renker return rc; 486b20a9c24SGerrit Renker } 487b20a9c24SGerrit Renker 4883fdadf7dSDmitry Mishin static int do_dccp_setsockopt(struct sock *sk, int level, int optname, 489b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 4907c657876SArnaldo Carvalho de Melo { 49109dbc389SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 49209dbc389SGerrit Renker int val, err = 0; 4937c657876SArnaldo Carvalho de Melo 49419102996SGerrit Renker switch (optname) { 49519102996SGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE: 49619102996SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 49719102996SGerrit Renker return 0; 49819102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_L: 49919102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_R: 50019102996SGerrit Renker DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n"); 50119102996SGerrit Renker return 0; 502b20a9c24SGerrit Renker case DCCP_SOCKOPT_CCID: 503b20a9c24SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 504b20a9c24SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 505b20a9c24SGerrit Renker return dccp_setsockopt_ccid(sk, optname, optval, optlen); 50619102996SGerrit Renker } 50719102996SGerrit Renker 50819102996SGerrit Renker if (optlen < (int)sizeof(int)) 509a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 510a84ffe43SArnaldo Carvalho de Melo 511a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 512a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 513a84ffe43SArnaldo Carvalho de Melo 51467e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 51567e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 516a84ffe43SArnaldo Carvalho de Melo 51767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 518a84ffe43SArnaldo Carvalho de Melo switch (optname) { 519b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 520b8599d20SGerrit Renker if (dp->dccps_role != DCCP_ROLE_SERVER) 521b8599d20SGerrit Renker err = -EOPNOTSUPP; 522b8599d20SGerrit Renker else 523b8599d20SGerrit Renker dp->dccps_server_timewait = (val != 0); 524b8599d20SGerrit Renker break; 52529450559SGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 52629450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, false); 527d6da3511STomasz Grobelny break; 52829450559SGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 52929450559SGerrit Renker err = dccp_setsockopt_cscov(sk, val, true); 530d6da3511STomasz Grobelny break; 531871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 532871a2c16STomasz Grobelny if (sk->sk_state != DCCP_CLOSED) 533871a2c16STomasz Grobelny err = -EISCONN; 534871a2c16STomasz Grobelny else if (val < 0 || val >= DCCPQ_POLICY_MAX) 535871a2c16STomasz Grobelny err = -EINVAL; 536871a2c16STomasz Grobelny else 537871a2c16STomasz Grobelny dp->dccps_qpolicy = val; 538871a2c16STomasz Grobelny break; 539871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 540871a2c16STomasz Grobelny if (val < 0) 541871a2c16STomasz Grobelny err = -EINVAL; 542871a2c16STomasz Grobelny else 543871a2c16STomasz Grobelny dp->dccps_tx_qlen = val; 544871a2c16STomasz Grobelny break; 545a84ffe43SArnaldo Carvalho de Melo default: 546a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 547a84ffe43SArnaldo Carvalho de Melo break; 548a84ffe43SArnaldo Carvalho de Melo } 549410e27a4SGerrit Renker release_sock(sk); 55019102996SGerrit Renker 551a84ffe43SArnaldo Carvalho de Melo return err; 5527c657876SArnaldo Carvalho de Melo } 5537c657876SArnaldo Carvalho de Melo 5543fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, 555b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 5563fdadf7dSDmitry Mishin { 5573fdadf7dSDmitry Mishin if (level != SOL_DCCP) 5583fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 5593fdadf7dSDmitry Mishin optname, optval, 5603fdadf7dSDmitry Mishin optlen); 5613fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5623fdadf7dSDmitry Mishin } 563543d9cfeSArnaldo Carvalho de Melo 564f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 565f21e68caSArnaldo Carvalho de Melo 5663fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5673fdadf7dSDmitry Mishin int compat_dccp_setsockopt(struct sock *sk, int level, int optname, 568b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 5693fdadf7dSDmitry Mishin { 570dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 571dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_setsockopt(sk, level, optname, 572dec73ff0SArnaldo Carvalho de Melo optval, optlen); 5733fdadf7dSDmitry Mishin return do_dccp_setsockopt(sk, level, optname, optval, optlen); 5743fdadf7dSDmitry Mishin } 575543d9cfeSArnaldo Carvalho de Melo 5763fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_setsockopt); 5773fdadf7dSDmitry Mishin #endif 5783fdadf7dSDmitry Mishin 57967e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 58060fe62e7SAndrea Bittau __be32 __user *optval, 58167e6b629SArnaldo Carvalho de Melo int __user *optlen) 58267e6b629SArnaldo Carvalho de Melo { 58367e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 58467e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 58567e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 58667e6b629SArnaldo Carvalho de Melo 58767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 58867e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 58967e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 59067e6b629SArnaldo Carvalho de Melo total_len += slen; 59167e6b629SArnaldo Carvalho de Melo } 59267e6b629SArnaldo Carvalho de Melo 59367e6b629SArnaldo Carvalho de Melo err = -EINVAL; 59467e6b629SArnaldo Carvalho de Melo if (total_len > len) 59567e6b629SArnaldo Carvalho de Melo goto out; 59667e6b629SArnaldo Carvalho de Melo 59767e6b629SArnaldo Carvalho de Melo err = 0; 59867e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 59967e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 60067e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 60167e6b629SArnaldo Carvalho de Melo err = -EFAULT; 60267e6b629SArnaldo Carvalho de Melo out: 60367e6b629SArnaldo Carvalho de Melo release_sock(sk); 60467e6b629SArnaldo Carvalho de Melo return err; 60567e6b629SArnaldo Carvalho de Melo } 60667e6b629SArnaldo Carvalho de Melo 6073fdadf7dSDmitry Mishin static int do_dccp_getsockopt(struct sock *sk, int level, int optname, 608a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 6097c657876SArnaldo Carvalho de Melo { 610a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 611a84ffe43SArnaldo Carvalho de Melo int val, len; 6127c657876SArnaldo Carvalho de Melo 613a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 614a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 615a84ffe43SArnaldo Carvalho de Melo 61639ebc027SArnaldo Carvalho de Melo if (len < (int)sizeof(int)) 617a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 618a84ffe43SArnaldo Carvalho de Melo 619a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 620a84ffe43SArnaldo Carvalho de Melo 621a84ffe43SArnaldo Carvalho de Melo switch (optname) { 622a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 6235aed3243SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n"); 624841bac1dSArnaldo Carvalho de Melo return 0; 62588f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 62688f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 62760fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 6287c559a9eSGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS: 6297c559a9eSGerrit Renker val = dp->dccps_mss_cache; 6307c559a9eSGerrit Renker break; 631d90ebcbfSGerrit Renker case DCCP_SOCKOPT_AVAILABLE_CCIDS: 632d90ebcbfSGerrit Renker return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen); 63371c262a3SGerrit Renker case DCCP_SOCKOPT_TX_CCID: 63471c262a3SGerrit Renker val = ccid_get_current_tx_ccid(dp); 63571c262a3SGerrit Renker if (val < 0) 63671c262a3SGerrit Renker return -ENOPROTOOPT; 63771c262a3SGerrit Renker break; 63871c262a3SGerrit Renker case DCCP_SOCKOPT_RX_CCID: 63971c262a3SGerrit Renker val = ccid_get_current_rx_ccid(dp); 64071c262a3SGerrit Renker if (val < 0) 64171c262a3SGerrit Renker return -ENOPROTOOPT; 64271c262a3SGerrit Renker break; 643b8599d20SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT: 644b8599d20SGerrit Renker val = dp->dccps_server_timewait; 645b8599d20SGerrit Renker break; 6466f4e5fffSGerrit Renker case DCCP_SOCKOPT_SEND_CSCOV: 6476f4e5fffSGerrit Renker val = dp->dccps_pcslen; 6486f4e5fffSGerrit Renker break; 6496f4e5fffSGerrit Renker case DCCP_SOCKOPT_RECV_CSCOV: 6506f4e5fffSGerrit Renker val = dp->dccps_pcrlen; 6516f4e5fffSGerrit Renker break; 652871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID: 653871a2c16STomasz Grobelny val = dp->dccps_qpolicy; 654871a2c16STomasz Grobelny break; 655871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_TXQLEN: 656871a2c16STomasz Grobelny val = dp->dccps_tx_qlen; 657871a2c16STomasz Grobelny break; 65888f964dbSArnaldo Carvalho de Melo case 128 ... 191: 65988f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 66088f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 66188f964dbSArnaldo Carvalho de Melo case 192 ... 255: 66288f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 66388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 664a84ffe43SArnaldo Carvalho de Melo default: 665a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 666a84ffe43SArnaldo Carvalho de Melo } 667a84ffe43SArnaldo Carvalho de Melo 66879133506SGerrit Renker len = sizeof(val); 669a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 670a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 671a84ffe43SArnaldo Carvalho de Melo 672a84ffe43SArnaldo Carvalho de Melo return 0; 6737c657876SArnaldo Carvalho de Melo } 6747c657876SArnaldo Carvalho de Melo 6753fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname, 6763fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6773fdadf7dSDmitry Mishin { 6783fdadf7dSDmitry Mishin if (level != SOL_DCCP) 6793fdadf7dSDmitry Mishin return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 6803fdadf7dSDmitry Mishin optname, optval, 6813fdadf7dSDmitry Mishin optlen); 6823fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6833fdadf7dSDmitry Mishin } 684543d9cfeSArnaldo Carvalho de Melo 685f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 686f21e68caSArnaldo Carvalho de Melo 6873fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6883fdadf7dSDmitry Mishin int compat_dccp_getsockopt(struct sock *sk, int level, int optname, 6893fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 6903fdadf7dSDmitry Mishin { 691dec73ff0SArnaldo Carvalho de Melo if (level != SOL_DCCP) 692dec73ff0SArnaldo Carvalho de Melo return inet_csk_compat_getsockopt(sk, level, optname, 693dec73ff0SArnaldo Carvalho de Melo optval, optlen); 6943fdadf7dSDmitry Mishin return do_dccp_getsockopt(sk, level, optname, optval, optlen); 6953fdadf7dSDmitry Mishin } 696543d9cfeSArnaldo Carvalho de Melo 6973fdadf7dSDmitry Mishin EXPORT_SYMBOL_GPL(compat_dccp_getsockopt); 6983fdadf7dSDmitry Mishin #endif 6993fdadf7dSDmitry Mishin 700871a2c16STomasz Grobelny static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb) 701871a2c16STomasz Grobelny { 702f95b414eSGu Zheng struct cmsghdr *cmsg; 703871a2c16STomasz Grobelny 704871a2c16STomasz Grobelny /* 705871a2c16STomasz Grobelny * Assign an (opaque) qpolicy priority value to skb->priority. 706871a2c16STomasz Grobelny * 707871a2c16STomasz Grobelny * We are overloading this skb field for use with the qpolicy subystem. 708871a2c16STomasz Grobelny * The skb->priority is normally used for the SO_PRIORITY option, which 709871a2c16STomasz Grobelny * is initialised from sk_priority. Since the assignment of sk_priority 710871a2c16STomasz Grobelny * to skb->priority happens later (on layer 3), we overload this field 711871a2c16STomasz Grobelny * for use with queueing priorities as long as the skb is on layer 4. 712871a2c16STomasz Grobelny * The default priority value (if nothing is set) is 0. 713871a2c16STomasz Grobelny */ 714871a2c16STomasz Grobelny skb->priority = 0; 715871a2c16STomasz Grobelny 716f95b414eSGu Zheng for_each_cmsghdr(cmsg, msg) { 717871a2c16STomasz Grobelny if (!CMSG_OK(msg, cmsg)) 718871a2c16STomasz Grobelny return -EINVAL; 719871a2c16STomasz Grobelny 720871a2c16STomasz Grobelny if (cmsg->cmsg_level != SOL_DCCP) 721871a2c16STomasz Grobelny continue; 722871a2c16STomasz Grobelny 72304910265STomasz Grobelny if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX && 72404910265STomasz Grobelny !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type)) 72504910265STomasz Grobelny return -EINVAL; 72604910265STomasz Grobelny 727871a2c16STomasz Grobelny switch (cmsg->cmsg_type) { 728871a2c16STomasz Grobelny case DCCP_SCM_PRIORITY: 729871a2c16STomasz Grobelny if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32))) 730871a2c16STomasz Grobelny return -EINVAL; 731871a2c16STomasz Grobelny skb->priority = *(__u32 *)CMSG_DATA(cmsg); 732871a2c16STomasz Grobelny break; 733871a2c16STomasz Grobelny default: 734871a2c16STomasz Grobelny return -EINVAL; 735871a2c16STomasz Grobelny } 736871a2c16STomasz Grobelny } 737871a2c16STomasz Grobelny return 0; 738871a2c16STomasz Grobelny } 739871a2c16STomasz Grobelny 7401b784140SYing Xue int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 7417c657876SArnaldo Carvalho de Melo { 7427c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 7437c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 7447c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 7457c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7467c657876SArnaldo Carvalho de Melo int rc, size; 7477c657876SArnaldo Carvalho de Melo long timeo; 7487c657876SArnaldo Carvalho de Melo 7497c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 7507c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 7517c657876SArnaldo Carvalho de Melo 7527c657876SArnaldo Carvalho de Melo lock_sock(sk); 753b1308dc0SIan McDonald 754871a2c16STomasz Grobelny if (dccp_qpolicy_full(sk)) { 755b1308dc0SIan McDonald rc = -EAGAIN; 756b1308dc0SIan McDonald goto out_release; 757b1308dc0SIan McDonald } 758b1308dc0SIan McDonald 75927258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 7607c657876SArnaldo Carvalho de Melo 7617c657876SArnaldo Carvalho de Melo /* 7627c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 7637c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 7647c657876SArnaldo Carvalho de Melo */ 7657c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 766cecd8d0eSGerrit Renker if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN)) 7677c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 76827258ee5SArnaldo Carvalho de Melo goto out_release; 7697c657876SArnaldo Carvalho de Melo 7707c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 7717c657876SArnaldo Carvalho de Melo release_sock(sk); 7727c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 7737c657876SArnaldo Carvalho de Melo lock_sock(sk); 7747c657876SArnaldo Carvalho de Melo if (skb == NULL) 7757c657876SArnaldo Carvalho de Melo goto out_release; 7767c657876SArnaldo Carvalho de Melo 7777c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 7786ce8e9ceSAl Viro rc = memcpy_from_msg(skb_put(skb, len), msg, len); 77927258ee5SArnaldo Carvalho de Melo if (rc != 0) 78027258ee5SArnaldo Carvalho de Melo goto out_discard; 7817c657876SArnaldo Carvalho de Melo 782871a2c16STomasz Grobelny rc = dccp_msghdr_parse(msg, skb); 783871a2c16STomasz Grobelny if (rc != 0) 784871a2c16STomasz Grobelny goto out_discard; 785871a2c16STomasz Grobelny 786871a2c16STomasz Grobelny dccp_qpolicy_push(sk, skb); 787b1fcf55eSGerrit Renker /* 788b1fcf55eSGerrit Renker * The xmit_timer is set if the TX CCID is rate-based and will expire 789b1fcf55eSGerrit Renker * when congestion control permits to release further packets into the 790b1fcf55eSGerrit Renker * network. Window-based CCIDs do not use this timer. 791b1fcf55eSGerrit Renker */ 792b1fcf55eSGerrit Renker if (!timer_pending(&dp->dccps_xmit_timer)) 793b1fcf55eSGerrit Renker dccp_write_xmit(sk); 7947c657876SArnaldo Carvalho de Melo out_release: 7957c657876SArnaldo Carvalho de Melo release_sock(sk); 7967c657876SArnaldo Carvalho de Melo return rc ? : len; 79727258ee5SArnaldo Carvalho de Melo out_discard: 79827258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 7997c657876SArnaldo Carvalho de Melo goto out_release; 8007c657876SArnaldo Carvalho de Melo } 8017c657876SArnaldo Carvalho de Melo 802f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 803f21e68caSArnaldo Carvalho de Melo 8041b784140SYing Xue int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, 8051b784140SYing Xue int flags, int *addr_len) 8067c657876SArnaldo Carvalho de Melo { 8077c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 8087c657876SArnaldo Carvalho de Melo long timeo; 8097c657876SArnaldo Carvalho de Melo 8107c657876SArnaldo Carvalho de Melo lock_sock(sk); 8117c657876SArnaldo Carvalho de Melo 812531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 813531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8147c657876SArnaldo Carvalho de Melo goto out; 815531669a0SArnaldo Carvalho de Melo } 8167c657876SArnaldo Carvalho de Melo 8177c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 8187c657876SArnaldo Carvalho de Melo 8197c657876SArnaldo Carvalho de Melo do { 820531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 8217c657876SArnaldo Carvalho de Melo 822531669a0SArnaldo Carvalho de Melo if (skb == NULL) 823531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 8247c657876SArnaldo Carvalho de Melo 8257c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 8267c657876SArnaldo Carvalho de Melo 8270c869620SGerrit Renker switch (dh->dccph_type) { 8280c869620SGerrit Renker case DCCP_PKT_DATA: 8290c869620SGerrit Renker case DCCP_PKT_DATAACK: 8307c657876SArnaldo Carvalho de Melo goto found_ok_skb; 8317c657876SArnaldo Carvalho de Melo 8320c869620SGerrit Renker case DCCP_PKT_CLOSE: 8330c869620SGerrit Renker case DCCP_PKT_CLOSEREQ: 8340c869620SGerrit Renker if (!(flags & MSG_PEEK)) 8350c869620SGerrit Renker dccp_finish_passive_close(sk); 8360c869620SGerrit Renker /* fall through */ 8370c869620SGerrit Renker case DCCP_PKT_RESET: 8380c869620SGerrit Renker dccp_pr_debug("found fin (%s) ok!\n", 8390c869620SGerrit Renker dccp_packet_name(dh->dccph_type)); 840531669a0SArnaldo Carvalho de Melo len = 0; 8417c657876SArnaldo Carvalho de Melo goto found_fin_ok; 8420c869620SGerrit Renker default: 8437690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 8447690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 8457bced397SDan Williams sk_eat_skb(sk, skb); 8460c869620SGerrit Renker } 847531669a0SArnaldo Carvalho de Melo verify_sock_status: 848531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 849531669a0SArnaldo Carvalho de Melo len = 0; 8507c657876SArnaldo Carvalho de Melo break; 8517c657876SArnaldo Carvalho de Melo } 8527c657876SArnaldo Carvalho de Melo 853531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 854531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 8557c657876SArnaldo Carvalho de Melo break; 856531669a0SArnaldo Carvalho de Melo } 857531669a0SArnaldo Carvalho de Melo 858531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 859531669a0SArnaldo Carvalho de Melo len = 0; 860531669a0SArnaldo Carvalho de Melo break; 861531669a0SArnaldo Carvalho de Melo } 8627c657876SArnaldo Carvalho de Melo 8637c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 8647c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 8657c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 8667c657876SArnaldo Carvalho de Melo * from never connected socket. 8677c657876SArnaldo Carvalho de Melo */ 868531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 8697c657876SArnaldo Carvalho de Melo break; 8707c657876SArnaldo Carvalho de Melo } 871531669a0SArnaldo Carvalho de Melo len = 0; 8727c657876SArnaldo Carvalho de Melo break; 8737c657876SArnaldo Carvalho de Melo } 8747c657876SArnaldo Carvalho de Melo 8757c657876SArnaldo Carvalho de Melo if (!timeo) { 876531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 8777c657876SArnaldo Carvalho de Melo break; 8787c657876SArnaldo Carvalho de Melo } 8797c657876SArnaldo Carvalho de Melo 8807c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 881531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 8827c657876SArnaldo Carvalho de Melo break; 8837c657876SArnaldo Carvalho de Melo } 8847c657876SArnaldo Carvalho de Melo 885dfbafc99SSabrina Dubroca sk_wait_data(sk, &timeo, NULL); 8867c657876SArnaldo Carvalho de Melo continue; 8877c657876SArnaldo Carvalho de Melo found_ok_skb: 888531669a0SArnaldo Carvalho de Melo if (len > skb->len) 889531669a0SArnaldo Carvalho de Melo len = skb->len; 890531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 891531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 8927c657876SArnaldo Carvalho de Melo 89351f3d02bSDavid S. Miller if (skb_copy_datagram_msg(skb, 0, msg, len)) { 8947c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 895531669a0SArnaldo Carvalho de Melo len = -EFAULT; 8967c657876SArnaldo Carvalho de Melo break; 8977c657876SArnaldo Carvalho de Melo } 89855d95590SGerrit Renker if (flags & MSG_TRUNC) 89955d95590SGerrit Renker len = skb->len; 9007c657876SArnaldo Carvalho de Melo found_fin_ok: 9017c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 9027bced397SDan Williams sk_eat_skb(sk, skb); 9037c657876SArnaldo Carvalho de Melo break; 904531669a0SArnaldo Carvalho de Melo } while (1); 9057c657876SArnaldo Carvalho de Melo out: 9067c657876SArnaldo Carvalho de Melo release_sock(sk); 907531669a0SArnaldo Carvalho de Melo return len; 9087c657876SArnaldo Carvalho de Melo } 9097c657876SArnaldo Carvalho de Melo 910f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 911f21e68caSArnaldo Carvalho de Melo 912f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 9137c657876SArnaldo Carvalho de Melo { 9147c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 9157c657876SArnaldo Carvalho de Melo unsigned char old_state; 9167c657876SArnaldo Carvalho de Melo int err; 9177c657876SArnaldo Carvalho de Melo 9187c657876SArnaldo Carvalho de Melo lock_sock(sk); 9197c657876SArnaldo Carvalho de Melo 9207c657876SArnaldo Carvalho de Melo err = -EINVAL; 9217c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 9227c657876SArnaldo Carvalho de Melo goto out; 9237c657876SArnaldo Carvalho de Melo 9247c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 9257c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 9267c657876SArnaldo Carvalho de Melo goto out; 9277c657876SArnaldo Carvalho de Melo 9287c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 9297c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 9307c657876SArnaldo Carvalho de Melo */ 9317c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 9327c657876SArnaldo Carvalho de Melo /* 9337c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 9347c657876SArnaldo Carvalho de Melo * see tcp_listen_start 9357c657876SArnaldo Carvalho de Melo */ 93672a3effaSEric Dumazet err = dccp_listen_start(sk, backlog); 9377c657876SArnaldo Carvalho de Melo if (err) 9387c657876SArnaldo Carvalho de Melo goto out; 9397c657876SArnaldo Carvalho de Melo } 9407c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 9417c657876SArnaldo Carvalho de Melo err = 0; 9427c657876SArnaldo Carvalho de Melo 9437c657876SArnaldo Carvalho de Melo out: 9447c657876SArnaldo Carvalho de Melo release_sock(sk); 9457c657876SArnaldo Carvalho de Melo return err; 9467c657876SArnaldo Carvalho de Melo } 9477c657876SArnaldo Carvalho de Melo 948f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 949f21e68caSArnaldo Carvalho de Melo 9500c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk) 9517c657876SArnaldo Carvalho de Melo { 9520c869620SGerrit Renker u8 next_state = DCCP_CLOSED; 9537c657876SArnaldo Carvalho de Melo 9540c869620SGerrit Renker switch (sk->sk_state) { 9550c869620SGerrit Renker case DCCP_PASSIVE_CLOSE: 9560c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ: 9570c869620SGerrit Renker dccp_finish_passive_close(sk); 9580c869620SGerrit Renker break; 9590c869620SGerrit Renker case DCCP_PARTOPEN: 9600c869620SGerrit Renker dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk); 9610c869620SGerrit Renker inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 9620c869620SGerrit Renker /* fall through */ 9630c869620SGerrit Renker case DCCP_OPEN: 9640c869620SGerrit Renker dccp_send_close(sk, 1); 9657c657876SArnaldo Carvalho de Melo 966b8599d20SGerrit Renker if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER && 967b8599d20SGerrit Renker !dccp_sk(sk)->dccps_server_timewait) 9680c869620SGerrit Renker next_state = DCCP_ACTIVE_CLOSEREQ; 9690c869620SGerrit Renker else 9700c869620SGerrit Renker next_state = DCCP_CLOSING; 9710c869620SGerrit Renker /* fall through */ 9720c869620SGerrit Renker default: 9730c869620SGerrit Renker dccp_set_state(sk, next_state); 9740c869620SGerrit Renker } 9757c657876SArnaldo Carvalho de Melo } 9767c657876SArnaldo Carvalho de Melo 9777c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 9787c657876SArnaldo Carvalho de Melo { 97997e5848dSIan McDonald struct dccp_sock *dp = dccp_sk(sk); 9807c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 981d83bd95bSGerrit Renker u32 data_was_unread = 0; 982134af346SHerbert Xu int state; 9837c657876SArnaldo Carvalho de Melo 9847c657876SArnaldo Carvalho de Melo lock_sock(sk); 9857c657876SArnaldo Carvalho de Melo 9867c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 9877c657876SArnaldo Carvalho de Melo 9887c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 9897c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 9907c657876SArnaldo Carvalho de Melo 9917c657876SArnaldo Carvalho de Melo /* Special case. */ 9927c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 9937c657876SArnaldo Carvalho de Melo 9947c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 9957c657876SArnaldo Carvalho de Melo } 9967c657876SArnaldo Carvalho de Melo 99797e5848dSIan McDonald sk_stop_timer(sk, &dp->dccps_xmit_timer); 99897e5848dSIan McDonald 9997c657876SArnaldo Carvalho de Melo /* 10007c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 10017c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 10027c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 10037c657876SArnaldo Carvalho de Melo */ 10047c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 1005d83bd95bSGerrit Renker data_was_unread += skb->len; 10067c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 10077c657876SArnaldo Carvalho de Melo } 10087c657876SArnaldo Carvalho de Melo 1009346da62cSEric Dumazet /* If socket has been already reset kill it. */ 1010346da62cSEric Dumazet if (sk->sk_state == DCCP_CLOSED) 1011346da62cSEric Dumazet goto adjudge_to_death; 1012346da62cSEric Dumazet 1013d83bd95bSGerrit Renker if (data_was_unread) { 1014d83bd95bSGerrit Renker /* Unread data was tossed, send an appropriate Reset Code */ 10152f34b329SGerrit Renker DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread); 1016d83bd95bSGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); 1017d83bd95bSGerrit Renker dccp_set_state(sk, DCCP_CLOSED); 1018d83bd95bSGerrit Renker } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 10197c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 10207c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 10210c869620SGerrit Renker } else if (sk->sk_state != DCCP_CLOSED) { 1022b1fcf55eSGerrit Renker /* 1023b1fcf55eSGerrit Renker * Normal connection termination. May need to wait if there are 1024b1fcf55eSGerrit Renker * still packets in the TX queue that are delayed by the CCID. 1025b1fcf55eSGerrit Renker */ 1026b1fcf55eSGerrit Renker dccp_flush_write_queue(sk, &timeout); 10270c869620SGerrit Renker dccp_terminate_connection(sk); 10287c657876SArnaldo Carvalho de Melo } 10297c657876SArnaldo Carvalho de Melo 1030b1fcf55eSGerrit Renker /* 1031b1fcf55eSGerrit Renker * Flush write queue. This may be necessary in several cases: 1032b1fcf55eSGerrit Renker * - we have been closed by the peer but still have application data; 1033b1fcf55eSGerrit Renker * - abortive termination (unread data or zero linger time), 1034b1fcf55eSGerrit Renker * - normal termination but queue could not be flushed within time limit 1035b1fcf55eSGerrit Renker */ 1036b1fcf55eSGerrit Renker __skb_queue_purge(&sk->sk_write_queue); 1037b1fcf55eSGerrit Renker 10387c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 10397c657876SArnaldo Carvalho de Melo 10407c657876SArnaldo Carvalho de Melo adjudge_to_death: 1041134af346SHerbert Xu state = sk->sk_state; 1042134af346SHerbert Xu sock_hold(sk); 1043134af346SHerbert Xu sock_orphan(sk); 1044134af346SHerbert Xu 10457ad07e7cSArnaldo Carvalho de Melo /* 10467ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 10477ad07e7cSArnaldo Carvalho de Melo */ 10487c657876SArnaldo Carvalho de Melo release_sock(sk); 10497c657876SArnaldo Carvalho de Melo /* 10507c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 10517c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 10527c657876SArnaldo Carvalho de Melo */ 10537c657876SArnaldo Carvalho de Melo local_bh_disable(); 10547c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 1055547b792cSIlpo Järvinen WARN_ON(sock_owned_by_user(sk)); 10567c657876SArnaldo Carvalho de Melo 1057eb4dea58SHerbert Xu percpu_counter_inc(sk->sk_prot->orphan_count); 1058eb4dea58SHerbert Xu 1059134af346SHerbert Xu /* Have we already been destroyed by a softirq or backlog? */ 1060134af346SHerbert Xu if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED) 1061134af346SHerbert Xu goto out; 10627c657876SArnaldo Carvalho de Melo 10637c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 10647c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 10657c657876SArnaldo Carvalho de Melo 10667c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 10677c657876SArnaldo Carvalho de Melo 1068134af346SHerbert Xu out: 10697c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 10707c657876SArnaldo Carvalho de Melo local_bh_enable(); 10717c657876SArnaldo Carvalho de Melo sock_put(sk); 10727c657876SArnaldo Carvalho de Melo } 10737c657876SArnaldo Carvalho de Melo 1074f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 1075f21e68caSArnaldo Carvalho de Melo 10767c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 10777c657876SArnaldo Carvalho de Melo { 10788e8c71f1SGerrit Renker dccp_pr_debug("called shutdown(%x)\n", how); 10797c657876SArnaldo Carvalho de Melo } 10807c657876SArnaldo Carvalho de Melo 1081f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 1082f21e68caSArnaldo Carvalho de Melo 10830c5b8a46SFabian Frederick static inline int __init dccp_mib_init(void) 10847c657876SArnaldo Carvalho de Melo { 1085698365faSWANG Cong dccp_statistics = alloc_percpu(struct dccp_mib); 1086698365faSWANG Cong if (!dccp_statistics) 1087698365faSWANG Cong return -ENOMEM; 1088698365faSWANG Cong return 0; 10897c657876SArnaldo Carvalho de Melo } 10907c657876SArnaldo Carvalho de Melo 109124e8b7e4SYOSHIFUJI Hideaki static inline void dccp_mib_exit(void) 109246f09ffaSArnaldo Carvalho de Melo { 1093698365faSWANG Cong free_percpu(dccp_statistics); 109446f09ffaSArnaldo Carvalho de Melo } 109546f09ffaSArnaldo Carvalho de Melo 10967c657876SArnaldo Carvalho de Melo static int thash_entries; 10977c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 10987c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 10997c657876SArnaldo Carvalho de Melo 1100a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 1101eb939922SRusty Russell bool dccp_debug; 110243264991SGerrit Renker module_param(dccp_debug, bool, 0644); 11037c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 1104f21e68caSArnaldo Carvalho de Melo 1105f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 1106a1d3a355SArnaldo Carvalho de Melo #endif 11077c657876SArnaldo Carvalho de Melo 11087c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 11097c657876SArnaldo Carvalho de Melo { 11107c657876SArnaldo Carvalho de Melo unsigned long goal; 11117c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 1112dd24c001SEric Dumazet int rc; 11137c657876SArnaldo Carvalho de Melo 1114028b0275SPatrick McHardy BUILD_BUG_ON(sizeof(struct dccp_skb_cb) > 1115028b0275SPatrick McHardy FIELD_SIZEOF(struct sk_buff, cb)); 1116908c7f19STejun Heo rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL); 1117dd24c001SEric Dumazet if (rc) 1118d14a0ebdSGerrit Renker goto out_fail; 1119dd24c001SEric Dumazet rc = -ENOBUFS; 11205caea4eaSEric Dumazet inet_hashinfo_init(&dccp_hashinfo); 11217690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 11227690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 11237690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 112420c2df83SPaul Mundt SLAB_HWCACHE_ALIGN, NULL); 11257c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 1126dd24c001SEric Dumazet goto out_free_percpu; 11277c657876SArnaldo Carvalho de Melo 11287c657876SArnaldo Carvalho de Melo /* 11297c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 11307c657876SArnaldo Carvalho de Melo * hash tables. 11317c657876SArnaldo Carvalho de Melo * 11327c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 11337c657876SArnaldo Carvalho de Melo */ 11344481374cSJan Beulich if (totalram_pages >= (128 * 1024)) 11354481374cSJan Beulich goal = totalram_pages >> (21 - PAGE_SHIFT); 11367c657876SArnaldo Carvalho de Melo else 11374481374cSJan Beulich goal = totalram_pages >> (23 - PAGE_SHIFT); 11387c657876SArnaldo Carvalho de Melo 11397c657876SArnaldo Carvalho de Melo if (thash_entries) 11407690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 11417690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 11427c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 11437c657876SArnaldo Carvalho de Melo ; 11447c657876SArnaldo Carvalho de Melo do { 1145f373b53bSEric Dumazet unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE / 11467c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 1147f373b53bSEric Dumazet 1148f373b53bSEric Dumazet while (hash_size & (hash_size - 1)) 1149f373b53bSEric Dumazet hash_size--; 1150f373b53bSEric Dumazet dccp_hashinfo.ehash_mask = hash_size - 1; 11517c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 11521c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order); 11537c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 11547c657876SArnaldo Carvalho de Melo 11557c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 115659348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP established hash table"); 11577c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 11587c657876SArnaldo Carvalho de Melo } 11597c657876SArnaldo Carvalho de Melo 116005dbc7b5SEric Dumazet for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) 11613ab5aee7SEric Dumazet INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i); 11627c657876SArnaldo Carvalho de Melo 1163230140cfSEric Dumazet if (inet_ehash_locks_alloc(&dccp_hashinfo)) 1164230140cfSEric Dumazet goto out_free_dccp_ehash; 1165230140cfSEric Dumazet 11667c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 11677c657876SArnaldo Carvalho de Melo 11687c657876SArnaldo Carvalho de Melo do { 11697c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 11707c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 11717690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 11727690af3fSArnaldo Carvalho de Melo bhash_order > 0) 11737c657876SArnaldo Carvalho de Melo continue; 11747c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 11751c29b3ffSMel Gorman __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order); 11767c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 11777c657876SArnaldo Carvalho de Melo 11787c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 117959348b19SGerrit Renker DCCP_CRIT("Failed to allocate DCCP bind hash table"); 1180230140cfSEric Dumazet goto out_free_dccp_locks; 11817c657876SArnaldo Carvalho de Melo } 11827c657876SArnaldo Carvalho de Melo 11837c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 11847c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 11857c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 11867c657876SArnaldo Carvalho de Melo } 11877c657876SArnaldo Carvalho de Melo 118846f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1189fa23e2ecSArnaldo Carvalho de Melo if (rc) 11907c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 11917c657876SArnaldo Carvalho de Melo 11929b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 11937c657876SArnaldo Carvalho de Melo if (rc) 1194b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 11959b07ef5dSArnaldo Carvalho de Melo 1196e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 11979b07ef5dSArnaldo Carvalho de Melo if (rc) 11989b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 11994c70f383SGerrit Renker 1200ddebc973SGerrit Renker rc = ccid_initialize_builtins(); 1201ddebc973SGerrit Renker if (rc) 1202ddebc973SGerrit Renker goto out_sysctl_exit; 1203ddebc973SGerrit Renker 12044c70f383SGerrit Renker dccp_timestamping_init(); 1205d14a0ebdSGerrit Renker 1206d14a0ebdSGerrit Renker return 0; 1207d14a0ebdSGerrit Renker 1208ddebc973SGerrit Renker out_sysctl_exit: 1209ddebc973SGerrit Renker dccp_sysctl_exit(); 12109b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 12119b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1212b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 121346f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 12147c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 12157c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 1216230140cfSEric Dumazet out_free_dccp_locks: 1217230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12187c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 12197c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 12207c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 12217c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 1222dd24c001SEric Dumazet out_free_percpu: 1223dd24c001SEric Dumazet percpu_counter_destroy(&dccp_orphan_count); 1224d14a0ebdSGerrit Renker out_fail: 1225d14a0ebdSGerrit Renker dccp_hashinfo.bhash = NULL; 1226d14a0ebdSGerrit Renker dccp_hashinfo.ehash = NULL; 1227d14a0ebdSGerrit Renker dccp_hashinfo.bind_bucket_cachep = NULL; 1228d14a0ebdSGerrit Renker return rc; 12297c657876SArnaldo Carvalho de Melo } 12307c657876SArnaldo Carvalho de Melo 12317c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 12327c657876SArnaldo Carvalho de Melo { 1233ddebc973SGerrit Renker ccid_cleanup_builtins(); 123446f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1235725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1236725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1237725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1238725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1239f373b53bSEric Dumazet get_order((dccp_hashinfo.ehash_mask + 1) * 1240725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 1241230140cfSEric Dumazet inet_ehash_locks_free(&dccp_hashinfo); 12427c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 12439b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1244e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 1245476181cbSWei Yongjun percpu_counter_destroy(&dccp_orphan_count); 12467c657876SArnaldo Carvalho de Melo } 12477c657876SArnaldo Carvalho de Melo 12487c657876SArnaldo Carvalho de Melo module_init(dccp_init); 12497c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 12507c657876SArnaldo Carvalho de Melo 12517c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 12527c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 12537c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1254