tipc: Fix bug in topology server byte swapping routine
[deliverable/linux.git] / net / tipc / subscr.c
1 /*
2 * net/tipc/subscr.c: TIPC subscription service
3 *
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, 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 "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
50 */
51
52 struct subscriber {
53 u32 ref;
54 spinlock_t *lock;
55 struct list_head subscriber_list;
56 struct list_head subscription_list;
57 u32 port_ref;
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
69 struct 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
77 static 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
83 *
84 * Returns converted value
85 */
86
87 static u32 htohl(u32 in, int swap)
88 {
89 return swap ? (u32)___constant_swab32(in) : in;
90 }
91
92 /**
93 * subscr_send_event - send a message containing a tipc_event to the subscriber
94 */
95
96 static void subscr_send_event(struct subscription *sub,
97 u32 found_lower,
98 u32 found_upper,
99 u32 event,
100 u32 port_ref,
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
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);
113 tipc_send(sub->owner->port_ref, 1, &msg_sect);
114 }
115
116 /**
117 * tipc_subscr_overlap - test for subscription overlap with the given values
118 *
119 * Returns 1 if there is overlap, otherwise 0.
120 */
121
122 int tipc_subscr_overlap(struct subscription *sub,
123 u32 found_lower,
124 u32 found_upper)
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 /**
137 * tipc_subscr_report_overlap - issue event if there is subscription overlap
138 *
139 * Protected by nameseq.lock in name_table.c
140 */
141
142 void tipc_subscr_report_overlap(struct subscription *sub,
143 u32 found_lower,
144 u32 found_upper,
145 u32 event,
146 u32 port_ref,
147 u32 node,
148 int must)
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);
152 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
153 return;
154 if (!must && !(sub->filter & TIPC_SUB_PORTS))
155 return;
156
157 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
158 }
159
160 /**
161 * subscr_timeout - subscription timeout has occurred
162 */
163
164 static 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;
172 subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
173 if (subscriber == NULL)
174 return;
175
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
183 /* Unlink subscription from name table */
184
185 tipc_nametbl_unsubscribe(sub);
186
187 /* Notify subscriber of timeout, then unlink subscription */
188
189 subscr_send_event(sub,
190 sub->evt.s.seq.lower,
191 sub->evt.s.seq.upper,
192 TIPC_SUBSCR_TIMEOUT,
193 0,
194 0);
195 list_del(&sub->subscription_list);
196
197 /* Now destroy subscription */
198
199 tipc_ref_unlock(subscriber_ref);
200 k_term_timer(&sub->timer);
201 kfree(sub);
202 atomic_dec(&topsrv.subscription_count);
203 }
204
205 /**
206 * subscr_del - delete a subscription within a subscription list
207 *
208 * Called with subscriber locked.
209 */
210
211 static 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
219 /**
220 * subscr_terminate - terminate communication with a subscriber
221 *
222 * Called with subscriber locked. Routine must temporarily release this lock
223 * to enable subscription timeout routine(s) to finish without deadlocking;
224 * the lock is then reclaimed to allow caller to release it upon return.
225 * (This should work even in the unlikely event some other thread creates
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
230 static void subscr_terminate(struct subscriber *subscriber)
231 {
232 struct subscription *sub;
233 struct subscription *sub_temp;
234
235 /* Invalidate subscriber reference */
236
237 tipc_ref_discard(subscriber->ref);
238 spin_unlock_bh(subscriber->lock);
239
240 /* Destroy any existing subscriptions for subscriber */
241
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 }
248 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
249 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
250 subscr_del(sub);
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
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
280 static 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
313 /**
314 * subscr_subscribe - create subscription for subscriber
315 *
316 * Called with subscriber locked
317 */
318
319 static void subscr_subscribe(struct tipc_subscr *s,
320 struct subscriber *subscriber)
321 {
322 struct subscription *sub;
323 int swap;
324
325 /* Determine subscriber's endianness */
326
327 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
328
329 /* Detect & process a subscription cancellation request */
330
331 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
332 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
333 subscr_cancel(s, subscriber);
334 return;
335 }
336
337 /* Refuse subscription if global limit exceeded */
338
339 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
340 warn("Subscription rejected, subscription limit reached (%u)\n",
341 tipc_max_subscriptions);
342 subscr_terminate(subscriber);
343 return;
344 }
345
346 /* Allocate subscription object */
347
348 sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
349 if (!sub) {
350 warn("Subscription rejected, no memory\n");
351 subscr_terminate(subscriber);
352 return;
353 }
354
355 /* Initialize subscription object */
356
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);
362 if ((!(sub->filter & TIPC_SUB_PORTS)
363 == !(sub->filter & TIPC_SUB_SERVICE))
364 || (sub->seq.lower > sub->seq.upper)) {
365 warn("Subscription rejected, illegal request\n");
366 kfree(sub);
367 subscr_terminate(subscriber);
368 return;
369 }
370 sub->event_cb = subscr_send_event;
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);
375 sub->swap = swap;
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;
383 tipc_nametbl_subscribe(sub);
384 }
385
386 /**
387 * subscr_conn_shutdown_event - handle termination request from subscriber
388 */
389
390 static 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 {
397 struct subscriber *subscriber;
398 spinlock_t *subscriber_lock;
399
400 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
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
413 static 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 {
419 struct subscriber *subscriber;
420 spinlock_t *subscriber_lock;
421
422 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
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);
431
432 spin_unlock_bh(subscriber_lock);
433 }
434
435 /**
436 * subscr_named_msg_event - handle request to establish a new subscriber
437 */
438
439 static void subscr_named_msg_event(void *usr_handle,
440 u32 port_ref,
441 struct sk_buff **buf,
442 const unchar *data,
443 u32 size,
444 u32 importance,
445 struct tipc_portid const *orig,
446 struct tipc_name_seq const *dest)
447 {
448 struct subscriber *subscriber;
449 struct iovec msg_sect = {NULL, 0};
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))) {
455 warn("Subscriber rejected, invalid subscription size\n");
456 return;
457 }
458
459 /* Create subscriber object */
460
461 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
462 if (subscriber == NULL) {
463 warn("Subscriber rejected, no memory\n");
464 return;
465 }
466 INIT_LIST_HEAD(&subscriber->subscription_list);
467 INIT_LIST_HEAD(&subscriber->subscriber_list);
468 subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
469 if (subscriber->ref == 0) {
470 warn("Subscriber rejected, reference table exhausted\n");
471 kfree(subscriber);
472 return;
473 }
474 spin_unlock_bh(subscriber->lock);
475
476 /* Establish a connection to subscriber */
477
478 tipc_createport(topsrv.user_ref,
479 (void *)(unsigned long)subscriber->ref,
480 importance,
481 NULL,
482 NULL,
483 subscr_conn_shutdown_event,
484 NULL,
485 NULL,
486 subscr_conn_msg_event,
487 NULL,
488 &subscriber->port_ref);
489 if (subscriber->port_ref == 0) {
490 warn("Subscriber rejected, unable to create port\n");
491 tipc_ref_discard(subscriber->ref);
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
500 tipc_ref_lock(subscriber->ref);
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
519 int tipc_subscr_start(void)
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));
525 spin_lock_init(&topsrv.lock);
526 INIT_LIST_HEAD(&topsrv.subscriber_list);
527
528 spin_lock_bh(&topsrv.lock);
529 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
530 if (res) {
531 spin_unlock_bh(&topsrv.lock);
532 return res;
533 }
534
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)
547 goto failed;
548
549 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
550 if (res)
551 goto failed;
552
553 spin_unlock_bh(&topsrv.lock);
554 return 0;
555
556 failed:
557 err("Failed to create subscription service\n");
558 tipc_detach(topsrv.user_ref);
559 topsrv.user_ref = 0;
560 spin_unlock_bh(&topsrv.lock);
561 return res;
562 }
563
564 void tipc_subscr_stop(void)
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);
572 list_for_each_entry_safe(subscriber, subscriber_temp,
573 &topsrv.subscriber_list,
574 subscriber_list) {
575 tipc_ref_lock(subscriber->ref);
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
586 int tipc_ispublished(struct tipc_name const *name)
587 {
588 u32 domain = 0;
589
590 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
591 }
592
This page took 0.054512 seconds and 6 git commands to generate.