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