subscr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_subscrb_put(struct tipc_subscriber *subscriber);
  53. /**
  54. * htohl - convert value to endianness used by destination
  55. * @in: value to convert
  56. * @swap: non-zero if endianness must be reversed
  57. *
  58. * Returns converted value
  59. */
  60. static u32 htohl(u32 in, int swap)
  61. {
  62. return swap ? swab32(in) : in;
  63. }
  64. static void tipc_subscrp_send_event(struct tipc_subscription *sub,
  65. u32 found_lower, u32 found_upper,
  66. u32 event, u32 port_ref, u32 node)
  67. {
  68. struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
  69. struct tipc_subscriber *subscriber = sub->subscriber;
  70. struct kvec msg_sect;
  71. msg_sect.iov_base = (void *)&sub->evt;
  72. msg_sect.iov_len = sizeof(struct tipc_event);
  73. sub->evt.event = htohl(event, sub->swap);
  74. sub->evt.found_lower = htohl(found_lower, sub->swap);
  75. sub->evt.found_upper = htohl(found_upper, sub->swap);
  76. sub->evt.port.ref = htohl(port_ref, sub->swap);
  77. sub->evt.port.node = htohl(node, sub->swap);
  78. tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
  79. msg_sect.iov_base, msg_sect.iov_len);
  80. }
  81. /**
  82. * tipc_subscrp_check_overlap - test for subscription overlap with the
  83. * given values
  84. *
  85. * Returns 1 if there is overlap, otherwise 0.
  86. */
  87. int tipc_subscrp_check_overlap(struct tipc_name_seq *seq, u32 found_lower,
  88. u32 found_upper)
  89. {
  90. if (found_lower < seq->lower)
  91. found_lower = seq->lower;
  92. if (found_upper > seq->upper)
  93. found_upper = seq->upper;
  94. if (found_lower > found_upper)
  95. return 0;
  96. return 1;
  97. }
  98. u32 tipc_subscrp_convert_seq_type(u32 type, int swap)
  99. {
  100. return htohl(type, swap);
  101. }
  102. void tipc_subscrp_convert_seq(struct tipc_name_seq *in, int swap,
  103. struct tipc_name_seq *out)
  104. {
  105. out->type = htohl(in->type, swap);
  106. out->lower = htohl(in->lower, swap);
  107. out->upper = htohl(in->upper, swap);
  108. }
  109. void tipc_subscrp_report_overlap(struct tipc_subscription *sub, u32 found_lower,
  110. u32 found_upper, u32 event, u32 port_ref,
  111. u32 node, u32 scope, int must)
  112. {
  113. u32 filter = htohl(sub->evt.s.filter, sub->swap);
  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 && !(filter & TIPC_SUB_PORTS))
  119. return;
  120. if (filter & TIPC_SUB_CLUSTER_SCOPE && scope == TIPC_NODE_SCOPE)
  121. return;
  122. if (filter & TIPC_SUB_NODE_SCOPE && scope != TIPC_NODE_SCOPE)
  123. return;
  124. tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
  125. node);
  126. }
  127. static void tipc_subscrp_timeout(struct timer_list *t)
  128. {
  129. struct tipc_subscription *sub = from_timer(sub, t, timer);
  130. struct tipc_subscriber *subscriber = sub->subscriber;
  131. spin_lock_bh(&subscriber->lock);
  132. tipc_nametbl_unsubscribe(sub);
  133. list_del(&sub->subscrp_list);
  134. spin_unlock_bh(&subscriber->lock);
  135. /* Notify subscriber of timeout */
  136. tipc_subscrp_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
  137. TIPC_SUBSCR_TIMEOUT, 0, 0);
  138. tipc_subscrp_put(sub);
  139. }
  140. static void tipc_subscrb_kref_release(struct kref *kref)
  141. {
  142. kfree(container_of(kref,struct tipc_subscriber, kref));
  143. }
  144. static void tipc_subscrb_put(struct tipc_subscriber *subscriber)
  145. {
  146. kref_put(&subscriber->kref, tipc_subscrb_kref_release);
  147. }
  148. static void tipc_subscrb_get(struct tipc_subscriber *subscriber)
  149. {
  150. kref_get(&subscriber->kref);
  151. }
  152. static void tipc_subscrp_kref_release(struct kref *kref)
  153. {
  154. struct tipc_subscription *sub = container_of(kref,
  155. struct tipc_subscription,
  156. kref);
  157. struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
  158. struct tipc_subscriber *subscriber = sub->subscriber;
  159. atomic_dec(&tn->subscription_count);
  160. kfree(sub);
  161. tipc_subscrb_put(subscriber);
  162. }
  163. void tipc_subscrp_put(struct tipc_subscription *subscription)
  164. {
  165. kref_put(&subscription->kref, tipc_subscrp_kref_release);
  166. }
  167. void tipc_subscrp_get(struct tipc_subscription *subscription)
  168. {
  169. kref_get(&subscription->kref);
  170. }
  171. /* tipc_subscrb_subscrp_delete - delete a specific subscription or all
  172. * subscriptions for a given subscriber.
  173. */
  174. static void tipc_subscrb_subscrp_delete(struct tipc_subscriber *subscriber,
  175. struct tipc_subscr *s)
  176. {
  177. struct list_head *subscription_list = &subscriber->subscrp_list;
  178. struct tipc_subscription *sub, *temp;
  179. u32 timeout;
  180. spin_lock_bh(&subscriber->lock);
  181. list_for_each_entry_safe(sub, temp, subscription_list, subscrp_list) {
  182. if (s && memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr)))
  183. continue;
  184. timeout = htohl(sub->evt.s.timeout, sub->swap);
  185. if (timeout == TIPC_WAIT_FOREVER || del_timer(&sub->timer)) {
  186. tipc_nametbl_unsubscribe(sub);
  187. list_del(&sub->subscrp_list);
  188. tipc_subscrp_put(sub);
  189. }
  190. if (s)
  191. break;
  192. }
  193. spin_unlock_bh(&subscriber->lock);
  194. }
  195. static struct tipc_subscriber *tipc_subscrb_create(int conid)
  196. {
  197. struct tipc_subscriber *subscriber;
  198. subscriber = kzalloc(sizeof(*subscriber), GFP_ATOMIC);
  199. if (!subscriber) {
  200. pr_warn("Subscriber rejected, no memory\n");
  201. return NULL;
  202. }
  203. INIT_LIST_HEAD(&subscriber->subscrp_list);
  204. kref_init(&subscriber->kref);
  205. subscriber->conid = conid;
  206. spin_lock_init(&subscriber->lock);
  207. return subscriber;
  208. }
  209. static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
  210. {
  211. tipc_subscrb_subscrp_delete(subscriber, NULL);
  212. tipc_subscrb_put(subscriber);
  213. }
  214. static void tipc_subscrp_cancel(struct tipc_subscr *s,
  215. struct tipc_subscriber *subscriber)
  216. {
  217. tipc_subscrb_get(subscriber);
  218. tipc_subscrb_subscrp_delete(subscriber, s);
  219. tipc_subscrb_put(subscriber);
  220. }
  221. static struct tipc_subscription *tipc_subscrp_create(struct net *net,
  222. struct tipc_subscr *s,
  223. int swap)
  224. {
  225. struct tipc_net *tn = net_generic(net, tipc_net_id);
  226. struct tipc_subscription *sub;
  227. u32 filter = htohl(s->filter, swap);
  228. /* Refuse subscription if global limit exceeded */
  229. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
  230. pr_warn("Subscription rejected, limit reached (%u)\n",
  231. TIPC_MAX_SUBSCRIPTIONS);
  232. return NULL;
  233. }
  234. /* Allocate subscription object */
  235. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  236. if (!sub) {
  237. pr_warn("Subscription rejected, no memory\n");
  238. return NULL;
  239. }
  240. /* Initialize subscription object */
  241. sub->net = net;
  242. if (((filter & TIPC_SUB_PORTS) && (filter & TIPC_SUB_SERVICE)) ||
  243. (htohl(s->seq.lower, swap) > htohl(s->seq.upper, swap))) {
  244. pr_warn("Subscription rejected, illegal request\n");
  245. kfree(sub);
  246. return NULL;
  247. }
  248. sub->swap = swap;
  249. memcpy(&sub->evt.s, s, sizeof(*s));
  250. atomic_inc(&tn->subscription_count);
  251. kref_init(&sub->kref);
  252. return sub;
  253. }
  254. static int tipc_subscrp_subscribe(struct net *net, struct tipc_subscr *s,
  255. struct tipc_subscriber *subscriber, int swap,
  256. bool status)
  257. {
  258. struct tipc_subscription *sub = NULL;
  259. u32 timeout;
  260. sub = tipc_subscrp_create(net, s, swap);
  261. if (!sub)
  262. return -1;
  263. spin_lock_bh(&subscriber->lock);
  264. list_add(&sub->subscrp_list, &subscriber->subscrp_list);
  265. sub->subscriber = subscriber;
  266. tipc_nametbl_subscribe(sub, status);
  267. tipc_subscrb_get(subscriber);
  268. spin_unlock_bh(&subscriber->lock);
  269. timer_setup(&sub->timer, tipc_subscrp_timeout, 0);
  270. timeout = htohl(sub->evt.s.timeout, swap);
  271. if (timeout != TIPC_WAIT_FOREVER)
  272. mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
  273. return 0;
  274. }
  275. /* Handle one termination request for the subscriber */
  276. static void tipc_subscrb_release_cb(int conid, void *usr_data)
  277. {
  278. tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
  279. }
  280. /* Handle one request to create a new subscription for the subscriber */
  281. static int tipc_subscrb_rcv_cb(struct net *net, int conid,
  282. struct sockaddr_tipc *addr, void *usr_data,
  283. void *buf, size_t len)
  284. {
  285. struct tipc_subscriber *subscriber = usr_data;
  286. struct tipc_subscr *s = (struct tipc_subscr *)buf;
  287. bool status;
  288. int swap;
  289. /* Determine subscriber's endianness */
  290. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE |
  291. TIPC_SUB_CANCEL));
  292. /* Detect & process a subscription cancellation request */
  293. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  294. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  295. tipc_subscrp_cancel(s, subscriber);
  296. return 0;
  297. }
  298. status = !(s->filter & htohl(TIPC_SUB_NO_STATUS, swap));
  299. return tipc_subscrp_subscribe(net, s, subscriber, swap, status);
  300. }
  301. /* Handle one request to establish a new subscriber */
  302. static void *tipc_subscrb_connect_cb(int conid)
  303. {
  304. return (void *)tipc_subscrb_create(conid);
  305. }
  306. int tipc_topsrv_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 = tipc_subscrb_rcv_cb;
  332. topsrv->tipc_conn_new = tipc_subscrb_connect_cb;
  333. topsrv->tipc_conn_release = tipc_subscrb_release_cb;
  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_topsrv_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. }