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 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 307c657876SArnaldo Carvalho de Melo #include <asm/semaphore.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 447c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 457c657876SArnaldo Carvalho de Melo 46f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_orphan_count); 47f21e68caSArnaldo Carvalho de Melo 48075ae866SArnaldo Carvalho de Melo struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { 49075ae866SArnaldo Carvalho de Melo .lhash_lock = RW_LOCK_UNLOCKED, 50075ae866SArnaldo Carvalho de Melo .lhash_users = ATOMIC_INIT(0), 51075ae866SArnaldo Carvalho de Melo .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), 52075ae866SArnaldo Carvalho de Melo }; 53075ae866SArnaldo Carvalho de Melo 54075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo); 55075ae866SArnaldo Carvalho de Melo 567c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 577c657876SArnaldo Carvalho de Melo { 587c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 597c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 607c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 617c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 627c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 637c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 647c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 657c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 667c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 677c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 687c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 697c657876SArnaldo Carvalho de Melo }; 707c657876SArnaldo Carvalho de Melo 717c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 727c657876SArnaldo Carvalho de Melo return "INVALID"; 737c657876SArnaldo Carvalho de Melo else 747c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 757c657876SArnaldo Carvalho de Melo } 767c657876SArnaldo Carvalho de Melo 777c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 787c657876SArnaldo Carvalho de Melo 797c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 807c657876SArnaldo Carvalho de Melo { 817c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 827c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 837c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 847c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 857c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 867c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 877c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 887c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 897c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 907c657876SArnaldo Carvalho de Melo }; 917c657876SArnaldo Carvalho de Melo 927c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 937c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 947c657876SArnaldo Carvalho de Melo else 957c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 967c657876SArnaldo Carvalho de Melo } 977c657876SArnaldo Carvalho de Melo 987c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 997c657876SArnaldo Carvalho de Melo 100c985ed70SArnaldo Carvalho de Melo void dccp_hash(struct sock *sk) 101c985ed70SArnaldo Carvalho de Melo { 102c985ed70SArnaldo Carvalho de Melo inet_hash(&dccp_hashinfo, sk); 103c985ed70SArnaldo Carvalho de Melo } 104c985ed70SArnaldo Carvalho de Melo 105c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hash); 106c985ed70SArnaldo Carvalho de Melo 107c985ed70SArnaldo Carvalho de Melo void dccp_unhash(struct sock *sk) 108c985ed70SArnaldo Carvalho de Melo { 109c985ed70SArnaldo Carvalho de Melo inet_unhash(&dccp_hashinfo, sk); 110c985ed70SArnaldo Carvalho de Melo } 111c985ed70SArnaldo Carvalho de Melo 112c985ed70SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_unhash); 113c985ed70SArnaldo Carvalho de Melo 1143e0fadc5SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk) 1153e0fadc5SArnaldo Carvalho de Melo { 1163e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1173e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1183e0fadc5SArnaldo Carvalho de Melo static int dccp_ctl_socket_init = 1; 1193e0fadc5SArnaldo Carvalho de Melo 1203e0fadc5SArnaldo Carvalho de Melo dccp_options_init(&dp->dccps_options); 1213e0fadc5SArnaldo Carvalho de Melo do_gettimeofday(&dp->dccps_epoch); 1223e0fadc5SArnaldo Carvalho de Melo 1233e0fadc5SArnaldo Carvalho de Melo /* 1243e0fadc5SArnaldo Carvalho de Melo * FIXME: We're hardcoding the CCID, and doing this at this point makes 1253e0fadc5SArnaldo Carvalho de Melo * the listening (master) sock get CCID control blocks, which is not 1263e0fadc5SArnaldo Carvalho de Melo * necessary, but for now, to not mess with the test userspace apps, 1273e0fadc5SArnaldo Carvalho de Melo * lets leave it here, later the real solution is to do this in a 1283e0fadc5SArnaldo Carvalho de Melo * setsockopt(CCIDs-I-want/accept). -acme 1293e0fadc5SArnaldo Carvalho de Melo */ 1303e0fadc5SArnaldo Carvalho de Melo if (likely(!dccp_ctl_socket_init)) { 1313e0fadc5SArnaldo Carvalho de Melo int rc = dccp_feat_init(sk); 1323e0fadc5SArnaldo Carvalho de Melo 1333e0fadc5SArnaldo Carvalho de Melo if (rc) 1343e0fadc5SArnaldo Carvalho de Melo return rc; 1353e0fadc5SArnaldo Carvalho de Melo 1363e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 1373e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL); 1383e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec == NULL) 1393e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1403e0fadc5SArnaldo Carvalho de Melo } 1413e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = 1423e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_new(dp->dccps_options.dccpo_rx_ccid, 1433e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1443e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid = 1453e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_new(dp->dccps_options.dccpo_tx_ccid, 1463e0fadc5SArnaldo Carvalho de Melo sk, GFP_KERNEL); 1473e0fadc5SArnaldo Carvalho de Melo if (unlikely(dp->dccps_hc_rx_ccid == NULL || 1483e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_tx_ccid == NULL)) { 1493e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 1503e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 1513e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 1523e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 1533e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 1543e0fadc5SArnaldo Carvalho de Melo } 1553e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 1563e0fadc5SArnaldo Carvalho de Melo return -ENOMEM; 1573e0fadc5SArnaldo Carvalho de Melo } 1583e0fadc5SArnaldo Carvalho de Melo } else { 1593e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */ 1603e0fadc5SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending); 1613e0fadc5SArnaldo Carvalho de Melo INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf); 1623e0fadc5SArnaldo Carvalho de Melo dccp_ctl_socket_init = 0; 1633e0fadc5SArnaldo Carvalho de Melo } 1643e0fadc5SArnaldo Carvalho de Melo 1653e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk); 1663e0fadc5SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT; 1673e0fadc5SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED; 1683e0fadc5SArnaldo Carvalho de Melo sk->sk_write_space = dccp_write_space; 1693e0fadc5SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss; 1703e0fadc5SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536; 1713e0fadc5SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED; 1723e0fadc5SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_INVALID_VALUE; 1733e0fadc5SArnaldo Carvalho de Melo dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1; 1743e0fadc5SArnaldo Carvalho de Melo 1753e0fadc5SArnaldo Carvalho de Melo return 0; 1763e0fadc5SArnaldo Carvalho de Melo } 1773e0fadc5SArnaldo Carvalho de Melo 1783e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock); 1793e0fadc5SArnaldo Carvalho de Melo 1803e0fadc5SArnaldo Carvalho de Melo int dccp_destroy_sock(struct sock *sk) 1813e0fadc5SArnaldo Carvalho de Melo { 1823e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 1833e0fadc5SArnaldo Carvalho de Melo 1843e0fadc5SArnaldo Carvalho de Melo /* 1853e0fadc5SArnaldo Carvalho de Melo * DCCP doesn't use sk_write_queue, just sk_send_head 1863e0fadc5SArnaldo Carvalho de Melo * for retransmissions 1873e0fadc5SArnaldo Carvalho de Melo */ 1883e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 1893e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head); 1903e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 1913e0fadc5SArnaldo Carvalho de Melo } 1923e0fadc5SArnaldo Carvalho de Melo 1933e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */ 1943e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL) 1953e0fadc5SArnaldo Carvalho de Melo inet_put_port(&dccp_hashinfo, sk); 1963e0fadc5SArnaldo Carvalho de Melo 1973e0fadc5SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 1983e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL; 1993e0fadc5SArnaldo Carvalho de Melo 2003e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_options.dccpo_send_ack_vector) { 2013e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 2023e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ackvec = NULL; 2033e0fadc5SArnaldo Carvalho de Melo } 2043e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 2053e0fadc5SArnaldo Carvalho de Melo ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 2063e0fadc5SArnaldo Carvalho de Melo dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 2073e0fadc5SArnaldo Carvalho de Melo 2083e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */ 2093e0fadc5SArnaldo Carvalho de Melo dccp_feat_clean(sk); 2103e0fadc5SArnaldo Carvalho de Melo 2113e0fadc5SArnaldo Carvalho de Melo return 0; 2123e0fadc5SArnaldo Carvalho de Melo } 2133e0fadc5SArnaldo Carvalho de Melo 2143e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_destroy_sock); 2153e0fadc5SArnaldo Carvalho de Melo 2167c657876SArnaldo Carvalho de Melo static inline int dccp_listen_start(struct sock *sk) 2177c657876SArnaldo Carvalho de Melo { 21867e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 21967e6b629SArnaldo Carvalho de Melo 22067e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 22167e6b629SArnaldo Carvalho de Melo /* 22267e6b629SArnaldo Carvalho de Melo * Apps need to use setsockopt(DCCP_SOCKOPT_SERVICE) 22367e6b629SArnaldo Carvalho de Melo * before calling listen() 22467e6b629SArnaldo Carvalho de Melo */ 22567e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 22667e6b629SArnaldo Carvalho de Melo return -EPROTO; 2277c657876SArnaldo Carvalho de Melo return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE); 2287c657876SArnaldo Carvalho de Melo } 2297c657876SArnaldo Carvalho de Melo 2307c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 2317c657876SArnaldo Carvalho de Melo { 2327c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 2337c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 2347c657876SArnaldo Carvalho de Melo int err = 0; 2357c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 2367c657876SArnaldo Carvalho de Melo 2377c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 2387c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 2397c657876SArnaldo Carvalho de Melo 2407c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 2417c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 2427c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 2437c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 2447c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 2457c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 2467c657876SArnaldo Carvalho de Melo 2477c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 2487c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 2497c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 2507c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 2517c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 2527c657876SArnaldo Carvalho de Melo } 2537c657876SArnaldo Carvalho de Melo 2547c657876SArnaldo Carvalho de Melo inet->dport = 0; 2557c657876SArnaldo Carvalho de Melo 2567c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 2577c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 2587c657876SArnaldo Carvalho de Melo 2597c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 2607c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 2617c657876SArnaldo Carvalho de Melo 2627c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 2637c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 2647c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 2657c657876SArnaldo Carvalho de Melo 2667c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 2677c657876SArnaldo Carvalho de Melo 2687c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 2697c657876SArnaldo Carvalho de Melo return err; 2707c657876SArnaldo Carvalho de Melo } 2717c657876SArnaldo Carvalho de Melo 272f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_disconnect); 273f21e68caSArnaldo Carvalho de Melo 274331968bdSArnaldo Carvalho de Melo /* 275331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 276331968bdSArnaldo Carvalho de Melo * 277331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 278331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 279331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 280331968bdSArnaldo Carvalho de Melo */ 281f21e68caSArnaldo Carvalho de Melo unsigned int dccp_poll(struct file *file, struct socket *sock, 282331968bdSArnaldo Carvalho de Melo poll_table *wait) 283331968bdSArnaldo Carvalho de Melo { 284331968bdSArnaldo Carvalho de Melo unsigned int mask; 285331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 286331968bdSArnaldo Carvalho de Melo 287331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 288331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 289331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 290331968bdSArnaldo Carvalho de Melo 291331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 292331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 293331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 294331968bdSArnaldo Carvalho de Melo */ 295331968bdSArnaldo Carvalho de Melo 296331968bdSArnaldo Carvalho de Melo mask = 0; 297331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 298331968bdSArnaldo Carvalho de Melo mask = POLLERR; 299331968bdSArnaldo Carvalho de Melo 300331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 301331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 302331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 303331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 304331968bdSArnaldo Carvalho de Melo 305331968bdSArnaldo Carvalho de Melo /* Connected? */ 306331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 307331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 308331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 309331968bdSArnaldo Carvalho de Melo 310331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 311331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 312331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 313331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 314331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 315331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 316331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 317331968bdSArnaldo Carvalho de Melo 318331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 319331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 320331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 321331968bdSArnaldo Carvalho de Melo */ 322331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 323331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 324331968bdSArnaldo Carvalho de Melo } 325331968bdSArnaldo Carvalho de Melo } 326331968bdSArnaldo Carvalho de Melo } 327331968bdSArnaldo Carvalho de Melo return mask; 328331968bdSArnaldo Carvalho de Melo } 329331968bdSArnaldo Carvalho de Melo 330f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll); 331f21e68caSArnaldo Carvalho de Melo 3327c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3337c657876SArnaldo Carvalho de Melo { 3347c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 3357c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 3367c657876SArnaldo Carvalho de Melo } 3377c657876SArnaldo Carvalho de Melo 338f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl); 339f21e68caSArnaldo Carvalho de Melo 34060fe62e7SAndrea Bittau static int dccp_setsockopt_service(struct sock *sk, const __be32 service, 34167e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 34267e6b629SArnaldo Carvalho de Melo { 34367e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 34467e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 34567e6b629SArnaldo Carvalho de Melo 34667e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 34767e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 34867e6b629SArnaldo Carvalho de Melo return -EINVAL; 34967e6b629SArnaldo Carvalho de Melo 35067e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 35167e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 35267e6b629SArnaldo Carvalho de Melo if (sl == NULL) 35367e6b629SArnaldo Carvalho de Melo return -ENOMEM; 35467e6b629SArnaldo Carvalho de Melo 35567e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 35667e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 35767e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 35867e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 35967e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 36067e6b629SArnaldo Carvalho de Melo kfree(sl); 36167e6b629SArnaldo Carvalho de Melo return -EFAULT; 36267e6b629SArnaldo Carvalho de Melo } 36367e6b629SArnaldo Carvalho de Melo } 36467e6b629SArnaldo Carvalho de Melo 36567e6b629SArnaldo Carvalho de Melo lock_sock(sk); 36667e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 36767e6b629SArnaldo Carvalho de Melo 36867e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 36967e6b629SArnaldo Carvalho de Melo 37067e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 37167e6b629SArnaldo Carvalho de Melo release_sock(sk); 37267e6b629SArnaldo Carvalho de Melo return 0; 37367e6b629SArnaldo Carvalho de Melo } 37467e6b629SArnaldo Carvalho de Melo 375afe00251SAndrea Bittau /* byte 1 is feature. the rest is the preference list */ 376afe00251SAndrea Bittau static int dccp_setsockopt_change(struct sock *sk, int type, 377afe00251SAndrea Bittau struct dccp_so_feat __user *optval) 378afe00251SAndrea Bittau { 379afe00251SAndrea Bittau struct dccp_so_feat opt; 380afe00251SAndrea Bittau u8 *val; 381afe00251SAndrea Bittau int rc; 382afe00251SAndrea Bittau 383afe00251SAndrea Bittau if (copy_from_user(&opt, optval, sizeof(opt))) 384afe00251SAndrea Bittau return -EFAULT; 385afe00251SAndrea Bittau 386afe00251SAndrea Bittau val = kmalloc(opt.dccpsf_len, GFP_KERNEL); 387afe00251SAndrea Bittau if (!val) 388afe00251SAndrea Bittau return -ENOMEM; 389afe00251SAndrea Bittau 390afe00251SAndrea Bittau if (copy_from_user(val, opt.dccpsf_val, opt.dccpsf_len)) { 391afe00251SAndrea Bittau rc = -EFAULT; 392afe00251SAndrea Bittau goto out_free_val; 393afe00251SAndrea Bittau } 394afe00251SAndrea Bittau 395afe00251SAndrea Bittau rc = dccp_feat_change(sk, type, opt.dccpsf_feat, val, opt.dccpsf_len, 396afe00251SAndrea Bittau GFP_KERNEL); 397afe00251SAndrea Bittau if (rc) 398afe00251SAndrea Bittau goto out_free_val; 399afe00251SAndrea Bittau 400afe00251SAndrea Bittau out: 401afe00251SAndrea Bittau return rc; 402afe00251SAndrea Bittau 403afe00251SAndrea Bittau out_free_val: 404afe00251SAndrea Bittau kfree(val); 405afe00251SAndrea Bittau goto out; 406afe00251SAndrea Bittau } 407afe00251SAndrea Bittau 4087c657876SArnaldo Carvalho de Melo int dccp_setsockopt(struct sock *sk, int level, int optname, 409a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 4107c657876SArnaldo Carvalho de Melo { 411a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 412a84ffe43SArnaldo Carvalho de Melo int err; 413a84ffe43SArnaldo Carvalho de Melo int val; 4147c657876SArnaldo Carvalho de Melo 4157c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 41657cca05aSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level, 41757cca05aSArnaldo Carvalho de Melo optname, optval, 41857cca05aSArnaldo Carvalho de Melo optlen); 4197c657876SArnaldo Carvalho de Melo 420a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 421a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 422a84ffe43SArnaldo Carvalho de Melo 423a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 424a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 425a84ffe43SArnaldo Carvalho de Melo 42667e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 42767e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 428a84ffe43SArnaldo Carvalho de Melo 42967e6b629SArnaldo Carvalho de Melo lock_sock(sk); 430a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 431a84ffe43SArnaldo Carvalho de Melo err = 0; 432a84ffe43SArnaldo Carvalho de Melo 433a84ffe43SArnaldo Carvalho de Melo switch (optname) { 434a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 435a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 436a84ffe43SArnaldo Carvalho de Melo break; 437afe00251SAndrea Bittau 438afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_L: 439afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 440afe00251SAndrea Bittau err = -EINVAL; 441afe00251SAndrea Bittau else 442afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_L, 443afe00251SAndrea Bittau (struct dccp_so_feat *) 444afe00251SAndrea Bittau optval); 445afe00251SAndrea Bittau break; 446afe00251SAndrea Bittau 447afe00251SAndrea Bittau case DCCP_SOCKOPT_CHANGE_R: 448afe00251SAndrea Bittau if (optlen != sizeof(struct dccp_so_feat)) 449afe00251SAndrea Bittau err = -EINVAL; 450afe00251SAndrea Bittau else 451afe00251SAndrea Bittau err = dccp_setsockopt_change(sk, DCCPO_CHANGE_R, 452afe00251SAndrea Bittau (struct dccp_so_feat *) 453afe00251SAndrea Bittau optval); 454afe00251SAndrea Bittau break; 455afe00251SAndrea Bittau 456a84ffe43SArnaldo Carvalho de Melo default: 457a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 458a84ffe43SArnaldo Carvalho de Melo break; 459a84ffe43SArnaldo Carvalho de Melo } 460a84ffe43SArnaldo Carvalho de Melo 461a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 462a84ffe43SArnaldo Carvalho de Melo return err; 4637c657876SArnaldo Carvalho de Melo } 4647c657876SArnaldo Carvalho de Melo 465f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt); 466f21e68caSArnaldo Carvalho de Melo 46767e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 46860fe62e7SAndrea Bittau __be32 __user *optval, 46967e6b629SArnaldo Carvalho de Melo int __user *optlen) 47067e6b629SArnaldo Carvalho de Melo { 47167e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 47267e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 47367e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 47467e6b629SArnaldo Carvalho de Melo 47567e6b629SArnaldo Carvalho de Melo lock_sock(sk); 47667e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 47767e6b629SArnaldo Carvalho de Melo goto out; 47867e6b629SArnaldo Carvalho de Melo 47967e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 48067e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 48167e6b629SArnaldo Carvalho de Melo total_len += slen; 48267e6b629SArnaldo Carvalho de Melo } 48367e6b629SArnaldo Carvalho de Melo 48467e6b629SArnaldo Carvalho de Melo err = -EINVAL; 48567e6b629SArnaldo Carvalho de Melo if (total_len > len) 48667e6b629SArnaldo Carvalho de Melo goto out; 48767e6b629SArnaldo Carvalho de Melo 48867e6b629SArnaldo Carvalho de Melo err = 0; 48967e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 49067e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 49167e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 49267e6b629SArnaldo Carvalho de Melo err = -EFAULT; 49367e6b629SArnaldo Carvalho de Melo out: 49467e6b629SArnaldo Carvalho de Melo release_sock(sk); 49567e6b629SArnaldo Carvalho de Melo return err; 49667e6b629SArnaldo Carvalho de Melo } 49767e6b629SArnaldo Carvalho de Melo 4987c657876SArnaldo Carvalho de Melo int dccp_getsockopt(struct sock *sk, int level, int optname, 499a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 5007c657876SArnaldo Carvalho de Melo { 501a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 502a84ffe43SArnaldo Carvalho de Melo int val, len; 5037c657876SArnaldo Carvalho de Melo 5047c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 50557cca05aSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level, 50657cca05aSArnaldo Carvalho de Melo optname, optval, 50757cca05aSArnaldo Carvalho de Melo optlen); 508a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 509a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 510a84ffe43SArnaldo Carvalho de Melo 51188f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 512a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 513a84ffe43SArnaldo Carvalho de Melo 514a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 515a84ffe43SArnaldo Carvalho de Melo 516a84ffe43SArnaldo Carvalho de Melo switch (optname) { 517a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 518a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 51988f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 520a84ffe43SArnaldo Carvalho de Melo break; 52188f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 52288f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 52360fe62e7SAndrea Bittau (__be32 __user *)optval, optlen); 52488f964dbSArnaldo Carvalho de Melo case 128 ... 191: 52588f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 52688f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 52788f964dbSArnaldo Carvalho de Melo case 192 ... 255: 52888f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 52988f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 530a84ffe43SArnaldo Carvalho de Melo default: 531a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 532a84ffe43SArnaldo Carvalho de Melo } 533a84ffe43SArnaldo Carvalho de Melo 534a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 535a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 536a84ffe43SArnaldo Carvalho de Melo 537a84ffe43SArnaldo Carvalho de Melo return 0; 5387c657876SArnaldo Carvalho de Melo } 5397c657876SArnaldo Carvalho de Melo 540f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_getsockopt); 541f21e68caSArnaldo Carvalho de Melo 5427c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 5437c657876SArnaldo Carvalho de Melo size_t len) 5447c657876SArnaldo Carvalho de Melo { 5457c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 5467c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 5477c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 5487c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 5497c657876SArnaldo Carvalho de Melo int rc, size; 5507c657876SArnaldo Carvalho de Melo long timeo; 5517c657876SArnaldo Carvalho de Melo 5527c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 5537c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 5547c657876SArnaldo Carvalho de Melo 5557c657876SArnaldo Carvalho de Melo lock_sock(sk); 55627258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 5577c657876SArnaldo Carvalho de Melo 5587c657876SArnaldo Carvalho de Melo /* 5597c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 5607c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 5617c657876SArnaldo Carvalho de Melo */ 5627c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 5637c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 5647c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 56527258ee5SArnaldo Carvalho de Melo goto out_release; 5667c657876SArnaldo Carvalho de Melo 5677c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 5687c657876SArnaldo Carvalho de Melo release_sock(sk); 5697c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 5707c657876SArnaldo Carvalho de Melo lock_sock(sk); 5717c657876SArnaldo Carvalho de Melo if (skb == NULL) 5727c657876SArnaldo Carvalho de Melo goto out_release; 5737c657876SArnaldo Carvalho de Melo 5747c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 5757c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 57627258ee5SArnaldo Carvalho de Melo if (rc != 0) 57727258ee5SArnaldo Carvalho de Melo goto out_discard; 5787c657876SArnaldo Carvalho de Melo 579d6809c12SArnaldo Carvalho de Melo rc = dccp_write_xmit(sk, skb, &timeo); 58020472af9SArnaldo Carvalho de Melo /* 58120472af9SArnaldo Carvalho de Melo * XXX we don't use sk_write_queue, so just discard the packet. 58220472af9SArnaldo Carvalho de Melo * Current plan however is to _use_ sk_write_queue with 58320472af9SArnaldo Carvalho de Melo * an algorith similar to tcp_sendmsg, where the main difference 58420472af9SArnaldo Carvalho de Melo * is that in DCCP we have to respect packet boundaries, so 58520472af9SArnaldo Carvalho de Melo * no coalescing of skbs. 58620472af9SArnaldo Carvalho de Melo * 58720472af9SArnaldo Carvalho de Melo * This bug was _quickly_ found & fixed by just looking at an OSTRA 58820472af9SArnaldo Carvalho de Melo * generated callgraph 8) -acme 58920472af9SArnaldo Carvalho de Melo */ 5907c657876SArnaldo Carvalho de Melo out_release: 5917c657876SArnaldo Carvalho de Melo release_sock(sk); 5927c657876SArnaldo Carvalho de Melo return rc ? : len; 59327258ee5SArnaldo Carvalho de Melo out_discard: 59427258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 5957c657876SArnaldo Carvalho de Melo goto out_release; 5967c657876SArnaldo Carvalho de Melo } 5977c657876SArnaldo Carvalho de Melo 598f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg); 599f21e68caSArnaldo Carvalho de Melo 6007c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6017c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 6027c657876SArnaldo Carvalho de Melo { 6037c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 6047c657876SArnaldo Carvalho de Melo long timeo; 6057c657876SArnaldo Carvalho de Melo 6067c657876SArnaldo Carvalho de Melo lock_sock(sk); 6077c657876SArnaldo Carvalho de Melo 608531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 609531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6107c657876SArnaldo Carvalho de Melo goto out; 611531669a0SArnaldo Carvalho de Melo } 6127c657876SArnaldo Carvalho de Melo 6137c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 6147c657876SArnaldo Carvalho de Melo 6157c657876SArnaldo Carvalho de Melo do { 616531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 6177c657876SArnaldo Carvalho de Melo 618531669a0SArnaldo Carvalho de Melo if (skb == NULL) 619531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 6207c657876SArnaldo Carvalho de Melo 6217c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 6227c657876SArnaldo Carvalho de Melo 6237c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 6247c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 6257c657876SArnaldo Carvalho de Melo goto found_ok_skb; 6267c657876SArnaldo Carvalho de Melo 6277c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 6287c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 6297c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 630531669a0SArnaldo Carvalho de Melo len = 0; 6317c657876SArnaldo Carvalho de Melo goto found_fin_ok; 6327c657876SArnaldo Carvalho de Melo } 6337690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 6347690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 635531669a0SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 636531669a0SArnaldo Carvalho de Melo verify_sock_status: 637531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 638531669a0SArnaldo Carvalho de Melo len = 0; 6397c657876SArnaldo Carvalho de Melo break; 6407c657876SArnaldo Carvalho de Melo } 6417c657876SArnaldo Carvalho de Melo 642531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 643531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 6447c657876SArnaldo Carvalho de Melo break; 645531669a0SArnaldo Carvalho de Melo } 646531669a0SArnaldo Carvalho de Melo 647531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 648531669a0SArnaldo Carvalho de Melo len = 0; 649531669a0SArnaldo Carvalho de Melo break; 650531669a0SArnaldo Carvalho de Melo } 6517c657876SArnaldo Carvalho de Melo 6527c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 6537c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 6547c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 6557c657876SArnaldo Carvalho de Melo * from never connected socket. 6567c657876SArnaldo Carvalho de Melo */ 657531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 6587c657876SArnaldo Carvalho de Melo break; 6597c657876SArnaldo Carvalho de Melo } 660531669a0SArnaldo Carvalho de Melo len = 0; 6617c657876SArnaldo Carvalho de Melo break; 6627c657876SArnaldo Carvalho de Melo } 6637c657876SArnaldo Carvalho de Melo 6647c657876SArnaldo Carvalho de Melo if (!timeo) { 665531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 6667c657876SArnaldo Carvalho de Melo break; 6677c657876SArnaldo Carvalho de Melo } 6687c657876SArnaldo Carvalho de Melo 6697c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 670531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 6717c657876SArnaldo Carvalho de Melo break; 6727c657876SArnaldo Carvalho de Melo } 6737c657876SArnaldo Carvalho de Melo 6747c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 6757c657876SArnaldo Carvalho de Melo continue; 6767c657876SArnaldo Carvalho de Melo found_ok_skb: 677531669a0SArnaldo Carvalho de Melo if (len > skb->len) 678531669a0SArnaldo Carvalho de Melo len = skb->len; 679531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 680531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 6817c657876SArnaldo Carvalho de Melo 682531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 6837c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 684531669a0SArnaldo Carvalho de Melo len = -EFAULT; 6857c657876SArnaldo Carvalho de Melo break; 6867c657876SArnaldo Carvalho de Melo } 6877c657876SArnaldo Carvalho de Melo found_fin_ok: 6887c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 6897c657876SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 6907c657876SArnaldo Carvalho de Melo break; 691531669a0SArnaldo Carvalho de Melo } while (1); 6927c657876SArnaldo Carvalho de Melo out: 6937c657876SArnaldo Carvalho de Melo release_sock(sk); 694531669a0SArnaldo Carvalho de Melo return len; 6957c657876SArnaldo Carvalho de Melo } 6967c657876SArnaldo Carvalho de Melo 697f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg); 698f21e68caSArnaldo Carvalho de Melo 699f21e68caSArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog) 7007c657876SArnaldo Carvalho de Melo { 7017c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 7027c657876SArnaldo Carvalho de Melo unsigned char old_state; 7037c657876SArnaldo Carvalho de Melo int err; 7047c657876SArnaldo Carvalho de Melo 7057c657876SArnaldo Carvalho de Melo lock_sock(sk); 7067c657876SArnaldo Carvalho de Melo 7077c657876SArnaldo Carvalho de Melo err = -EINVAL; 7087c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 7097c657876SArnaldo Carvalho de Melo goto out; 7107c657876SArnaldo Carvalho de Melo 7117c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 7127c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 7137c657876SArnaldo Carvalho de Melo goto out; 7147c657876SArnaldo Carvalho de Melo 7157c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 7167c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 7177c657876SArnaldo Carvalho de Melo */ 7187c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 7197c657876SArnaldo Carvalho de Melo /* 7207c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 7217c657876SArnaldo Carvalho de Melo * see tcp_listen_start 7227c657876SArnaldo Carvalho de Melo */ 7237c657876SArnaldo Carvalho de Melo err = dccp_listen_start(sk); 7247c657876SArnaldo Carvalho de Melo if (err) 7257c657876SArnaldo Carvalho de Melo goto out; 7267c657876SArnaldo Carvalho de Melo } 7277c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 7287c657876SArnaldo Carvalho de Melo err = 0; 7297c657876SArnaldo Carvalho de Melo 7307c657876SArnaldo Carvalho de Melo out: 7317c657876SArnaldo Carvalho de Melo release_sock(sk); 7327c657876SArnaldo Carvalho de Melo return err; 7337c657876SArnaldo Carvalho de Melo } 7347c657876SArnaldo Carvalho de Melo 735f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_dccp_listen); 736f21e68caSArnaldo Carvalho de Melo 7377c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 7387c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 7397c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 7407c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 7417c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 7427c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 7437c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 7447c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 7457c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 7467c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 7477c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 7487c657876SArnaldo Carvalho de Melo }; 7497c657876SArnaldo Carvalho de Melo 7507c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 7517c657876SArnaldo Carvalho de Melo { 7527c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 7537c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 7547c657876SArnaldo Carvalho de Melo 7557c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 7567c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 7577c657876SArnaldo Carvalho de Melo 7587c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 7597c657876SArnaldo Carvalho de Melo } 7607c657876SArnaldo Carvalho de Melo 7617c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 7627c657876SArnaldo Carvalho de Melo { 7637c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 7647c657876SArnaldo Carvalho de Melo 7657c657876SArnaldo Carvalho de Melo lock_sock(sk); 7667c657876SArnaldo Carvalho de Melo 7677c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 7687c657876SArnaldo Carvalho de Melo 7697c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 7707c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 7717c657876SArnaldo Carvalho de Melo 7727c657876SArnaldo Carvalho de Melo /* Special case. */ 7737c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 7747c657876SArnaldo Carvalho de Melo 7757c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 7767c657876SArnaldo Carvalho de Melo } 7777c657876SArnaldo Carvalho de Melo 7787c657876SArnaldo Carvalho de Melo /* 7797c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 7807c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 7817c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 7827c657876SArnaldo Carvalho de Melo */ 7837c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 7847c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 7857c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 7867c657876SArnaldo Carvalho de Melo } 7877c657876SArnaldo Carvalho de Melo 7887c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 7897c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 7907c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 7917c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 7927ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 7937c657876SArnaldo Carvalho de Melo } 7947c657876SArnaldo Carvalho de Melo 7957c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 7967c657876SArnaldo Carvalho de Melo 7977c657876SArnaldo Carvalho de Melo adjudge_to_death: 7987ad07e7cSArnaldo Carvalho de Melo /* 7997ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 8007ad07e7cSArnaldo Carvalho de Melo */ 8017c657876SArnaldo Carvalho de Melo release_sock(sk); 8027c657876SArnaldo Carvalho de Melo /* 8037c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 8047c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 8057c657876SArnaldo Carvalho de Melo */ 8067c657876SArnaldo Carvalho de Melo local_bh_disable(); 8077c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 8087c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 8097c657876SArnaldo Carvalho de Melo 8107c657876SArnaldo Carvalho de Melo sock_hold(sk); 8117c657876SArnaldo Carvalho de Melo sock_orphan(sk); 8127c657876SArnaldo Carvalho de Melo 8137ad07e7cSArnaldo Carvalho de Melo /* 8147ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 8157ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 8167ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 8177ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 8187ad07e7cSArnaldo Carvalho de Melo */ 8197ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 8207ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 8217ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 8227ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 8237ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 8247ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 8257ad07e7cSArnaldo Carvalho de Melo #if 0 8267ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 8277c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 8287ad07e7cSArnaldo Carvalho de Melo #endif 8297ad07e7cSArnaldo Carvalho de Melo } 8307c657876SArnaldo Carvalho de Melo 8317ad07e7cSArnaldo Carvalho de Melo atomic_inc(sk->sk_prot->orphan_count); 8327c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 8337c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 8347c657876SArnaldo Carvalho de Melo 8357c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 8367c657876SArnaldo Carvalho de Melo 8377c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 8387c657876SArnaldo Carvalho de Melo local_bh_enable(); 8397c657876SArnaldo Carvalho de Melo sock_put(sk); 8407c657876SArnaldo Carvalho de Melo } 8417c657876SArnaldo Carvalho de Melo 842f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_close); 843f21e68caSArnaldo Carvalho de Melo 8447c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 8457c657876SArnaldo Carvalho de Melo { 8467c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 8477c657876SArnaldo Carvalho de Melo } 8487c657876SArnaldo Carvalho de Melo 849f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown); 850f21e68caSArnaldo Carvalho de Melo 85146f09ffaSArnaldo Carvalho de Melo static int __init dccp_mib_init(void) 8527c657876SArnaldo Carvalho de Melo { 8537c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 8547c657876SArnaldo Carvalho de Melo 8557c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 8567c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 8577c657876SArnaldo Carvalho de Melo goto out; 8587c657876SArnaldo Carvalho de Melo 8597c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 8607c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 8617c657876SArnaldo Carvalho de Melo goto out_free_one; 8627c657876SArnaldo Carvalho de Melo 8637c657876SArnaldo Carvalho de Melo rc = 0; 8647c657876SArnaldo Carvalho de Melo out: 8657c657876SArnaldo Carvalho de Melo return rc; 8667c657876SArnaldo Carvalho de Melo out_free_one: 8677c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 8687c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 8697c657876SArnaldo Carvalho de Melo goto out; 8707c657876SArnaldo Carvalho de Melo 8717c657876SArnaldo Carvalho de Melo } 8727c657876SArnaldo Carvalho de Melo 873b61fafc4SArnaldo Carvalho de Melo static void dccp_mib_exit(void) 87446f09ffaSArnaldo Carvalho de Melo { 87546f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 87646f09ffaSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 87746f09ffaSArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 87846f09ffaSArnaldo Carvalho de Melo } 87946f09ffaSArnaldo Carvalho de Melo 8807c657876SArnaldo Carvalho de Melo static int thash_entries; 8817c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 8827c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 8837c657876SArnaldo Carvalho de Melo 884a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 8857c657876SArnaldo Carvalho de Melo int dccp_debug; 8867c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 8877c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 888f21e68caSArnaldo Carvalho de Melo 889f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_debug); 890a1d3a355SArnaldo Carvalho de Melo #endif 8917c657876SArnaldo Carvalho de Melo 8927c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 8937c657876SArnaldo Carvalho de Melo { 8947c657876SArnaldo Carvalho de Melo unsigned long goal; 8957c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 896b61fafc4SArnaldo Carvalho de Melo int rc = -ENOBUFS; 8977c657876SArnaldo Carvalho de Melo 8987690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 8997690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 9007690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 9017690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 9027c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 903b61fafc4SArnaldo Carvalho de Melo goto out; 9047c657876SArnaldo Carvalho de Melo 9057c657876SArnaldo Carvalho de Melo /* 9067c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 9077c657876SArnaldo Carvalho de Melo * hash tables. 9087c657876SArnaldo Carvalho de Melo * 9097c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 9107c657876SArnaldo Carvalho de Melo */ 9117c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 9127c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 9137c657876SArnaldo Carvalho de Melo else 9147c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 9157c657876SArnaldo Carvalho de Melo 9167c657876SArnaldo Carvalho de Melo if (thash_entries) 9177690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 9187690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 9197c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 9207c657876SArnaldo Carvalho de Melo ; 9217c657876SArnaldo Carvalho de Melo do { 9227c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 9237c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 9247c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 9257690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 9267690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 9277c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 9287c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 9297c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 9307c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 9317c657876SArnaldo Carvalho de Melo 9327c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 9337c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 9347c657876SArnaldo Carvalho de Melo "established hash table\n"); 9357c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 9367c657876SArnaldo Carvalho de Melo } 9377c657876SArnaldo Carvalho de Melo 9387c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 9397c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 9407c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 9417c657876SArnaldo Carvalho de Melo } 9427c657876SArnaldo Carvalho de Melo 9437c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 9447c657876SArnaldo Carvalho de Melo 9457c657876SArnaldo Carvalho de Melo do { 9467c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 9477c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 9487690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 9497690af3fSArnaldo Carvalho de Melo bhash_order > 0) 9507c657876SArnaldo Carvalho de Melo continue; 9517c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 9527c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 9537c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 9547c657876SArnaldo Carvalho de Melo 9557c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 9567c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 9577c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 9587c657876SArnaldo Carvalho de Melo } 9597c657876SArnaldo Carvalho de Melo 9607c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 9617c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 9627c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 9637c657876SArnaldo Carvalho de Melo } 9647c657876SArnaldo Carvalho de Melo 96546f09ffaSArnaldo Carvalho de Melo rc = dccp_mib_init(); 966fa23e2ecSArnaldo Carvalho de Melo if (rc) 9677c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 9687c657876SArnaldo Carvalho de Melo 9699b07ef5dSArnaldo Carvalho de Melo rc = dccp_ackvec_init(); 9707c657876SArnaldo Carvalho de Melo if (rc) 971b61fafc4SArnaldo Carvalho de Melo goto out_free_dccp_mib; 9729b07ef5dSArnaldo Carvalho de Melo 973e55d912fSArnaldo Carvalho de Melo rc = dccp_sysctl_init(); 9749b07ef5dSArnaldo Carvalho de Melo if (rc) 9759b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit; 9767c657876SArnaldo Carvalho de Melo out: 9777c657876SArnaldo Carvalho de Melo return rc; 9789b07ef5dSArnaldo Carvalho de Melo out_ackvec_exit: 9799b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 980b61fafc4SArnaldo Carvalho de Melo out_free_dccp_mib: 98146f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 9827c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 9837c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 9847c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 9857c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 9867c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 9877c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 9887c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 9897c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 9907c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 9917c657876SArnaldo Carvalho de Melo goto out; 9927c657876SArnaldo Carvalho de Melo } 9937c657876SArnaldo Carvalho de Melo 9947c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 9957c657876SArnaldo Carvalho de Melo { 99646f09ffaSArnaldo Carvalho de Melo dccp_mib_exit(); 997725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 998725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 999725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 1000725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 1001725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 1002725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 10037c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 10049b07ef5dSArnaldo Carvalho de Melo dccp_ackvec_exit(); 1005e55d912fSArnaldo Carvalho de Melo dccp_sysctl_exit(); 10067c657876SArnaldo Carvalho de Melo } 10077c657876SArnaldo Carvalho de Melo 10087c657876SArnaldo Carvalho de Melo module_init(dccp_init); 10097c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 10107c657876SArnaldo Carvalho de Melo 10117c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 10127c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 10137c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 1014