subscr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * Copyright (c) 2020-2021, Red Hat Inc
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "core.h"
  38. #include "name_table.h"
  39. #include "subscr.h"
  40. static void tipc_sub_send_event(struct tipc_subscription *sub,
  41. struct publication *p,
  42. u32 event)
  43. {
  44. struct tipc_subscr *s = &sub->evt.s;
  45. struct tipc_event *evt = &sub->evt;
  46. if (sub->inactive)
  47. return;
  48. tipc_evt_write(evt, event, event);
  49. if (p) {
  50. tipc_evt_write(evt, found_lower, p->sr.lower);
  51. tipc_evt_write(evt, found_upper, p->sr.upper);
  52. tipc_evt_write(evt, port.ref, p->sk.ref);
  53. tipc_evt_write(evt, port.node, p->sk.node);
  54. } else {
  55. tipc_evt_write(evt, found_lower, s->seq.lower);
  56. tipc_evt_write(evt, found_upper, s->seq.upper);
  57. tipc_evt_write(evt, port.ref, 0);
  58. tipc_evt_write(evt, port.node, 0);
  59. }
  60. tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
  61. }
  62. /**
  63. * tipc_sub_check_overlap - test for subscription overlap with the given values
  64. * @subscribed: the service range subscribed for
  65. * @found: the service range we are checking for match
  66. *
  67. * Returns true if there is overlap, otherwise false.
  68. */
  69. static bool tipc_sub_check_overlap(struct tipc_service_range *subscribed,
  70. struct tipc_service_range *found)
  71. {
  72. u32 found_lower = found->lower;
  73. u32 found_upper = found->upper;
  74. if (found_lower < subscribed->lower)
  75. found_lower = subscribed->lower;
  76. if (found_upper > subscribed->upper)
  77. found_upper = subscribed->upper;
  78. return found_lower <= found_upper;
  79. }
  80. void tipc_sub_report_overlap(struct tipc_subscription *sub,
  81. struct publication *p,
  82. u32 event, bool must)
  83. {
  84. struct tipc_service_range *sr = &sub->s.seq;
  85. u32 filter = sub->s.filter;
  86. if (!tipc_sub_check_overlap(sr, &p->sr))
  87. return;
  88. if (!must && !(filter & TIPC_SUB_PORTS))
  89. return;
  90. if (filter & TIPC_SUB_CLUSTER_SCOPE && p->scope == TIPC_NODE_SCOPE)
  91. return;
  92. if (filter & TIPC_SUB_NODE_SCOPE && p->scope != TIPC_NODE_SCOPE)
  93. return;
  94. spin_lock(&sub->lock);
  95. tipc_sub_send_event(sub, p, event);
  96. spin_unlock(&sub->lock);
  97. }
  98. static void tipc_sub_timeout(struct timer_list *t)
  99. {
  100. struct tipc_subscription *sub = from_timer(sub, t, timer);
  101. spin_lock(&sub->lock);
  102. tipc_sub_send_event(sub, NULL, TIPC_SUBSCR_TIMEOUT);
  103. sub->inactive = true;
  104. spin_unlock(&sub->lock);
  105. }
  106. static void tipc_sub_kref_release(struct kref *kref)
  107. {
  108. kfree(container_of(kref, struct tipc_subscription, kref));
  109. }
  110. void tipc_sub_put(struct tipc_subscription *subscription)
  111. {
  112. kref_put(&subscription->kref, tipc_sub_kref_release);
  113. }
  114. void tipc_sub_get(struct tipc_subscription *subscription)
  115. {
  116. kref_get(&subscription->kref);
  117. }
  118. struct tipc_subscription *tipc_sub_subscribe(struct net *net,
  119. struct tipc_subscr *s,
  120. int conid)
  121. {
  122. u32 lower = tipc_sub_read(s, seq.lower);
  123. u32 upper = tipc_sub_read(s, seq.upper);
  124. u32 filter = tipc_sub_read(s, filter);
  125. struct tipc_subscription *sub;
  126. u32 timeout;
  127. if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
  128. lower > upper) {
  129. pr_warn("Subscription rejected, illegal request\n");
  130. return NULL;
  131. }
  132. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  133. if (!sub) {
  134. pr_warn("Subscription rejected, no memory\n");
  135. return NULL;
  136. }
  137. INIT_LIST_HEAD(&sub->service_list);
  138. INIT_LIST_HEAD(&sub->sub_list);
  139. sub->net = net;
  140. sub->conid = conid;
  141. sub->inactive = false;
  142. memcpy(&sub->evt.s, s, sizeof(*s));
  143. sub->s.seq.type = tipc_sub_read(s, seq.type);
  144. sub->s.seq.lower = lower;
  145. sub->s.seq.upper = upper;
  146. sub->s.filter = filter;
  147. sub->s.timeout = tipc_sub_read(s, timeout);
  148. memcpy(sub->s.usr_handle, s->usr_handle, 8);
  149. spin_lock_init(&sub->lock);
  150. kref_init(&sub->kref);
  151. if (!tipc_nametbl_subscribe(sub)) {
  152. kfree(sub);
  153. return NULL;
  154. }
  155. timer_setup(&sub->timer, tipc_sub_timeout, 0);
  156. timeout = tipc_sub_read(&sub->evt.s, timeout);
  157. if (timeout != TIPC_WAIT_FOREVER)
  158. mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
  159. return sub;
  160. }
  161. void tipc_sub_unsubscribe(struct tipc_subscription *sub)
  162. {
  163. tipc_nametbl_unsubscribe(sub);
  164. if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
  165. del_timer_sync(&sub->timer);
  166. list_del(&sub->sub_list);
  167. tipc_sub_put(sub);
  168. }