subscr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /**
  40. * htohl - convert value to endianness used by destination
  41. * @in: value to convert
  42. * @swap: non-zero if endianness must be reversed
  43. *
  44. * Returns converted value
  45. */
  46. static u32 htohl(u32 in, int swap)
  47. {
  48. return swap ? swab32(in) : in;
  49. }
  50. static void tipc_subscrp_send_event(struct tipc_subscription *sub,
  51. u32 found_lower, u32 found_upper,
  52. u32 event, u32 port, u32 node)
  53. {
  54. struct tipc_event *evt = &sub->evt;
  55. bool swap = sub->swap;
  56. if (sub->inactive)
  57. return;
  58. evt->event = htohl(event, swap);
  59. evt->found_lower = htohl(found_lower, swap);
  60. evt->found_upper = htohl(found_upper, swap);
  61. evt->port.ref = htohl(port, swap);
  62. evt->port.node = htohl(node, swap);
  63. tipc_conn_queue_evt(sub->server, sub->conid, event, evt);
  64. }
  65. /**
  66. * tipc_subscrp_check_overlap - test for subscription overlap with the
  67. * given values
  68. *
  69. * Returns 1 if there is overlap, otherwise 0.
  70. */
  71. int tipc_subscrp_check_overlap(struct tipc_name_seq *seq, u32 found_lower,
  72. u32 found_upper)
  73. {
  74. if (found_lower < seq->lower)
  75. found_lower = seq->lower;
  76. if (found_upper > seq->upper)
  77. found_upper = seq->upper;
  78. if (found_lower > found_upper)
  79. return 0;
  80. return 1;
  81. }
  82. u32 tipc_subscrp_convert_seq_type(u32 type, int swap)
  83. {
  84. return htohl(type, swap);
  85. }
  86. void tipc_subscrp_convert_seq(struct tipc_name_seq *in, int swap,
  87. struct tipc_name_seq *out)
  88. {
  89. out->type = htohl(in->type, swap);
  90. out->lower = htohl(in->lower, swap);
  91. out->upper = htohl(in->upper, swap);
  92. }
  93. void tipc_subscrp_report_overlap(struct tipc_subscription *sub, u32 found_lower,
  94. u32 found_upper, u32 event, u32 port_ref,
  95. u32 node, u32 scope, int must)
  96. {
  97. u32 filter = htohl(sub->evt.s.filter, sub->swap);
  98. struct tipc_name_seq seq;
  99. tipc_subscrp_convert_seq(&sub->evt.s.seq, sub->swap, &seq);
  100. if (!tipc_subscrp_check_overlap(&seq, found_lower, found_upper))
  101. return;
  102. if (!must && !(filter & TIPC_SUB_PORTS))
  103. return;
  104. if (filter & TIPC_SUB_CLUSTER_SCOPE && scope == TIPC_NODE_SCOPE)
  105. return;
  106. if (filter & TIPC_SUB_NODE_SCOPE && scope != TIPC_NODE_SCOPE)
  107. return;
  108. spin_lock(&sub->lock);
  109. tipc_subscrp_send_event(sub, found_lower, found_upper,
  110. event, port_ref, node);
  111. spin_unlock(&sub->lock);
  112. }
  113. static void tipc_subscrp_timeout(struct timer_list *t)
  114. {
  115. struct tipc_subscription *sub = from_timer(sub, t, timer);
  116. struct tipc_subscr *s = &sub->evt.s;
  117. spin_lock(&sub->lock);
  118. tipc_subscrp_send_event(sub, s->seq.lower, s->seq.upper,
  119. TIPC_SUBSCR_TIMEOUT, 0, 0);
  120. sub->inactive = true;
  121. spin_unlock(&sub->lock);
  122. }
  123. static void tipc_subscrp_kref_release(struct kref *kref)
  124. {
  125. struct tipc_subscription *sub;
  126. struct tipc_net *tn;
  127. sub = container_of(kref, struct tipc_subscription, kref);
  128. tn = tipc_net(sub->server->net);
  129. atomic_dec(&tn->subscription_count);
  130. kfree(sub);
  131. }
  132. void tipc_subscrp_put(struct tipc_subscription *subscription)
  133. {
  134. kref_put(&subscription->kref, tipc_subscrp_kref_release);
  135. }
  136. void tipc_subscrp_get(struct tipc_subscription *subscription)
  137. {
  138. kref_get(&subscription->kref);
  139. }
  140. static struct tipc_subscription *tipc_subscrp_create(struct tipc_server *srv,
  141. struct tipc_subscr *s,
  142. int conid, bool swap)
  143. {
  144. struct tipc_net *tn = tipc_net(srv->net);
  145. struct tipc_subscription *sub;
  146. u32 filter = htohl(s->filter, swap);
  147. /* Refuse subscription if global limit exceeded */
  148. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
  149. pr_warn("Subscription rejected, limit reached (%u)\n",
  150. TIPC_MAX_SUBSCRIPTIONS);
  151. return NULL;
  152. }
  153. /* Allocate subscription object */
  154. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  155. if (!sub) {
  156. pr_warn("Subscription rejected, no memory\n");
  157. return NULL;
  158. }
  159. /* Initialize subscription object */
  160. sub->server = srv;
  161. sub->conid = conid;
  162. sub->inactive = false;
  163. if (((filter & TIPC_SUB_PORTS) && (filter & TIPC_SUB_SERVICE)) ||
  164. (htohl(s->seq.lower, swap) > htohl(s->seq.upper, swap))) {
  165. pr_warn("Subscription rejected, illegal request\n");
  166. kfree(sub);
  167. return NULL;
  168. }
  169. sub->swap = swap;
  170. memcpy(&sub->evt.s, s, sizeof(*s));
  171. spin_lock_init(&sub->lock);
  172. atomic_inc(&tn->subscription_count);
  173. kref_init(&sub->kref);
  174. return sub;
  175. }
  176. struct tipc_subscription *tipc_subscrp_subscribe(struct tipc_server *srv,
  177. struct tipc_subscr *s,
  178. int conid, bool swap,
  179. bool status)
  180. {
  181. struct tipc_subscription *sub = NULL;
  182. u32 timeout;
  183. sub = tipc_subscrp_create(srv, s, conid, swap);
  184. if (!sub)
  185. return NULL;
  186. tipc_nametbl_subscribe(sub, status);
  187. timer_setup(&sub->timer, tipc_subscrp_timeout, 0);
  188. timeout = htohl(sub->evt.s.timeout, swap);
  189. if (timeout != TIPC_WAIT_FOREVER)
  190. mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
  191. return sub;
  192. }
  193. void tipc_sub_delete(struct tipc_subscription *sub)
  194. {
  195. tipc_nametbl_unsubscribe(sub);
  196. if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
  197. del_timer_sync(&sub->timer);
  198. list_del(&sub->subscrp_list);
  199. tipc_subscrp_put(sub);
  200. }
  201. int tipc_topsrv_start(struct net *net)
  202. {
  203. struct tipc_net *tn = net_generic(net, tipc_net_id);
  204. const char name[] = "topology_server";
  205. struct tipc_server *topsrv;
  206. struct sockaddr_tipc *saddr;
  207. saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
  208. if (!saddr)
  209. return -ENOMEM;
  210. saddr->family = AF_TIPC;
  211. saddr->addrtype = TIPC_ADDR_NAMESEQ;
  212. saddr->addr.nameseq.type = TIPC_TOP_SRV;
  213. saddr->addr.nameseq.lower = TIPC_TOP_SRV;
  214. saddr->addr.nameseq.upper = TIPC_TOP_SRV;
  215. saddr->scope = TIPC_NODE_SCOPE;
  216. topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
  217. if (!topsrv) {
  218. kfree(saddr);
  219. return -ENOMEM;
  220. }
  221. topsrv->net = net;
  222. topsrv->saddr = saddr;
  223. topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
  224. strncpy(topsrv->name, name, strlen(name) + 1);
  225. tn->topsrv = topsrv;
  226. atomic_set(&tn->subscription_count, 0);
  227. return tipc_server_start(topsrv);
  228. }
  229. void tipc_topsrv_stop(struct net *net)
  230. {
  231. struct tipc_net *tn = net_generic(net, tipc_net_id);
  232. struct tipc_server *topsrv = tn->topsrv;
  233. tipc_server_stop(topsrv);
  234. kfree(topsrv->saddr);
  235. kfree(topsrv);
  236. }