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