subscr.c 11 KB

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