subscr.c 11.9 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
 * @kref: reference counter to tipc_subscription object
44
 * @conid: connection identifier to server connecting to subscriber
S
stephen hemminger 已提交
45
 * @lock: control access to subscriber
46
 * @subscrp_list: list of subscription objects for this subscriber
P
Per Liden 已提交
47
 */
48
struct tipc_subscriber {
49
	struct kref kref;
50 51
	int conid;
	spinlock_t lock;
52
	struct list_head subscrp_list;
P
Per Liden 已提交
53 54
};

55 56
static void tipc_subscrp_delete(struct tipc_subscription *sub);
static void tipc_subscrb_put(struct tipc_subscriber *subscriber);
57 58
static void tipc_subscrp_put(struct tipc_subscription *subscription);
static void tipc_subscrp_get(struct tipc_subscription *subscription);
59

60 61 62 63 64 65 66 67 68 69 70 71
/**
 * 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;
}

72 73 74
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 已提交
75
{
76
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
77 78
	struct tipc_subscriber *subscriber = sub->subscriber;
	struct kvec msg_sect;
P
Per Liden 已提交
79 80 81

	msg_sect.iov_base = (void *)&sub->evt;
	msg_sect.iov_len = sizeof(struct tipc_event);
82 83 84 85 86
	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);
87 88
	tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
			  msg_sect.iov_base, msg_sect.iov_len);
P
Per Liden 已提交
89 90 91
}

/**
92 93
 * tipc_subscrp_check_overlap - test for subscription overlap with the
 * given values
P
Per Liden 已提交
94 95 96
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
97
int tipc_subscrp_check_overlap(struct tipc_name_seq *seq, u32 found_lower,
98
			       u32 found_upper)
P
Per Liden 已提交
99
{
100 101 102 103
	if (found_lower < seq->lower)
		found_lower = seq->lower;
	if (found_upper > seq->upper)
		found_upper = seq->upper;
P
Per Liden 已提交
104 105 106 107 108
	if (found_lower > found_upper)
		return 0;
	return 1;
}

109 110 111 112 113 114 115 116 117 118 119 120 121
u32 tipc_subscrp_convert_seq_type(u32 type, int swap)
{
	return htohl(type, swap);
}

void tipc_subscrp_convert_seq(struct tipc_name_seq *in, int swap,
			      struct tipc_name_seq *out)
{
	out->type = htohl(in->type, swap);
	out->lower = htohl(in->lower, swap);
	out->upper = htohl(in->upper, swap);
}

122 123 124
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 已提交
125
{
126 127
	struct tipc_name_seq seq;

128
	tipc_subscrp_get(sub);
129 130
	tipc_subscrp_convert_seq(&sub->evt.s.seq, sub->swap, &seq);
	if (!tipc_subscrp_check_overlap(&seq, found_lower, found_upper))
P
Per Liden 已提交
131
		return;
132 133
	if (!must &&
	    !(htohl(sub->evt.s.filter, sub->swap) & TIPC_SUB_PORTS))
P
Per Liden 已提交
134
		return;
135

136 137
	tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
				node);
138
	tipc_subscrp_put(sub);
P
Per Liden 已提交
139 140
}

141
static void tipc_subscrp_timeout(unsigned long data)
P
Per Liden 已提交
142
{
143
	struct tipc_subscription *sub = (struct tipc_subscription *)data;
144 145 146 147 148
	struct tipc_subscriber *subscriber = sub->subscriber;

	spin_lock_bh(&subscriber->lock);
	tipc_nametbl_unsubscribe(sub);
	spin_unlock_bh(&subscriber->lock);
149 150

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

154
	tipc_subscrp_put(sub);
P
Per Liden 已提交
155 156
}

157
static void tipc_subscrb_kref_release(struct kref *kref)
158
{
159
	kfree(container_of(kref,struct tipc_subscriber, kref));
160 161 162 163 164 165 166 167 168 169
}

static void tipc_subscrb_put(struct tipc_subscriber *subscriber)
{
	kref_put(&subscriber->kref, tipc_subscrb_kref_release);
}

static void tipc_subscrb_get(struct tipc_subscriber *subscriber)
{
	kref_get(&subscriber->kref);
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static void tipc_subscrp_kref_release(struct kref *kref)
{
	struct tipc_subscription *sub = container_of(kref,
						     struct tipc_subscription,
						     kref);
	struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
	struct tipc_subscriber *subscriber = sub->subscriber;

	spin_lock_bh(&subscriber->lock);
	list_del(&sub->subscrp_list);
	atomic_dec(&tn->subscription_count);
	spin_unlock_bh(&subscriber->lock);
	kfree(sub);
	tipc_subscrb_put(subscriber);
}

static void tipc_subscrp_put(struct tipc_subscription *subscription)
{
	kref_put(&subscription->kref, tipc_subscrp_kref_release);
}

static void tipc_subscrp_get(struct tipc_subscription *subscription)
{
	kref_get(&subscription->kref);
}

/* tipc_subscrb_subscrp_delete - delete a specific subscription or all
 * subscriptions for a given subscriber.
 */
static void tipc_subscrb_subscrp_delete(struct tipc_subscriber *subscriber,
					struct tipc_subscr *s)
{
	struct list_head *subscription_list = &subscriber->subscrp_list;
	struct tipc_subscription *sub, *temp;

	spin_lock_bh(&subscriber->lock);
	list_for_each_entry_safe(sub, temp, subscription_list,  subscrp_list) {
		if (s && memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr)))
			continue;

212
		tipc_nametbl_unsubscribe(sub);
213 214 215 216 217 218 219 220 221 222 223 224
		tipc_subscrp_get(sub);
		spin_unlock_bh(&subscriber->lock);
		tipc_subscrp_delete(sub);
		tipc_subscrp_put(sub);
		spin_lock_bh(&subscriber->lock);

		if (s)
			break;
	}
	spin_unlock_bh(&subscriber->lock);
}

225 226 227 228 229 230 231 232 233 234
static struct tipc_subscriber *tipc_subscrb_create(int conid)
{
	struct tipc_subscriber *subscriber;

	subscriber = kzalloc(sizeof(*subscriber), GFP_ATOMIC);
	if (!subscriber) {
		pr_warn("Subscriber rejected, no memory\n");
		return NULL;
	}
	INIT_LIST_HEAD(&subscriber->subscrp_list);
235
	kref_init(&subscriber->kref);
236 237 238 239 240 241
	subscriber->conid = conid;
	spin_lock_init(&subscriber->lock);

	return subscriber;
}

242
static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
243
{
244
	tipc_subscrb_subscrp_delete(subscriber, NULL);
245 246 247 248 249
	tipc_subscrb_put(subscriber);
}

static void tipc_subscrp_delete(struct tipc_subscription *sub)
{
250
	u32 timeout = htohl(sub->evt.s.timeout, sub->swap);
251

252 253
	if (timeout == TIPC_WAIT_FOREVER || del_timer(&sub->timer))
		tipc_subscrp_put(sub);
P
Per Liden 已提交
254 255
}

256 257
static void tipc_subscrp_cancel(struct tipc_subscr *s,
				struct tipc_subscriber *subscriber)
258
{
259
	tipc_subscrb_subscrp_delete(subscriber, s);
260 261
}

262 263
static struct tipc_subscription *tipc_subscrp_create(struct net *net,
						     struct tipc_subscr *s,
264
						     int swap)
265 266
{
	struct tipc_net *tn = net_generic(net, tipc_net_id);
267
	struct tipc_subscription *sub;
268
	u32 filter = htohl(s->filter, swap);
269

P
Per Liden 已提交
270
	/* Refuse subscription if global limit exceeded */
271
	if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
272
		pr_warn("Subscription rejected, limit reached (%u)\n",
273
			TIPC_MAX_SUBSCRIPTIONS);
274
		return NULL;
P
Per Liden 已提交
275 276 277
	}

	/* Allocate subscription object */
278
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
279
	if (!sub) {
280
		pr_warn("Subscription rejected, no memory\n");
281
		return NULL;
P
Per Liden 已提交
282 283 284
	}

	/* Initialize subscription object */
285
	sub->net = net;
286
	if (((filter & TIPC_SUB_PORTS) && (filter & TIPC_SUB_SERVICE)) ||
287
	    (htohl(s->seq.lower, swap) > htohl(s->seq.upper, swap))) {
288
		pr_warn("Subscription rejected, illegal request\n");
P
Per Liden 已提交
289
		kfree(sub);
290
		return NULL;
P
Per Liden 已提交
291
	}
292

293
	sub->swap = swap;
294
	memcpy(&sub->evt.s, s, sizeof(*s));
295
	atomic_inc(&tn->subscription_count);
296
	kref_init(&sub->kref);
297 298 299 300
	return sub;
}

static void tipc_subscrp_subscribe(struct net *net, struct tipc_subscr *s,
301
				   struct tipc_subscriber *subscriber, int swap)
302 303 304 305 306
{
	struct tipc_net *tn = net_generic(net, tipc_net_id);
	struct tipc_subscription *sub = NULL;
	u32 timeout;

307
	sub = tipc_subscrp_create(net, s, swap);
308 309 310 311 312
	if (!sub)
		return tipc_conn_terminate(tn->topsrv, subscriber->conid);

	spin_lock_bh(&subscriber->lock);
	list_add(&sub->subscrp_list, &subscriber->subscrp_list);
313 314
	sub->subscriber = subscriber;
	tipc_nametbl_subscribe(sub);
315
	tipc_subscrb_get(subscriber);
316 317
	spin_unlock_bh(&subscriber->lock);

318
	setup_timer(&sub->timer, tipc_subscrp_timeout, (unsigned long)sub);
319
	timeout = htohl(sub->evt.s.timeout, swap);
320

321 322
	if (timeout != TIPC_WAIT_FOREVER)
		mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
P
Per Liden 已提交
323 324
}

325
/* Handle one termination request for the subscriber */
326
static void tipc_subscrb_release_cb(int conid, void *usr_data)
P
Per Liden 已提交
327
{
328
	tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
P
Per Liden 已提交
329 330
}

331
/* Handle one request to create a new subscription for the subscriber */
332 333 334
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 已提交
335
{
336 337 338 339 340
	struct tipc_subscriber *subscriber = usr_data;
	struct tipc_subscr *s = (struct tipc_subscr *)buf;
	int swap;

	/* Determine subscriber's endianness */
341 342
	swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE |
			      TIPC_SUB_CANCEL));
343 344 345 346 347 348 349

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

350
	tipc_subscrp_subscribe(net, s, subscriber, swap);
P
Per Liden 已提交
351 352
}

353
/* Handle one request to establish a new subscriber */
354
static void *tipc_subscrb_connect_cb(int conid)
P
Per Liden 已提交
355
{
356
	return (void *)tipc_subscrb_create(conid);
P
Per Liden 已提交
357 358
}

359
int tipc_topsrv_start(struct net *net)
P
Per Liden 已提交
360
{
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	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);
386 387
	topsrv->tipc_conn_recvmsg	= tipc_subscrb_rcv_cb;
	topsrv->tipc_conn_new		= tipc_subscrb_connect_cb;
388
	topsrv->tipc_conn_release	= tipc_subscrb_release_cb;
389 390 391 392 393 394

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

	return tipc_server_start(topsrv);
P
Per Liden 已提交
395 396
}

397
void tipc_topsrv_stop(struct net *net)
P
Per Liden 已提交
398
{
399 400 401 402 403 404
	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 已提交
405
}