tipc: Fix bug in topology server byte swapping routine
[linux-2.6-block.git] / net / tipc / subscr.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/subscr.c: TIPC subscription service
c4307285 3 *
593a5f22 4 * Copyright (c) 2000-2006, Ericsson AB
b97bf3fd 5 * Copyright (c) 2005, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "subscr.h"
40#include "name_table.h"
41#include "ref.h"
42
43/**
44 * struct subscriber - TIPC network topology subscriber
45 * @ref: object reference to subscriber object itself
46 * @lock: pointer to spinlock controlling access to subscriber object
47 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
48 * @subscription_list: list of subscription objects for this subscriber
49 * @port_ref: object reference to port used to communicate with subscriber
b97bf3fd 50 */
c4307285 51
b97bf3fd
PL
52struct subscriber {
53 u32 ref;
c4307285 54 spinlock_t *lock;
b97bf3fd
PL
55 struct list_head subscriber_list;
56 struct list_head subscription_list;
57 u32 port_ref;
b97bf3fd
PL
58};
59
60/**
61 * struct top_srv - TIPC network topology subscription service
62 * @user_ref: TIPC userid of subscription service
63 * @setup_port: reference to TIPC port that handles subscription requests
64 * @subscription_count: number of active subscriptions (not subscribers!)
65 * @subscriber_list: list of ports subscribing to service
66 * @lock: spinlock govering access to subscriber list
67 */
68
69struct top_srv {
70 u32 user_ref;
71 u32 setup_port;
72 atomic_t subscription_count;
73 struct list_head subscriber_list;
74 spinlock_t lock;
75};
76
77static struct top_srv topsrv = { 0 };
78
79/**
80 * htohl - convert value to endianness used by destination
81 * @in: value to convert
82 * @swap: non-zero if endianness must be reversed
c4307285 83 *
b97bf3fd
PL
84 * Returns converted value
85 */
86
05790c64 87static u32 htohl(u32 in, int swap)
b97bf3fd 88{
fc5ad582 89 return swap ? (u32)___constant_swab32(in) : in;
b97bf3fd
PL
90}
91
92/**
93 * subscr_send_event - send a message containing a tipc_event to the subscriber
94 */
95
c4307285
YH
96static void subscr_send_event(struct subscription *sub,
97 u32 found_lower,
b97bf3fd 98 u32 found_upper,
c4307285
YH
99 u32 event,
100 u32 port_ref,
b97bf3fd
PL
101 u32 node)
102{
103 struct iovec msg_sect;
104
105 msg_sect.iov_base = (void *)&sub->evt;
106 msg_sect.iov_len = sizeof(struct tipc_event);
107
8e9501f5
AS
108 sub->evt.event = htohl(event, sub->swap);
109 sub->evt.found_lower = htohl(found_lower, sub->swap);
110 sub->evt.found_upper = htohl(found_upper, sub->swap);
111 sub->evt.port.ref = htohl(port_ref, sub->swap);
112 sub->evt.port.node = htohl(node, sub->swap);
b97bf3fd
PL
113 tipc_send(sub->owner->port_ref, 1, &msg_sect);
114}
115
116/**
4323add6 117 * tipc_subscr_overlap - test for subscription overlap with the given values
b97bf3fd
PL
118 *
119 * Returns 1 if there is overlap, otherwise 0.
120 */
121
c4307285
YH
122int tipc_subscr_overlap(struct subscription *sub,
123 u32 found_lower,
4323add6 124 u32 found_upper)
b97bf3fd
PL
125
126{
127 if (found_lower < sub->seq.lower)
128 found_lower = sub->seq.lower;
129 if (found_upper > sub->seq.upper)
130 found_upper = sub->seq.upper;
131 if (found_lower > found_upper)
132 return 0;
133 return 1;
134}
135
136/**
4323add6 137 * tipc_subscr_report_overlap - issue event if there is subscription overlap
c4307285 138 *
b97bf3fd
PL
139 * Protected by nameseq.lock in name_table.c
140 */
141
c4307285
YH
142void tipc_subscr_report_overlap(struct subscription *sub,
143 u32 found_lower,
4323add6 144 u32 found_upper,
c4307285
YH
145 u32 event,
146 u32 port_ref,
4323add6
PL
147 u32 node,
148 int must)
b97bf3fd
PL
149{
150 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
151 sub->seq.upper, found_lower, found_upper);
4323add6 152 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
b97bf3fd 153 return;
eb409460 154 if (!must && !(sub->filter & TIPC_SUB_PORTS))
b97bf3fd 155 return;
e15f8804
AS
156
157 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
b97bf3fd
PL
158}
159
160/**
161 * subscr_timeout - subscription timeout has occurred
162 */
163
164static void subscr_timeout(struct subscription *sub)
165{
166 struct subscriber *subscriber;
167 u32 subscriber_ref;
168
169 /* Validate subscriber reference (in case subscriber is terminating) */
170
171 subscriber_ref = sub->owner->ref;
4323add6 172 subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
b97bf3fd
PL
173 if (subscriber == NULL)
174 return;
175
eb409460
LC
176 /* Validate timeout (in case subscription is being cancelled) */
177
178 if (sub->timeout == TIPC_WAIT_FOREVER) {
179 tipc_ref_unlock(subscriber_ref);
180 return;
181 }
182
b97bf3fd
PL
183 /* Unlink subscription from name table */
184
4323add6 185 tipc_nametbl_unsubscribe(sub);
b97bf3fd
PL
186
187 /* Notify subscriber of timeout, then unlink subscription */
188
c4307285
YH
189 subscr_send_event(sub,
190 sub->evt.s.seq.lower,
b97bf3fd 191 sub->evt.s.seq.upper,
c4307285
YH
192 TIPC_SUBSCR_TIMEOUT,
193 0,
b97bf3fd
PL
194 0);
195 list_del(&sub->subscription_list);
196
197 /* Now destroy subscription */
198
4323add6 199 tipc_ref_unlock(subscriber_ref);
b97bf3fd
PL
200 k_term_timer(&sub->timer);
201 kfree(sub);
202 atomic_dec(&topsrv.subscription_count);
203}
204
eb409460
LC
205/**
206 * subscr_del - delete a subscription within a subscription list
207 *
208 * Called with subscriber locked.
209 */
210
211static void subscr_del(struct subscription *sub)
212{
213 tipc_nametbl_unsubscribe(sub);
214 list_del(&sub->subscription_list);
215 kfree(sub);
216 atomic_dec(&topsrv.subscription_count);
217}
218
b97bf3fd
PL
219/**
220 * subscr_terminate - terminate communication with a subscriber
c4307285 221 *
b97bf3fd 222 * Called with subscriber locked. Routine must temporarily release this lock
c4307285 223 * to enable subscription timeout routine(s) to finish without deadlocking;
b97bf3fd 224 * the lock is then reclaimed to allow caller to release it upon return.
c4307285 225 * (This should work even in the unlikely event some other thread creates
b97bf3fd
PL
226 * a new object reference in the interim that uses this lock; this routine will
227 * simply wait for it to be released, then claim it.)
228 */
229
230static void subscr_terminate(struct subscriber *subscriber)
231{
232 struct subscription *sub;
233 struct subscription *sub_temp;
234
235 /* Invalidate subscriber reference */
236
4323add6 237 tipc_ref_discard(subscriber->ref);
b97bf3fd
PL
238 spin_unlock_bh(subscriber->lock);
239
240 /* Destroy any existing subscriptions for subscriber */
c4307285 241
b97bf3fd
PL
242 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
243 subscription_list) {
244 if (sub->timeout != TIPC_WAIT_FOREVER) {
245 k_cancel_timer(&sub->timer);
246 k_term_timer(&sub->timer);
247 }
eb409460 248 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
b97bf3fd 249 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
eb409460 250 subscr_del(sub);
b97bf3fd
PL
251 }
252
253 /* Sever connection to subscriber */
254
255 tipc_shutdown(subscriber->port_ref);
256 tipc_deleteport(subscriber->port_ref);
257
258 /* Remove subscriber from topology server's subscriber list */
259
260 spin_lock_bh(&topsrv.lock);
261 list_del(&subscriber->subscriber_list);
262 spin_unlock_bh(&topsrv.lock);
263
264 /* Now destroy subscriber */
265
266 spin_lock_bh(subscriber->lock);
267 kfree(subscriber);
268}
269
eb409460
LC
270/**
271 * subscr_cancel - handle subscription cancellation request
272 *
273 * Called with subscriber locked. Routine must temporarily release this lock
274 * to enable the subscription timeout routine to finish without deadlocking;
275 * the lock is then reclaimed to allow caller to release it upon return.
276 *
277 * Note that fields of 's' use subscriber's endianness!
278 */
279
280static void subscr_cancel(struct tipc_subscr *s,
281 struct subscriber *subscriber)
282{
283 struct subscription *sub;
284 struct subscription *sub_temp;
285 int found = 0;
286
287 /* Find first matching subscription, exit if not found */
288
289 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
290 subscription_list) {
291 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
292 found = 1;
293 break;
294 }
295 }
296 if (!found)
297 return;
298
299 /* Cancel subscription timer (if used), then delete subscription */
300
301 if (sub->timeout != TIPC_WAIT_FOREVER) {
302 sub->timeout = TIPC_WAIT_FOREVER;
303 spin_unlock_bh(subscriber->lock);
304 k_cancel_timer(&sub->timer);
305 k_term_timer(&sub->timer);
306 spin_lock_bh(subscriber->lock);
307 }
308 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
309 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
310 subscr_del(sub);
311}
312
b97bf3fd
PL
313/**
314 * subscr_subscribe - create subscription for subscriber
c4307285 315 *
b97bf3fd
PL
316 * Called with subscriber locked
317 */
318
319static void subscr_subscribe(struct tipc_subscr *s,
320 struct subscriber *subscriber)
321{
322 struct subscription *sub;
8e9501f5 323 int swap;
b97bf3fd 324
8e9501f5 325 /* Determine subscriber's endianness */
eb409460 326
8e9501f5 327 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
eb409460
LC
328
329 /* Detect & process a subscription cancellation request */
330
8e9501f5
AS
331 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
332 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
eb409460
LC
333 subscr_cancel(s, subscriber);
334 return;
335 }
336
b97bf3fd
PL
337 /* Refuse subscription if global limit exceeded */
338
339 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
a10bd924
AS
340 warn("Subscription rejected, subscription limit reached (%u)\n",
341 tipc_max_subscriptions);
b97bf3fd
PL
342 subscr_terminate(subscriber);
343 return;
344 }
345
346 /* Allocate subscription object */
347
2710b57f 348 sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
a10bd924
AS
349 if (!sub) {
350 warn("Subscription rejected, no memory\n");
b97bf3fd
PL
351 subscr_terminate(subscriber);
352 return;
353 }
354
b97bf3fd
PL
355 /* Initialize subscription object */
356
8e9501f5
AS
357 sub->seq.type = htohl(s->seq.type, swap);
358 sub->seq.lower = htohl(s->seq.lower, swap);
359 sub->seq.upper = htohl(s->seq.upper, swap);
360 sub->timeout = htohl(s->timeout, swap);
361 sub->filter = htohl(s->filter, swap);
eb409460
LC
362 if ((!(sub->filter & TIPC_SUB_PORTS)
363 == !(sub->filter & TIPC_SUB_SERVICE))
b97bf3fd 364 || (sub->seq.lower > sub->seq.upper)) {
a10bd924 365 warn("Subscription rejected, illegal request\n");
b97bf3fd
PL
366 kfree(sub);
367 subscr_terminate(subscriber);
368 return;
369 }
e15f8804 370 sub->event_cb = subscr_send_event;
b97bf3fd
PL
371 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
372 INIT_LIST_HEAD(&sub->subscription_list);
373 INIT_LIST_HEAD(&sub->nameseq_list);
374 list_add(&sub->subscription_list, &subscriber->subscription_list);
8e9501f5 375 sub->swap = swap;
b97bf3fd
PL
376 atomic_inc(&topsrv.subscription_count);
377 if (sub->timeout != TIPC_WAIT_FOREVER) {
378 k_init_timer(&sub->timer,
379 (Handler)subscr_timeout, (unsigned long)sub);
380 k_start_timer(&sub->timer, sub->timeout);
381 }
382 sub->owner = subscriber;
4323add6 383 tipc_nametbl_subscribe(sub);
b97bf3fd
PL
384}
385
386/**
387 * subscr_conn_shutdown_event - handle termination request from subscriber
388 */
389
390static void subscr_conn_shutdown_event(void *usr_handle,
391 u32 portref,
392 struct sk_buff **buf,
393 unsigned char const *data,
394 unsigned int size,
395 int reason)
396{
880b005f 397 struct subscriber *subscriber;
b97bf3fd
PL
398 spinlock_t *subscriber_lock;
399
4323add6 400 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
b97bf3fd
PL
401 if (subscriber == NULL)
402 return;
403
404 subscriber_lock = subscriber->lock;
405 subscr_terminate(subscriber);
406 spin_unlock_bh(subscriber_lock);
407}
408
409/**
410 * subscr_conn_msg_event - handle new subscription request from subscriber
411 */
412
413static void subscr_conn_msg_event(void *usr_handle,
414 u32 port_ref,
415 struct sk_buff **buf,
416 const unchar *data,
417 u32 size)
418{
880b005f 419 struct subscriber *subscriber;
b97bf3fd
PL
420 spinlock_t *subscriber_lock;
421
4323add6 422 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
b97bf3fd
PL
423 if (subscriber == NULL)
424 return;
425
426 subscriber_lock = subscriber->lock;
427 if (size != sizeof(struct tipc_subscr))
428 subscr_terminate(subscriber);
429 else
430 subscr_subscribe((struct tipc_subscr *)data, subscriber);
c4307285 431
b97bf3fd
PL
432 spin_unlock_bh(subscriber_lock);
433}
434
435/**
436 * subscr_named_msg_event - handle request to establish a new subscriber
437 */
438
439static void subscr_named_msg_event(void *usr_handle,
440 u32 port_ref,
441 struct sk_buff **buf,
442 const unchar *data,
443 u32 size,
c4307285 444 u32 importance,
b97bf3fd
PL
445 struct tipc_portid const *orig,
446 struct tipc_name_seq const *dest)
447{
448 struct subscriber *subscriber;
1fc54d8f 449 struct iovec msg_sect = {NULL, 0};
b97bf3fd
PL
450 spinlock_t *subscriber_lock;
451
452 dbg("subscr_named_msg_event: orig = %x own = %x,\n",
453 orig->node, tipc_own_addr);
454 if (size && (size != sizeof(struct tipc_subscr))) {
a10bd924 455 warn("Subscriber rejected, invalid subscription size\n");
b97bf3fd
PL
456 return;
457 }
458
459 /* Create subscriber object */
460
0da974f4 461 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
b97bf3fd 462 if (subscriber == NULL) {
a10bd924 463 warn("Subscriber rejected, no memory\n");
b97bf3fd
PL
464 return;
465 }
b97bf3fd
PL
466 INIT_LIST_HEAD(&subscriber->subscription_list);
467 INIT_LIST_HEAD(&subscriber->subscriber_list);
4323add6 468 subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
b97bf3fd 469 if (subscriber->ref == 0) {
a10bd924 470 warn("Subscriber rejected, reference table exhausted\n");
b97bf3fd
PL
471 kfree(subscriber);
472 return;
473 }
7ef43eba 474 spin_unlock_bh(subscriber->lock);
b97bf3fd
PL
475
476 /* Establish a connection to subscriber */
477
478 tipc_createport(topsrv.user_ref,
880b005f 479 (void *)(unsigned long)subscriber->ref,
b97bf3fd 480 importance,
1fc54d8f
SR
481 NULL,
482 NULL,
b97bf3fd 483 subscr_conn_shutdown_event,
1fc54d8f
SR
484 NULL,
485 NULL,
b97bf3fd 486 subscr_conn_msg_event,
1fc54d8f 487 NULL,
b97bf3fd
PL
488 &subscriber->port_ref);
489 if (subscriber->port_ref == 0) {
a10bd924 490 warn("Subscriber rejected, unable to create port\n");
4323add6 491 tipc_ref_discard(subscriber->ref);
b97bf3fd
PL
492 kfree(subscriber);
493 return;
494 }
495 tipc_connect2port(subscriber->port_ref, orig);
496
497
498 /* Add subscriber to topology server's subscriber list */
499
4323add6 500 tipc_ref_lock(subscriber->ref);
b97bf3fd
PL
501 spin_lock_bh(&topsrv.lock);
502 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
503 spin_unlock_bh(&topsrv.lock);
504
505 /*
506 * Subscribe now if message contains a subscription,
507 * otherwise send an empty response to complete connection handshaking
508 */
509
510 subscriber_lock = subscriber->lock;
511 if (size)
512 subscr_subscribe((struct tipc_subscr *)data, subscriber);
513 else
514 tipc_send(subscriber->port_ref, 1, &msg_sect);
515
516 spin_unlock_bh(subscriber_lock);
517}
518
4323add6 519int tipc_subscr_start(void)
b97bf3fd
PL
520{
521 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
522 int res = -1;
523
524 memset(&topsrv, 0, sizeof (topsrv));
34af946a 525 spin_lock_init(&topsrv.lock);
b97bf3fd
PL
526 INIT_LIST_HEAD(&topsrv.subscriber_list);
527
528 spin_lock_bh(&topsrv.lock);
1fc54d8f 529 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
b97bf3fd
PL
530 if (res) {
531 spin_unlock_bh(&topsrv.lock);
532 return res;
533 }
534
c4307285
YH
535 res = tipc_createport(topsrv.user_ref,
536 NULL,
537 TIPC_CRITICAL_IMPORTANCE,
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 subscr_named_msg_event,
543 NULL,
544 NULL,
545 &topsrv.setup_port);
546 if (res)
b97bf3fd
PL
547 goto failed;
548
c4307285
YH
549 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
550 if (res)
b97bf3fd
PL
551 goto failed;
552
553 spin_unlock_bh(&topsrv.lock);
554 return 0;
555
556failed:
d0a14a9d 557 err("Failed to create subscription service\n");
b97bf3fd
PL
558 tipc_detach(topsrv.user_ref);
559 topsrv.user_ref = 0;
560 spin_unlock_bh(&topsrv.lock);
561 return res;
562}
563
4323add6 564void tipc_subscr_stop(void)
b97bf3fd
PL
565{
566 struct subscriber *subscriber;
567 struct subscriber *subscriber_temp;
568 spinlock_t *subscriber_lock;
569
570 if (topsrv.user_ref) {
571 tipc_deleteport(topsrv.setup_port);
c4307285 572 list_for_each_entry_safe(subscriber, subscriber_temp,
b97bf3fd
PL
573 &topsrv.subscriber_list,
574 subscriber_list) {
4323add6 575 tipc_ref_lock(subscriber->ref);
b97bf3fd
PL
576 subscriber_lock = subscriber->lock;
577 subscr_terminate(subscriber);
578 spin_unlock_bh(subscriber_lock);
579 }
580 tipc_detach(topsrv.user_ref);
581 topsrv.user_ref = 0;
582 }
583}
584
585
586int tipc_ispublished(struct tipc_name const *name)
587{
588 u32 domain = 0;
589
4323add6 590 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
b97bf3fd
PL
591}
592