subscr.c 12 KB

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