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