blob: c0d331f13eee9d393750bb46c824473ce02092c5 [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
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 Xue859fc7c2015-01-09 15:27:01 +080038#include "socket.h"
Jon Maloy14c04492017-10-13 11:04:17 +020039#include "addr.h"
40#include "msg.h"
Ying Xuec5fa7b32013-06-17 10:54:39 -040041#include <net/sock.h>
Ying Xue76100a82015-03-18 09:32:57 +080042#include <linux/module.h>
Ying Xuec5fa7b32013-06-17 10:54:39 -040043
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 Xue76100a82015-03-18 09:32:57 +080048#define CF_SERVER 2
Ying Xuec5fa7b32013-06-17 10:54:39 -040049
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 hemminger963a18552014-01-12 12:48:00 -080063 * @outqueue_lock: control access to the outqueue
Ying Xuec5fa7b32013-06-17 10:54:39 -040064 * @outqueue: list of connection objects for its server
65 * @swork: send work item
66 */
67struct 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 */
82struct outqueue_entry {
83 struct list_head list;
84 struct kvec iov;
85 struct sockaddr_tipc dest;
86};
87
88static void tipc_recv_work(struct work_struct *work);
89static void tipc_send_work(struct work_struct *work);
90static void tipc_clean_outqueues(struct tipc_conn *con);
91
92static void tipc_conn_kref_release(struct kref *kref)
93{
94 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
Parthasarathy Bhuvaraganfc0adfc2017-01-24 13:00:45 +010095 struct tipc_server *s = con->server;
96 struct sockaddr_tipc *saddr = s->saddr;
Ying Xue76100a82015-03-18 09:32:57 +080097 struct socket *sock = con->sock;
98 struct sock *sk;
Ying Xuec5fa7b32013-06-17 10:54:39 -040099
Ying Xue76100a82015-03-18 09:32:57 +0800100 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 Xue2b9bb7f2015-03-18 09:32:59 +0800106 saddr->scope = -TIPC_NODE_SCOPE;
107 kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr));
Ying Xuedef81f62015-04-23 09:37:38 -0400108 sock_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400109 con->sock = NULL;
110 }
Jon Maloy14c04492017-10-13 11:04:17 +0200111 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 Xuec5fa7b32013-06-17 10:54:39 -0400115 tipc_clean_outqueues(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400116 kfree(con);
117}
118
119static void conn_put(struct tipc_conn *con)
120{
121 kref_put(&con->kref, tipc_conn_kref_release);
122}
123
124static void conn_get(struct tipc_conn *con)
125{
126 kref_get(&con->kref);
127}
128
129static 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);
Jon Maloye88f2be2018-01-15 17:56:28 +0100135 if (con) {
136 if (!test_bit(CF_CONNECTED, &con->flags) ||
137 !kref_get_unless_zero(&con->kref))
138 con = NULL;
139 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400140 spin_unlock_bh(&s->idr_lock);
141 return con;
142}
143
David S. Miller676d2362014-04-11 16:15:36 -0400144static void sock_data_ready(struct sock *sk)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400145{
146 struct tipc_conn *con;
147
Eric Dumazetb91083a2016-05-17 17:44:09 -0700148 read_lock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400149 con = sock2con(sk);
150 if (con && test_bit(CF_CONNECTED, &con->flags)) {
151 conn_get(con);
152 if (!queue_work(con->server->rcv_wq, &con->rwork))
153 conn_put(con);
154 }
Eric Dumazetb91083a2016-05-17 17:44:09 -0700155 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400156}
157
158static void sock_write_space(struct sock *sk)
159{
160 struct tipc_conn *con;
161
Eric Dumazetb91083a2016-05-17 17:44:09 -0700162 read_lock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400163 con = sock2con(sk);
164 if (con && test_bit(CF_CONNECTED, &con->flags)) {
165 conn_get(con);
166 if (!queue_work(con->server->send_wq, &con->swork))
167 conn_put(con);
168 }
Eric Dumazetb91083a2016-05-17 17:44:09 -0700169 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400170}
171
172static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
173{
174 struct sock *sk = sock->sk;
175
176 write_lock_bh(&sk->sk_callback_lock);
177
178 sk->sk_data_ready = sock_data_ready;
179 sk->sk_write_space = sock_write_space;
180 sk->sk_user_data = con;
181
182 con->sock = sock;
183
184 write_unlock_bh(&sk->sk_callback_lock);
185}
186
Parthasarathy Bhuvaragan9dc3abd2017-01-24 13:00:46 +0100187static void tipc_close_conn(struct tipc_conn *con)
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200188{
189 struct tipc_server *s = con->server;
Jon Maloye88f2be2018-01-15 17:56:28 +0100190 struct sock *sk = con->sock->sk;
191 bool disconnect = false;
Parthasarathy Bhuvaragan333f7962016-04-12 13:05:21 +0200192
Jon Maloye88f2be2018-01-15 17:56:28 +0100193 write_lock_bh(&sk->sk_callback_lock);
194 disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
195 if (disconnect) {
196 sk->sk_user_data = NULL;
Parthasarathy Bhuvaragan9dc3abd2017-01-24 13:00:46 +0100197 if (con->conid)
198 s->tipc_conn_release(con->conid, con->usr_data);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400199 }
Jon Maloye88f2be2018-01-15 17:56:28 +0100200 write_unlock_bh(&sk->sk_callback_lock);
201
202 /* Handle concurrent calls from sending and receiving threads */
203 if (!disconnect)
204 return;
205
206 /* Don't flush pending works, -just let them expire */
207 kernel_sock_shutdown(con->sock, SHUT_RDWR);
208 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400209}
210
211static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s)
212{
213 struct tipc_conn *con;
214 int ret;
215
216 con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC);
217 if (!con)
218 return ERR_PTR(-ENOMEM);
219
220 kref_init(&con->kref);
221 INIT_LIST_HEAD(&con->outqueue);
222 spin_lock_init(&con->outqueue_lock);
223 INIT_WORK(&con->swork, tipc_send_work);
224 INIT_WORK(&con->rwork, tipc_recv_work);
225
226 spin_lock_bh(&s->idr_lock);
227 ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
228 if (ret < 0) {
229 kfree(con);
230 spin_unlock_bh(&s->idr_lock);
231 return ERR_PTR(-ENOMEM);
232 }
233 con->conid = ret;
234 s->idr_in_use++;
235 spin_unlock_bh(&s->idr_lock);
236
237 set_bit(CF_CONNECTED, &con->flags);
238 con->server = s;
239
240 return con;
241}
242
243static int tipc_receive_from_sock(struct tipc_conn *con)
244{
Ying Xuec5fa7b32013-06-17 10:54:39 -0400245 struct tipc_server *s = con->server;
Jon Maloye88f2be2018-01-15 17:56:28 +0100246 struct sock *sk = con->sock->sk;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400247 struct sockaddr_tipc addr;
Jon Maloye88f2be2018-01-15 17:56:28 +0100248 struct msghdr msg = {};
Ying Xuec5fa7b32013-06-17 10:54:39 -0400249 struct kvec iov;
250 void *buf;
251 int ret;
252
253 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC);
254 if (!buf) {
255 ret = -ENOMEM;
256 goto out_close;
257 }
258
259 iov.iov_base = buf;
260 iov.iov_len = s->max_rcvbuf_size;
261 msg.msg_name = &addr;
262 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
263 MSG_DONTWAIT);
264 if (ret <= 0) {
265 kmem_cache_free(s->rcvbuf_cache, buf);
266 goto out_close;
267 }
268
Jon Maloye88f2be2018-01-15 17:56:28 +0100269 read_lock_bh(&sk->sk_callback_lock);
270 if (test_bit(CF_CONNECTED, &con->flags))
271 ret = s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid,
272 &addr, con->usr_data, buf, ret);
273 read_unlock_bh(&sk->sk_callback_lock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400274 kmem_cache_free(s->rcvbuf_cache, buf);
Jon Maloye88f2be2018-01-15 17:56:28 +0100275 if (ret < 0)
276 tipc_conn_terminate(s, con->conid);
277 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400278
279out_close:
280 if (ret != -EWOULDBLOCK)
281 tipc_close_conn(con);
282 else if (ret == 0)
283 /* Don't return success if we really got EOF */
284 ret = -EAGAIN;
285
286 return ret;
287}
288
289static int tipc_accept_from_sock(struct tipc_conn *con)
290{
291 struct tipc_server *s = con->server;
292 struct socket *sock = con->sock;
293 struct socket *newsock;
294 struct tipc_conn *newcon;
295 int ret;
296
Ying Xue76100a82015-03-18 09:32:57 +0800297 ret = kernel_accept(sock, &newsock, O_NONBLOCK);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400298 if (ret < 0)
299 return ret;
300
301 newcon = tipc_alloc_conn(con->server);
302 if (IS_ERR(newcon)) {
303 ret = PTR_ERR(newcon);
304 sock_release(newsock);
305 return ret;
306 }
307
308 newcon->rx_action = tipc_receive_from_sock;
309 tipc_register_callbacks(newsock, newcon);
310
311 /* Notify that new connection is incoming */
312 newcon->usr_data = s->tipc_conn_new(newcon->conid);
Ying Xue90bdfcb2015-05-04 10:36:48 +0800313 if (!newcon->usr_data) {
314 sock_release(newsock);
Jon Maloya7d5f102017-12-04 22:00:20 +0100315 conn_put(newcon);
Ying Xue90bdfcb2015-05-04 10:36:48 +0800316 return -ENOMEM;
317 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400318
319 /* Wake up receive process in case of 'SYN+' message */
David S. Miller676d2362014-04-11 16:15:36 -0400320 newsock->sk->sk_data_ready(newsock->sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400321 return ret;
322}
323
324static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
325{
326 struct tipc_server *s = con->server;
327 struct socket *sock = NULL;
328 int ret;
329
Ying Xuefa787ae2015-05-13 11:20:38 +0800330 ret = sock_create_kern(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400331 if (ret < 0)
332 return NULL;
333 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
334 (char *)&s->imp, sizeof(s->imp));
335 if (ret < 0)
336 goto create_err;
337 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr));
338 if (ret < 0)
339 goto create_err;
340
341 switch (s->type) {
342 case SOCK_STREAM:
343 case SOCK_SEQPACKET:
344 con->rx_action = tipc_accept_from_sock;
345
346 ret = kernel_listen(sock, 0);
347 if (ret < 0)
348 goto create_err;
349 break;
350 case SOCK_DGRAM:
351 case SOCK_RDM:
352 con->rx_action = tipc_receive_from_sock;
353 break;
354 default:
355 pr_err("Unknown socket type %d\n", s->type);
356 goto create_err;
357 }
Ying Xue76100a82015-03-18 09:32:57 +0800358
359 /* As server's listening socket owner and creator is the same module,
360 * we have to decrease TIPC module reference count to guarantee that
361 * it remains zero after the server socket is created, otherwise,
362 * executing "rmmod" command is unable to make TIPC module deleted
363 * after TIPC module is inserted successfully.
364 *
365 * However, the reference count is ever increased twice in
366 * sock_create_kern(): one is to increase the reference count of owner
367 * of TIPC socket's proto_ops struct; another is to increment the
368 * reference count of owner of TIPC proto struct. Therefore, we must
369 * decrement the module reference count twice to ensure that it keeps
370 * zero after server's listening socket is created. Of course, we
371 * must bump the module reference count twice as well before the socket
372 * is closed.
373 */
374 module_put(sock->ops->owner);
375 module_put(sock->sk->sk_prot_creator->owner);
376 set_bit(CF_SERVER, &con->flags);
377
Ying Xuec5fa7b32013-06-17 10:54:39 -0400378 return sock;
379
380create_err:
Ying Xue76100a82015-03-18 09:32:57 +0800381 kernel_sock_shutdown(sock, SHUT_RDWR);
Ying Xuedef81f62015-04-23 09:37:38 -0400382 sock_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400383 return NULL;
384}
385
386static int tipc_open_listening_sock(struct tipc_server *s)
387{
388 struct socket *sock;
389 struct tipc_conn *con;
390
391 con = tipc_alloc_conn(s);
392 if (IS_ERR(con))
393 return PTR_ERR(con);
394
395 sock = tipc_create_listen_sock(con);
Ying Xuec756891a2013-08-01 08:29:18 -0400396 if (!sock) {
397 idr_remove(&s->conn_idr, con->conid);
398 s->idr_in_use--;
399 kfree(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400400 return -EINVAL;
Ying Xuec756891a2013-08-01 08:29:18 -0400401 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400402
403 tipc_register_callbacks(sock, con);
404 return 0;
405}
406
407static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
408{
409 struct outqueue_entry *entry;
410 void *buf;
411
412 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
413 if (!entry)
414 return NULL;
415
Amitoj Kaur Chawla810bf112016-06-23 10:19:37 +0530416 buf = kmemdup(data, len, GFP_ATOMIC);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400417 if (!buf) {
418 kfree(entry);
419 return NULL;
420 }
421
Ying Xuec5fa7b32013-06-17 10:54:39 -0400422 entry->iov.iov_base = buf;
423 entry->iov.iov_len = len;
424
425 return entry;
426}
427
428static void tipc_free_entry(struct outqueue_entry *e)
429{
430 kfree(e->iov.iov_base);
431 kfree(e);
432}
433
434static void tipc_clean_outqueues(struct tipc_conn *con)
435{
436 struct outqueue_entry *e, *safe;
437
438 spin_lock_bh(&con->outqueue_lock);
439 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
440 list_del(&e->list);
441 tipc_free_entry(e);
442 }
443 spin_unlock_bh(&con->outqueue_lock);
444}
445
446int tipc_conn_sendmsg(struct tipc_server *s, int conid,
447 struct sockaddr_tipc *addr, void *data, size_t len)
448{
449 struct outqueue_entry *e;
450 struct tipc_conn *con;
451
452 con = tipc_conn_lookup(s, conid);
453 if (!con)
454 return -EINVAL;
455
Parthasarathy Bhuvaragan4c887aa2017-01-24 13:00:47 +0100456 if (!test_bit(CF_CONNECTED, &con->flags)) {
457 conn_put(con);
458 return 0;
459 }
460
Ying Xuec5fa7b32013-06-17 10:54:39 -0400461 e = tipc_alloc_entry(data, len);
462 if (!e) {
463 conn_put(con);
464 return -ENOMEM;
465 }
466
467 if (addr)
468 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
469
470 spin_lock_bh(&con->outqueue_lock);
471 list_add_tail(&e->list, &con->outqueue);
472 spin_unlock_bh(&con->outqueue_lock);
473
Parthasarathy Bhuvaragan4c887aa2017-01-24 13:00:47 +0100474 if (!queue_work(s->send_wq, &con->swork))
Ying Xue4652edb2014-03-06 14:40:17 +0100475 conn_put(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400476 return 0;
477}
478
479void tipc_conn_terminate(struct tipc_server *s, int conid)
480{
481 struct tipc_conn *con;
482
483 con = tipc_conn_lookup(s, conid);
484 if (con) {
485 tipc_close_conn(con);
486 conn_put(con);
487 }
488}
489
Jon Maloy232d07b2018-01-08 21:03:30 +0100490bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
491 u32 upper, u32 filter, int *conid)
Jon Maloy14c04492017-10-13 11:04:17 +0200492{
493 struct tipc_subscriber *scbr;
494 struct tipc_subscr sub;
495 struct tipc_server *s;
496 struct tipc_conn *con;
497
498 sub.seq.type = type;
499 sub.seq.lower = lower;
500 sub.seq.upper = upper;
501 sub.timeout = TIPC_WAIT_FOREVER;
Jon Maloy83485002018-01-08 21:03:29 +0100502 sub.filter = filter;
Jon Maloy14c04492017-10-13 11:04:17 +0200503 *(u32 *)&sub.usr_handle = port;
504
505 con = tipc_alloc_conn(tipc_topsrv(net));
Dan Carpenterc75e4272017-10-18 10:48:25 +0300506 if (IS_ERR(con))
Jon Maloy14c04492017-10-13 11:04:17 +0200507 return false;
508
509 *conid = con->conid;
510 s = con->server;
511 scbr = s->tipc_conn_new(*conid);
512 if (!scbr) {
Cong Wang672ecbe2017-12-04 10:31:43 -0800513 conn_put(con);
Jon Maloy14c04492017-10-13 11:04:17 +0200514 return false;
515 }
516
517 con->usr_data = scbr;
518 con->sock = NULL;
519 s->tipc_conn_recvmsg(net, *conid, NULL, scbr, &sub, sizeof(sub));
520 return true;
521}
522
523void tipc_topsrv_kern_unsubscr(struct net *net, int conid)
524{
525 struct tipc_conn *con;
Jon Maloye88f2be2018-01-15 17:56:28 +0100526 struct tipc_server *srv;
Jon Maloy14c04492017-10-13 11:04:17 +0200527
528 con = tipc_conn_lookup(tipc_topsrv(net), conid);
529 if (!con)
530 return;
Jon Maloye88f2be2018-01-15 17:56:28 +0100531
532 test_and_clear_bit(CF_CONNECTED, &con->flags);
533 srv = con->server;
534 if (con->conid)
535 srv->tipc_conn_release(con->conid, con->usr_data);
536 conn_put(con);
Jon Maloy14c04492017-10-13 11:04:17 +0200537 conn_put(con);
538}
539
540static void tipc_send_kern_top_evt(struct net *net, struct tipc_event *evt)
541{
542 u32 port = *(u32 *)&evt->s.usr_handle;
543 u32 self = tipc_own_addr(net);
544 struct sk_buff_head evtq;
545 struct sk_buff *skb;
546
547 skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt),
548 self, self, port, port, 0);
549 if (!skb)
550 return;
551 msg_set_dest_droppable(buf_msg(skb), true);
552 memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt));
553 skb_queue_head_init(&evtq);
554 __skb_queue_tail(&evtq, skb);
555 tipc_sk_rcv(net, &evtq);
556}
557
Ying Xuec5fa7b32013-06-17 10:54:39 -0400558static void tipc_send_to_sock(struct tipc_conn *con)
559{
Ying Xuec5fa7b32013-06-17 10:54:39 -0400560 struct tipc_server *s = con->server;
561 struct outqueue_entry *e;
Jon Maloy14c04492017-10-13 11:04:17 +0200562 struct tipc_event *evt;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400563 struct msghdr msg;
Jon Maloy14c04492017-10-13 11:04:17 +0200564 int count = 0;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400565 int ret;
566
567 spin_lock_bh(&con->outqueue_lock);
Parthasarathy Bhuvaragan4c887aa2017-01-24 13:00:47 +0100568 while (test_bit(CF_CONNECTED, &con->flags)) {
Jon Maloy14c04492017-10-13 11:04:17 +0200569 e = list_entry(con->outqueue.next, struct outqueue_entry, list);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400570 if ((struct list_head *) e == &con->outqueue)
571 break;
Jon Maloy14c04492017-10-13 11:04:17 +0200572
Ying Xuec5fa7b32013-06-17 10:54:39 -0400573 spin_unlock_bh(&con->outqueue_lock);
574
Jon Maloy14c04492017-10-13 11:04:17 +0200575 if (con->sock) {
576 memset(&msg, 0, sizeof(msg));
577 msg.msg_flags = MSG_DONTWAIT;
578 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
579 msg.msg_name = &e->dest;
580 msg.msg_namelen = sizeof(struct sockaddr_tipc);
581 }
582 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
583 e->iov.iov_len);
584 if (ret == -EWOULDBLOCK || ret == 0) {
585 cond_resched();
586 goto out;
587 } else if (ret < 0) {
588 goto send_err;
589 }
590 } else {
591 evt = e->iov.iov_base;
592 tipc_send_kern_top_evt(s->net, evt);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400593 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400594 /* Don't starve users filling buffers */
595 if (++count >= MAX_SEND_MSG_COUNT) {
596 cond_resched();
597 count = 0;
598 }
599
600 spin_lock_bh(&con->outqueue_lock);
601 list_del(&e->list);
602 tipc_free_entry(e);
603 }
604 spin_unlock_bh(&con->outqueue_lock);
605out:
606 return;
607
608send_err:
609 tipc_close_conn(con);
610}
611
612static void tipc_recv_work(struct work_struct *work)
613{
614 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
615 int count = 0;
616
617 while (test_bit(CF_CONNECTED, &con->flags)) {
618 if (con->rx_action(con))
619 break;
620
621 /* Don't flood Rx machine */
622 if (++count >= MAX_RECV_MSG_COUNT) {
623 cond_resched();
624 count = 0;
625 }
626 }
627 conn_put(con);
628}
629
630static void tipc_send_work(struct work_struct *work)
631{
632 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
633
634 if (test_bit(CF_CONNECTED, &con->flags))
635 tipc_send_to_sock(con);
636
637 conn_put(con);
638}
639
640static void tipc_work_stop(struct tipc_server *s)
641{
642 destroy_workqueue(s->rcv_wq);
643 destroy_workqueue(s->send_wq);
644}
645
646static int tipc_work_start(struct tipc_server *s)
647{
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100648 s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400649 if (!s->rcv_wq) {
650 pr_err("can't start tipc receive workqueue\n");
651 return -ENOMEM;
652 }
653
Parthasarathy Bhuvaragan06c85812016-02-02 10:52:17 +0100654 s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400655 if (!s->send_wq) {
656 pr_err("can't start tipc send workqueue\n");
657 destroy_workqueue(s->rcv_wq);
658 return -ENOMEM;
659 }
660
661 return 0;
662}
663
664int tipc_server_start(struct tipc_server *s)
665{
666 int ret;
667
668 spin_lock_init(&s->idr_lock);
669 idr_init(&s->conn_idr);
670 s->idr_in_use = 0;
671
672 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
673 0, SLAB_HWCACHE_ALIGN, NULL);
674 if (!s->rcvbuf_cache)
675 return -ENOMEM;
676
677 ret = tipc_work_start(s);
678 if (ret < 0) {
679 kmem_cache_destroy(s->rcvbuf_cache);
680 return ret;
681 }
Ying Xuec756891a2013-08-01 08:29:18 -0400682 ret = tipc_open_listening_sock(s);
683 if (ret < 0) {
684 tipc_work_stop(s);
685 kmem_cache_destroy(s->rcvbuf_cache);
686 return ret;
687 }
Ying Xuec756891a2013-08-01 08:29:18 -0400688 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400689}
690
691void tipc_server_stop(struct tipc_server *s)
692{
693 struct tipc_conn *con;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400694 int id;
695
Ying Xuec5fa7b32013-06-17 10:54:39 -0400696 spin_lock_bh(&s->idr_lock);
Parthasarathy Bhuvaragan35e22e42017-01-24 13:00:48 +0100697 for (id = 0; s->idr_in_use; id++) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400698 con = idr_find(&s->conn_idr, id);
699 if (con) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400700 spin_unlock_bh(&s->idr_lock);
701 tipc_close_conn(con);
702 spin_lock_bh(&s->idr_lock);
703 }
704 }
705 spin_unlock_bh(&s->idr_lock);
706
707 tipc_work_stop(s);
708 kmem_cache_destroy(s->rcvbuf_cache);
709 idr_destroy(&s->conn_idr);
710}