blob: 0abbdd6986624f8938e95d9b01eb054a11db701a [file] [log] [blame]
Ying Xuec5fa7b32013-06-17 10:54:39 -04001/*
2 * net/tipc/server.c: TIPC server infrastructure
3 *
4 * Copyright (c) 2012-2013, Wind River Systems
Jon Maloydf79d042018-02-15 10:40:44 +01005 * Copyright (c) 2017, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04006 * 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 Maloyc901d262018-02-15 10:40:43 +010037#include "subscr.h"
Ying Xuec5fa7b32013-06-17 10:54:39 -040038#include "server.h"
39#include "core.h"
Ying Xue859fc7c2015-01-09 15:27:01 +080040#include "socket.h"
Jon Maloy14c04492017-10-13 11:04:17 +020041#include "addr.h"
42#include "msg.h"
Ying Xuec5fa7b32013-06-17 10:54:39 -040043#include <net/sock.h>
Ying Xue76100a82015-03-18 09:32:57 +080044#include <linux/module.h>
Ying Xuec5fa7b32013-06-17 10:54:39 -040045
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 Xue76100a82015-03-18 09:32:57 +080050#define CF_SERVER 2
Ying Xuec5fa7b32013-06-17 10:54:39 -040051
Jon Maloy5c45ab22018-02-15 10:40:49 +010052#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 */
72struct 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 Xuec5fa7b32013-06-17 10:54:39 -040083
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 Maloydf79d042018-02-15 10:40:44 +010091 * @sub_list: lsit to all pertaing subscriptions
92 * @sub_lock: lock protecting the subscription list
93 * @outqueue_lock: control access to the outqueue
Ying Xuec5fa7b32013-06-17 10:54:39 -040094 * @rwork: receive work item
Ying Xuec5fa7b32013-06-17 10:54:39 -040095 * @rx_action: what to do when connection socket is active
96 * @outqueue: pointer to first outbound message in queue
stephen hemminger963a18552014-01-12 12:48:00 -080097 * @outqueue_lock: control access to the outqueue
Ying Xuec5fa7b32013-06-17 10:54:39 -040098 * @swork: send work item
99 */
100struct tipc_conn {
101 struct kref kref;
102 int conid;
103 struct socket *sock;
104 unsigned long flags;
105 struct tipc_server *server;
Jon Maloydf79d042018-02-15 10:40:44 +0100106 struct list_head sub_list;
107 spinlock_t sub_lock; /* for subscription list */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400108 struct work_struct rwork;
109 int (*rx_action) (struct tipc_conn *con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400110 struct list_head outqueue;
111 spinlock_t outqueue_lock;
112 struct work_struct swork;
113};
114
115/* An entry waiting to be sent */
116struct outqueue_entry {
Jon Maloy414574a2018-02-15 10:40:45 +0100117 bool inactive;
118 struct tipc_event evt;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400119 struct list_head list;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400120};
121
122static void tipc_recv_work(struct work_struct *work);
123static void tipc_send_work(struct work_struct *work);
124static void tipc_clean_outqueues(struct tipc_conn *con);
125
Jon Maloy5c45ab22018-02-15 10:40:49 +0100126static struct tipc_conn *sock2con(struct sock *sk)
127{
128 return sk->sk_user_data;
129}
130
Jon Maloydf79d042018-02-15 10:40:44 +0100131static bool connected(struct tipc_conn *con)
132{
133 return con && test_bit(CF_CONNECTED, &con->flags);
134}
135
Ying Xuec5fa7b32013-06-17 10:54:39 -0400136static void tipc_conn_kref_release(struct kref *kref)
137{
138 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
Parthasarathy Bhuvaraganfc0adfc2017-01-24 13:00:45 +0100139 struct tipc_server *s = con->server;
Ying Xue76100a82015-03-18 09:32:57 +0800140 struct socket *sock = con->sock;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400141
Ying Xue76100a82015-03-18 09:32:57 +0800142 if (sock) {
Ying Xue76100a82015-03-18 09:32:57 +0800143 if (test_bit(CF_SERVER, &con->flags)) {
144 __module_get(sock->ops->owner);
Jon Maloydf79d042018-02-15 10:40:44 +0100145 __module_get(sock->sk->sk_prot_creator->owner);
Ying Xue76100a82015-03-18 09:32:57 +0800146 }
Ying Xuedef81f62015-04-23 09:37:38 -0400147 sock_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400148 con->sock = NULL;
149 }
Jon Maloy14c04492017-10-13 11:04:17 +0200150 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 Xuec5fa7b32013-06-17 10:54:39 -0400154 tipc_clean_outqueues(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400155 kfree(con);
156}
157
158static void conn_put(struct tipc_conn *con)
159{
160 kref_put(&con->kref, tipc_conn_kref_release);
161}
162
163static void conn_get(struct tipc_conn *con)
164{
165 kref_get(&con->kref);
166}
167
168static 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 Maloydf79d042018-02-15 10:40:44 +0100174 if (!connected(con) || !kref_get_unless_zero(&con->kref))
175 con = NULL;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400176 spin_unlock_bh(&s->idr_lock);
177 return con;
178}
179
Jon Maloy414574a2018-02-15 10:40:45 +0100180/* 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. Miller676d2362014-04-11 16:15:36 -0400183static void sock_data_ready(struct sock *sk)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400184{
185 struct tipc_conn *con;
186
Eric Dumazetb91083a2016-05-17 17:44:09 -0700187 read_lock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400188 con = sock2con(sk);
Jon Maloydf79d042018-02-15 10:40:44 +0100189 if (connected(con)) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400190 conn_get(con);
191 if (!queue_work(con->server->rcv_wq, &con->rwork))
192 conn_put(con);
193 }
Eric Dumazetb91083a2016-05-17 17:44:09 -0700194 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400195}
196
Jon Maloy414574a2018-02-15 10:40:45 +0100197/* 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 Xuec5fa7b32013-06-17 10:54:39 -0400201static void sock_write_space(struct sock *sk)
202{
203 struct tipc_conn *con;
204
Eric Dumazetb91083a2016-05-17 17:44:09 -0700205 read_lock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400206 con = sock2con(sk);
Jon Maloydf79d042018-02-15 10:40:44 +0100207 if (connected(con)) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400208 conn_get(con);
209 if (!queue_work(con->server->send_wq, &con->swork))
210 conn_put(con);
211 }
Eric Dumazetb91083a2016-05-17 17:44:09 -0700212 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400213}
214
215static 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 Maloydf79d042018-02-15 10:40:44 +0100230/* tipc_con_delete_sub - delete a specific or all subscriptions
231 * for a given subscriber
232 */
233static void tipc_con_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
234{
235 struct list_head *sub_list = &con->sub_list;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100236 struct tipc_net *tn = tipc_net(con->server->net);
Jon Maloydf79d042018-02-15 10:40:44 +0100237 struct tipc_subscription *sub, *tmp;
238
239 spin_lock_bh(&con->sub_lock);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100240 list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
Jon Maloy5c45ab22018-02-15 10:40:49 +0100241 if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
Jon Maloy242e82c2018-02-15 10:40:47 +0100242 tipc_sub_unsubscribe(sub);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100243 atomic_dec(&tn->subscription_count);
244 } else if (s) {
Jon Maloydf79d042018-02-15 10:40:44 +0100245 break;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100246 }
Jon Maloydf79d042018-02-15 10:40:44 +0100247 }
248 spin_unlock_bh(&con->sub_lock);
249}
250
Parthasarathy Bhuvaragan9dc3abd2017-01-24 13:00:46 +0100251static void tipc_close_conn(struct tipc_conn *con)
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200252{
Jon Maloye88f2be2018-01-15 17:56:28 +0100253 struct sock *sk = con->sock->sk;
254 bool disconnect = false;
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200255
Jon Maloye88f2be2018-01-15 17:56:28 +0100256 write_lock_bh(&sk->sk_callback_lock);
257 disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
Jon Maloydf79d042018-02-15 10:40:44 +0100258
Jon Maloye88f2be2018-01-15 17:56:28 +0100259 if (disconnect) {
260 sk->sk_user_data = NULL;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100261 tipc_con_delete_sub(con, NULL);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400262 }
Jon Maloye88f2be2018-01-15 17:56:28 +0100263 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 Xuec5fa7b32013-06-17 10:54:39 -0400272}
273
274static 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 Maloydf79d042018-02-15 10:40:44 +0100285 INIT_LIST_HEAD(&con->sub_list);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400286 spin_lock_init(&con->outqueue_lock);
Jon Maloydf79d042018-02-15 10:40:44 +0100287 spin_lock_init(&con->sub_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400288 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 Maloy414574a2018-02-15 10:40:45 +0100308static int tipc_con_rcv_sub(struct tipc_server *srv,
309 struct tipc_conn *con,
310 struct tipc_subscr *s)
Jon Maloydf79d042018-02-15 10:40:44 +0100311{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100312 struct tipc_net *tn = tipc_net(srv->net);
Jon Maloydf79d042018-02-15 10:40:44 +0100313 struct tipc_subscription *sub;
Jon Maloydf79d042018-02-15 10:40:44 +0100314
Jon Maloy8985ecc2018-02-15 10:40:46 +0100315 if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) {
Jon Maloydf79d042018-02-15 10:40:44 +0100316 tipc_con_delete_sub(con, s);
317 return 0;
318 }
Jon Maloy5c45ab22018-02-15 10:40:49 +0100319 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 Maloydf79d042018-02-15 10:40:44 +0100324 if (!sub)
325 return -1;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100326 atomic_inc(&tn->subscription_count);
Jon Maloydf79d042018-02-15 10:40:44 +0100327 spin_lock_bh(&con->sub_lock);
Jon Maloyda0a75e2018-02-15 10:40:48 +0100328 list_add(&sub->sub_list, &con->sub_list);
Jon Maloydf79d042018-02-15 10:40:44 +0100329 spin_unlock_bh(&con->sub_lock);
330 return 0;
331}
332
Ying Xuec5fa7b32013-06-17 10:54:39 -0400333static int tipc_receive_from_sock(struct tipc_conn *con)
334{
Jon Maloy414574a2018-02-15 10:40:45 +0100335 struct tipc_server *srv = con->server;
Jon Maloye88f2be2018-01-15 17:56:28 +0100336 struct sock *sk = con->sock->sk;
Jon Maloye88f2be2018-01-15 17:56:28 +0100337 struct msghdr msg = {};
Jon Maloy414574a2018-02-15 10:40:45 +0100338 struct tipc_subscr s;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400339 struct kvec iov;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400340 int ret;
341
Jon Maloy414574a2018-02-15 10:40:45 +0100342 iov.iov_base = &s;
343 iov.iov_len = sizeof(s);
Jon Maloyc901d262018-02-15 10:40:43 +0100344 msg.msg_name = NULL;
Al Virobc480272017-09-20 22:08:04 -0400345 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, iov.iov_len);
346 ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT);
Jon Maloy414574a2018-02-15 10:40:45 +0100347 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 Xuec5fa7b32013-06-17 10:54:39 -0400353 }
Jon Maloye88f2be2018-01-15 17:56:28 +0100354 if (ret < 0)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400355 tipc_close_conn(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400356
357 return ret;
358}
359
360static int tipc_accept_from_sock(struct tipc_conn *con)
361{
Ying Xuec5fa7b32013-06-17 10:54:39 -0400362 struct socket *sock = con->sock;
363 struct socket *newsock;
364 struct tipc_conn *newcon;
365 int ret;
366
Ying Xue76100a82015-03-18 09:32:57 +0800367 ret = kernel_accept(sock, &newsock, O_NONBLOCK);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400368 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 Xuec5fa7b32013-06-17 10:54:39 -0400381 /* Wake up receive process in case of 'SYN+' message */
David S. Miller676d2362014-04-11 16:15:36 -0400382 newsock->sk->sk_data_ready(newsock->sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400383 return ret;
384}
385
386static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
387{
388 struct tipc_server *s = con->server;
389 struct socket *sock = NULL;
Jon Maloy27469b72018-02-15 10:40:42 +0100390 int imp = TIPC_CRITICAL_IMPORTANCE;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400391 int ret;
392
Ying Xuefa787ae2015-05-13 11:20:38 +0800393 ret = sock_create_kern(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400394 if (ret < 0)
395 return NULL;
396 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
Jon Maloy27469b72018-02-15 10:40:42 +0100397 (char *)&imp, sizeof(imp));
Ying Xuec5fa7b32013-06-17 10:54:39 -0400398 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 Maloy27469b72018-02-15 10:40:42 +0100404 con->rx_action = tipc_accept_from_sock;
405 ret = kernel_listen(sock, 0);
406 if (ret < 0)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400407 goto create_err;
Ying Xue76100a82015-03-18 09:32:57 +0800408
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 Xuec5fa7b32013-06-17 10:54:39 -0400428 return sock;
429
430create_err:
Ying Xue76100a82015-03-18 09:32:57 +0800431 kernel_sock_shutdown(sock, SHUT_RDWR);
Ying Xuedef81f62015-04-23 09:37:38 -0400432 sock_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400433 return NULL;
434}
435
436static 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 Xuec756891a2013-08-01 08:29:18 -0400446 if (!sock) {
447 idr_remove(&s->conn_idr, con->conid);
448 s->idr_in_use--;
449 kfree(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400450 return -EINVAL;
Ying Xuec756891a2013-08-01 08:29:18 -0400451 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400452
453 tipc_register_callbacks(sock, con);
454 return 0;
455}
456
Ying Xuec5fa7b32013-06-17 10:54:39 -0400457static 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 Maloy414574a2018-02-15 10:40:45 +0100464 kfree(e);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400465 }
466 spin_unlock_bh(&con->outqueue_lock);
467}
468
Jon Maloy414574a2018-02-15 10:40:45 +0100469/* tipc_conn_queue_evt - interrupt level call from a subscription instance
470 * The queued job is launched in tipc_send_to_sock()
471 */
Jon Maloy5c45ab22018-02-15 10:40:49 +0100472void tipc_conn_queue_evt(struct net *net, int conid,
Jon Maloy414574a2018-02-15 10:40:45 +0100473 u32 event, struct tipc_event *evt)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400474{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100475 struct tipc_server *srv = tipc_topsrv(net);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400476 struct outqueue_entry *e;
477 struct tipc_conn *con;
478
Jon Maloy5c45ab22018-02-15 10:40:49 +0100479 con = tipc_conn_lookup(srv, conid);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400480 if (!con)
Jon Maloy414574a2018-02-15 10:40:45 +0100481 return;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400482
Jon Maloy414574a2018-02-15 10:40:45 +0100483 if (!connected(con))
484 goto err;
Parthasarathy Bhuvaragan4c887aa2017-01-24 13:00:47 +0100485
Jon Maloy414574a2018-02-15 10:40:45 +0100486 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 Xuec5fa7b32013-06-17 10:54:39 -0400491 spin_lock_bh(&con->outqueue_lock);
492 list_add_tail(&e->list, &con->outqueue);
493 spin_unlock_bh(&con->outqueue_lock);
494
Jon Maloy5c45ab22018-02-15 10:40:49 +0100495 if (queue_work(srv->send_wq, &con->swork))
Jon Maloy414574a2018-02-15 10:40:45 +0100496 return;
497err:
498 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400499}
500
Jon Maloy232d07b2018-01-08 21:03:30 +0100501bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
502 u32 upper, u32 filter, int *conid)
Jon Maloy14c04492017-10-13 11:04:17 +0200503{
Jon Maloy14c04492017-10-13 11:04:17 +0200504 struct tipc_subscr sub;
Jon Maloy14c04492017-10-13 11:04:17 +0200505 struct tipc_conn *con;
Jon Maloydf79d042018-02-15 10:40:44 +0100506 int rc;
Jon Maloy14c04492017-10-13 11:04:17 +0200507
508 sub.seq.type = type;
509 sub.seq.lower = lower;
510 sub.seq.upper = upper;
511 sub.timeout = TIPC_WAIT_FOREVER;
Jon Maloy83485002018-01-08 21:03:29 +0100512 sub.filter = filter;
Jon Maloy14c04492017-10-13 11:04:17 +0200513 *(u32 *)&sub.usr_handle = port;
514
515 con = tipc_alloc_conn(tipc_topsrv(net));
Dan Carpenterc75e4272017-10-18 10:48:25 +0300516 if (IS_ERR(con))
Jon Maloy14c04492017-10-13 11:04:17 +0200517 return false;
518
519 *conid = con->conid;
Jon Maloy14c04492017-10-13 11:04:17 +0200520 con->sock = NULL;
Jon Maloy414574a2018-02-15 10:40:45 +0100521 rc = tipc_con_rcv_sub(tipc_topsrv(net), con, &sub);
Jon Maloydf79d042018-02-15 10:40:44 +0100522 if (rc < 0)
523 tipc_close_conn(con);
524 return !rc;
Jon Maloy14c04492017-10-13 11:04:17 +0200525}
526
527void 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 Maloye88f2be2018-01-15 17:56:28 +0100534
535 test_and_clear_bit(CF_CONNECTED, &con->flags);
Jon Maloydf79d042018-02-15 10:40:44 +0100536 tipc_con_delete_sub(con, NULL);
Jon Maloye88f2be2018-01-15 17:56:28 +0100537 conn_put(con);
Jon Maloy14c04492017-10-13 11:04:17 +0200538 conn_put(con);
539}
540
541static 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 Xuec5fa7b32013-06-17 10:54:39 -0400559static void tipc_send_to_sock(struct tipc_conn *con)
560{
Jon Maloydf79d042018-02-15 10:40:44 +0100561 struct list_head *queue = &con->outqueue;
562 struct tipc_server *srv = con->server;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400563 struct outqueue_entry *e;
Jon Maloy14c04492017-10-13 11:04:17 +0200564 struct tipc_event *evt;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400565 struct msghdr msg;
Jon Maloy414574a2018-02-15 10:40:45 +0100566 struct kvec iov;
Jon Maloy14c04492017-10-13 11:04:17 +0200567 int count = 0;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400568 int ret;
569
570 spin_lock_bh(&con->outqueue_lock);
Jon Maloydf79d042018-02-15 10:40:44 +0100571
572 while (!list_empty(queue)) {
573 e = list_first_entry(queue, struct outqueue_entry, list);
Jon Maloy414574a2018-02-15 10:40:45 +0100574 evt = &e->evt;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400575 spin_unlock_bh(&con->outqueue_lock);
576
Jon Maloy414574a2018-02-15 10:40:45 +0100577 if (e->inactive)
Jon Maloydf79d042018-02-15 10:40:44 +0100578 tipc_con_delete_sub(con, &evt->s);
Jon Maloy414574a2018-02-15 10:40:45 +0100579
Jon Maloydf79d042018-02-15 10:40:44 +0100580 memset(&msg, 0, sizeof(msg));
581 msg.msg_flags = MSG_DONTWAIT;
Jon Maloy414574a2018-02-15 10:40:45 +0100582 iov.iov_base = evt;
583 iov.iov_len = sizeof(*evt);
584 msg.msg_name = NULL;
Jon Maloydf79d042018-02-15 10:40:44 +0100585
Jon Maloy14c04492017-10-13 11:04:17 +0200586 if (con->sock) {
Jon Maloy414574a2018-02-15 10:40:45 +0100587 ret = kernel_sendmsg(con->sock, &msg, &iov,
588 1, sizeof(*evt));
Jon Maloy14c04492017-10-13 11:04:17 +0200589 if (ret == -EWOULDBLOCK || ret == 0) {
590 cond_resched();
591 goto out;
592 } else if (ret < 0) {
Jon Maloy414574a2018-02-15 10:40:45 +0100593 goto err;
Jon Maloy14c04492017-10-13 11:04:17 +0200594 }
595 } else {
Jon Maloydf79d042018-02-15 10:40:44 +0100596 tipc_send_kern_top_evt(srv->net, evt);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400597 }
Jon Maloy27469b72018-02-15 10:40:42 +0100598
Ying Xuec5fa7b32013-06-17 10:54:39 -0400599 /* Don't starve users filling buffers */
600 if (++count >= MAX_SEND_MSG_COUNT) {
601 cond_resched();
602 count = 0;
603 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400604 spin_lock_bh(&con->outqueue_lock);
605 list_del(&e->list);
Jon Maloy414574a2018-02-15 10:40:45 +0100606 kfree(e);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400607 }
608 spin_unlock_bh(&con->outqueue_lock);
609out:
610 return;
Jon Maloy414574a2018-02-15 10:40:45 +0100611err:
Ying Xuec5fa7b32013-06-17 10:54:39 -0400612 tipc_close_conn(con);
613}
614
615static 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 Maloydf79d042018-02-15 10:40:44 +0100620 while (connected(con)) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400621 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
633static void tipc_send_work(struct work_struct *work)
634{
635 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
636
Jon Maloydf79d042018-02-15 10:40:44 +0100637 if (connected(con))
Ying Xuec5fa7b32013-06-17 10:54:39 -0400638 tipc_send_to_sock(con);
639
640 conn_put(con);
641}
642
643static void tipc_work_stop(struct tipc_server *s)
644{
645 destroy_workqueue(s->rcv_wq);
646 destroy_workqueue(s->send_wq);
647}
648
649static int tipc_work_start(struct tipc_server *s)
650{
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100651 s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400652 if (!s->rcv_wq) {
653 pr_err("can't start tipc receive workqueue\n");
654 return -ENOMEM;
655 }
656
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100657 s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400658 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 Maloy5c45ab22018-02-15 10:40:49 +0100667int tipc_topsrv_start(struct net *net)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400668{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100669 struct tipc_net *tn = tipc_net(net);
670 const char name[] = "topology_server";
671 struct sockaddr_tipc *saddr;
672 struct tipc_server *srv;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400673 int ret;
674
Jon Maloy5c45ab22018-02-15 10:40:49 +0100675 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 Xuec5fa7b32013-06-17 10:54:39 -0400684
Jon Maloy5c45ab22018-02-15 10:40:49 +0100685 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 Maloy414574a2018-02-15 10:40:45 +0100703 if (ret < 0)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400704 return ret;
Jon Maloy414574a2018-02-15 10:40:45 +0100705
Jon Maloy5c45ab22018-02-15 10:40:49 +0100706 ret = tipc_open_listening_sock(srv);
Jon Maloy414574a2018-02-15 10:40:45 +0100707 if (ret < 0)
Jon Maloy5c45ab22018-02-15 10:40:49 +0100708 tipc_work_stop(srv);
Jon Maloy414574a2018-02-15 10:40:45 +0100709
Ying Xuec756891a2013-08-01 08:29:18 -0400710 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400711}
712
Jon Maloy5c45ab22018-02-15 10:40:49 +0100713void tipc_topsrv_stop(struct net *net)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400714{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100715 struct tipc_server *srv = tipc_topsrv(net);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400716 struct tipc_conn *con;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400717 int id;
718
Jon Maloy5c45ab22018-02-15 10:40:49 +0100719 spin_lock_bh(&srv->idr_lock);
720 for (id = 0; srv->idr_in_use; id++) {
721 con = idr_find(&srv->conn_idr, id);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400722 if (con) {
Jon Maloy5c45ab22018-02-15 10:40:49 +0100723 spin_unlock_bh(&srv->idr_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400724 tipc_close_conn(con);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100725 spin_lock_bh(&srv->idr_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400726 }
727 }
Jon Maloy5c45ab22018-02-15 10:40:49 +0100728 spin_unlock_bh(&srv->idr_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400729
Jon Maloy5c45ab22018-02-15 10:40:49 +0100730 tipc_work_stop(srv);
731 idr_destroy(&srv->conn_idr);
732 kfree(srv->saddr);
733 kfree(srv);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400734}