subscr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. * @kref: reference counter to tipc_subscription object
  42. * @conid: connection identifier to server connecting to subscriber
  43. * @lock: control access to subscriber
  44. * @subscrp_list: list of subscription objects for this subscriber
  45. */
  46. struct tipc_subscriber {
  47. struct kref kref;
  48. int conid;
  49. spinlock_t lock;
  50. struct list_head subscrp_list;
  51. };
  52. static void tipc_subscrp_delete(struct tipc_subscription *sub);
  53. static void tipc_subscrb_put(struct tipc_subscriber *subscriber);
  54. /**
  55. * htohl - convert value to endianness used by destination
  56. * @in: value to convert
  57. * @swap: non-zero if endianness must be reversed
  58. *
  59. * Returns converted value
  60. */
  61. static u32 htohl(u32 in, int swap)
  62. {
  63. return swap ? swab32(in) : in;
  64. }
  65. static void tipc_subscrp_send_event(struct tipc_subscription *sub,
  66. u32 found_lower, u32 found_upper,
  67. u32 event, u32 port_ref, u32 node)
  68. {
  69. struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
  70. struct tipc_subscriber *subscriber = sub->subscriber;
  71. struct kvec msg_sect;
  72. msg_sect.iov_base = (void *)&sub->evt;
  73. msg_sect.iov_len = sizeof(struct tipc_event);
  74. sub->evt.event = htohl(event, sub->swap);
  75. sub->evt.found_lower = htohl(found_lower, sub->swap);
  76. sub->evt.found_upper = htohl(found_upper, sub->swap);
  77. sub->evt.port.ref = htohl(port_ref, sub->swap);
  78. sub->evt.port.node = htohl(node, sub->swap);
  79. tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
  80. msg_sect.iov_base, msg_sect.iov_len);
  81. }
  82. /**
  83. * tipc_subscrp_check_overlap - test for subscription overlap with the
  84. * given values
  85. *
  86. * Returns 1 if there is overlap, otherwise 0.
  87. */
  88. int tipc_subscrp_check_overlap(struct tipc_subscription *sub, u32 found_lower,
  89. u32 found_upper)
  90. {
  91. if (found_lower < sub->seq.lower)
  92. found_lower = sub->seq.lower;
  93. if (found_upper > sub->seq.upper)
  94. found_upper = sub->seq.upper;
  95. if (found_lower > found_upper)
  96. return 0;
  97. return 1;
  98. }
  99. void tipc_subscrp_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_subscrp_check_overlap(sub, found_lower, found_upper))
  104. return;
  105. if (!must && !(sub->filter & TIPC_SUB_PORTS))
  106. return;
  107. tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
  108. node);
  109. }
  110. static void tipc_subscrp_timeout(unsigned long data)
  111. {
  112. struct tipc_subscription *sub = (struct tipc_subscription *)data;
  113. struct tipc_subscriber *subscriber = sub->subscriber;
  114. /* Notify subscriber of timeout */
  115. tipc_subscrp_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
  116. TIPC_SUBSCR_TIMEOUT, 0, 0);
  117. spin_lock_bh(&subscriber->lock);
  118. tipc_subscrp_delete(sub);
  119. spin_unlock_bh(&subscriber->lock);
  120. tipc_subscrb_put(subscriber);
  121. }
  122. static void tipc_subscrb_kref_release(struct kref *kref)
  123. {
  124. struct tipc_subscriber *subcriber = container_of(kref,
  125. struct tipc_subscriber, kref);
  126. kfree(subcriber);
  127. }
  128. static void tipc_subscrb_put(struct tipc_subscriber *subscriber)
  129. {
  130. kref_put(&subscriber->kref, tipc_subscrb_kref_release);
  131. }
  132. static void tipc_subscrb_get(struct tipc_subscriber *subscriber)
  133. {
  134. kref_get(&subscriber->kref);
  135. }
  136. static struct tipc_subscriber *tipc_subscrb_create(int conid)
  137. {
  138. struct tipc_subscriber *subscriber;
  139. subscriber = kzalloc(sizeof(*subscriber), GFP_ATOMIC);
  140. if (!subscriber) {
  141. pr_warn("Subscriber rejected, no memory\n");
  142. return NULL;
  143. }
  144. kref_init(&subscriber->kref);
  145. INIT_LIST_HEAD(&subscriber->subscrp_list);
  146. subscriber->conid = conid;
  147. spin_lock_init(&subscriber->lock);
  148. return subscriber;
  149. }
  150. static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
  151. {
  152. struct tipc_subscription *sub, *temp;
  153. spin_lock_bh(&subscriber->lock);
  154. /* Destroy any existing subscriptions for subscriber */
  155. list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
  156. subscrp_list) {
  157. if (del_timer(&sub->timer)) {
  158. tipc_subscrp_delete(sub);
  159. tipc_subscrb_put(subscriber);
  160. }
  161. }
  162. spin_unlock_bh(&subscriber->lock);
  163. tipc_subscrb_put(subscriber);
  164. }
  165. static void tipc_subscrp_delete(struct tipc_subscription *sub)
  166. {
  167. struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
  168. tipc_nametbl_unsubscribe(sub);
  169. list_del(&sub->subscrp_list);
  170. kfree(sub);
  171. atomic_dec(&tn->subscription_count);
  172. }
  173. static void tipc_subscrp_cancel(struct tipc_subscr *s,
  174. struct tipc_subscriber *subscriber)
  175. {
  176. struct tipc_subscription *sub, *temp;
  177. spin_lock_bh(&subscriber->lock);
  178. /* Find first matching subscription, exit if not found */
  179. list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
  180. subscrp_list) {
  181. if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
  182. if (del_timer(&sub->timer)) {
  183. tipc_subscrp_delete(sub);
  184. tipc_subscrb_put(subscriber);
  185. }
  186. break;
  187. }
  188. }
  189. spin_unlock_bh(&subscriber->lock);
  190. }
  191. static int tipc_subscrp_create(struct net *net, struct tipc_subscr *s,
  192. struct tipc_subscriber *subscriber,
  193. struct tipc_subscription **sub_p)
  194. {
  195. struct tipc_net *tn = net_generic(net, tipc_net_id);
  196. struct tipc_subscription *sub;
  197. int swap;
  198. /* Determine subscriber's endianness */
  199. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
  200. /* Detect & process a subscription cancellation request */
  201. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  202. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  203. tipc_subscrp_cancel(s, subscriber);
  204. return 0;
  205. }
  206. /* Refuse subscription if global limit exceeded */
  207. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
  208. pr_warn("Subscription rejected, limit reached (%u)\n",
  209. TIPC_MAX_SUBSCRIPTIONS);
  210. return -EINVAL;
  211. }
  212. /* Allocate subscription object */
  213. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  214. if (!sub) {
  215. pr_warn("Subscription rejected, no memory\n");
  216. return -ENOMEM;
  217. }
  218. /* Initialize subscription object */
  219. sub->net = net;
  220. sub->seq.type = htohl(s->seq.type, swap);
  221. sub->seq.lower = htohl(s->seq.lower, swap);
  222. sub->seq.upper = htohl(s->seq.upper, swap);
  223. sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
  224. sub->filter = htohl(s->filter, swap);
  225. if ((!(sub->filter & TIPC_SUB_PORTS) ==
  226. !(sub->filter & TIPC_SUB_SERVICE)) ||
  227. (sub->seq.lower > sub->seq.upper)) {
  228. pr_warn("Subscription rejected, illegal request\n");
  229. kfree(sub);
  230. return -EINVAL;
  231. }
  232. spin_lock_bh(&subscriber->lock);
  233. list_add(&sub->subscrp_list, &subscriber->subscrp_list);
  234. spin_unlock_bh(&subscriber->lock);
  235. sub->subscriber = subscriber;
  236. sub->swap = swap;
  237. memcpy(&sub->evt.s, s, sizeof(*s));
  238. atomic_inc(&tn->subscription_count);
  239. setup_timer(&sub->timer, tipc_subscrp_timeout, (unsigned long)sub);
  240. if (sub->timeout != TIPC_WAIT_FOREVER)
  241. sub->timeout += jiffies;
  242. if (!mod_timer(&sub->timer, sub->timeout))
  243. tipc_subscrb_get(subscriber);
  244. *sub_p = sub;
  245. return 0;
  246. }
  247. /* Handle one termination request for the subscriber */
  248. static void tipc_subscrb_shutdown_cb(int conid, void *usr_data)
  249. {
  250. tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
  251. }
  252. /* Handle one request to create a new subscription for the subscriber */
  253. static void tipc_subscrb_rcv_cb(struct net *net, int conid,
  254. struct sockaddr_tipc *addr, void *usr_data,
  255. void *buf, size_t len)
  256. {
  257. struct tipc_subscriber *subscrb = usr_data;
  258. struct tipc_subscription *sub = NULL;
  259. struct tipc_net *tn = net_generic(net, tipc_net_id);
  260. if (tipc_subscrp_create(net, (struct tipc_subscr *)buf, subscrb, &sub))
  261. return tipc_conn_terminate(tn->topsrv, subscrb->conid);
  262. if (sub)
  263. tipc_nametbl_subscribe(sub);
  264. }
  265. /* Handle one request to establish a new subscriber */
  266. static void *tipc_subscrb_connect_cb(int conid)
  267. {
  268. return (void *)tipc_subscrb_create(conid);
  269. }
  270. int tipc_topsrv_start(struct net *net)
  271. {
  272. struct tipc_net *tn = net_generic(net, tipc_net_id);
  273. const char name[] = "topology_server";
  274. struct tipc_server *topsrv;
  275. struct sockaddr_tipc *saddr;
  276. saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
  277. if (!saddr)
  278. return -ENOMEM;
  279. saddr->family = AF_TIPC;
  280. saddr->addrtype = TIPC_ADDR_NAMESEQ;
  281. saddr->addr.nameseq.type = TIPC_TOP_SRV;
  282. saddr->addr.nameseq.lower = TIPC_TOP_SRV;
  283. saddr->addr.nameseq.upper = TIPC_TOP_SRV;
  284. saddr->scope = TIPC_NODE_SCOPE;
  285. topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
  286. if (!topsrv) {
  287. kfree(saddr);
  288. return -ENOMEM;
  289. }
  290. topsrv->net = net;
  291. topsrv->saddr = saddr;
  292. topsrv->imp = TIPC_CRITICAL_IMPORTANCE;
  293. topsrv->type = SOCK_SEQPACKET;
  294. topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
  295. topsrv->tipc_conn_recvmsg = tipc_subscrb_rcv_cb;
  296. topsrv->tipc_conn_new = tipc_subscrb_connect_cb;
  297. topsrv->tipc_conn_shutdown = tipc_subscrb_shutdown_cb;
  298. strscpy(topsrv->name, name, sizeof(topsrv->name));
  299. tn->topsrv = topsrv;
  300. atomic_set(&tn->subscription_count, 0);
  301. return tipc_server_start(topsrv);
  302. }
  303. void tipc_topsrv_stop(struct net *net)
  304. {
  305. struct tipc_net *tn = net_generic(net, tipc_net_id);
  306. struct tipc_server *topsrv = tn->topsrv;
  307. tipc_server_stop(topsrv);
  308. kfree(topsrv->saddr);
  309. kfree(topsrv);
  310. }