17c657876SArnaldo Carvalho de Melo /* 27c657876SArnaldo Carvalho de Melo * net/dccp/proto.c 37c657876SArnaldo Carvalho de Melo * 47c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol 57c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 67c657876SArnaldo Carvalho de Melo * 77c657876SArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or modify it 87c657876SArnaldo Carvalho de Melo * under the terms of the GNU General Public License version 2 as 97c657876SArnaldo Carvalho de Melo * published by the Free Software Foundation. 107c657876SArnaldo Carvalho de Melo */ 117c657876SArnaldo Carvalho de Melo 127c657876SArnaldo Carvalho de Melo #include <linux/config.h> 137c657876SArnaldo Carvalho de Melo #include <linux/dccp.h> 147c657876SArnaldo Carvalho de Melo #include <linux/module.h> 157c657876SArnaldo Carvalho de Melo #include <linux/types.h> 167c657876SArnaldo Carvalho de Melo #include <linux/sched.h> 177c657876SArnaldo Carvalho de Melo #include <linux/kernel.h> 187c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h> 197c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h> 207c657876SArnaldo Carvalho de Melo #include <linux/in.h> 217c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h> 227c657876SArnaldo Carvalho de Melo #include <linux/init.h> 237c657876SArnaldo Carvalho de Melo #include <linux/random.h> 247c657876SArnaldo Carvalho de Melo #include <net/checksum.h> 257c657876SArnaldo Carvalho de Melo 267c657876SArnaldo Carvalho de Melo #include <net/inet_common.h> 2714c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h> 287c657876SArnaldo Carvalho de Melo #include <net/protocol.h> 297c657876SArnaldo Carvalho de Melo #include <net/sock.h> 307c657876SArnaldo Carvalho de Melo #include <net/xfrm.h> 317c657876SArnaldo Carvalho de Melo 327c657876SArnaldo Carvalho de Melo #include <asm/semaphore.h> 337c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h> 347c657876SArnaldo Carvalho de Melo #include <linux/timer.h> 357c657876SArnaldo Carvalho de Melo #include <linux/delay.h> 367c657876SArnaldo Carvalho de Melo #include <linux/poll.h> 377c657876SArnaldo Carvalho de Melo 387c657876SArnaldo Carvalho de Melo #include "ccid.h" 397c657876SArnaldo Carvalho de Melo #include "dccp.h" 40afe00251SAndrea Bittau #include "feat.h" 417c657876SArnaldo Carvalho de Melo 42ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 437c657876SArnaldo Carvalho de Melo 44f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics); 45f21e68caSArnaldo Carvalho de Melo 467c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 477c657876SArnaldo Carvalho de Melo 48f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 49f21e68caSArnaldo Carvalho de Melo 50075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 51075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 52075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 53075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 54075ae866SArnaldo Carvalho de Melo }; 55075ae866SArnaldo Carvalho de Melo 56075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 57075ae866SArnaldo Carvalho de Melo 587c657876SArnaldo Carvalho de Melo static struct net_protocol dccp_protocol = { 597c657876SArnaldo Carvalho de Melo .handler = dccp_v4_rcv, 607c657876SArnaldo Carvalho de Melo .err_handler = dccp_v4_err, 61a516b049SPatrick McHardy .no_policy = 1, 627c657876SArnaldo Carvalho de Melo }; 637c657876SArnaldo Carvalho de Melo 647c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 657c657876SArnaldo Carvalho de Melo { 667c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 677c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 687c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 697c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 707c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 717c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 727c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 737c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 747c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 757c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 767c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 777c657876SArnaldo Carvalho de Melo }; 787c657876SArnaldo Carvalho de Melo 797c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 807c657876SArnaldo Carvalho de Melo return "INVALID"; 817c657876SArnaldo Carvalho de Melo else 827c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 837c657876SArnaldo Carvalho de Melo } 847c657876SArnaldo Carvalho de Melo 857c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 867c657876SArnaldo Carvalho de Melo 877c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 887c657876SArnaldo Carvalho de Melo { 897c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 907c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 917c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 927c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 937c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 947c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 957c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 967c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 977c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 987c657876SArnaldo Carvalho de Melo }; 997c657876SArnaldo Carvalho de Melo 1007c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 1017c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 1027c657876SArnaldo Carvalho de Melo else 1037c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 1047c657876SArnaldo Carvalho de Melo } 1057c657876SArnaldo Carvalho de Melo 1067c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 1077c657876SArnaldo Carvalho de Melo 108c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 109c985ed70SArnaldo Carvalho de Melo { 110c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 111c985ed70SArnaldo Carvalho de Melo } 112c985ed70SArnaldo Carvalho de Melo 113c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 114c985ed70SArnaldo Carvalho de Melo 115c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 116c985ed70SArnaldo Carvalho de Melo { 117c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 118c985ed70SArnaldo Carvalho de Melo } 119c985ed70SArnaldo Carvalho de Melo 120c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 121c985ed70SArnaldo Carvalho de Melo 1223e0fadc5SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk) 1233e0fadc5SArnaldo Carvalho de Melo { 1243e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1253e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1263e0fadc5SArnaldo Carvalho de Melo static int dccp_ctl_socket_init = 1; 1273e0fadc5SArnaldo Carvalho de Melo 1283e0fadc5SArnaldo Carvalho de Melo dccp_options_init(&dp->dccps_options); 1293e0fadc5SArnaldo Carvalho de Melo do_gettimeofday(&dp->dccps_epoch); 1303e0fadc5SArnaldo Carvalho de Melo 1313e0fadc5SArnaldo Carvalho de Melo /* 1323e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1333e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1343e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1353e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1363e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1373e0fadc5SArnaldo Carvalho de Melo */ 1383e0fadc5SArnaldo Carvalho de Melo if (likely(!dccp_ctl_socket_init)) { 1393e0fadc5SArnaldo Carvalho de Melo int rc = dccp_feat_init(sk); 1403e0fadc5SArnaldo Carvalho de Melo 1413e0fadc5SArnaldo Carvalho de Melo if (rc) 1423e0fadc5SArnaldo Carvalho de Melo return rc; 1433e0fadc5SArnaldo Carvalho de Melo 1443e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 1453e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1463e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1473e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1483e0fadc5SArnaldo Carvalho de Melo } 1493e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = 1503e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_new(dp->dccps_options.dccpo_rx_ccid, 1513e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1523e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = 1533e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_new(dp->dccps_options.dccpo_tx_ccid, 1543e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1553e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1563e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 1573e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 1583e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 1593e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 1603e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 1613e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 1623e0fadc5SArnaldo Carvalho de Melo } 1633e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 1643e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1653e0fadc5SArnaldo Carvalho de Melo } 1663e0fadc5SArnaldo Carvalho de Melo } else { 1673e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 1683e0fadc5SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending); 1693e0fadc5SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf); 1703e0fadc5SArnaldo Carvalho de Melo dccp_ctl_socket_init = 0; 1713e0fadc5SArnaldo Carvalho de Melo } 1723e0fadc5SArnaldo Carvalho de Melo 1733e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 1743e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 1753e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 1763e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 1773e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 1783e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 1793e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 1803e0fadc5SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_INVALID_VALUE; 1813e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 1823e0fadc5SArnaldo Carvalho de Melo 1833e0fadc5SArnaldo Carvalho de Melo return 0; 1843e0fadc5SArnaldo Carvalho de Melo } 1853e0fadc5SArnaldo Carvalho de Melo 1863e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 1873e0fadc5SArnaldo Carvalho de Melo 1883e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 1893e0fadc5SArnaldo Carvalho de Melo { 1903e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1913e0fadc5SArnaldo Carvalho de Melo 1923e0fadc5SArnaldo Carvalho de Melo /* 1933e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 1943e0fadc5SArnaldo Carvalho de Melo * for retransmissions 1953e0fadc5SArnaldo Carvalho de Melo */ 1963e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 1973e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 1983e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 1993e0fadc5SArnaldo Carvalho de Melo } 2003e0fadc5SArnaldo Carvalho de Melo 2013e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 2023e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 2033e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 2043e0fadc5SArnaldo Carvalho de Melo 2053e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 2063e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 2073e0fadc5SArnaldo Carvalho de Melo 2083e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 2093e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2103e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2113e0fadc5SArnaldo Carvalho de Melo } 2123e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2133e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2143e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2153e0fadc5SArnaldo Carvalho de Melo 2163e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2173e0fadc5SArnaldo Carvalho de Melo dccp_feat_clean(sk); 2183e0fadc5SArnaldo Carvalho de Melo 2193e0fadc5SArnaldo Carvalho de Melo return 0; 2203e0fadc5SArnaldo Carvalho de Melo } 2213e0fadc5SArnaldo Carvalho de Melo 2223e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2233e0fadc5SArnaldo Carvalho de Melo 2247c657876SArnaldo Carvalho de Melo static inline int dccp_listen_start(struct sock *sk) 2257c657876SArnaldo Carvalho de Melo { 22667e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 22767e6b629SArnaldo Carvalho de Melo 22867e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 22967e6b629SArnaldo Carvalho de Melo /* 23067e6b629SArnaldo Carvalho de Melo * Apps need to use setsockopt(DCCP_SOCKOPT_SERVICE) 23167e6b629SArnaldo Carvalho de Melo * before calling listen() 23267e6b629SArnaldo Carvalho de Melo */ 23367e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 23467e6b629SArnaldo Carvalho de Melo return -EPROTO; 2357c657876SArnaldo Carvalho de Melo return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE); 2367c657876SArnaldo Carvalho de Melo } 2377c657876SArnaldo Carvalho de Melo 2387c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2397c657876SArnaldo Carvalho de Melo { 2407c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2417c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2427c657876SArnaldo Carvalho de Melo int err = 0; 2437c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2447c657876SArnaldo Carvalho de Melo 2457c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2467c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2477c657876SArnaldo Carvalho de Melo 2487c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2497c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2507c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2517c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2527c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2537c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2547c657876SArnaldo Carvalho de Melo 2557c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2567c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2577c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2587c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2597c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2607c657876SArnaldo Carvalho de Melo } 2617c657876SArnaldo Carvalho de Melo 2627c657876SArnaldo Carvalho de Melo inet->dport = 0; 2637c657876SArnaldo Carvalho de Melo 2647c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 2657c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 2667c657876SArnaldo Carvalho de Melo 2677c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 2687c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 2697c657876SArnaldo Carvalho de Melo 2707c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 2717c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 2727c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 2737c657876SArnaldo Carvalho de Melo 2747c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 2757c657876SArnaldo Carvalho de Melo 2767c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 2777c657876SArnaldo Carvalho de Melo return err; 2787c657876SArnaldo Carvalho de Melo } 2797c657876SArnaldo Carvalho de Melo 280f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 281f21e68caSArnaldo Carvalho de Melo 282331968bdSArnaldo Carvalho de Melo /* 283331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 284331968bdSArnaldo Carvalho de Melo * 285331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 286331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 287331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 288331968bdSArnaldo Carvalho de Melo */ 289f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 290331968bdSArnaldo Carvalho de Melo poll_table *wait) 291331968bdSArnaldo Carvalho de Melo { 292331968bdSArnaldo Carvalho de Melo unsigned int mask; 293331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 294331968bdSArnaldo Carvalho de Melo 295331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 296331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 297331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 298331968bdSArnaldo Carvalho de Melo 299331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 300331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 301331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 302331968bdSArnaldo Carvalho de Melo */ 303331968bdSArnaldo Carvalho de Melo 304331968bdSArnaldo Carvalho de Melo mask = 0; 305331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 306331968bdSArnaldo Carvalho de Melo mask = POLLERR; 307331968bdSArnaldo Carvalho de Melo 308331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 309331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 310331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 311331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 312331968bdSArnaldo Carvalho de Melo 313331968bdSArnaldo Carvalho de Melo /* Connected? */ 314331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 315331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 316331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 317331968bdSArnaldo Carvalho de Melo 318331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 319331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 320331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 321331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 322331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 323331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 324331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 325331968bdSArnaldo Carvalho de Melo 326331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 327331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 328331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 329331968bdSArnaldo Carvalho de Melo */ 330331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 331331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 332331968bdSArnaldo Carvalho de Melo } 333331968bdSArnaldo Carvalho de Melo } 334331968bdSArnaldo Carvalho de Melo } 335331968bdSArnaldo Carvalho de Melo return mask; 336331968bdSArnaldo Carvalho de Melo } 337331968bdSArnaldo Carvalho de Melo 338f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 339f21e68caSArnaldo Carvalho de Melo 3407c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3417c657876SArnaldo Carvalho de Melo { 3427c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3437c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3447c657876SArnaldo Carvalho de Melo } 3457c657876SArnaldo Carvalho de Melo 346f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 347f21e68caSArnaldo Carvalho de Melo 34860fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 34967e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 35067e6b629SArnaldo Carvalho de Melo { 35167e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 35267e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 35367e6b629SArnaldo Carvalho de Melo 35467e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 35567e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 35667e6b629SArnaldo Carvalho de Melo return -EINVAL; 35767e6b629SArnaldo Carvalho de Melo 35867e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 35967e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 36067e6b629SArnaldo Carvalho de Melo if (sl == NULL) 36167e6b629SArnaldo Carvalho de Melo return -ENOMEM; 36267e6b629SArnaldo Carvalho de Melo 36367e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 36467e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 36567e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 36667e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 36767e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 36867e6b629SArnaldo Carvalho de Melo kfree(sl); 36967e6b629SArnaldo Carvalho de Melo return -EFAULT; 37067e6b629SArnaldo Carvalho de Melo } 37167e6b629SArnaldo Carvalho de Melo } 37267e6b629SArnaldo Carvalho de Melo 37367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 37467e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 37567e6b629SArnaldo Carvalho de Melo 37667e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 37767e6b629SArnaldo Carvalho de Melo 37867e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 37967e6b629SArnaldo Carvalho de Melo release_sock(sk); 38067e6b629SArnaldo Carvalho de Melo return 0; 38167e6b629SArnaldo Carvalho de Melo } 38267e6b629SArnaldo Carvalho de Melo 383afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 384afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 385afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 386afe00251SAndrea Bittau { 387afe00251SAndrea Bittau struct dccp_so_feat opt; 388afe00251SAndrea Bittau u8 *val; 389afe00251SAndrea Bittau int rc; 390afe00251SAndrea Bittau 391afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 392afe00251SAndrea Bittau return -EFAULT; 393afe00251SAndrea Bittau 394afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 395afe00251SAndrea Bittau if (!val) 396afe00251SAndrea Bittau return -ENOMEM; 397afe00251SAndrea Bittau 398afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 399afe00251SAndrea Bittau rc = -EFAULT; 400afe00251SAndrea Bittau goto out_free_val; 401afe00251SAndrea Bittau } 402afe00251SAndrea Bittau 403afe00251SAndrea Bittau rc = dccp_feat_change(sk, type, opt.dccpsf_feat, val, opt.dccpsf_len, 404afe00251SAndrea Bittau GFP_KERNEL); 405afe00251SAndrea Bittau if (rc) 406afe00251SAndrea Bittau goto out_free_val; 407afe00251SAndrea Bittau 408afe00251SAndrea Bittau out: 409afe00251SAndrea Bittau return rc; 410afe00251SAndrea Bittau 411afe00251SAndrea Bittau out_free_val: 412afe00251SAndrea Bittau kfree(val); 413afe00251SAndrea Bittau goto out; 414afe00251SAndrea Bittau } 415afe00251SAndrea Bittau 4167c657876SArnaldo Carvalho de Melo int dccp_setsockopt(struct sock *sk, int level, int optname, 417a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4187c657876SArnaldo Carvalho de Melo { 419a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 420a84ffe43SArnaldo Carvalho de Melo int err; 421a84ffe43SArnaldo Carvalho de Melo int val; 4227c657876SArnaldo Carvalho de Melo 4237c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 42457cca05aSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 42557cca05aSArnaldo Carvalho de Melo optname, optval, 42657cca05aSArnaldo Carvalho de Melo optlen); 4277c657876SArnaldo Carvalho de Melo 428a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 429a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 430a84ffe43SArnaldo Carvalho de Melo 431a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 432a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 433a84ffe43SArnaldo Carvalho de Melo 43467e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 43567e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 436a84ffe43SArnaldo Carvalho de Melo 43767e6b629SArnaldo Carvalho de Melo lock_sock(sk); 438a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 439a84ffe43SArnaldo Carvalho de Melo err = 0; 440a84ffe43SArnaldo Carvalho de Melo 441a84ffe43SArnaldo Carvalho de Melo switch (optname) { 442a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 443a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 444a84ffe43SArnaldo Carvalho de Melo break; 445afe00251SAndrea Bittau 446afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 447afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 448afe00251SAndrea Bittau err = -EINVAL; 449afe00251SAndrea Bittau else 450afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L, 451afe00251SAndrea Bittau (struct dccp_so_feat *) 452afe00251SAndrea Bittau optval); 453afe00251SAndrea Bittau break; 454afe00251SAndrea Bittau 455afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 456afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 457afe00251SAndrea Bittau err = -EINVAL; 458afe00251SAndrea Bittau else 459afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 460afe00251SAndrea Bittau (struct dccp_so_feat *) 461afe00251SAndrea Bittau optval); 462afe00251SAndrea Bittau break; 463afe00251SAndrea Bittau 464a84ffe43SArnaldo Carvalho de Melo default: 465a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 466a84ffe43SArnaldo Carvalho de Melo break; 467a84ffe43SArnaldo Carvalho de Melo } 468a84ffe43SArnaldo Carvalho de Melo 469a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 470a84ffe43SArnaldo Carvalho de Melo return err; 4717c657876SArnaldo Carvalho de Melo } 4727c657876SArnaldo Carvalho de Melo 473f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 474f21e68caSArnaldo Carvalho de Melo 47567e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 47660fe62e7SAndrea Bittau __be32 __user *optval, 47767e6b629SArnaldo Carvalho de Melo int __user *optlen) 47867e6b629SArnaldo Carvalho de Melo { 47967e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 48067e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 48167e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 48267e6b629SArnaldo Carvalho de Melo 48367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 48467e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 48567e6b629SArnaldo Carvalho de Melo goto out; 48667e6b629SArnaldo Carvalho de Melo 48767e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 48867e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 48967e6b629SArnaldo Carvalho de Melo total_len += slen; 49067e6b629SArnaldo Carvalho de Melo } 49167e6b629SArnaldo Carvalho de Melo 49267e6b629SArnaldo Carvalho de Melo err = -EINVAL; 49367e6b629SArnaldo Carvalho de Melo if (total_len > len) 49467e6b629SArnaldo Carvalho de Melo goto out; 49567e6b629SArnaldo Carvalho de Melo 49667e6b629SArnaldo Carvalho de Melo err = 0; 49767e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 49867e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 49967e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 50067e6b629SArnaldo Carvalho de Melo err = -EFAULT; 50167e6b629SArnaldo Carvalho de Melo out: 50267e6b629SArnaldo Carvalho de Melo release_sock(sk); 50367e6b629SArnaldo Carvalho de Melo return err; 50467e6b629SArnaldo Carvalho de Melo } 50567e6b629SArnaldo Carvalho de Melo 5067c657876SArnaldo Carvalho de Melo int dccp_getsockopt(struct sock *sk, int level, int optname, 507a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5087c657876SArnaldo Carvalho de Melo { 509a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 510a84ffe43SArnaldo Carvalho de Melo int val, len; 5117c657876SArnaldo Carvalho de Melo 5127c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 51357cca05aSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 51457cca05aSArnaldo Carvalho de Melo optname, optval, 51557cca05aSArnaldo Carvalho de Melo optlen); 516a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 517a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 518a84ffe43SArnaldo Carvalho de Melo 51988f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 520a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 521a84ffe43SArnaldo Carvalho de Melo 522a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 523a84ffe43SArnaldo Carvalho de Melo 524a84ffe43SArnaldo Carvalho de Melo switch (optname) { 525a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 526a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 52788f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 528a84ffe43SArnaldo Carvalho de Melo break; 52988f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 53088f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 53160fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 53288f964dbSArnaldo Carvalho de Melo case 128 ... 191: 53388f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 53488f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 53588f964dbSArnaldo Carvalho de Melo case 192 ... 255: 53688f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 53788f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 538a84ffe43SArnaldo Carvalho de Melo default: 539a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 540a84ffe43SArnaldo Carvalho de Melo } 541a84ffe43SArnaldo Carvalho de Melo 542a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 543a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 544a84ffe43SArnaldo Carvalho de Melo 545a84ffe43SArnaldo Carvalho de Melo return 0; 5467c657876SArnaldo Carvalho de Melo } 5477c657876SArnaldo Carvalho de Melo 548f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 549f21e68caSArnaldo Carvalho de Melo 5507c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 5517c657876SArnaldo Carvalho de Melo size_t len) 5527c657876SArnaldo Carvalho de Melo { 5537c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 5547c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 5557c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 5567c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 5577c657876SArnaldo Carvalho de Melo int rc, size; 5587c657876SArnaldo Carvalho de Melo long timeo; 5597c657876SArnaldo Carvalho de Melo 5607c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 5617c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 5627c657876SArnaldo Carvalho de Melo 5637c657876SArnaldo Carvalho de Melo lock_sock(sk); 56427258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 5657c657876SArnaldo Carvalho de Melo 5667c657876SArnaldo Carvalho de Melo /* 5677c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 5687c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 5697c657876SArnaldo Carvalho de Melo */ 5707c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 5717c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 5727c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 57327258ee5SArnaldo Carvalho de Melo goto out_release; 5747c657876SArnaldo Carvalho de Melo 5757c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 5767c657876SArnaldo Carvalho de Melo release_sock(sk); 5777c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 5787c657876SArnaldo Carvalho de Melo lock_sock(sk); 5797c657876SArnaldo Carvalho de Melo if (skb == NULL) 5807c657876SArnaldo Carvalho de Melo goto out_release; 5817c657876SArnaldo Carvalho de Melo 5827c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 5837c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 58427258ee5SArnaldo Carvalho de Melo if (rc != 0) 58527258ee5SArnaldo Carvalho de Melo goto out_discard; 5867c657876SArnaldo Carvalho de Melo 587d6809c12SArnaldo Carvalho de Melo rc = dccp_write_xmit(sk, skb, &timeo); 58820472af9SArnaldo Carvalho de Melo /* 58920472af9SArnaldo Carvalho de Melo * XXX we don't use sk_write_queue, so just discard the packet. 59020472af9SArnaldo Carvalho de Melo * Current plan however is to _use_ sk_write_queue with 59120472af9SArnaldo Carvalho de Melo * an algorith similar to tcp_sendmsg, where the main difference 59220472af9SArnaldo Carvalho de Melo * is that in DCCP we have to respect packet boundaries, so 59320472af9SArnaldo Carvalho de Melo * no coalescing of skbs. 59420472af9SArnaldo Carvalho de Melo * 59520472af9SArnaldo Carvalho de Melo * This bug was _quickly_ found & fixed by just looking at an OSTRA 59620472af9SArnaldo Carvalho de Melo * generated callgraph 8) -acme 59720472af9SArnaldo Carvalho de Melo */ 5987c657876SArnaldo Carvalho de Melo out_release: 5997c657876SArnaldo Carvalho de Melo release_sock(sk); 6007c657876SArnaldo Carvalho de Melo return rc ? : len; 60127258ee5SArnaldo Carvalho de Melo out_discard: 60227258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 6037c657876SArnaldo Carvalho de Melo goto out_release; 6047c657876SArnaldo Carvalho de Melo } 6057c657876SArnaldo Carvalho de Melo 606f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 607f21e68caSArnaldo Carvalho de Melo 6087c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6097c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6107c657876SArnaldo Carvalho de Melo { 6117c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6127c657876SArnaldo Carvalho de Melo long timeo; 6137c657876SArnaldo Carvalho de Melo 6147c657876SArnaldo Carvalho de Melo lock_sock(sk); 6157c657876SArnaldo Carvalho de Melo 616531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 617531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6187c657876SArnaldo Carvalho de Melo goto out; 619531669a0SArnaldo Carvalho de Melo } 6207c657876SArnaldo Carvalho de Melo 6217c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 6227c657876SArnaldo Carvalho de Melo 6237c657876SArnaldo Carvalho de Melo do { 624531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 6257c657876SArnaldo Carvalho de Melo 626531669a0SArnaldo Carvalho de Melo if (skb == NULL) 627531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 6287c657876SArnaldo Carvalho de Melo 6297c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 6307c657876SArnaldo Carvalho de Melo 6317c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 6327c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 6337c657876SArnaldo Carvalho de Melo goto found_ok_skb; 6347c657876SArnaldo Carvalho de Melo 6357c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 6367c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 6377c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 638531669a0SArnaldo Carvalho de Melo len = 0; 6397c657876SArnaldo Carvalho de Melo goto found_fin_ok; 6407c657876SArnaldo Carvalho de Melo } 6417690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 6427690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 643531669a0SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 644531669a0SArnaldo Carvalho de Melo verify_sock_status: 645531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 646531669a0SArnaldo Carvalho de Melo len = 0; 6477c657876SArnaldo Carvalho de Melo break; 6487c657876SArnaldo Carvalho de Melo } 6497c657876SArnaldo Carvalho de Melo 650531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 651531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 6527c657876SArnaldo Carvalho de Melo break; 653531669a0SArnaldo Carvalho de Melo } 654531669a0SArnaldo Carvalho de Melo 655531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 656531669a0SArnaldo Carvalho de Melo len = 0; 657531669a0SArnaldo Carvalho de Melo break; 658531669a0SArnaldo Carvalho de Melo } 6597c657876SArnaldo Carvalho de Melo 6607c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 6617c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 6627c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 6637c657876SArnaldo Carvalho de Melo * from never connected socket. 6647c657876SArnaldo Carvalho de Melo */ 665531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6667c657876SArnaldo Carvalho de Melo break; 6677c657876SArnaldo Carvalho de Melo } 668531669a0SArnaldo Carvalho de Melo len = 0; 6697c657876SArnaldo Carvalho de Melo break; 6707c657876SArnaldo Carvalho de Melo } 6717c657876SArnaldo Carvalho de Melo 6727c657876SArnaldo Carvalho de Melo if (!timeo) { 673531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 6747c657876SArnaldo Carvalho de Melo break; 6757c657876SArnaldo Carvalho de Melo } 6767c657876SArnaldo Carvalho de Melo 6777c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 678531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 6797c657876SArnaldo Carvalho de Melo break; 6807c657876SArnaldo Carvalho de Melo } 6817c657876SArnaldo Carvalho de Melo 6827c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 6837c657876SArnaldo Carvalho de Melo continue; 6847c657876SArnaldo Carvalho de Melo found_ok_skb: 685531669a0SArnaldo Carvalho de Melo if (len > skb->len) 686531669a0SArnaldo Carvalho de Melo len = skb->len; 687531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 688531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 6897c657876SArnaldo Carvalho de Melo 690531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 6917c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 692531669a0SArnaldo Carvalho de Melo len = -EFAULT; 6937c657876SArnaldo Carvalho de Melo break; 6947c657876SArnaldo Carvalho de Melo } 6957c657876SArnaldo Carvalho de Melo found_fin_ok: 6967c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 6977c657876SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 6987c657876SArnaldo Carvalho de Melo break; 699531669a0SArnaldo Carvalho de Melo } while (1); 7007c657876SArnaldo Carvalho de Melo out: 7017c657876SArnaldo Carvalho de Melo release_sock(sk); 702531669a0SArnaldo Carvalho de Melo return len; 7037c657876SArnaldo Carvalho de Melo } 7047c657876SArnaldo Carvalho de Melo 705f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 706f21e68caSArnaldo Carvalho de Melo 707f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7087c657876SArnaldo Carvalho de Melo { 7097c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7107c657876SArnaldo Carvalho de Melo unsigned char old_state; 7117c657876SArnaldo Carvalho de Melo int err; 7127c657876SArnaldo Carvalho de Melo 7137c657876SArnaldo Carvalho de Melo lock_sock(sk); 7147c657876SArnaldo Carvalho de Melo 7157c657876SArnaldo Carvalho de Melo err = -EINVAL; 7167c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 7177c657876SArnaldo Carvalho de Melo goto out; 7187c657876SArnaldo Carvalho de Melo 7197c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 7207c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 7217c657876SArnaldo Carvalho de Melo goto out; 7227c657876SArnaldo Carvalho de Melo 7237c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 7247c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 7257c657876SArnaldo Carvalho de Melo */ 7267c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 7277c657876SArnaldo Carvalho de Melo /* 7287c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 7297c657876SArnaldo Carvalho de Melo * see tcp_listen_start 7307c657876SArnaldo Carvalho de Melo */ 7317c657876SArnaldo Carvalho de Melo err = dccp_listen_start(sk); 7327c657876SArnaldo Carvalho de Melo if (err) 7337c657876SArnaldo Carvalho de Melo goto out; 7347c657876SArnaldo Carvalho de Melo } 7357c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 7367c657876SArnaldo Carvalho de Melo err = 0; 7377c657876SArnaldo Carvalho de Melo 7387c657876SArnaldo Carvalho de Melo out: 7397c657876SArnaldo Carvalho de Melo release_sock(sk); 7407c657876SArnaldo Carvalho de Melo return err; 7417c657876SArnaldo Carvalho de Melo } 7427c657876SArnaldo Carvalho de Melo 743f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 744f21e68caSArnaldo Carvalho de Melo 7457c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 7467c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 7477c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 7487c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 7497c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 7507c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 7517c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 7527c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 7537c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 7547c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 7557c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 7567c657876SArnaldo Carvalho de Melo }; 7577c657876SArnaldo Carvalho de Melo 7587c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 7597c657876SArnaldo Carvalho de Melo { 7607c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 7617c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 7627c657876SArnaldo Carvalho de Melo 7637c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 7647c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 7657c657876SArnaldo Carvalho de Melo 7667c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 7677c657876SArnaldo Carvalho de Melo } 7687c657876SArnaldo Carvalho de Melo 7697c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 7707c657876SArnaldo Carvalho de Melo { 7717c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7727c657876SArnaldo Carvalho de Melo 7737c657876SArnaldo Carvalho de Melo lock_sock(sk); 7747c657876SArnaldo Carvalho de Melo 7757c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 7767c657876SArnaldo Carvalho de Melo 7777c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 7787c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 7797c657876SArnaldo Carvalho de Melo 7807c657876SArnaldo Carvalho de Melo /* Special case. */ 7817c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 7827c657876SArnaldo Carvalho de Melo 7837c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 7847c657876SArnaldo Carvalho de Melo } 7857c657876SArnaldo Carvalho de Melo 7867c657876SArnaldo Carvalho de Melo /* 7877c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 7887c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 7897c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 7907c657876SArnaldo Carvalho de Melo */ 7917c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 7927c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 7937c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 7947c657876SArnaldo Carvalho de Melo } 7957c657876SArnaldo Carvalho de Melo 7967c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 7977c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 7987c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 7997c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 8007ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 8017c657876SArnaldo Carvalho de Melo } 8027c657876SArnaldo Carvalho de Melo 8037c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 8047c657876SArnaldo Carvalho de Melo 8057c657876SArnaldo Carvalho de Melo adjudge_to_death: 8067ad07e7cSArnaldo Carvalho de Melo /* 8077ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 8087ad07e7cSArnaldo Carvalho de Melo */ 8097c657876SArnaldo Carvalho de Melo release_sock(sk); 8107c657876SArnaldo Carvalho de Melo /* 8117c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 8127c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 8137c657876SArnaldo Carvalho de Melo */ 8147c657876SArnaldo Carvalho de Melo local_bh_disable(); 8157c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 8167c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 8177c657876SArnaldo Carvalho de Melo 8187c657876SArnaldo Carvalho de Melo sock_hold(sk); 8197c657876SArnaldo Carvalho de Melo sock_orphan(sk); 8207c657876SArnaldo Carvalho de Melo 8217ad07e7cSArnaldo Carvalho de Melo /* 8227ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 8237ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 8247ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 8257ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 8267ad07e7cSArnaldo Carvalho de Melo */ 8277ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 8287ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 8297ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 8307ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 8317ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 8327ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 8337ad07e7cSArnaldo Carvalho de Melo #if 0 8347ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 8357c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8367ad07e7cSArnaldo Carvalho de Melo #endif 8377ad07e7cSArnaldo Carvalho de Melo } 8387c657876SArnaldo Carvalho de Melo 8397ad07e7cSArnaldo Carvalho de Melo atomic_inc(sk->sk_prot->orphan_count); 8407c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 8417c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 8427c657876SArnaldo Carvalho de Melo 8437c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 8447c657876SArnaldo Carvalho de Melo 8457c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 8467c657876SArnaldo Carvalho de Melo local_bh_enable(); 8477c657876SArnaldo Carvalho de Melo sock_put(sk); 8487c657876SArnaldo Carvalho de Melo } 8497c657876SArnaldo Carvalho de Melo 850f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 851f21e68caSArnaldo Carvalho de Melo 8527c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 8537c657876SArnaldo Carvalho de Melo { 8547c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 8557c657876SArnaldo Carvalho de Melo } 8567c657876SArnaldo Carvalho de Melo 857f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 858f21e68caSArnaldo Carvalho de Melo 85990ddc4f0SEric Dumazet static const struct proto_ops inet_dccp_ops = { 8607c657876SArnaldo Carvalho de Melo .family = PF_INET, 8617c657876SArnaldo Carvalho de Melo .owner = THIS_MODULE, 8627c657876SArnaldo Carvalho de Melo .release = inet_release, 8637c657876SArnaldo Carvalho de Melo .bind = inet_bind, 8647c657876SArnaldo Carvalho de Melo .connect = inet_stream_connect, 8657c657876SArnaldo Carvalho de Melo .socketpair = sock_no_socketpair, 8667c657876SArnaldo Carvalho de Melo .accept = inet_accept, 8677c657876SArnaldo Carvalho de Melo .getname = inet_getname, 868331968bdSArnaldo Carvalho de Melo /* FIXME: work on tcp_poll to rename it to inet_csk_poll */ 869331968bdSArnaldo Carvalho de Melo .poll = dccp_poll, 8707c657876SArnaldo Carvalho de Melo .ioctl = inet_ioctl, 8717690af3fSArnaldo Carvalho de Melo /* FIXME: work on inet_listen to rename it to sock_common_listen */ 8727690af3fSArnaldo Carvalho de Melo .listen = inet_dccp_listen, 8737c657876SArnaldo Carvalho de Melo .shutdown = inet_shutdown, 8747c657876SArnaldo Carvalho de Melo .setsockopt = sock_common_setsockopt, 8757c657876SArnaldo Carvalho de Melo .getsockopt = sock_common_getsockopt, 8767c657876SArnaldo Carvalho de Melo .sendmsg = inet_sendmsg, 8777c657876SArnaldo Carvalho de Melo .recvmsg = sock_common_recvmsg, 8787c657876SArnaldo Carvalho de Melo .mmap = sock_no_mmap, 8797c657876SArnaldo Carvalho de Melo .sendpage = sock_no_sendpage, 8807c657876SArnaldo Carvalho de Melo }; 8817c657876SArnaldo Carvalho de Melo 8827c657876SArnaldo Carvalho de Melo extern struct net_proto_family inet_family_ops; 8837c657876SArnaldo Carvalho de Melo 8847c657876SArnaldo Carvalho de Melo static struct inet_protosw dccp_v4_protosw = { 8857c657876SArnaldo Carvalho de Melo .type = SOCK_DCCP, 8867c657876SArnaldo Carvalho de Melo .protocol = IPPROTO_DCCP, 88734ca6860SArnaldo Carvalho de Melo .prot = &dccp_prot, 8887c657876SArnaldo Carvalho de Melo .ops = &inet_dccp_ops, 8897c657876SArnaldo Carvalho de Melo .capability = -1, 8907c657876SArnaldo Carvalho de Melo .no_check = 0, 891d83d8461SArnaldo Carvalho de Melo .flags = INET_PROTOSW_ICSK, 8927c657876SArnaldo Carvalho de Melo }; 8937c657876SArnaldo Carvalho de Melo 8947c657876SArnaldo Carvalho de Melo /* 8957c657876SArnaldo Carvalho de Melo * This is the global socket data structure used for responding to 8967c657876SArnaldo Carvalho de Melo * the Out-of-the-blue (OOTB) packets. A control sock will be created 8977c657876SArnaldo Carvalho de Melo * for this socket at the initialization time. 8987c657876SArnaldo Carvalho de Melo */ 8997c657876SArnaldo Carvalho de Melo struct socket *dccp_ctl_socket; 9007c657876SArnaldo Carvalho de Melo 9017c657876SArnaldo Carvalho de Melo static char dccp_ctl_socket_err_msg[] __initdata = 9027c657876SArnaldo Carvalho de Melo KERN_ERR "DCCP: Failed to create the control socket.\n"; 9037c657876SArnaldo Carvalho de Melo 9047c657876SArnaldo Carvalho de Melo static int __init dccp_ctl_sock_init(void) 9057c657876SArnaldo Carvalho de Melo { 9067c657876SArnaldo Carvalho de Melo int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP, 9077c657876SArnaldo Carvalho de Melo &dccp_ctl_socket); 9087c657876SArnaldo Carvalho de Melo if (rc < 0) 9097c657876SArnaldo Carvalho de Melo printk(dccp_ctl_socket_err_msg); 9107c657876SArnaldo Carvalho de Melo else { 9117c657876SArnaldo Carvalho de Melo dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; 9127c657876SArnaldo Carvalho de Melo inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1; 9137c657876SArnaldo Carvalho de Melo 9147c657876SArnaldo Carvalho de Melo /* Unhash it so that IP input processing does not even 9157c657876SArnaldo Carvalho de Melo * see it, we do not wish this socket to see incoming 9167c657876SArnaldo Carvalho de Melo * packets. 9177c657876SArnaldo Carvalho de Melo */ 9187c657876SArnaldo Carvalho de Melo dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk); 9197c657876SArnaldo Carvalho de Melo } 9207c657876SArnaldo Carvalho de Melo 9217c657876SArnaldo Carvalho de Melo return rc; 9227c657876SArnaldo Carvalho de Melo } 9237c657876SArnaldo Carvalho de Melo 924725ba8eeSArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_UNLOAD_HACK 925725ba8eeSArnaldo Carvalho de Melo void dccp_ctl_sock_exit(void) 9267c657876SArnaldo Carvalho de Melo { 9275480855bSArnaldo Carvalho de Melo if (dccp_ctl_socket != NULL) { 9287c657876SArnaldo Carvalho de Melo sock_release(dccp_ctl_socket); 9295480855bSArnaldo Carvalho de Melo dccp_ctl_socket = NULL; 9305480855bSArnaldo Carvalho de Melo } 9317c657876SArnaldo Carvalho de Melo } 9327c657876SArnaldo Carvalho de Melo 933725ba8eeSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit); 934725ba8eeSArnaldo Carvalho de Melo #endif 935725ba8eeSArnaldo Carvalho de Melo 93646f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 9377c657876SArnaldo Carvalho de Melo { 9387c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 9397c657876SArnaldo Carvalho de Melo 9407c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 9417c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 9427c657876SArnaldo Carvalho de Melo goto out; 9437c657876SArnaldo Carvalho de Melo 9447c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 9457c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 9467c657876SArnaldo Carvalho de Melo goto out_free_one; 9477c657876SArnaldo Carvalho de Melo 9487c657876SArnaldo Carvalho de Melo rc = 0; 9497c657876SArnaldo Carvalho de Melo out: 9507c657876SArnaldo Carvalho de Melo return rc; 9517c657876SArnaldo Carvalho de Melo out_free_one: 9527c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 9537c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 9547c657876SArnaldo Carvalho de Melo goto out; 9557c657876SArnaldo Carvalho de Melo 9567c657876SArnaldo Carvalho de Melo } 9577c657876SArnaldo Carvalho de Melo 95846f09ffaSArnaldo Carvalho de Melo static int dccp_mib_exit(void) 95946f09ffaSArnaldo Carvalho de Melo { 96046f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 96146f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 96246f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 96346f09ffaSArnaldo Carvalho de Melo } 96446f09ffaSArnaldo Carvalho de Melo 9657c657876SArnaldo Carvalho de Melo static int thash_entries; 9667c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 9677c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 9687c657876SArnaldo Carvalho de Melo 969a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 9707c657876SArnaldo Carvalho de Melo int dccp_debug; 9717c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 9727c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 973f21e68caSArnaldo Carvalho de Melo 974f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 975a1d3a355SArnaldo Carvalho de Melo #endif 9767c657876SArnaldo Carvalho de Melo 9777c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 9787c657876SArnaldo Carvalho de Melo { 9797c657876SArnaldo Carvalho de Melo unsigned long goal; 9807c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 98134ca6860SArnaldo Carvalho de Melo int rc = proto_register(&dccp_prot, 1); 9827c657876SArnaldo Carvalho de Melo 9837c657876SArnaldo Carvalho de Melo if (rc) 9847c657876SArnaldo Carvalho de Melo goto out; 9857c657876SArnaldo Carvalho de Melo 986fa23e2ecSArnaldo Carvalho de Melo rc = -ENOBUFS; 9877690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 9887690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 9897690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 9907690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 9917c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 9927c657876SArnaldo Carvalho de Melo goto out_proto_unregister; 9937c657876SArnaldo Carvalho de Melo 9947c657876SArnaldo Carvalho de Melo /* 9957c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 9967c657876SArnaldo Carvalho de Melo * hash tables. 9977c657876SArnaldo Carvalho de Melo * 9987c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 9997c657876SArnaldo Carvalho de Melo */ 10007c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 10017c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 10027c657876SArnaldo Carvalho de Melo else 10037c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 10047c657876SArnaldo Carvalho de Melo 10057c657876SArnaldo Carvalho de Melo if (thash_entries) 10067690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 10077690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 10087c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 10097c657876SArnaldo Carvalho de Melo ; 10107c657876SArnaldo Carvalho de Melo do { 10117c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 10127c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 10137c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 10147690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 10157690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 10167c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 10177c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 10187c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 10197c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 10207c657876SArnaldo Carvalho de Melo 10217c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 10227c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 10237c657876SArnaldo Carvalho de Melo "established hash table\n"); 10247c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 10257c657876SArnaldo Carvalho de Melo } 10267c657876SArnaldo Carvalho de Melo 10277c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 10287c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 10297c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 10307c657876SArnaldo Carvalho de Melo } 10317c657876SArnaldo Carvalho de Melo 10327c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 10337c657876SArnaldo Carvalho de Melo 10347c657876SArnaldo Carvalho de Melo do { 10357c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 10367c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 10377690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 10387690af3fSArnaldo Carvalho de Melo bhash_order > 0) 10397c657876SArnaldo Carvalho de Melo continue; 10407c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 10417c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 10427c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 10437c657876SArnaldo Carvalho de Melo 10447c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 10457c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 10467c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 10477c657876SArnaldo Carvalho de Melo } 10487c657876SArnaldo Carvalho de Melo 10497c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 10507c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 10517c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 10527c657876SArnaldo Carvalho de Melo } 10537c657876SArnaldo Carvalho de Melo 105446f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 1055fa23e2ecSArnaldo Carvalho de Melo if (rc) 10567c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 10577c657876SArnaldo Carvalho de Melo 10587c657876SArnaldo Carvalho de Melo rc = -EAGAIN; 10597c657876SArnaldo Carvalho de Melo if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP)) 10607c657876SArnaldo Carvalho de Melo goto out_free_dccp_v4_mibs; 10617c657876SArnaldo Carvalho de Melo 10627c657876SArnaldo Carvalho de Melo inet_register_protosw(&dccp_v4_protosw); 10637c657876SArnaldo Carvalho de Melo 10649b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 10657c657876SArnaldo Carvalho de Melo if (rc) 10667c657876SArnaldo Carvalho de Melo goto out_unregister_protosw; 10679b07ef5dSArnaldo Carvalho de Melo 1068e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 10699b07ef5dSArnaldo Carvalho de Melo if (rc) 10709b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 1071e55d912fSArnaldo Carvalho de Melo 1072e55d912fSArnaldo Carvalho de Melo rc = dccp_ctl_sock_init(); 1073e55d912fSArnaldo Carvalho de Melo if (rc) 1074e55d912fSArnaldo Carvalho de Melo goto out_sysctl_exit; 10757c657876SArnaldo Carvalho de Melo out: 10767c657876SArnaldo Carvalho de Melo return rc; 1077e55d912fSArnaldo Carvalho de Melo out_sysctl_exit: 1078e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 10799b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 10809b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 10817c657876SArnaldo Carvalho de Melo out_unregister_protosw: 10827c657876SArnaldo Carvalho de Melo inet_unregister_protosw(&dccp_v4_protosw); 10837c657876SArnaldo Carvalho de Melo inet_del_protocol(&dccp_protocol, IPPROTO_DCCP); 10847c657876SArnaldo Carvalho de Melo out_free_dccp_v4_mibs: 108546f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 10867c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 10877c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 10887c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 10897c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 10907c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 10917c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 10927c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 10937c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10947c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 10957c657876SArnaldo Carvalho de Melo out_proto_unregister: 109634ca6860SArnaldo Carvalho de Melo proto_unregister(&dccp_prot); 10977c657876SArnaldo Carvalho de Melo goto out; 10987c657876SArnaldo Carvalho de Melo } 10997c657876SArnaldo Carvalho de Melo 11007c657876SArnaldo Carvalho de Melo static const char dccp_del_proto_err_msg[] __exitdata = 11017c657876SArnaldo Carvalho de Melo KERN_ERR "can't remove dccp net_protocol\n"; 11027c657876SArnaldo Carvalho de Melo 11037c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 11047c657876SArnaldo Carvalho de Melo { 11057c657876SArnaldo Carvalho de Melo inet_unregister_protosw(&dccp_v4_protosw); 11067c657876SArnaldo Carvalho de Melo 11077c657876SArnaldo Carvalho de Melo if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0) 11087c657876SArnaldo Carvalho de Melo printk(dccp_del_proto_err_msg); 11097c657876SArnaldo Carvalho de Melo 111046f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 1111725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 1112725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 1113725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1114725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1115725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1116725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 11177c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 111834ca6860SArnaldo Carvalho de Melo proto_unregister(&dccp_prot); 11199b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1120e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 11217c657876SArnaldo Carvalho de Melo } 11227c657876SArnaldo Carvalho de Melo 11237c657876SArnaldo Carvalho de Melo module_init(dccp_init); 11247c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 11257c657876SArnaldo Carvalho de Melo 1126bb97d31fSArnaldo Carvalho de Melo /* 1127bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33) 1128bb97d31fSArnaldo Carvalho de Melo * values directly, Also cover the case where the protocol is not specified, 1129bb97d31fSArnaldo Carvalho de Melo * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP 1130bb97d31fSArnaldo Carvalho de Melo */ 1131bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6"); 1132bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6"); 11337c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 11347c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 11357c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1136