subscr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_name_seq *seq, u32 found_lower,
  89. u32 found_upper)
  90. {
  91. if (found_lower < seq->lower)
  92. found_lower = seq->lower;
  93. if (found_upper > seq->upper)
  94. found_upper = seq->upper;
  95. if (found_lower > found_upper)
  96. return 0;
  97. return 1;
  98. }
  99. u32 tipc_subscrp_convert_seq_type(u32 type, int swap)
  100. {
  101. return htohl(type, swap);
  102. }
  103. void tipc_subscrp_convert_seq(struct tipc_name_seq *in, int swap,
  104. struct tipc_name_seq *out)
  105. {
  106. out->type = htohl(in->type, swap);
  107. out->lower = htohl(in->lower, swap);
  108. out->upper = htohl(in->upper, swap);
  109. }
  110. void tipc_subscrp_report_overlap(struct tipc_subscription *sub, u32 found_lower,
  111. u32 found_upper, u32 event, u32 port_ref,
  112. u32 node, int must)
  113. {
  114. struct tipc_name_seq seq;
  115. tipc_subscrp_convert_seq(&sub->evt.s.seq, sub->swap, &seq);
  116. if (!tipc_subscrp_check_overlap(&seq, found_lower, found_upper))
  117. return;
  118. if (!must &&
  119. !(htohl(sub->evt.s.filter, sub->swap) & TIPC_SUB_PORTS))
  120. return;
  121. tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
  122. node);
  123. }
  124. static void tipc_subscrp_timeout(unsigned long data)
  125. {
  126. struct tipc_subscription *sub = (struct tipc_subscription *)data;
  127. struct tipc_subscriber *subscriber = sub->subscriber;
  128. /* Notify subscriber of timeout */
  129. tipc_subscrp_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
  130. TIPC_SUBSCR_TIMEOUT, 0, 0);
  131. spin_lock_bh(&subscriber->lock);
  132. tipc_subscrp_delete(sub);
  133. spin_unlock_bh(&subscriber->lock);
  134. tipc_subscrb_put(subscriber);
  135. }
  136. static void tipc_subscrb_kref_release(struct kref *kref)
  137. {
  138. struct tipc_subscriber *subcriber = container_of(kref,
  139. struct tipc_subscriber, kref);
  140. kfree(subcriber);
  141. }
  142. static void tipc_subscrb_put(struct tipc_subscriber *subscriber)
  143. {
  144. kref_put(&subscriber->kref, tipc_subscrb_kref_release);
  145. }
  146. static void tipc_subscrb_get(struct tipc_subscriber *subscriber)
  147. {
  148. kref_get(&subscriber->kref);
  149. }
  150. static struct tipc_subscriber *tipc_subscrb_create(int conid)
  151. {
  152. struct tipc_subscriber *subscriber;
  153. subscriber = kzalloc(sizeof(*subscriber), GFP_ATOMIC);
  154. if (!subscriber) {
  155. pr_warn("Subscriber rejected, no memory\n");
  156. return NULL;
  157. }
  158. kref_init(&subscriber->kref);
  159. INIT_LIST_HEAD(&subscriber->subscrp_list);
  160. subscriber->conid = conid;
  161. spin_lock_init(&subscriber->lock);
  162. return subscriber;
  163. }
  164. static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
  165. {
  166. struct tipc_subscription *sub, *temp;
  167. spin_lock_bh(&subscriber->lock);
  168. /* Destroy any existing subscriptions for subscriber */
  169. list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
  170. subscrp_list) {
  171. if (del_timer(&sub->timer)) {
  172. tipc_subscrp_delete(sub);
  173. tipc_subscrb_put(subscriber);
  174. }
  175. }
  176. spin_unlock_bh(&subscriber->lock);
  177. tipc_subscrb_put(subscriber);
  178. }
  179. static void tipc_subscrp_delete(struct tipc_subscription *sub)
  180. {
  181. struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
  182. tipc_nametbl_unsubscribe(sub);
  183. list_del(&sub->subscrp_list);
  184. kfree(sub);
  185. atomic_dec(&tn->subscription_count);
  186. }
  187. static void tipc_subscrp_cancel(struct tipc_subscr *s,
  188. struct tipc_subscriber *subscriber)
  189. {
  190. struct tipc_subscription *sub, *temp;
  191. spin_lock_bh(&subscriber->lock);
  192. /* Find first matching subscription, exit if not found */
  193. list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
  194. subscrp_list) {
  195. if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
  196. if (del_timer(&sub->timer)) {
  197. tipc_subscrp_delete(sub);
  198. tipc_subscrb_put(subscriber);
  199. }
  200. break;
  201. }
  202. }
  203. spin_unlock_bh(&subscriber->lock);
  204. }
  205. static struct tipc_subscription *tipc_subscrp_create(struct net *net,
  206. struct tipc_subscr *s,
  207. int swap)
  208. {
  209. struct tipc_net *tn = net_generic(net, tipc_net_id);
  210. struct tipc_subscription *sub;
  211. u32 filter = htohl(s->filter, swap);
  212. /* Refuse subscription if global limit exceeded */
  213. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
  214. pr_warn("Subscription rejected, limit reached (%u)\n",
  215. TIPC_MAX_SUBSCRIPTIONS);
  216. return NULL;
  217. }
  218. /* Allocate subscription object */
  219. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  220. if (!sub) {
  221. pr_warn("Subscription rejected, no memory\n");
  222. return NULL;
  223. }
  224. /* Initialize subscription object */
  225. sub->net = net;
  226. if (((filter & TIPC_SUB_PORTS) && (filter & TIPC_SUB_SERVICE)) ||
  227. (htohl(s->seq.lower, swap) > htohl(s->seq.upper, swap))) {
  228. pr_warn("Subscription rejected, illegal request\n");
  229. kfree(sub);
  230. return NULL;
  231. }
  232. sub->swap = swap;
  233. memcpy(&sub->evt.s, s, sizeof(*s));
  234. atomic_inc(&tn->subscription_count);
  235. setup_timer(&sub->timer, tipc_subscrp_timeout, (unsigned long)sub);
  236. return sub;
  237. }
  238. static void tipc_subscrp_subscribe(struct net *net, struct tipc_subscr *s,
  239. struct tipc_subscriber *subscriber, int swap)
  240. {
  241. struct tipc_net *tn = net_generic(net, tipc_net_id);
  242. struct tipc_subscription *sub = NULL;
  243. u32 timeout;
  244. sub = tipc_subscrp_create(net, s, swap);
  245. if (!sub)
  246. return tipc_conn_terminate(tn->topsrv, subscriber->conid);
  247. spin_lock_bh(&subscriber->lock);
  248. list_add(&sub->subscrp_list, &subscriber->subscrp_list);
  249. sub->subscriber = subscriber;
  250. tipc_nametbl_subscribe(sub);
  251. spin_unlock_bh(&subscriber->lock);
  252. timeout = htohl(sub->evt.s.timeout, swap);
  253. if (!mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout)))
  254. tipc_subscrb_get(subscriber);
  255. }
  256. /* Handle one termination request for the subscriber */
  257. static void tipc_subscrb_shutdown_cb(int conid, void *usr_data)
  258. {
  259. tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
  260. }
  261. /* Handle one request to create a new subscription for the subscriber */
  262. static void tipc_subscrb_rcv_cb(struct net *net, int conid,
  263. struct sockaddr_tipc *addr, void *usr_data,
  264. void *buf, size_t len)
  265. {
  266. struct tipc_subscriber *subscriber = usr_data;
  267. struct tipc_subscr *s = (struct tipc_subscr *)buf;
  268. int swap;
  269. /* Determine subscriber's endianness */
  270. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE |
  271. TIPC_SUB_CANCEL));
  272. /* Detect & process a subscription cancellation request */
  273. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  274. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  275. return tipc_subscrp_cancel(s, subscriber);
  276. }
  277. tipc_subscrp_subscribe(net, s, subscriber, swap);
  278. }
  279. /* Handle one request to establish a new subscriber */
  280. static void *tipc_subscrb_connect_cb(int conid)
  281. {
  282. return (void *)tipc_subscrb_create(conid);
  283. }
  284. int tipc_topsrv_start(struct net *net)
  285. {
  286. struct tipc_net *tn = net_generic(net, tipc_net_id);
  287. const char name[] = "topology_server";
  288. struct tipc_server *topsrv;
  289. struct sockaddr_tipc *saddr;
  290. saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
  291. if (!saddr)
  292. return -ENOMEM;
  293. saddr->family = AF_TIPC;
  294. saddr->addrtype = TIPC_ADDR_NAMESEQ;
  295. saddr->addr.nameseq.type = TIPC_TOP_SRV;
  296. saddr->addr.nameseq.lower = TIPC_TOP_SRV;
  297. saddr->addr.nameseq.upper = TIPC_TOP_SRV;
  298. saddr->scope = TIPC_NODE_SCOPE;
  299. topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
  300. if (!topsrv) {
  301. kfree(saddr);
  302. return -ENOMEM;
  303. }
  304. topsrv->net = net;
  305. topsrv->saddr = saddr;
  306. topsrv->imp = TIPC_CRITICAL_IMPORTANCE;
  307. topsrv->type = SOCK_SEQPACKET;
  308. topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
  309. topsrv->tipc_conn_recvmsg = tipc_subscrb_rcv_cb;
  310. topsrv->tipc_conn_new = tipc_subscrb_connect_cb;
  311. topsrv->tipc_conn_shutdown = tipc_subscrb_shutdown_cb;
  312. strncpy(topsrv->name, name, strlen(name) + 1);
  313. tn->topsrv = topsrv;
  314. atomic_set(&tn->subscription_count, 0);
  315. return tipc_server_start(topsrv);
  316. }
  317. void tipc_topsrv_stop(struct net *net)
  318. {
  319. struct tipc_net *tn = net_generic(net, tipc_net_id);
  320. struct tipc_server *topsrv = tn->topsrv;
  321. tipc_server_stop(topsrv);
  322. kfree(topsrv->saddr);
  323. kfree(topsrv);
  324. }