blob: 8ee0c07d00e9bb16259ff2776a111b55885e8bda [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 Maloy026321c2018-02-15 10:40:51 +01005 * Copyright (c) 2017-2018, 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"
Jon Maloy026321c2018-02-15 10:40:51 +010038#include "topsrv.h"
Ying Xuec5fa7b32013-06-17 10:54:39 -040039#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"
John Rutherford6c9081a2019-08-07 12:52:29 +100043#include "bearer.h"
Ying Xuec5fa7b32013-06-17 10:54:39 -040044#include <net/sock.h>
Ying Xue76100a82015-03-18 09:32:57 +080045#include <linux/module.h>
Peilin Ye40e0b092023-01-19 16:45:16 -080046#include <trace/events/sock.h>
Ying Xuec5fa7b32013-06-17 10:54:39 -040047
48/* Number of messages to send before rescheduling */
49#define MAX_SEND_MSG_COUNT 25
50#define MAX_RECV_MSG_COUNT 25
51#define CF_CONNECTED 1
52
Jon Maloy5c45ab22018-02-15 10:40:49 +010053#define TIPC_SERVER_NAME_LEN 32
54
55/**
Jon Maloy026321c2018-02-15 10:40:51 +010056 * struct tipc_topsrv - TIPC server structure
Jon Maloy5c45ab22018-02-15 10:40:49 +010057 * @conn_idr: identifier set of connection
58 * @idr_lock: protect the connection identifier set
59 * @idr_in_use: amount of allocated identifier entry
60 * @net: network namspace instance
Zhenbo Gao05a68432018-09-03 14:08:40 +080061 * @awork: accept work item
Jon Maloy5c45ab22018-02-15 10:40:49 +010062 * @rcv_wq: receive workqueue
63 * @send_wq: send workqueue
Zhenbo Gao05a68432018-09-03 14:08:40 +080064 * @listener: topsrv listener socket
Jon Maloy5c45ab22018-02-15 10:40:49 +010065 * @name: server name
Jon Maloy5c45ab22018-02-15 10:40:49 +010066 */
Jon Maloy026321c2018-02-15 10:40:51 +010067struct tipc_topsrv {
Jon Maloy5c45ab22018-02-15 10:40:49 +010068 struct idr conn_idr;
69 spinlock_t idr_lock; /* for idr list */
70 int idr_in_use;
71 struct net *net;
Jon Maloy0ef897b2018-02-15 10:40:50 +010072 struct work_struct awork;
Jon Maloy5c45ab22018-02-15 10:40:49 +010073 struct workqueue_struct *rcv_wq;
74 struct workqueue_struct *send_wq;
Jon Maloy0ef897b2018-02-15 10:40:50 +010075 struct socket *listener;
Jon Maloy5c45ab22018-02-15 10:40:49 +010076 char name[TIPC_SERVER_NAME_LEN];
77};
Ying Xuec5fa7b32013-06-17 10:54:39 -040078
79/**
80 * struct tipc_conn - TIPC connection structure
81 * @kref: reference counter to connection object
82 * @conid: connection identifier
83 * @sock: socket handler associated with connection
84 * @flags: indicates connection state
85 * @server: pointer to connected server
Jon Maloydf79d042018-02-15 10:40:44 +010086 * @sub_list: lsit to all pertaing subscriptions
87 * @sub_lock: lock protecting the subscription list
Ying Xuec5fa7b32013-06-17 10:54:39 -040088 * @rwork: receive work item
Ying Xuec5fa7b32013-06-17 10:54:39 -040089 * @outqueue: pointer to first outbound message in queue
stephen hemminger963a18552014-01-12 12:48:00 -080090 * @outqueue_lock: control access to the outqueue
Ying Xuec5fa7b32013-06-17 10:54:39 -040091 * @swork: send work item
92 */
93struct tipc_conn {
94 struct kref kref;
95 int conid;
96 struct socket *sock;
97 unsigned long flags;
Jon Maloy026321c2018-02-15 10:40:51 +010098 struct tipc_topsrv *server;
Jon Maloydf79d042018-02-15 10:40:44 +010099 struct list_head sub_list;
100 spinlock_t sub_lock; /* for subscription list */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400101 struct work_struct rwork;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400102 struct list_head outqueue;
Jon Maloy026321c2018-02-15 10:40:51 +0100103 spinlock_t outqueue_lock; /* for outqueue */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400104 struct work_struct swork;
105};
106
107/* An entry waiting to be sent */
108struct outqueue_entry {
Jon Maloy414574a2018-02-15 10:40:45 +0100109 bool inactive;
110 struct tipc_event evt;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400111 struct list_head list;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400112};
113
Jon Maloy026321c2018-02-15 10:40:51 +0100114static void tipc_conn_recv_work(struct work_struct *work);
115static void tipc_conn_send_work(struct work_struct *work);
116static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt);
117static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100118
Jon Maloydf79d042018-02-15 10:40:44 +0100119static bool connected(struct tipc_conn *con)
120{
121 return con && test_bit(CF_CONNECTED, &con->flags);
122}
123
Ying Xuec5fa7b32013-06-17 10:54:39 -0400124static void tipc_conn_kref_release(struct kref *kref)
125{
126 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
Jon Maloy026321c2018-02-15 10:40:51 +0100127 struct tipc_topsrv *s = con->server;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100128 struct outqueue_entry *e, *safe;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400129
Jon Maloy14c04492017-10-13 11:04:17 +0200130 spin_lock_bh(&s->idr_lock);
131 idr_remove(&s->conn_idr, con->conid);
132 s->idr_in_use--;
133 spin_unlock_bh(&s->idr_lock);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100134 if (con->sock)
135 sock_release(con->sock);
136
137 spin_lock_bh(&con->outqueue_lock);
138 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
139 list_del(&e->list);
140 kfree(e);
141 }
142 spin_unlock_bh(&con->outqueue_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400143 kfree(con);
144}
145
146static void conn_put(struct tipc_conn *con)
147{
148 kref_put(&con->kref, tipc_conn_kref_release);
149}
150
151static void conn_get(struct tipc_conn *con)
152{
153 kref_get(&con->kref);
154}
155
Jon Maloy026321c2018-02-15 10:40:51 +0100156static void tipc_conn_close(struct tipc_conn *con)
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200157{
Jon Maloye88f2be2018-01-15 17:56:28 +0100158 struct sock *sk = con->sock->sk;
159 bool disconnect = false;
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200160
Jon Maloye88f2be2018-01-15 17:56:28 +0100161 write_lock_bh(&sk->sk_callback_lock);
162 disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
Jon Maloydf79d042018-02-15 10:40:44 +0100163
Jon Maloye88f2be2018-01-15 17:56:28 +0100164 if (disconnect) {
165 sk->sk_user_data = NULL;
Jon Maloy026321c2018-02-15 10:40:51 +0100166 tipc_conn_delete_sub(con, NULL);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400167 }
Jon Maloye88f2be2018-01-15 17:56:28 +0100168 write_unlock_bh(&sk->sk_callback_lock);
169
170 /* Handle concurrent calls from sending and receiving threads */
171 if (!disconnect)
172 return;
173
174 /* Don't flush pending works, -just let them expire */
175 kernel_sock_shutdown(con->sock, SHUT_RDWR);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100176
Jon Maloye88f2be2018-01-15 17:56:28 +0100177 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400178}
179
Xin Long0e5d56c2022-11-18 16:45:00 -0500180static struct tipc_conn *tipc_conn_alloc(struct tipc_topsrv *s, struct socket *sock)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400181{
182 struct tipc_conn *con;
183 int ret;
184
Jon Maloy026321c2018-02-15 10:40:51 +0100185 con = kzalloc(sizeof(*con), GFP_ATOMIC);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400186 if (!con)
187 return ERR_PTR(-ENOMEM);
188
189 kref_init(&con->kref);
190 INIT_LIST_HEAD(&con->outqueue);
Jon Maloydf79d042018-02-15 10:40:44 +0100191 INIT_LIST_HEAD(&con->sub_list);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400192 spin_lock_init(&con->outqueue_lock);
Jon Maloydf79d042018-02-15 10:40:44 +0100193 spin_lock_init(&con->sub_lock);
Jon Maloy026321c2018-02-15 10:40:51 +0100194 INIT_WORK(&con->swork, tipc_conn_send_work);
195 INIT_WORK(&con->rwork, tipc_conn_recv_work);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400196
197 spin_lock_bh(&s->idr_lock);
198 ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
199 if (ret < 0) {
200 kfree(con);
201 spin_unlock_bh(&s->idr_lock);
202 return ERR_PTR(-ENOMEM);
203 }
204 con->conid = ret;
205 s->idr_in_use++;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400206
207 set_bit(CF_CONNECTED, &con->flags);
208 con->server = s;
Xin Long0e5d56c2022-11-18 16:45:00 -0500209 con->sock = sock;
Xin Longa7b42962022-11-18 16:45:01 -0500210 conn_get(con);
Xin Long0e5d56c2022-11-18 16:45:00 -0500211 spin_unlock_bh(&s->idr_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400212
213 return con;
214}
215
Jon Maloy026321c2018-02-15 10:40:51 +0100216static struct tipc_conn *tipc_conn_lookup(struct tipc_topsrv *s, int conid)
Jon Maloydf79d042018-02-15 10:40:44 +0100217{
Jon Maloy026321c2018-02-15 10:40:51 +0100218 struct tipc_conn *con;
Jon Maloydf79d042018-02-15 10:40:44 +0100219
Jon Maloy026321c2018-02-15 10:40:51 +0100220 spin_lock_bh(&s->idr_lock);
221 con = idr_find(&s->conn_idr, conid);
222 if (!connected(con) || !kref_get_unless_zero(&con->kref))
223 con = NULL;
224 spin_unlock_bh(&s->idr_lock);
225 return con;
Jon Maloydf79d042018-02-15 10:40:44 +0100226}
227
Jon Maloy026321c2018-02-15 10:40:51 +0100228/* tipc_conn_delete_sub - delete a specific or all subscriptions
229 * for a given subscriber
230 */
231static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400232{
Jon Maloy026321c2018-02-15 10:40:51 +0100233 struct tipc_net *tn = tipc_net(con->server->net);
234 struct list_head *sub_list = &con->sub_list;
235 struct tipc_subscription *sub, *tmp;
236
237 spin_lock_bh(&con->sub_lock);
238 list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
239 if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
240 tipc_sub_unsubscribe(sub);
241 atomic_dec(&tn->subscription_count);
Tuong Lien88690b12020-05-13 19:33:18 +0700242 if (s)
243 break;
Jon Maloy026321c2018-02-15 10:40:51 +0100244 }
245 }
246 spin_unlock_bh(&con->sub_lock);
247}
248
249static void tipc_conn_send_to_sock(struct tipc_conn *con)
250{
251 struct list_head *queue = &con->outqueue;
252 struct tipc_topsrv *srv = con->server;
253 struct outqueue_entry *e;
254 struct tipc_event *evt;
255 struct msghdr msg;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400256 struct kvec iov;
Jon Maloy026321c2018-02-15 10:40:51 +0100257 int count = 0;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400258 int ret;
259
Jon Maloy026321c2018-02-15 10:40:51 +0100260 spin_lock_bh(&con->outqueue_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400261
Jon Maloy026321c2018-02-15 10:40:51 +0100262 while (!list_empty(queue)) {
263 e = list_first_entry(queue, struct outqueue_entry, list);
264 evt = &e->evt;
265 spin_unlock_bh(&con->outqueue_lock);
266
267 if (e->inactive)
268 tipc_conn_delete_sub(con, &evt->s);
269
270 memset(&msg, 0, sizeof(msg));
271 msg.msg_flags = MSG_DONTWAIT;
272 iov.iov_base = evt;
273 iov.iov_len = sizeof(*evt);
274 msg.msg_name = NULL;
275
276 if (con->sock) {
277 ret = kernel_sendmsg(con->sock, &msg, &iov,
278 1, sizeof(*evt));
279 if (ret == -EWOULDBLOCK || ret == 0) {
280 cond_resched();
281 return;
282 } else if (ret < 0) {
283 return tipc_conn_close(con);
284 }
285 } else {
286 tipc_topsrv_kern_evt(srv->net, evt);
287 }
288
289 /* Don't starve users filling buffers */
290 if (++count >= MAX_SEND_MSG_COUNT) {
291 cond_resched();
292 count = 0;
293 }
294 spin_lock_bh(&con->outqueue_lock);
295 list_del(&e->list);
296 kfree(e);
297 }
298 spin_unlock_bh(&con->outqueue_lock);
299}
300
301static void tipc_conn_send_work(struct work_struct *work)
302{
303 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
304
305 if (connected(con))
306 tipc_conn_send_to_sock(con);
307
308 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400309}
310
Zhenbo Gaoa484ef32018-09-03 16:36:46 +0800311/* tipc_topsrv_queue_evt() - interrupt level call from a subscription instance
312 * The queued work is launched into tipc_conn_send_work()->tipc_conn_send_to_sock()
Jon Maloy414574a2018-02-15 10:40:45 +0100313 */
Jon Maloy026321c2018-02-15 10:40:51 +0100314void tipc_topsrv_queue_evt(struct net *net, int conid,
315 u32 event, struct tipc_event *evt)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400316{
Jon Maloy026321c2018-02-15 10:40:51 +0100317 struct tipc_topsrv *srv = tipc_topsrv(net);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400318 struct outqueue_entry *e;
319 struct tipc_conn *con;
320
Jon Maloy5c45ab22018-02-15 10:40:49 +0100321 con = tipc_conn_lookup(srv, conid);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400322 if (!con)
Jon Maloy414574a2018-02-15 10:40:45 +0100323 return;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400324
Jon Maloy414574a2018-02-15 10:40:45 +0100325 if (!connected(con))
326 goto err;
Parthasarathy Bhuvaragan4c887aa62017-01-24 13:00:47 +0100327
Jon Maloy414574a2018-02-15 10:40:45 +0100328 e = kmalloc(sizeof(*e), GFP_ATOMIC);
329 if (!e)
330 goto err;
331 e->inactive = (event == TIPC_SUBSCR_TIMEOUT);
332 memcpy(&e->evt, evt, sizeof(*evt));
Ying Xuec5fa7b32013-06-17 10:54:39 -0400333 spin_lock_bh(&con->outqueue_lock);
334 list_add_tail(&e->list, &con->outqueue);
335 spin_unlock_bh(&con->outqueue_lock);
336
Jon Maloy5c45ab22018-02-15 10:40:49 +0100337 if (queue_work(srv->send_wq, &con->swork))
Jon Maloy414574a2018-02-15 10:40:45 +0100338 return;
339err:
340 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400341}
342
Jon Maloy026321c2018-02-15 10:40:51 +0100343/* tipc_conn_write_space - interrupt callback after a sendmsg EAGAIN
344 * Indicates that there now is more space in the send buffer
345 * The queued work is launched into tipc_send_work()->tipc_conn_send_to_sock()
346 */
347static void tipc_conn_write_space(struct sock *sk)
Jon Maloy14c04492017-10-13 11:04:17 +0200348{
349 struct tipc_conn *con;
350
Jon Maloy026321c2018-02-15 10:40:51 +0100351 read_lock_bh(&sk->sk_callback_lock);
352 con = sk->sk_user_data;
353 if (connected(con)) {
354 conn_get(con);
355 if (!queue_work(con->server->send_wq, &con->swork))
356 conn_put(con);
357 }
358 read_unlock_bh(&sk->sk_callback_lock);
Jon Maloy14c04492017-10-13 11:04:17 +0200359}
360
Jon Maloy026321c2018-02-15 10:40:51 +0100361static int tipc_conn_rcv_sub(struct tipc_topsrv *srv,
362 struct tipc_conn *con,
363 struct tipc_subscr *s)
Jon Maloy14c04492017-10-13 11:04:17 +0200364{
Jon Maloy026321c2018-02-15 10:40:51 +0100365 struct tipc_net *tn = tipc_net(srv->net);
366 struct tipc_subscription *sub;
Tuong Lien88690b12020-05-13 19:33:18 +0700367 u32 s_filter = tipc_sub_read(s, filter);
Jon Maloy14c04492017-10-13 11:04:17 +0200368
Tuong Lien88690b12020-05-13 19:33:18 +0700369 if (s_filter & TIPC_SUB_CANCEL) {
370 tipc_sub_write(s, filter, s_filter & ~TIPC_SUB_CANCEL);
Jon Maloy026321c2018-02-15 10:40:51 +0100371 tipc_conn_delete_sub(con, s);
372 return 0;
373 }
374 if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) {
375 pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR);
376 return -1;
377 }
378 sub = tipc_sub_subscribe(srv->net, s, con->conid);
379 if (!sub)
380 return -1;
381 atomic_inc(&tn->subscription_count);
382 spin_lock_bh(&con->sub_lock);
383 list_add(&sub->sub_list, &con->sub_list);
384 spin_unlock_bh(&con->sub_lock);
385 return 0;
Jon Maloy14c04492017-10-13 11:04:17 +0200386}
387
Jon Maloy026321c2018-02-15 10:40:51 +0100388static int tipc_conn_rcv_from_sock(struct tipc_conn *con)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400389{
Jon Maloy026321c2018-02-15 10:40:51 +0100390 struct tipc_topsrv *srv = con->server;
391 struct sock *sk = con->sock->sk;
392 struct msghdr msg = {};
393 struct tipc_subscr s;
Jon Maloy414574a2018-02-15 10:40:45 +0100394 struct kvec iov;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400395 int ret;
396
Jon Maloy026321c2018-02-15 10:40:51 +0100397 iov.iov_base = &s;
398 iov.iov_len = sizeof(s);
399 msg.msg_name = NULL;
Al Virode4eda92022-09-15 20:25:47 -0400400 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, iov.iov_len);
Jon Maloy026321c2018-02-15 10:40:51 +0100401 ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT);
402 if (ret == -EWOULDBLOCK)
403 return -EWOULDBLOCK;
Ying Xuea88289f2019-01-14 17:22:24 +0800404 if (ret == sizeof(s)) {
Jon Maloy026321c2018-02-15 10:40:51 +0100405 read_lock_bh(&sk->sk_callback_lock);
Tuong Lien0771d7d2020-05-13 19:33:17 +0700406 /* RACE: the connection can be closed in the meantime */
407 if (likely(connected(con)))
408 ret = tipc_conn_rcv_sub(srv, con, &s);
Jon Maloy026321c2018-02-15 10:40:51 +0100409 read_unlock_bh(&sk->sk_callback_lock);
Tuong Lien980d6922020-05-04 11:15:54 +0700410 if (!ret)
411 return 0;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400412 }
Jon Maloy026321c2018-02-15 10:40:51 +0100413
Tuong Lien980d6922020-05-04 11:15:54 +0700414 tipc_conn_close(con);
Jon Maloy026321c2018-02-15 10:40:51 +0100415 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400416}
417
Jon Maloy026321c2018-02-15 10:40:51 +0100418static void tipc_conn_recv_work(struct work_struct *work)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400419{
420 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
421 int count = 0;
422
Jon Maloydf79d042018-02-15 10:40:44 +0100423 while (connected(con)) {
Jon Maloy026321c2018-02-15 10:40:51 +0100424 if (tipc_conn_rcv_from_sock(con))
Ying Xuec5fa7b32013-06-17 10:54:39 -0400425 break;
426
427 /* Don't flood Rx machine */
428 if (++count >= MAX_RECV_MSG_COUNT) {
429 cond_resched();
430 count = 0;
431 }
432 }
433 conn_put(con);
434}
435
Jon Maloy026321c2018-02-15 10:40:51 +0100436/* tipc_conn_data_ready - interrupt callback indicating the socket has data
437 * The queued work is launched into tipc_recv_work()->tipc_conn_rcv_from_sock()
438 */
439static void tipc_conn_data_ready(struct sock *sk)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400440{
Jon Maloy026321c2018-02-15 10:40:51 +0100441 struct tipc_conn *con;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400442
Peilin Ye40e0b092023-01-19 16:45:16 -0800443 trace_sk_data_ready(sk);
444
Jon Maloy026321c2018-02-15 10:40:51 +0100445 read_lock_bh(&sk->sk_callback_lock);
446 con = sk->sk_user_data;
447 if (connected(con)) {
448 conn_get(con);
449 if (!queue_work(con->server->rcv_wq, &con->rwork))
450 conn_put(con);
451 }
452 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400453}
454
Jon Maloy026321c2018-02-15 10:40:51 +0100455static void tipc_topsrv_accept(struct work_struct *work)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400456{
Jon Maloy026321c2018-02-15 10:40:51 +0100457 struct tipc_topsrv *srv = container_of(work, struct tipc_topsrv, awork);
Xin Long82cb4e42022-10-18 15:19:50 -0400458 struct socket *newsock, *lsock;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100459 struct tipc_conn *con;
460 struct sock *newsk;
461 int ret;
462
Xin Long82cb4e42022-10-18 15:19:50 -0400463 spin_lock_bh(&srv->idr_lock);
464 if (!srv->listener) {
465 spin_unlock_bh(&srv->idr_lock);
466 return;
467 }
468 lsock = srv->listener;
469 spin_unlock_bh(&srv->idr_lock);
470
Jon Maloy0ef897b2018-02-15 10:40:50 +0100471 while (1) {
472 ret = kernel_accept(lsock, &newsock, O_NONBLOCK);
473 if (ret < 0)
474 return;
Xin Long0e5d56c2022-11-18 16:45:00 -0500475 con = tipc_conn_alloc(srv, newsock);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100476 if (IS_ERR(con)) {
477 ret = PTR_ERR(con);
478 sock_release(newsock);
479 return;
480 }
481 /* Register callbacks */
482 newsk = newsock->sk;
483 write_lock_bh(&newsk->sk_callback_lock);
Jon Maloy026321c2018-02-15 10:40:51 +0100484 newsk->sk_data_ready = tipc_conn_data_ready;
485 newsk->sk_write_space = tipc_conn_write_space;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100486 newsk->sk_user_data = con;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100487 write_unlock_bh(&newsk->sk_callback_lock);
488
489 /* Wake up receive process in case of 'SYN+' message */
490 newsk->sk_data_ready(newsk);
Xin Longa7b42962022-11-18 16:45:01 -0500491 conn_put(con);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100492 }
493}
494
Christophe JAILLETbad7f862019-07-21 12:38:11 +0200495/* tipc_topsrv_listener_data_ready - interrupt callback with connection request
Jon Maloy026321c2018-02-15 10:40:51 +0100496 * The queued job is launched into tipc_topsrv_accept()
Jon Maloy0ef897b2018-02-15 10:40:50 +0100497 */
Jon Maloy026321c2018-02-15 10:40:51 +0100498static void tipc_topsrv_listener_data_ready(struct sock *sk)
Jon Maloy0ef897b2018-02-15 10:40:50 +0100499{
Jon Maloy026321c2018-02-15 10:40:51 +0100500 struct tipc_topsrv *srv;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100501
Peilin Ye40e0b092023-01-19 16:45:16 -0800502 trace_sk_data_ready(sk);
503
Jon Maloy0ef897b2018-02-15 10:40:50 +0100504 read_lock_bh(&sk->sk_callback_lock);
505 srv = sk->sk_user_data;
Xin Long82cb4e42022-10-18 15:19:50 -0400506 if (srv)
Jon Maloy0ef897b2018-02-15 10:40:50 +0100507 queue_work(srv->rcv_wq, &srv->awork);
508 read_unlock_bh(&sk->sk_callback_lock);
509}
510
Jon Maloy026321c2018-02-15 10:40:51 +0100511static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)
Jon Maloy0ef897b2018-02-15 10:40:50 +0100512{
Jon Maloy0ef897b2018-02-15 10:40:50 +0100513 struct socket *lsock = NULL;
514 struct sockaddr_tipc saddr;
515 struct sock *sk;
516 int rc;
517
518 rc = sock_create_kern(srv->net, AF_TIPC, SOCK_SEQPACKET, 0, &lsock);
519 if (rc < 0)
520 return rc;
521
522 srv->listener = lsock;
523 sk = lsock->sk;
524 write_lock_bh(&sk->sk_callback_lock);
Jon Maloy026321c2018-02-15 10:40:51 +0100525 sk->sk_data_ready = tipc_topsrv_listener_data_ready;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100526 sk->sk_user_data = srv;
527 write_unlock_bh(&sk->sk_callback_lock);
528
Christoph Hellwig095ae612020-05-28 07:12:36 +0200529 lock_sock(sk);
530 rc = tsk_set_importance(sk, TIPC_CRITICAL_IMPORTANCE);
531 release_sock(sk);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100532 if (rc < 0)
533 goto err;
534
535 saddr.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -0500536 saddr.addrtype = TIPC_SERVICE_RANGE;
537 saddr.addr.nameseq.type = TIPC_TOP_SRV;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100538 saddr.addr.nameseq.lower = TIPC_TOP_SRV;
539 saddr.addr.nameseq.upper = TIPC_TOP_SRV;
540 saddr.scope = TIPC_NODE_SCOPE;
541
Jon Maloy72671b32020-10-29 21:29:38 -0400542 rc = tipc_sk_bind(lsock, (struct sockaddr *)&saddr, sizeof(saddr));
Jon Maloy0ef897b2018-02-15 10:40:50 +0100543 if (rc < 0)
544 goto err;
545 rc = kernel_listen(lsock, 0);
546 if (rc < 0)
547 goto err;
548
549 /* As server's listening socket owner and creator is the same module,
550 * we have to decrease TIPC module reference count to guarantee that
551 * it remains zero after the server socket is created, otherwise,
552 * executing "rmmod" command is unable to make TIPC module deleted
553 * after TIPC module is inserted successfully.
554 *
555 * However, the reference count is ever increased twice in
556 * sock_create_kern(): one is to increase the reference count of owner
557 * of TIPC socket's proto_ops struct; another is to increment the
558 * reference count of owner of TIPC proto struct. Therefore, we must
559 * decrement the module reference count twice to ensure that it keeps
560 * zero after server's listening socket is created. Of course, we
561 * must bump the module reference count twice as well before the socket
562 * is closed.
563 */
564 module_put(lsock->ops->owner);
565 module_put(sk->sk_prot_creator->owner);
566
567 return 0;
568err:
569 sock_release(lsock);
570 return -EINVAL;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400571}
572
Jon Maloy026321c2018-02-15 10:40:51 +0100573bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
574 u32 upper, u32 filter, int *conid)
575{
576 struct tipc_subscr sub;
577 struct tipc_conn *con;
578 int rc;
579
580 sub.seq.type = type;
581 sub.seq.lower = lower;
582 sub.seq.upper = upper;
583 sub.timeout = TIPC_WAIT_FOREVER;
584 sub.filter = filter;
Alexander Potapenko777ecaa2022-10-12 17:25:14 +0200585 *(u64 *)&sub.usr_handle = (u64)port;
Jon Maloy026321c2018-02-15 10:40:51 +0100586
Xin Long0e5d56c2022-11-18 16:45:00 -0500587 con = tipc_conn_alloc(tipc_topsrv(net), NULL);
Jon Maloy026321c2018-02-15 10:40:51 +0100588 if (IS_ERR(con))
589 return false;
590
591 *conid = con->conid;
Jon Maloy026321c2018-02-15 10:40:51 +0100592 rc = tipc_conn_rcv_sub(tipc_topsrv(net), con, &sub);
Xin Longa7b42962022-11-18 16:45:01 -0500593 if (rc)
594 conn_put(con);
595
Jon Maloy96c252b2018-02-19 12:48:21 +0100596 conn_put(con);
Xin Longa7b42962022-11-18 16:45:01 -0500597 return !rc;
Jon Maloy026321c2018-02-15 10:40:51 +0100598}
599
600void tipc_topsrv_kern_unsubscr(struct net *net, int conid)
601{
602 struct tipc_conn *con;
603
604 con = tipc_conn_lookup(tipc_topsrv(net), conid);
605 if (!con)
606 return;
607
608 test_and_clear_bit(CF_CONNECTED, &con->flags);
609 tipc_conn_delete_sub(con, NULL);
610 conn_put(con);
611 conn_put(con);
612}
613
614static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt)
615{
616 u32 port = *(u32 *)&evt->s.usr_handle;
617 u32 self = tipc_own_addr(net);
618 struct sk_buff_head evtq;
619 struct sk_buff *skb;
620
621 skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt),
622 self, self, port, port, 0);
623 if (!skb)
624 return;
625 msg_set_dest_droppable(buf_msg(skb), true);
626 memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt));
627 skb_queue_head_init(&evtq);
628 __skb_queue_tail(&evtq, skb);
John Rutherford6c9081a2019-08-07 12:52:29 +1000629 tipc_loopback_trace(net, &evtq);
Jon Maloy026321c2018-02-15 10:40:51 +0100630 tipc_sk_rcv(net, &evtq);
631}
632
633static int tipc_topsrv_work_start(struct tipc_topsrv *s)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400634{
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100635 s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400636 if (!s->rcv_wq) {
637 pr_err("can't start tipc receive workqueue\n");
638 return -ENOMEM;
639 }
640
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100641 s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400642 if (!s->send_wq) {
643 pr_err("can't start tipc send workqueue\n");
644 destroy_workqueue(s->rcv_wq);
645 return -ENOMEM;
646 }
647
648 return 0;
649}
650
Jon Maloy026321c2018-02-15 10:40:51 +0100651static void tipc_topsrv_work_stop(struct tipc_topsrv *s)
Jon Maloy0ef897b2018-02-15 10:40:50 +0100652{
653 destroy_workqueue(s->rcv_wq);
654 destroy_workqueue(s->send_wq);
655}
656
Junwei Hu526f5b82019-05-20 14:43:59 +0800657static int tipc_topsrv_start(struct net *net)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400658{
Jon Maloy5c45ab22018-02-15 10:40:49 +0100659 struct tipc_net *tn = tipc_net(net);
660 const char name[] = "topology_server";
Jon Maloy026321c2018-02-15 10:40:51 +0100661 struct tipc_topsrv *srv;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400662 int ret;
663
Jon Maloy5c45ab22018-02-15 10:40:49 +0100664 srv = kzalloc(sizeof(*srv), GFP_ATOMIC);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100665 if (!srv)
Jon Maloy5c45ab22018-02-15 10:40:49 +0100666 return -ENOMEM;
Jon Maloy0ef897b2018-02-15 10:40:50 +0100667
668 srv->net = net;
Jon Maloy026321c2018-02-15 10:40:51 +0100669 INIT_WORK(&srv->awork, tipc_topsrv_accept);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100670
Guoqing Jiang29e270f2018-10-19 12:08:22 +0800671 strscpy(srv->name, name, sizeof(srv->name));
Jon Maloy5c45ab22018-02-15 10:40:49 +0100672 tn->topsrv = srv;
673 atomic_set(&tn->subscription_count, 0);
674
675 spin_lock_init(&srv->idr_lock);
676 idr_init(&srv->conn_idr);
677 srv->idr_in_use = 0;
678
Jon Maloy026321c2018-02-15 10:40:51 +0100679 ret = tipc_topsrv_work_start(srv);
Jon Maloy414574a2018-02-15 10:40:45 +0100680 if (ret < 0)
Wang Haifa6882c2020-11-09 22:09:13 +0800681 goto err_start;
Jon Maloy414574a2018-02-15 10:40:45 +0100682
Jon Maloy026321c2018-02-15 10:40:51 +0100683 ret = tipc_topsrv_create_listener(srv);
Jon Maloy414574a2018-02-15 10:40:45 +0100684 if (ret < 0)
Wang Haifa6882c2020-11-09 22:09:13 +0800685 goto err_create;
Jon Maloy414574a2018-02-15 10:40:45 +0100686
Wang Haifa6882c2020-11-09 22:09:13 +0800687 return 0;
688
689err_create:
690 tipc_topsrv_work_stop(srv);
691err_start:
692 kfree(srv);
Ying Xuec756891a2013-08-01 08:29:18 -0400693 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400694}
695
Junwei Hu526f5b82019-05-20 14:43:59 +0800696static void tipc_topsrv_stop(struct net *net)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400697{
Jon Maloy026321c2018-02-15 10:40:51 +0100698 struct tipc_topsrv *srv = tipc_topsrv(net);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100699 struct socket *lsock = srv->listener;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400700 struct tipc_conn *con;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400701 int id;
702
Jon Maloy5c45ab22018-02-15 10:40:49 +0100703 spin_lock_bh(&srv->idr_lock);
704 for (id = 0; srv->idr_in_use; id++) {
705 con = idr_find(&srv->conn_idr, id);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400706 if (con) {
Jon Maloy5c45ab22018-02-15 10:40:49 +0100707 spin_unlock_bh(&srv->idr_lock);
Jon Maloy026321c2018-02-15 10:40:51 +0100708 tipc_conn_close(con);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100709 spin_lock_bh(&srv->idr_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400710 }
711 }
Jon Maloy0ef897b2018-02-15 10:40:50 +0100712 __module_get(lsock->ops->owner);
713 __module_get(lsock->sk->sk_prot_creator->owner);
Jon Maloy0ef897b2018-02-15 10:40:50 +0100714 srv->listener = NULL;
Jon Maloy5c45ab22018-02-15 10:40:49 +0100715 spin_unlock_bh(&srv->idr_lock);
Xin Long82cb4e42022-10-18 15:19:50 -0400716
Jon Maloy026321c2018-02-15 10:40:51 +0100717 tipc_topsrv_work_stop(srv);
Xin Long82cb4e42022-10-18 15:19:50 -0400718 sock_release(lsock);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100719 idr_destroy(&srv->conn_idr);
Jon Maloy5c45ab22018-02-15 10:40:49 +0100720 kfree(srv);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400721}
Junwei Hu526f5b82019-05-20 14:43:59 +0800722
723int __net_init tipc_topsrv_init_net(struct net *net)
724{
725 return tipc_topsrv_start(net);
726}
727
728void __net_exit tipc_topsrv_exit_net(struct net *net)
729{
730 tipc_topsrv_stop(net);
731}