Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/server.c: TIPC server infrastructure |
| 3 | * |
| 4 | * Copyright (c) 2012-2013, Wind River Systems |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 5 | * Copyright (c) 2017, Ericsson AB |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 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. |
| 19 | * |
| 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 |
| 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
Jon Maloy | c901d26 | 2018-02-15 10:40:43 +0100 | [diff] [blame] | 37 | #include "subscr.h" |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 38 | #include "server.h" |
| 39 | #include "core.h" |
Ying Xue | 859fc7c | 2015-01-09 15:27:01 +0800 | [diff] [blame] | 40 | #include "socket.h" |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 41 | #include "addr.h" |
| 42 | #include "msg.h" |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 43 | #include <net/sock.h> |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 44 | #include <linux/module.h> |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 45 | |
| 46 | /* Number of messages to send before rescheduling */ |
| 47 | #define MAX_SEND_MSG_COUNT 25 |
| 48 | #define MAX_RECV_MSG_COUNT 25 |
| 49 | #define CF_CONNECTED 1 |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 50 | #define CF_SERVER 2 |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 51 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 52 | #define TIPC_SERVER_NAME_LEN 32 |
| 53 | |
| 54 | /** |
| 55 | * struct tipc_server - TIPC server structure |
| 56 | * @conn_idr: identifier set of connection |
| 57 | * @idr_lock: protect the connection identifier set |
| 58 | * @idr_in_use: amount of allocated identifier entry |
| 59 | * @net: network namspace instance |
| 60 | * @rcvbuf_cache: memory cache of server receive buffer |
| 61 | * @rcv_wq: receive workqueue |
| 62 | * @send_wq: send workqueue |
| 63 | * @max_rcvbuf_size: maximum permitted receive message length |
| 64 | * @tipc_conn_new: callback will be called when new connection is incoming |
| 65 | * @tipc_conn_release: callback will be called before releasing the connection |
| 66 | * @tipc_conn_recvmsg: callback will be called when message arrives |
| 67 | * @saddr: TIPC server address |
| 68 | * @name: server name |
| 69 | * @imp: message importance |
| 70 | * @type: socket type |
| 71 | */ |
| 72 | struct tipc_server { |
| 73 | struct idr conn_idr; |
| 74 | spinlock_t idr_lock; /* for idr list */ |
| 75 | int idr_in_use; |
| 76 | struct net *net; |
| 77 | struct workqueue_struct *rcv_wq; |
| 78 | struct workqueue_struct *send_wq; |
| 79 | int max_rcvbuf_size; |
| 80 | struct sockaddr_tipc *saddr; |
| 81 | char name[TIPC_SERVER_NAME_LEN]; |
| 82 | }; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * struct tipc_conn - TIPC connection structure |
| 86 | * @kref: reference counter to connection object |
| 87 | * @conid: connection identifier |
| 88 | * @sock: socket handler associated with connection |
| 89 | * @flags: indicates connection state |
| 90 | * @server: pointer to connected server |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 91 | * @sub_list: lsit to all pertaing subscriptions |
| 92 | * @sub_lock: lock protecting the subscription list |
| 93 | * @outqueue_lock: control access to the outqueue |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 94 | * @rwork: receive work item |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 95 | * @rx_action: what to do when connection socket is active |
| 96 | * @outqueue: pointer to first outbound message in queue |
stephen hemminger | 963a1855 | 2014-01-12 12:48:00 -0800 | [diff] [blame] | 97 | * @outqueue_lock: control access to the outqueue |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 98 | * @swork: send work item |
| 99 | */ |
| 100 | struct tipc_conn { |
| 101 | struct kref kref; |
| 102 | int conid; |
| 103 | struct socket *sock; |
| 104 | unsigned long flags; |
| 105 | struct tipc_server *server; |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 106 | struct list_head sub_list; |
| 107 | spinlock_t sub_lock; /* for subscription list */ |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 108 | struct work_struct rwork; |
| 109 | int (*rx_action) (struct tipc_conn *con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 110 | struct list_head outqueue; |
| 111 | spinlock_t outqueue_lock; |
| 112 | struct work_struct swork; |
| 113 | }; |
| 114 | |
| 115 | /* An entry waiting to be sent */ |
| 116 | struct outqueue_entry { |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 117 | bool inactive; |
| 118 | struct tipc_event evt; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 119 | struct list_head list; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static void tipc_recv_work(struct work_struct *work); |
| 123 | static void tipc_send_work(struct work_struct *work); |
| 124 | static void tipc_clean_outqueues(struct tipc_conn *con); |
| 125 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 126 | static struct tipc_conn *sock2con(struct sock *sk) |
| 127 | { |
| 128 | return sk->sk_user_data; |
| 129 | } |
| 130 | |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 131 | static bool connected(struct tipc_conn *con) |
| 132 | { |
| 133 | return con && test_bit(CF_CONNECTED, &con->flags); |
| 134 | } |
| 135 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 136 | static void tipc_conn_kref_release(struct kref *kref) |
| 137 | { |
| 138 | struct tipc_conn *con = container_of(kref, struct tipc_conn, kref); |
Parthasarathy Bhuvaragan | fc0adfc | 2017-01-24 13:00:45 +0100 | [diff] [blame] | 139 | struct tipc_server *s = con->server; |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 140 | struct socket *sock = con->sock; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 141 | |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 142 | if (sock) { |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 143 | if (test_bit(CF_SERVER, &con->flags)) { |
| 144 | __module_get(sock->ops->owner); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 145 | __module_get(sock->sk->sk_prot_creator->owner); |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 146 | } |
Ying Xue | def81f6 | 2015-04-23 09:37:38 -0400 | [diff] [blame] | 147 | sock_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 148 | con->sock = NULL; |
| 149 | } |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 150 | spin_lock_bh(&s->idr_lock); |
| 151 | idr_remove(&s->conn_idr, con->conid); |
| 152 | s->idr_in_use--; |
| 153 | spin_unlock_bh(&s->idr_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 154 | tipc_clean_outqueues(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 155 | kfree(con); |
| 156 | } |
| 157 | |
| 158 | static void conn_put(struct tipc_conn *con) |
| 159 | { |
| 160 | kref_put(&con->kref, tipc_conn_kref_release); |
| 161 | } |
| 162 | |
| 163 | static void conn_get(struct tipc_conn *con) |
| 164 | { |
| 165 | kref_get(&con->kref); |
| 166 | } |
| 167 | |
| 168 | static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid) |
| 169 | { |
| 170 | struct tipc_conn *con; |
| 171 | |
| 172 | spin_lock_bh(&s->idr_lock); |
| 173 | con = idr_find(&s->conn_idr, conid); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 174 | if (!connected(con) || !kref_get_unless_zero(&con->kref)) |
| 175 | con = NULL; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 176 | spin_unlock_bh(&s->idr_lock); |
| 177 | return con; |
| 178 | } |
| 179 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 180 | /* sock_data_ready - interrupt callback indicating the socket has data to read |
| 181 | * The queued job is launched in tipc_recv_from_sock() |
| 182 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 183 | static void sock_data_ready(struct sock *sk) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 184 | { |
| 185 | struct tipc_conn *con; |
| 186 | |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 187 | read_lock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 188 | con = sock2con(sk); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 189 | if (connected(con)) { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 190 | conn_get(con); |
| 191 | if (!queue_work(con->server->rcv_wq, &con->rwork)) |
| 192 | conn_put(con); |
| 193 | } |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 194 | read_unlock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 197 | /* sock_write_space - interrupt callback after a sendmsg EAGAIN |
| 198 | * Indicates that there now is more is space in the send buffer |
| 199 | * The queued job is launched in tipc_send_to_sock() |
| 200 | */ |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 201 | static void sock_write_space(struct sock *sk) |
| 202 | { |
| 203 | struct tipc_conn *con; |
| 204 | |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 205 | read_lock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 206 | con = sock2con(sk); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 207 | if (connected(con)) { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 208 | conn_get(con); |
| 209 | if (!queue_work(con->server->send_wq, &con->swork)) |
| 210 | conn_put(con); |
| 211 | } |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 212 | read_unlock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con) |
| 216 | { |
| 217 | struct sock *sk = sock->sk; |
| 218 | |
| 219 | write_lock_bh(&sk->sk_callback_lock); |
| 220 | |
| 221 | sk->sk_data_ready = sock_data_ready; |
| 222 | sk->sk_write_space = sock_write_space; |
| 223 | sk->sk_user_data = con; |
| 224 | |
| 225 | con->sock = sock; |
| 226 | |
| 227 | write_unlock_bh(&sk->sk_callback_lock); |
| 228 | } |
| 229 | |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 230 | /* tipc_con_delete_sub - delete a specific or all subscriptions |
| 231 | * for a given subscriber |
| 232 | */ |
| 233 | static void tipc_con_delete_sub(struct tipc_conn *con, struct tipc_subscr *s) |
| 234 | { |
| 235 | struct list_head *sub_list = &con->sub_list; |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 236 | struct tipc_net *tn = tipc_net(con->server->net); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 237 | struct tipc_subscription *sub, *tmp; |
| 238 | |
| 239 | spin_lock_bh(&con->sub_lock); |
Jon Maloy | da0a75e | 2018-02-15 10:40:48 +0100 | [diff] [blame] | 240 | list_for_each_entry_safe(sub, tmp, sub_list, sub_list) { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 241 | if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) { |
Jon Maloy | 242e82c | 2018-02-15 10:40:47 +0100 | [diff] [blame] | 242 | tipc_sub_unsubscribe(sub); |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 243 | atomic_dec(&tn->subscription_count); |
| 244 | } else if (s) { |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 245 | break; |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 246 | } |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 247 | } |
| 248 | spin_unlock_bh(&con->sub_lock); |
| 249 | } |
| 250 | |
Parthasarathy Bhuvaragan | 9dc3abd | 2017-01-24 13:00:46 +0100 | [diff] [blame] | 251 | static void tipc_close_conn(struct tipc_conn *con) |
Parthasarathy Bhuvaragan | 333f796 | 2016-04-12 13:05:21 +0200 | [diff] [blame] | 252 | { |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 253 | struct sock *sk = con->sock->sk; |
| 254 | bool disconnect = false; |
Parthasarathy Bhuvaragan | 333f796 | 2016-04-12 13:05:21 +0200 | [diff] [blame] | 255 | |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 256 | write_lock_bh(&sk->sk_callback_lock); |
| 257 | disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 258 | |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 259 | if (disconnect) { |
| 260 | sk->sk_user_data = NULL; |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 261 | tipc_con_delete_sub(con, NULL); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 262 | } |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 263 | write_unlock_bh(&sk->sk_callback_lock); |
| 264 | |
| 265 | /* Handle concurrent calls from sending and receiving threads */ |
| 266 | if (!disconnect) |
| 267 | return; |
| 268 | |
| 269 | /* Don't flush pending works, -just let them expire */ |
| 270 | kernel_sock_shutdown(con->sock, SHUT_RDWR); |
| 271 | conn_put(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s) |
| 275 | { |
| 276 | struct tipc_conn *con; |
| 277 | int ret; |
| 278 | |
| 279 | con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC); |
| 280 | if (!con) |
| 281 | return ERR_PTR(-ENOMEM); |
| 282 | |
| 283 | kref_init(&con->kref); |
| 284 | INIT_LIST_HEAD(&con->outqueue); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 285 | INIT_LIST_HEAD(&con->sub_list); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 286 | spin_lock_init(&con->outqueue_lock); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 287 | spin_lock_init(&con->sub_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 288 | INIT_WORK(&con->swork, tipc_send_work); |
| 289 | INIT_WORK(&con->rwork, tipc_recv_work); |
| 290 | |
| 291 | spin_lock_bh(&s->idr_lock); |
| 292 | ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC); |
| 293 | if (ret < 0) { |
| 294 | kfree(con); |
| 295 | spin_unlock_bh(&s->idr_lock); |
| 296 | return ERR_PTR(-ENOMEM); |
| 297 | } |
| 298 | con->conid = ret; |
| 299 | s->idr_in_use++; |
| 300 | spin_unlock_bh(&s->idr_lock); |
| 301 | |
| 302 | set_bit(CF_CONNECTED, &con->flags); |
| 303 | con->server = s; |
| 304 | |
| 305 | return con; |
| 306 | } |
| 307 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 308 | static int tipc_con_rcv_sub(struct tipc_server *srv, |
| 309 | struct tipc_conn *con, |
| 310 | struct tipc_subscr *s) |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 311 | { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 312 | struct tipc_net *tn = tipc_net(srv->net); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 313 | struct tipc_subscription *sub; |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 314 | |
Jon Maloy | 8985ecc | 2018-02-15 10:40:46 +0100 | [diff] [blame] | 315 | if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) { |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 316 | tipc_con_delete_sub(con, s); |
| 317 | return 0; |
| 318 | } |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 319 | if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) { |
| 320 | pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR); |
| 321 | return -1; |
| 322 | } |
| 323 | sub = tipc_sub_subscribe(srv->net, s, con->conid); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 324 | if (!sub) |
| 325 | return -1; |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 326 | atomic_inc(&tn->subscription_count); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 327 | spin_lock_bh(&con->sub_lock); |
Jon Maloy | da0a75e | 2018-02-15 10:40:48 +0100 | [diff] [blame] | 328 | list_add(&sub->sub_list, &con->sub_list); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 329 | spin_unlock_bh(&con->sub_lock); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 333 | static int tipc_receive_from_sock(struct tipc_conn *con) |
| 334 | { |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 335 | struct tipc_server *srv = con->server; |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 336 | struct sock *sk = con->sock->sk; |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 337 | struct msghdr msg = {}; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 338 | struct tipc_subscr s; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 339 | struct kvec iov; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 340 | int ret; |
| 341 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 342 | iov.iov_base = &s; |
| 343 | iov.iov_len = sizeof(s); |
Jon Maloy | c901d26 | 2018-02-15 10:40:43 +0100 | [diff] [blame] | 344 | msg.msg_name = NULL; |
Al Viro | bc48027 | 2017-09-20 22:08:04 -0400 | [diff] [blame] | 345 | iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, iov.iov_len); |
| 346 | ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 347 | if (ret == -EWOULDBLOCK) |
| 348 | return -EWOULDBLOCK; |
| 349 | if (ret > 0) { |
| 350 | read_lock_bh(&sk->sk_callback_lock); |
| 351 | ret = tipc_con_rcv_sub(srv, con, &s); |
| 352 | read_unlock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 353 | } |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 354 | if (ret < 0) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 355 | tipc_close_conn(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int tipc_accept_from_sock(struct tipc_conn *con) |
| 361 | { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 362 | struct socket *sock = con->sock; |
| 363 | struct socket *newsock; |
| 364 | struct tipc_conn *newcon; |
| 365 | int ret; |
| 366 | |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 367 | ret = kernel_accept(sock, &newsock, O_NONBLOCK); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | |
| 371 | newcon = tipc_alloc_conn(con->server); |
| 372 | if (IS_ERR(newcon)) { |
| 373 | ret = PTR_ERR(newcon); |
| 374 | sock_release(newsock); |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | newcon->rx_action = tipc_receive_from_sock; |
| 379 | tipc_register_callbacks(newsock, newcon); |
| 380 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 381 | /* Wake up receive process in case of 'SYN+' message */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 382 | newsock->sk->sk_data_ready(newsock->sk); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static struct socket *tipc_create_listen_sock(struct tipc_conn *con) |
| 387 | { |
| 388 | struct tipc_server *s = con->server; |
| 389 | struct socket *sock = NULL; |
Jon Maloy | 27469b7 | 2018-02-15 10:40:42 +0100 | [diff] [blame] | 390 | int imp = TIPC_CRITICAL_IMPORTANCE; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 391 | int ret; |
| 392 | |
Ying Xue | fa787ae | 2015-05-13 11:20:38 +0800 | [diff] [blame] | 393 | ret = sock_create_kern(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | return NULL; |
| 396 | ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE, |
Jon Maloy | 27469b7 | 2018-02-15 10:40:42 +0100 | [diff] [blame] | 397 | (char *)&imp, sizeof(imp)); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 398 | if (ret < 0) |
| 399 | goto create_err; |
| 400 | ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr)); |
| 401 | if (ret < 0) |
| 402 | goto create_err; |
| 403 | |
Jon Maloy | 27469b7 | 2018-02-15 10:40:42 +0100 | [diff] [blame] | 404 | con->rx_action = tipc_accept_from_sock; |
| 405 | ret = kernel_listen(sock, 0); |
| 406 | if (ret < 0) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 407 | goto create_err; |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 408 | |
| 409 | /* As server's listening socket owner and creator is the same module, |
| 410 | * we have to decrease TIPC module reference count to guarantee that |
| 411 | * it remains zero after the server socket is created, otherwise, |
| 412 | * executing "rmmod" command is unable to make TIPC module deleted |
| 413 | * after TIPC module is inserted successfully. |
| 414 | * |
| 415 | * However, the reference count is ever increased twice in |
| 416 | * sock_create_kern(): one is to increase the reference count of owner |
| 417 | * of TIPC socket's proto_ops struct; another is to increment the |
| 418 | * reference count of owner of TIPC proto struct. Therefore, we must |
| 419 | * decrement the module reference count twice to ensure that it keeps |
| 420 | * zero after server's listening socket is created. Of course, we |
| 421 | * must bump the module reference count twice as well before the socket |
| 422 | * is closed. |
| 423 | */ |
| 424 | module_put(sock->ops->owner); |
| 425 | module_put(sock->sk->sk_prot_creator->owner); |
| 426 | set_bit(CF_SERVER, &con->flags); |
| 427 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 428 | return sock; |
| 429 | |
| 430 | create_err: |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 431 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Ying Xue | def81f6 | 2015-04-23 09:37:38 -0400 | [diff] [blame] | 432 | sock_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | static int tipc_open_listening_sock(struct tipc_server *s) |
| 437 | { |
| 438 | struct socket *sock; |
| 439 | struct tipc_conn *con; |
| 440 | |
| 441 | con = tipc_alloc_conn(s); |
| 442 | if (IS_ERR(con)) |
| 443 | return PTR_ERR(con); |
| 444 | |
| 445 | sock = tipc_create_listen_sock(con); |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 446 | if (!sock) { |
| 447 | idr_remove(&s->conn_idr, con->conid); |
| 448 | s->idr_in_use--; |
| 449 | kfree(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 450 | return -EINVAL; |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 451 | } |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 452 | |
| 453 | tipc_register_callbacks(sock, con); |
| 454 | return 0; |
| 455 | } |
| 456 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 457 | static void tipc_clean_outqueues(struct tipc_conn *con) |
| 458 | { |
| 459 | struct outqueue_entry *e, *safe; |
| 460 | |
| 461 | spin_lock_bh(&con->outqueue_lock); |
| 462 | list_for_each_entry_safe(e, safe, &con->outqueue, list) { |
| 463 | list_del(&e->list); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 464 | kfree(e); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 465 | } |
| 466 | spin_unlock_bh(&con->outqueue_lock); |
| 467 | } |
| 468 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 469 | /* tipc_conn_queue_evt - interrupt level call from a subscription instance |
| 470 | * The queued job is launched in tipc_send_to_sock() |
| 471 | */ |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 472 | void tipc_conn_queue_evt(struct net *net, int conid, |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 473 | u32 event, struct tipc_event *evt) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 474 | { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 475 | struct tipc_server *srv = tipc_topsrv(net); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 476 | struct outqueue_entry *e; |
| 477 | struct tipc_conn *con; |
| 478 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 479 | con = tipc_conn_lookup(srv, conid); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 480 | if (!con) |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 481 | return; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 482 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 483 | if (!connected(con)) |
| 484 | goto err; |
Parthasarathy Bhuvaragan | 4c887aa | 2017-01-24 13:00:47 +0100 | [diff] [blame] | 485 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 486 | e = kmalloc(sizeof(*e), GFP_ATOMIC); |
| 487 | if (!e) |
| 488 | goto err; |
| 489 | e->inactive = (event == TIPC_SUBSCR_TIMEOUT); |
| 490 | memcpy(&e->evt, evt, sizeof(*evt)); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 491 | spin_lock_bh(&con->outqueue_lock); |
| 492 | list_add_tail(&e->list, &con->outqueue); |
| 493 | spin_unlock_bh(&con->outqueue_lock); |
| 494 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 495 | if (queue_work(srv->send_wq, &con->swork)) |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 496 | return; |
| 497 | err: |
| 498 | conn_put(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 501 | bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower, |
| 502 | u32 upper, u32 filter, int *conid) |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 503 | { |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 504 | struct tipc_subscr sub; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 505 | struct tipc_conn *con; |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 506 | int rc; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 507 | |
| 508 | sub.seq.type = type; |
| 509 | sub.seq.lower = lower; |
| 510 | sub.seq.upper = upper; |
| 511 | sub.timeout = TIPC_WAIT_FOREVER; |
Jon Maloy | 8348500 | 2018-01-08 21:03:29 +0100 | [diff] [blame] | 512 | sub.filter = filter; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 513 | *(u32 *)&sub.usr_handle = port; |
| 514 | |
| 515 | con = tipc_alloc_conn(tipc_topsrv(net)); |
Dan Carpenter | c75e427 | 2017-10-18 10:48:25 +0300 | [diff] [blame] | 516 | if (IS_ERR(con)) |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 517 | return false; |
| 518 | |
| 519 | *conid = con->conid; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 520 | con->sock = NULL; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 521 | rc = tipc_con_rcv_sub(tipc_topsrv(net), con, &sub); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 522 | if (rc < 0) |
| 523 | tipc_close_conn(con); |
| 524 | return !rc; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void tipc_topsrv_kern_unsubscr(struct net *net, int conid) |
| 528 | { |
| 529 | struct tipc_conn *con; |
| 530 | |
| 531 | con = tipc_conn_lookup(tipc_topsrv(net), conid); |
| 532 | if (!con) |
| 533 | return; |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 534 | |
| 535 | test_and_clear_bit(CF_CONNECTED, &con->flags); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 536 | tipc_con_delete_sub(con, NULL); |
Jon Maloy | e88f2be | 2018-01-15 17:56:28 +0100 | [diff] [blame] | 537 | conn_put(con); |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 538 | conn_put(con); |
| 539 | } |
| 540 | |
| 541 | static void tipc_send_kern_top_evt(struct net *net, struct tipc_event *evt) |
| 542 | { |
| 543 | u32 port = *(u32 *)&evt->s.usr_handle; |
| 544 | u32 self = tipc_own_addr(net); |
| 545 | struct sk_buff_head evtq; |
| 546 | struct sk_buff *skb; |
| 547 | |
| 548 | skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt), |
| 549 | self, self, port, port, 0); |
| 550 | if (!skb) |
| 551 | return; |
| 552 | msg_set_dest_droppable(buf_msg(skb), true); |
| 553 | memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt)); |
| 554 | skb_queue_head_init(&evtq); |
| 555 | __skb_queue_tail(&evtq, skb); |
| 556 | tipc_sk_rcv(net, &evtq); |
| 557 | } |
| 558 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 559 | static void tipc_send_to_sock(struct tipc_conn *con) |
| 560 | { |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 561 | struct list_head *queue = &con->outqueue; |
| 562 | struct tipc_server *srv = con->server; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 563 | struct outqueue_entry *e; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 564 | struct tipc_event *evt; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 565 | struct msghdr msg; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 566 | struct kvec iov; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 567 | int count = 0; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 568 | int ret; |
| 569 | |
| 570 | spin_lock_bh(&con->outqueue_lock); |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 571 | |
| 572 | while (!list_empty(queue)) { |
| 573 | e = list_first_entry(queue, struct outqueue_entry, list); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 574 | evt = &e->evt; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 575 | spin_unlock_bh(&con->outqueue_lock); |
| 576 | |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 577 | if (e->inactive) |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 578 | tipc_con_delete_sub(con, &evt->s); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 579 | |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 580 | memset(&msg, 0, sizeof(msg)); |
| 581 | msg.msg_flags = MSG_DONTWAIT; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 582 | iov.iov_base = evt; |
| 583 | iov.iov_len = sizeof(*evt); |
| 584 | msg.msg_name = NULL; |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 585 | |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 586 | if (con->sock) { |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 587 | ret = kernel_sendmsg(con->sock, &msg, &iov, |
| 588 | 1, sizeof(*evt)); |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 589 | if (ret == -EWOULDBLOCK || ret == 0) { |
| 590 | cond_resched(); |
| 591 | goto out; |
| 592 | } else if (ret < 0) { |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 593 | goto err; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 594 | } |
| 595 | } else { |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 596 | tipc_send_kern_top_evt(srv->net, evt); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 597 | } |
Jon Maloy | 27469b7 | 2018-02-15 10:40:42 +0100 | [diff] [blame] | 598 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 599 | /* Don't starve users filling buffers */ |
| 600 | if (++count >= MAX_SEND_MSG_COUNT) { |
| 601 | cond_resched(); |
| 602 | count = 0; |
| 603 | } |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 604 | spin_lock_bh(&con->outqueue_lock); |
| 605 | list_del(&e->list); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 606 | kfree(e); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 607 | } |
| 608 | spin_unlock_bh(&con->outqueue_lock); |
| 609 | out: |
| 610 | return; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 611 | err: |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 612 | tipc_close_conn(con); |
| 613 | } |
| 614 | |
| 615 | static void tipc_recv_work(struct work_struct *work) |
| 616 | { |
| 617 | struct tipc_conn *con = container_of(work, struct tipc_conn, rwork); |
| 618 | int count = 0; |
| 619 | |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 620 | while (connected(con)) { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 621 | if (con->rx_action(con)) |
| 622 | break; |
| 623 | |
| 624 | /* Don't flood Rx machine */ |
| 625 | if (++count >= MAX_RECV_MSG_COUNT) { |
| 626 | cond_resched(); |
| 627 | count = 0; |
| 628 | } |
| 629 | } |
| 630 | conn_put(con); |
| 631 | } |
| 632 | |
| 633 | static void tipc_send_work(struct work_struct *work) |
| 634 | { |
| 635 | struct tipc_conn *con = container_of(work, struct tipc_conn, swork); |
| 636 | |
Jon Maloy | df79d04 | 2018-02-15 10:40:44 +0100 | [diff] [blame] | 637 | if (connected(con)) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 638 | tipc_send_to_sock(con); |
| 639 | |
| 640 | conn_put(con); |
| 641 | } |
| 642 | |
| 643 | static void tipc_work_stop(struct tipc_server *s) |
| 644 | { |
| 645 | destroy_workqueue(s->rcv_wq); |
| 646 | destroy_workqueue(s->send_wq); |
| 647 | } |
| 648 | |
| 649 | static int tipc_work_start(struct tipc_server *s) |
| 650 | { |
Parthasarathy Bhuvaragan | 06c8581 | 2016-02-02 10:52:17 +0100 | [diff] [blame] | 651 | s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 652 | if (!s->rcv_wq) { |
| 653 | pr_err("can't start tipc receive workqueue\n"); |
| 654 | return -ENOMEM; |
| 655 | } |
| 656 | |
Parthasarathy Bhuvaragan | 06c8581 | 2016-02-02 10:52:17 +0100 | [diff] [blame] | 657 | s->send_wq = alloc_ordered_workqueue("tipc_send", 0); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 658 | if (!s->send_wq) { |
| 659 | pr_err("can't start tipc send workqueue\n"); |
| 660 | destroy_workqueue(s->rcv_wq); |
| 661 | return -ENOMEM; |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 667 | int tipc_topsrv_start(struct net *net) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 668 | { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 669 | struct tipc_net *tn = tipc_net(net); |
| 670 | const char name[] = "topology_server"; |
| 671 | struct sockaddr_tipc *saddr; |
| 672 | struct tipc_server *srv; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 673 | int ret; |
| 674 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 675 | saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC); |
| 676 | if (!saddr) |
| 677 | return -ENOMEM; |
| 678 | saddr->family = AF_TIPC; |
| 679 | saddr->addrtype = TIPC_ADDR_NAMESEQ; |
| 680 | saddr->addr.nameseq.type = TIPC_TOP_SRV; |
| 681 | saddr->addr.nameseq.lower = TIPC_TOP_SRV; |
| 682 | saddr->addr.nameseq.upper = TIPC_TOP_SRV; |
| 683 | saddr->scope = TIPC_NODE_SCOPE; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 684 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 685 | srv = kzalloc(sizeof(*srv), GFP_ATOMIC); |
| 686 | if (!srv) { |
| 687 | kfree(saddr); |
| 688 | return -ENOMEM; |
| 689 | } |
| 690 | srv->net = net; |
| 691 | srv->saddr = saddr; |
| 692 | srv->max_rcvbuf_size = sizeof(struct tipc_subscr); |
| 693 | |
| 694 | strncpy(srv->name, name, strlen(name) + 1); |
| 695 | tn->topsrv = srv; |
| 696 | atomic_set(&tn->subscription_count, 0); |
| 697 | |
| 698 | spin_lock_init(&srv->idr_lock); |
| 699 | idr_init(&srv->conn_idr); |
| 700 | srv->idr_in_use = 0; |
| 701 | |
| 702 | ret = tipc_work_start(srv); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 703 | if (ret < 0) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 704 | return ret; |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 705 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 706 | ret = tipc_open_listening_sock(srv); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 707 | if (ret < 0) |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 708 | tipc_work_stop(srv); |
Jon Maloy | 414574a | 2018-02-15 10:40:45 +0100 | [diff] [blame] | 709 | |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 710 | return ret; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 713 | void tipc_topsrv_stop(struct net *net) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 714 | { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 715 | struct tipc_server *srv = tipc_topsrv(net); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 716 | struct tipc_conn *con; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 717 | int id; |
| 718 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 719 | spin_lock_bh(&srv->idr_lock); |
| 720 | for (id = 0; srv->idr_in_use; id++) { |
| 721 | con = idr_find(&srv->conn_idr, id); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 722 | if (con) { |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 723 | spin_unlock_bh(&srv->idr_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 724 | tipc_close_conn(con); |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 725 | spin_lock_bh(&srv->idr_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 726 | } |
| 727 | } |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 728 | spin_unlock_bh(&srv->idr_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 729 | |
Jon Maloy | 5c45ab2 | 2018-02-15 10:40:49 +0100 | [diff] [blame^] | 730 | tipc_work_stop(srv); |
| 731 | idr_destroy(&srv->conn_idr); |
| 732 | kfree(srv->saddr); |
| 733 | kfree(srv); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 734 | } |