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 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 37 | #include <linux/rhashtable.h> |
| 38 | #include <linux/jhash.h> |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 39 | #include "core.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 40 | #include "name_table.h" |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 41 | #include "node.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 42 | #include "link.h" |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 43 | #include "config.h" |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 44 | #include "socket.h" |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 45 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 46 | #define SS_LISTENING -1 /* socket is listening */ |
| 47 | #define SS_READY -2 /* socket is connectionless */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 48 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 49 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
Ying Xue | 2f55c43 | 2015-01-09 15:27:00 +0800 | [diff] [blame] | 50 | #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */ |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 51 | #define TIPC_FWD_MSG 1 |
| 52 | #define TIPC_CONN_OK 0 |
| 53 | #define TIPC_CONN_PROBING 1 |
| 54 | #define TIPC_MAX_PORT 0xffffffff |
| 55 | #define TIPC_MIN_PORT 1 |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * struct tipc_sock - TIPC socket structure |
| 59 | * @sk: socket - interacts with 'port' and with user via the socket API |
| 60 | * @connected: non-zero if port is currently connected to a peer port |
| 61 | * @conn_type: TIPC type used when connection was established |
| 62 | * @conn_instance: TIPC instance used when connection was established |
| 63 | * @published: non-zero if port has one or more associated names |
| 64 | * @max_pkt: maximum packet size "hint" used when building messages sent by port |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 65 | * @portid: unique port identity in TIPC socket hash table |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 66 | * @phdr: preformatted message header used when sending messages |
| 67 | * @port_list: adjacent ports in TIPC's global list of ports |
| 68 | * @publications: list of publications for port |
| 69 | * @pub_count: total # of publications port has made during its lifetime |
| 70 | * @probing_state: |
Ying Xue | 2f55c43 | 2015-01-09 15:27:00 +0800 | [diff] [blame] | 71 | * @probing_intv: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 72 | * @timer: |
| 73 | * @port: port - interacts with 'sk' and with the rest of the TIPC stack |
| 74 | * @peer_name: the peer of the connection, if any |
| 75 | * @conn_timeout: the time we can wait for an unresponded setup request |
| 76 | * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue |
| 77 | * @link_cong: non-zero if owner must sleep because of link congestion |
| 78 | * @sent_unacked: # messages sent by socket, and not yet acked by peer |
| 79 | * @rcv_unacked: # messages read by user, but not yet acked back to peer |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 80 | * @node: hash table node |
| 81 | * @rcu: rcu struct for tipc_sock |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 82 | */ |
| 83 | struct tipc_sock { |
| 84 | struct sock sk; |
| 85 | int connected; |
| 86 | u32 conn_type; |
| 87 | u32 conn_instance; |
| 88 | int published; |
| 89 | u32 max_pkt; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 90 | u32 portid; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 91 | struct tipc_msg phdr; |
| 92 | struct list_head sock_list; |
| 93 | struct list_head publications; |
| 94 | u32 pub_count; |
| 95 | u32 probing_state; |
Ying Xue | 2f55c43 | 2015-01-09 15:27:00 +0800 | [diff] [blame] | 96 | unsigned long probing_intv; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 97 | struct timer_list timer; |
| 98 | uint conn_timeout; |
| 99 | atomic_t dupl_rcvcnt; |
| 100 | bool link_cong; |
| 101 | uint sent_unacked; |
| 102 | uint rcv_unacked; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 103 | struct rhash_head node; |
| 104 | struct rcu_head rcu; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 105 | }; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 106 | |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 107 | 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] | 108 | static void tipc_data_ready(struct sock *sk); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 109 | static void tipc_write_space(struct sock *sk); |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 110 | static int tipc_release(struct socket *sock); |
| 111 | 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] | 112 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p); |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 113 | static void tipc_sk_timeout(unsigned long data); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 114 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 115 | struct tipc_name_seq const *seq); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 116 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 117 | struct tipc_name_seq const *seq); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 118 | static struct tipc_sock *tipc_sk_lookup(u32 portid); |
| 119 | static int tipc_sk_insert(struct tipc_sock *tsk); |
| 120 | static void tipc_sk_remove(struct tipc_sock *tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 121 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 122 | static const struct proto_ops packet_ops; |
| 123 | static const struct proto_ops stream_ops; |
| 124 | static const struct proto_ops msg_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 125 | |
| 126 | static struct proto tipc_proto; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 127 | static struct proto tipc_proto_kern; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 128 | |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 129 | static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = { |
| 130 | [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC }, |
| 131 | [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 }, |
| 132 | [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 }, |
| 133 | [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED }, |
| 134 | [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG } |
| 135 | }; |
| 136 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 137 | /* |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 138 | * Revised TIPC socket locking policy: |
| 139 | * |
| 140 | * Most socket operations take the standard socket lock when they start |
| 141 | * and hold it until they finish (or until they need to sleep). Acquiring |
| 142 | * this lock grants the owner exclusive access to the fields of the socket |
| 143 | * data structures, with the exception of the backlog queue. A few socket |
| 144 | * operations can be done without taking the socket lock because they only |
| 145 | * read socket information that never changes during the life of the socket. |
| 146 | * |
| 147 | * Socket operations may acquire the lock for the associated TIPC port if they |
| 148 | * need to perform an operation on the port. If any routine needs to acquire |
| 149 | * both the socket lock and the port lock it must take the socket lock first |
| 150 | * to avoid the risk of deadlock. |
| 151 | * |
| 152 | * The dispatcher handling incoming messages cannot grab the socket lock in |
| 153 | * the standard fashion, since invoked it runs at the BH level and cannot block. |
| 154 | * Instead, it checks to see if the socket lock is currently owned by someone, |
| 155 | * and either handles the message itself or adds it to the socket's backlog |
| 156 | * queue; in the latter case the queued message is processed once the process |
| 157 | * owning the socket lock releases it. |
| 158 | * |
| 159 | * NOTE: Releasing the socket lock while an operation is sleeping overcomes |
| 160 | * the problem of a blocked socket operation preventing any other operations |
| 161 | * from occurring. However, applications must be careful if they have |
| 162 | * multiple threads trying to send (or receive) on the same socket, as these |
| 163 | * operations might interfere with each other. For example, doing a connect |
| 164 | * and a receive at the same time might allow the receive to consume the |
| 165 | * ACK message meant for the connect. While additional work could be done |
| 166 | * to try and overcome this, it doesn't seem to be worthwhile at the present. |
| 167 | * |
| 168 | * NOTE: Releasing the socket lock while an operation is sleeping also ensures |
| 169 | * that another operation that must be performed in a non-blocking manner is |
| 170 | * not delayed for very long because the lock has already been taken. |
| 171 | * |
| 172 | * NOTE: This code assumes that certain fields of a port/socket pair are |
| 173 | * constant over its lifetime; such fields can be examined without taking |
| 174 | * the socket lock and/or port lock, and do not need to be re-read even |
| 175 | * after resuming processing after waiting. These fields include: |
| 176 | * - socket type |
| 177 | * - pointer to socket sk structure (aka tipc_sock structure) |
| 178 | * - pointer to port structure |
| 179 | * - port reference |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 180 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 181 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 182 | /* Protects tipc socket hash table mutations */ |
| 183 | static struct rhashtable tipc_sk_rht; |
| 184 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 185 | static u32 tsk_peer_node(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 186 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 187 | return msg_destnode(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 190 | static u32 tsk_peer_port(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 191 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 192 | return msg_destport(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 195 | static bool tsk_unreliable(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 196 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 197 | return msg_src_droppable(&tsk->phdr) != 0; |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 200 | static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 201 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 202 | msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 205 | static bool tsk_unreturnable(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 206 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 207 | return msg_dest_droppable(&tsk->phdr) != 0; |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 210 | static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 211 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 212 | msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 215 | static int tsk_importance(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 216 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 217 | return msg_importance(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 220 | static int tsk_set_importance(struct tipc_sock *tsk, int imp) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 221 | { |
| 222 | if (imp > TIPC_CRITICAL_IMPORTANCE) |
| 223 | return -EINVAL; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 224 | msg_set_importance(&tsk->phdr, (u32)imp); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 227 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 228 | static struct tipc_sock *tipc_sk(const struct sock *sk) |
| 229 | { |
| 230 | return container_of(sk, struct tipc_sock, sk); |
| 231 | } |
| 232 | |
| 233 | static int tsk_conn_cong(struct tipc_sock *tsk) |
| 234 | { |
| 235 | return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN; |
| 236 | } |
| 237 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 238 | /** |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 239 | * tsk_advance_rx_queue - discard first buffer in socket receive queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 240 | * |
| 241 | * Caller must hold socket lock |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 242 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 243 | static void tsk_advance_rx_queue(struct sock *sk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 244 | { |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 245 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 249 | * tsk_rej_rx_queue - reject all buffers in socket receive queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 250 | * |
| 251 | * Caller must hold socket lock |
| 252 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 253 | static void tsk_rej_rx_queue(struct sock *sk) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 254 | { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 255 | struct sk_buff *skb; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 256 | u32 dnode; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 257 | |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 258 | while ((skb = __skb_dequeue(&sk->sk_receive_queue))) { |
| 259 | if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT)) |
| 260 | tipc_link_xmit_skb(skb, dnode, 0); |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 261 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 264 | /* tsk_peer_msg - verify if message was sent by connected port's peer |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 265 | * |
| 266 | * Handles cases where the node's network address has changed from |
| 267 | * the default of <0.0.0> to its configured setting. |
| 268 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 269 | static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 270 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 271 | u32 peer_port = tsk_peer_port(tsk); |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 272 | u32 orig_node; |
| 273 | u32 peer_node; |
| 274 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 275 | if (unlikely(!tsk->connected)) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 276 | return false; |
| 277 | |
| 278 | if (unlikely(msg_origport(msg) != peer_port)) |
| 279 | return false; |
| 280 | |
| 281 | orig_node = msg_orignode(msg); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 282 | peer_node = tsk_peer_node(tsk); |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 283 | |
| 284 | if (likely(orig_node == peer_node)) |
| 285 | return true; |
| 286 | |
| 287 | if (!orig_node && (peer_node == tipc_own_addr)) |
| 288 | return true; |
| 289 | |
| 290 | if (!peer_node && (orig_node == tipc_own_addr)) |
| 291 | return true; |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 296 | /** |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 297 | * tipc_sk_create - create a TIPC socket |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 298 | * @net: network namespace (must be default network) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 299 | * @sock: pre-allocated socket structure |
| 300 | * @protocol: protocol indicator (must be 0) |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 301 | * @kern: caused by kernel or by userspace? |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 302 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 303 | * This routine creates additional data structures used by the TIPC socket, |
| 304 | * initializes them, and links them together. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 305 | * |
| 306 | * Returns 0 on success, errno otherwise |
| 307 | */ |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 308 | static int tipc_sk_create(struct net *net, struct socket *sock, |
| 309 | int protocol, int kern) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 310 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 311 | const struct proto_ops *ops; |
| 312 | socket_state state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 313 | struct sock *sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 314 | struct tipc_sock *tsk; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame] | 315 | struct tipc_msg *msg; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 316 | |
| 317 | /* Validate arguments */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 318 | if (unlikely(protocol != 0)) |
| 319 | return -EPROTONOSUPPORT; |
| 320 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 321 | switch (sock->type) { |
| 322 | case SOCK_STREAM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 323 | ops = &stream_ops; |
| 324 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 325 | break; |
| 326 | case SOCK_SEQPACKET: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 327 | ops = &packet_ops; |
| 328 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 329 | break; |
| 330 | case SOCK_DGRAM: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 331 | case SOCK_RDM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 332 | ops = &msg_ops; |
| 333 | state = SS_READY; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 334 | break; |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 335 | default: |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 336 | return -EPROTOTYPE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 339 | /* Allocate socket's protocol area */ |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 340 | if (!kern) |
| 341 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); |
| 342 | else |
| 343 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern); |
| 344 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 345 | if (sk == NULL) |
| 346 | return -ENOMEM; |
| 347 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 348 | tsk = tipc_sk(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 349 | tsk->max_pkt = MAX_PKT_DEFAULT; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 350 | INIT_LIST_HEAD(&tsk->publications); |
| 351 | msg = &tsk->phdr; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame] | 352 | tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, |
| 353 | NAMED_H_SIZE, 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 354 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 355 | /* Finish initializing socket data structures */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 356 | sock->ops = ops; |
| 357 | sock->state = state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 358 | sock_init_data(sock, sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 359 | if (tipc_sk_insert(tsk)) { |
| 360 | pr_warn("Socket create failed; port numbrer exhausted\n"); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | msg_set_origport(msg, tsk->portid); |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 364 | setup_timer(&tsk->timer, tipc_sk_timeout, (unsigned long)tsk); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 365 | sk->sk_backlog_rcv = tipc_backlog_rcv; |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 366 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 367 | sk->sk_data_ready = tipc_data_ready; |
| 368 | sk->sk_write_space = tipc_write_space; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 369 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 370 | tsk->sent_unacked = 0; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 371 | atomic_set(&tsk->dupl_rcvcnt, 0); |
Allan Stephens | 7ef43eb | 2008-05-12 15:42:28 -0700 | [diff] [blame] | 372 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 373 | if (sock->state == SS_READY) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 374 | tsk_set_unreturnable(tsk, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 375 | if (sock->type == SOCK_DGRAM) |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 376 | tsk_set_unreliable(tsk, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 377 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 382 | * tipc_sock_create_local - create TIPC socket from inside TIPC module |
| 383 | * @type: socket type - SOCK_RDM or SOCK_SEQPACKET |
| 384 | * |
| 385 | * We cannot use sock_creat_kern here because it bumps module user count. |
| 386 | * Since socket owner and creator is the same module we must make sure |
| 387 | * that module count remains zero for module local sockets, otherwise |
| 388 | * we cannot do rmmod. |
| 389 | * |
| 390 | * Returns 0 on success, errno otherwise |
| 391 | */ |
| 392 | int tipc_sock_create_local(int type, struct socket **res) |
| 393 | { |
| 394 | int rc; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 395 | |
| 396 | rc = sock_create_lite(AF_TIPC, type, 0, res); |
| 397 | if (rc < 0) { |
| 398 | pr_err("Failed to create kernel socket\n"); |
| 399 | return rc; |
| 400 | } |
| 401 | tipc_sk_create(&init_net, *res, 0, 1); |
| 402 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * tipc_sock_release_local - release socket created by tipc_sock_create_local |
| 408 | * @sock: the socket to be released. |
| 409 | * |
| 410 | * Module reference count is not incremented when such sockets are created, |
| 411 | * so we must keep it from being decremented when they are released. |
| 412 | */ |
| 413 | void tipc_sock_release_local(struct socket *sock) |
| 414 | { |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 415 | tipc_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 416 | sock->ops = NULL; |
| 417 | sock_release(sock); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * tipc_sock_accept_local - accept a connection on a socket created |
| 422 | * with tipc_sock_create_local. Use this function to avoid that |
| 423 | * module reference count is inadvertently incremented. |
| 424 | * |
| 425 | * @sock: the accepting socket |
| 426 | * @newsock: reference to the new socket to be created |
| 427 | * @flags: socket flags |
| 428 | */ |
| 429 | |
| 430 | int tipc_sock_accept_local(struct socket *sock, struct socket **newsock, |
Paul Gortmaker | ae8509c | 2013-06-17 10:54:47 -0400 | [diff] [blame] | 431 | int flags) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 432 | { |
| 433 | struct sock *sk = sock->sk; |
| 434 | int ret; |
| 435 | |
| 436 | ret = sock_create_lite(sk->sk_family, sk->sk_type, |
| 437 | sk->sk_protocol, newsock); |
| 438 | if (ret < 0) |
| 439 | return ret; |
| 440 | |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 441 | ret = tipc_accept(sock, *newsock, flags); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 442 | if (ret < 0) { |
| 443 | sock_release(*newsock); |
| 444 | return ret; |
| 445 | } |
| 446 | (*newsock)->ops = sock->ops; |
| 447 | return ret; |
| 448 | } |
| 449 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 450 | static void tipc_sk_callback(struct rcu_head *head) |
| 451 | { |
| 452 | struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu); |
| 453 | |
| 454 | sock_put(&tsk->sk); |
| 455 | } |
| 456 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 457 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 458 | * tipc_release - destroy a TIPC socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 459 | * @sock: socket to destroy |
| 460 | * |
| 461 | * This routine cleans up any messages that are still queued on the socket. |
| 462 | * For DGRAM and RDM socket types, all queued messages are rejected. |
| 463 | * For SEQPACKET and STREAM socket types, the first message is rejected |
| 464 | * and any others are discarded. (If the first message on a STREAM socket |
| 465 | * 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] | 466 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 467 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
| 468 | * are returned or discarded according to the "destination droppable" setting |
| 469 | * specified for the message by the sender. |
| 470 | * |
| 471 | * Returns 0 on success, errno otherwise |
| 472 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 473 | static int tipc_release(struct socket *sock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 474 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 475 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 476 | struct tipc_sock *tsk; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 477 | struct sk_buff *skb; |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 478 | u32 dnode, probing_state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 479 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 480 | /* |
| 481 | * Exit if socket isn't fully initialized (occurs when a failed accept() |
| 482 | * releases a pre-allocated child socket that was never used) |
| 483 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 484 | if (sk == NULL) |
| 485 | return 0; |
| 486 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 487 | tsk = tipc_sk(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 488 | lock_sock(sk); |
| 489 | |
| 490 | /* |
| 491 | * Reject all unreceived messages, except on an active connection |
| 492 | * (which disconnects locally & sends a 'FIN+' to peer) |
| 493 | */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 494 | dnode = tsk_peer_node(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 495 | while (sock->state != SS_DISCONNECTING) { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 496 | skb = __skb_dequeue(&sk->sk_receive_queue); |
| 497 | if (skb == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 498 | break; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 499 | if (TIPC_SKB_CB(skb)->handle != NULL) |
| 500 | kfree_skb(skb); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 501 | else { |
| 502 | if ((sock->state == SS_CONNECTING) || |
| 503 | (sock->state == SS_CONNECTED)) { |
| 504 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 505 | tsk->connected = 0; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 506 | tipc_node_remove_conn(dnode, tsk->portid); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 507 | } |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 508 | if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT)) |
| 509 | tipc_link_xmit_skb(skb, dnode, 0); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 510 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 513 | tipc_sk_withdraw(tsk, 0, NULL); |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 514 | probing_state = tsk->probing_state; |
| 515 | if (del_timer_sync(&tsk->timer) && probing_state != TIPC_CONN_PROBING) |
| 516 | sock_put(sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 517 | tipc_sk_remove(tsk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 518 | if (tsk->connected) { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 519 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame] | 520 | SHORT_H_SIZE, 0, dnode, tipc_own_addr, |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 521 | tsk_peer_port(tsk), |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 522 | tsk->portid, TIPC_ERR_NO_PORT); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 523 | if (skb) |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 524 | tipc_link_xmit_skb(skb, dnode, tsk->portid); |
| 525 | tipc_node_remove_conn(dnode, tsk->portid); |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame] | 526 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 527 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 528 | /* Discard any remaining (connection-based) messages in receive queue */ |
Ying Xue | 57467e5 | 2013-01-20 23:30:08 +0100 | [diff] [blame] | 529 | __skb_queue_purge(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 530 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 531 | /* Reject any messages that accumulated in backlog queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 532 | sock->state = SS_DISCONNECTING; |
| 533 | release_sock(sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 534 | |
| 535 | call_rcu(&tsk->rcu, tipc_sk_callback); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 536 | sock->sk = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 537 | |
Geert Uytterhoeven | 065d7e3 | 2014-04-06 15:56:14 +0200 | [diff] [blame] | 538 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 542 | * tipc_bind - associate or disassocate TIPC name(s) with a socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 543 | * @sock: socket structure |
| 544 | * @uaddr: socket address describing name(s) and desired operation |
| 545 | * @uaddr_len: size of socket address data structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 546 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 547 | * Name and name sequence binding is indicated using a positive scope value; |
| 548 | * a negative scope value unbinds the specified name. Specifying no name |
| 549 | * (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] | 550 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 551 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 552 | * |
| 553 | * NOTE: This routine doesn't need to take the socket lock since it doesn't |
| 554 | * access any non-constant socket information. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 555 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 556 | static int tipc_bind(struct socket *sock, struct sockaddr *uaddr, |
| 557 | int uaddr_len) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 558 | { |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 559 | struct sock *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 560 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 561 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 562 | int res = -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 563 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 564 | lock_sock(sk); |
| 565 | if (unlikely(!uaddr_len)) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 566 | res = tipc_sk_withdraw(tsk, 0, NULL); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 567 | goto exit; |
| 568 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 569 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 570 | if (uaddr_len < sizeof(struct sockaddr_tipc)) { |
| 571 | res = -EINVAL; |
| 572 | goto exit; |
| 573 | } |
| 574 | if (addr->family != AF_TIPC) { |
| 575 | res = -EAFNOSUPPORT; |
| 576 | goto exit; |
| 577 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 578 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 579 | if (addr->addrtype == TIPC_ADDR_NAME) |
| 580 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 581 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { |
| 582 | res = -EAFNOSUPPORT; |
| 583 | goto exit; |
| 584 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 585 | |
Ying Xue | 13a2e89 | 2013-06-17 10:54:40 -0400 | [diff] [blame] | 586 | if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && |
Ying Xue | 7d0ab17 | 2013-06-17 10:54:41 -0400 | [diff] [blame] | 587 | (addr->addr.nameseq.type != TIPC_TOP_SRV) && |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 588 | (addr->addr.nameseq.type != TIPC_CFG_SRV)) { |
| 589 | res = -EACCES; |
| 590 | goto exit; |
| 591 | } |
Allan Stephens | c422f1b | 2011-11-02 15:49:40 -0400 | [diff] [blame] | 592 | |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 593 | res = (addr->scope > 0) ? |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 594 | tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) : |
| 595 | tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 596 | exit: |
| 597 | release_sock(sk); |
| 598 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 599 | } |
| 600 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 601 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 602 | * tipc_getname - get port ID of socket or peer socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 603 | * @sock: socket structure |
| 604 | * @uaddr: area for returned socket address |
| 605 | * @uaddr_len: area for returned length of socket address |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 606 | * @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] | 607 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 608 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 609 | * |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 610 | * NOTE: This routine doesn't need to take the socket lock since it only |
| 611 | * accesses socket information that is unchanging (or which changes in |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 612 | * a completely predictable manner). |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 613 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 614 | static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, |
| 615 | int *uaddr_len, int peer) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 616 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 617 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 618 | struct tipc_sock *tsk = tipc_sk(sock->sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 619 | |
Kulikov Vasiliy | 88f8a5e | 2010-10-31 07:10:32 +0000 | [diff] [blame] | 620 | memset(addr, 0, sizeof(*addr)); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 621 | if (peer) { |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 622 | if ((sock->state != SS_CONNECTED) && |
| 623 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) |
| 624 | return -ENOTCONN; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 625 | addr->addr.id.ref = tsk_peer_port(tsk); |
| 626 | addr->addr.id.node = tsk_peer_node(tsk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 627 | } else { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 628 | addr->addr.id.ref = tsk->portid; |
Allan Stephens | b924dcf | 2010-11-30 12:01:03 +0000 | [diff] [blame] | 629 | addr->addr.id.node = tipc_own_addr; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 630 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 631 | |
| 632 | *uaddr_len = sizeof(*addr); |
| 633 | addr->addrtype = TIPC_ADDR_ID; |
| 634 | addr->family = AF_TIPC; |
| 635 | addr->scope = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 636 | addr->addr.name.domain = 0; |
| 637 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 638 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 642 | * tipc_poll - read and possibly block on pollmask |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 643 | * @file: file structure associated with the socket |
| 644 | * @sock: socket for which to calculate the poll bits |
| 645 | * @wait: ??? |
| 646 | * |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 647 | * Returns pollmask value |
| 648 | * |
| 649 | * COMMENTARY: |
| 650 | * It appears that the usual socket locking mechanisms are not useful here |
| 651 | * since the pollmask info is potentially out-of-date the moment this routine |
| 652 | * exits. TCP and other protocols seem to rely on higher level poll routines |
| 653 | * to handle any preventable race conditions, so TIPC will do the same ... |
| 654 | * |
| 655 | * TIPC sets the returned events as follows: |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 656 | * |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 657 | * socket state flags set |
| 658 | * ------------ --------- |
| 659 | * unconnected no read flags |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 660 | * POLLOUT if port is not congested |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 661 | * |
| 662 | * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue |
| 663 | * no write flags |
| 664 | * |
| 665 | * connected POLLIN/POLLRDNORM if data in rx queue |
| 666 | * POLLOUT if port is not congested |
| 667 | * |
| 668 | * disconnecting POLLIN/POLLRDNORM/POLLHUP |
| 669 | * no write flags |
| 670 | * |
| 671 | * listening POLLIN if SYN in rx queue |
| 672 | * no write flags |
| 673 | * |
| 674 | * ready POLLIN/POLLRDNORM if data in rx queue |
| 675 | * [connectionless] POLLOUT (since port cannot be congested) |
| 676 | * |
| 677 | * IMPORTANT: The fact that a read or write operation is indicated does NOT |
| 678 | * imply that the operation will succeed, merely that it should be performed |
| 679 | * and will not block. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 680 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 681 | static unsigned int tipc_poll(struct file *file, struct socket *sock, |
| 682 | poll_table *wait) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 683 | { |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 684 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 685 | struct tipc_sock *tsk = tipc_sk(sk); |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 686 | u32 mask = 0; |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 687 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 688 | sock_poll_wait(file, sk_sleep(sk), wait); |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 689 | |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 690 | switch ((int)sock->state) { |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 691 | case SS_UNCONNECTED: |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 692 | if (!tsk->link_cong) |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 693 | mask |= POLLOUT; |
| 694 | break; |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 695 | case SS_READY: |
| 696 | case SS_CONNECTED: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 697 | if (!tsk->link_cong && !tsk_conn_cong(tsk)) |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 698 | mask |= POLLOUT; |
| 699 | /* fall thru' */ |
| 700 | case SS_CONNECTING: |
| 701 | case SS_LISTENING: |
| 702 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 703 | mask |= (POLLIN | POLLRDNORM); |
| 704 | break; |
| 705 | case SS_DISCONNECTING: |
| 706 | mask = (POLLIN | POLLRDNORM | POLLHUP); |
| 707 | break; |
| 708 | } |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 709 | |
| 710 | return mask; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 711 | } |
| 712 | |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 713 | /** |
| 714 | * tipc_sendmcast - send multicast message |
| 715 | * @sock: socket structure |
| 716 | * @seq: destination address |
Al Viro | 562640f | 2014-11-15 01:13:43 -0500 | [diff] [blame] | 717 | * @msg: message to send |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 718 | * @dsz: total length of message data |
| 719 | * @timeo: timeout to wait for wakeup |
| 720 | * |
| 721 | * Called from function tipc_sendmsg(), which has done all sanity checks |
| 722 | * Returns the number of bytes sent on success, or errno |
| 723 | */ |
| 724 | static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq, |
Al Viro | 562640f | 2014-11-15 01:13:43 -0500 | [diff] [blame] | 725 | struct msghdr *msg, size_t dsz, long timeo) |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 726 | { |
| 727 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 728 | struct tipc_msg *mhdr = &tipc_sk(sk)->phdr; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 729 | struct sk_buff_head head; |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 730 | uint mtu; |
| 731 | int rc; |
| 732 | |
| 733 | msg_set_type(mhdr, TIPC_MCAST_MSG); |
| 734 | msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE); |
| 735 | msg_set_destport(mhdr, 0); |
| 736 | msg_set_destnode(mhdr, 0); |
| 737 | msg_set_nametype(mhdr, seq->type); |
| 738 | msg_set_namelower(mhdr, seq->lower); |
| 739 | msg_set_nameupper(mhdr, seq->upper); |
| 740 | msg_set_hdr_sz(mhdr, MCAST_H_SIZE); |
| 741 | |
| 742 | new_mtu: |
| 743 | mtu = tipc_bclink_get_mtu(); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 744 | __skb_queue_head_init(&head); |
| 745 | rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head); |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 746 | if (unlikely(rc < 0)) |
| 747 | return rc; |
| 748 | |
| 749 | do { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 750 | rc = tipc_bclink_xmit(&head); |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 751 | if (likely(rc >= 0)) { |
| 752 | rc = dsz; |
| 753 | break; |
| 754 | } |
| 755 | if (rc == -EMSGSIZE) |
| 756 | goto new_mtu; |
| 757 | if (rc != -ELINKCONG) |
| 758 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 759 | tipc_sk(sk)->link_cong = 1; |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 760 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
| 761 | if (rc) |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 762 | __skb_queue_purge(&head); |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 763 | } while (!rc); |
| 764 | return rc; |
| 765 | } |
| 766 | |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 767 | /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets |
| 768 | */ |
| 769 | void tipc_sk_mcast_rcv(struct sk_buff *buf) |
| 770 | { |
| 771 | struct tipc_msg *msg = buf_msg(buf); |
| 772 | struct tipc_port_list dports = {0, NULL, }; |
| 773 | struct tipc_port_list *item; |
| 774 | struct sk_buff *b; |
| 775 | uint i, last, dst = 0; |
| 776 | u32 scope = TIPC_CLUSTER_SCOPE; |
| 777 | |
| 778 | if (in_own_node(msg_orignode(msg))) |
| 779 | scope = TIPC_NODE_SCOPE; |
| 780 | |
| 781 | /* Create destination port list: */ |
| 782 | tipc_nametbl_mc_translate(msg_nametype(msg), |
| 783 | msg_namelower(msg), |
| 784 | msg_nameupper(msg), |
| 785 | scope, |
| 786 | &dports); |
| 787 | last = dports.count; |
| 788 | if (!last) { |
| 789 | kfree_skb(buf); |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | for (item = &dports; item; item = item->next) { |
| 794 | for (i = 0; i < PLSIZE && ++dst <= last; i++) { |
| 795 | b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf; |
| 796 | if (!b) { |
| 797 | pr_warn("Failed do clone mcast rcv buffer\n"); |
| 798 | continue; |
| 799 | } |
| 800 | msg_set_destport(msg, item->ports[i]); |
| 801 | tipc_sk_rcv(b); |
| 802 | } |
| 803 | } |
| 804 | tipc_port_list_free(&dports); |
| 805 | } |
| 806 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 807 | /** |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 808 | * tipc_sk_proto_rcv - receive a connection mng protocol message |
| 809 | * @tsk: receiving socket |
| 810 | * @dnode: node to send response message to, if any |
| 811 | * @buf: buffer containing protocol message |
| 812 | * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if |
| 813 | * (CONN_PROBE_REPLY) message should be forwarded. |
| 814 | */ |
Wei Yongjun | 52f50ce | 2014-07-20 13:14:28 +0800 | [diff] [blame] | 815 | static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, |
| 816 | struct sk_buff *buf) |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 817 | { |
| 818 | struct tipc_msg *msg = buf_msg(buf); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 819 | int conn_cong; |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 820 | |
| 821 | /* Ignore if connection cannot be validated: */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 822 | if (!tsk_peer_msg(tsk, msg)) |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 823 | goto exit; |
| 824 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 825 | tsk->probing_state = TIPC_CONN_OK; |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 826 | |
| 827 | if (msg_type(msg) == CONN_ACK) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 828 | conn_cong = tsk_conn_cong(tsk); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 829 | tsk->sent_unacked -= msg_msgcnt(msg); |
| 830 | if (conn_cong) |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 831 | tsk->sk.sk_write_space(&tsk->sk); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 832 | } else if (msg_type(msg) == CONN_PROBE) { |
| 833 | if (!tipc_msg_reverse(buf, dnode, TIPC_OK)) |
| 834 | return TIPC_OK; |
| 835 | msg_set_type(msg, CONN_PROBE_REPLY); |
| 836 | return TIPC_FWD_MSG; |
| 837 | } |
| 838 | /* Do nothing if msg_type() == CONN_PROBE_REPLY */ |
| 839 | exit: |
| 840 | kfree_skb(buf); |
| 841 | return TIPC_OK; |
| 842 | } |
| 843 | |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 844 | static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) |
| 845 | { |
| 846 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 847 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 848 | DEFINE_WAIT(wait); |
| 849 | int done; |
| 850 | |
| 851 | do { |
| 852 | int err = sock_error(sk); |
| 853 | if (err) |
| 854 | return err; |
| 855 | if (sock->state == SS_DISCONNECTING) |
| 856 | return -EPIPE; |
| 857 | if (!*timeo_p) |
| 858 | return -EAGAIN; |
| 859 | if (signal_pending(current)) |
| 860 | return sock_intr_errno(*timeo_p); |
| 861 | |
| 862 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 863 | done = sk_wait_event(sk, timeo_p, !tsk->link_cong); |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 864 | finish_wait(sk_sleep(sk), &wait); |
| 865 | } while (!done); |
| 866 | return 0; |
| 867 | } |
| 868 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 869 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 870 | * tipc_sendmsg - send message in connectionless manner |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 871 | * @iocb: if NULL, indicates that socket lock is already held |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 872 | * @sock: socket structure |
| 873 | * @m: message to send |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 874 | * @dsz: amount of user data to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 875 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 876 | * Message must have an destination specified explicitly. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 877 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 878 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 879 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 880 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 881 | * Returns the number of bytes sent on success, or errno otherwise |
| 882 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 883 | static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 884 | struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 885 | { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 886 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 887 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 888 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 889 | struct tipc_msg *mhdr = &tsk->phdr; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 890 | u32 dnode, dport; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 891 | struct sk_buff_head head; |
| 892 | struct sk_buff *skb; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 893 | struct tipc_name_seq *seq = &dest->addr.nameseq; |
| 894 | u32 mtu; |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 895 | long timeo; |
Erik Hugne | 88b17b6 | 2014-12-03 14:44:44 +0100 | [diff] [blame] | 896 | int rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 897 | |
| 898 | if (unlikely(!dest)) |
| 899 | return -EDESTADDRREQ; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 900 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 901 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
| 902 | (dest->family != AF_TIPC))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 903 | return -EINVAL; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 904 | |
| 905 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 906 | return -EMSGSIZE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 907 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 908 | if (iocb) |
| 909 | lock_sock(sk); |
| 910 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 911 | if (unlikely(sock->state != SS_READY)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 912 | if (sock->state == SS_LISTENING) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 913 | rc = -EPIPE; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 914 | goto exit; |
Allan Stephens | 3388007 | 2006-06-25 23:44:57 -0700 | [diff] [blame] | 915 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 916 | if (sock->state != SS_UNCONNECTED) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 917 | rc = -EISCONN; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 918 | goto exit; |
| 919 | } |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 920 | if (tsk->published) { |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 921 | rc = -EOPNOTSUPP; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 922 | goto exit; |
| 923 | } |
| 924 | if (dest->addrtype == TIPC_ADDR_NAME) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 925 | tsk->conn_type = dest->addr.name.name.type; |
| 926 | tsk->conn_instance = dest->addr.name.name.instance; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 927 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Ying Xue | 3f40504 | 2014-01-17 09:50:05 +0800 | [diff] [blame] | 930 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 931 | |
| 932 | if (dest->addrtype == TIPC_ADDR_MCAST) { |
Al Viro | 562640f | 2014-11-15 01:13:43 -0500 | [diff] [blame] | 933 | rc = tipc_sendmcast(sock, seq, m, dsz, timeo); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 934 | goto exit; |
| 935 | } else if (dest->addrtype == TIPC_ADDR_NAME) { |
| 936 | u32 type = dest->addr.name.name.type; |
| 937 | u32 inst = dest->addr.name.name.instance; |
| 938 | u32 domain = dest->addr.name.domain; |
| 939 | |
| 940 | dnode = domain; |
| 941 | msg_set_type(mhdr, TIPC_NAMED_MSG); |
| 942 | msg_set_hdr_sz(mhdr, NAMED_H_SIZE); |
| 943 | msg_set_nametype(mhdr, type); |
| 944 | msg_set_nameinst(mhdr, inst); |
| 945 | msg_set_lookup_scope(mhdr, tipc_addr_scope(domain)); |
| 946 | dport = tipc_nametbl_translate(type, inst, &dnode); |
| 947 | msg_set_destnode(mhdr, dnode); |
| 948 | msg_set_destport(mhdr, dport); |
| 949 | if (unlikely(!dport && !dnode)) { |
| 950 | rc = -EHOSTUNREACH; |
| 951 | goto exit; |
| 952 | } |
| 953 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
| 954 | dnode = dest->addr.id.node; |
| 955 | msg_set_type(mhdr, TIPC_DIRECT_MSG); |
| 956 | msg_set_lookup_scope(mhdr, 0); |
| 957 | msg_set_destnode(mhdr, dnode); |
| 958 | msg_set_destport(mhdr, dest->addr.id.ref); |
| 959 | msg_set_hdr_sz(mhdr, BASIC_H_SIZE); |
| 960 | } |
| 961 | |
| 962 | new_mtu: |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 963 | mtu = tipc_node_get_mtu(dnode, tsk->portid); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 964 | __skb_queue_head_init(&head); |
| 965 | rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 966 | if (rc < 0) |
| 967 | goto exit; |
| 968 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 969 | do { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 970 | skb = skb_peek(&head); |
| 971 | TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 972 | rc = tipc_link_xmit(&head, dnode, tsk->portid); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 973 | if (likely(rc >= 0)) { |
| 974 | if (sock->state != SS_READY) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 975 | sock->state = SS_CONNECTING; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 976 | rc = dsz; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 977 | break; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 978 | } |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 979 | if (rc == -EMSGSIZE) |
| 980 | goto new_mtu; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 981 | if (rc != -ELINKCONG) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 982 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 983 | tsk->link_cong = 1; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 984 | rc = tipc_wait_for_sndmsg(sock, &timeo); |
Erik Hugne | 70452dc | 2014-07-06 20:38:50 -0400 | [diff] [blame] | 985 | if (rc) |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 986 | __skb_queue_purge(&head); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 987 | } while (!rc); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 988 | exit: |
| 989 | if (iocb) |
| 990 | release_sock(sk); |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 991 | |
| 992 | return rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 995 | static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) |
| 996 | { |
| 997 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 998 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 999 | DEFINE_WAIT(wait); |
| 1000 | int done; |
| 1001 | |
| 1002 | do { |
| 1003 | int err = sock_error(sk); |
| 1004 | if (err) |
| 1005 | return err; |
| 1006 | if (sock->state == SS_DISCONNECTING) |
| 1007 | return -EPIPE; |
| 1008 | else if (sock->state != SS_CONNECTED) |
| 1009 | return -ENOTCONN; |
| 1010 | if (!*timeo_p) |
| 1011 | return -EAGAIN; |
| 1012 | if (signal_pending(current)) |
| 1013 | return sock_intr_errno(*timeo_p); |
| 1014 | |
| 1015 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1016 | done = sk_wait_event(sk, timeo_p, |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1017 | (!tsk->link_cong && |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1018 | !tsk_conn_cong(tsk)) || |
| 1019 | !tsk->connected); |
Ying Xue | 391a6dd | 2014-01-17 09:50:06 +0800 | [diff] [blame] | 1020 | finish_wait(sk_sleep(sk), &wait); |
| 1021 | } while (!done); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1025 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1026 | * tipc_send_stream - send stream-oriented data |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1027 | * @iocb: (unused) |
| 1028 | * @sock: socket structure |
| 1029 | * @m: data to send |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1030 | * @dsz: total length of data to be transmitted |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1031 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1032 | * Used for SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1033 | * |
| 1034 | * Returns the number of bytes sent on success (or partial success), |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 1035 | * or errno if no data sent |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1036 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1037 | static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1038 | struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1039 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1040 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1041 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1042 | struct tipc_msg *mhdr = &tsk->phdr; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1043 | struct sk_buff_head head; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1044 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1045 | u32 portid = tsk->portid; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1046 | int rc = -EINVAL; |
| 1047 | long timeo; |
| 1048 | u32 dnode; |
| 1049 | uint mtu, send, sent = 0; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1050 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1051 | /* Handle implied connection establishment */ |
| 1052 | if (unlikely(dest)) { |
| 1053 | rc = tipc_sendmsg(iocb, sock, m, dsz); |
| 1054 | if (dsz && (dsz == rc)) |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1055 | tsk->sent_unacked = 1; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1056 | return rc; |
| 1057 | } |
| 1058 | if (dsz > (uint)INT_MAX) |
| 1059 | return -EMSGSIZE; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1060 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1061 | if (iocb) |
| 1062 | lock_sock(sk); |
| 1063 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1064 | if (unlikely(sock->state != SS_CONNECTED)) { |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1065 | if (sock->state == SS_DISCONNECTING) |
| 1066 | rc = -EPIPE; |
wangweidong | b055597 | 2013-12-27 10:09:39 +0800 | [diff] [blame] | 1067 | else |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1068 | rc = -ENOTCONN; |
wangweidong | 3b8401f | 2013-12-12 09:36:40 +0800 | [diff] [blame] | 1069 | goto exit; |
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 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1072 | timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1073 | dnode = tsk_peer_node(tsk); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1074 | |
| 1075 | next: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1076 | mtu = tsk->max_pkt; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1077 | send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1078 | __skb_queue_head_init(&head); |
| 1079 | rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1080 | if (unlikely(rc < 0)) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1081 | goto exit; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1082 | do { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1083 | if (likely(!tsk_conn_cong(tsk))) { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1084 | rc = tipc_link_xmit(&head, dnode, portid); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1085 | if (likely(!rc)) { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1086 | tsk->sent_unacked++; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1087 | sent += send; |
| 1088 | if (sent == dsz) |
| 1089 | break; |
| 1090 | goto next; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 1091 | } |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1092 | if (rc == -EMSGSIZE) { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1093 | tsk->max_pkt = tipc_node_get_mtu(dnode, portid); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1094 | goto next; |
| 1095 | } |
| 1096 | if (rc != -ELINKCONG) |
| 1097 | break; |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 1098 | tsk->link_cong = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1099 | } |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1100 | rc = tipc_wait_for_sndpkt(sock, &timeo); |
Erik Hugne | 70452dc | 2014-07-06 20:38:50 -0400 | [diff] [blame] | 1101 | if (rc) |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1102 | __skb_queue_purge(&head); |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1103 | } while (!rc); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1104 | exit: |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1105 | if (iocb) |
| 1106 | release_sock(sk); |
| 1107 | return sent ? sent : rc; |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * tipc_send_packet - send a connection-oriented message |
| 1112 | * @iocb: if NULL, indicates that socket lock is already held |
| 1113 | * @sock: socket structure |
| 1114 | * @m: message to send |
| 1115 | * @dsz: length of data to be transmitted |
| 1116 | * |
| 1117 | * Used for SOCK_SEQPACKET messages. |
| 1118 | * |
| 1119 | * Returns the number of bytes sent on success, or errno otherwise |
| 1120 | */ |
| 1121 | static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, |
| 1122 | struct msghdr *m, size_t dsz) |
| 1123 | { |
| 1124 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
| 1125 | return -EMSGSIZE; |
| 1126 | |
| 1127 | return tipc_send_stream(iocb, sock, m, dsz); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1130 | /* tipc_sk_finish_conn - complete the setup of a connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1131 | */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1132 | static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1133 | u32 peer_node) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1134 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1135 | struct tipc_msg *msg = &tsk->phdr; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1136 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1137 | msg_set_destnode(msg, peer_node); |
| 1138 | msg_set_destport(msg, peer_port); |
| 1139 | msg_set_type(msg, TIPC_CONN_MSG); |
| 1140 | msg_set_lookup_scope(msg, 0); |
| 1141 | msg_set_hdr_sz(msg, SHORT_H_SIZE); |
Jon Paul Maloy | f9fef18 | 2014-03-12 11:31:08 -0400 | [diff] [blame] | 1142 | |
Ying Xue | 2f55c43 | 2015-01-09 15:27:00 +0800 | [diff] [blame] | 1143 | tsk->probing_intv = CONN_PROBING_INTERVAL; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1144 | tsk->probing_state = TIPC_CONN_OK; |
| 1145 | tsk->connected = 1; |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 1146 | if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv)) |
| 1147 | sock_hold(&tsk->sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1148 | tipc_node_add_conn(peer_node, tsk->portid, peer_port); |
| 1149 | tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->portid); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * set_orig_addr - capture sender's address for received message |
| 1154 | * @m: descriptor for message info |
| 1155 | * @msg: received message header |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1156 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1157 | * Note: Address is not captured if not requested by receiver. |
| 1158 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 1159 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1160 | { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1161 | DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1162 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1163 | if (addr) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1164 | addr->family = AF_TIPC; |
| 1165 | addr->addrtype = TIPC_ADDR_ID; |
Mathias Krause | 60085c3 | 2013-04-07 01:52:00 +0000 | [diff] [blame] | 1166 | memset(&addr->addr, 0, sizeof(addr->addr)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1167 | addr->addr.id.ref = msg_origport(msg); |
| 1168 | addr->addr.id.node = msg_orignode(msg); |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1169 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
| 1170 | addr->scope = 0; /* could leave uninitialized */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1171 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /** |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1176 | * tipc_sk_anc_data_recv - optionally capture ancillary data for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1177 | * @m: descriptor for message info |
| 1178 | * @msg: received message header |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1179 | * @tsk: TIPC port associated with message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1180 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1181 | * Note: Ancillary data is not captured if not requested by receiver. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1182 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1183 | * Returns 0 if successful, otherwise errno |
| 1184 | */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1185 | static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
| 1186 | struct tipc_sock *tsk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1187 | { |
| 1188 | u32 anc_data[3]; |
| 1189 | u32 err; |
| 1190 | u32 dest_type; |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1191 | int has_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1192 | int res; |
| 1193 | |
| 1194 | if (likely(m->msg_controllen == 0)) |
| 1195 | return 0; |
| 1196 | |
| 1197 | /* Optionally capture errored message object(s) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1198 | err = msg ? msg_errcode(msg) : 0; |
| 1199 | if (unlikely(err)) { |
| 1200 | anc_data[0] = err; |
| 1201 | anc_data[1] = msg_data_sz(msg); |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1202 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
| 1203 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1204 | return res; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1205 | if (anc_data[1]) { |
| 1206 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], |
| 1207 | msg_data(msg)); |
| 1208 | if (res) |
| 1209 | return res; |
| 1210 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* Optionally capture message destination object */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1214 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
| 1215 | switch (dest_type) { |
| 1216 | case TIPC_NAMED_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1217 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1218 | anc_data[0] = msg_nametype(msg); |
| 1219 | anc_data[1] = msg_namelower(msg); |
| 1220 | anc_data[2] = msg_namelower(msg); |
| 1221 | break; |
| 1222 | case TIPC_MCAST_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1223 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1224 | anc_data[0] = msg_nametype(msg); |
| 1225 | anc_data[1] = msg_namelower(msg); |
| 1226 | anc_data[2] = msg_nameupper(msg); |
| 1227 | break; |
| 1228 | case TIPC_CONN_MSG: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1229 | has_name = (tsk->conn_type != 0); |
| 1230 | anc_data[0] = tsk->conn_type; |
| 1231 | anc_data[1] = tsk->conn_instance; |
| 1232 | anc_data[2] = tsk->conn_instance; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1233 | break; |
| 1234 | default: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 1235 | has_name = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1236 | } |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1237 | if (has_name) { |
| 1238 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); |
| 1239 | if (res) |
| 1240 | return res; |
| 1241 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1246 | static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack) |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1247 | { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1248 | struct sk_buff *skb = NULL; |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1249 | struct tipc_msg *msg; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1250 | u32 peer_port = tsk_peer_port(tsk); |
| 1251 | u32 dnode = tsk_peer_node(tsk); |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1252 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1253 | if (!tsk->connected) |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1254 | return; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1255 | skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode, |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1256 | tipc_own_addr, peer_port, tsk->portid, TIPC_OK); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1257 | if (!skb) |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1258 | return; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1259 | msg = buf_msg(skb); |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1260 | msg_set_msgcnt(msg, ack); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1261 | tipc_link_xmit_skb(skb, dnode, msg_link_selector(msg)); |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1264 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1265 | { |
| 1266 | struct sock *sk = sock->sk; |
| 1267 | DEFINE_WAIT(wait); |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1268 | long timeo = *timeop; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1269 | int err; |
| 1270 | |
| 1271 | for (;;) { |
| 1272 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 1273 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1274 | if (sock->state == SS_DISCONNECTING) { |
| 1275 | err = -ENOTCONN; |
| 1276 | break; |
| 1277 | } |
| 1278 | release_sock(sk); |
| 1279 | timeo = schedule_timeout(timeo); |
| 1280 | lock_sock(sk); |
| 1281 | } |
| 1282 | err = 0; |
| 1283 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 1284 | break; |
| 1285 | err = sock_intr_errno(timeo); |
| 1286 | if (signal_pending(current)) |
| 1287 | break; |
| 1288 | err = -EAGAIN; |
| 1289 | if (!timeo) |
| 1290 | break; |
| 1291 | } |
| 1292 | finish_wait(sk_sleep(sk), &wait); |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1293 | *timeop = timeo; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1294 | return err; |
| 1295 | } |
| 1296 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1297 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1298 | * tipc_recvmsg - receive packet-oriented message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1299 | * @iocb: (unused) |
| 1300 | * @m: descriptor for message info |
| 1301 | * @buf_len: total size of user buffer area |
| 1302 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1303 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1304 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 1305 | * If the complete message doesn't fit in user area, truncate it. |
| 1306 | * |
| 1307 | * Returns size of returned message data, errno otherwise |
| 1308 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1309 | static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1310 | struct msghdr *m, size_t buf_len, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1311 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1312 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1313 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1314 | struct sk_buff *buf; |
| 1315 | struct tipc_msg *msg; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1316 | long timeo; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1317 | unsigned int sz; |
| 1318 | u32 err; |
| 1319 | int res; |
| 1320 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1321 | /* Catch invalid receive requests */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1322 | if (unlikely(!buf_len)) |
| 1323 | return -EINVAL; |
| 1324 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1325 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1326 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1327 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1328 | res = -ENOTCONN; |
| 1329 | goto exit; |
| 1330 | } |
| 1331 | |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1332 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1333 | restart: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1334 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1335 | /* Look for a message in receive queue; wait if necessary */ |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1336 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1337 | if (res) |
| 1338 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1339 | |
| 1340 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1341 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1342 | msg = buf_msg(buf); |
| 1343 | sz = msg_data_sz(msg); |
| 1344 | err = msg_errcode(msg); |
| 1345 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1346 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1347 | if ((!sz) && (!err)) { |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 1348 | tsk_advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1349 | goto restart; |
| 1350 | } |
| 1351 | |
| 1352 | /* Capture sender's address (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1353 | set_orig_addr(m, msg); |
| 1354 | |
| 1355 | /* Capture ancillary data (optional) */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1356 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1357 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1358 | goto exit; |
| 1359 | |
| 1360 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1361 | if (!err) { |
| 1362 | if (unlikely(buf_len < sz)) { |
| 1363 | sz = buf_len; |
| 1364 | m->msg_flags |= MSG_TRUNC; |
| 1365 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1366 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz); |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1367 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1368 | goto exit; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1369 | res = sz; |
| 1370 | } else { |
| 1371 | if ((sock->state == SS_READY) || |
| 1372 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) |
| 1373 | res = 0; |
| 1374 | else |
| 1375 | res = -ECONNRESET; |
| 1376 | } |
| 1377 | |
| 1378 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1379 | if (likely(!(flags & MSG_PEEK))) { |
Allan Stephens | 9900980 | 2008-04-15 00:06:12 -0700 | [diff] [blame] | 1380 | if ((sock->state != SS_READY) && |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1381 | (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1382 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1383 | tsk->rcv_unacked = 0; |
| 1384 | } |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 1385 | tsk_advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1386 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1387 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1388 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1389 | return res; |
| 1390 | } |
| 1391 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1392 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1393 | * tipc_recv_stream - receive stream-oriented data |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1394 | * @iocb: (unused) |
| 1395 | * @m: descriptor for message info |
| 1396 | * @buf_len: total size of user buffer area |
| 1397 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1398 | * |
| 1399 | * Used for SOCK_STREAM messages only. If not enough data is available |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1400 | * will optionally wait for more; never truncates data. |
| 1401 | * |
| 1402 | * Returns size of returned message data, errno otherwise |
| 1403 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1404 | static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, |
| 1405 | struct msghdr *m, size_t buf_len, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1406 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1407 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1408 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1409 | struct sk_buff *buf; |
| 1410 | struct tipc_msg *msg; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1411 | long timeo; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1412 | unsigned int sz; |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1413 | int sz_to_copy, target, needed; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1414 | int sz_copied = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1415 | u32 err; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1416 | int res = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1417 | |
| 1418 | /* Catch invalid receive attempts */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1419 | if (unlikely(!buf_len)) |
| 1420 | return -EINVAL; |
| 1421 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1422 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1423 | |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1424 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1425 | res = -ENOTCONN; |
| 1426 | goto exit; |
| 1427 | } |
| 1428 | |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1429 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1430 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 1431 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1432 | restart: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1433 | /* Look for a message in receive queue; wait if necessary */ |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1434 | res = tipc_wait_for_rcvmsg(sock, &timeo); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1435 | if (res) |
| 1436 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1437 | |
| 1438 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1439 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1440 | msg = buf_msg(buf); |
| 1441 | sz = msg_data_sz(msg); |
| 1442 | err = msg_errcode(msg); |
| 1443 | |
| 1444 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1445 | if ((!sz) && (!err)) { |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 1446 | tsk_advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1447 | goto restart; |
| 1448 | } |
| 1449 | |
| 1450 | /* Optionally capture sender's address & ancillary data of first msg */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1451 | if (sz_copied == 0) { |
| 1452 | set_orig_addr(m, msg); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1453 | res = tipc_sk_anc_data_recv(m, msg, tsk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1454 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1455 | goto exit; |
| 1456 | } |
| 1457 | |
| 1458 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1459 | if (!err) { |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1460 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1461 | |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1462 | sz -= offset; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1463 | needed = (buf_len - sz_copied); |
| 1464 | sz_to_copy = (sz <= needed) ? sz : needed; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1465 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1466 | res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset, |
| 1467 | m, sz_to_copy); |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1468 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1469 | goto exit; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1470 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1471 | sz_copied += sz_to_copy; |
| 1472 | |
| 1473 | if (sz_to_copy < sz) { |
| 1474 | if (!(flags & MSG_PEEK)) |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1475 | TIPC_SKB_CB(buf)->handle = |
| 1476 | (void *)(unsigned long)(offset + sz_to_copy); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1477 | goto exit; |
| 1478 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1479 | } else { |
| 1480 | if (sz_copied != 0) |
| 1481 | goto exit; /* can't add error msg to valid data */ |
| 1482 | |
| 1483 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) |
| 1484 | res = 0; |
| 1485 | else |
| 1486 | res = -ECONNRESET; |
| 1487 | } |
| 1488 | |
| 1489 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1490 | if (likely(!(flags & MSG_PEEK))) { |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1491 | if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1492 | tipc_sk_send_ack(tsk, tsk->rcv_unacked); |
Jon Paul Maloy | 6012052 | 2014-06-25 20:41:42 -0500 | [diff] [blame] | 1493 | tsk->rcv_unacked = 0; |
| 1494 | } |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 1495 | tsk_advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1496 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1497 | |
| 1498 | /* Loop around if more data is required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1499 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
| 1500 | (!skb_queue_empty(&sk->sk_receive_queue) || |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1501 | (sz_copied < target)) && /* and more is ready or required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1502 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
| 1503 | (!err)) /* and haven't reached a FIN */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1504 | goto restart; |
| 1505 | |
| 1506 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1507 | release_sock(sk); |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 1508 | return sz_copied ? sz_copied : res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /** |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1512 | * tipc_write_space - wake up thread if port congestion is released |
| 1513 | * @sk: socket |
| 1514 | */ |
| 1515 | static void tipc_write_space(struct sock *sk) |
| 1516 | { |
| 1517 | struct socket_wq *wq; |
| 1518 | |
| 1519 | rcu_read_lock(); |
| 1520 | wq = rcu_dereference(sk->sk_wq); |
| 1521 | if (wq_has_sleeper(wq)) |
| 1522 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | |
| 1523 | POLLWRNORM | POLLWRBAND); |
| 1524 | rcu_read_unlock(); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * tipc_data_ready - wake up threads to indicate messages have been received |
| 1529 | * @sk: socket |
| 1530 | * @len: the length of messages |
| 1531 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1532 | static void tipc_data_ready(struct sock *sk) |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1533 | { |
| 1534 | struct socket_wq *wq; |
| 1535 | |
| 1536 | rcu_read_lock(); |
| 1537 | wq = rcu_dereference(sk->sk_wq); |
| 1538 | if (wq_has_sleeper(wq)) |
| 1539 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
| 1540 | POLLRDNORM | POLLRDBAND); |
| 1541 | rcu_read_unlock(); |
| 1542 | } |
| 1543 | |
| 1544 | /** |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1545 | * filter_connect - Handle all incoming messages for a connection-based socket |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1546 | * @tsk: TIPC socket |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1547 | * @msg: message |
| 1548 | * |
stephen hemminger | b2ad5e5 | 2014-10-29 22:58:51 -0700 | [diff] [blame] | 1549 | * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1550 | */ |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1551 | static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf) |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1552 | { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1553 | struct sock *sk = &tsk->sk; |
Jon Paul Maloy | 8826cde | 2014-03-12 11:31:09 -0400 | [diff] [blame] | 1554 | struct socket *sock = sk->sk_socket; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1555 | struct tipc_msg *msg = buf_msg(*buf); |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1556 | int retval = -TIPC_ERR_NO_PORT; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1557 | |
| 1558 | if (msg_mcast(msg)) |
| 1559 | return retval; |
| 1560 | |
| 1561 | switch ((int)sock->state) { |
| 1562 | case SS_CONNECTED: |
| 1563 | /* Accept only connection-based messages sent by peer */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 1564 | if (tsk_peer_msg(tsk, msg)) { |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1565 | if (unlikely(msg_errcode(msg))) { |
| 1566 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1567 | tsk->connected = 0; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1568 | /* let timer expire on it's own */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1569 | tipc_node_remove_conn(tsk_peer_node(tsk), |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1570 | tsk->portid); |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1571 | } |
| 1572 | retval = TIPC_OK; |
| 1573 | } |
| 1574 | break; |
| 1575 | case SS_CONNECTING: |
| 1576 | /* Accept only ACK or NACK message */ |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1577 | |
| 1578 | if (unlikely(!msg_connected(msg))) |
| 1579 | break; |
| 1580 | |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1581 | if (unlikely(msg_errcode(msg))) { |
| 1582 | sock->state = SS_DISCONNECTING; |
Erik Hugne | 2c8d851 | 2013-08-28 09:29:58 +0200 | [diff] [blame] | 1583 | sk->sk_err = ECONNREFUSED; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1584 | retval = TIPC_OK; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1585 | break; |
| 1586 | } |
| 1587 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1588 | if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) { |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1589 | sock->state = SS_DISCONNECTING; |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1590 | sk->sk_err = EINVAL; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1591 | retval = TIPC_OK; |
| 1592 | break; |
| 1593 | } |
| 1594 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1595 | tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg)); |
| 1596 | msg_set_importance(&tsk->phdr, msg_importance(msg)); |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1597 | sock->state = SS_CONNECTED; |
| 1598 | |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1599 | /* If an incoming message is an 'ACK-', it should be |
| 1600 | * discarded here because it doesn't contain useful |
| 1601 | * data. In addition, we should try to wake up |
| 1602 | * connect() routine if sleeping. |
| 1603 | */ |
| 1604 | if (msg_data_sz(msg) == 0) { |
| 1605 | kfree_skb(*buf); |
| 1606 | *buf = NULL; |
| 1607 | if (waitqueue_active(sk_sleep(sk))) |
| 1608 | wake_up_interruptible(sk_sleep(sk)); |
| 1609 | } |
| 1610 | retval = TIPC_OK; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 1611 | break; |
| 1612 | case SS_LISTENING: |
| 1613 | case SS_UNCONNECTED: |
| 1614 | /* Accept only SYN message */ |
| 1615 | if (!msg_connected(msg) && !(msg_errcode(msg))) |
| 1616 | retval = TIPC_OK; |
| 1617 | break; |
| 1618 | case SS_DISCONNECTING: |
| 1619 | break; |
| 1620 | default: |
| 1621 | pr_err("Unknown socket state %u\n", sock->state); |
| 1622 | } |
| 1623 | return retval; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1627 | * rcvbuf_limit - get proper overload limit of socket receive queue |
| 1628 | * @sk: socket |
| 1629 | * @buf: message |
| 1630 | * |
| 1631 | * For all connection oriented messages, irrespective of importance, |
| 1632 | * the default overload value (i.e. 67MB) is set as limit. |
| 1633 | * |
| 1634 | * For all connectionless messages, by default new queue limits are |
| 1635 | * as belows: |
| 1636 | * |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 1637 | * TIPC_LOW_IMPORTANCE (4 MB) |
| 1638 | * TIPC_MEDIUM_IMPORTANCE (8 MB) |
| 1639 | * TIPC_HIGH_IMPORTANCE (16 MB) |
| 1640 | * TIPC_CRITICAL_IMPORTANCE (32 MB) |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1641 | * |
| 1642 | * Returns overload limit according to corresponding message importance |
| 1643 | */ |
| 1644 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf) |
| 1645 | { |
| 1646 | struct tipc_msg *msg = buf_msg(buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1647 | |
| 1648 | if (msg_connected(msg)) |
wangweidong | 0cee6bb | 2013-12-12 09:36:39 +0800 | [diff] [blame] | 1649 | return sysctl_tipc_rmem[2]; |
| 1650 | |
| 1651 | return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE << |
| 1652 | msg_importance(msg); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1656 | * filter_rcv - validate incoming message |
| 1657 | * @sk: socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1658 | * @buf: message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1659 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1660 | * Enqueues message on receive queue if acceptable; optionally handles |
| 1661 | * disconnect indication for a connected socket. |
| 1662 | * |
| 1663 | * Called with socket lock already taken; port lock may also be taken. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1664 | * |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1665 | * 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] | 1666 | * 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] | 1667 | */ |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1668 | static int filter_rcv(struct sock *sk, struct sk_buff *buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1669 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1670 | struct socket *sock = sk->sk_socket; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 1671 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1672 | struct tipc_msg *msg = buf_msg(buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1673 | unsigned int limit = rcvbuf_limit(sk, buf); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1674 | u32 onode; |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1675 | int rc = TIPC_OK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1676 | |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1677 | if (unlikely(msg_user(msg) == CONN_MANAGER)) |
| 1678 | return tipc_sk_proto_rcv(tsk, &onode, buf); |
Jon Paul Maloy | ec8a2e5 | 2014-06-25 20:41:40 -0500 | [diff] [blame] | 1679 | |
Jon Paul Maloy | 50100a5 | 2014-08-22 18:09:07 -0400 | [diff] [blame] | 1680 | if (unlikely(msg_user(msg) == SOCK_WAKEUP)) { |
| 1681 | kfree_skb(buf); |
| 1682 | tsk->link_cong = 0; |
| 1683 | sk->sk_write_space(sk); |
| 1684 | return TIPC_OK; |
| 1685 | } |
| 1686 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1687 | /* Reject message if it is wrong sort of message for socket */ |
Allan Stephens | aad5854 | 2012-04-26 18:13:08 -0400 | [diff] [blame] | 1688 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1689 | return -TIPC_ERR_NO_PORT; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1690 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1691 | if (sock->state == SS_READY) { |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1692 | if (msg_connected(msg)) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1693 | return -TIPC_ERR_NO_PORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1694 | } else { |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1695 | rc = filter_connect(tsk, &buf); |
| 1696 | if (rc != TIPC_OK || buf == NULL) |
| 1697 | return rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* Reject message if there isn't room to queue it */ |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1701 | if (sk_rmem_alloc_get(sk) + buf->truesize >= limit) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1702 | return -TIPC_ERR_OVERLOAD; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1703 | |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1704 | /* Enqueue message */ |
Ying Xue | 4068243 | 2013-10-18 07:23:16 +0200 | [diff] [blame] | 1705 | TIPC_SKB_CB(buf)->handle = NULL; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1706 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 1707 | skb_set_owner_r(buf, sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1708 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1709 | sk->sk_data_ready(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1710 | return TIPC_OK; |
| 1711 | } |
| 1712 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1713 | /** |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1714 | * tipc_backlog_rcv - handle incoming message from backlog queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1715 | * @sk: socket |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1716 | * @skb: message |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1717 | * |
| 1718 | * Caller must hold socket lock, but not port lock. |
| 1719 | * |
| 1720 | * Returns 0 |
| 1721 | */ |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1722 | static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1723 | { |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1724 | int rc; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1725 | u32 onode; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1726 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1727 | uint truesize = skb->truesize; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1728 | |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1729 | rc = filter_rcv(sk, skb); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1730 | |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1731 | if (likely(!rc)) { |
| 1732 | if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT) |
| 1733 | atomic_add(truesize, &tsk->dupl_rcvcnt); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1737 | if ((rc < 0) && !tipc_msg_reverse(skb, &onode, -rc)) |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1738 | return 0; |
| 1739 | |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1740 | tipc_link_xmit_skb(skb, onode, 0); |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1741 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | /** |
Jon Paul Maloy | 24be34b | 2014-03-12 11:31:10 -0400 | [diff] [blame] | 1746 | * tipc_sk_rcv - handle incoming message |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1747 | * @skb: buffer containing arriving message |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1748 | * Consumes buffer |
| 1749 | * Returns 0 if success, or errno: -EHOSTUNREACH |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1750 | */ |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1751 | int tipc_sk_rcv(struct sk_buff *skb) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1752 | { |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1753 | struct tipc_sock *tsk; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1754 | struct sock *sk; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1755 | u32 dport = msg_destport(buf_msg(skb)); |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1756 | int rc = TIPC_OK; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1757 | uint limit; |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1758 | u32 dnode; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1759 | |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1760 | /* Validate destination and message */ |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1761 | tsk = tipc_sk_lookup(dport); |
Jon Paul Maloy | 9b50fd0 | 2014-08-22 18:09:15 -0400 | [diff] [blame] | 1762 | if (unlikely(!tsk)) { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1763 | rc = tipc_msg_eval(skb, &dnode); |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1764 | goto exit; |
| 1765 | } |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1766 | sk = &tsk->sk; |
| 1767 | |
| 1768 | /* Queue message */ |
Ying Xue | 1a194c2 | 2014-10-20 14:46:35 +0800 | [diff] [blame] | 1769 | spin_lock_bh(&sk->sk_lock.slock); |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1770 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1771 | if (!sock_owned_by_user(sk)) { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1772 | rc = filter_rcv(sk, skb); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1773 | } else { |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 1774 | if (sk->sk_backlog.len == 0) |
| 1775 | atomic_set(&tsk->dupl_rcvcnt, 0); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1776 | limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt); |
| 1777 | if (sk_add_backlog(sk, skb, limit)) |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1778 | rc = -TIPC_ERR_OVERLOAD; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1779 | } |
Ying Xue | 1a194c2 | 2014-10-20 14:46:35 +0800 | [diff] [blame] | 1780 | spin_unlock_bh(&sk->sk_lock.slock); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 1781 | sock_put(sk); |
Jon Paul Maloy | e4de5fa | 2014-06-25 20:41:31 -0500 | [diff] [blame] | 1782 | if (likely(!rc)) |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 1783 | return 0; |
| 1784 | exit: |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1785 | if ((rc < 0) && !tipc_msg_reverse(skb, &dnode, -rc)) |
Jon Paul Maloy | 8db1bae | 2014-06-25 20:41:35 -0500 | [diff] [blame] | 1786 | return -EHOSTUNREACH; |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1787 | |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1788 | tipc_link_xmit_skb(skb, dnode, 0); |
Jon Paul Maloy | 5a37907 | 2014-06-25 20:41:36 -0500 | [diff] [blame] | 1789 | return (rc < 0) ? -EHOSTUNREACH : 0; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1792 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
| 1793 | { |
| 1794 | struct sock *sk = sock->sk; |
| 1795 | DEFINE_WAIT(wait); |
| 1796 | int done; |
| 1797 | |
| 1798 | do { |
| 1799 | int err = sock_error(sk); |
| 1800 | if (err) |
| 1801 | return err; |
| 1802 | if (!*timeo_p) |
| 1803 | return -ETIMEDOUT; |
| 1804 | if (signal_pending(current)) |
| 1805 | return sock_intr_errno(*timeo_p); |
| 1806 | |
| 1807 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 1808 | done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); |
| 1809 | finish_wait(sk_sleep(sk), &wait); |
| 1810 | } while (!done); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1814 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1815 | * tipc_connect - establish a connection to another TIPC port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1816 | * @sock: socket structure |
| 1817 | * @dest: socket address for destination port |
| 1818 | * @destlen: size of socket address data structure |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1819 | * @flags: file-related flags associated with socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1820 | * |
| 1821 | * Returns 0 on success, errno otherwise |
| 1822 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1823 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
| 1824 | int destlen, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1825 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1826 | struct sock *sk = sock->sk; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1827 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
| 1828 | struct msghdr m = {NULL,}; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1829 | long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; |
| 1830 | socket_state previous; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1831 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1832 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1833 | lock_sock(sk); |
| 1834 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1835 | /* 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] | 1836 | if (sock->state == SS_READY) { |
| 1837 | res = -EOPNOTSUPP; |
| 1838 | goto exit; |
| 1839 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1840 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1841 | /* |
| 1842 | * Reject connection attempt using multicast address |
| 1843 | * |
| 1844 | * Note: send_msg() validates the rest of the address fields, |
| 1845 | * so there's no need to do it here |
| 1846 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1847 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
| 1848 | res = -EINVAL; |
| 1849 | goto exit; |
| 1850 | } |
| 1851 | |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1852 | previous = sock->state; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1853 | switch (sock->state) { |
| 1854 | case SS_UNCONNECTED: |
| 1855 | /* Send a 'SYN-' to destination */ |
| 1856 | m.msg_name = dest; |
| 1857 | m.msg_namelen = destlen; |
| 1858 | |
| 1859 | /* If connect is in non-blocking case, set MSG_DONTWAIT to |
| 1860 | * indicate send_msg() is never blocked. |
| 1861 | */ |
| 1862 | if (!timeout) |
| 1863 | m.msg_flags = MSG_DONTWAIT; |
| 1864 | |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1865 | res = tipc_sendmsg(NULL, sock, &m, 0); |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1866 | if ((res < 0) && (res != -EWOULDBLOCK)) |
| 1867 | goto exit; |
| 1868 | |
| 1869 | /* Just entered SS_CONNECTING state; the only |
| 1870 | * difference is that return value in non-blocking |
| 1871 | * case is EINPROGRESS, rather than EALREADY. |
| 1872 | */ |
| 1873 | res = -EINPROGRESS; |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1874 | case SS_CONNECTING: |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1875 | if (previous == SS_CONNECTING) |
| 1876 | res = -EALREADY; |
| 1877 | if (!timeout) |
| 1878 | goto exit; |
| 1879 | timeout = msecs_to_jiffies(timeout); |
| 1880 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ |
| 1881 | res = tipc_wait_for_connect(sock, &timeout); |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 1882 | break; |
| 1883 | case SS_CONNECTED: |
| 1884 | res = -EISCONN; |
| 1885 | break; |
| 1886 | default: |
| 1887 | res = -EINVAL; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 1888 | break; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1889 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1890 | exit: |
| 1891 | release_sock(sk); |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1892 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1893 | } |
| 1894 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1895 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1896 | * tipc_listen - allow socket to listen for incoming connections |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1897 | * @sock: socket structure |
| 1898 | * @len: (unused) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1899 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1900 | * Returns 0 on success, errno otherwise |
| 1901 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1902 | static int tipc_listen(struct socket *sock, int len) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1903 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1904 | struct sock *sk = sock->sk; |
| 1905 | int res; |
| 1906 | |
| 1907 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1908 | |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 1909 | if (sock->state != SS_UNCONNECTED) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1910 | res = -EINVAL; |
| 1911 | else { |
| 1912 | sock->state = SS_LISTENING; |
| 1913 | res = 0; |
| 1914 | } |
| 1915 | |
| 1916 | release_sock(sk); |
| 1917 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1918 | } |
| 1919 | |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1920 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
| 1921 | { |
| 1922 | struct sock *sk = sock->sk; |
| 1923 | DEFINE_WAIT(wait); |
| 1924 | int err; |
| 1925 | |
| 1926 | /* True wake-one mechanism for incoming connections: only |
| 1927 | * one process gets woken up, not the 'whole herd'. |
| 1928 | * Since we do not 'race & poll' for established sockets |
| 1929 | * anymore, the common case will execute the loop only once. |
| 1930 | */ |
| 1931 | for (;;) { |
| 1932 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
| 1933 | TASK_INTERRUPTIBLE); |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 1934 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1935 | release_sock(sk); |
| 1936 | timeo = schedule_timeout(timeo); |
| 1937 | lock_sock(sk); |
| 1938 | } |
| 1939 | err = 0; |
| 1940 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 1941 | break; |
| 1942 | err = -EINVAL; |
| 1943 | if (sock->state != SS_LISTENING) |
| 1944 | break; |
| 1945 | err = sock_intr_errno(timeo); |
| 1946 | if (signal_pending(current)) |
| 1947 | break; |
| 1948 | err = -EAGAIN; |
| 1949 | if (!timeo) |
| 1950 | break; |
| 1951 | } |
| 1952 | finish_wait(sk_sleep(sk), &wait); |
| 1953 | return err; |
| 1954 | } |
| 1955 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1956 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1957 | * tipc_accept - wait for connection request |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1958 | * @sock: listening socket |
| 1959 | * @newsock: new socket that is to be connected |
| 1960 | * @flags: file-related flags associated with socket |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1961 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1962 | * Returns 0 on success, errno otherwise |
| 1963 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1964 | 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] | 1965 | { |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1966 | struct sock *new_sk, *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1967 | struct sk_buff *buf; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1968 | struct tipc_sock *new_tsock; |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1969 | struct tipc_msg *msg; |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1970 | long timeo; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1971 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1972 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1973 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1974 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1975 | if (sock->state != SS_LISTENING) { |
| 1976 | res = -EINVAL; |
| 1977 | goto exit; |
| 1978 | } |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 1979 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 1980 | res = tipc_wait_for_accept(sock, timeo); |
| 1981 | if (res) |
| 1982 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1983 | |
| 1984 | buf = skb_peek(&sk->sk_receive_queue); |
| 1985 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 1986 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1987 | if (res) |
| 1988 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1989 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1990 | new_sk = new_sock->sk; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1991 | new_tsock = tipc_sk(new_sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1992 | msg = buf_msg(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1993 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1994 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
| 1995 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1996 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 1997 | /* |
| 1998 | * Reject any stray messages received by new socket |
| 1999 | * before the socket lock was taken (very, very unlikely) |
| 2000 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 2001 | tsk_rej_rx_queue(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2002 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2003 | /* Connect new socket to it's peer */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2004 | tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2005 | new_sock->state = SS_CONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2006 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2007 | tsk_set_importance(new_tsock, msg_importance(msg)); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2008 | if (msg_named(msg)) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2009 | new_tsock->conn_type = msg_nametype(msg); |
| 2010 | new_tsock->conn_instance = msg_nameinst(msg); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2011 | } |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2012 | |
| 2013 | /* |
| 2014 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. |
| 2015 | * Respond to 'SYN+' by queuing it on new socket. |
| 2016 | */ |
| 2017 | if (!msg_data_sz(msg)) { |
| 2018 | struct msghdr m = {NULL,}; |
| 2019 | |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 2020 | tsk_advance_rx_queue(sk); |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2021 | tipc_send_packet(NULL, new_sock, &m, 0); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2022 | } else { |
| 2023 | __skb_dequeue(&sk->sk_receive_queue); |
| 2024 | __skb_queue_head(&new_sk->sk_receive_queue, buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2025 | skb_set_owner_r(buf, new_sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2026 | } |
| 2027 | release_sock(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2028 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2029 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2030 | return res; |
| 2031 | } |
| 2032 | |
| 2033 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2034 | * tipc_shutdown - shutdown socket connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2035 | * @sock: socket structure |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 2036 | * @how: direction to close (must be SHUT_RDWR) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2037 | * |
| 2038 | * Terminates connection (if necessary), then purges socket's receive queue. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2039 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2040 | * Returns 0 on success, errno otherwise |
| 2041 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2042 | static int tipc_shutdown(struct socket *sock, int how) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2043 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2044 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2045 | struct tipc_sock *tsk = tipc_sk(sk); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2046 | struct sk_buff *skb; |
Jon Paul Maloy | 80e44c2 | 2014-08-22 18:09:10 -0400 | [diff] [blame] | 2047 | u32 dnode; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2048 | int res; |
| 2049 | |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 2050 | if (how != SHUT_RDWR) |
| 2051 | return -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2052 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2053 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2054 | |
| 2055 | switch (sock->state) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2056 | case SS_CONNECTING: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2057 | case SS_CONNECTED: |
| 2058 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2059 | restart: |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 2060 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2061 | skb = __skb_dequeue(&sk->sk_receive_queue); |
| 2062 | if (skb) { |
| 2063 | if (TIPC_SKB_CB(skb)->handle != NULL) { |
| 2064 | kfree_skb(skb); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2065 | goto restart; |
| 2066 | } |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2067 | if (tipc_msg_reverse(skb, &dnode, TIPC_CONN_SHUTDOWN)) |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2068 | tipc_link_xmit_skb(skb, dnode, tsk->portid); |
| 2069 | tipc_node_remove_conn(dnode, tsk->portid); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2070 | } else { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2071 | dnode = tsk_peer_node(tsk); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2072 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
Jon Paul Maloy | 80e44c2 | 2014-08-22 18:09:10 -0400 | [diff] [blame] | 2073 | TIPC_CONN_MSG, SHORT_H_SIZE, |
| 2074 | 0, dnode, tipc_own_addr, |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2075 | tsk_peer_port(tsk), |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2076 | tsk->portid, TIPC_CONN_SHUTDOWN); |
| 2077 | tipc_link_xmit_skb(skb, dnode, tsk->portid); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2078 | } |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2079 | tsk->connected = 0; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2080 | sock->state = SS_DISCONNECTING; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2081 | tipc_node_remove_conn(dnode, tsk->portid); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2082 | /* fall through */ |
| 2083 | |
| 2084 | case SS_DISCONNECTING: |
| 2085 | |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 2086 | /* Discard any unreceived messages */ |
Ying Xue | 57467e5 | 2013-01-20 23:30:08 +0100 | [diff] [blame] | 2087 | __skb_queue_purge(&sk->sk_receive_queue); |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 2088 | |
| 2089 | /* Wake up anyone sleeping in poll */ |
| 2090 | sk->sk_state_change(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2091 | res = 0; |
| 2092 | break; |
| 2093 | |
| 2094 | default: |
| 2095 | res = -ENOTCONN; |
| 2096 | } |
| 2097 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2098 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2099 | return res; |
| 2100 | } |
| 2101 | |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2102 | static void tipc_sk_timeout(unsigned long data) |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2103 | { |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2104 | struct tipc_sock *tsk = (struct tipc_sock *)data; |
| 2105 | struct sock *sk = &tsk->sk; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2106 | struct sk_buff *skb = NULL; |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2107 | u32 peer_port, peer_node; |
| 2108 | |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2109 | bh_lock_sock(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2110 | if (!tsk->connected) { |
Jon Paul Maloy | 6c9808c | 2014-08-22 18:09:16 -0400 | [diff] [blame] | 2111 | bh_unlock_sock(sk); |
| 2112 | goto exit; |
| 2113 | } |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2114 | peer_port = tsk_peer_port(tsk); |
| 2115 | peer_node = tsk_peer_node(tsk); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2116 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2117 | if (tsk->probing_state == TIPC_CONN_PROBING) { |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2118 | /* Previous probe not answered -> self abort */ |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2119 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG, |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2120 | SHORT_H_SIZE, 0, tipc_own_addr, |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2121 | peer_node, tsk->portid, peer_port, |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2122 | TIPC_ERR_NO_PORT); |
| 2123 | } else { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2124 | skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2125 | 0, peer_node, tipc_own_addr, |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2126 | peer_port, tsk->portid, TIPC_OK); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2127 | tsk->probing_state = TIPC_CONN_PROBING; |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2128 | if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv)) |
| 2129 | sock_hold(sk); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2130 | } |
| 2131 | bh_unlock_sock(sk); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2132 | if (skb) |
Ying Xue | f2f2a96 | 2015-01-09 15:27:02 +0800 | [diff] [blame^] | 2133 | tipc_link_xmit_skb(skb, peer_node, tsk->portid); |
Jon Paul Maloy | 6c9808c | 2014-08-22 18:09:16 -0400 | [diff] [blame] | 2134 | exit: |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2135 | sock_put(sk); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2138 | static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2139 | struct tipc_name_seq const *seq) |
| 2140 | { |
| 2141 | struct publication *publ; |
| 2142 | u32 key; |
| 2143 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2144 | if (tsk->connected) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2145 | return -EINVAL; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2146 | key = tsk->portid + tsk->pub_count + 1; |
| 2147 | if (key == tsk->portid) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2148 | return -EADDRINUSE; |
| 2149 | |
| 2150 | publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper, |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2151 | scope, tsk->portid, key); |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2152 | if (unlikely(!publ)) |
| 2153 | return -EINVAL; |
| 2154 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2155 | list_add(&publ->pport_list, &tsk->publications); |
| 2156 | tsk->pub_count++; |
| 2157 | tsk->published = 1; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
| 2160 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2161 | static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2162 | struct tipc_name_seq const *seq) |
| 2163 | { |
| 2164 | struct publication *publ; |
| 2165 | struct publication *safe; |
| 2166 | int rc = -EINVAL; |
| 2167 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2168 | list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) { |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2169 | if (seq) { |
| 2170 | if (publ->scope != scope) |
| 2171 | continue; |
| 2172 | if (publ->type != seq->type) |
| 2173 | continue; |
| 2174 | if (publ->lower != seq->lower) |
| 2175 | continue; |
| 2176 | if (publ->upper != seq->upper) |
| 2177 | break; |
| 2178 | tipc_nametbl_withdraw(publ->type, publ->lower, |
| 2179 | publ->ref, publ->key); |
| 2180 | rc = 0; |
| 2181 | break; |
| 2182 | } |
| 2183 | tipc_nametbl_withdraw(publ->type, publ->lower, |
| 2184 | publ->ref, publ->key); |
| 2185 | rc = 0; |
| 2186 | } |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2187 | if (list_empty(&tsk->publications)) |
| 2188 | tsk->published = 0; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2189 | return rc; |
| 2190 | } |
| 2191 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2192 | static int tipc_sk_show(struct tipc_sock *tsk, char *buf, |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2193 | int len, int full_id) |
| 2194 | { |
| 2195 | struct publication *publ; |
| 2196 | int ret; |
| 2197 | |
| 2198 | if (full_id) |
| 2199 | ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:", |
| 2200 | tipc_zone(tipc_own_addr), |
| 2201 | tipc_cluster(tipc_own_addr), |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2202 | tipc_node(tipc_own_addr), tsk->portid); |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2203 | else |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2204 | ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid); |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2205 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2206 | if (tsk->connected) { |
| 2207 | u32 dport = tsk_peer_port(tsk); |
| 2208 | u32 destnode = tsk_peer_node(tsk); |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2209 | |
| 2210 | ret += tipc_snprintf(buf + ret, len - ret, |
| 2211 | " connected to <%u.%u.%u:%u>", |
| 2212 | tipc_zone(destnode), |
| 2213 | tipc_cluster(destnode), |
| 2214 | tipc_node(destnode), dport); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2215 | if (tsk->conn_type != 0) |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2216 | ret += tipc_snprintf(buf + ret, len - ret, |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2217 | " via {%u,%u}", tsk->conn_type, |
| 2218 | tsk->conn_instance); |
| 2219 | } else if (tsk->published) { |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2220 | ret += tipc_snprintf(buf + ret, len - ret, " bound to"); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2221 | list_for_each_entry(publ, &tsk->publications, pport_list) { |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2222 | if (publ->lower == publ->upper) |
| 2223 | ret += tipc_snprintf(buf + ret, len - ret, |
| 2224 | " {%u,%u}", publ->type, |
| 2225 | publ->lower); |
| 2226 | else |
| 2227 | ret += tipc_snprintf(buf + ret, len - ret, |
| 2228 | " {%u,%u,%u}", publ->type, |
| 2229 | publ->lower, publ->upper); |
| 2230 | } |
| 2231 | } |
| 2232 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); |
| 2233 | return ret; |
| 2234 | } |
| 2235 | |
| 2236 | struct sk_buff *tipc_sk_socks_show(void) |
| 2237 | { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2238 | const struct bucket_table *tbl; |
| 2239 | struct rhash_head *pos; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2240 | struct sk_buff *buf; |
| 2241 | struct tlv_desc *rep_tlv; |
| 2242 | char *pb; |
| 2243 | int pb_len; |
| 2244 | struct tipc_sock *tsk; |
| 2245 | int str_len = 0; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2246 | int i; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2247 | |
| 2248 | buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); |
| 2249 | if (!buf) |
| 2250 | return NULL; |
| 2251 | rep_tlv = (struct tlv_desc *)buf->data; |
| 2252 | pb = TLV_DATA(rep_tlv); |
| 2253 | pb_len = ULTRA_STRING_MAX_LEN; |
| 2254 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2255 | rcu_read_lock(); |
| 2256 | tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); |
| 2257 | for (i = 0; i < tbl->size; i++) { |
| 2258 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { |
| 2259 | spin_lock_bh(&tsk->sk.sk_lock.slock); |
| 2260 | str_len += tipc_sk_show(tsk, pb + str_len, |
| 2261 | pb_len - str_len, 0); |
| 2262 | spin_unlock_bh(&tsk->sk.sk_lock.slock); |
| 2263 | } |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2264 | } |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2265 | rcu_read_unlock(); |
| 2266 | |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2267 | str_len += 1; /* for "\0" */ |
| 2268 | skb_put(buf, TLV_SPACE(str_len)); |
| 2269 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); |
| 2270 | |
| 2271 | return buf; |
| 2272 | } |
| 2273 | |
| 2274 | /* tipc_sk_reinit: set non-zero address in all existing sockets |
| 2275 | * when we go from standalone to network mode. |
| 2276 | */ |
| 2277 | void tipc_sk_reinit(void) |
| 2278 | { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2279 | const struct bucket_table *tbl; |
| 2280 | struct rhash_head *pos; |
| 2281 | struct tipc_sock *tsk; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2282 | struct tipc_msg *msg; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2283 | int i; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2284 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2285 | rcu_read_lock(); |
| 2286 | tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); |
| 2287 | for (i = 0; i < tbl->size; i++) { |
| 2288 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { |
| 2289 | spin_lock_bh(&tsk->sk.sk_lock.slock); |
| 2290 | msg = &tsk->phdr; |
| 2291 | msg_set_prevnode(msg, tipc_own_addr); |
| 2292 | msg_set_orignode(msg, tipc_own_addr); |
| 2293 | spin_unlock_bh(&tsk->sk.sk_lock.slock); |
| 2294 | } |
| 2295 | } |
| 2296 | rcu_read_unlock(); |
| 2297 | } |
| 2298 | |
| 2299 | static struct tipc_sock *tipc_sk_lookup(u32 portid) |
| 2300 | { |
| 2301 | struct tipc_sock *tsk; |
| 2302 | |
| 2303 | rcu_read_lock(); |
| 2304 | tsk = rhashtable_lookup(&tipc_sk_rht, &portid); |
| 2305 | if (tsk) |
| 2306 | sock_hold(&tsk->sk); |
| 2307 | rcu_read_unlock(); |
| 2308 | |
| 2309 | return tsk; |
| 2310 | } |
| 2311 | |
| 2312 | static int tipc_sk_insert(struct tipc_sock *tsk) |
| 2313 | { |
| 2314 | u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1; |
| 2315 | u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT; |
| 2316 | |
| 2317 | while (remaining--) { |
| 2318 | portid++; |
| 2319 | if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT)) |
| 2320 | portid = TIPC_MIN_PORT; |
| 2321 | tsk->portid = portid; |
| 2322 | sock_hold(&tsk->sk); |
| 2323 | if (rhashtable_lookup_insert(&tipc_sk_rht, &tsk->node)) |
| 2324 | return 0; |
| 2325 | sock_put(&tsk->sk); |
| 2326 | } |
| 2327 | |
| 2328 | return -1; |
| 2329 | } |
| 2330 | |
| 2331 | static void tipc_sk_remove(struct tipc_sock *tsk) |
| 2332 | { |
| 2333 | struct sock *sk = &tsk->sk; |
| 2334 | |
| 2335 | if (rhashtable_remove(&tipc_sk_rht, &tsk->node)) { |
| 2336 | WARN_ON(atomic_read(&sk->sk_refcnt) == 1); |
| 2337 | __sock_put(sk); |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2341 | int tipc_sk_rht_init(void) |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2342 | { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2343 | struct rhashtable_params rht_params = { |
| 2344 | .nelem_hint = 192, |
| 2345 | .head_offset = offsetof(struct tipc_sock, node), |
| 2346 | .key_offset = offsetof(struct tipc_sock, portid), |
| 2347 | .key_len = sizeof(u32), /* portid */ |
| 2348 | .hashfn = jhash, |
| 2349 | .max_shift = 20, /* 1M */ |
| 2350 | .min_shift = 8, /* 256 */ |
| 2351 | .grow_decision = rht_grow_above_75, |
| 2352 | .shrink_decision = rht_shrink_below_30, |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2353 | }; |
| 2354 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2355 | return rhashtable_init(&tipc_sk_rht, &rht_params); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2356 | } |
| 2357 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2358 | void tipc_sk_rht_destroy(void) |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2359 | { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2360 | /* Wait for socket readers to complete */ |
| 2361 | synchronize_net(); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2362 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2363 | rhashtable_destroy(&tipc_sk_rht); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2367 | * tipc_setsockopt - set socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2368 | * @sock: socket structure |
| 2369 | * @lvl: option level |
| 2370 | * @opt: option identifier |
| 2371 | * @ov: pointer to new option value |
| 2372 | * @ol: length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2373 | * |
| 2374 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2375 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2376 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2377 | * Returns 0 on success, errno otherwise |
| 2378 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2379 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
| 2380 | char __user *ov, unsigned int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2381 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2382 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2383 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2384 | u32 value; |
| 2385 | int res; |
| 2386 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2387 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 2388 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2389 | if (lvl != SOL_TIPC) |
| 2390 | return -ENOPROTOOPT; |
| 2391 | if (ol < sizeof(value)) |
| 2392 | return -EINVAL; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 2393 | res = get_user(value, (u32 __user *)ov); |
| 2394 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2395 | return res; |
| 2396 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2397 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2398 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2399 | switch (opt) { |
| 2400 | case TIPC_IMPORTANCE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2401 | res = tsk_set_importance(tsk, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2402 | break; |
| 2403 | case TIPC_SRC_DROPPABLE: |
| 2404 | if (sock->type != SOCK_STREAM) |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2405 | tsk_set_unreliable(tsk, value); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2406 | else |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2407 | res = -ENOPROTOOPT; |
| 2408 | break; |
| 2409 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2410 | tsk_set_unreturnable(tsk, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2411 | break; |
| 2412 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 2413 | tipc_sk(sk)->conn_timeout = value; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2414 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2415 | break; |
| 2416 | default: |
| 2417 | res = -EINVAL; |
| 2418 | } |
| 2419 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2420 | release_sock(sk); |
| 2421 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2422 | return res; |
| 2423 | } |
| 2424 | |
| 2425 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2426 | * tipc_getsockopt - get socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2427 | * @sock: socket structure |
| 2428 | * @lvl: option level |
| 2429 | * @opt: option identifier |
| 2430 | * @ov: receptacle for option value |
| 2431 | * @ol: receptacle for length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2432 | * |
| 2433 | * 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] | 2434 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2435 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2436 | * Returns 0 on success, errno otherwise |
| 2437 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2438 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
| 2439 | char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2440 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2441 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2442 | struct tipc_sock *tsk = tipc_sk(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2443 | int len; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2444 | u32 value; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2445 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2446 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2447 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 2448 | return put_user(0, ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2449 | if (lvl != SOL_TIPC) |
| 2450 | return -ENOPROTOOPT; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 2451 | res = get_user(len, ol); |
| 2452 | if (res) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2453 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2454 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2455 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2456 | |
| 2457 | switch (opt) { |
| 2458 | case TIPC_IMPORTANCE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2459 | value = tsk_importance(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2460 | break; |
| 2461 | case TIPC_SRC_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2462 | value = tsk_unreliable(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2463 | break; |
| 2464 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2465 | value = tsk_unreturnable(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2466 | break; |
| 2467 | case TIPC_CONN_TIMEOUT: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2468 | value = tsk->conn_timeout; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2469 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2470 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2471 | case TIPC_NODE_RECVQ_DEPTH: |
Ying Xue | 9da3d47 | 2012-11-27 06:15:27 -0500 | [diff] [blame] | 2472 | value = 0; /* was tipc_queue_size, now obsolete */ |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 2473 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2474 | case TIPC_SOCK_RECVQ_DEPTH: |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 2475 | value = skb_queue_len(&sk->sk_receive_queue); |
| 2476 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2477 | default: |
| 2478 | res = -EINVAL; |
| 2479 | } |
| 2480 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2481 | release_sock(sk); |
| 2482 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 2483 | if (res) |
| 2484 | return res; /* "get" failed */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2485 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 2486 | if (len < sizeof(value)) |
| 2487 | return -EINVAL; |
| 2488 | |
| 2489 | if (copy_to_user(ov, &value, sizeof(value))) |
| 2490 | return -EFAULT; |
| 2491 | |
| 2492 | return put_user(sizeof(value), ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
Wei Yongjun | 52f50ce | 2014-07-20 13:14:28 +0800 | [diff] [blame] | 2495 | 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] | 2496 | { |
| 2497 | struct tipc_sioc_ln_req lnr; |
| 2498 | void __user *argp = (void __user *)arg; |
| 2499 | |
| 2500 | switch (cmd) { |
| 2501 | case SIOCGETLINKNAME: |
| 2502 | if (copy_from_user(&lnr, argp, sizeof(lnr))) |
| 2503 | return -EFAULT; |
Ying Xue | 7b8613e | 2014-10-20 14:44:25 +0800 | [diff] [blame] | 2504 | if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2505 | lnr.linkname, TIPC_MAX_LINK_NAME)) { |
| 2506 | if (copy_to_user(argp, &lnr, sizeof(lnr))) |
| 2507 | return -EFAULT; |
| 2508 | return 0; |
| 2509 | } |
| 2510 | return -EADDRNOTAVAIL; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2511 | default: |
| 2512 | return -ENOIOCTLCMD; |
| 2513 | } |
| 2514 | } |
| 2515 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 2516 | /* Protocol switches for the various types of TIPC sockets */ |
| 2517 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2518 | static const struct proto_ops msg_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2519 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2520 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2521 | .release = tipc_release, |
| 2522 | .bind = tipc_bind, |
| 2523 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2524 | .socketpair = sock_no_socketpair, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 2525 | .accept = sock_no_accept, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2526 | .getname = tipc_getname, |
| 2527 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2528 | .ioctl = tipc_ioctl, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 2529 | .listen = sock_no_listen, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2530 | .shutdown = tipc_shutdown, |
| 2531 | .setsockopt = tipc_setsockopt, |
| 2532 | .getsockopt = tipc_getsockopt, |
| 2533 | .sendmsg = tipc_sendmsg, |
| 2534 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2535 | .mmap = sock_no_mmap, |
| 2536 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2537 | }; |
| 2538 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2539 | static const struct proto_ops packet_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2540 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2541 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2542 | .release = tipc_release, |
| 2543 | .bind = tipc_bind, |
| 2544 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2545 | .socketpair = sock_no_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2546 | .accept = tipc_accept, |
| 2547 | .getname = tipc_getname, |
| 2548 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2549 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2550 | .listen = tipc_listen, |
| 2551 | .shutdown = tipc_shutdown, |
| 2552 | .setsockopt = tipc_setsockopt, |
| 2553 | .getsockopt = tipc_getsockopt, |
| 2554 | .sendmsg = tipc_send_packet, |
| 2555 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2556 | .mmap = sock_no_mmap, |
| 2557 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2558 | }; |
| 2559 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2560 | static const struct proto_ops stream_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2561 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2562 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2563 | .release = tipc_release, |
| 2564 | .bind = tipc_bind, |
| 2565 | .connect = tipc_connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 2566 | .socketpair = sock_no_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2567 | .accept = tipc_accept, |
| 2568 | .getname = tipc_getname, |
| 2569 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 2570 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2571 | .listen = tipc_listen, |
| 2572 | .shutdown = tipc_shutdown, |
| 2573 | .setsockopt = tipc_setsockopt, |
| 2574 | .getsockopt = tipc_getsockopt, |
| 2575 | .sendmsg = tipc_send_stream, |
| 2576 | .recvmsg = tipc_recv_stream, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 2577 | .mmap = sock_no_mmap, |
| 2578 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2579 | }; |
| 2580 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 2581 | static const struct net_proto_family tipc_family_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 2582 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2583 | .family = AF_TIPC, |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 2584 | .create = tipc_sk_create |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2585 | }; |
| 2586 | |
| 2587 | static struct proto tipc_proto = { |
| 2588 | .name = "TIPC", |
| 2589 | .owner = THIS_MODULE, |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 2590 | .obj_size = sizeof(struct tipc_sock), |
| 2591 | .sysctl_rmem = sysctl_tipc_rmem |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2592 | }; |
| 2593 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 2594 | static struct proto tipc_proto_kern = { |
| 2595 | .name = "TIPC", |
| 2596 | .obj_size = sizeof(struct tipc_sock), |
| 2597 | .sysctl_rmem = sysctl_tipc_rmem |
| 2598 | }; |
| 2599 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2600 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2601 | * tipc_socket_init - initialize TIPC socket interface |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2602 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2603 | * Returns 0 on success, errno otherwise |
| 2604 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2605 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2606 | { |
| 2607 | int res; |
| 2608 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2609 | res = proto_register(&tipc_proto, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2610 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 2611 | pr_err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2612 | goto out; |
| 2613 | } |
| 2614 | |
| 2615 | res = sock_register(&tipc_family_ops); |
| 2616 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 2617 | pr_err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2618 | proto_unregister(&tipc_proto); |
| 2619 | goto out; |
| 2620 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2621 | out: |
| 2622 | return res; |
| 2623 | } |
| 2624 | |
| 2625 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2626 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2627 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 2628 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2629 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2630 | sock_unregister(tipc_family_ops.family); |
| 2631 | proto_unregister(&tipc_proto); |
| 2632 | } |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2633 | |
| 2634 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 2635 | static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2636 | { |
| 2637 | u32 peer_node; |
| 2638 | u32 peer_port; |
| 2639 | struct nlattr *nest; |
| 2640 | |
| 2641 | peer_node = tsk_peer_node(tsk); |
| 2642 | peer_port = tsk_peer_port(tsk); |
| 2643 | |
| 2644 | nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON); |
| 2645 | |
| 2646 | if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node)) |
| 2647 | goto msg_full; |
| 2648 | if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port)) |
| 2649 | goto msg_full; |
| 2650 | |
| 2651 | if (tsk->conn_type != 0) { |
| 2652 | if (nla_put_flag(skb, TIPC_NLA_CON_FLAG)) |
| 2653 | goto msg_full; |
| 2654 | if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type)) |
| 2655 | goto msg_full; |
| 2656 | if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance)) |
| 2657 | goto msg_full; |
| 2658 | } |
| 2659 | nla_nest_end(skb, nest); |
| 2660 | |
| 2661 | return 0; |
| 2662 | |
| 2663 | msg_full: |
| 2664 | nla_nest_cancel(skb, nest); |
| 2665 | |
| 2666 | return -EMSGSIZE; |
| 2667 | } |
| 2668 | |
| 2669 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 2670 | static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb, |
| 2671 | struct tipc_sock *tsk) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2672 | { |
| 2673 | int err; |
| 2674 | void *hdr; |
| 2675 | struct nlattr *attrs; |
| 2676 | |
| 2677 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 2678 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); |
| 2679 | if (!hdr) |
| 2680 | goto msg_cancel; |
| 2681 | |
| 2682 | attrs = nla_nest_start(skb, TIPC_NLA_SOCK); |
| 2683 | if (!attrs) |
| 2684 | goto genlmsg_cancel; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2685 | if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid)) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2686 | goto attr_msg_cancel; |
| 2687 | if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr)) |
| 2688 | goto attr_msg_cancel; |
| 2689 | |
| 2690 | if (tsk->connected) { |
| 2691 | err = __tipc_nl_add_sk_con(skb, tsk); |
| 2692 | if (err) |
| 2693 | goto attr_msg_cancel; |
| 2694 | } else if (!list_empty(&tsk->publications)) { |
| 2695 | if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL)) |
| 2696 | goto attr_msg_cancel; |
| 2697 | } |
| 2698 | nla_nest_end(skb, attrs); |
| 2699 | genlmsg_end(skb, hdr); |
| 2700 | |
| 2701 | return 0; |
| 2702 | |
| 2703 | attr_msg_cancel: |
| 2704 | nla_nest_cancel(skb, attrs); |
| 2705 | genlmsg_cancel: |
| 2706 | genlmsg_cancel(skb, hdr); |
| 2707 | msg_cancel: |
| 2708 | return -EMSGSIZE; |
| 2709 | } |
| 2710 | |
| 2711 | int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 2712 | { |
| 2713 | int err; |
| 2714 | struct tipc_sock *tsk; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2715 | const struct bucket_table *tbl; |
| 2716 | struct rhash_head *pos; |
| 2717 | u32 prev_portid = cb->args[0]; |
| 2718 | u32 portid = prev_portid; |
| 2719 | int i; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2720 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2721 | rcu_read_lock(); |
| 2722 | tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); |
| 2723 | for (i = 0; i < tbl->size; i++) { |
| 2724 | rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { |
| 2725 | spin_lock_bh(&tsk->sk.sk_lock.slock); |
| 2726 | portid = tsk->portid; |
| 2727 | err = __tipc_nl_add_sk(skb, cb, tsk); |
| 2728 | spin_unlock_bh(&tsk->sk.sk_lock.slock); |
| 2729 | if (err) |
| 2730 | break; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2731 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2732 | prev_portid = portid; |
| 2733 | } |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2734 | } |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2735 | rcu_read_unlock(); |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2736 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2737 | cb->args[0] = prev_portid; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 2738 | |
| 2739 | return skb->len; |
| 2740 | } |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2741 | |
| 2742 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 2743 | static int __tipc_nl_add_sk_publ(struct sk_buff *skb, |
| 2744 | struct netlink_callback *cb, |
| 2745 | struct publication *publ) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2746 | { |
| 2747 | void *hdr; |
| 2748 | struct nlattr *attrs; |
| 2749 | |
| 2750 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 2751 | &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET); |
| 2752 | if (!hdr) |
| 2753 | goto msg_cancel; |
| 2754 | |
| 2755 | attrs = nla_nest_start(skb, TIPC_NLA_PUBL); |
| 2756 | if (!attrs) |
| 2757 | goto genlmsg_cancel; |
| 2758 | |
| 2759 | if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) |
| 2760 | goto attr_msg_cancel; |
| 2761 | if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type)) |
| 2762 | goto attr_msg_cancel; |
| 2763 | if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower)) |
| 2764 | goto attr_msg_cancel; |
| 2765 | if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper)) |
| 2766 | goto attr_msg_cancel; |
| 2767 | |
| 2768 | nla_nest_end(skb, attrs); |
| 2769 | genlmsg_end(skb, hdr); |
| 2770 | |
| 2771 | return 0; |
| 2772 | |
| 2773 | attr_msg_cancel: |
| 2774 | nla_nest_cancel(skb, attrs); |
| 2775 | genlmsg_cancel: |
| 2776 | genlmsg_cancel(skb, hdr); |
| 2777 | msg_cancel: |
| 2778 | return -EMSGSIZE; |
| 2779 | } |
| 2780 | |
| 2781 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 2782 | static int __tipc_nl_list_sk_publ(struct sk_buff *skb, |
| 2783 | struct netlink_callback *cb, |
| 2784 | struct tipc_sock *tsk, u32 *last_publ) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2785 | { |
| 2786 | int err; |
| 2787 | struct publication *p; |
| 2788 | |
| 2789 | if (*last_publ) { |
| 2790 | list_for_each_entry(p, &tsk->publications, pport_list) { |
| 2791 | if (p->key == *last_publ) |
| 2792 | break; |
| 2793 | } |
| 2794 | if (p->key != *last_publ) { |
| 2795 | /* We never set seq or call nl_dump_check_consistent() |
| 2796 | * this means that setting prev_seq here will cause the |
| 2797 | * consistence check to fail in the netlink callback |
| 2798 | * handler. Resulting in the last NLMSG_DONE message |
| 2799 | * having the NLM_F_DUMP_INTR flag set. |
| 2800 | */ |
| 2801 | cb->prev_seq = 1; |
| 2802 | *last_publ = 0; |
| 2803 | return -EPIPE; |
| 2804 | } |
| 2805 | } else { |
| 2806 | p = list_first_entry(&tsk->publications, struct publication, |
| 2807 | pport_list); |
| 2808 | } |
| 2809 | |
| 2810 | list_for_each_entry_from(p, &tsk->publications, pport_list) { |
| 2811 | err = __tipc_nl_add_sk_publ(skb, cb, p); |
| 2812 | if (err) { |
| 2813 | *last_publ = p->key; |
| 2814 | return err; |
| 2815 | } |
| 2816 | } |
| 2817 | *last_publ = 0; |
| 2818 | |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
| 2822 | int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 2823 | { |
| 2824 | int err; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2825 | u32 tsk_portid = cb->args[0]; |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2826 | u32 last_publ = cb->args[1]; |
| 2827 | u32 done = cb->args[2]; |
| 2828 | struct tipc_sock *tsk; |
| 2829 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2830 | if (!tsk_portid) { |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2831 | struct nlattr **attrs; |
| 2832 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; |
| 2833 | |
| 2834 | err = tipc_nlmsg_parse(cb->nlh, &attrs); |
| 2835 | if (err) |
| 2836 | return err; |
| 2837 | |
| 2838 | err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, |
| 2839 | attrs[TIPC_NLA_SOCK], |
| 2840 | tipc_nl_sock_policy); |
| 2841 | if (err) |
| 2842 | return err; |
| 2843 | |
| 2844 | if (!sock[TIPC_NLA_SOCK_REF]) |
| 2845 | return -EINVAL; |
| 2846 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2847 | tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | if (done) |
| 2851 | return 0; |
| 2852 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2853 | tsk = tipc_sk_lookup(tsk_portid); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2854 | if (!tsk) |
| 2855 | return -EINVAL; |
| 2856 | |
| 2857 | lock_sock(&tsk->sk); |
| 2858 | err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ); |
| 2859 | if (!err) |
| 2860 | done = 1; |
| 2861 | release_sock(&tsk->sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2862 | sock_put(&tsk->sk); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2863 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2864 | cb->args[0] = tsk_portid; |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 2865 | cb->args[1] = last_publ; |
| 2866 | cb->args[2] = done; |
| 2867 | |
| 2868 | return skb->len; |
| 2869 | } |