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 |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the names of the copyright holders nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * Alternatively, this software may be distributed under the terms of the |
| 20 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 21 | * Software Foundation. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | * POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | #include "server.h" |
| 37 | #include "core.h" |
Ying Xue | 859fc7c | 2015-01-09 15:27:01 +0800 | [diff] [blame] | 38 | #include "socket.h" |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 39 | #include "addr.h" |
| 40 | #include "msg.h" |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 41 | #include <net/sock.h> |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 42 | #include <linux/module.h> |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 43 | |
| 44 | /* Number of messages to send before rescheduling */ |
| 45 | #define MAX_SEND_MSG_COUNT 25 |
| 46 | #define MAX_RECV_MSG_COUNT 25 |
| 47 | #define CF_CONNECTED 1 |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 48 | #define CF_SERVER 2 |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 49 | |
| 50 | #define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data) |
| 51 | |
| 52 | /** |
| 53 | * struct tipc_conn - TIPC connection structure |
| 54 | * @kref: reference counter to connection object |
| 55 | * @conid: connection identifier |
| 56 | * @sock: socket handler associated with connection |
| 57 | * @flags: indicates connection state |
| 58 | * @server: pointer to connected server |
| 59 | * @rwork: receive work item |
| 60 | * @usr_data: user-specified field |
| 61 | * @rx_action: what to do when connection socket is active |
| 62 | * @outqueue: pointer to first outbound message in queue |
stephen hemminger | 963a1855 | 2014-01-12 12:48:00 -0800 | [diff] [blame] | 63 | * @outqueue_lock: control access to the outqueue |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 64 | * @outqueue: list of connection objects for its server |
| 65 | * @swork: send work item |
| 66 | */ |
| 67 | struct tipc_conn { |
| 68 | struct kref kref; |
| 69 | int conid; |
| 70 | struct socket *sock; |
| 71 | unsigned long flags; |
| 72 | struct tipc_server *server; |
| 73 | struct work_struct rwork; |
| 74 | int (*rx_action) (struct tipc_conn *con); |
| 75 | void *usr_data; |
| 76 | struct list_head outqueue; |
| 77 | spinlock_t outqueue_lock; |
| 78 | struct work_struct swork; |
| 79 | }; |
| 80 | |
| 81 | /* An entry waiting to be sent */ |
| 82 | struct outqueue_entry { |
| 83 | struct list_head list; |
| 84 | struct kvec iov; |
| 85 | struct sockaddr_tipc dest; |
| 86 | }; |
| 87 | |
| 88 | static void tipc_recv_work(struct work_struct *work); |
| 89 | static void tipc_send_work(struct work_struct *work); |
| 90 | static void tipc_clean_outqueues(struct tipc_conn *con); |
| 91 | |
| 92 | static void tipc_conn_kref_release(struct kref *kref) |
| 93 | { |
| 94 | struct tipc_conn *con = container_of(kref, struct tipc_conn, kref); |
Parthasarathy Bhuvaragan | fc0adfc | 2017-01-24 13:00:45 +0100 | [diff] [blame] | 95 | struct tipc_server *s = con->server; |
| 96 | struct sockaddr_tipc *saddr = s->saddr; |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 97 | struct socket *sock = con->sock; |
| 98 | struct sock *sk; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 99 | |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 100 | if (sock) { |
| 101 | sk = sock->sk; |
| 102 | if (test_bit(CF_SERVER, &con->flags)) { |
| 103 | __module_get(sock->ops->owner); |
| 104 | __module_get(sk->sk_prot_creator->owner); |
| 105 | } |
Ying Xue | 2b9bb7f | 2015-03-18 09:32:59 +0800 | [diff] [blame] | 106 | saddr->scope = -TIPC_NODE_SCOPE; |
| 107 | kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr)); |
Ying Xue | def81f6 | 2015-04-23 09:37:38 -0400 | [diff] [blame] | 108 | sock_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 109 | con->sock = NULL; |
| 110 | } |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 111 | spin_lock_bh(&s->idr_lock); |
| 112 | idr_remove(&s->conn_idr, con->conid); |
| 113 | s->idr_in_use--; |
| 114 | spin_unlock_bh(&s->idr_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 115 | tipc_clean_outqueues(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 116 | kfree(con); |
| 117 | } |
| 118 | |
| 119 | static void conn_put(struct tipc_conn *con) |
| 120 | { |
| 121 | kref_put(&con->kref, tipc_conn_kref_release); |
| 122 | } |
| 123 | |
| 124 | static void conn_get(struct tipc_conn *con) |
| 125 | { |
| 126 | kref_get(&con->kref); |
| 127 | } |
| 128 | |
| 129 | static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid) |
| 130 | { |
| 131 | struct tipc_conn *con; |
| 132 | |
| 133 | spin_lock_bh(&s->idr_lock); |
| 134 | con = idr_find(&s->conn_idr, conid); |
Parthasarathy Bhuvaragan | fc0adfc | 2017-01-24 13:00:45 +0100 | [diff] [blame] | 135 | if (con && test_bit(CF_CONNECTED, &con->flags)) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 136 | conn_get(con); |
Parthasarathy Bhuvaragan | fc0adfc | 2017-01-24 13:00:45 +0100 | [diff] [blame] | 137 | else |
| 138 | con = NULL; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 139 | spin_unlock_bh(&s->idr_lock); |
| 140 | return con; |
| 141 | } |
| 142 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 143 | static void sock_data_ready(struct sock *sk) |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 144 | { |
| 145 | struct tipc_conn *con; |
| 146 | |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 147 | read_lock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 148 | con = sock2con(sk); |
| 149 | if (con && test_bit(CF_CONNECTED, &con->flags)) { |
| 150 | conn_get(con); |
| 151 | if (!queue_work(con->server->rcv_wq, &con->rwork)) |
| 152 | conn_put(con); |
| 153 | } |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 154 | read_unlock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void sock_write_space(struct sock *sk) |
| 158 | { |
| 159 | struct tipc_conn *con; |
| 160 | |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 161 | read_lock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 162 | con = sock2con(sk); |
| 163 | if (con && test_bit(CF_CONNECTED, &con->flags)) { |
| 164 | conn_get(con); |
| 165 | if (!queue_work(con->server->send_wq, &con->swork)) |
| 166 | conn_put(con); |
| 167 | } |
Eric Dumazet | b91083a | 2016-05-17 17:44:09 -0700 | [diff] [blame] | 168 | read_unlock_bh(&sk->sk_callback_lock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con) |
| 172 | { |
| 173 | struct sock *sk = sock->sk; |
| 174 | |
| 175 | write_lock_bh(&sk->sk_callback_lock); |
| 176 | |
| 177 | sk->sk_data_ready = sock_data_ready; |
| 178 | sk->sk_write_space = sock_write_space; |
| 179 | sk->sk_user_data = con; |
| 180 | |
| 181 | con->sock = sock; |
| 182 | |
| 183 | write_unlock_bh(&sk->sk_callback_lock); |
| 184 | } |
| 185 | |
| 186 | static void tipc_unregister_callbacks(struct tipc_conn *con) |
| 187 | { |
| 188 | struct sock *sk = con->sock->sk; |
| 189 | |
| 190 | write_lock_bh(&sk->sk_callback_lock); |
| 191 | sk->sk_user_data = NULL; |
| 192 | write_unlock_bh(&sk->sk_callback_lock); |
| 193 | } |
| 194 | |
Parthasarathy Bhuvaragan | 9dc3abd | 2017-01-24 13:00:46 +0100 | [diff] [blame] | 195 | static void tipc_close_conn(struct tipc_conn *con) |
Parthasarathy Bhuvaragan | 333f796 | 2016-04-12 13:05:21 +0200 | [diff] [blame] | 196 | { |
| 197 | struct tipc_server *s = con->server; |
| 198 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 199 | if (test_and_clear_bit(CF_CONNECTED, &con->flags)) { |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 200 | if (con->sock) |
| 201 | tipc_unregister_callbacks(con); |
Parthasarathy Bhuvaragan | 9dc3abd | 2017-01-24 13:00:46 +0100 | [diff] [blame] | 202 | |
| 203 | if (con->conid) |
| 204 | s->tipc_conn_release(con->conid, con->usr_data); |
Ying Xue | 6d4ebeb | 2014-03-06 14:40:16 +0100 | [diff] [blame] | 205 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 206 | /* We shouldn't flush pending works as we may be in the |
| 207 | * thread. In fact the races with pending rx/tx work structs |
| 208 | * are harmless for us here as we have already deleted this |
Parthasarathy Bhuvaragan | 333f796 | 2016-04-12 13:05:21 +0200 | [diff] [blame] | 209 | * connection from server connection list. |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 210 | */ |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 211 | if (con->sock) |
| 212 | kernel_sock_shutdown(con->sock, SHUT_RDWR); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 213 | conn_put(con); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s) |
| 218 | { |
| 219 | struct tipc_conn *con; |
| 220 | int ret; |
| 221 | |
| 222 | con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC); |
| 223 | if (!con) |
| 224 | return ERR_PTR(-ENOMEM); |
| 225 | |
| 226 | kref_init(&con->kref); |
| 227 | INIT_LIST_HEAD(&con->outqueue); |
| 228 | spin_lock_init(&con->outqueue_lock); |
| 229 | INIT_WORK(&con->swork, tipc_send_work); |
| 230 | INIT_WORK(&con->rwork, tipc_recv_work); |
| 231 | |
| 232 | spin_lock_bh(&s->idr_lock); |
| 233 | ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC); |
| 234 | if (ret < 0) { |
| 235 | kfree(con); |
| 236 | spin_unlock_bh(&s->idr_lock); |
| 237 | return ERR_PTR(-ENOMEM); |
| 238 | } |
| 239 | con->conid = ret; |
| 240 | s->idr_in_use++; |
| 241 | spin_unlock_bh(&s->idr_lock); |
| 242 | |
| 243 | set_bit(CF_CONNECTED, &con->flags); |
| 244 | con->server = s; |
| 245 | |
| 246 | return con; |
| 247 | } |
| 248 | |
| 249 | static int tipc_receive_from_sock(struct tipc_conn *con) |
| 250 | { |
| 251 | struct msghdr msg = {}; |
| 252 | struct tipc_server *s = con->server; |
| 253 | struct sockaddr_tipc addr; |
| 254 | struct kvec iov; |
| 255 | void *buf; |
| 256 | int ret; |
| 257 | |
| 258 | buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC); |
| 259 | if (!buf) { |
| 260 | ret = -ENOMEM; |
| 261 | goto out_close; |
| 262 | } |
| 263 | |
| 264 | iov.iov_base = buf; |
| 265 | iov.iov_len = s->max_rcvbuf_size; |
| 266 | msg.msg_name = &addr; |
| 267 | ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len, |
| 268 | MSG_DONTWAIT); |
| 269 | if (ret <= 0) { |
| 270 | kmem_cache_free(s->rcvbuf_cache, buf); |
| 271 | goto out_close; |
| 272 | } |
| 273 | |
Ying Xue | 4ac1c8d | 2015-01-09 15:27:09 +0800 | [diff] [blame] | 274 | s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr, |
| 275 | con->usr_data, buf, ret); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 276 | |
| 277 | kmem_cache_free(s->rcvbuf_cache, buf); |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | out_close: |
| 282 | if (ret != -EWOULDBLOCK) |
| 283 | tipc_close_conn(con); |
| 284 | else if (ret == 0) |
| 285 | /* Don't return success if we really got EOF */ |
| 286 | ret = -EAGAIN; |
| 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | static int tipc_accept_from_sock(struct tipc_conn *con) |
| 292 | { |
| 293 | struct tipc_server *s = con->server; |
| 294 | struct socket *sock = con->sock; |
| 295 | struct socket *newsock; |
| 296 | struct tipc_conn *newcon; |
| 297 | int ret; |
| 298 | |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 299 | ret = kernel_accept(sock, &newsock, O_NONBLOCK); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | |
| 303 | newcon = tipc_alloc_conn(con->server); |
| 304 | if (IS_ERR(newcon)) { |
| 305 | ret = PTR_ERR(newcon); |
| 306 | sock_release(newsock); |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | newcon->rx_action = tipc_receive_from_sock; |
| 311 | tipc_register_callbacks(newsock, newcon); |
| 312 | |
| 313 | /* Notify that new connection is incoming */ |
| 314 | newcon->usr_data = s->tipc_conn_new(newcon->conid); |
Ying Xue | 90bdfcb | 2015-05-04 10:36:48 +0800 | [diff] [blame] | 315 | if (!newcon->usr_data) { |
| 316 | sock_release(newsock); |
Jon Maloy | a7d5f10 | 2017-12-04 22:00:20 +0100 | [diff] [blame] | 317 | conn_put(newcon); |
Ying Xue | 90bdfcb | 2015-05-04 10:36:48 +0800 | [diff] [blame] | 318 | return -ENOMEM; |
| 319 | } |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 320 | |
| 321 | /* Wake up receive process in case of 'SYN+' message */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 322 | newsock->sk->sk_data_ready(newsock->sk); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static struct socket *tipc_create_listen_sock(struct tipc_conn *con) |
| 327 | { |
| 328 | struct tipc_server *s = con->server; |
| 329 | struct socket *sock = NULL; |
| 330 | int ret; |
| 331 | |
Ying Xue | fa787ae | 2015-05-13 11:20:38 +0800 | [diff] [blame] | 332 | ret = sock_create_kern(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 333 | if (ret < 0) |
| 334 | return NULL; |
| 335 | ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE, |
| 336 | (char *)&s->imp, sizeof(s->imp)); |
| 337 | if (ret < 0) |
| 338 | goto create_err; |
| 339 | ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr)); |
| 340 | if (ret < 0) |
| 341 | goto create_err; |
| 342 | |
| 343 | switch (s->type) { |
| 344 | case SOCK_STREAM: |
| 345 | case SOCK_SEQPACKET: |
| 346 | con->rx_action = tipc_accept_from_sock; |
| 347 | |
| 348 | ret = kernel_listen(sock, 0); |
| 349 | if (ret < 0) |
| 350 | goto create_err; |
| 351 | break; |
| 352 | case SOCK_DGRAM: |
| 353 | case SOCK_RDM: |
| 354 | con->rx_action = tipc_receive_from_sock; |
| 355 | break; |
| 356 | default: |
| 357 | pr_err("Unknown socket type %d\n", s->type); |
| 358 | goto create_err; |
| 359 | } |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 360 | |
| 361 | /* As server's listening socket owner and creator is the same module, |
| 362 | * we have to decrease TIPC module reference count to guarantee that |
| 363 | * it remains zero after the server socket is created, otherwise, |
| 364 | * executing "rmmod" command is unable to make TIPC module deleted |
| 365 | * after TIPC module is inserted successfully. |
| 366 | * |
| 367 | * However, the reference count is ever increased twice in |
| 368 | * sock_create_kern(): one is to increase the reference count of owner |
| 369 | * of TIPC socket's proto_ops struct; another is to increment the |
| 370 | * reference count of owner of TIPC proto struct. Therefore, we must |
| 371 | * decrement the module reference count twice to ensure that it keeps |
| 372 | * zero after server's listening socket is created. Of course, we |
| 373 | * must bump the module reference count twice as well before the socket |
| 374 | * is closed. |
| 375 | */ |
| 376 | module_put(sock->ops->owner); |
| 377 | module_put(sock->sk->sk_prot_creator->owner); |
| 378 | set_bit(CF_SERVER, &con->flags); |
| 379 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 380 | return sock; |
| 381 | |
| 382 | create_err: |
Ying Xue | 76100a8 | 2015-03-18 09:32:57 +0800 | [diff] [blame] | 383 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Ying Xue | def81f6 | 2015-04-23 09:37:38 -0400 | [diff] [blame] | 384 | sock_release(sock); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | static int tipc_open_listening_sock(struct tipc_server *s) |
| 389 | { |
| 390 | struct socket *sock; |
| 391 | struct tipc_conn *con; |
| 392 | |
| 393 | con = tipc_alloc_conn(s); |
| 394 | if (IS_ERR(con)) |
| 395 | return PTR_ERR(con); |
| 396 | |
| 397 | sock = tipc_create_listen_sock(con); |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 398 | if (!sock) { |
| 399 | idr_remove(&s->conn_idr, con->conid); |
| 400 | s->idr_in_use--; |
| 401 | kfree(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 402 | return -EINVAL; |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 403 | } |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 404 | |
| 405 | tipc_register_callbacks(sock, con); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static struct outqueue_entry *tipc_alloc_entry(void *data, int len) |
| 410 | { |
| 411 | struct outqueue_entry *entry; |
| 412 | void *buf; |
| 413 | |
| 414 | entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC); |
| 415 | if (!entry) |
| 416 | return NULL; |
| 417 | |
Amitoj Kaur Chawla | 810bf11 | 2016-06-23 10:19:37 +0530 | [diff] [blame] | 418 | buf = kmemdup(data, len, GFP_ATOMIC); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 419 | if (!buf) { |
| 420 | kfree(entry); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 424 | entry->iov.iov_base = buf; |
| 425 | entry->iov.iov_len = len; |
| 426 | |
| 427 | return entry; |
| 428 | } |
| 429 | |
| 430 | static void tipc_free_entry(struct outqueue_entry *e) |
| 431 | { |
| 432 | kfree(e->iov.iov_base); |
| 433 | kfree(e); |
| 434 | } |
| 435 | |
| 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); |
| 443 | tipc_free_entry(e); |
| 444 | } |
| 445 | spin_unlock_bh(&con->outqueue_lock); |
| 446 | } |
| 447 | |
| 448 | int tipc_conn_sendmsg(struct tipc_server *s, int conid, |
| 449 | struct sockaddr_tipc *addr, void *data, size_t len) |
| 450 | { |
| 451 | struct outqueue_entry *e; |
| 452 | struct tipc_conn *con; |
| 453 | |
| 454 | con = tipc_conn_lookup(s, conid); |
| 455 | if (!con) |
| 456 | return -EINVAL; |
| 457 | |
Parthasarathy Bhuvaragan | 4c887aa | 2017-01-24 13:00:47 +0100 | [diff] [blame] | 458 | if (!test_bit(CF_CONNECTED, &con->flags)) { |
| 459 | conn_put(con); |
| 460 | return 0; |
| 461 | } |
| 462 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 463 | e = tipc_alloc_entry(data, len); |
| 464 | if (!e) { |
| 465 | conn_put(con); |
| 466 | return -ENOMEM; |
| 467 | } |
| 468 | |
| 469 | if (addr) |
| 470 | memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc)); |
| 471 | |
| 472 | spin_lock_bh(&con->outqueue_lock); |
| 473 | list_add_tail(&e->list, &con->outqueue); |
| 474 | spin_unlock_bh(&con->outqueue_lock); |
| 475 | |
Parthasarathy Bhuvaragan | 4c887aa | 2017-01-24 13:00:47 +0100 | [diff] [blame] | 476 | if (!queue_work(s->send_wq, &con->swork)) |
Ying Xue | 4652edb | 2014-03-06 14:40:17 +0100 | [diff] [blame] | 477 | conn_put(con); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | void tipc_conn_terminate(struct tipc_server *s, int conid) |
| 482 | { |
| 483 | struct tipc_conn *con; |
| 484 | |
| 485 | con = tipc_conn_lookup(s, conid); |
| 486 | if (con) { |
| 487 | tipc_close_conn(con); |
| 488 | conn_put(con); |
| 489 | } |
| 490 | } |
| 491 | |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 492 | bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, |
Jon Maloy | 8348500 | 2018-01-08 21:03:29 +0100 | [diff] [blame^] | 493 | u32 filter, u32 lower, u32 upper, int *conid) |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 494 | { |
| 495 | struct tipc_subscriber *scbr; |
| 496 | struct tipc_subscr sub; |
| 497 | struct tipc_server *s; |
| 498 | struct tipc_conn *con; |
| 499 | |
| 500 | sub.seq.type = type; |
| 501 | sub.seq.lower = lower; |
| 502 | sub.seq.upper = upper; |
| 503 | sub.timeout = TIPC_WAIT_FOREVER; |
Jon Maloy | 8348500 | 2018-01-08 21:03:29 +0100 | [diff] [blame^] | 504 | sub.filter = filter; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 505 | *(u32 *)&sub.usr_handle = port; |
| 506 | |
| 507 | con = tipc_alloc_conn(tipc_topsrv(net)); |
Dan Carpenter | c75e427 | 2017-10-18 10:48:25 +0300 | [diff] [blame] | 508 | if (IS_ERR(con)) |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 509 | return false; |
| 510 | |
| 511 | *conid = con->conid; |
| 512 | s = con->server; |
| 513 | scbr = s->tipc_conn_new(*conid); |
| 514 | if (!scbr) { |
Cong Wang | 672ecbe | 2017-12-04 10:31:43 -0800 | [diff] [blame] | 515 | conn_put(con); |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | con->usr_data = scbr; |
| 520 | con->sock = NULL; |
| 521 | s->tipc_conn_recvmsg(net, *conid, NULL, scbr, &sub, sizeof(sub)); |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | void tipc_topsrv_kern_unsubscr(struct net *net, int conid) |
| 526 | { |
| 527 | struct tipc_conn *con; |
| 528 | |
| 529 | con = tipc_conn_lookup(tipc_topsrv(net), conid); |
| 530 | if (!con) |
| 531 | return; |
| 532 | tipc_close_conn(con); |
| 533 | conn_put(con); |
| 534 | } |
| 535 | |
| 536 | static void tipc_send_kern_top_evt(struct net *net, struct tipc_event *evt) |
| 537 | { |
| 538 | u32 port = *(u32 *)&evt->s.usr_handle; |
| 539 | u32 self = tipc_own_addr(net); |
| 540 | struct sk_buff_head evtq; |
| 541 | struct sk_buff *skb; |
| 542 | |
| 543 | skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt), |
| 544 | self, self, port, port, 0); |
| 545 | if (!skb) |
| 546 | return; |
| 547 | msg_set_dest_droppable(buf_msg(skb), true); |
| 548 | memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt)); |
| 549 | skb_queue_head_init(&evtq); |
| 550 | __skb_queue_tail(&evtq, skb); |
| 551 | tipc_sk_rcv(net, &evtq); |
| 552 | } |
| 553 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 554 | static void tipc_send_to_sock(struct tipc_conn *con) |
| 555 | { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 556 | struct tipc_server *s = con->server; |
| 557 | struct outqueue_entry *e; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 558 | struct tipc_event *evt; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 559 | struct msghdr msg; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 560 | int count = 0; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 561 | int ret; |
| 562 | |
| 563 | spin_lock_bh(&con->outqueue_lock); |
Parthasarathy Bhuvaragan | 4c887aa | 2017-01-24 13:00:47 +0100 | [diff] [blame] | 564 | while (test_bit(CF_CONNECTED, &con->flags)) { |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 565 | e = list_entry(con->outqueue.next, struct outqueue_entry, list); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 566 | if ((struct list_head *) e == &con->outqueue) |
| 567 | break; |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 568 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 569 | spin_unlock_bh(&con->outqueue_lock); |
| 570 | |
Jon Maloy | 14c0449 | 2017-10-13 11:04:17 +0200 | [diff] [blame] | 571 | if (con->sock) { |
| 572 | memset(&msg, 0, sizeof(msg)); |
| 573 | msg.msg_flags = MSG_DONTWAIT; |
| 574 | if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) { |
| 575 | msg.msg_name = &e->dest; |
| 576 | msg.msg_namelen = sizeof(struct sockaddr_tipc); |
| 577 | } |
| 578 | ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1, |
| 579 | e->iov.iov_len); |
| 580 | if (ret == -EWOULDBLOCK || ret == 0) { |
| 581 | cond_resched(); |
| 582 | goto out; |
| 583 | } else if (ret < 0) { |
| 584 | goto send_err; |
| 585 | } |
| 586 | } else { |
| 587 | evt = e->iov.iov_base; |
| 588 | tipc_send_kern_top_evt(s->net, evt); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 589 | } |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 590 | /* Don't starve users filling buffers */ |
| 591 | if (++count >= MAX_SEND_MSG_COUNT) { |
| 592 | cond_resched(); |
| 593 | count = 0; |
| 594 | } |
| 595 | |
| 596 | spin_lock_bh(&con->outqueue_lock); |
| 597 | list_del(&e->list); |
| 598 | tipc_free_entry(e); |
| 599 | } |
| 600 | spin_unlock_bh(&con->outqueue_lock); |
| 601 | out: |
| 602 | return; |
| 603 | |
| 604 | send_err: |
| 605 | tipc_close_conn(con); |
| 606 | } |
| 607 | |
| 608 | static void tipc_recv_work(struct work_struct *work) |
| 609 | { |
| 610 | struct tipc_conn *con = container_of(work, struct tipc_conn, rwork); |
| 611 | int count = 0; |
| 612 | |
| 613 | while (test_bit(CF_CONNECTED, &con->flags)) { |
| 614 | if (con->rx_action(con)) |
| 615 | break; |
| 616 | |
| 617 | /* Don't flood Rx machine */ |
| 618 | if (++count >= MAX_RECV_MSG_COUNT) { |
| 619 | cond_resched(); |
| 620 | count = 0; |
| 621 | } |
| 622 | } |
| 623 | conn_put(con); |
| 624 | } |
| 625 | |
| 626 | static void tipc_send_work(struct work_struct *work) |
| 627 | { |
| 628 | struct tipc_conn *con = container_of(work, struct tipc_conn, swork); |
| 629 | |
| 630 | if (test_bit(CF_CONNECTED, &con->flags)) |
| 631 | tipc_send_to_sock(con); |
| 632 | |
| 633 | conn_put(con); |
| 634 | } |
| 635 | |
| 636 | static void tipc_work_stop(struct tipc_server *s) |
| 637 | { |
| 638 | destroy_workqueue(s->rcv_wq); |
| 639 | destroy_workqueue(s->send_wq); |
| 640 | } |
| 641 | |
| 642 | static int tipc_work_start(struct tipc_server *s) |
| 643 | { |
Parthasarathy Bhuvaragan | 06c8581 | 2016-02-02 10:52:17 +0100 | [diff] [blame] | 644 | s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 645 | if (!s->rcv_wq) { |
| 646 | pr_err("can't start tipc receive workqueue\n"); |
| 647 | return -ENOMEM; |
| 648 | } |
| 649 | |
Parthasarathy Bhuvaragan | 06c8581 | 2016-02-02 10:52:17 +0100 | [diff] [blame] | 650 | s->send_wq = alloc_ordered_workqueue("tipc_send", 0); |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 651 | if (!s->send_wq) { |
| 652 | pr_err("can't start tipc send workqueue\n"); |
| 653 | destroy_workqueue(s->rcv_wq); |
| 654 | return -ENOMEM; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | int tipc_server_start(struct tipc_server *s) |
| 661 | { |
| 662 | int ret; |
| 663 | |
| 664 | spin_lock_init(&s->idr_lock); |
| 665 | idr_init(&s->conn_idr); |
| 666 | s->idr_in_use = 0; |
| 667 | |
| 668 | s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size, |
| 669 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 670 | if (!s->rcvbuf_cache) |
| 671 | return -ENOMEM; |
| 672 | |
| 673 | ret = tipc_work_start(s); |
| 674 | if (ret < 0) { |
| 675 | kmem_cache_destroy(s->rcvbuf_cache); |
| 676 | return ret; |
| 677 | } |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 678 | ret = tipc_open_listening_sock(s); |
| 679 | if (ret < 0) { |
| 680 | tipc_work_stop(s); |
| 681 | kmem_cache_destroy(s->rcvbuf_cache); |
| 682 | return ret; |
| 683 | } |
Ying Xue | c756891a | 2013-08-01 08:29:18 -0400 | [diff] [blame] | 684 | return ret; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void tipc_server_stop(struct tipc_server *s) |
| 688 | { |
| 689 | struct tipc_conn *con; |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 690 | int id; |
| 691 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 692 | spin_lock_bh(&s->idr_lock); |
Parthasarathy Bhuvaragan | 35e22e4 | 2017-01-24 13:00:48 +0100 | [diff] [blame] | 693 | for (id = 0; s->idr_in_use; id++) { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 694 | con = idr_find(&s->conn_idr, id); |
| 695 | if (con) { |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 696 | spin_unlock_bh(&s->idr_lock); |
| 697 | tipc_close_conn(con); |
| 698 | spin_lock_bh(&s->idr_lock); |
| 699 | } |
| 700 | } |
| 701 | spin_unlock_bh(&s->idr_lock); |
| 702 | |
| 703 | tipc_work_stop(s); |
| 704 | kmem_cache_destroy(s->rcvbuf_cache); |
| 705 | idr_destroy(&s->conn_idr); |
| 706 | } |