subscr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * net/tipc/subscr.c: TIPC network topology service
  3. *
  4. * Copyright (c) 2000-2017, Ericsson AB
  5. * Copyright (c) 2005-2007, 2010-2013, 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. #include "core.h"
  37. #include "name_table.h"
  38. #include "subscr.h"
  39. static void tipc_sub_send_event(struct tipc_subscription *sub,
  40. u32 found_lower, u32 found_upper,
  41. u32 event, u32 port, u32 node)
  42. {
  43. struct tipc_event *evt = &sub->evt;
  44. if (sub->inactive)
  45. return;
  46. tipc_evt_write(evt, event, event);
  47. tipc_evt_write(evt, found_lower, found_lower);
  48. tipc_evt_write(evt, found_upper, found_upper);
  49. tipc_evt_write(evt, port.ref, port);
  50. tipc_evt_write(evt, port.node, node);
  51. tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
  52. }
  53. /**
  54. * tipc_sub_check_overlap - test for subscription overlap with the
  55. * given values
  56. *
  57. * Returns 1 if there is overlap, otherwise 0.
  58. */
  59. int tipc_sub_check_overlap(struct tipc_name_seq *seq, u32 found_lower,
  60. u32 found_upper)
  61. {
  62. if (found_lower < seq->lower)
  63. found_lower = seq->lower;
  64. if (found_upper > seq->upper)
  65. found_upper = seq->upper;
  66. if (found_lower > found_upper)
  67. return 0;
  68. return 1;
  69. }
  70. void tipc_sub_report_overlap(struct tipc_subscription *sub,
  71. u32 found_lower, u32 found_upper,
  72. u32 event, u32 port, u32 node,
  73. u32 scope, int must)
  74. {
  75. struct tipc_subscr *s = &sub->evt.s;
  76. u32 filter = tipc_sub_read(s, filter);
  77. struct tipc_name_seq seq;
  78. seq.type = tipc_sub_read(s, seq.type);
  79. seq.lower = tipc_sub_read(s, seq.lower);
  80. seq.upper = tipc_sub_read(s, seq.upper);
  81. if (!tipc_sub_check_overlap(&seq, found_lower, found_upper))
  82. return;
  83. if (!must && !(filter & TIPC_SUB_PORTS))
  84. return;
  85. if (filter & TIPC_SUB_CLUSTER_SCOPE && scope == TIPC_NODE_SCOPE)
  86. return;
  87. if (filter & TIPC_SUB_NODE_SCOPE && scope != TIPC_NODE_SCOPE)
  88. return;
  89. spin_lock(&sub->lock);
  90. tipc_sub_send_event(sub, found_lower, found_upper,
  91. event, port, node);
  92. spin_unlock(&sub->lock);
  93. }
  94. static void tipc_sub_timeout(struct timer_list *t)
  95. {
  96. struct tipc_subscription *sub = from_timer(sub, t, timer);
  97. struct tipc_subscr *s = &sub->evt.s;
  98. spin_lock(&sub->lock);
  99. tipc_sub_send_event(sub, s->seq.lower, s->seq.upper,
  100. TIPC_SUBSCR_TIMEOUT, 0, 0);
  101. sub->inactive = true;
  102. spin_unlock(&sub->lock);
  103. }
  104. static void tipc_sub_kref_release(struct kref *kref)
  105. {
  106. kfree(container_of(kref, struct tipc_subscription, kref));
  107. }
  108. void tipc_sub_put(struct tipc_subscription *subscription)
  109. {
  110. kref_put(&subscription->kref, tipc_sub_kref_release);
  111. }
  112. void tipc_sub_get(struct tipc_subscription *subscription)
  113. {
  114. kref_get(&subscription->kref);
  115. }
  116. struct tipc_subscription *tipc_sub_subscribe(struct net *net,
  117. struct tipc_subscr *s,
  118. int conid)
  119. {
  120. u32 filter = tipc_sub_read(s, filter);
  121. struct tipc_subscription *sub;
  122. u32 timeout;
  123. if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
  124. (tipc_sub_read(s, seq.lower) > tipc_sub_read(s, seq.upper))) {
  125. pr_warn("Subscription rejected, illegal request\n");
  126. return NULL;
  127. }
  128. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  129. if (!sub) {
  130. pr_warn("Subscription rejected, no memory\n");
  131. return NULL;
  132. }
  133. INIT_LIST_HEAD(&sub->service_list);
  134. INIT_LIST_HEAD(&sub->sub_list);
  135. sub->net = net;
  136. sub->conid = conid;
  137. sub->inactive = false;
  138. memcpy(&sub->evt.s, s, sizeof(*s));
  139. spin_lock_init(&sub->lock);
  140. kref_init(&sub->kref);
  141. if (!tipc_nametbl_subscribe(sub)) {
  142. kfree(sub);
  143. return NULL;
  144. }
  145. timer_setup(&sub->timer, tipc_sub_timeout, 0);
  146. timeout = tipc_sub_read(&sub->evt.s, timeout);
  147. if (timeout != TIPC_WAIT_FOREVER)
  148. mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
  149. return sub;
  150. }
  151. void tipc_sub_unsubscribe(struct tipc_subscription *sub)
  152. {
  153. tipc_nametbl_unsubscribe(sub);
  154. if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
  155. del_timer_sync(&sub->timer);
  156. list_del(&sub->sub_list);
  157. tipc_sub_put(sub);
  158. }