subscr.c 11.7 KB
Newer Older
P
Per Liden 已提交
1
/*
2
 * net/tipc/subscr.c: TIPC network topology service
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 2000-2006, Ericsson AB
5
 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
11 12 13 14 15 16 17 18
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
P
Per Liden 已提交
19
 *
P
Per Liden 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
P
Per Liden 已提交
34 35 36 37 38
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "name_table.h"
39
#include "subscr.h"
P
Per Liden 已提交
40 41

/**
42
 * struct tipc_subscriber - TIPC network topology subscriber
43
 * @conid: connection identifier to server connecting to subscriber
S
stephen hemminger 已提交
44
 * @lock: control access to subscriber
P
Per Liden 已提交
45 46
 * @subscription_list: list of subscription objects for this subscriber
 */
47
struct tipc_subscriber {
48 49
	int conid;
	spinlock_t lock;
P
Per Liden 已提交
50 51 52
	struct list_head subscription_list;
};

53 54 55 56 57 58 59 60 61 62 63 64
/**
 * htohl - convert value to endianness used by destination
 * @in: value to convert
 * @swap: non-zero if endianness must be reversed
 *
 * Returns converted value
 */
static u32 htohl(u32 in, int swap)
{
	return swap ? swab32(in) : in;
}

65 66
static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
			      u32 found_upper, u32 event, u32 port_ref,
P
Per Liden 已提交
67 68
			      u32 node)
{
69
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
70 71
	struct tipc_subscriber *subscriber = sub->subscriber;
	struct kvec msg_sect;
P
Per Liden 已提交
72 73 74

	msg_sect.iov_base = (void *)&sub->evt;
	msg_sect.iov_len = sizeof(struct tipc_event);
75 76 77 78 79
	sub->evt.event = htohl(event, sub->swap);
	sub->evt.found_lower = htohl(found_lower, sub->swap);
	sub->evt.found_upper = htohl(found_upper, sub->swap);
	sub->evt.port.ref = htohl(port_ref, sub->swap);
	sub->evt.port.node = htohl(node, sub->swap);
80 81
	tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
			  msg_sect.iov_base, msg_sect.iov_len);
P
Per Liden 已提交
82 83 84
}

/**
85
 * tipc_subscr_overlap - test for subscription overlap with the given values
P
Per Liden 已提交
86 87 88
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
89
int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
90
			u32 found_upper)
P
Per Liden 已提交
91 92 93 94 95 96 97 98 99 100 101
{
	if (found_lower < sub->seq.lower)
		found_lower = sub->seq.lower;
	if (found_upper > sub->seq.upper)
		found_upper = sub->seq.upper;
	if (found_lower > found_upper)
		return 0;
	return 1;
}

/**
102
 * tipc_subscr_report_overlap - issue event if there is subscription overlap
103
 *
P
Per Liden 已提交
104 105
 * Protected by nameseq.lock in name_table.c
 */
106 107 108
void tipc_subscr_report_overlap(struct tipc_subscription *sub, u32 found_lower,
				u32 found_upper, u32 event, u32 port_ref,
				u32 node, int must)
P
Per Liden 已提交
109
{
110
	if (!tipc_subscr_overlap(sub, found_lower, found_upper))
P
Per Liden 已提交
111
		return;
112
	if (!must && !(sub->filter & TIPC_SUB_PORTS))
P
Per Liden 已提交
113
		return;
114

115
	subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
P
Per Liden 已提交
116 117
}

118
static void subscr_timeout(unsigned long data)
P
Per Liden 已提交
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);
P
Per Liden 已提交
123

124 125 126
	/* The spin lock per subscriber is used to protect its members */
	spin_lock_bh(&subscriber->lock);

127 128
	/* Validate timeout (in case subscription is being cancelled) */
	if (sub->timeout == TIPC_WAIT_FOREVER) {
129
		spin_unlock_bh(&subscriber->lock);
130 131 132
		return;
	}

P
Per Liden 已提交
133
	/* Unlink subscription from name table */
134
	tipc_nametbl_unsubscribe(sub);
P
Per Liden 已提交
135

136
	/* Unlink subscription from subscriber */
P
Per Liden 已提交
137 138
	list_del(&sub->subscription_list);

139
	spin_unlock_bh(&subscriber->lock);
140 141 142 143 144

	/* Notify subscriber of timeout */
	subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
			  TIPC_SUBSCR_TIMEOUT, 0, 0);

P
Per Liden 已提交
145 146
	/* Now destroy subscription */
	kfree(sub);
147
	atomic_dec(&tn->subscription_count);
P
Per Liden 已提交
148 149
}

150 151 152
/**
 * subscr_del - delete a subscription within a subscription list
 *
153
 * Called with subscriber lock held.
154
 */
155
static void subscr_del(struct tipc_subscription *sub)
156
{
157 158
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);

159 160 161
	tipc_nametbl_unsubscribe(sub);
	list_del(&sub->subscription_list);
	kfree(sub);
162
	atomic_dec(&tn->subscription_count);
163 164
}

P
Per Liden 已提交
165 166
/**
 * subscr_terminate - terminate communication with a subscriber
167
 *
168
 * Note: Must call it in process context since it might sleep.
P
Per Liden 已提交
169
 */
170
static void subscr_terminate(struct tipc_subscription *sub)
P
Per Liden 已提交
171
{
172 173 174 175
	struct tipc_subscriber *subscriber = sub->subscriber;
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);

	tipc_conn_terminate(tn->topsrv, subscriber->conid);
176 177 178 179
}

static void subscr_release(struct tipc_subscriber *subscriber)
{
180 181
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
P
Per Liden 已提交
182

183
	spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
184 185 186 187 188

	/* Destroy any existing subscriptions for subscriber */
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
				 subscription_list) {
		if (sub->timeout != TIPC_WAIT_FOREVER) {
189
			spin_unlock_bh(&subscriber->lock);
190
			del_timer_sync(&sub->timer);
191
			spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
192
		}
193
		subscr_del(sub);
P
Per Liden 已提交
194
	}
195
	spin_unlock_bh(&subscriber->lock);
196 197

	/* Now destroy subscriber */
P
Per Liden 已提交
198 199 200
	kfree(subscriber);
}

201 202 203
/**
 * subscr_cancel - handle subscription cancellation request
 *
204
 * Called with subscriber lock held. Routine must temporarily release lock
205 206 207 208 209 210
 * to enable the subscription timeout routine to finish without deadlocking;
 * the lock is then reclaimed to allow caller to release it upon return.
 *
 * Note that fields of 's' use subscriber's endianness!
 */
static void subscr_cancel(struct tipc_subscr *s,
211
			  struct tipc_subscriber *subscriber)
212
{
213 214
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
215 216 217 218 219
	int found = 0;

	/* Find first matching subscription, exit if not found */
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
				 subscription_list) {
220 221 222 223
		if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
			found = 1;
			break;
		}
224 225 226 227 228 229 230
	}
	if (!found)
		return;

	/* Cancel subscription timer (if used), then delete subscription */
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		sub->timeout = TIPC_WAIT_FOREVER;
231
		spin_unlock_bh(&subscriber->lock);
232
		del_timer_sync(&sub->timer);
233
		spin_lock_bh(&subscriber->lock);
234 235 236 237
	}
	subscr_del(sub);
}

P
Per Liden 已提交
238 239
/**
 * subscr_subscribe - create subscription for subscriber
240
 *
241
 * Called with subscriber lock held.
P
Per Liden 已提交
242
 */
243
static int subscr_subscribe(struct net *net, struct tipc_subscr *s,
244
			    struct tipc_subscriber *subscriber,
245 246 247
			    struct tipc_subscription **sub_p)
{
	struct tipc_net *tn = net_generic(net, tipc_net_id);
248
	struct tipc_subscription *sub;
249 250 251 252
	int swap;

	/* Determine subscriber's endianness */
	swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
253 254

	/* Detect & process a subscription cancellation request */
255 256
	if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
		s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
257
		subscr_cancel(s, subscriber);
258
		return 0;
259 260
	}

P
Per Liden 已提交
261
	/* Refuse subscription if global limit exceeded */
262
	if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
263
		pr_warn("Subscription rejected, limit reached (%u)\n",
264
			TIPC_MAX_SUBSCRIPTIONS);
265
		return -EINVAL;
P
Per Liden 已提交
266 267 268
	}

	/* Allocate subscription object */
269
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
270
	if (!sub) {
271
		pr_warn("Subscription rejected, no memory\n");
272
		return -ENOMEM;
P
Per Liden 已提交
273 274 275
	}

	/* Initialize subscription object */
276
	sub->net = net;
277 278 279
	sub->seq.type = htohl(s->seq.type, swap);
	sub->seq.lower = htohl(s->seq.lower, swap);
	sub->seq.upper = htohl(s->seq.upper, swap);
280
	sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
281
	sub->filter = htohl(s->filter, swap);
282 283
	if ((!(sub->filter & TIPC_SUB_PORTS) ==
	     !(sub->filter & TIPC_SUB_SERVICE)) ||
284
	    (sub->seq.lower > sub->seq.upper)) {
285
		pr_warn("Subscription rejected, illegal request\n");
P
Per Liden 已提交
286
		kfree(sub);
287
		return -EINVAL;
P
Per Liden 已提交
288 289
	}
	list_add(&sub->subscription_list, &subscriber->subscription_list);
290
	sub->subscriber = subscriber;
291
	sub->swap = swap;
292
	memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
293
	atomic_inc(&tn->subscription_count);
P
Per Liden 已提交
294
	if (sub->timeout != TIPC_WAIT_FOREVER) {
295 296
		setup_timer(&sub->timer, subscr_timeout, (unsigned long)sub);
		mod_timer(&sub->timer, jiffies + sub->timeout);
P
Per Liden 已提交
297
	}
298 299
	*sub_p = sub;
	return 0;
P
Per Liden 已提交
300 301
}

302 303
/* Handle one termination request for the subscriber */
static void subscr_conn_shutdown_event(int conid, void *usr_data)
P
Per Liden 已提交
304
{
305
	subscr_release((struct tipc_subscriber *)usr_data);
P
Per Liden 已提交
306 307
}

308
/* Handle one request to create a new subscription for the subscriber */
309 310 311
static void subscr_conn_msg_event(struct net *net, int conid,
				  struct sockaddr_tipc *addr, void *usr_data,
				  void *buf, size_t len)
P
Per Liden 已提交
312
{
313
	struct tipc_subscriber *subscriber = usr_data;
314
	struct tipc_subscription *sub = NULL;
315

316
	spin_lock_bh(&subscriber->lock);
317 318
	if (subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber,
			     &sub) < 0) {
319
		spin_unlock_bh(&subscriber->lock);
320
		subscr_terminate(sub);
321 322
		return;
	}
323 324 325
	if (sub)
		tipc_nametbl_subscribe(sub);
	spin_unlock_bh(&subscriber->lock);
P
Per Liden 已提交
326 327
}

328 329
/* Handle one request to establish a new subscriber */
static void *subscr_named_msg_event(int conid)
P
Per Liden 已提交
330
{
331
	struct tipc_subscriber *subscriber;
P
Per Liden 已提交
332 333

	/* Create subscriber object */
334
	subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
P
Per Liden 已提交
335
	if (subscriber == NULL) {
336
		pr_warn("Subscriber rejected, no memory\n");
337
		return NULL;
P
Per Liden 已提交
338 339
	}
	INIT_LIST_HEAD(&subscriber->subscription_list);
340 341
	subscriber->conid = conid;
	spin_lock_init(&subscriber->lock);
342

343
	return (void *)subscriber;
P
Per Liden 已提交
344 345
}

346
int tipc_subscr_start(struct net *net)
P
Per Liden 已提交
347
{
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	struct tipc_net *tn = net_generic(net, tipc_net_id);
	const char name[] = "topology_server";
	struct tipc_server *topsrv;
	struct sockaddr_tipc *saddr;

	saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
	if (!saddr)
		return -ENOMEM;
	saddr->family			= AF_TIPC;
	saddr->addrtype			= TIPC_ADDR_NAMESEQ;
	saddr->addr.nameseq.type	= TIPC_TOP_SRV;
	saddr->addr.nameseq.lower	= TIPC_TOP_SRV;
	saddr->addr.nameseq.upper	= TIPC_TOP_SRV;
	saddr->scope			= TIPC_NODE_SCOPE;

	topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
	if (!topsrv) {
		kfree(saddr);
		return -ENOMEM;
	}
	topsrv->net			= net;
	topsrv->saddr			= saddr;
	topsrv->imp			= TIPC_CRITICAL_IMPORTANCE;
	topsrv->type			= SOCK_SEQPACKET;
	topsrv->max_rcvbuf_size		= sizeof(struct tipc_subscr);
	topsrv->tipc_conn_recvmsg	= subscr_conn_msg_event;
	topsrv->tipc_conn_new		= subscr_named_msg_event;
	topsrv->tipc_conn_shutdown	= subscr_conn_shutdown_event;

	strncpy(topsrv->name, name, strlen(name) + 1);
	tn->topsrv = topsrv;
	atomic_set(&tn->subscription_count, 0);

	return tipc_server_start(topsrv);
P
Per Liden 已提交
382 383
}

384
void tipc_subscr_stop(struct net *net)
P
Per Liden 已提交
385
{
386 387 388 389 390 391
	struct tipc_net *tn = net_generic(net, tipc_net_id);
	struct tipc_server *topsrv = tn->topsrv;

	tipc_server_stop(topsrv);
	kfree(topsrv->saddr);
	kfree(topsrv);
P
Per Liden 已提交
392
}