blob: a57c8407cbf30822b1f92e5f25b7bc3c09ffe9be [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"
Ying Xuec5fa7b32013-06-17 10:54:39 -040039#include <net/sock.h>
Ying Xue76100a82015-03-18 09:32:57 +080040#include <linux/module.h>
Ying Xuec5fa7b32013-06-17 10:54:39 -040041
42/* Number of messages to send before rescheduling */
43#define MAX_SEND_MSG_COUNT 25
44#define MAX_RECV_MSG_COUNT 25
45#define CF_CONNECTED 1
Ying Xue76100a82015-03-18 09:32:57 +080046#define CF_SERVER 2
Ying Xuec5fa7b32013-06-17 10:54:39 -040047
48#define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data)
49
50/**
51 * struct tipc_conn - TIPC connection structure
52 * @kref: reference counter to connection object
53 * @conid: connection identifier
54 * @sock: socket handler associated with connection
55 * @flags: indicates connection state
56 * @server: pointer to connected server
57 * @rwork: receive work item
58 * @usr_data: user-specified field
59 * @rx_action: what to do when connection socket is active
60 * @outqueue: pointer to first outbound message in queue
stephen hemminger963a18552014-01-12 12:48:00 -080061 * @outqueue_lock: control access to the outqueue
Ying Xuec5fa7b32013-06-17 10:54:39 -040062 * @outqueue: list of connection objects for its server
63 * @swork: send work item
64 */
65struct tipc_conn {
66 struct kref kref;
67 int conid;
68 struct socket *sock;
69 unsigned long flags;
70 struct tipc_server *server;
71 struct work_struct rwork;
72 int (*rx_action) (struct tipc_conn *con);
73 void *usr_data;
74 struct list_head outqueue;
75 spinlock_t outqueue_lock;
76 struct work_struct swork;
77};
78
79/* An entry waiting to be sent */
80struct outqueue_entry {
81 struct list_head list;
82 struct kvec iov;
83 struct sockaddr_tipc dest;
84};
85
86static void tipc_recv_work(struct work_struct *work);
87static void tipc_send_work(struct work_struct *work);
88static void tipc_clean_outqueues(struct tipc_conn *con);
89
90static void tipc_conn_kref_release(struct kref *kref)
91{
92 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
Ying Xue76100a82015-03-18 09:32:57 +080093 struct socket *sock = con->sock;
94 struct sock *sk;
Ying Xuec5fa7b32013-06-17 10:54:39 -040095
Ying Xue76100a82015-03-18 09:32:57 +080096 if (sock) {
97 sk = sock->sk;
98 if (test_bit(CF_SERVER, &con->flags)) {
99 __module_get(sock->ops->owner);
100 __module_get(sk->sk_prot_creator->owner);
101 }
102 sk_release_kernel(sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400103 con->sock = NULL;
104 }
105
106 tipc_clean_outqueues(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400107 kfree(con);
108}
109
110static void conn_put(struct tipc_conn *con)
111{
112 kref_put(&con->kref, tipc_conn_kref_release);
113}
114
115static void conn_get(struct tipc_conn *con)
116{
117 kref_get(&con->kref);
118}
119
120static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid)
121{
122 struct tipc_conn *con;
123
124 spin_lock_bh(&s->idr_lock);
125 con = idr_find(&s->conn_idr, conid);
126 if (con)
127 conn_get(con);
128 spin_unlock_bh(&s->idr_lock);
129 return con;
130}
131
David S. Miller676d2362014-04-11 16:15:36 -0400132static void sock_data_ready(struct sock *sk)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400133{
134 struct tipc_conn *con;
135
136 read_lock(&sk->sk_callback_lock);
137 con = sock2con(sk);
138 if (con && test_bit(CF_CONNECTED, &con->flags)) {
139 conn_get(con);
140 if (!queue_work(con->server->rcv_wq, &con->rwork))
141 conn_put(con);
142 }
143 read_unlock(&sk->sk_callback_lock);
144}
145
146static void sock_write_space(struct sock *sk)
147{
148 struct tipc_conn *con;
149
150 read_lock(&sk->sk_callback_lock);
151 con = sock2con(sk);
152 if (con && test_bit(CF_CONNECTED, &con->flags)) {
153 conn_get(con);
154 if (!queue_work(con->server->send_wq, &con->swork))
155 conn_put(con);
156 }
157 read_unlock(&sk->sk_callback_lock);
158}
159
160static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
161{
162 struct sock *sk = sock->sk;
163
164 write_lock_bh(&sk->sk_callback_lock);
165
166 sk->sk_data_ready = sock_data_ready;
167 sk->sk_write_space = sock_write_space;
168 sk->sk_user_data = con;
169
170 con->sock = sock;
171
172 write_unlock_bh(&sk->sk_callback_lock);
173}
174
175static void tipc_unregister_callbacks(struct tipc_conn *con)
176{
177 struct sock *sk = con->sock->sk;
178
179 write_lock_bh(&sk->sk_callback_lock);
180 sk->sk_user_data = NULL;
181 write_unlock_bh(&sk->sk_callback_lock);
182}
183
184static void tipc_close_conn(struct tipc_conn *con)
185{
186 struct tipc_server *s = con->server;
187
188 if (test_and_clear_bit(CF_CONNECTED, &con->flags)) {
Ying Xue6d4ebeb2014-03-06 14:40:16 +0100189 if (con->conid)
190 s->tipc_conn_shutdown(con->conid, con->usr_data);
191
Ying Xuec5fa7b32013-06-17 10:54:39 -0400192 spin_lock_bh(&s->idr_lock);
193 idr_remove(&s->conn_idr, con->conid);
194 s->idr_in_use--;
195 spin_unlock_bh(&s->idr_lock);
196
197 tipc_unregister_callbacks(con);
198
199 /* We shouldn't flush pending works as we may be in the
200 * thread. In fact the races with pending rx/tx work structs
201 * are harmless for us here as we have already deleted this
202 * connection from server connection list and set
203 * sk->sk_user_data to 0 before releasing connection object.
204 */
205 kernel_sock_shutdown(con->sock, SHUT_RDWR);
206
207 conn_put(con);
208 }
209}
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{
245 struct msghdr msg = {};
246 struct tipc_server *s = con->server;
247 struct sockaddr_tipc addr;
248 struct kvec iov;
249 void *buf;
250 int ret;
251
252 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC);
253 if (!buf) {
254 ret = -ENOMEM;
255 goto out_close;
256 }
257
258 iov.iov_base = buf;
259 iov.iov_len = s->max_rcvbuf_size;
260 msg.msg_name = &addr;
261 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
262 MSG_DONTWAIT);
263 if (ret <= 0) {
264 kmem_cache_free(s->rcvbuf_cache, buf);
265 goto out_close;
266 }
267
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800268 s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr,
269 con->usr_data, buf, ret);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400270
271 kmem_cache_free(s->rcvbuf_cache, buf);
272
273 return 0;
274
275out_close:
276 if (ret != -EWOULDBLOCK)
277 tipc_close_conn(con);
278 else if (ret == 0)
279 /* Don't return success if we really got EOF */
280 ret = -EAGAIN;
281
282 return ret;
283}
284
285static int tipc_accept_from_sock(struct tipc_conn *con)
286{
287 struct tipc_server *s = con->server;
288 struct socket *sock = con->sock;
289 struct socket *newsock;
290 struct tipc_conn *newcon;
291 int ret;
292
Ying Xue76100a82015-03-18 09:32:57 +0800293 ret = kernel_accept(sock, &newsock, O_NONBLOCK);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400294 if (ret < 0)
295 return ret;
296
297 newcon = tipc_alloc_conn(con->server);
298 if (IS_ERR(newcon)) {
299 ret = PTR_ERR(newcon);
300 sock_release(newsock);
301 return ret;
302 }
303
304 newcon->rx_action = tipc_receive_from_sock;
305 tipc_register_callbacks(newsock, newcon);
306
307 /* Notify that new connection is incoming */
308 newcon->usr_data = s->tipc_conn_new(newcon->conid);
309
310 /* Wake up receive process in case of 'SYN+' message */
David S. Miller676d2362014-04-11 16:15:36 -0400311 newsock->sk->sk_data_ready(newsock->sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400312 return ret;
313}
314
315static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
316{
317 struct tipc_server *s = con->server;
318 struct socket *sock = NULL;
319 int ret;
320
Ying Xue76100a82015-03-18 09:32:57 +0800321 ret = sock_create_kern(AF_TIPC, SOCK_SEQPACKET, 0, &sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400322 if (ret < 0)
323 return NULL;
Ying Xue76100a82015-03-18 09:32:57 +0800324
325 sk_change_net(sock->sk, s->net);
326
Ying Xuec5fa7b32013-06-17 10:54:39 -0400327 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
328 (char *)&s->imp, sizeof(s->imp));
329 if (ret < 0)
330 goto create_err;
331 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr));
332 if (ret < 0)
333 goto create_err;
334
335 switch (s->type) {
336 case SOCK_STREAM:
337 case SOCK_SEQPACKET:
338 con->rx_action = tipc_accept_from_sock;
339
340 ret = kernel_listen(sock, 0);
341 if (ret < 0)
342 goto create_err;
343 break;
344 case SOCK_DGRAM:
345 case SOCK_RDM:
346 con->rx_action = tipc_receive_from_sock;
347 break;
348 default:
349 pr_err("Unknown socket type %d\n", s->type);
350 goto create_err;
351 }
Ying Xue76100a82015-03-18 09:32:57 +0800352
353 /* As server's listening socket owner and creator is the same module,
354 * we have to decrease TIPC module reference count to guarantee that
355 * it remains zero after the server socket is created, otherwise,
356 * executing "rmmod" command is unable to make TIPC module deleted
357 * after TIPC module is inserted successfully.
358 *
359 * However, the reference count is ever increased twice in
360 * sock_create_kern(): one is to increase the reference count of owner
361 * of TIPC socket's proto_ops struct; another is to increment the
362 * reference count of owner of TIPC proto struct. Therefore, we must
363 * decrement the module reference count twice to ensure that it keeps
364 * zero after server's listening socket is created. Of course, we
365 * must bump the module reference count twice as well before the socket
366 * is closed.
367 */
368 module_put(sock->ops->owner);
369 module_put(sock->sk->sk_prot_creator->owner);
370 set_bit(CF_SERVER, &con->flags);
371
Ying Xuec5fa7b32013-06-17 10:54:39 -0400372 return sock;
373
374create_err:
Ying Xue76100a82015-03-18 09:32:57 +0800375 kernel_sock_shutdown(sock, SHUT_RDWR);
376 sk_release_kernel(sock->sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400377 return NULL;
378}
379
380static int tipc_open_listening_sock(struct tipc_server *s)
381{
382 struct socket *sock;
383 struct tipc_conn *con;
384
385 con = tipc_alloc_conn(s);
386 if (IS_ERR(con))
387 return PTR_ERR(con);
388
389 sock = tipc_create_listen_sock(con);
Ying Xuec756891a2013-08-01 08:29:18 -0400390 if (!sock) {
391 idr_remove(&s->conn_idr, con->conid);
392 s->idr_in_use--;
393 kfree(con);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400394 return -EINVAL;
Ying Xuec756891a2013-08-01 08:29:18 -0400395 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400396
397 tipc_register_callbacks(sock, con);
398 return 0;
399}
400
401static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
402{
403 struct outqueue_entry *entry;
404 void *buf;
405
406 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
407 if (!entry)
408 return NULL;
409
410 buf = kmalloc(len, GFP_ATOMIC);
411 if (!buf) {
412 kfree(entry);
413 return NULL;
414 }
415
416 memcpy(buf, data, len);
417 entry->iov.iov_base = buf;
418 entry->iov.iov_len = len;
419
420 return entry;
421}
422
423static void tipc_free_entry(struct outqueue_entry *e)
424{
425 kfree(e->iov.iov_base);
426 kfree(e);
427}
428
429static void tipc_clean_outqueues(struct tipc_conn *con)
430{
431 struct outqueue_entry *e, *safe;
432
433 spin_lock_bh(&con->outqueue_lock);
434 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
435 list_del(&e->list);
436 tipc_free_entry(e);
437 }
438 spin_unlock_bh(&con->outqueue_lock);
439}
440
441int tipc_conn_sendmsg(struct tipc_server *s, int conid,
442 struct sockaddr_tipc *addr, void *data, size_t len)
443{
444 struct outqueue_entry *e;
445 struct tipc_conn *con;
446
447 con = tipc_conn_lookup(s, conid);
448 if (!con)
449 return -EINVAL;
450
451 e = tipc_alloc_entry(data, len);
452 if (!e) {
453 conn_put(con);
454 return -ENOMEM;
455 }
456
457 if (addr)
458 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
459
460 spin_lock_bh(&con->outqueue_lock);
461 list_add_tail(&e->list, &con->outqueue);
462 spin_unlock_bh(&con->outqueue_lock);
463
Ying Xue4652edb2014-03-06 14:40:17 +0100464 if (test_bit(CF_CONNECTED, &con->flags)) {
Ying Xuec5fa7b32013-06-17 10:54:39 -0400465 if (!queue_work(s->send_wq, &con->swork))
466 conn_put(con);
Ying Xue4652edb2014-03-06 14:40:17 +0100467 } else {
468 conn_put(con);
469 }
Ying Xuec5fa7b32013-06-17 10:54:39 -0400470 return 0;
471}
472
473void tipc_conn_terminate(struct tipc_server *s, int conid)
474{
475 struct tipc_conn *con;
476
477 con = tipc_conn_lookup(s, conid);
478 if (con) {
479 tipc_close_conn(con);
480 conn_put(con);
481 }
482}
483
484static void tipc_send_to_sock(struct tipc_conn *con)
485{
486 int count = 0;
487 struct tipc_server *s = con->server;
488 struct outqueue_entry *e;
489 struct msghdr msg;
490 int ret;
491
492 spin_lock_bh(&con->outqueue_lock);
493 while (1) {
494 e = list_entry(con->outqueue.next, struct outqueue_entry,
495 list);
496 if ((struct list_head *) e == &con->outqueue)
497 break;
498 spin_unlock_bh(&con->outqueue_lock);
499
500 memset(&msg, 0, sizeof(msg));
501 msg.msg_flags = MSG_DONTWAIT;
502
503 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
504 msg.msg_name = &e->dest;
505 msg.msg_namelen = sizeof(struct sockaddr_tipc);
506 }
507 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
508 e->iov.iov_len);
509 if (ret == -EWOULDBLOCK || ret == 0) {
510 cond_resched();
511 goto out;
512 } else if (ret < 0) {
513 goto send_err;
514 }
515
516 /* Don't starve users filling buffers */
517 if (++count >= MAX_SEND_MSG_COUNT) {
518 cond_resched();
519 count = 0;
520 }
521
522 spin_lock_bh(&con->outqueue_lock);
523 list_del(&e->list);
524 tipc_free_entry(e);
525 }
526 spin_unlock_bh(&con->outqueue_lock);
527out:
528 return;
529
530send_err:
531 tipc_close_conn(con);
532}
533
534static void tipc_recv_work(struct work_struct *work)
535{
536 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
537 int count = 0;
538
539 while (test_bit(CF_CONNECTED, &con->flags)) {
540 if (con->rx_action(con))
541 break;
542
543 /* Don't flood Rx machine */
544 if (++count >= MAX_RECV_MSG_COUNT) {
545 cond_resched();
546 count = 0;
547 }
548 }
549 conn_put(con);
550}
551
552static void tipc_send_work(struct work_struct *work)
553{
554 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
555
556 if (test_bit(CF_CONNECTED, &con->flags))
557 tipc_send_to_sock(con);
558
559 conn_put(con);
560}
561
562static void tipc_work_stop(struct tipc_server *s)
563{
564 destroy_workqueue(s->rcv_wq);
565 destroy_workqueue(s->send_wq);
566}
567
568static int tipc_work_start(struct tipc_server *s)
569{
570 s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1);
571 if (!s->rcv_wq) {
572 pr_err("can't start tipc receive workqueue\n");
573 return -ENOMEM;
574 }
575
576 s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1);
577 if (!s->send_wq) {
578 pr_err("can't start tipc send workqueue\n");
579 destroy_workqueue(s->rcv_wq);
580 return -ENOMEM;
581 }
582
583 return 0;
584}
585
586int tipc_server_start(struct tipc_server *s)
587{
588 int ret;
589
590 spin_lock_init(&s->idr_lock);
591 idr_init(&s->conn_idr);
592 s->idr_in_use = 0;
593
594 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
595 0, SLAB_HWCACHE_ALIGN, NULL);
596 if (!s->rcvbuf_cache)
597 return -ENOMEM;
598
599 ret = tipc_work_start(s);
600 if (ret < 0) {
601 kmem_cache_destroy(s->rcvbuf_cache);
602 return ret;
603 }
Ying Xuec756891a2013-08-01 08:29:18 -0400604 ret = tipc_open_listening_sock(s);
605 if (ret < 0) {
606 tipc_work_stop(s);
607 kmem_cache_destroy(s->rcvbuf_cache);
608 return ret;
609 }
Ying Xuec756891a2013-08-01 08:29:18 -0400610 return ret;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400611}
612
613void tipc_server_stop(struct tipc_server *s)
614{
615 struct tipc_conn *con;
616 int total = 0;
617 int id;
618
Ying Xuec5fa7b32013-06-17 10:54:39 -0400619 spin_lock_bh(&s->idr_lock);
620 for (id = 0; total < s->idr_in_use; id++) {
621 con = idr_find(&s->conn_idr, id);
622 if (con) {
623 total++;
624 spin_unlock_bh(&s->idr_lock);
625 tipc_close_conn(con);
626 spin_lock_bh(&s->idr_lock);
627 }
628 }
629 spin_unlock_bh(&s->idr_lock);
630
631 tipc_work_stop(s);
632 kmem_cache_destroy(s->rcvbuf_cache);
633 idr_destroy(&s->conn_idr);
634}