1/*
2 * net/tipc/subscr.c: TIPC network topology service
3 *
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
6 * 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
37#include "core.h"
38#include "name_table.h"
39#include "subscr.h"
40
41/**
42 * struct tipc_subscriber - TIPC network topology subscriber
43 * @conid: connection identifier to server connecting to subscriber
44 * @lock: control access to subscriber
45 * @subscription_list: list of subscription objects for this subscriber
46 */
47struct tipc_subscriber {
48	int conid;
49	spinlock_t lock;
50	struct list_head subscription_list;
51};
52
53/**
54 * htohl - convert value to endianness used by destination
55 * @in: value to convert
56 * @swap: non-zero if endianness must be reversed
57 *
58 * Returns converted value
59 */
60static u32 htohl(u32 in, int swap)
61{
62	return swap ? swab32(in) : in;
63}
64
65static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
66			      u32 found_upper, u32 event, u32 port_ref,
67			      u32 node)
68{
69	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
70	struct tipc_subscriber *subscriber = sub->subscriber;
71	struct kvec msg_sect;
72
73	msg_sect.iov_base = (void *)&sub->evt;
74	msg_sect.iov_len = sizeof(struct tipc_event);
75	sub->evt.event = htohl(event, sub->swap);
76	sub->evt.found_lower = htohl(found_lower, sub->swap);
77	sub->evt.found_upper = htohl(found_upper, sub->swap);
78	sub->evt.port.ref = htohl(port_ref, sub->swap);
79	sub->evt.port.node = htohl(node, sub->swap);
80	tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
81			  msg_sect.iov_base, msg_sect.iov_len);
82}
83
84/**
85 * tipc_subscr_overlap - test for subscription overlap with the given values
86 *
87 * Returns 1 if there is overlap, otherwise 0.
88 */
89int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
90			u32 found_upper)
91{
92	if (found_lower < sub->seq.lower)
93		found_lower = sub->seq.lower;
94	if (found_upper > sub->seq.upper)
95		found_upper = sub->seq.upper;
96	if (found_lower > found_upper)
97		return 0;
98	return 1;
99}
100
101/**
102 * tipc_subscr_report_overlap - issue event if there is subscription overlap
103 *
104 * Protected by nameseq.lock in name_table.c
105 */
106void tipc_subscr_report_overlap(struct tipc_subscription *sub, u32 found_lower,
107				u32 found_upper, u32 event, u32 port_ref,
108				u32 node, int must)
109{
110	if (!tipc_subscr_overlap(sub, found_lower, found_upper))
111		return;
112	if (!must && !(sub->filter & TIPC_SUB_PORTS))
113		return;
114
115	subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
116}
117
118static void subscr_timeout(unsigned long data)
119{
120	struct tipc_subscription *sub = (struct tipc_subscription *)data;
121	struct tipc_subscriber *subscriber = sub->subscriber;
122	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
123
124	/* The spin lock per subscriber is used to protect its members */
125	spin_lock_bh(&subscriber->lock);
126
127	/* Validate timeout (in case subscription is being cancelled) */
128	if (sub->timeout == TIPC_WAIT_FOREVER) {
129		spin_unlock_bh(&subscriber->lock);
130		return;
131	}
132
133	/* Unlink subscription from name table */
134	tipc_nametbl_unsubscribe(sub);
135
136	/* Unlink subscription from subscriber */
137	list_del(&sub->subscription_list);
138
139	spin_unlock_bh(&subscriber->lock);
140
141	/* Notify subscriber of timeout */
142	subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
143			  TIPC_SUBSCR_TIMEOUT, 0, 0);
144
145	/* Now destroy subscription */
146	kfree(sub);
147	atomic_dec(&tn->subscription_count);
148}
149
150/**
151 * subscr_del - delete a subscription within a subscription list
152 *
153 * Called with subscriber lock held.
154 */
155static void subscr_del(struct tipc_subscription *sub)
156{
157	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
158
159	tipc_nametbl_unsubscribe(sub);
160	list_del(&sub->subscription_list);
161	kfree(sub);
162	atomic_dec(&tn->subscription_count);
163}
164
165static void subscr_release(struct tipc_subscriber *subscriber)
166{
167	struct tipc_subscription *sub;
168	struct tipc_subscription *sub_temp;
169
170	spin_lock_bh(&subscriber->lock);
171
172	/* Destroy any existing subscriptions for subscriber */
173	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
174				 subscription_list) {
175		if (sub->timeout != TIPC_WAIT_FOREVER) {
176			spin_unlock_bh(&subscriber->lock);
177			del_timer_sync(&sub->timer);
178			spin_lock_bh(&subscriber->lock);
179		}
180		subscr_del(sub);
181	}
182	spin_unlock_bh(&subscriber->lock);
183
184	/* Now destroy subscriber */
185	kfree(subscriber);
186}
187
188/**
189 * subscr_cancel - handle subscription cancellation request
190 *
191 * Called with subscriber lock held. Routine must temporarily release lock
192 * to enable the subscription timeout routine to finish without deadlocking;
193 * the lock is then reclaimed to allow caller to release it upon return.
194 *
195 * Note that fields of 's' use subscriber's endianness!
196 */
197static void subscr_cancel(struct tipc_subscr *s,
198			  struct tipc_subscriber *subscriber)
199{
200	struct tipc_subscription *sub;
201	struct tipc_subscription *sub_temp;
202	int found = 0;
203
204	/* Find first matching subscription, exit if not found */
205	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
206				 subscription_list) {
207		if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
208			found = 1;
209			break;
210		}
211	}
212	if (!found)
213		return;
214
215	/* Cancel subscription timer (if used), then delete subscription */
216	if (sub->timeout != TIPC_WAIT_FOREVER) {
217		sub->timeout = TIPC_WAIT_FOREVER;
218		spin_unlock_bh(&subscriber->lock);
219		del_timer_sync(&sub->timer);
220		spin_lock_bh(&subscriber->lock);
221	}
222	subscr_del(sub);
223}
224
225/**
226 * subscr_subscribe - create subscription for subscriber
227 *
228 * Called with subscriber lock held.
229 */
230static int subscr_subscribe(struct net *net, struct tipc_subscr *s,
231			    struct tipc_subscriber *subscriber,
232			    struct tipc_subscription **sub_p)
233{
234	struct tipc_net *tn = net_generic(net, tipc_net_id);
235	struct tipc_subscription *sub;
236	int swap;
237
238	/* Determine subscriber's endianness */
239	swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
240
241	/* Detect & process a subscription cancellation request */
242	if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
243		s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
244		subscr_cancel(s, subscriber);
245		return 0;
246	}
247
248	/* Refuse subscription if global limit exceeded */
249	if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
250		pr_warn("Subscription rejected, limit reached (%u)\n",
251			TIPC_MAX_SUBSCRIPTIONS);
252		return -EINVAL;
253	}
254
255	/* Allocate subscription object */
256	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
257	if (!sub) {
258		pr_warn("Subscription rejected, no memory\n");
259		return -ENOMEM;
260	}
261
262	/* Initialize subscription object */
263	sub->net = net;
264	sub->seq.type = htohl(s->seq.type, swap);
265	sub->seq.lower = htohl(s->seq.lower, swap);
266	sub->seq.upper = htohl(s->seq.upper, swap);
267	sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
268	sub->filter = htohl(s->filter, swap);
269	if ((!(sub->filter & TIPC_SUB_PORTS) ==
270	     !(sub->filter & TIPC_SUB_SERVICE)) ||
271	    (sub->seq.lower > sub->seq.upper)) {
272		pr_warn("Subscription rejected, illegal request\n");
273		kfree(sub);
274		return -EINVAL;
275	}
276	list_add(&sub->subscription_list, &subscriber->subscription_list);
277	sub->subscriber = subscriber;
278	sub->swap = swap;
279	memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
280	atomic_inc(&tn->subscription_count);
281	if (sub->timeout != TIPC_WAIT_FOREVER) {
282		setup_timer(&sub->timer, subscr_timeout, (unsigned long)sub);
283		mod_timer(&sub->timer, jiffies + sub->timeout);
284	}
285	*sub_p = sub;
286	return 0;
287}
288
289/* Handle one termination request for the subscriber */
290static void subscr_conn_shutdown_event(int conid, void *usr_data)
291{
292	subscr_release((struct tipc_subscriber *)usr_data);
293}
294
295/* Handle one request to create a new subscription for the subscriber */
296static void subscr_conn_msg_event(struct net *net, int conid,
297				  struct sockaddr_tipc *addr, void *usr_data,
298				  void *buf, size_t len)
299{
300	struct tipc_subscriber *subscriber = usr_data;
301	struct tipc_subscription *sub = NULL;
302	struct tipc_net *tn = net_generic(net, tipc_net_id);
303
304	spin_lock_bh(&subscriber->lock);
305	if (subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber, &sub))
306		tipc_conn_terminate(tn->topsrv, subscriber->conid);
307	else
308		tipc_nametbl_subscribe(sub);
309	spin_unlock_bh(&subscriber->lock);
310}
311
312/* Handle one request to establish a new subscriber */
313static void *subscr_named_msg_event(int conid)
314{
315	struct tipc_subscriber *subscriber;
316
317	/* Create subscriber object */
318	subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
319	if (subscriber == NULL) {
320		pr_warn("Subscriber rejected, no memory\n");
321		return NULL;
322	}
323	INIT_LIST_HEAD(&subscriber->subscription_list);
324	subscriber->conid = conid;
325	spin_lock_init(&subscriber->lock);
326
327	return (void *)subscriber;
328}
329
330int tipc_subscr_start(struct net *net)
331{
332	struct tipc_net *tn = net_generic(net, tipc_net_id);
333	const char name[] = "topology_server";
334	struct tipc_server *topsrv;
335	struct sockaddr_tipc *saddr;
336
337	saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
338	if (!saddr)
339		return -ENOMEM;
340	saddr->family			= AF_TIPC;
341	saddr->addrtype			= TIPC_ADDR_NAMESEQ;
342	saddr->addr.nameseq.type	= TIPC_TOP_SRV;
343	saddr->addr.nameseq.lower	= TIPC_TOP_SRV;
344	saddr->addr.nameseq.upper	= TIPC_TOP_SRV;
345	saddr->scope			= TIPC_NODE_SCOPE;
346
347	topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
348	if (!topsrv) {
349		kfree(saddr);
350		return -ENOMEM;
351	}
352	topsrv->net			= net;
353	topsrv->saddr			= saddr;
354	topsrv->imp			= TIPC_CRITICAL_IMPORTANCE;
355	topsrv->type			= SOCK_SEQPACKET;
356	topsrv->max_rcvbuf_size		= sizeof(struct tipc_subscr);
357	topsrv->tipc_conn_recvmsg	= subscr_conn_msg_event;
358	topsrv->tipc_conn_new		= subscr_named_msg_event;
359	topsrv->tipc_conn_shutdown	= subscr_conn_shutdown_event;
360
361	strncpy(topsrv->name, name, strlen(name) + 1);
362	tn->topsrv = topsrv;
363	atomic_set(&tn->subscription_count, 0);
364
365	return tipc_server_start(topsrv);
366}
367
368void tipc_subscr_stop(struct net *net)
369{
370	struct tipc_net *tn = net_generic(net, tipc_net_id);
371	struct tipc_server *topsrv = tn->topsrv;
372
373	tipc_server_stop(topsrv);
374	kfree(topsrv->saddr);
375	kfree(topsrv);
376}
377