subscr.c 11.0 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
45
 * @subscrp_list: list of subscription objects for this subscriber
P
Per Liden 已提交
46
 */
47
struct tipc_subscriber {
48 49
	int conid;
	spinlock_t lock;
50
	struct list_head subscrp_list;
P
Per Liden 已提交
51 52
};

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 67
static void tipc_subscrp_send_event(struct tipc_subscription *sub,
				    u32 found_lower, u32 found_upper,
				    u32 event, u32 port_ref, u32 node)
P
Per Liden 已提交
68
{
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 86
 * tipc_subscrp_check_overlap - test for subscription overlap with the
 * given values
P
Per Liden 已提交
87 88 89
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
90 91
int tipc_subscrp_check_overlap(struct tipc_subscription *sub, u32 found_lower,
			       u32 found_upper)
P
Per Liden 已提交
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 103 104
void tipc_subscrp_report_overlap(struct tipc_subscription *sub, u32 found_lower,
				 u32 found_upper, u32 event, u32 port_ref,
				 u32 node, int must)
P
Per Liden 已提交
105
{
106
	if (!tipc_subscrp_check_overlap(sub, found_lower, found_upper))
P
Per Liden 已提交
107
		return;
108
	if (!must && !(sub->filter & TIPC_SUB_PORTS))
P
Per Liden 已提交
109
		return;
110

111 112
	tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
				node);
P
Per Liden 已提交
113 114
}

115
static void tipc_subscrp_timeout(unsigned long data)
P
Per Liden 已提交
116
{
117
	struct tipc_subscription *sub = (struct tipc_subscription *)data;
118
	struct tipc_subscriber *subscriber = sub->subscriber;
119
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
P
Per Liden 已提交
120

121 122 123
	/* The spin lock per subscriber is used to protect its members */
	spin_lock_bh(&subscriber->lock);

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

P
Per Liden 已提交
130
	/* Unlink subscription from name table */
131
	tipc_nametbl_unsubscribe(sub);
P
Per Liden 已提交
132

133
	/* Unlink subscription from subscriber */
134
	list_del(&sub->subscrp_list);
P
Per Liden 已提交
135

136
	spin_unlock_bh(&subscriber->lock);
137 138

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

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

147
static void tipc_subscrp_delete(struct tipc_subscription *sub)
148
{
149 150
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);

151
	tipc_nametbl_unsubscribe(sub);
152
	list_del(&sub->subscrp_list);
153
	kfree(sub);
154
	atomic_dec(&tn->subscription_count);
155 156
}

157
static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
158
{
159 160
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
P
Per Liden 已提交
161

162
	spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
163 164

	/* Destroy any existing subscriptions for subscriber */
165 166
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscrp_list,
				 subscrp_list) {
P
Per Liden 已提交
167
		if (sub->timeout != TIPC_WAIT_FOREVER) {
168
			spin_unlock_bh(&subscriber->lock);
169
			del_timer_sync(&sub->timer);
170
			spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
171
		}
172
		tipc_subscrp_delete(sub);
P
Per Liden 已提交
173
	}
174
	spin_unlock_bh(&subscriber->lock);
175 176

	/* Now destroy subscriber */
P
Per Liden 已提交
177 178 179
	kfree(subscriber);
}

180
/**
181
 * tipc_subscrp_cancel - handle subscription cancellation request
182
 *
183
 * Called with subscriber lock held. Routine must temporarily release lock
184 185 186 187 188
 * 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!
 */
189 190
static void tipc_subscrp_cancel(struct tipc_subscr *s,
				struct tipc_subscriber *subscriber)
191
{
192 193
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
194 195 196
	int found = 0;

	/* Find first matching subscription, exit if not found */
197 198
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscrp_list,
				 subscrp_list) {
199 200 201 202
		if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
			found = 1;
			break;
		}
203 204 205 206 207 208 209
	}
	if (!found)
		return;

	/* Cancel subscription timer (if used), then delete subscription */
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		sub->timeout = TIPC_WAIT_FOREVER;
210
		spin_unlock_bh(&subscriber->lock);
211
		del_timer_sync(&sub->timer);
212
		spin_lock_bh(&subscriber->lock);
213
	}
214
	tipc_subscrp_delete(sub);
215 216
}

217 218 219
static int tipc_subscrp_create(struct net *net, struct tipc_subscr *s,
			       struct tipc_subscriber *subscriber,
			       struct tipc_subscription **sub_p)
220 221
{
	struct tipc_net *tn = net_generic(net, tipc_net_id);
222
	struct tipc_subscription *sub;
223 224 225 226
	int swap;

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

	/* Detect & process a subscription cancellation request */
229 230
	if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
		s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
231
		tipc_subscrp_cancel(s, subscriber);
232
		return 0;
233 234
	}

P
Per Liden 已提交
235
	/* Refuse subscription if global limit exceeded */
236
	if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
237
		pr_warn("Subscription rejected, limit reached (%u)\n",
238
			TIPC_MAX_SUBSCRIPTIONS);
239
		return -EINVAL;
P
Per Liden 已提交
240 241 242
	}

	/* Allocate subscription object */
243
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
244
	if (!sub) {
245
		pr_warn("Subscription rejected, no memory\n");
246
		return -ENOMEM;
P
Per Liden 已提交
247 248 249
	}

	/* Initialize subscription object */
250
	sub->net = net;
251 252 253
	sub->seq.type = htohl(s->seq.type, swap);
	sub->seq.lower = htohl(s->seq.lower, swap);
	sub->seq.upper = htohl(s->seq.upper, swap);
254
	sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
255
	sub->filter = htohl(s->filter, swap);
256 257
	if ((!(sub->filter & TIPC_SUB_PORTS) ==
	     !(sub->filter & TIPC_SUB_SERVICE)) ||
258
	    (sub->seq.lower > sub->seq.upper)) {
259
		pr_warn("Subscription rejected, illegal request\n");
P
Per Liden 已提交
260
		kfree(sub);
261
		return -EINVAL;
P
Per Liden 已提交
262
	}
263
	list_add(&sub->subscrp_list, &subscriber->subscrp_list);
264
	sub->subscriber = subscriber;
265
	sub->swap = swap;
266
	memcpy(&sub->evt.s, s, sizeof(*s));
267
	atomic_inc(&tn->subscription_count);
P
Per Liden 已提交
268
	if (sub->timeout != TIPC_WAIT_FOREVER) {
269 270
		setup_timer(&sub->timer, tipc_subscrp_timeout,
			    (unsigned long)sub);
271
		mod_timer(&sub->timer, jiffies + sub->timeout);
P
Per Liden 已提交
272
	}
273 274
	*sub_p = sub;
	return 0;
P
Per Liden 已提交
275 276
}

277
/* Handle one termination request for the subscriber */
278
static void tipc_subscrb_shutdown_cb(int conid, void *usr_data)
P
Per Liden 已提交
279
{
280
	tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
P
Per Liden 已提交
281 282
}

283
/* Handle one request to create a new subscription for the subscriber */
284 285 286
static void tipc_subscrb_rcv_cb(struct net *net, int conid,
				struct sockaddr_tipc *addr, void *usr_data,
				void *buf, size_t len)
P
Per Liden 已提交
287
{
288
	struct tipc_subscriber *subscriber = usr_data;
289
	struct tipc_subscription *sub = NULL;
290
	struct tipc_net *tn = net_generic(net, tipc_net_id);
291

292
	spin_lock_bh(&subscriber->lock);
293
	tipc_subscrp_create(net, (struct tipc_subscr *)buf, subscriber, &sub);
294 295
	if (sub)
		tipc_nametbl_subscribe(sub);
296 297
	else
		tipc_conn_terminate(tn->topsrv, subscriber->conid);
298
	spin_unlock_bh(&subscriber->lock);
P
Per Liden 已提交
299 300
}

301
/* Handle one request to establish a new subscriber */
302
static void *tipc_subscrb_connect_cb(int conid)
P
Per Liden 已提交
303
{
304
	struct tipc_subscriber *subscriber;
P
Per Liden 已提交
305 306

	/* Create subscriber object */
307
	subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
P
Per Liden 已提交
308
	if (subscriber == NULL) {
309
		pr_warn("Subscriber rejected, no memory\n");
310
		return NULL;
P
Per Liden 已提交
311
	}
312
	INIT_LIST_HEAD(&subscriber->subscrp_list);
313 314
	subscriber->conid = conid;
	spin_lock_init(&subscriber->lock);
315

316
	return (void *)subscriber;
P
Per Liden 已提交
317 318
}

319
int tipc_topsrv_start(struct net *net)
P
Per Liden 已提交
320
{
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	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);
346 347 348
	topsrv->tipc_conn_recvmsg	= tipc_subscrb_rcv_cb;
	topsrv->tipc_conn_new		= tipc_subscrb_connect_cb;
	topsrv->tipc_conn_shutdown	= tipc_subscrb_shutdown_cb;
349 350 351 352 353 354

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

	return tipc_server_start(topsrv);
P
Per Liden 已提交
355 356
}

357
void tipc_topsrv_stop(struct net *net)
P
Per Liden 已提交
358
{
359 360 361 362 363 364
	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 已提交
365
}