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> 277c657876SArnaldo Carvalho de Melo #include <net/ip.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 #include <linux/dccp.h> 387c657876SArnaldo Carvalho de Melo 397c657876SArnaldo Carvalho de Melo #include "ccid.h" 407c657876SArnaldo Carvalho de Melo #include "dccp.h" 417c657876SArnaldo Carvalho de Melo 42ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly; 437c657876SArnaldo Carvalho de Melo 447c657876SArnaldo Carvalho de Melo atomic_t dccp_orphan_count = ATOMIC_INIT(0); 457c657876SArnaldo Carvalho de Melo 467c657876SArnaldo Carvalho de Melo static struct net_protocol dccp_protocol = { 477c657876SArnaldo Carvalho de Melo .handler = dccp_v4_rcv, 487c657876SArnaldo Carvalho de Melo .err_handler = dccp_v4_err, 497c657876SArnaldo Carvalho de Melo }; 507c657876SArnaldo Carvalho de Melo 517c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type) 527c657876SArnaldo Carvalho de Melo { 537c657876SArnaldo Carvalho de Melo static const char *dccp_packet_names[] = { 547c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST", 557c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE", 567c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA", 577c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK", 587c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK", 597c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ", 607c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE", 617c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET", 627c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC", 637c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK", 647c657876SArnaldo Carvalho de Melo }; 657c657876SArnaldo Carvalho de Melo 667c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES) 677c657876SArnaldo Carvalho de Melo return "INVALID"; 687c657876SArnaldo Carvalho de Melo else 697c657876SArnaldo Carvalho de Melo return dccp_packet_names[type]; 707c657876SArnaldo Carvalho de Melo } 717c657876SArnaldo Carvalho de Melo 727c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name); 737c657876SArnaldo Carvalho de Melo 747c657876SArnaldo Carvalho de Melo const char *dccp_state_name(const int state) 757c657876SArnaldo Carvalho de Melo { 767c657876SArnaldo Carvalho de Melo static char *dccp_state_names[] = { 777c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = "OPEN", 787c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = "REQUESTING", 797c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = "PARTOPEN", 807c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = "LISTEN", 817c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = "RESPOND", 827c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = "CLOSING", 837c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = "TIME_WAIT", 847c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = "CLOSED", 857c657876SArnaldo Carvalho de Melo }; 867c657876SArnaldo Carvalho de Melo 877c657876SArnaldo Carvalho de Melo if (state >= DCCP_MAX_STATES) 887c657876SArnaldo Carvalho de Melo return "INVALID STATE!"; 897c657876SArnaldo Carvalho de Melo else 907c657876SArnaldo Carvalho de Melo return dccp_state_names[state]; 917c657876SArnaldo Carvalho de Melo } 927c657876SArnaldo Carvalho de Melo 937c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_state_name); 947c657876SArnaldo Carvalho de Melo 957c657876SArnaldo Carvalho de Melo static inline int dccp_listen_start(struct sock *sk) 967c657876SArnaldo Carvalho de Melo { 9767e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 9867e6b629SArnaldo Carvalho de Melo 9967e6b629SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN; 10067e6b629SArnaldo Carvalho de Melo /* 10167e6b629SArnaldo Carvalho de Melo * Apps need to use setsockopt(DCCP_SOCKOPT_SERVICE) 10267e6b629SArnaldo Carvalho de Melo * before calling listen() 10367e6b629SArnaldo Carvalho de Melo */ 10467e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 10567e6b629SArnaldo Carvalho de Melo return -EPROTO; 1067c657876SArnaldo Carvalho de Melo return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE); 1077c657876SArnaldo Carvalho de Melo } 1087c657876SArnaldo Carvalho de Melo 1097c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags) 1107c657876SArnaldo Carvalho de Melo { 1117c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 1127c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 1137c657876SArnaldo Carvalho de Melo int err = 0; 1147c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state; 1157c657876SArnaldo Carvalho de Melo 1167c657876SArnaldo Carvalho de Melo if (old_state != DCCP_CLOSED) 1177c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 1187c657876SArnaldo Carvalho de Melo 1197c657876SArnaldo Carvalho de Melo /* ABORT function of RFC793 */ 1207c657876SArnaldo Carvalho de Melo if (old_state == DCCP_LISTEN) { 1217c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 1227c657876SArnaldo Carvalho de Melo /* FIXME: do the active reset thing */ 1237c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING) 1247c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET; 1257c657876SArnaldo Carvalho de Melo 1267c657876SArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk); 1277c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue); 1287c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) { 1297c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head); 1307c657876SArnaldo Carvalho de Melo sk->sk_send_head = NULL; 1317c657876SArnaldo Carvalho de Melo } 1327c657876SArnaldo Carvalho de Melo 1337c657876SArnaldo Carvalho de Melo inet->dport = 0; 1347c657876SArnaldo Carvalho de Melo 1357c657876SArnaldo Carvalho de Melo if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 1367c657876SArnaldo Carvalho de Melo inet_reset_saddr(sk); 1377c657876SArnaldo Carvalho de Melo 1387c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0; 1397c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE); 1407c657876SArnaldo Carvalho de Melo 1417c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 1427c657876SArnaldo Carvalho de Melo inet_csk_delack_init(sk); 1437c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk); 1447c657876SArnaldo Carvalho de Melo 1457c657876SArnaldo Carvalho de Melo BUG_TRAP(!inet->num || icsk->icsk_bind_hash); 1467c657876SArnaldo Carvalho de Melo 1477c657876SArnaldo Carvalho de Melo sk->sk_error_report(sk); 1487c657876SArnaldo Carvalho de Melo return err; 1497c657876SArnaldo Carvalho de Melo } 1507c657876SArnaldo Carvalho de Melo 151331968bdSArnaldo Carvalho de Melo /* 152331968bdSArnaldo Carvalho de Melo * Wait for a DCCP event. 153331968bdSArnaldo Carvalho de Melo * 154331968bdSArnaldo Carvalho de Melo * Note that we don't need to lock the socket, as the upper poll layers 155331968bdSArnaldo Carvalho de Melo * take care of normal races (between the test and the event) and we don't 156331968bdSArnaldo Carvalho de Melo * go look at any of the socket buffers directly. 157331968bdSArnaldo Carvalho de Melo */ 158331968bdSArnaldo Carvalho de Melo static unsigned int dccp_poll(struct file *file, struct socket *sock, 159331968bdSArnaldo Carvalho de Melo poll_table *wait) 160331968bdSArnaldo Carvalho de Melo { 161331968bdSArnaldo Carvalho de Melo unsigned int mask; 162331968bdSArnaldo Carvalho de Melo struct sock *sk = sock->sk; 163331968bdSArnaldo Carvalho de Melo 164331968bdSArnaldo Carvalho de Melo poll_wait(file, sk->sk_sleep, wait); 165331968bdSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) 166331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk); 167331968bdSArnaldo Carvalho de Melo 168331968bdSArnaldo Carvalho de Melo /* Socket is not locked. We are protected from async events 169331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes 170331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case. 171331968bdSArnaldo Carvalho de Melo */ 172331968bdSArnaldo Carvalho de Melo 173331968bdSArnaldo Carvalho de Melo mask = 0; 174331968bdSArnaldo Carvalho de Melo if (sk->sk_err) 175331968bdSArnaldo Carvalho de Melo mask = POLLERR; 176331968bdSArnaldo Carvalho de Melo 177331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED) 178331968bdSArnaldo Carvalho de Melo mask |= POLLHUP; 179331968bdSArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) 180331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 181331968bdSArnaldo Carvalho de Melo 182331968bdSArnaldo Carvalho de Melo /* Connected? */ 183331968bdSArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) { 184331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0) 185331968bdSArnaldo Carvalho de Melo mask |= POLLIN | POLLRDNORM; 186331968bdSArnaldo Carvalho de Melo 187331968bdSArnaldo Carvalho de Melo if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { 188331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 189331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 190331968bdSArnaldo Carvalho de Melo } else { /* send SIGIO later */ 191331968bdSArnaldo Carvalho de Melo set_bit(SOCK_ASYNC_NOSPACE, 192331968bdSArnaldo Carvalho de Melo &sk->sk_socket->flags); 193331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 194331968bdSArnaldo Carvalho de Melo 195331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after 196331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set, 197331968bdSArnaldo Carvalho de Melo * IO signal will be lost. 198331968bdSArnaldo Carvalho de Melo */ 199331968bdSArnaldo Carvalho de Melo if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) 200331968bdSArnaldo Carvalho de Melo mask |= POLLOUT | POLLWRNORM; 201331968bdSArnaldo Carvalho de Melo } 202331968bdSArnaldo Carvalho de Melo } 203331968bdSArnaldo Carvalho de Melo } 204331968bdSArnaldo Carvalho de Melo return mask; 205331968bdSArnaldo Carvalho de Melo } 206331968bdSArnaldo Carvalho de Melo 2077c657876SArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) 2087c657876SArnaldo Carvalho de Melo { 2097c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 2107c657876SArnaldo Carvalho de Melo return -ENOIOCTLCMD; 2117c657876SArnaldo Carvalho de Melo } 2127c657876SArnaldo Carvalho de Melo 21367e6b629SArnaldo Carvalho de Melo static int dccp_setsockopt_service(struct sock *sk, const u32 service, 21467e6b629SArnaldo Carvalho de Melo char __user *optval, int optlen) 21567e6b629SArnaldo Carvalho de Melo { 21667e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk); 21767e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL; 21867e6b629SArnaldo Carvalho de Melo 21967e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE || 22067e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32)) 22167e6b629SArnaldo Carvalho de Melo return -EINVAL; 22267e6b629SArnaldo Carvalho de Melo 22367e6b629SArnaldo Carvalho de Melo if (optlen > sizeof(service)) { 22467e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL); 22567e6b629SArnaldo Carvalho de Melo if (sl == NULL) 22667e6b629SArnaldo Carvalho de Melo return -ENOMEM; 22767e6b629SArnaldo Carvalho de Melo 22867e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1; 22967e6b629SArnaldo Carvalho de Melo if (copy_from_user(sl->dccpsl_list, 23067e6b629SArnaldo Carvalho de Melo optval + sizeof(service), 23167e6b629SArnaldo Carvalho de Melo optlen - sizeof(service)) || 23267e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) { 23367e6b629SArnaldo Carvalho de Melo kfree(sl); 23467e6b629SArnaldo Carvalho de Melo return -EFAULT; 23567e6b629SArnaldo Carvalho de Melo } 23667e6b629SArnaldo Carvalho de Melo } 23767e6b629SArnaldo Carvalho de Melo 23867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 23967e6b629SArnaldo Carvalho de Melo dp->dccps_service = service; 24067e6b629SArnaldo Carvalho de Melo 24167e6b629SArnaldo Carvalho de Melo if (dp->dccps_service_list != NULL) 24267e6b629SArnaldo Carvalho de Melo kfree(dp->dccps_service_list); 24367e6b629SArnaldo Carvalho de Melo 24467e6b629SArnaldo Carvalho de Melo dp->dccps_service_list = sl; 24567e6b629SArnaldo Carvalho de Melo release_sock(sk); 24667e6b629SArnaldo Carvalho de Melo return 0; 24767e6b629SArnaldo Carvalho de Melo } 24867e6b629SArnaldo Carvalho de Melo 2497c657876SArnaldo Carvalho de Melo int dccp_setsockopt(struct sock *sk, int level, int optname, 250a1d3a355SArnaldo Carvalho de Melo char __user *optval, int optlen) 2517c657876SArnaldo Carvalho de Melo { 252a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 253a84ffe43SArnaldo Carvalho de Melo int err; 254a84ffe43SArnaldo Carvalho de Melo int val; 2557c657876SArnaldo Carvalho de Melo 2567c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 2577c657876SArnaldo Carvalho de Melo return ip_setsockopt(sk, level, optname, optval, optlen); 2587c657876SArnaldo Carvalho de Melo 259a84ffe43SArnaldo Carvalho de Melo if (optlen < sizeof(int)) 260a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 261a84ffe43SArnaldo Carvalho de Melo 262a84ffe43SArnaldo Carvalho de Melo if (get_user(val, (int __user *)optval)) 263a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 264a84ffe43SArnaldo Carvalho de Melo 26567e6b629SArnaldo Carvalho de Melo if (optname == DCCP_SOCKOPT_SERVICE) 26667e6b629SArnaldo Carvalho de Melo return dccp_setsockopt_service(sk, val, optval, optlen); 267a84ffe43SArnaldo Carvalho de Melo 26867e6b629SArnaldo Carvalho de Melo lock_sock(sk); 269a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 270a84ffe43SArnaldo Carvalho de Melo err = 0; 271a84ffe43SArnaldo Carvalho de Melo 272a84ffe43SArnaldo Carvalho de Melo switch (optname) { 273a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 274a84ffe43SArnaldo Carvalho de Melo dp->dccps_packet_size = val; 275a84ffe43SArnaldo Carvalho de Melo break; 276a84ffe43SArnaldo Carvalho de Melo default: 277a84ffe43SArnaldo Carvalho de Melo err = -ENOPROTOOPT; 278a84ffe43SArnaldo Carvalho de Melo break; 279a84ffe43SArnaldo Carvalho de Melo } 280a84ffe43SArnaldo Carvalho de Melo 281a84ffe43SArnaldo Carvalho de Melo release_sock(sk); 282a84ffe43SArnaldo Carvalho de Melo return err; 2837c657876SArnaldo Carvalho de Melo } 2847c657876SArnaldo Carvalho de Melo 28567e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len, 28667e6b629SArnaldo Carvalho de Melo u32 __user *optval, 28767e6b629SArnaldo Carvalho de Melo int __user *optlen) 28867e6b629SArnaldo Carvalho de Melo { 28967e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 29067e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl; 29167e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32); 29267e6b629SArnaldo Carvalho de Melo 29367e6b629SArnaldo Carvalho de Melo lock_sock(sk); 29467e6b629SArnaldo Carvalho de Melo if (dccp_service_not_initialized(sk)) 29567e6b629SArnaldo Carvalho de Melo goto out; 29667e6b629SArnaldo Carvalho de Melo 29767e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) { 29867e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32); 29967e6b629SArnaldo Carvalho de Melo total_len += slen; 30067e6b629SArnaldo Carvalho de Melo } 30167e6b629SArnaldo Carvalho de Melo 30267e6b629SArnaldo Carvalho de Melo err = -EINVAL; 30367e6b629SArnaldo Carvalho de Melo if (total_len > len) 30467e6b629SArnaldo Carvalho de Melo goto out; 30567e6b629SArnaldo Carvalho de Melo 30667e6b629SArnaldo Carvalho de Melo err = 0; 30767e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) || 30867e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) || 30967e6b629SArnaldo Carvalho de Melo (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen))) 31067e6b629SArnaldo Carvalho de Melo err = -EFAULT; 31167e6b629SArnaldo Carvalho de Melo out: 31267e6b629SArnaldo Carvalho de Melo release_sock(sk); 31367e6b629SArnaldo Carvalho de Melo return err; 31467e6b629SArnaldo Carvalho de Melo } 31567e6b629SArnaldo Carvalho de Melo 3167c657876SArnaldo Carvalho de Melo int dccp_getsockopt(struct sock *sk, int level, int optname, 317a1d3a355SArnaldo Carvalho de Melo char __user *optval, int __user *optlen) 3187c657876SArnaldo Carvalho de Melo { 319a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp; 320a84ffe43SArnaldo Carvalho de Melo int val, len; 3217c657876SArnaldo Carvalho de Melo 3227c657876SArnaldo Carvalho de Melo if (level != SOL_DCCP) 3237c657876SArnaldo Carvalho de Melo return ip_getsockopt(sk, level, optname, optval, optlen); 3247c657876SArnaldo Carvalho de Melo 325a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen)) 326a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 327a84ffe43SArnaldo Carvalho de Melo 32888f964dbSArnaldo Carvalho de Melo if (len < sizeof(int)) 329a84ffe43SArnaldo Carvalho de Melo return -EINVAL; 330a84ffe43SArnaldo Carvalho de Melo 331a84ffe43SArnaldo Carvalho de Melo dp = dccp_sk(sk); 332a84ffe43SArnaldo Carvalho de Melo 333a84ffe43SArnaldo Carvalho de Melo switch (optname) { 334a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_PACKET_SIZE: 335a84ffe43SArnaldo Carvalho de Melo val = dp->dccps_packet_size; 33688f964dbSArnaldo Carvalho de Melo len = sizeof(dp->dccps_packet_size); 337a84ffe43SArnaldo Carvalho de Melo break; 33888f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_SERVICE: 33988f964dbSArnaldo Carvalho de Melo return dccp_getsockopt_service(sk, len, 34088f964dbSArnaldo Carvalho de Melo (u32 __user *)optval, optlen); 34188f964dbSArnaldo Carvalho de Melo case 128 ... 191: 34288f964dbSArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname, 34388f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 34488f964dbSArnaldo Carvalho de Melo case 192 ... 255: 34588f964dbSArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname, 34688f964dbSArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen); 347a84ffe43SArnaldo Carvalho de Melo default: 348a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT; 349a84ffe43SArnaldo Carvalho de Melo } 350a84ffe43SArnaldo Carvalho de Melo 351a84ffe43SArnaldo Carvalho de Melo if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 352a84ffe43SArnaldo Carvalho de Melo return -EFAULT; 353a84ffe43SArnaldo Carvalho de Melo 354a84ffe43SArnaldo Carvalho de Melo return 0; 3557c657876SArnaldo Carvalho de Melo } 3567c657876SArnaldo Carvalho de Melo 3577c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 3587c657876SArnaldo Carvalho de Melo size_t len) 3597c657876SArnaldo Carvalho de Melo { 3607c657876SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk); 3617c657876SArnaldo Carvalho de Melo const int flags = msg->msg_flags; 3627c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT; 3637c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 3647c657876SArnaldo Carvalho de Melo int rc, size; 3657c657876SArnaldo Carvalho de Melo long timeo; 3667c657876SArnaldo Carvalho de Melo 3677c657876SArnaldo Carvalho de Melo if (len > dp->dccps_mss_cache) 3687c657876SArnaldo Carvalho de Melo return -EMSGSIZE; 3697c657876SArnaldo Carvalho de Melo 3707c657876SArnaldo Carvalho de Melo lock_sock(sk); 37127258ee5SArnaldo Carvalho de Melo timeo = sock_sndtimeo(sk, noblock); 3727c657876SArnaldo Carvalho de Melo 3737c657876SArnaldo Carvalho de Melo /* 3747c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending, 3757c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process. 3767c657876SArnaldo Carvalho de Melo */ 3777c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */ 3787c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING)) 3797c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0) 38027258ee5SArnaldo Carvalho de Melo goto out_release; 3817c657876SArnaldo Carvalho de Melo 3827c657876SArnaldo Carvalho de Melo size = sk->sk_prot->max_header + len; 3837c657876SArnaldo Carvalho de Melo release_sock(sk); 3847c657876SArnaldo Carvalho de Melo skb = sock_alloc_send_skb(sk, size, noblock, &rc); 3857c657876SArnaldo Carvalho de Melo lock_sock(sk); 3867c657876SArnaldo Carvalho de Melo if (skb == NULL) 3877c657876SArnaldo Carvalho de Melo goto out_release; 3887c657876SArnaldo Carvalho de Melo 3897c657876SArnaldo Carvalho de Melo skb_reserve(skb, sk->sk_prot->max_header); 3907c657876SArnaldo Carvalho de Melo rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 39127258ee5SArnaldo Carvalho de Melo if (rc != 0) 39227258ee5SArnaldo Carvalho de Melo goto out_discard; 3937c657876SArnaldo Carvalho de Melo 394d6809c12SArnaldo Carvalho de Melo rc = dccp_write_xmit(sk, skb, &timeo); 39520472af9SArnaldo Carvalho de Melo /* 39620472af9SArnaldo Carvalho de Melo * XXX we don't use sk_write_queue, so just discard the packet. 39720472af9SArnaldo Carvalho de Melo * Current plan however is to _use_ sk_write_queue with 39820472af9SArnaldo Carvalho de Melo * an algorith similar to tcp_sendmsg, where the main difference 39920472af9SArnaldo Carvalho de Melo * is that in DCCP we have to respect packet boundaries, so 40020472af9SArnaldo Carvalho de Melo * no coalescing of skbs. 40120472af9SArnaldo Carvalho de Melo * 40220472af9SArnaldo Carvalho de Melo * This bug was _quickly_ found & fixed by just looking at an OSTRA 40320472af9SArnaldo Carvalho de Melo * generated callgraph 8) -acme 40420472af9SArnaldo Carvalho de Melo */ 40520472af9SArnaldo Carvalho de Melo if (rc != 0) 40620472af9SArnaldo Carvalho de Melo goto out_discard; 4077c657876SArnaldo Carvalho de Melo out_release: 4087c657876SArnaldo Carvalho de Melo release_sock(sk); 4097c657876SArnaldo Carvalho de Melo return rc ? : len; 41027258ee5SArnaldo Carvalho de Melo out_discard: 41127258ee5SArnaldo Carvalho de Melo kfree_skb(skb); 4127c657876SArnaldo Carvalho de Melo goto out_release; 4137c657876SArnaldo Carvalho de Melo } 4147c657876SArnaldo Carvalho de Melo 4157c657876SArnaldo Carvalho de Melo int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 4167c657876SArnaldo Carvalho de Melo size_t len, int nonblock, int flags, int *addr_len) 4177c657876SArnaldo Carvalho de Melo { 4187c657876SArnaldo Carvalho de Melo const struct dccp_hdr *dh; 4197c657876SArnaldo Carvalho de Melo long timeo; 4207c657876SArnaldo Carvalho de Melo 4217c657876SArnaldo Carvalho de Melo lock_sock(sk); 4227c657876SArnaldo Carvalho de Melo 423531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 424531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 4257c657876SArnaldo Carvalho de Melo goto out; 426531669a0SArnaldo Carvalho de Melo } 4277c657876SArnaldo Carvalho de Melo 4287c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, nonblock); 4297c657876SArnaldo Carvalho de Melo 4307c657876SArnaldo Carvalho de Melo do { 431531669a0SArnaldo Carvalho de Melo struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 4327c657876SArnaldo Carvalho de Melo 433531669a0SArnaldo Carvalho de Melo if (skb == NULL) 434531669a0SArnaldo Carvalho de Melo goto verify_sock_status; 4357c657876SArnaldo Carvalho de Melo 4367c657876SArnaldo Carvalho de Melo dh = dccp_hdr(skb); 4377c657876SArnaldo Carvalho de Melo 4387c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_DATA || 4397c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_DATAACK) 4407c657876SArnaldo Carvalho de Melo goto found_ok_skb; 4417c657876SArnaldo Carvalho de Melo 4427c657876SArnaldo Carvalho de Melo if (dh->dccph_type == DCCP_PKT_RESET || 4437c657876SArnaldo Carvalho de Melo dh->dccph_type == DCCP_PKT_CLOSE) { 4447c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin ok!\n"); 445531669a0SArnaldo Carvalho de Melo len = 0; 4467c657876SArnaldo Carvalho de Melo goto found_fin_ok; 4477c657876SArnaldo Carvalho de Melo } 4487690af3fSArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n", 4497690af3fSArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type)); 450531669a0SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 451531669a0SArnaldo Carvalho de Melo verify_sock_status: 452531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) { 453531669a0SArnaldo Carvalho de Melo len = 0; 4547c657876SArnaldo Carvalho de Melo break; 4557c657876SArnaldo Carvalho de Melo } 4567c657876SArnaldo Carvalho de Melo 457531669a0SArnaldo Carvalho de Melo if (sk->sk_err) { 458531669a0SArnaldo Carvalho de Melo len = sock_error(sk); 4597c657876SArnaldo Carvalho de Melo break; 460531669a0SArnaldo Carvalho de Melo } 461531669a0SArnaldo Carvalho de Melo 462531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) { 463531669a0SArnaldo Carvalho de Melo len = 0; 464531669a0SArnaldo Carvalho de Melo break; 465531669a0SArnaldo Carvalho de Melo } 4667c657876SArnaldo Carvalho de Melo 4677c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) { 4687c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) { 4697c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read 4707c657876SArnaldo Carvalho de Melo * from never connected socket. 4717c657876SArnaldo Carvalho de Melo */ 472531669a0SArnaldo Carvalho de Melo len = -ENOTCONN; 4737c657876SArnaldo Carvalho de Melo break; 4747c657876SArnaldo Carvalho de Melo } 475531669a0SArnaldo Carvalho de Melo len = 0; 4767c657876SArnaldo Carvalho de Melo break; 4777c657876SArnaldo Carvalho de Melo } 4787c657876SArnaldo Carvalho de Melo 4797c657876SArnaldo Carvalho de Melo if (!timeo) { 480531669a0SArnaldo Carvalho de Melo len = -EAGAIN; 4817c657876SArnaldo Carvalho de Melo break; 4827c657876SArnaldo Carvalho de Melo } 4837c657876SArnaldo Carvalho de Melo 4847c657876SArnaldo Carvalho de Melo if (signal_pending(current)) { 485531669a0SArnaldo Carvalho de Melo len = sock_intr_errno(timeo); 4867c657876SArnaldo Carvalho de Melo break; 4877c657876SArnaldo Carvalho de Melo } 4887c657876SArnaldo Carvalho de Melo 4897c657876SArnaldo Carvalho de Melo sk_wait_data(sk, &timeo); 4907c657876SArnaldo Carvalho de Melo continue; 4917c657876SArnaldo Carvalho de Melo found_ok_skb: 492531669a0SArnaldo Carvalho de Melo if (len > skb->len) 493531669a0SArnaldo Carvalho de Melo len = skb->len; 494531669a0SArnaldo Carvalho de Melo else if (len < skb->len) 495531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC; 4967c657876SArnaldo Carvalho de Melo 497531669a0SArnaldo Carvalho de Melo if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { 4987c657876SArnaldo Carvalho de Melo /* Exception. Bailout! */ 499531669a0SArnaldo Carvalho de Melo len = -EFAULT; 5007c657876SArnaldo Carvalho de Melo break; 5017c657876SArnaldo Carvalho de Melo } 5027c657876SArnaldo Carvalho de Melo found_fin_ok: 5037c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK)) 5047c657876SArnaldo Carvalho de Melo sk_eat_skb(sk, skb); 5057c657876SArnaldo Carvalho de Melo break; 506531669a0SArnaldo Carvalho de Melo } while (1); 5077c657876SArnaldo Carvalho de Melo out: 5087c657876SArnaldo Carvalho de Melo release_sock(sk); 509531669a0SArnaldo Carvalho de Melo return len; 5107c657876SArnaldo Carvalho de Melo } 5117c657876SArnaldo Carvalho de Melo 5127c657876SArnaldo Carvalho de Melo static int inet_dccp_listen(struct socket *sock, int backlog) 5137c657876SArnaldo Carvalho de Melo { 5147c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk; 5157c657876SArnaldo Carvalho de Melo unsigned char old_state; 5167c657876SArnaldo Carvalho de Melo int err; 5177c657876SArnaldo Carvalho de Melo 5187c657876SArnaldo Carvalho de Melo lock_sock(sk); 5197c657876SArnaldo Carvalho de Melo 5207c657876SArnaldo Carvalho de Melo err = -EINVAL; 5217c657876SArnaldo Carvalho de Melo if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) 5227c657876SArnaldo Carvalho de Melo goto out; 5237c657876SArnaldo Carvalho de Melo 5247c657876SArnaldo Carvalho de Melo old_state = sk->sk_state; 5257c657876SArnaldo Carvalho de Melo if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) 5267c657876SArnaldo Carvalho de Melo goto out; 5277c657876SArnaldo Carvalho de Melo 5287c657876SArnaldo Carvalho de Melo /* Really, if the socket is already in listen state 5297c657876SArnaldo Carvalho de Melo * we can only allow the backlog to be adjusted. 5307c657876SArnaldo Carvalho de Melo */ 5317c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) { 5327c657876SArnaldo Carvalho de Melo /* 5337c657876SArnaldo Carvalho de Melo * FIXME: here it probably should be sk->sk_prot->listen_start 5347c657876SArnaldo Carvalho de Melo * see tcp_listen_start 5357c657876SArnaldo Carvalho de Melo */ 5367c657876SArnaldo Carvalho de Melo err = dccp_listen_start(sk); 5377c657876SArnaldo Carvalho de Melo if (err) 5387c657876SArnaldo Carvalho de Melo goto out; 5397c657876SArnaldo Carvalho de Melo } 5407c657876SArnaldo Carvalho de Melo sk->sk_max_ack_backlog = backlog; 5417c657876SArnaldo Carvalho de Melo err = 0; 5427c657876SArnaldo Carvalho de Melo 5437c657876SArnaldo Carvalho de Melo out: 5447c657876SArnaldo Carvalho de Melo release_sock(sk); 5457c657876SArnaldo Carvalho de Melo return err; 5467c657876SArnaldo Carvalho de Melo } 5477c657876SArnaldo Carvalho de Melo 5487c657876SArnaldo Carvalho de Melo static const unsigned char dccp_new_state[] = { 5497c657876SArnaldo Carvalho de Melo /* current state: new state: action: */ 5507c657876SArnaldo Carvalho de Melo [0] = DCCP_CLOSED, 5517c657876SArnaldo Carvalho de Melo [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 5527c657876SArnaldo Carvalho de Melo [DCCP_REQUESTING] = DCCP_CLOSED, 5537c657876SArnaldo Carvalho de Melo [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, 5547c657876SArnaldo Carvalho de Melo [DCCP_LISTEN] = DCCP_CLOSED, 5557c657876SArnaldo Carvalho de Melo [DCCP_RESPOND] = DCCP_CLOSED, 5567c657876SArnaldo Carvalho de Melo [DCCP_CLOSING] = DCCP_CLOSED, 5577c657876SArnaldo Carvalho de Melo [DCCP_TIME_WAIT] = DCCP_CLOSED, 5587c657876SArnaldo Carvalho de Melo [DCCP_CLOSED] = DCCP_CLOSED, 5597c657876SArnaldo Carvalho de Melo }; 5607c657876SArnaldo Carvalho de Melo 5617c657876SArnaldo Carvalho de Melo static int dccp_close_state(struct sock *sk) 5627c657876SArnaldo Carvalho de Melo { 5637c657876SArnaldo Carvalho de Melo const int next = dccp_new_state[sk->sk_state]; 5647c657876SArnaldo Carvalho de Melo const int ns = next & DCCP_STATE_MASK; 5657c657876SArnaldo Carvalho de Melo 5667c657876SArnaldo Carvalho de Melo if (ns != sk->sk_state) 5677c657876SArnaldo Carvalho de Melo dccp_set_state(sk, ns); 5687c657876SArnaldo Carvalho de Melo 5697c657876SArnaldo Carvalho de Melo return next & DCCP_ACTION_FIN; 5707c657876SArnaldo Carvalho de Melo } 5717c657876SArnaldo Carvalho de Melo 5727c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout) 5737c657876SArnaldo Carvalho de Melo { 5747c657876SArnaldo Carvalho de Melo struct sk_buff *skb; 5757c657876SArnaldo Carvalho de Melo 5767c657876SArnaldo Carvalho de Melo lock_sock(sk); 5777c657876SArnaldo Carvalho de Melo 5787c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK; 5797c657876SArnaldo Carvalho de Melo 5807c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) { 5817c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 5827c657876SArnaldo Carvalho de Melo 5837c657876SArnaldo Carvalho de Melo /* Special case. */ 5847c657876SArnaldo Carvalho de Melo inet_csk_listen_stop(sk); 5857c657876SArnaldo Carvalho de Melo 5867c657876SArnaldo Carvalho de Melo goto adjudge_to_death; 5877c657876SArnaldo Carvalho de Melo } 5887c657876SArnaldo Carvalho de Melo 5897c657876SArnaldo Carvalho de Melo /* 5907c657876SArnaldo Carvalho de Melo * We need to flush the recv. buffs. We do this only on the 5917c657876SArnaldo Carvalho de Melo * descriptor close, not protocol-sourced closes, because the 5927c657876SArnaldo Carvalho de Melo *reader process may not have drained the data yet! 5937c657876SArnaldo Carvalho de Melo */ 5947c657876SArnaldo Carvalho de Melo /* FIXME: check for unread data */ 5957c657876SArnaldo Carvalho de Melo while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { 5967c657876SArnaldo Carvalho de Melo __kfree_skb(skb); 5977c657876SArnaldo Carvalho de Melo } 5987c657876SArnaldo Carvalho de Melo 5997c657876SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 6007c657876SArnaldo Carvalho de Melo /* Check zero linger _after_ checking for unread data. */ 6017c657876SArnaldo Carvalho de Melo sk->sk_prot->disconnect(sk, 0); 6027c657876SArnaldo Carvalho de Melo } else if (dccp_close_state(sk)) { 6037ad07e7cSArnaldo Carvalho de Melo dccp_send_close(sk, 1); 6047c657876SArnaldo Carvalho de Melo } 6057c657876SArnaldo Carvalho de Melo 6067c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout); 6077c657876SArnaldo Carvalho de Melo 6087c657876SArnaldo Carvalho de Melo adjudge_to_death: 6097ad07e7cSArnaldo Carvalho de Melo /* 6107ad07e7cSArnaldo Carvalho de Melo * It is the last release_sock in its life. It will remove backlog. 6117ad07e7cSArnaldo Carvalho de Melo */ 6127c657876SArnaldo Carvalho de Melo release_sock(sk); 6137c657876SArnaldo Carvalho de Melo /* 6147c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock 6157c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs. 6167c657876SArnaldo Carvalho de Melo */ 6177c657876SArnaldo Carvalho de Melo local_bh_disable(); 6187c657876SArnaldo Carvalho de Melo bh_lock_sock(sk); 6197c657876SArnaldo Carvalho de Melo BUG_TRAP(!sock_owned_by_user(sk)); 6207c657876SArnaldo Carvalho de Melo 6217c657876SArnaldo Carvalho de Melo sock_hold(sk); 6227c657876SArnaldo Carvalho de Melo sock_orphan(sk); 6237c657876SArnaldo Carvalho de Melo 6247ad07e7cSArnaldo Carvalho de Melo /* 6257ad07e7cSArnaldo Carvalho de Melo * The last release_sock may have processed the CLOSE or RESET 6267ad07e7cSArnaldo Carvalho de Melo * packet moving sock to CLOSED state, if not we have to fire 6277ad07e7cSArnaldo Carvalho de Melo * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination" 6287ad07e7cSArnaldo Carvalho de Melo * in draft-ietf-dccp-spec-11. -acme 6297ad07e7cSArnaldo Carvalho de Melo */ 6307ad07e7cSArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSING) { 6317ad07e7cSArnaldo Carvalho de Melo /* FIXME: should start at 2 * RTT */ 6327ad07e7cSArnaldo Carvalho de Melo /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */ 6337ad07e7cSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 6347ad07e7cSArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto, 6357ad07e7cSArnaldo Carvalho de Melo DCCP_RTO_MAX); 6367ad07e7cSArnaldo Carvalho de Melo #if 0 6377ad07e7cSArnaldo Carvalho de Melo /* Yeah, we should use sk->sk_prot->orphan_count, etc */ 6387c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED); 6397ad07e7cSArnaldo Carvalho de Melo #endif 6407ad07e7cSArnaldo Carvalho de Melo } 6417c657876SArnaldo Carvalho de Melo 6427ad07e7cSArnaldo Carvalho de Melo atomic_inc(sk->sk_prot->orphan_count); 6437c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) 6447c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk); 6457c657876SArnaldo Carvalho de Melo 6467c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */ 6477c657876SArnaldo Carvalho de Melo 6487c657876SArnaldo Carvalho de Melo bh_unlock_sock(sk); 6497c657876SArnaldo Carvalho de Melo local_bh_enable(); 6507c657876SArnaldo Carvalho de Melo sock_put(sk); 6517c657876SArnaldo Carvalho de Melo } 6527c657876SArnaldo Carvalho de Melo 6537c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how) 6547c657876SArnaldo Carvalho de Melo { 6557c657876SArnaldo Carvalho de Melo dccp_pr_debug("entry\n"); 6567c657876SArnaldo Carvalho de Melo } 6577c657876SArnaldo Carvalho de Melo 658a1d3a355SArnaldo Carvalho de Melo static struct proto_ops inet_dccp_ops = { 6597c657876SArnaldo Carvalho de Melo .family = PF_INET, 6607c657876SArnaldo Carvalho de Melo .owner = THIS_MODULE, 6617c657876SArnaldo Carvalho de Melo .release = inet_release, 6627c657876SArnaldo Carvalho de Melo .bind = inet_bind, 6637c657876SArnaldo Carvalho de Melo .connect = inet_stream_connect, 6647c657876SArnaldo Carvalho de Melo .socketpair = sock_no_socketpair, 6657c657876SArnaldo Carvalho de Melo .accept = inet_accept, 6667c657876SArnaldo Carvalho de Melo .getname = inet_getname, 667331968bdSArnaldo Carvalho de Melo /* FIXME: work on tcp_poll to rename it to inet_csk_poll */ 668331968bdSArnaldo Carvalho de Melo .poll = dccp_poll, 6697c657876SArnaldo Carvalho de Melo .ioctl = inet_ioctl, 6707690af3fSArnaldo Carvalho de Melo /* FIXME: work on inet_listen to rename it to sock_common_listen */ 6717690af3fSArnaldo Carvalho de Melo .listen = inet_dccp_listen, 6727c657876SArnaldo Carvalho de Melo .shutdown = inet_shutdown, 6737c657876SArnaldo Carvalho de Melo .setsockopt = sock_common_setsockopt, 6747c657876SArnaldo Carvalho de Melo .getsockopt = sock_common_getsockopt, 6757c657876SArnaldo Carvalho de Melo .sendmsg = inet_sendmsg, 6767c657876SArnaldo Carvalho de Melo .recvmsg = sock_common_recvmsg, 6777c657876SArnaldo Carvalho de Melo .mmap = sock_no_mmap, 6787c657876SArnaldo Carvalho de Melo .sendpage = sock_no_sendpage, 6797c657876SArnaldo Carvalho de Melo }; 6807c657876SArnaldo Carvalho de Melo 6817c657876SArnaldo Carvalho de Melo extern struct net_proto_family inet_family_ops; 6827c657876SArnaldo Carvalho de Melo 6837c657876SArnaldo Carvalho de Melo static struct inet_protosw dccp_v4_protosw = { 6847c657876SArnaldo Carvalho de Melo .type = SOCK_DCCP, 6857c657876SArnaldo Carvalho de Melo .protocol = IPPROTO_DCCP, 6867c657876SArnaldo Carvalho de Melo .prot = &dccp_v4_prot, 6877c657876SArnaldo Carvalho de Melo .ops = &inet_dccp_ops, 6887c657876SArnaldo Carvalho de Melo .capability = -1, 6897c657876SArnaldo Carvalho de Melo .no_check = 0, 6907c657876SArnaldo Carvalho de Melo .flags = 0, 6917c657876SArnaldo Carvalho de Melo }; 6927c657876SArnaldo Carvalho de Melo 6937c657876SArnaldo Carvalho de Melo /* 6947c657876SArnaldo Carvalho de Melo * This is the global socket data structure used for responding to 6957c657876SArnaldo Carvalho de Melo * the Out-of-the-blue (OOTB) packets. A control sock will be created 6967c657876SArnaldo Carvalho de Melo * for this socket at the initialization time. 6977c657876SArnaldo Carvalho de Melo */ 6987c657876SArnaldo Carvalho de Melo struct socket *dccp_ctl_socket; 6997c657876SArnaldo Carvalho de Melo 7007c657876SArnaldo Carvalho de Melo static char dccp_ctl_socket_err_msg[] __initdata = 7017c657876SArnaldo Carvalho de Melo KERN_ERR "DCCP: Failed to create the control socket.\n"; 7027c657876SArnaldo Carvalho de Melo 7037c657876SArnaldo Carvalho de Melo static int __init dccp_ctl_sock_init(void) 7047c657876SArnaldo Carvalho de Melo { 7057c657876SArnaldo Carvalho de Melo int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP, 7067c657876SArnaldo Carvalho de Melo &dccp_ctl_socket); 7077c657876SArnaldo Carvalho de Melo if (rc < 0) 7087c657876SArnaldo Carvalho de Melo printk(dccp_ctl_socket_err_msg); 7097c657876SArnaldo Carvalho de Melo else { 7107c657876SArnaldo Carvalho de Melo dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; 7117c657876SArnaldo Carvalho de Melo inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1; 7127c657876SArnaldo Carvalho de Melo 7137c657876SArnaldo Carvalho de Melo /* Unhash it so that IP input processing does not even 7147c657876SArnaldo Carvalho de Melo * see it, we do not wish this socket to see incoming 7157c657876SArnaldo Carvalho de Melo * packets. 7167c657876SArnaldo Carvalho de Melo */ 7177c657876SArnaldo Carvalho de Melo dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk); 7187c657876SArnaldo Carvalho de Melo } 7197c657876SArnaldo Carvalho de Melo 7207c657876SArnaldo Carvalho de Melo return rc; 7217c657876SArnaldo Carvalho de Melo } 7227c657876SArnaldo Carvalho de Melo 723725ba8eeSArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_UNLOAD_HACK 724725ba8eeSArnaldo Carvalho de Melo void dccp_ctl_sock_exit(void) 7257c657876SArnaldo Carvalho de Melo { 7265480855bSArnaldo Carvalho de Melo if (dccp_ctl_socket != NULL) { 7277c657876SArnaldo Carvalho de Melo sock_release(dccp_ctl_socket); 7285480855bSArnaldo Carvalho de Melo dccp_ctl_socket = NULL; 7295480855bSArnaldo Carvalho de Melo } 7307c657876SArnaldo Carvalho de Melo } 7317c657876SArnaldo Carvalho de Melo 732725ba8eeSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit); 733725ba8eeSArnaldo Carvalho de Melo #endif 734725ba8eeSArnaldo Carvalho de Melo 7357c657876SArnaldo Carvalho de Melo static int __init init_dccp_v4_mibs(void) 7367c657876SArnaldo Carvalho de Melo { 7377c657876SArnaldo Carvalho de Melo int rc = -ENOMEM; 7387c657876SArnaldo Carvalho de Melo 7397c657876SArnaldo Carvalho de Melo dccp_statistics[0] = alloc_percpu(struct dccp_mib); 7407c657876SArnaldo Carvalho de Melo if (dccp_statistics[0] == NULL) 7417c657876SArnaldo Carvalho de Melo goto out; 7427c657876SArnaldo Carvalho de Melo 7437c657876SArnaldo Carvalho de Melo dccp_statistics[1] = alloc_percpu(struct dccp_mib); 7447c657876SArnaldo Carvalho de Melo if (dccp_statistics[1] == NULL) 7457c657876SArnaldo Carvalho de Melo goto out_free_one; 7467c657876SArnaldo Carvalho de Melo 7477c657876SArnaldo Carvalho de Melo rc = 0; 7487c657876SArnaldo Carvalho de Melo out: 7497c657876SArnaldo Carvalho de Melo return rc; 7507c657876SArnaldo Carvalho de Melo out_free_one: 7517c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 7527c657876SArnaldo Carvalho de Melo dccp_statistics[0] = NULL; 7537c657876SArnaldo Carvalho de Melo goto out; 7547c657876SArnaldo Carvalho de Melo 7557c657876SArnaldo Carvalho de Melo } 7567c657876SArnaldo Carvalho de Melo 7577c657876SArnaldo Carvalho de Melo static int thash_entries; 7587c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444); 7597c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); 7607c657876SArnaldo Carvalho de Melo 761a1d3a355SArnaldo Carvalho de Melo #ifdef CONFIG_IP_DCCP_DEBUG 7627c657876SArnaldo Carvalho de Melo int dccp_debug; 7637c657876SArnaldo Carvalho de Melo module_param(dccp_debug, int, 0444); 7647c657876SArnaldo Carvalho de Melo MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); 765a1d3a355SArnaldo Carvalho de Melo #endif 7667c657876SArnaldo Carvalho de Melo 7677c657876SArnaldo Carvalho de Melo static int __init dccp_init(void) 7687c657876SArnaldo Carvalho de Melo { 7697c657876SArnaldo Carvalho de Melo unsigned long goal; 7707c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i; 7717c657876SArnaldo Carvalho de Melo int rc = proto_register(&dccp_v4_prot, 1); 7727c657876SArnaldo Carvalho de Melo 7737c657876SArnaldo Carvalho de Melo if (rc) 7747c657876SArnaldo Carvalho de Melo goto out; 7757c657876SArnaldo Carvalho de Melo 7767690af3fSArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = 7777690af3fSArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket", 7787690af3fSArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0, 7797690af3fSArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN, NULL, NULL); 7807c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep) 7817c657876SArnaldo Carvalho de Melo goto out_proto_unregister; 7827c657876SArnaldo Carvalho de Melo 7837c657876SArnaldo Carvalho de Melo /* 7847c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket 7857c657876SArnaldo Carvalho de Melo * hash tables. 7867c657876SArnaldo Carvalho de Melo * 7877c657876SArnaldo Carvalho de Melo * The methodology is similar to that of the buffer cache. 7887c657876SArnaldo Carvalho de Melo */ 7897c657876SArnaldo Carvalho de Melo if (num_physpages >= (128 * 1024)) 7907c657876SArnaldo Carvalho de Melo goal = num_physpages >> (21 - PAGE_SHIFT); 7917c657876SArnaldo Carvalho de Melo else 7927c657876SArnaldo Carvalho de Melo goal = num_physpages >> (23 - PAGE_SHIFT); 7937c657876SArnaldo Carvalho de Melo 7947c657876SArnaldo Carvalho de Melo if (thash_entries) 7957690af3fSArnaldo Carvalho de Melo goal = (thash_entries * 7967690af3fSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; 7977c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) 7987c657876SArnaldo Carvalho de Melo ; 7997c657876SArnaldo Carvalho de Melo do { 8007c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / 8017c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket); 8027c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size >>= 1; 8037690af3fSArnaldo Carvalho de Melo while (dccp_hashinfo.ehash_size & 8047690af3fSArnaldo Carvalho de Melo (dccp_hashinfo.ehash_size - 1)) 8057c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash_size--; 8067c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = (struct inet_ehash_bucket *) 8077c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, ehash_order); 8087c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0); 8097c657876SArnaldo Carvalho de Melo 8107c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) { 8117c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP " 8127c657876SArnaldo Carvalho de Melo "established hash table\n"); 8137c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep; 8147c657876SArnaldo Carvalho de Melo } 8157c657876SArnaldo Carvalho de Melo 8167c657876SArnaldo Carvalho de Melo for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { 8177c657876SArnaldo Carvalho de Melo rwlock_init(&dccp_hashinfo.ehash[i].lock); 8187c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); 8197c657876SArnaldo Carvalho de Melo } 8207c657876SArnaldo Carvalho de Melo 8217c657876SArnaldo Carvalho de Melo bhash_order = ehash_order; 8227c657876SArnaldo Carvalho de Melo 8237c657876SArnaldo Carvalho de Melo do { 8247c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / 8257c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket); 8267690af3fSArnaldo Carvalho de Melo if ((dccp_hashinfo.bhash_size > (64 * 1024)) && 8277690af3fSArnaldo Carvalho de Melo bhash_order > 0) 8287c657876SArnaldo Carvalho de Melo continue; 8297c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) 8307c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC, bhash_order); 8317c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.bhash && --bhash_order >= 0); 8327c657876SArnaldo Carvalho de Melo 8337c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) { 8347c657876SArnaldo Carvalho de Melo printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); 8357c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash; 8367c657876SArnaldo Carvalho de Melo } 8377c657876SArnaldo Carvalho de Melo 8387c657876SArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) { 8397c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock); 8407c657876SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); 8417c657876SArnaldo Carvalho de Melo } 8427c657876SArnaldo Carvalho de Melo 8437c657876SArnaldo Carvalho de Melo if (init_dccp_v4_mibs()) 8447c657876SArnaldo Carvalho de Melo goto out_free_dccp_bhash; 8457c657876SArnaldo Carvalho de Melo 8467c657876SArnaldo Carvalho de Melo rc = -EAGAIN; 8477c657876SArnaldo Carvalho de Melo if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP)) 8487c657876SArnaldo Carvalho de Melo goto out_free_dccp_v4_mibs; 8497c657876SArnaldo Carvalho de Melo 8507c657876SArnaldo Carvalho de Melo inet_register_protosw(&dccp_v4_protosw); 8517c657876SArnaldo Carvalho de Melo 8527c657876SArnaldo Carvalho de Melo rc = dccp_ctl_sock_init(); 8537c657876SArnaldo Carvalho de Melo if (rc) 8547c657876SArnaldo Carvalho de Melo goto out_unregister_protosw; 8557c657876SArnaldo Carvalho de Melo out: 8567c657876SArnaldo Carvalho de Melo return rc; 8577c657876SArnaldo Carvalho de Melo out_unregister_protosw: 8587c657876SArnaldo Carvalho de Melo inet_unregister_protosw(&dccp_v4_protosw); 8597c657876SArnaldo Carvalho de Melo inet_del_protocol(&dccp_protocol, IPPROTO_DCCP); 8607c657876SArnaldo Carvalho de Melo out_free_dccp_v4_mibs: 8617c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 8627c657876SArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 8637c657876SArnaldo Carvalho de Melo dccp_statistics[0] = dccp_statistics[1] = NULL; 8647c657876SArnaldo Carvalho de Melo out_free_dccp_bhash: 8657c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); 8667c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash = NULL; 8677c657876SArnaldo Carvalho de Melo out_free_dccp_ehash: 8687c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); 8697c657876SArnaldo Carvalho de Melo dccp_hashinfo.ehash = NULL; 8707c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep: 8717c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 8727c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep = NULL; 8737c657876SArnaldo Carvalho de Melo out_proto_unregister: 8747c657876SArnaldo Carvalho de Melo proto_unregister(&dccp_v4_prot); 8757c657876SArnaldo Carvalho de Melo goto out; 8767c657876SArnaldo Carvalho de Melo } 8777c657876SArnaldo Carvalho de Melo 8787c657876SArnaldo Carvalho de Melo static const char dccp_del_proto_err_msg[] __exitdata = 8797c657876SArnaldo Carvalho de Melo KERN_ERR "can't remove dccp net_protocol\n"; 8807c657876SArnaldo Carvalho de Melo 8817c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void) 8827c657876SArnaldo Carvalho de Melo { 8837c657876SArnaldo Carvalho de Melo inet_unregister_protosw(&dccp_v4_protosw); 8847c657876SArnaldo Carvalho de Melo 8857c657876SArnaldo Carvalho de Melo if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0) 8867c657876SArnaldo Carvalho de Melo printk(dccp_del_proto_err_msg); 8877c657876SArnaldo Carvalho de Melo 888725ba8eeSArnaldo Carvalho de Melo free_percpu(dccp_statistics[0]); 889725ba8eeSArnaldo Carvalho de Melo free_percpu(dccp_statistics[1]); 890725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, 891725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.bhash_size * 892725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_bind_hashbucket))); 893725ba8eeSArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash, 894725ba8eeSArnaldo Carvalho de Melo get_order(dccp_hashinfo.ehash_size * 895725ba8eeSArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket))); 8967c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); 897725ba8eeSArnaldo Carvalho de Melo proto_unregister(&dccp_v4_prot); 8987c657876SArnaldo Carvalho de Melo } 8997c657876SArnaldo Carvalho de Melo 9007c657876SArnaldo Carvalho de Melo module_init(dccp_init); 9017c657876SArnaldo Carvalho de Melo module_exit(dccp_fini); 9027c657876SArnaldo Carvalho de Melo 903bb97d31fSArnaldo Carvalho de Melo /* 904bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33) 905bb97d31fSArnaldo Carvalho de Melo * values directly, Also cover the case where the protocol is not specified, 906bb97d31fSArnaldo Carvalho de Melo * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP 907bb97d31fSArnaldo Carvalho de Melo */ 908bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6"); 909bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6"); 9107c657876SArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 9117c657876SArnaldo Carvalho de Melo MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); 9127c657876SArnaldo Carvalho de Melo MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); 913