Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
Jon Paul Maloy | 02c00c2 | 2014-06-09 11:08:18 -0500 | [diff] [blame] | 2 | * net/tipc/socket.c: TIPC socket API |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 4 | * Copyright (c) 2001-2007, 2012-2014, Ericsson AB |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 5 | * Copyright (c) 2004-2008, 2010-2013, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 37 | #include "core.h" |
Allan Stephens | d265fef | 2010-11-30 12:00:53 +0000 | [diff] [blame] | 38 | #include "port.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 39 | #include "name_table.h" |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 40 | #include "node.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 41 | #include "link.h" |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 42 | #include <linux/export.h> |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 43 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 44 | #define SS_LISTENING -1 /* socket is listening */ |
| 45 | #define SS_READY -2 /* socket is connectionless */ |
| 46 | |
Allan Stephens | 3654ea0 | 2008-04-13 21:35:11 -0700 | [diff] [blame] | 47 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 48 | #define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */ |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 49 | #define TIPC_FWD_MSG 1 |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 50 | |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 51 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 52 | static void tipc_data_ready(struct sock *sk); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 53 | static void tipc_write_space(struct sock *sk); |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 54 | static int tipc_release(struct socket *sock); |
| 55 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags); |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 56 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 57 | static void tipc_sk_timeout(unsigned long ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 58 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 59 | static const struct proto_ops packet_ops; |
| 60 | static const struct proto_ops stream_ops; |
| 61 | static const struct proto_ops msg_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 62 | |
| 63 | static struct proto tipc_proto; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 64 | static struct proto tipc_proto_kern; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 65 | |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 66 | DEFINE_SPINLOCK(tipc_port_list_lock); |
| 67 | LIST_HEAD(tipc_socks); |
| 68 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 69 | /* |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 70 | * Revised TIPC socket locking policy: |
| 71 | * |
| 72 | * Most socket operations take the standard socket lock when they start |
| 73 | * and hold it until they finish (or until they need to sleep). Acquiring |
| 74 | * this lock grants the owner exclusive access to the fields of the socket |
| 75 | * data structures, with the exception of the backlog queue. A few socket |
| 76 | * operations can be done without taking the socket lock because they only |
| 77 | * read socket information that never changes during the life of the socket. |
| 78 | * |
| 79 | * Socket operations may acquire the lock for the associated TIPC port if they |
| 80 | * need to perform an operation on the port. If any routine needs to acquire |
| 81 | * both the socket lock and the port lock it must take the socket lock first |
| 82 | * to avoid the risk of deadlock. |
| 83 | * |
| 84 | * The dispatcher handling incoming messages cannot grab the socket lock in |
| 85 | * the standard fashion, since invoked it runs at the BH level and cannot block. |
| 86 | * Instead, it checks to see if the socket lock is currently owned by someone, |
| 87 | * and either handles the message itself or adds it to the socket's backlog |
| 88 | * queue; in the latter case the queued message is processed once the process |
| 89 | * owning the socket lock releases it. |
| 90 | * |
| 91 | * NOTE: Releasing the socket lock while an operation is sleeping overcomes |
| 92 | * the problem of a blocked socket operation preventing any other operations |
| 93 | * from occurring. However, applications must be careful if they have |
| 94 | * multiple threads trying to send (or receive) on the same socket, as these |
| 95 | * operations might interfere with each other. For example, doing a connect |
| 96 | * and a receive at the same time might allow the receive to consume the |
| 97 | * ACK message meant for the connect. While additional work could be done |
| 98 | * to try and overcome this, it doesn't seem to be worthwhile at the present. |
| 99 | * |
| 100 | * NOTE: Releasing the socket lock while an operation is sleeping also ensures |
| 101 | * that another operation that must be performed in a non-blocking manner is |
| 102 | * not delayed for very long because the lock has already been taken. |
| 103 | * |
| 104 | * NOTE: This code assumes that certain fields of a port/socket pair are |
| 105 | * constant over its lifetime; such fields can be examined without taking |
| 106 | * the socket lock and/or port lock, and do not need to be re-read even |
| 107 | * after resuming processing after waiting. These fields include: |
| 108 | * - socket type |
| 109 | * - pointer to socket sk structure (aka tipc_sock structure) |
| 110 | * - pointer to port structure |
| 111 | * - port reference |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 112 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 113 | |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 114 | #include "socket.h" |
| 115 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 116 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 117 | * advance_rx_queue - discard first buffer in socket receive queue |
| 118 | * |
| 119 | * Caller must hold socket lock |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 120 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 121 | static void advance_rx_queue(struct sock *sk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 122 | { |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 123 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 127 | * reject_rx_queue - reject all buffers in socket receive queue |
| 128 | * |
| 129 | * Caller must hold socket lock |
| 130 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 131 | static void reject_rx_queue(struct sock *sk) |
| 132 | { |
| 133 | struct sk_buff *buf; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 134 | u32 dnode; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 135 | |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 136 | while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { |
| 137 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 138 | tipc_link_xmit(buf, dnode, 0); |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 139 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 143 | * tipc_sk_create - create a TIPC socket |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 144 | * @net: network namespace (must be default network) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 145 | * @sock: pre-allocated socket structure |
| 146 | * @protocol: protocol indicator (must be 0) |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 147 | * @kern: caused by kernel or by userspace? |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 148 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 149 | * This routine creates additional data structures used by the TIPC socket, |
| 150 | * initializes them, and links them together. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 151 | * |
| 152 | * Returns 0 on success, errno otherwise |
| 153 | */ |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 154 | static int tipc_sk_create(struct net *net, struct socket *sock, |
| 155 | int protocol, int kern) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 156 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 157 | const struct proto_ops *ops; |
| 158 | socket_state state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 159 | struct sock *sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 160 | struct tipc_sock *tsk; |
| 161 | struct tipc_port *port; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 162 | struct tipc_msg *msg; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 163 | u32 ref; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 164 | |
| 165 | /* Validate arguments */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 166 | if (unlikely(protocol != 0)) |
| 167 | return -EPROTONOSUPPORT; |
| 168 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 169 | switch (sock->type) { |
| 170 | case SOCK_STREAM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 171 | ops = &stream_ops; |
| 172 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 173 | break; |
| 174 | case SOCK_SEQPACKET: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 175 | ops = &packet_ops; |
| 176 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 177 | break; |
| 178 | case SOCK_DGRAM: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 179 | case SOCK_RDM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 180 | ops = &msg_ops; |
| 181 | state = SS_READY; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 182 | break; |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 183 | default: |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 184 | return -EPROTOTYPE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 187 | /* Allocate socket's protocol area */ |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 188 | if (!kern) |
| 189 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); |
| 190 | else |
| 191 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern); |
| 192 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 193 | if (sk == NULL) |
| 194 | return -ENOMEM; |
| 195 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 196 | tsk = tipc_sk(sk); |
| 197 | port = &tsk->port; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 198 | ref = tipc_ref_acquire(port, &port->lock); |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 199 | if (!ref) { |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 200 | pr_warn("Socket create failed; reference table exhausted\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 201 | return -ENOMEM; |
| 202 | } |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 203 | port->max_pkt = MAX_PKT_DEFAULT; |
| 204 | port->ref = ref; |
| 205 | INIT_LIST_HEAD(&port->publications); |
| 206 | INIT_LIST_HEAD(&port->port_list); |
| 207 | |
| 208 | /* Guard against race during node address update */ |
| 209 | spin_lock_bh(&tipc_port_list_lock); |
| 210 | msg = &port->phdr; |
| 211 | tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, |
| 212 | NAMED_H_SIZE, 0); |
| 213 | msg_set_origport(msg, ref); |
| 214 | list_add_tail(&port->port_list, &tipc_socks); |
| 215 | spin_unlock_bh(&tipc_port_list_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 216 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 217 | /* Finish initializing socket data structures */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 218 | sock->ops = ops; |
| 219 | sock->state = state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 220 | sock_init_data(sock, sk); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 221 | k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 222 | sk->sk_backlog_rcv = tipc_backlog_rcv; |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 223 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 224 | sk->sk_data_ready = tipc_data_ready; |
| 225 | sk->sk_write_space = tipc_write_space; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 226 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 227 | tsk->sent_unacked = 0; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 228 | atomic_set(&tsk->dupl_rcvcnt, 0); |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 229 | tipc_port_unlock(port); |
Allan Stephens | 7ef43eb | 2008-05-12 15:42:28 -0700 | [diff] [blame] | 230 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 231 | if (sock->state == SS_READY) { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 232 | tipc_port_set_unreturnable(port, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 233 | if (sock->type == SOCK_DGRAM) |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 234 | tipc_port_set_unreliable(port, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 235 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /** |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 240 | * tipc_sock_create_local - create TIPC socket from inside TIPC module |
| 241 | * @type: socket type - SOCK_RDM or SOCK_SEQPACKET |
| 242 | * |
| 243 | * We cannot use sock_creat_kern here because it bumps module user count. |
| 244 | * Since socket owner and creator is the same module we must make sure |
| 245 | * that module count remains zero for module local sockets, otherwise |
| 246 | * we cannot do rmmod. |
| 247 | * |
| 248 | * Returns 0 on success, errno otherwise |
| 249 | */ |
| 250 | int tipc_sock_create_local(int type, struct socket **res) |
| 251 | { |
| 252 | int rc; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 253 | |
| 254 | rc = sock_create_lite(AF_TIPC, type, 0, res); |
| 255 | if (rc < 0) { |
| 256 | pr_err("Failed to create kernel socket\n"); |
| 257 | return rc; |
| 258 | } |
| 259 | tipc_sk_create(&init_net, *res, 0, 1); |
| 260 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * tipc_sock_release_local - release socket created by tipc_sock_create_local |
| 266 | * @sock: the socket to be released. |
| 267 | * |
| 268 | * Module reference count is not incremented when such sockets are created, |
| 269 | * so we must keep it from being decremented when they are released. |
| 270 | */ |
| 271 | void tipc_sock_release_local(struct socket *sock) |
| 272 | { |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 273 | tipc_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 274 | sock->ops = NULL; |
| 275 | sock_release(sock); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * tipc_sock_accept_local - accept a connection on a socket created |
| 280 | * with tipc_sock_create_local. Use this function to avoid that |
| 281 | * module reference count is inadvertently incremented. |
| 282 | * |
| 283 | * @sock: the accepting socket |
| 284 | * @newsock: reference to the new socket to be created |
| 285 | * @flags: socket flags |
| 286 | */ |
| 287 | |
| 288 | int tipc_sock_accept_local(struct socket *sock, struct socket **newsock, |
Paul Gortmaker | ae8509c | 2013-06-17 10:54:47 -0400 | [diff] [blame] | 289 | int flags) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 290 | { |
| 291 | struct sock *sk = sock->sk; |
| 292 | int ret; |
| 293 | |
| 294 | ret = sock_create_lite(sk->sk_family, sk->sk_type, |
| 295 | sk->sk_protocol, newsock); |
| 296 | if (ret < 0) |
| 297 | return ret; |
| 298 | |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 299 | ret = tipc_accept(sock, *newsock, flags); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 300 | if (ret < 0) { |
| 301 | sock_release(*newsock); |
| 302 | return ret; |
| 303 | } |
| 304 | (*newsock)->ops = sock->ops; |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 309 | * tipc_release - destroy a TIPC socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 310 | * @sock: socket to destroy |
| 311 | * |
| 312 | * This routine cleans up any messages that are still queued on the socket. |
| 313 | * For DGRAM and RDM socket types, all queued messages are rejected. |
| 314 | * For SEQPACKET and STREAM socket types, the first message is rejected |
| 315 | * and any others are discarded. (If the first message on a STREAM socket |
| 316 | * is partially-read, it is discarded and the next one is rejected instead.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 317 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 318 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
| 319 | * are returned or discarded according to the "destination droppable" setting |
| 320 | * specified for the message by the sender. |
| 321 | * |
| 322 | * Returns 0 on success, errno otherwise |
| 323 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 324 | static int tipc_release(struct socket *sock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 325 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 326 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 327 | struct tipc_sock *tsk; |
| 328 | struct tipc_port *port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 329 | struct sk_buff *buf; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 330 | u32 dnode; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 331 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Exit if socket isn't fully initialized (occurs when a failed accept() |
| 334 | * releases a pre-allocated child socket that was never used) |
| 335 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 336 | if (sk == NULL) |
| 337 | return 0; |
| 338 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 339 | tsk = tipc_sk(sk); |
| 340 | port = &tsk->port; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 341 | lock_sock(sk); |
| 342 | |
| 343 | /* |
| 344 | * Reject all unreceived messages, except on an active connection |
| 345 | * (which disconnects locally & sends a 'FIN+' to peer) |
| 346 | */ |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 347 | dnode = tipc_port_peernode(port); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 348 | while (sock->state != SS_DISCONNECTING) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 349 | buf = __skb_dequeue(&sk->sk_receive_queue); |
| 350 | if (buf == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 351 | break; |
Ying Xue | 4068243 | 2013-10-18 07:23:16 +0200 | [diff] [blame] | 352 | if (TIPC_SKB_CB(buf)->handle != NULL) |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 353 | kfree_skb(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 354 | else { |
| 355 | if ((sock->state == SS_CONNECTING) || |
| 356 | (sock->state == SS_CONNECTED)) { |
| 357 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 358 | port->connected = 0; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 359 | tipc_node_remove_conn(dnode, port->ref); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 360 | } |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 361 | if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT)) |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 362 | tipc_link_xmit(buf, dnode, 0); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 363 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame^] | 366 | tipc_withdraw(port, 0, NULL); |
| 367 | spin_lock_bh(port->lock); |
| 368 | tipc_ref_discard(port->ref); |
| 369 | spin_unlock_bh(port->lock); |
| 370 | k_cancel_timer(&port->timer); |
| 371 | if (port->connected) { |
| 372 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
| 373 | SHORT_H_SIZE, 0, dnode, tipc_own_addr, |
| 374 | tipc_port_peerport(port), |
| 375 | port->ref, TIPC_ERR_NO_PORT); |
| 376 | if (buf) |
| 377 | tipc_link_xmit(buf, dnode, port->ref); |
| 378 | tipc_node_remove_conn(dnode, port->ref); |
| 379 | } |
| 380 | spin_lock_bh(&tipc_port_list_lock); |
| 381 | list_del(&port->port_list); |
| 382 | spin_unlock_bh(&tipc_port_list_lock); |
| 383 | k_term_timer(&port->timer); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 384 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 385 | /* Discard any remaining (connection-based) messages in receive queue */ |
Ying Xue | 57467e5 | 2013-01-20 23:30:08 +0100 | [diff] [blame] | 386 | __skb_queue_purge(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 387 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 388 | /* Reject any messages that accumulated in backlog queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 389 | sock->state = SS_DISCONNECTING; |
| 390 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 391 | sock_put(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 392 | sock->sk = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 393 | |
Geert Uytterhoeven | 065d7e3 | 2014-04-06 15:56:14 +0200 | [diff] [blame] | 394 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 398 | * tipc_bind - associate or disassocate TIPC name(s) with a socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 399 | * @sock: socket structure |
| 400 | * @uaddr: socket address describing name(s) and desired operation |
| 401 | * @uaddr_len: size of socket address data structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 402 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 403 | * Name and name sequence binding is indicated using a positive scope value; |
| 404 | * a negative scope value unbinds the specified name. Specifying no name |
| 405 | * (i.e. a socket address length of 0) unbinds all names from the socket. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 406 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 407 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 408 | * |
| 409 | * NOTE: This routine doesn't need to take the socket lock since it doesn't |
| 410 | * access any non-constant socket information. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 411 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 412 | static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, |
| 413 | int uaddr_len) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 414 | { |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 415 | struct sock *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 416 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 417 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 418 | int res = -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 419 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 420 | lock_sock(sk); |
| 421 | if (unlikely(!uaddr_len)) { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 422 | res = tipc_withdraw(&tsk->port, 0, NULL); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 423 | goto exit; |
| 424 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 425 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 426 | if (uaddr_len < sizeof(struct sockaddr_tipc)) { |
| 427 | res = -EINVAL; |
| 428 | goto exit; |
| 429 | } |
| 430 | if (addr->family != AF_TIPC) { |
| 431 | res = -EAFNOSUPPORT; |
| 432 | goto exit; |
| 433 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 434 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 435 | if (addr->addrtype == TIPC_ADDR_NAME) |
| 436 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 437 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { |
| 438 | res = -EAFNOSUPPORT; |
| 439 | goto exit; |
| 440 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 441 | |
Ying Xue | 13a2e89 | 2013-06-17 10:54:40 -0400 | [diff] [blame] | 442 | if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && |
Ying Xue | 7d0ab17 | 2013-06-17 10:54:41 -0400 | [diff] [blame] | 443 | (addr->addr.nameseq.type != TIPC_TOP_SRV) && |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 444 | (addr->addr.nameseq.type != TIPC_CFG_SRV)) { |
| 445 | res = -EACCES; |
| 446 | goto exit; |
| 447 | } |
Allan Stephens | c422f1b | 2011-11-02 15:49:40 -0400 | [diff] [blame] | 448 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 449 | res = (addr->scope > 0) ? |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 450 | tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) : |
| 451 | tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 452 | exit: |
| 453 | release_sock(sk); |
| 454 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 455 | } |
| 456 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 457 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 458 | * tipc_getname - get port ID of socket or peer socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 459 | * @sock: socket structure |
| 460 | * @uaddr: area for returned socket address |
| 461 | * @uaddr_len: area for returned length of socket address |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 462 | * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 463 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 464 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 465 | * |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 466 | * NOTE: This routine doesn't need to take the socket lock since it only |
| 467 | * accesses socket information that is unchanging (or which changes in |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 468 | * a completely predictable manner). |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 469 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 470 | static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, |
| 471 | int *uaddr_len, int peer) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 472 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 473 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 474 | struct tipc_sock *tsk = tipc_sk(sock->sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 475 | |
Kulikov Vasiliy | 88f8a5e | 2010-10-31 07:10:32 +0000 | [diff] [blame] | 476 | memset(addr, 0, sizeof(*addr)); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 477 | if (peer) { |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 478 | if ((sock->state != SS_CONNECTED) && |
| 479 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) |
| 480 | return -ENOTCONN; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 481 | addr->addr.id.ref = tipc_port_peerport(&tsk->port); |
| 482 | addr->addr.id.node = tipc_port_peernode(&tsk->port); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 483 | } else { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 484 | addr->addr.id.ref = tsk->port.ref; |
Allan Stephens | b924dcf | 2010-11-30 12:01:03 +0000 | [diff] [blame] | 485 | addr->addr.id.node = tipc_own_addr; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 486 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 487 | |
| 488 | *uaddr_len = sizeof(*addr); |
| 489 | addr->addrtype = TIPC_ADDR_ID; |
| 490 | addr->family = AF_TIPC; |
| 491 | addr->scope = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 492 | addr->addr.name.domain = 0; |
| 493 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 494 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 498 | * tipc_poll - read and possibly block on pollmask |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 499 | * @file: file structure associated with the socket |
| 500 | * @sock: socket for which to calculate the poll bits |
| 501 | * @wait: ??? |
| 502 | * |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 503 | * Returns pollmask value |
| 504 | * |
| 505 | * COMMENTARY: |
| 506 | * It appears that the usual socket locking mechanisms are not useful here |
| 507 | * since the pollmask info is potentially out-of-date the moment this routine |
| 508 | * exits. TCP and other protocols seem to rely on higher level poll routines |
| 509 | * to handle any preventable race conditions, so TIPC will do the same ... |
| 510 | * |
| 511 | * TIPC sets the returned events as follows: |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 512 | * |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 513 | * socket state flags set |
| 514 | * ------------ --------- |
| 515 | * unconnected no read flags |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 516 | * POLLOUT if port is not congested |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 517 | * |
| 518 | * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue |
| 519 | * no write flags |
| 520 | * |
| 521 | * connected POLLIN/POLLRDNORM if data in rx queue |
| 522 | * POLLOUT if port is not congested |
| 523 | * |
| 524 | * disconnecting POLLIN/POLLRDNORM/POLLHUP |
| 525 | * no write flags |
| 526 | * |
| 527 | * listening POLLIN if SYN in rx queue |
| 528 | * no write flags |
| 529 | * |
| 530 | * ready POLLIN/POLLRDNORM if data in rx queue |
| 531 | * [connectionless] POLLOUT (since port cannot be congested) |
| 532 | * |
| 533 | * IMPORTANT: The fact that a read or write operation is indicated does NOT |
| 534 | * imply that the operation will succeed, merely that it should be performed |
| 535 | * and will not block. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 536 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 537 | static unsigned int tipc_poll(struct file *file, struct socket *sock, |
| 538 | poll_table *wait) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 539 | { |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 540 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 541 | struct tipc_sock *tsk = tipc_sk(sk); |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 542 | u32 mask = 0; |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 543 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 544 | sock_poll_wait(file, sk_sleep(sk), wait); |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 545 | |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 546 | switch ((int)sock->state) { |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 547 | case SS_UNCONNECTED: |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 548 | if (!tsk->link_cong) |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 549 | mask |= POLLOUT; |
| 550 | break; |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 551 | case SS_READY: |
| 552 | case SS_CONNECTED: |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 553 | if (!tsk->link_cong && !tipc_sk_conn_cong(tsk)) |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 554 | mask |= POLLOUT; |
| 555 | /* fall thru' */ |
| 556 | case SS_CONNECTING: |
| 557 | case SS_LISTENING: |
| 558 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 559 | mask |= (POLLIN | POLLRDNORM); |
| 560 | break; |
| 561 | case SS_DISCONNECTING: |
| 562 | mask = (POLLIN | POLLRDNORM | POLLHUP); |
| 563 | break; |
| 564 | } |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 565 | |
| 566 | return mask; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 569 | /** |
| 570 | * tipc_sendmcast - send multicast message |
| 571 | * @sock: socket structure |
| 572 | * @seq: destination address |
| 573 | * @iov: message data to send |
| 574 | * @dsz: total length of message data |
| 575 | * @timeo: timeout to wait for wakeup |
| 576 | * |
| 577 | * Called from function tipc_sendmsg(), which has done all sanity checks |
| 578 | * Returns the number of bytes sent on success, or errno |
| 579 | */ |
| 580 | static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, |
| 581 | struct iovec *iov, size_t dsz, long timeo) |
| 582 | { |
| 583 | struct sock *sk = sock->sk; |
| 584 | struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr; |
| 585 | struct sk_buff *buf; |
| 586 | uint mtu; |
| 587 | int rc; |
| 588 | |
| 589 | msg_set_type(mhdr, TIPC_MCAST_MSG); |
| 590 | msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE); |
| 591 | msg_set_destport(mhdr, 0); |
| 592 | msg_set_destnode(mhdr, 0); |
| 593 | msg_set_nametype(mhdr, seq->type); |
| 594 | msg_set_namelower(mhdr, seq->lower); |
| 595 | msg_set_nameupper(mhdr, seq->upper); |
| 596 | msg_set_hdr_sz(mhdr, MCAST_H_SIZE); |
| 597 | |
| 598 | new_mtu: |
| 599 | mtu = tipc_bclink_get_mtu(); |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 600 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 601 | if (unlikely(rc < 0)) |
| 602 | return rc; |
| 603 | |
| 604 | do { |
| 605 | rc = tipc_bclink_xmit(buf); |
| 606 | if (likely(rc >= 0)) { |
| 607 | rc = dsz; |
| 608 | break; |
| 609 | } |
| 610 | if (rc == -EMSGSIZE) |
| 611 | goto new_mtu; |
| 612 | if (rc != -ELINKCONG) |
| 613 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 614 | tipc_sk(sk)->link_cong = 1; |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 615 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
| 616 | if (rc) |
| 617 | kfree_skb_list(buf); |
| 618 | } while (!rc); |
| 619 | return rc; |
| 620 | } |
| 621 | |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 622 | /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets |
| 623 | */ |
| 624 | void tipc_sk_mcast_rcv(struct sk_buff *buf) |
| 625 | { |
| 626 | struct tipc_msg *msg = buf_msg(buf); |
| 627 | struct tipc_port_list dports = {0, NULL, }; |
| 628 | struct tipc_port_list *item; |
| 629 | struct sk_buff *b; |
| 630 | uint i, last, dst = 0; |
| 631 | u32 scope = TIPC_CLUSTER_SCOPE; |
| 632 | |
| 633 | if (in_own_node(msg_orignode(msg))) |
| 634 | scope = TIPC_NODE_SCOPE; |
| 635 | |
| 636 | /* Create destination port list: */ |
| 637 | tipc_nametbl_mc_translate(msg_nametype(msg), |
| 638 | msg_namelower(msg), |
| 639 | msg_nameupper(msg), |
| 640 | scope, |
| 641 | &dports); |
| 642 | last = dports.count; |
| 643 | if (!last) { |
| 644 | kfree_skb(buf); |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | for (item = &dports; item; item = item->next) { |
| 649 | for (i = 0; i < PLSIZE && ++dst <= last; i++) { |
| 650 | b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf; |
| 651 | if (!b) { |
| 652 | pr_warn("Failed do clone mcast rcv buffer\n"); |
| 653 | continue; |
| 654 | } |
| 655 | msg_set_destport(msg, item->ports[i]); |
| 656 | tipc_sk_rcv(b); |
| 657 | } |
| 658 | } |
| 659 | tipc_port_list_free(&dports); |
| 660 | } |
| 661 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 662 | /** |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 663 | * tipc_sk_proto_rcv - receive a connection mng protocol message |
| 664 | * @tsk: receiving socket |
| 665 | * @dnode: node to send response message to, if any |
| 666 | * @buf: buffer containing protocol message |
| 667 | * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if |
| 668 | * (CONN_PROBE_REPLY) message should be forwarded. |
| 669 | */ |
Wei Yongjun | 52f50ce | 2014-07-20 13:14:28 +0800 | [diff] [blame] | 670 | static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, |
| 671 | struct sk_buff *buf) |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 672 | { |
| 673 | struct tipc_msg *msg = buf_msg(buf); |
| 674 | struct tipc_port *port = &tsk->port; |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 675 | int conn_cong; |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 676 | |
| 677 | /* Ignore if connection cannot be validated: */ |
| 678 | if (!port->connected || !tipc_port_peer_msg(port, msg)) |
| 679 | goto exit; |
| 680 | |
| 681 | port->probing_state = TIPC_CONN_OK; |
| 682 | |
| 683 | if (msg_type(msg) == CONN_ACK) { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 684 | conn_cong = tipc_sk_conn_cong(tsk); |
| 685 | tsk->sent_unacked -= msg_msgcnt(msg); |
| 686 | if (conn_cong) |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 687 | tsk->sk.sk_write_space(&tsk->sk); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 688 | } else if (msg_type(msg) == CONN_PROBE) { |
| 689 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) |
| 690 | return TIPC_OK; |
| 691 | msg_set_type(msg, CONN_PROBE_REPLY); |
| 692 | return TIPC_FWD_MSG; |
| 693 | } |
| 694 | /* Do nothing if msg_type() == CONN_PROBE_REPLY */ |
| 695 | exit: |
| 696 | kfree_skb(buf); |
| 697 | return TIPC_OK; |
| 698 | } |
| 699 | |
| 700 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 701 | * dest_name_check - verify user is permitted to send to specified port name |
| 702 | * @dest: destination address |
| 703 | * @m: descriptor for message to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 704 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 705 | * Prevents restricted configuration commands from being issued by |
| 706 | * unauthorized users. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 707 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 708 | * Returns 0 if permission is granted, otherwise errno |
| 709 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 710 | static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 711 | { |
| 712 | struct tipc_cfg_msg_hdr hdr; |
| 713 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 714 | if (unlikely(dest->addrtype == TIPC_ADDR_ID)) |
| 715 | return 0; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 716 | if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) |
| 717 | return 0; |
| 718 | if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) |
| 719 | return 0; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 720 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
| 721 | return -EACCES; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 722 | |
Allan Stephens | 3f8dd94 | 2011-01-18 13:09:29 -0500 | [diff] [blame] | 723 | if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr))) |
| 724 | return -EMSGSIZE; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 725 | if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 726 | return -EFAULT; |
Allan Stephens | 70cb234 | 2006-06-25 23:41:47 -0700 | [diff] [blame] | 727 | if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 728 | return -EACCES; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 729 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 732 | |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 733 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) |
| 734 | { |
| 735 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 736 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 737 | DEFINE_WAIT(wait); |
| 738 | int done; |
| 739 | |
| 740 | do { |
| 741 | int err = sock_error(sk); |
| 742 | if (err) |
| 743 | return err; |
| 744 | if (sock->state == SS_DISCONNECTING) |
| 745 | return -EPIPE; |
| 746 | if (!*timeo_p) |
| 747 | return -EAGAIN; |
| 748 | if (signal_pending(current)) |
| 749 | return sock_intr_errno(*timeo_p); |
| 750 | |
| 751 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 752 | done = sk_wait_event(sk, timeo_p, !tsk->link_cong); |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 753 | finish_wait(sk_sleep(sk), &wait); |
| 754 | } while (!done); |
| 755 | return 0; |
| 756 | } |
| 757 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 758 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 759 | * tipc_sendmsg - send message in connectionless manner |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 760 | * @iocb: if NULL, indicates that socket lock is already held |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 761 | * @sock: socket structure |
| 762 | * @m: message to send |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 763 | * @dsz: amount of user data to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 764 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 765 | * Message must have an destination specified explicitly. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 766 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 767 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 768 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 769 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 770 | * Returns the number of bytes sent on success, or errno otherwise |
| 771 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 772 | static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 773 | struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 774 | { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 775 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 776 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 777 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 5c31142 | 2014-03-12 11:31:13 -0400 | [diff] [blame] | 778 | struct tipc_port *port = &tsk->port; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 779 | struct tipc_msg *mhdr = &port->phdr; |
| 780 | struct iovec *iov = m->msg_iov; |
| 781 | u32 dnode, dport; |
| 782 | struct sk_buff *buf; |
| 783 | struct tipc_name_seq *seq = &dest->addr.nameseq; |
| 784 | u32 mtu; |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 785 | long timeo; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 786 | int rc = -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 787 | |
| 788 | if (unlikely(!dest)) |
| 789 | return -EDESTADDRREQ; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 790 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 791 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
| 792 | (dest->family != AF_TIPC))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 793 | return -EINVAL; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 794 | |
| 795 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 796 | return -EMSGSIZE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 797 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 798 | if (iocb) |
| 799 | lock_sock(sk); |
| 800 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 801 | if (unlikely(sock->state != SS_READY)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 802 | if (sock->state == SS_LISTENING) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 803 | rc = -EPIPE; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 804 | goto exit; |
Allan Stephens | 3388007 | 2006-06-25 23:44:57 -0700 | [diff] [blame] | 805 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 806 | if (sock->state != SS_UNCONNECTED) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 807 | rc = -EISCONN; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 808 | goto exit; |
| 809 | } |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 810 | if (tsk->port.published) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 811 | rc = -EOPNOTSUPP; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 812 | goto exit; |
| 813 | } |
| 814 | if (dest->addrtype == TIPC_ADDR_NAME) { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 815 | tsk->port.conn_type = dest->addr.name.name.type; |
| 816 | tsk->port.conn_instance = dest->addr.name.name.instance; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 817 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 818 | } |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 819 | rc = dest_name_check(dest, m); |
| 820 | if (rc) |
| 821 | goto exit; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 822 | |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 823 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 824 | |
| 825 | if (dest->addrtype == TIPC_ADDR_MCAST) { |
| 826 | rc = tipc_sendmcast(sock, seq, iov, dsz, timeo); |
| 827 | goto exit; |
| 828 | } else if (dest->addrtype == TIPC_ADDR_NAME) { |
| 829 | u32 type = dest->addr.name.name.type; |
| 830 | u32 inst = dest->addr.name.name.instance; |
| 831 | u32 domain = dest->addr.name.domain; |
| 832 | |
| 833 | dnode = domain; |
| 834 | msg_set_type(mhdr, TIPC_NAMED_MSG); |
| 835 | msg_set_hdr_sz(mhdr, NAMED_H_SIZE); |
| 836 | msg_set_nametype(mhdr, type); |
| 837 | msg_set_nameinst(mhdr, inst); |
| 838 | msg_set_lookup_scope(mhdr, tipc_addr_scope(domain)); |
| 839 | dport = tipc_nametbl_translate(type, inst, &dnode); |
| 840 | msg_set_destnode(mhdr, dnode); |
| 841 | msg_set_destport(mhdr, dport); |
| 842 | if (unlikely(!dport && !dnode)) { |
| 843 | rc = -EHOSTUNREACH; |
| 844 | goto exit; |
| 845 | } |
| 846 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
| 847 | dnode = dest->addr.id.node; |
| 848 | msg_set_type(mhdr, TIPC_DIRECT_MSG); |
| 849 | msg_set_lookup_scope(mhdr, 0); |
| 850 | msg_set_destnode(mhdr, dnode); |
| 851 | msg_set_destport(mhdr, dest->addr.id.ref); |
| 852 | msg_set_hdr_sz(mhdr, BASIC_H_SIZE); |
| 853 | } |
| 854 | |
| 855 | new_mtu: |
| 856 | mtu = tipc_node_get_mtu(dnode, tsk->port.ref); |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 857 | rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 858 | if (rc < 0) |
| 859 | goto exit; |
| 860 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 861 | do { |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 862 | TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong; |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 863 | rc = tipc_link_xmit(buf, dnode, tsk->port.ref); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 864 | if (likely(rc >= 0)) { |
| 865 | if (sock->state != SS_READY) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 866 | sock->state = SS_CONNECTING; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 867 | rc = dsz; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 868 | break; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 869 | } |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 870 | if (rc == -EMSGSIZE) |
| 871 | goto new_mtu; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 872 | if (rc != -ELINKCONG) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 873 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 874 | tsk->link_cong = 1; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 875 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
Erik Hugne | 70452dc | 2014-07-06 20:38:50 -0400 | [diff] [blame] | 876 | if (rc) |
| 877 | kfree_skb_list(buf); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 878 | } while (!rc); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 879 | exit: |
| 880 | if (iocb) |
| 881 | release_sock(sk); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 882 | |
| 883 | return rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 886 | static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) |
| 887 | { |
| 888 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 889 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 890 | DEFINE_WAIT(wait); |
| 891 | int done; |
| 892 | |
| 893 | do { |
| 894 | int err = sock_error(sk); |
| 895 | if (err) |
| 896 | return err; |
| 897 | if (sock->state == SS_DISCONNECTING) |
| 898 | return -EPIPE; |
| 899 | else if (sock->state != SS_CONNECTED) |
| 900 | return -ENOTCONN; |
| 901 | if (!*timeo_p) |
| 902 | return -EAGAIN; |
| 903 | if (signal_pending(current)) |
| 904 | return sock_intr_errno(*timeo_p); |
| 905 | |
| 906 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 907 | done = sk_wait_event(sk, timeo_p, |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 908 | (!tsk->link_cong && |
| 909 | !tipc_sk_conn_cong(tsk)) || |
| 910 | !tsk->port.connected); |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 911 | finish_wait(sk_sleep(sk), &wait); |
| 912 | } while (!done); |
| 913 | return 0; |
| 914 | } |
| 915 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 916 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 917 | * tipc_send_stream - send stream-oriented data |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 918 | * @iocb: (unused) |
| 919 | * @sock: socket structure |
| 920 | * @m: data to send |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 921 | * @dsz: total length of data to be transmitted |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 922 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 923 | * Used for SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 924 | * |
| 925 | * Returns the number of bytes sent on success (or partial success), |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 926 | * or errno if no data sent |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 927 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 928 | static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 929 | struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 930 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 931 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 932 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 933 | struct tipc_port *port = &tsk->port; |
| 934 | struct tipc_msg *mhdr = &port->phdr; |
| 935 | struct sk_buff *buf; |
| 936 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
| 937 | u32 ref = port->ref; |
| 938 | int rc = -EINVAL; |
| 939 | long timeo; |
| 940 | u32 dnode; |
| 941 | uint mtu, send, sent = 0; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 942 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 943 | /* Handle implied connection establishment */ |
| 944 | if (unlikely(dest)) { |
| 945 | rc = tipc_sendmsg(iocb, sock, m, dsz); |
| 946 | if (dsz && (dsz == rc)) |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 947 | tsk->sent_unacked = 1; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 948 | return rc; |
| 949 | } |
| 950 | if (dsz > (uint)INT_MAX) |
| 951 | return -EMSGSIZE; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 952 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 953 | if (iocb) |
| 954 | lock_sock(sk); |
| 955 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 956 | if (unlikely(sock->state != SS_CONNECTED)) { |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 957 | if (sock->state == SS_DISCONNECTING) |
| 958 | rc = -EPIPE; |
wangweidong | b055597 | 2013-12-27 10:09:39 +0800 | [diff] [blame] | 959 | else |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 960 | rc = -ENOTCONN; |
wangweidong | 3b8401f | 2013-12-12 09:36:40 +0800 | [diff] [blame] | 961 | goto exit; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 962 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 963 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 964 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
| 965 | dnode = tipc_port_peernode(port); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 966 | |
| 967 | next: |
| 968 | mtu = port->max_pkt; |
| 969 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 970 | rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 971 | if (unlikely(rc < 0)) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 972 | goto exit; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 973 | do { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 974 | if (likely(!tipc_sk_conn_cong(tsk))) { |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 975 | rc = tipc_link_xmit(buf, dnode, ref); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 976 | if (likely(!rc)) { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 977 | tsk->sent_unacked++; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 978 | sent += send; |
| 979 | if (sent == dsz) |
| 980 | break; |
| 981 | goto next; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 982 | } |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 983 | if (rc == -EMSGSIZE) { |
| 984 | port->max_pkt = tipc_node_get_mtu(dnode, ref); |
| 985 | goto next; |
| 986 | } |
| 987 | if (rc != -ELINKCONG) |
| 988 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 989 | tsk->link_cong = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 990 | } |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 991 | rc = tipc_wait_for_sndpkt(sock, &timeo); |
Erik Hugne | 70452dc | 2014-07-06 20:38:50 -0400 | [diff] [blame] | 992 | if (rc) |
| 993 | kfree_skb_list(buf); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 994 | } while (!rc); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 995 | exit: |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 996 | if (iocb) |
| 997 | release_sock(sk); |
| 998 | return sent ? sent : rc; |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * tipc_send_packet - send a connection-oriented message |
| 1003 | * @iocb: if NULL, indicates that socket lock is already held |
| 1004 | * @sock: socket structure |
| 1005 | * @m: message to send |
| 1006 | * @dsz: length of data to be transmitted |
| 1007 | * |
| 1008 | * Used for SOCK_SEQPACKET messages. |
| 1009 | * |
| 1010 | * Returns the number of bytes sent on success, or errno otherwise |
| 1011 | */ |
| 1012 | static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, |
| 1013 | struct msghdr *m, size_t dsz) |
| 1014 | { |
| 1015 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
| 1016 | return -EMSGSIZE; |
| 1017 | |
| 1018 | return tipc_send_stream(iocb, sock, m, dsz); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1021 | /* tipc_sk_finish_conn - complete the setup of a connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1022 | */ |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1023 | static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port, |
| 1024 | u32 peer_node) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1025 | { |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1026 | struct tipc_msg *msg = &port->phdr; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1027 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1028 | msg_set_destnode(msg, peer_node); |
| 1029 | msg_set_destport(msg, peer_port); |
| 1030 | msg_set_type(msg, TIPC_CONN_MSG); |
| 1031 | msg_set_lookup_scope(msg, 0); |
| 1032 | msg_set_hdr_sz(msg, SHORT_H_SIZE); |
Jon Paul Maloy | f9fef18 | 2014-03-12 11:31:08 -0400 | [diff] [blame] | 1033 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1034 | port->probing_interval = CONN_PROBING_INTERVAL; |
| 1035 | port->probing_state = TIPC_CONN_OK; |
| 1036 | port->connected = 1; |
| 1037 | k_start_timer(&port->timer, port->probing_interval); |
| 1038 | tipc_node_add_conn(peer_node, port->ref, peer_port); |
| 1039 | port->max_pkt = tipc_node_get_mtu(peer_node, port->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * set_orig_addr - capture sender's address for received message |
| 1044 | * @m: descriptor for message info |
| 1045 | * @msg: received message header |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1046 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1047 | * Note: Address is not captured if not requested by receiver. |
| 1048 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 1049 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1050 | { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1051 | DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1052 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1053 | if (addr) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1054 | addr->family = AF_TIPC; |
| 1055 | addr->addrtype = TIPC_ADDR_ID; |
Mathias Krause | 60085c3 | 2013-04-07 01:52:00 +0000 | [diff] [blame] | 1056 | memset(&addr->addr, 0, sizeof(addr->addr)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1057 | addr->addr.id.ref = msg_origport(msg); |
| 1058 | addr->addr.id.node = msg_orignode(msg); |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1059 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
| 1060 | addr->scope = 0; /* could leave uninitialized */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1061 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /** |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1066 | * anc_data_recv - optionally capture ancillary data for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1067 | * @m: descriptor for message info |
| 1068 | * @msg: received message header |
| 1069 | * @tport: TIPC port associated with message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1070 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1071 | * Note: Ancillary data is not captured if not requested by receiver. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1072 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1073 | * Returns 0 if successful, otherwise errno |
| 1074 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 1075 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
Paul Gortmaker | ae8509c | 2013-06-17 10:54:47 -0400 | [diff] [blame] | 1076 | struct tipc_port *tport) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1077 | { |
| 1078 | u32 anc_data[3]; |
| 1079 | u32 err; |
| 1080 | u32 dest_type; |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1081 | int has_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1082 | int res; |
| 1083 | |
| 1084 | if (likely(m->msg_controllen == 0)) |
| 1085 | return 0; |
| 1086 | |
| 1087 | /* Optionally capture errored message object(s) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1088 | err = msg ? msg_errcode(msg) : 0; |
| 1089 | if (unlikely(err)) { |
| 1090 | anc_data[0] = err; |
| 1091 | anc_data[1] = msg_data_sz(msg); |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1092 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
| 1093 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1094 | return res; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1095 | if (anc_data[1]) { |
| 1096 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], |
| 1097 | msg_data(msg)); |
| 1098 | if (res) |
| 1099 | return res; |
| 1100 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* Optionally capture message destination object */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1104 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
| 1105 | switch (dest_type) { |
| 1106 | case TIPC_NAMED_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1107 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1108 | anc_data[0] = msg_nametype(msg); |
| 1109 | anc_data[1] = msg_namelower(msg); |
| 1110 | anc_data[2] = msg_namelower(msg); |
| 1111 | break; |
| 1112 | case TIPC_MCAST_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1113 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1114 | anc_data[0] = msg_nametype(msg); |
| 1115 | anc_data[1] = msg_namelower(msg); |
| 1116 | anc_data[2] = msg_nameupper(msg); |
| 1117 | break; |
| 1118 | case TIPC_CONN_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1119 | has_name = (tport->conn_type != 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1120 | anc_data[0] = tport->conn_type; |
| 1121 | anc_data[1] = tport->conn_instance; |
| 1122 | anc_data[2] = tport->conn_instance; |
| 1123 | break; |
| 1124 | default: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1125 | has_name = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1126 | } |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1127 | if (has_name) { |
| 1128 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); |
| 1129 | if (res) |
| 1130 | return res; |
| 1131 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1136 | static void tipc_sk_send_ack(struct tipc_port *port, uint ack) |
| 1137 | { |
| 1138 | struct sk_buff *buf = NULL; |
| 1139 | struct tipc_msg *msg; |
| 1140 | u32 peer_port = tipc_port_peerport(port); |
| 1141 | u32 dnode = tipc_port_peernode(port); |
| 1142 | |
| 1143 | if (!port->connected) |
| 1144 | return; |
| 1145 | buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode, |
| 1146 | tipc_own_addr, peer_port, port->ref, TIPC_OK); |
| 1147 | if (!buf) |
| 1148 | return; |
| 1149 | msg = buf_msg(buf); |
| 1150 | msg_set_msgcnt(msg, ack); |
| 1151 | tipc_link_xmit(buf, dnode, msg_link_selector(msg)); |
| 1152 | } |
| 1153 | |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1154 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1155 | { |
| 1156 | struct sock *sk = sock->sk; |
| 1157 | DEFINE_WAIT(wait); |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1158 | long timeo = *timeop; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1159 | int err; |
| 1160 | |
| 1161 | for (;;) { |
| 1162 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 1163 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1164 | if (sock->state == SS_DISCONNECTING) { |
| 1165 | err = -ENOTCONN; |
| 1166 | break; |
| 1167 | } |
| 1168 | release_sock(sk); |
| 1169 | timeo = schedule_timeout(timeo); |
| 1170 | lock_sock(sk); |
| 1171 | } |
| 1172 | err = 0; |
| 1173 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 1174 | break; |
| 1175 | err = sock_intr_errno(timeo); |
| 1176 | if (signal_pending(current)) |
| 1177 | break; |
| 1178 | err = -EAGAIN; |
| 1179 | if (!timeo) |
| 1180 | break; |
| 1181 | } |
| 1182 | finish_wait(sk_sleep(sk), &wait); |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1183 | *timeop = timeo; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1184 | return err; |
| 1185 | } |
| 1186 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1187 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1188 | * tipc_recvmsg - receive packet-oriented message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1189 | * @iocb: (unused) |
| 1190 | * @m: descriptor for message info |
| 1191 | * @buf_len: total size of user buffer area |
| 1192 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1193 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1194 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 1195 | * If the complete message doesn't fit in user area, truncate it. |
| 1196 | * |
| 1197 | * Returns size of returned message data, errno otherwise |
| 1198 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1199 | static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1200 | struct msghdr *m, size_t buf_len, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1201 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1202 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1203 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1204 | struct tipc_port *port = &tsk->port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1205 | struct sk_buff *buf; |
| 1206 | struct tipc_msg *msg; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1207 | long timeo; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1208 | unsigned int sz; |
| 1209 | u32 err; |
| 1210 | int res; |
| 1211 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1212 | /* Catch invalid receive requests */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1213 | if (unlikely(!buf_len)) |
| 1214 | return -EINVAL; |
| 1215 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1216 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1217 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1218 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1219 | res = -ENOTCONN; |
| 1220 | goto exit; |
| 1221 | } |
| 1222 | |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1223 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1224 | restart: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1225 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1226 | /* Look for a message in receive queue; wait if necessary */ |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1227 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1228 | if (res) |
| 1229 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1230 | |
| 1231 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1232 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1233 | msg = buf_msg(buf); |
| 1234 | sz = msg_data_sz(msg); |
| 1235 | err = msg_errcode(msg); |
| 1236 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1237 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1238 | if ((!sz) && (!err)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1239 | advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1240 | goto restart; |
| 1241 | } |
| 1242 | |
| 1243 | /* Capture sender's address (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1244 | set_orig_addr(m, msg); |
| 1245 | |
| 1246 | /* Capture ancillary data (optional) */ |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1247 | res = anc_data_recv(m, msg, port); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1248 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1249 | goto exit; |
| 1250 | |
| 1251 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1252 | if (!err) { |
| 1253 | if (unlikely(buf_len < sz)) { |
| 1254 | sz = buf_len; |
| 1255 | m->msg_flags |= MSG_TRUNC; |
| 1256 | } |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1257 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg), |
| 1258 | m->msg_iov, sz); |
| 1259 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1260 | goto exit; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1261 | res = sz; |
| 1262 | } else { |
| 1263 | if ((sock->state == SS_READY) || |
| 1264 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) |
| 1265 | res = 0; |
| 1266 | else |
| 1267 | res = -ECONNRESET; |
| 1268 | } |
| 1269 | |
| 1270 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1271 | if (likely(!(flags & MSG_PEEK))) { |
Allan Stephens | 99009806 | 2008-04-15 00:06:12 -0700 | [diff] [blame] | 1272 | if ((sock->state != SS_READY) && |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1273 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1274 | tipc_sk_send_ack(port, tsk->rcv_unacked); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1275 | tsk->rcv_unacked = 0; |
| 1276 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1277 | advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1278 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1279 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1280 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1281 | return res; |
| 1282 | } |
| 1283 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1284 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1285 | * tipc_recv_stream - receive stream-oriented data |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1286 | * @iocb: (unused) |
| 1287 | * @m: descriptor for message info |
| 1288 | * @buf_len: total size of user buffer area |
| 1289 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1290 | * |
| 1291 | * Used for SOCK_STREAM messages only. If not enough data is available |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1292 | * will optionally wait for more; never truncates data. |
| 1293 | * |
| 1294 | * Returns size of returned message data, errno otherwise |
| 1295 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1296 | static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, |
| 1297 | struct msghdr *m, size_t buf_len, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1298 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1299 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1300 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1301 | struct tipc_port *port = &tsk->port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1302 | struct sk_buff *buf; |
| 1303 | struct tipc_msg *msg; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1304 | long timeo; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1305 | unsigned int sz; |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1306 | int sz_to_copy, target, needed; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1307 | int sz_copied = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1308 | u32 err; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1309 | int res = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1310 | |
| 1311 | /* Catch invalid receive attempts */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1312 | if (unlikely(!buf_len)) |
| 1313 | return -EINVAL; |
| 1314 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1315 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1316 | |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1317 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1318 | res = -ENOTCONN; |
| 1319 | goto exit; |
| 1320 | } |
| 1321 | |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1322 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1323 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 1324 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1325 | restart: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1326 | /* Look for a message in receive queue; wait if necessary */ |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1327 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1328 | if (res) |
| 1329 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1330 | |
| 1331 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1332 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1333 | msg = buf_msg(buf); |
| 1334 | sz = msg_data_sz(msg); |
| 1335 | err = msg_errcode(msg); |
| 1336 | |
| 1337 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1338 | if ((!sz) && (!err)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1339 | advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1340 | goto restart; |
| 1341 | } |
| 1342 | |
| 1343 | /* Optionally capture sender's address & ancillary data of first msg */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1344 | if (sz_copied == 0) { |
| 1345 | set_orig_addr(m, msg); |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1346 | res = anc_data_recv(m, msg, port); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1347 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1348 | goto exit; |
| 1349 | } |
| 1350 | |
| 1351 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1352 | if (!err) { |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1353 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1354 | |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1355 | sz -= offset; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1356 | needed = (buf_len - sz_copied); |
| 1357 | sz_to_copy = (sz <= needed) ? sz : needed; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1358 | |
| 1359 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset, |
| 1360 | m->msg_iov, sz_to_copy); |
| 1361 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1362 | goto exit; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1363 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1364 | sz_copied += sz_to_copy; |
| 1365 | |
| 1366 | if (sz_to_copy < sz) { |
| 1367 | if (!(flags & MSG_PEEK)) |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1368 | TIPC_SKB_CB(buf)->handle = |
| 1369 | (void *)(unsigned long)(offset + sz_to_copy); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1370 | goto exit; |
| 1371 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1372 | } else { |
| 1373 | if (sz_copied != 0) |
| 1374 | goto exit; /* can't add error msg to valid data */ |
| 1375 | |
| 1376 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) |
| 1377 | res = 0; |
| 1378 | else |
| 1379 | res = -ECONNRESET; |
| 1380 | } |
| 1381 | |
| 1382 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1383 | if (likely(!(flags & MSG_PEEK))) { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1384 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1385 | tipc_sk_send_ack(port, tsk->rcv_unacked); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1386 | tsk->rcv_unacked = 0; |
| 1387 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1388 | advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1389 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* Loop around if more data is required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1392 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
| 1393 | (!skb_queue_empty(&sk->sk_receive_queue) || |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1394 | (sz_copied < target)) && /* and more is ready or required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1395 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
| 1396 | (!err)) /* and haven't reached a FIN */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1397 | goto restart; |
| 1398 | |
| 1399 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1400 | release_sock(sk); |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 1401 | return sz_copied ? sz_copied : res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /** |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1405 | * tipc_write_space - wake up thread if port congestion is released |
| 1406 | * @sk: socket |
| 1407 | */ |
| 1408 | static void tipc_write_space(struct sock *sk) |
| 1409 | { |
| 1410 | struct socket_wq *wq; |
| 1411 | |
| 1412 | rcu_read_lock(); |
| 1413 | wq = rcu_dereference(sk->sk_wq); |
| 1414 | if (wq_has_sleeper(wq)) |
| 1415 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | |
| 1416 | POLLWRNORM | POLLWRBAND); |
| 1417 | rcu_read_unlock(); |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * tipc_data_ready - wake up threads to indicate messages have been received |
| 1422 | * @sk: socket |
| 1423 | * @len: the length of messages |
| 1424 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1425 | static void tipc_data_ready(struct sock *sk) |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1426 | { |
| 1427 | struct socket_wq *wq; |
| 1428 | |
| 1429 | rcu_read_lock(); |
| 1430 | wq = rcu_dereference(sk->sk_wq); |
| 1431 | if (wq_has_sleeper(wq)) |
| 1432 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
| 1433 | POLLRDNORM | POLLRDBAND); |
| 1434 | rcu_read_unlock(); |
| 1435 | } |
| 1436 | |
| 1437 | /** |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1438 | * filter_connect - Handle all incoming messages for a connection-based socket |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1439 | * @tsk: TIPC socket |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1440 | * @msg: message |
| 1441 | * |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1442 | * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1443 | */ |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1444 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1445 | { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1446 | struct sock *sk = &tsk->sk; |
| 1447 | struct tipc_port *port = &tsk->port; |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1448 | struct socket *sock = sk->sk_socket; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1449 | struct tipc_msg *msg = buf_msg(*buf); |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1450 | |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1451 | int retval = -TIPC_ERR_NO_PORT; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1452 | |
| 1453 | if (msg_mcast(msg)) |
| 1454 | return retval; |
| 1455 | |
| 1456 | switch ((int)sock->state) { |
| 1457 | case SS_CONNECTED: |
| 1458 | /* Accept only connection-based messages sent by peer */ |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1459 | if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) { |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1460 | if (unlikely(msg_errcode(msg))) { |
| 1461 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1462 | port->connected = 0; |
| 1463 | /* let timer expire on it's own */ |
| 1464 | tipc_node_remove_conn(tipc_port_peernode(port), |
| 1465 | port->ref); |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1466 | } |
| 1467 | retval = TIPC_OK; |
| 1468 | } |
| 1469 | break; |
| 1470 | case SS_CONNECTING: |
| 1471 | /* Accept only ACK or NACK message */ |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1472 | |
| 1473 | if (unlikely(!msg_connected(msg))) |
| 1474 | break; |
| 1475 | |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1476 | if (unlikely(msg_errcode(msg))) { |
| 1477 | sock->state = SS_DISCONNECTING; |
Erik Hugne | 2c8d851 | 2013-08-28 09:29:58 +0200 | [diff] [blame] | 1478 | sk->sk_err = ECONNREFUSED; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1479 | retval = TIPC_OK; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1480 | break; |
| 1481 | } |
| 1482 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1483 | if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) { |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1484 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1485 | sk->sk_err = EINVAL; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1486 | retval = TIPC_OK; |
| 1487 | break; |
| 1488 | } |
| 1489 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1490 | tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg)); |
| 1491 | msg_set_importance(&port->phdr, msg_importance(msg)); |
| 1492 | sock->state = SS_CONNECTED; |
| 1493 | |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1494 | /* If an incoming message is an 'ACK-', it should be |
| 1495 | * discarded here because it doesn't contain useful |
| 1496 | * data. In addition, we should try to wake up |
| 1497 | * connect() routine if sleeping. |
| 1498 | */ |
| 1499 | if (msg_data_sz(msg) == 0) { |
| 1500 | kfree_skb(*buf); |
| 1501 | *buf = NULL; |
| 1502 | if (waitqueue_active(sk_sleep(sk))) |
| 1503 | wake_up_interruptible(sk_sleep(sk)); |
| 1504 | } |
| 1505 | retval = TIPC_OK; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1506 | break; |
| 1507 | case SS_LISTENING: |
| 1508 | case SS_UNCONNECTED: |
| 1509 | /* Accept only SYN message */ |
| 1510 | if (!msg_connected(msg) && !(msg_errcode(msg))) |
| 1511 | retval = TIPC_OK; |
| 1512 | break; |
| 1513 | case SS_DISCONNECTING: |
| 1514 | break; |
| 1515 | default: |
| 1516 | pr_err("Unknown socket state %u\n", sock->state); |
| 1517 | } |
| 1518 | return retval; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1522 | * rcvbuf_limit - get proper overload limit of socket receive queue |
| 1523 | * @sk: socket |
| 1524 | * @buf: message |
| 1525 | * |
| 1526 | * For all connection oriented messages, irrespective of importance, |
| 1527 | * the default overload value (i.e. 67MB) is set as limit. |
| 1528 | * |
| 1529 | * For all connectionless messages, by default new queue limits are |
| 1530 | * as belows: |
| 1531 | * |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 1532 | * TIPC_LOW_IMPORTANCE (4 MB) |
| 1533 | * TIPC_MEDIUM_IMPORTANCE (8 MB) |
| 1534 | * TIPC_HIGH_IMPORTANCE (16 MB) |
| 1535 | * TIPC_CRITICAL_IMPORTANCE (32 MB) |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1536 | * |
| 1537 | * Returns overload limit according to corresponding message importance |
| 1538 | */ |
| 1539 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf) |
| 1540 | { |
| 1541 | struct tipc_msg *msg = buf_msg(buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1542 | |
| 1543 | if (msg_connected(msg)) |
wangweidong | 0cee6bb | 2013-12-12 09:36:39 +0800 | [diff] [blame] | 1544 | return sysctl_tipc_rmem[2]; |
| 1545 | |
| 1546 | return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE << |
| 1547 | msg_importance(msg); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1551 | * filter_rcv - validate incoming message |
| 1552 | * @sk: socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1553 | * @buf: message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1554 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1555 | * Enqueues message on receive queue if acceptable; optionally handles |
| 1556 | * disconnect indication for a connected socket. |
| 1557 | * |
| 1558 | * Called with socket lock already taken; port lock may also be taken. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1559 | * |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1560 | * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1561 | * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1562 | */ |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1563 | static int filter_rcv(struct sock *sk, struct sk_buff *buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1564 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1565 | struct socket *sock = sk->sk_socket; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1566 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1567 | struct tipc_msg *msg = buf_msg(buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1568 | unsigned int limit = rcvbuf_limit(sk, buf); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1569 | u32 onode; |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1570 | int rc = TIPC_OK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1571 | |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1572 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
| 1573 | return tipc_sk_proto_rcv(tsk, &onode, buf); |
Jon Paul Maloy | ec8a2e5 | 2014-06-25 20:41:40 -0500 | [diff] [blame] | 1574 | |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 1575 | if (unlikely(msg_user(msg) == SOCK_WAKEUP)) { |
| 1576 | kfree_skb(buf); |
| 1577 | tsk->link_cong = 0; |
| 1578 | sk->sk_write_space(sk); |
| 1579 | return TIPC_OK; |
| 1580 | } |
| 1581 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1582 | /* Reject message if it is wrong sort of message for socket */ |
Allan Stephens | aad5854 | 2012-04-26 18:13:08 -0400 | [diff] [blame] | 1583 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1584 | return -TIPC_ERR_NO_PORT; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1585 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1586 | if (sock->state == SS_READY) { |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1587 | if (msg_connected(msg)) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1588 | return -TIPC_ERR_NO_PORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1589 | } else { |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1590 | rc = filter_connect(tsk, &buf); |
| 1591 | if (rc != TIPC_OK || buf == NULL) |
| 1592 | return rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* Reject message if there isn't room to queue it */ |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1596 | if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1597 | return -TIPC_ERR_OVERLOAD; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1598 | |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1599 | /* Enqueue message */ |
Ying Xue | 4068243 | 2013-10-18 07:23:16 +0200 | [diff] [blame] | 1600 | TIPC_SKB_CB(buf)->handle = NULL; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1601 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1602 | skb_set_owner_r(buf, sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1603 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1604 | sk->sk_data_ready(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1605 | return TIPC_OK; |
| 1606 | } |
| 1607 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1608 | /** |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1609 | * tipc_backlog_rcv - handle incoming message from backlog queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1610 | * @sk: socket |
| 1611 | * @buf: message |
| 1612 | * |
| 1613 | * Caller must hold socket lock, but not port lock. |
| 1614 | * |
| 1615 | * Returns 0 |
| 1616 | */ |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1617 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1618 | { |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1619 | int rc; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1620 | u32 onode; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1621 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 02c00c2 | 2014-06-09 11:08:18 -0500 | [diff] [blame] | 1622 | uint truesize = buf->truesize; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1623 | |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1624 | rc = filter_rcv(sk, buf); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1625 | |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1626 | if (likely(!rc)) { |
| 1627 | if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) |
| 1628 | atomic_add(truesize, &tsk->dupl_rcvcnt); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc)) |
| 1633 | return 0; |
| 1634 | |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 1635 | tipc_link_xmit(buf, onode, 0); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1636 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | /** |
Jon Paul Maloy | 24be34b | 2014-03-12 11:31:10 -0400 | [diff] [blame] | 1641 | * tipc_sk_rcv - handle incoming message |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1642 | * @buf: buffer containing arriving message |
| 1643 | * Consumes buffer |
| 1644 | * Returns 0 if success, or errno: -EHOSTUNREACH |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1645 | */ |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1646 | int tipc_sk_rcv(struct sk_buff *buf) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1647 | { |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1648 | struct tipc_sock *tsk; |
| 1649 | struct tipc_port *port; |
| 1650 | struct sock *sk; |
| 1651 | u32 dport = msg_destport(buf_msg(buf)); |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1652 | int rc = TIPC_OK; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1653 | uint limit; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1654 | u32 dnode; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1655 | |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1656 | /* Validate destination and message */ |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1657 | port = tipc_port_lock(dport); |
| 1658 | if (unlikely(!port)) { |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1659 | rc = tipc_msg_eval(buf, &dnode); |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1660 | goto exit; |
| 1661 | } |
| 1662 | |
| 1663 | tsk = tipc_port_to_sock(port); |
| 1664 | sk = &tsk->sk; |
| 1665 | |
| 1666 | /* Queue message */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1667 | bh_lock_sock(sk); |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1668 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1669 | if (!sock_owned_by_user(sk)) { |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1670 | rc = filter_rcv(sk, buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1671 | } else { |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1672 | if (sk->sk_backlog.len == 0) |
| 1673 | atomic_set(&tsk->dupl_rcvcnt, 0); |
| 1674 | limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt); |
| 1675 | if (sk_add_backlog(sk, buf, limit)) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1676 | rc = -TIPC_ERR_OVERLOAD; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1677 | } |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1678 | bh_unlock_sock(sk); |
| 1679 | tipc_port_unlock(port); |
| 1680 | |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1681 | if (likely(!rc)) |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1682 | return 0; |
| 1683 | exit: |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1684 | if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc)) |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1685 | return -EHOSTUNREACH; |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1686 | |
Jon Paul Maloy | 9fbfb8b | 2014-07-16 20:41:03 -0400 | [diff] [blame] | 1687 | tipc_link_xmit(buf, dnode, 0); |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1688 | return (rc < 0) ? -EHOSTUNREACH : 0; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1691 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
| 1692 | { |
| 1693 | struct sock *sk = sock->sk; |
| 1694 | DEFINE_WAIT(wait); |
| 1695 | int done; |
| 1696 | |
| 1697 | do { |
| 1698 | int err = sock_error(sk); |
| 1699 | if (err) |
| 1700 | return err; |
| 1701 | if (!*timeo_p) |
| 1702 | return -ETIMEDOUT; |
| 1703 | if (signal_pending(current)) |
| 1704 | return sock_intr_errno(*timeo_p); |
| 1705 | |
| 1706 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1707 | done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); |
| 1708 | finish_wait(sk_sleep(sk), &wait); |
| 1709 | } while (!done); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1713 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1714 | * tipc_connect - establish a connection to another TIPC port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1715 | * @sock: socket structure |
| 1716 | * @dest: socket address for destination port |
| 1717 | * @destlen: size of socket address data structure |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1718 | * @flags: file-related flags associated with socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1719 | * |
| 1720 | * Returns 0 on success, errno otherwise |
| 1721 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1722 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
| 1723 | int destlen, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1724 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1725 | struct sock *sk = sock->sk; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1726 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
| 1727 | struct msghdr m = {NULL,}; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1728 | long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; |
| 1729 | socket_state previous; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1730 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1731 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1732 | lock_sock(sk); |
| 1733 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1734 | /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1735 | if (sock->state == SS_READY) { |
| 1736 | res = -EOPNOTSUPP; |
| 1737 | goto exit; |
| 1738 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1739 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1740 | /* |
| 1741 | * Reject connection attempt using multicast address |
| 1742 | * |
| 1743 | * Note: send_msg() validates the rest of the address fields, |
| 1744 | * so there's no need to do it here |
| 1745 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1746 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
| 1747 | res = -EINVAL; |
| 1748 | goto exit; |
| 1749 | } |
| 1750 | |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1751 | previous = sock->state; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1752 | switch (sock->state) { |
| 1753 | case SS_UNCONNECTED: |
| 1754 | /* Send a 'SYN-' to destination */ |
| 1755 | m.msg_name = dest; |
| 1756 | m.msg_namelen = destlen; |
| 1757 | |
| 1758 | /* If connect is in non-blocking case, set MSG_DONTWAIT to |
| 1759 | * indicate send_msg() is never blocked. |
| 1760 | */ |
| 1761 | if (!timeout) |
| 1762 | m.msg_flags = MSG_DONTWAIT; |
| 1763 | |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1764 | res = tipc_sendmsg(NULL, sock, &m, 0); |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1765 | if ((res < 0) && (res != -EWOULDBLOCK)) |
| 1766 | goto exit; |
| 1767 | |
| 1768 | /* Just entered SS_CONNECTING state; the only |
| 1769 | * difference is that return value in non-blocking |
| 1770 | * case is EINPROGRESS, rather than EALREADY. |
| 1771 | */ |
| 1772 | res = -EINPROGRESS; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1773 | case SS_CONNECTING: |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1774 | if (previous == SS_CONNECTING) |
| 1775 | res = -EALREADY; |
| 1776 | if (!timeout) |
| 1777 | goto exit; |
| 1778 | timeout = msecs_to_jiffies(timeout); |
| 1779 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ |
| 1780 | res = tipc_wait_for_connect(sock, &timeout); |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1781 | break; |
| 1782 | case SS_CONNECTED: |
| 1783 | res = -EISCONN; |
| 1784 | break; |
| 1785 | default: |
| 1786 | res = -EINVAL; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1787 | break; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1788 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1789 | exit: |
| 1790 | release_sock(sk); |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1791 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1792 | } |
| 1793 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1794 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1795 | * tipc_listen - allow socket to listen for incoming connections |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1796 | * @sock: socket structure |
| 1797 | * @len: (unused) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1798 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1799 | * Returns 0 on success, errno otherwise |
| 1800 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1801 | static int tipc_listen(struct socket *sock, int len) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1802 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1803 | struct sock *sk = sock->sk; |
| 1804 | int res; |
| 1805 | |
| 1806 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1807 | |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 1808 | if (sock->state != SS_UNCONNECTED) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1809 | res = -EINVAL; |
| 1810 | else { |
| 1811 | sock->state = SS_LISTENING; |
| 1812 | res = 0; |
| 1813 | } |
| 1814 | |
| 1815 | release_sock(sk); |
| 1816 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1819 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
| 1820 | { |
| 1821 | struct sock *sk = sock->sk; |
| 1822 | DEFINE_WAIT(wait); |
| 1823 | int err; |
| 1824 | |
| 1825 | /* True wake-one mechanism for incoming connections: only |
| 1826 | * one process gets woken up, not the 'whole herd'. |
| 1827 | * Since we do not 'race & poll' for established sockets |
| 1828 | * anymore, the common case will execute the loop only once. |
| 1829 | */ |
| 1830 | for (;;) { |
| 1831 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
| 1832 | TASK_INTERRUPTIBLE); |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 1833 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1834 | release_sock(sk); |
| 1835 | timeo = schedule_timeout(timeo); |
| 1836 | lock_sock(sk); |
| 1837 | } |
| 1838 | err = 0; |
| 1839 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 1840 | break; |
| 1841 | err = -EINVAL; |
| 1842 | if (sock->state != SS_LISTENING) |
| 1843 | break; |
| 1844 | err = sock_intr_errno(timeo); |
| 1845 | if (signal_pending(current)) |
| 1846 | break; |
| 1847 | err = -EAGAIN; |
| 1848 | if (!timeo) |
| 1849 | break; |
| 1850 | } |
| 1851 | finish_wait(sk_sleep(sk), &wait); |
| 1852 | return err; |
| 1853 | } |
| 1854 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1855 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1856 | * tipc_accept - wait for connection request |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1857 | * @sock: listening socket |
| 1858 | * @newsock: new socket that is to be connected |
| 1859 | * @flags: file-related flags associated with socket |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1860 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1861 | * Returns 0 on success, errno otherwise |
| 1862 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1863 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1864 | { |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1865 | struct sock *new_sk, *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1866 | struct sk_buff *buf; |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1867 | struct tipc_port *new_port; |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1868 | struct tipc_msg *msg; |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1869 | long timeo; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1870 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1871 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1872 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1873 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1874 | if (sock->state != SS_LISTENING) { |
| 1875 | res = -EINVAL; |
| 1876 | goto exit; |
| 1877 | } |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1878 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 1879 | res = tipc_wait_for_accept(sock, timeo); |
| 1880 | if (res) |
| 1881 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1882 | |
| 1883 | buf = skb_peek(&sk->sk_receive_queue); |
| 1884 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 1885 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1886 | if (res) |
| 1887 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1888 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1889 | new_sk = new_sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1890 | new_port = &tipc_sk(new_sk)->port; |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1891 | msg = buf_msg(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1892 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1893 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
| 1894 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1895 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1896 | /* |
| 1897 | * Reject any stray messages received by new socket |
| 1898 | * before the socket lock was taken (very, very unlikely) |
| 1899 | */ |
| 1900 | reject_rx_queue(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1901 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1902 | /* Connect new socket to it's peer */ |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1903 | tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg)); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1904 | new_sock->state = SS_CONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1905 | |
Jon Paul Maloy | 3b4f302 | 2014-03-12 11:31:11 -0400 | [diff] [blame] | 1906 | tipc_port_set_importance(new_port, msg_importance(msg)); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1907 | if (msg_named(msg)) { |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1908 | new_port->conn_type = msg_nametype(msg); |
| 1909 | new_port->conn_instance = msg_nameinst(msg); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1910 | } |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. |
| 1914 | * Respond to 'SYN+' by queuing it on new socket. |
| 1915 | */ |
| 1916 | if (!msg_data_sz(msg)) { |
| 1917 | struct msghdr m = {NULL,}; |
| 1918 | |
| 1919 | advance_rx_queue(sk); |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1920 | tipc_send_packet(NULL, new_sock, &m, 0); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1921 | } else { |
| 1922 | __skb_dequeue(&sk->sk_receive_queue); |
| 1923 | __skb_queue_head(&new_sk->sk_receive_queue, buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1924 | skb_set_owner_r(buf, new_sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1925 | } |
| 1926 | release_sock(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1927 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1928 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1929 | return res; |
| 1930 | } |
| 1931 | |
| 1932 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1933 | * tipc_shutdown - shutdown socket connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1934 | * @sock: socket structure |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 1935 | * @how: direction to close (must be SHUT_RDWR) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1936 | * |
| 1937 | * Terminates connection (if necessary), then purges socket's receive queue. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1938 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1939 | * Returns 0 on success, errno otherwise |
| 1940 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1941 | static int tipc_shutdown(struct socket *sock, int how) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1942 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1943 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1944 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1945 | struct tipc_port *port = &tsk->port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1946 | struct sk_buff *buf; |
Jon Paul Maloy | 80e44c2 | 2014-08-22 18:09:10 -0400 | [diff] [blame] | 1947 | u32 dnode; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1948 | int res; |
| 1949 | |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 1950 | if (how != SHUT_RDWR) |
| 1951 | return -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1952 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1953 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1954 | |
| 1955 | switch (sock->state) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1956 | case SS_CONNECTING: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1957 | case SS_CONNECTED: |
| 1958 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1959 | restart: |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 1960 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1961 | buf = __skb_dequeue(&sk->sk_receive_queue); |
| 1962 | if (buf) { |
Ying Xue | 4068243 | 2013-10-18 07:23:16 +0200 | [diff] [blame] | 1963 | if (TIPC_SKB_CB(buf)->handle != NULL) { |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 1964 | kfree_skb(buf); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1965 | goto restart; |
| 1966 | } |
Jon Paul Maloy | 80e44c2 | 2014-08-22 18:09:10 -0400 | [diff] [blame] | 1967 | if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN)) |
| 1968 | tipc_link_xmit(buf, dnode, port->ref); |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1969 | tipc_node_remove_conn(dnode, port->ref); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1970 | } else { |
Jon Paul Maloy | 80e44c2 | 2014-08-22 18:09:10 -0400 | [diff] [blame] | 1971 | dnode = tipc_port_peernode(port); |
| 1972 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
| 1973 | TIPC_CONN_MSG, SHORT_H_SIZE, |
| 1974 | 0, dnode, tipc_own_addr, |
| 1975 | tipc_port_peerport(port), |
| 1976 | port->ref, TIPC_CONN_SHUTDOWN); |
| 1977 | tipc_link_xmit(buf, dnode, port->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1978 | } |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1979 | port->connected = 0; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1980 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1981 | tipc_node_remove_conn(dnode, port->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1982 | /* fall through */ |
| 1983 | |
| 1984 | case SS_DISCONNECTING: |
| 1985 | |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 1986 | /* Discard any unreceived messages */ |
Ying Xue | 57467e5 | 2013-01-20 23:30:08 +0100 | [diff] [blame] | 1987 | __skb_queue_purge(&sk->sk_receive_queue); |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 1988 | |
| 1989 | /* Wake up anyone sleeping in poll */ |
| 1990 | sk->sk_state_change(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1991 | res = 0; |
| 1992 | break; |
| 1993 | |
| 1994 | default: |
| 1995 | res = -ENOTCONN; |
| 1996 | } |
| 1997 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1998 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1999 | return res; |
| 2000 | } |
| 2001 | |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2002 | static void tipc_sk_timeout(unsigned long ref) |
| 2003 | { |
| 2004 | struct tipc_port *port = tipc_port_lock(ref); |
| 2005 | struct tipc_sock *tsk; |
| 2006 | struct sock *sk; |
| 2007 | struct sk_buff *buf = NULL; |
| 2008 | struct tipc_msg *msg = NULL; |
| 2009 | u32 peer_port, peer_node; |
| 2010 | |
| 2011 | if (!port) |
| 2012 | return; |
| 2013 | |
| 2014 | if (!port->connected) { |
| 2015 | tipc_port_unlock(port); |
| 2016 | return; |
| 2017 | } |
| 2018 | tsk = tipc_port_to_sock(port); |
| 2019 | sk = &tsk->sk; |
| 2020 | bh_lock_sock(sk); |
| 2021 | peer_port = tipc_port_peerport(port); |
| 2022 | peer_node = tipc_port_peernode(port); |
| 2023 | |
| 2024 | if (port->probing_state == TIPC_CONN_PROBING) { |
| 2025 | /* Previous probe not answered -> self abort */ |
| 2026 | buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
| 2027 | SHORT_H_SIZE, 0, tipc_own_addr, |
| 2028 | peer_node, ref, peer_port, |
| 2029 | TIPC_ERR_NO_PORT); |
| 2030 | } else { |
| 2031 | buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, |
| 2032 | 0, peer_node, tipc_own_addr, |
| 2033 | peer_port, ref, TIPC_OK); |
| 2034 | port->probing_state = TIPC_CONN_PROBING; |
| 2035 | k_start_timer(&port->timer, port->probing_interval); |
| 2036 | } |
| 2037 | bh_unlock_sock(sk); |
| 2038 | tipc_port_unlock(port); |
| 2039 | if (!buf) |
| 2040 | return; |
| 2041 | |
| 2042 | msg = buf_msg(buf); |
| 2043 | tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg)); |
| 2044 | } |
| 2045 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2046 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2047 | * tipc_setsockopt - set socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2048 | * @sock: socket structure |
| 2049 | * @lvl: option level |
| 2050 | * @opt: option identifier |
| 2051 | * @ov: pointer to new option value |
| 2052 | * @ol: length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2053 | * |
| 2054 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2055 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2056 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2057 | * Returns 0 on success, errno otherwise |
| 2058 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2059 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
| 2060 | char __user *ov, unsigned int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2061 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2062 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2063 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2064 | struct tipc_port *port = &tsk->port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2065 | u32 value; |
| 2066 | int res; |
| 2067 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2068 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 2069 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2070 | if (lvl != SOL_TIPC) |
| 2071 | return -ENOPROTOOPT; |
| 2072 | if (ol < sizeof(value)) |
| 2073 | return -EINVAL; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 2074 | res = get_user(value, (u32 __user *)ov); |
| 2075 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2076 | return res; |
| 2077 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2078 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2079 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2080 | switch (opt) { |
| 2081 | case TIPC_IMPORTANCE: |
Erik Hugne | ac32c7f | 2014-08-15 16:44:35 +0200 | [diff] [blame] | 2082 | res = tipc_port_set_importance(port, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2083 | break; |
| 2084 | case TIPC_SRC_DROPPABLE: |
| 2085 | if (sock->type != SOCK_STREAM) |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2086 | tipc_port_set_unreliable(port, value); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2087 | else |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2088 | res = -ENOPROTOOPT; |
| 2089 | break; |
| 2090 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2091 | tipc_port_set_unreturnable(port, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2092 | break; |
| 2093 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 2094 | tipc_sk(sk)->conn_timeout = value; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2095 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2096 | break; |
| 2097 | default: |
| 2098 | res = -EINVAL; |
| 2099 | } |
| 2100 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2101 | release_sock(sk); |
| 2102 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2103 | return res; |
| 2104 | } |
| 2105 | |
| 2106 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2107 | * tipc_getsockopt - get socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2108 | * @sock: socket structure |
| 2109 | * @lvl: option level |
| 2110 | * @opt: option identifier |
| 2111 | * @ov: receptacle for option value |
| 2112 | * @ol: receptacle for length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2113 | * |
| 2114 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2115 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2116 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2117 | * Returns 0 on success, errno otherwise |
| 2118 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2119 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
| 2120 | char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2121 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2122 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2123 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2124 | struct tipc_port *port = &tsk->port; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2125 | int len; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2126 | u32 value; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2127 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2128 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2129 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 2130 | return put_user(0, ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2131 | if (lvl != SOL_TIPC) |
| 2132 | return -ENOPROTOOPT; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 2133 | res = get_user(len, ol); |
| 2134 | if (res) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2135 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2136 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2137 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2138 | |
| 2139 | switch (opt) { |
| 2140 | case TIPC_IMPORTANCE: |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2141 | value = tipc_port_importance(port); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2142 | break; |
| 2143 | case TIPC_SRC_DROPPABLE: |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2144 | value = tipc_port_unreliable(port); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2145 | break; |
| 2146 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2147 | value = tipc_port_unreturnable(port); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2148 | break; |
| 2149 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 2150 | value = tipc_sk(sk)->conn_timeout; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2151 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2152 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2153 | case TIPC_NODE_RECVQ_DEPTH: |
Ying Xue | 9da3d47 | 2012-11-27 06:15:27 -0500 | [diff] [blame] | 2154 | value = 0; /* was tipc_queue_size, now obsolete */ |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 2155 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2156 | case TIPC_SOCK_RECVQ_DEPTH: |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 2157 | value = skb_queue_len(&sk->sk_receive_queue); |
| 2158 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2159 | default: |
| 2160 | res = -EINVAL; |
| 2161 | } |
| 2162 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2163 | release_sock(sk); |
| 2164 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 2165 | if (res) |
| 2166 | return res; /* "get" failed */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2167 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 2168 | if (len < sizeof(value)) |
| 2169 | return -EINVAL; |
| 2170 | |
| 2171 | if (copy_to_user(ov, &value, sizeof(value))) |
| 2172 | return -EFAULT; |
| 2173 | |
| 2174 | return put_user(sizeof(value), ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Wei Yongjun | 52f50ce | 2014-07-20 13:14:28 +0800 | [diff] [blame] | 2177 | static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg) |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2178 | { |
| 2179 | struct tipc_sioc_ln_req lnr; |
| 2180 | void __user *argp = (void __user *)arg; |
| 2181 | |
| 2182 | switch (cmd) { |
| 2183 | case SIOCGETLINKNAME: |
| 2184 | if (copy_from_user(&lnr, argp, sizeof(lnr))) |
| 2185 | return -EFAULT; |
| 2186 | if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer, |
| 2187 | lnr.linkname, TIPC_MAX_LINK_NAME)) { |
| 2188 | if (copy_to_user(argp, &lnr, sizeof(lnr))) |
| 2189 | return -EFAULT; |
| 2190 | return 0; |
| 2191 | } |
| 2192 | return -EADDRNOTAVAIL; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2193 | default: |
| 2194 | return -ENOIOCTLCMD; |
| 2195 | } |
| 2196 | } |
| 2197 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 2198 | /* Protocol switches for the various types of TIPC sockets */ |
| 2199 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2200 | static const struct proto_ops msg_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2201 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2202 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2203 | .release = tipc_release, |
| 2204 | .bind = tipc_bind, |
| 2205 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2206 | .socketpair = sock_no_socketpair, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 2207 | .accept = sock_no_accept, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2208 | .getname = tipc_getname, |
| 2209 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2210 | .ioctl = tipc_ioctl, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 2211 | .listen = sock_no_listen, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2212 | .shutdown = tipc_shutdown, |
| 2213 | .setsockopt = tipc_setsockopt, |
| 2214 | .getsockopt = tipc_getsockopt, |
| 2215 | .sendmsg = tipc_sendmsg, |
| 2216 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2217 | .mmap = sock_no_mmap, |
| 2218 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2219 | }; |
| 2220 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2221 | static const struct proto_ops packet_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2222 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2223 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2224 | .release = tipc_release, |
| 2225 | .bind = tipc_bind, |
| 2226 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2227 | .socketpair = sock_no_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2228 | .accept = tipc_accept, |
| 2229 | .getname = tipc_getname, |
| 2230 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2231 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2232 | .listen = tipc_listen, |
| 2233 | .shutdown = tipc_shutdown, |
| 2234 | .setsockopt = tipc_setsockopt, |
| 2235 | .getsockopt = tipc_getsockopt, |
| 2236 | .sendmsg = tipc_send_packet, |
| 2237 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2238 | .mmap = sock_no_mmap, |
| 2239 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2240 | }; |
| 2241 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2242 | static const struct proto_ops stream_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2243 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2244 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2245 | .release = tipc_release, |
| 2246 | .bind = tipc_bind, |
| 2247 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2248 | .socketpair = sock_no_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2249 | .accept = tipc_accept, |
| 2250 | .getname = tipc_getname, |
| 2251 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2252 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2253 | .listen = tipc_listen, |
| 2254 | .shutdown = tipc_shutdown, |
| 2255 | .setsockopt = tipc_setsockopt, |
| 2256 | .getsockopt = tipc_getsockopt, |
| 2257 | .sendmsg = tipc_send_stream, |
| 2258 | .recvmsg = tipc_recv_stream, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2259 | .mmap = sock_no_mmap, |
| 2260 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2261 | }; |
| 2262 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2263 | static const struct net_proto_family tipc_family_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2264 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2265 | .family = AF_TIPC, |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 2266 | .create = tipc_sk_create |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2267 | }; |
| 2268 | |
| 2269 | static struct proto tipc_proto = { |
| 2270 | .name = "TIPC", |
| 2271 | .owner = THIS_MODULE, |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 2272 | .obj_size = sizeof(struct tipc_sock), |
| 2273 | .sysctl_rmem = sysctl_tipc_rmem |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2274 | }; |
| 2275 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 2276 | static struct proto tipc_proto_kern = { |
| 2277 | .name = "TIPC", |
| 2278 | .obj_size = sizeof(struct tipc_sock), |
| 2279 | .sysctl_rmem = sysctl_tipc_rmem |
| 2280 | }; |
| 2281 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2282 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2283 | * tipc_socket_init - initialize TIPC socket interface |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2284 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2285 | * Returns 0 on success, errno otherwise |
| 2286 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2287 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2288 | { |
| 2289 | int res; |
| 2290 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2291 | res = proto_register(&tipc_proto, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2292 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 2293 | pr_err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2294 | goto out; |
| 2295 | } |
| 2296 | |
| 2297 | res = sock_register(&tipc_family_ops); |
| 2298 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 2299 | pr_err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2300 | proto_unregister(&tipc_proto); |
| 2301 | goto out; |
| 2302 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2303 | out: |
| 2304 | return res; |
| 2305 | } |
| 2306 | |
| 2307 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2308 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2309 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2310 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2311 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2312 | sock_unregister(tipc_family_ops.family); |
| 2313 | proto_unregister(&tipc_proto); |
| 2314 | } |