subscr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * net/tipc/subscr.c: TIPC network topology service
  3. *
  4. * Copyright (c) 2000-2006, 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. * struct tipc_subscriber - TIPC network topology subscriber
  41. * @conid: connection identifier to server connecting to subscriber
  42. * @lock: control access to subscriber
  43. * @subscription_list: list of subscription objects for this subscriber
  44. */
  45. struct tipc_subscriber {
  46. int conid;
  47. spinlock_t lock;
  48. struct list_head subscription_list;
  49. };
  50. static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
  51. void *usr_data, void *buf, size_t len);
  52. static void *subscr_named_msg_event(int conid);
  53. static void subscr_conn_shutdown_event(int conid, void *usr_data);
  54. static atomic_t subscription_count = ATOMIC_INIT(0);
  55. static struct sockaddr_tipc topsrv_addr __read_mostly = {
  56. .family = AF_TIPC,
  57. .addrtype = TIPC_ADDR_NAMESEQ,
  58. .addr.nameseq.type = TIPC_TOP_SRV,
  59. .addr.nameseq.lower = TIPC_TOP_SRV,
  60. .addr.nameseq.upper = TIPC_TOP_SRV,
  61. .scope = TIPC_NODE_SCOPE
  62. };
  63. static struct tipc_server topsrv __read_mostly = {
  64. .saddr = &topsrv_addr,
  65. .imp = TIPC_CRITICAL_IMPORTANCE,
  66. .type = SOCK_SEQPACKET,
  67. .max_rcvbuf_size = sizeof(struct tipc_subscr),
  68. .name = "topology_server",
  69. .tipc_conn_recvmsg = subscr_conn_msg_event,
  70. .tipc_conn_new = subscr_named_msg_event,
  71. .tipc_conn_shutdown = subscr_conn_shutdown_event,
  72. };
  73. /**
  74. * htohl - convert value to endianness used by destination
  75. * @in: value to convert
  76. * @swap: non-zero if endianness must be reversed
  77. *
  78. * Returns converted value
  79. */
  80. static u32 htohl(u32 in, int swap)
  81. {
  82. return swap ? swab32(in) : in;
  83. }
  84. static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
  85. u32 found_upper, u32 event, u32 port_ref,
  86. u32 node)
  87. {
  88. struct tipc_subscriber *subscriber = sub->subscriber;
  89. struct kvec msg_sect;
  90. msg_sect.iov_base = (void *)&sub->evt;
  91. msg_sect.iov_len = sizeof(struct tipc_event);
  92. sub->evt.event = htohl(event, sub->swap);
  93. sub->evt.found_lower = htohl(found_lower, sub->swap);
  94. sub->evt.found_upper = htohl(found_upper, sub->swap);
  95. sub->evt.port.ref = htohl(port_ref, sub->swap);
  96. sub->evt.port.node = htohl(node, sub->swap);
  97. tipc_conn_sendmsg(&topsrv, subscriber->conid, NULL, msg_sect.iov_base,
  98. msg_sect.iov_len);
  99. }
  100. /**
  101. * tipc_subscr_overlap - test for subscription overlap with the given values
  102. *
  103. * Returns 1 if there is overlap, otherwise 0.
  104. */
  105. int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
  106. u32 found_upper)
  107. {
  108. if (found_lower < sub->seq.lower)
  109. found_lower = sub->seq.lower;
  110. if (found_upper > sub->seq.upper)
  111. found_upper = sub->seq.upper;
  112. if (found_lower > found_upper)
  113. return 0;
  114. return 1;
  115. }
  116. /**
  117. * tipc_subscr_report_overlap - issue event if there is subscription overlap
  118. *
  119. * Protected by nameseq.lock in name_table.c
  120. */
  121. void tipc_subscr_report_overlap(struct tipc_subscription *sub, u32 found_lower,
  122. u32 found_upper, u32 event, u32 port_ref,
  123. u32 node, int must)
  124. {
  125. if (!tipc_subscr_overlap(sub, found_lower, found_upper))
  126. return;
  127. if (!must && !(sub->filter & TIPC_SUB_PORTS))
  128. return;
  129. subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
  130. }
  131. static void subscr_timeout(struct tipc_subscription *sub)
  132. {
  133. struct tipc_subscriber *subscriber = sub->subscriber;
  134. /* The spin lock per subscriber is used to protect its members */
  135. spin_lock_bh(&subscriber->lock);
  136. /* Validate timeout (in case subscription is being cancelled) */
  137. if (sub->timeout == TIPC_WAIT_FOREVER) {
  138. spin_unlock_bh(&subscriber->lock);
  139. return;
  140. }
  141. /* Unlink subscription from name table */
  142. tipc_nametbl_unsubscribe(sub);
  143. /* Unlink subscription from subscriber */
  144. list_del(&sub->subscription_list);
  145. spin_unlock_bh(&subscriber->lock);
  146. /* Notify subscriber of timeout */
  147. subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
  148. TIPC_SUBSCR_TIMEOUT, 0, 0);
  149. /* Now destroy subscription */
  150. k_term_timer(&sub->timer);
  151. kfree(sub);
  152. atomic_dec(&subscription_count);
  153. }
  154. /**
  155. * subscr_del - delete a subscription within a subscription list
  156. *
  157. * Called with subscriber lock held.
  158. */
  159. static void subscr_del(struct tipc_subscription *sub)
  160. {
  161. tipc_nametbl_unsubscribe(sub);
  162. list_del(&sub->subscription_list);
  163. kfree(sub);
  164. atomic_dec(&subscription_count);
  165. }
  166. /**
  167. * subscr_terminate - terminate communication with a subscriber
  168. *
  169. * Note: Must call it in process context since it might sleep.
  170. */
  171. static void subscr_terminate(struct tipc_subscriber *subscriber)
  172. {
  173. tipc_conn_terminate(&topsrv, subscriber->conid);
  174. }
  175. static void subscr_release(struct tipc_subscriber *subscriber)
  176. {
  177. struct tipc_subscription *sub;
  178. struct tipc_subscription *sub_temp;
  179. spin_lock_bh(&subscriber->lock);
  180. /* Destroy any existing subscriptions for subscriber */
  181. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  182. subscription_list) {
  183. if (sub->timeout != TIPC_WAIT_FOREVER) {
  184. spin_unlock_bh(&subscriber->lock);
  185. k_cancel_timer(&sub->timer);
  186. k_term_timer(&sub->timer);
  187. spin_lock_bh(&subscriber->lock);
  188. }
  189. subscr_del(sub);
  190. }
  191. spin_unlock_bh(&subscriber->lock);
  192. /* Now destroy subscriber */
  193. kfree(subscriber);
  194. }
  195. /**
  196. * subscr_cancel - handle subscription cancellation request
  197. *
  198. * Called with subscriber lock held. Routine must temporarily release lock
  199. * to enable the subscription timeout routine to finish without deadlocking;
  200. * the lock is then reclaimed to allow caller to release it upon return.
  201. *
  202. * Note that fields of 's' use subscriber's endianness!
  203. */
  204. static void subscr_cancel(struct tipc_subscr *s,
  205. struct tipc_subscriber *subscriber)
  206. {
  207. struct tipc_subscription *sub;
  208. struct tipc_subscription *sub_temp;
  209. int found = 0;
  210. /* Find first matching subscription, exit if not found */
  211. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  212. subscription_list) {
  213. if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
  214. found = 1;
  215. break;
  216. }
  217. }
  218. if (!found)
  219. return;
  220. /* Cancel subscription timer (if used), then delete subscription */
  221. if (sub->timeout != TIPC_WAIT_FOREVER) {
  222. sub->timeout = TIPC_WAIT_FOREVER;
  223. spin_unlock_bh(&subscriber->lock);
  224. k_cancel_timer(&sub->timer);
  225. k_term_timer(&sub->timer);
  226. spin_lock_bh(&subscriber->lock);
  227. }
  228. subscr_del(sub);
  229. }
  230. /**
  231. * subscr_subscribe - create subscription for subscriber
  232. *
  233. * Called with subscriber lock held.
  234. */
  235. static int subscr_subscribe(struct tipc_subscr *s,
  236. struct tipc_subscriber *subscriber,
  237. struct tipc_subscription **sub_p) {
  238. struct tipc_subscription *sub;
  239. int swap;
  240. /* Determine subscriber's endianness */
  241. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
  242. /* Detect & process a subscription cancellation request */
  243. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  244. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  245. subscr_cancel(s, subscriber);
  246. return 0;
  247. }
  248. /* Refuse subscription if global limit exceeded */
  249. if (atomic_read(&subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
  250. pr_warn("Subscription rejected, limit reached (%u)\n",
  251. TIPC_MAX_SUBSCRIPTIONS);
  252. return -EINVAL;
  253. }
  254. /* Allocate subscription object */
  255. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  256. if (!sub) {
  257. pr_warn("Subscription rejected, no memory\n");
  258. return -ENOMEM;
  259. }
  260. /* Initialize subscription object */
  261. sub->seq.type = htohl(s->seq.type, swap);
  262. sub->seq.lower = htohl(s->seq.lower, swap);
  263. sub->seq.upper = htohl(s->seq.upper, swap);
  264. sub->timeout = htohl(s->timeout, swap);
  265. sub->filter = htohl(s->filter, swap);
  266. if ((!(sub->filter & TIPC_SUB_PORTS) ==
  267. !(sub->filter & TIPC_SUB_SERVICE)) ||
  268. (sub->seq.lower > sub->seq.upper)) {
  269. pr_warn("Subscription rejected, illegal request\n");
  270. kfree(sub);
  271. return -EINVAL;
  272. }
  273. INIT_LIST_HEAD(&sub->nameseq_list);
  274. list_add(&sub->subscription_list, &subscriber->subscription_list);
  275. sub->subscriber = subscriber;
  276. sub->swap = swap;
  277. memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
  278. atomic_inc(&subscription_count);
  279. if (sub->timeout != TIPC_WAIT_FOREVER) {
  280. k_init_timer(&sub->timer,
  281. (Handler)subscr_timeout, (unsigned long)sub);
  282. k_start_timer(&sub->timer, sub->timeout);
  283. }
  284. *sub_p = sub;
  285. return 0;
  286. }
  287. /* Handle one termination request for the subscriber */
  288. static void subscr_conn_shutdown_event(int conid, void *usr_data)
  289. {
  290. subscr_release((struct tipc_subscriber *)usr_data);
  291. }
  292. /* Handle one request to create a new subscription for the subscriber */
  293. static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
  294. void *usr_data, void *buf, size_t len)
  295. {
  296. struct tipc_subscriber *subscriber = usr_data;
  297. struct tipc_subscription *sub = NULL;
  298. spin_lock_bh(&subscriber->lock);
  299. if (subscr_subscribe((struct tipc_subscr *)buf, subscriber, &sub) < 0) {
  300. spin_unlock_bh(&subscriber->lock);
  301. subscr_terminate(subscriber);
  302. return;
  303. }
  304. if (sub)
  305. tipc_nametbl_subscribe(sub);
  306. spin_unlock_bh(&subscriber->lock);
  307. }
  308. /* Handle one request to establish a new subscriber */
  309. static void *subscr_named_msg_event(int conid)
  310. {
  311. struct tipc_subscriber *subscriber;
  312. /* Create subscriber object */
  313. subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
  314. if (subscriber == NULL) {
  315. pr_warn("Subscriber rejected, no memory\n");
  316. return NULL;
  317. }
  318. INIT_LIST_HEAD(&subscriber->subscription_list);
  319. subscriber->conid = conid;
  320. spin_lock_init(&subscriber->lock);
  321. return (void *)subscriber;
  322. }
  323. int tipc_subscr_start(void)
  324. {
  325. return tipc_server_start(&topsrv);
  326. }
  327. void tipc_subscr_stop(void)
  328. {
  329. tipc_server_stop(&topsrv);
  330. }