1/*
2 * Copyright (c) 2006 Oracle.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
34#include <linux/in.h>
35#include <net/tcp.h>
36
37#include "rds.h"
38#include "tcp.h"
39
40void rds_tcp_state_change(struct sock *sk)
41{
42	void (*state_change)(struct sock *sk);
43	struct rds_connection *conn;
44	struct rds_tcp_connection *tc;
45
46	read_lock(&sk->sk_callback_lock);
47	conn = sk->sk_user_data;
48	if (!conn) {
49		state_change = sk->sk_state_change;
50		goto out;
51	}
52	tc = conn->c_transport_data;
53	state_change = tc->t_orig_state_change;
54
55	rdsdebug("sock %p state_change to %d\n", tc->t_sock, sk->sk_state);
56
57	switch(sk->sk_state) {
58		/* ignore connecting sockets as they make progress */
59		case TCP_SYN_SENT:
60		case TCP_SYN_RECV:
61			break;
62		case TCP_ESTABLISHED:
63			rds_connect_complete(conn);
64			break;
65		case TCP_CLOSE_WAIT:
66		case TCP_CLOSE:
67			rds_conn_drop(conn);
68		default:
69			break;
70	}
71out:
72	read_unlock(&sk->sk_callback_lock);
73	state_change(sk);
74}
75
76int rds_tcp_conn_connect(struct rds_connection *conn)
77{
78	struct socket *sock = NULL;
79	struct sockaddr_in src, dest;
80	int ret;
81
82	ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
83	if (ret < 0)
84		goto out;
85
86	rds_tcp_tune(sock);
87
88	src.sin_family = AF_INET;
89	src.sin_addr.s_addr = (__force u32)conn->c_laddr;
90	src.sin_port = (__force u16)htons(0);
91
92	ret = sock->ops->bind(sock, (struct sockaddr *)&src, sizeof(src));
93	if (ret) {
94		rdsdebug("bind failed with %d at address %pI4\n",
95			 ret, &conn->c_laddr);
96		goto out;
97	}
98
99	dest.sin_family = AF_INET;
100	dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
101	dest.sin_port = (__force u16)htons(RDS_TCP_PORT);
102
103	/*
104	 * once we call connect() we can start getting callbacks and they
105	 * own the socket
106	 */
107	rds_tcp_set_callbacks(sock, conn);
108	ret = sock->ops->connect(sock, (struct sockaddr *)&dest, sizeof(dest),
109				 O_NONBLOCK);
110
111	rdsdebug("connect to address %pI4 returned %d\n", &conn->c_faddr, ret);
112	if (ret == -EINPROGRESS)
113		ret = 0;
114	if (ret == 0)
115		sock = NULL;
116	else
117		rds_tcp_restore_callbacks(sock, conn->c_transport_data);
118
119out:
120	if (sock)
121		sock_release(sock);
122	return ret;
123}
124
125/*
126 * Before killing the tcp socket this needs to serialize with callbacks.  The
127 * caller has already grabbed the sending sem so we're serialized with other
128 * senders.
129 *
130 * TCP calls the callbacks with the sock lock so we hold it while we reset the
131 * callbacks to those set by TCP.  Our callbacks won't execute again once we
132 * hold the sock lock.
133 */
134void rds_tcp_conn_shutdown(struct rds_connection *conn)
135{
136	struct rds_tcp_connection *tc = conn->c_transport_data;
137	struct socket *sock = tc->t_sock;
138
139	rdsdebug("shutting down conn %p tc %p sock %p\n", conn, tc, sock);
140
141	if (sock) {
142		sock->ops->shutdown(sock, RCV_SHUTDOWN | SEND_SHUTDOWN);
143		lock_sock(sock->sk);
144		rds_tcp_restore_callbacks(sock, tc); /* tc->tc_sock = NULL */
145
146		release_sock(sock->sk);
147		sock_release(sock);
148	}
149
150	if (tc->t_tinc) {
151		rds_inc_put(&tc->t_tinc->ti_inc);
152		tc->t_tinc = NULL;
153	}
154	tc->t_tinc_hdr_rem = sizeof(struct rds_header);
155	tc->t_tinc_data_rem = 0;
156}
157