Linux Audio

Check our new training course

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