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