topsrv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * net/tipc/server.c: TIPC server infrastructure
  3. *
  4. * Copyright (c) 2012-2013, Wind River Systems
  5. * Copyright (c) 2017-2018, Ericsson AB
  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 "subscr.h"
  37. #include "topsrv.h"
  38. #include "core.h"
  39. #include "socket.h"
  40. #include "addr.h"
  41. #include "msg.h"
  42. #include <net/sock.h>
  43. #include <linux/module.h>
  44. /* Number of messages to send before rescheduling */
  45. #define MAX_SEND_MSG_COUNT 25
  46. #define MAX_RECV_MSG_COUNT 25
  47. #define CF_CONNECTED 1
  48. #define CF_SERVER 2
  49. #define TIPC_SERVER_NAME_LEN 32
  50. /**
  51. * struct tipc_topsrv - TIPC server structure
  52. * @conn_idr: identifier set of connection
  53. * @idr_lock: protect the connection identifier set
  54. * @idr_in_use: amount of allocated identifier entry
  55. * @net: network namspace instance
  56. * @awork: accept work item
  57. * @rcv_wq: receive workqueue
  58. * @send_wq: send workqueue
  59. * @max_rcvbuf_size: maximum permitted receive message length
  60. * @listener: topsrv listener socket
  61. * @name: server name
  62. */
  63. struct tipc_topsrv {
  64. struct idr conn_idr;
  65. spinlock_t idr_lock; /* for idr list */
  66. int idr_in_use;
  67. struct net *net;
  68. struct work_struct awork;
  69. struct workqueue_struct *rcv_wq;
  70. struct workqueue_struct *send_wq;
  71. int max_rcvbuf_size;
  72. struct socket *listener;
  73. char name[TIPC_SERVER_NAME_LEN];
  74. };
  75. /**
  76. * struct tipc_conn - TIPC connection structure
  77. * @kref: reference counter to connection object
  78. * @conid: connection identifier
  79. * @sock: socket handler associated with connection
  80. * @flags: indicates connection state
  81. * @server: pointer to connected server
  82. * @sub_list: lsit to all pertaing subscriptions
  83. * @sub_lock: lock protecting the subscription list
  84. * @rwork: receive work item
  85. * @outqueue: pointer to first outbound message in queue
  86. * @outqueue_lock: control access to the outqueue
  87. * @swork: send work item
  88. */
  89. struct tipc_conn {
  90. struct kref kref;
  91. int conid;
  92. struct socket *sock;
  93. unsigned long flags;
  94. struct tipc_topsrv *server;
  95. struct list_head sub_list;
  96. spinlock_t sub_lock; /* for subscription list */
  97. struct work_struct rwork;
  98. struct list_head outqueue;
  99. spinlock_t outqueue_lock; /* for outqueue */
  100. struct work_struct swork;
  101. };
  102. /* An entry waiting to be sent */
  103. struct outqueue_entry {
  104. bool inactive;
  105. struct tipc_event evt;
  106. struct list_head list;
  107. };
  108. static void tipc_conn_recv_work(struct work_struct *work);
  109. static void tipc_conn_send_work(struct work_struct *work);
  110. static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt);
  111. static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s);
  112. static bool connected(struct tipc_conn *con)
  113. {
  114. return con && test_bit(CF_CONNECTED, &con->flags);
  115. }
  116. static void tipc_conn_kref_release(struct kref *kref)
  117. {
  118. struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
  119. struct tipc_topsrv *s = con->server;
  120. struct outqueue_entry *e, *safe;
  121. spin_lock_bh(&s->idr_lock);
  122. idr_remove(&s->conn_idr, con->conid);
  123. s->idr_in_use--;
  124. spin_unlock_bh(&s->idr_lock);
  125. if (con->sock)
  126. sock_release(con->sock);
  127. spin_lock_bh(&con->outqueue_lock);
  128. list_for_each_entry_safe(e, safe, &con->outqueue, list) {
  129. list_del(&e->list);
  130. kfree(e);
  131. }
  132. spin_unlock_bh(&con->outqueue_lock);
  133. kfree(con);
  134. }
  135. static void conn_put(struct tipc_conn *con)
  136. {
  137. kref_put(&con->kref, tipc_conn_kref_release);
  138. }
  139. static void conn_get(struct tipc_conn *con)
  140. {
  141. kref_get(&con->kref);
  142. }
  143. static void tipc_conn_close(struct tipc_conn *con)
  144. {
  145. struct sock *sk = con->sock->sk;
  146. bool disconnect = false;
  147. write_lock_bh(&sk->sk_callback_lock);
  148. disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
  149. if (disconnect) {
  150. sk->sk_user_data = NULL;
  151. tipc_conn_delete_sub(con, NULL);
  152. }
  153. write_unlock_bh(&sk->sk_callback_lock);
  154. /* Handle concurrent calls from sending and receiving threads */
  155. if (!disconnect)
  156. return;
  157. /* Don't flush pending works, -just let them expire */
  158. kernel_sock_shutdown(con->sock, SHUT_RDWR);
  159. conn_put(con);
  160. }
  161. static struct tipc_conn *tipc_conn_alloc(struct tipc_topsrv *s)
  162. {
  163. struct tipc_conn *con;
  164. int ret;
  165. con = kzalloc(sizeof(*con), GFP_ATOMIC);
  166. if (!con)
  167. return ERR_PTR(-ENOMEM);
  168. kref_init(&con->kref);
  169. INIT_LIST_HEAD(&con->outqueue);
  170. INIT_LIST_HEAD(&con->sub_list);
  171. spin_lock_init(&con->outqueue_lock);
  172. spin_lock_init(&con->sub_lock);
  173. INIT_WORK(&con->swork, tipc_conn_send_work);
  174. INIT_WORK(&con->rwork, tipc_conn_recv_work);
  175. spin_lock_bh(&s->idr_lock);
  176. ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
  177. if (ret < 0) {
  178. kfree(con);
  179. spin_unlock_bh(&s->idr_lock);
  180. return ERR_PTR(-ENOMEM);
  181. }
  182. con->conid = ret;
  183. s->idr_in_use++;
  184. spin_unlock_bh(&s->idr_lock);
  185. set_bit(CF_CONNECTED, &con->flags);
  186. con->server = s;
  187. return con;
  188. }
  189. static struct tipc_conn *tipc_conn_lookup(struct tipc_topsrv *s, int conid)
  190. {
  191. struct tipc_conn *con;
  192. spin_lock_bh(&s->idr_lock);
  193. con = idr_find(&s->conn_idr, conid);
  194. if (!connected(con) || !kref_get_unless_zero(&con->kref))
  195. con = NULL;
  196. spin_unlock_bh(&s->idr_lock);
  197. return con;
  198. }
  199. /* tipc_conn_delete_sub - delete a specific or all subscriptions
  200. * for a given subscriber
  201. */
  202. static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
  203. {
  204. struct tipc_net *tn = tipc_net(con->server->net);
  205. struct list_head *sub_list = &con->sub_list;
  206. struct tipc_subscription *sub, *tmp;
  207. spin_lock_bh(&con->sub_lock);
  208. list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
  209. if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
  210. tipc_sub_unsubscribe(sub);
  211. atomic_dec(&tn->subscription_count);
  212. } else if (s) {
  213. break;
  214. }
  215. }
  216. spin_unlock_bh(&con->sub_lock);
  217. }
  218. static void tipc_conn_send_to_sock(struct tipc_conn *con)
  219. {
  220. struct list_head *queue = &con->outqueue;
  221. struct tipc_topsrv *srv = con->server;
  222. struct outqueue_entry *e;
  223. struct tipc_event *evt;
  224. struct msghdr msg;
  225. struct kvec iov;
  226. int count = 0;
  227. int ret;
  228. spin_lock_bh(&con->outqueue_lock);
  229. while (!list_empty(queue)) {
  230. e = list_first_entry(queue, struct outqueue_entry, list);
  231. evt = &e->evt;
  232. spin_unlock_bh(&con->outqueue_lock);
  233. if (e->inactive)
  234. tipc_conn_delete_sub(con, &evt->s);
  235. memset(&msg, 0, sizeof(msg));
  236. msg.msg_flags = MSG_DONTWAIT;
  237. iov.iov_base = evt;
  238. iov.iov_len = sizeof(*evt);
  239. msg.msg_name = NULL;
  240. if (con->sock) {
  241. ret = kernel_sendmsg(con->sock, &msg, &iov,
  242. 1, sizeof(*evt));
  243. if (ret == -EWOULDBLOCK || ret == 0) {
  244. cond_resched();
  245. return;
  246. } else if (ret < 0) {
  247. return tipc_conn_close(con);
  248. }
  249. } else {
  250. tipc_topsrv_kern_evt(srv->net, evt);
  251. }
  252. /* Don't starve users filling buffers */
  253. if (++count >= MAX_SEND_MSG_COUNT) {
  254. cond_resched();
  255. count = 0;
  256. }
  257. spin_lock_bh(&con->outqueue_lock);
  258. list_del(&e->list);
  259. kfree(e);
  260. }
  261. spin_unlock_bh(&con->outqueue_lock);
  262. }
  263. static void tipc_conn_send_work(struct work_struct *work)
  264. {
  265. struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
  266. if (connected(con))
  267. tipc_conn_send_to_sock(con);
  268. conn_put(con);
  269. }
  270. /* tipc_topsrv_queue_evt() - interrupt level call from a subscription instance
  271. * The queued work is launched into tipc_conn_send_work()->tipc_conn_send_to_sock()
  272. */
  273. void tipc_topsrv_queue_evt(struct net *net, int conid,
  274. u32 event, struct tipc_event *evt)
  275. {
  276. struct tipc_topsrv *srv = tipc_topsrv(net);
  277. struct outqueue_entry *e;
  278. struct tipc_conn *con;
  279. con = tipc_conn_lookup(srv, conid);
  280. if (!con)
  281. return;
  282. if (!connected(con))
  283. goto err;
  284. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  285. if (!e)
  286. goto err;
  287. e->inactive = (event == TIPC_SUBSCR_TIMEOUT);
  288. memcpy(&e->evt, evt, sizeof(*evt));
  289. spin_lock_bh(&con->outqueue_lock);
  290. list_add_tail(&e->list, &con->outqueue);
  291. spin_unlock_bh(&con->outqueue_lock);
  292. if (queue_work(srv->send_wq, &con->swork))
  293. return;
  294. err:
  295. conn_put(con);
  296. }
  297. /* tipc_conn_write_space - interrupt callback after a sendmsg EAGAIN
  298. * Indicates that there now is more space in the send buffer
  299. * The queued work is launched into tipc_send_work()->tipc_conn_send_to_sock()
  300. */
  301. static void tipc_conn_write_space(struct sock *sk)
  302. {
  303. struct tipc_conn *con;
  304. read_lock_bh(&sk->sk_callback_lock);
  305. con = sk->sk_user_data;
  306. if (connected(con)) {
  307. conn_get(con);
  308. if (!queue_work(con->server->send_wq, &con->swork))
  309. conn_put(con);
  310. }
  311. read_unlock_bh(&sk->sk_callback_lock);
  312. }
  313. static int tipc_conn_rcv_sub(struct tipc_topsrv *srv,
  314. struct tipc_conn *con,
  315. struct tipc_subscr *s)
  316. {
  317. struct tipc_net *tn = tipc_net(srv->net);
  318. struct tipc_subscription *sub;
  319. if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) {
  320. tipc_conn_delete_sub(con, s);
  321. return 0;
  322. }
  323. if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) {
  324. pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR);
  325. return -1;
  326. }
  327. sub = tipc_sub_subscribe(srv->net, s, con->conid);
  328. if (!sub)
  329. return -1;
  330. atomic_inc(&tn->subscription_count);
  331. spin_lock_bh(&con->sub_lock);
  332. list_add(&sub->sub_list, &con->sub_list);
  333. spin_unlock_bh(&con->sub_lock);
  334. return 0;
  335. }
  336. static int tipc_conn_rcv_from_sock(struct tipc_conn *con)
  337. {
  338. struct tipc_topsrv *srv = con->server;
  339. struct sock *sk = con->sock->sk;
  340. struct msghdr msg = {};
  341. struct tipc_subscr s;
  342. struct kvec iov;
  343. int ret;
  344. iov.iov_base = &s;
  345. iov.iov_len = sizeof(s);
  346. msg.msg_name = NULL;
  347. iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, iov.iov_len);
  348. ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT);
  349. if (ret == -EWOULDBLOCK)
  350. return -EWOULDBLOCK;
  351. if (ret > 0) {
  352. read_lock_bh(&sk->sk_callback_lock);
  353. ret = tipc_conn_rcv_sub(srv, con, &s);
  354. read_unlock_bh(&sk->sk_callback_lock);
  355. }
  356. if (ret < 0)
  357. tipc_conn_close(con);
  358. return ret;
  359. }
  360. static void tipc_conn_recv_work(struct work_struct *work)
  361. {
  362. struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
  363. int count = 0;
  364. while (connected(con)) {
  365. if (tipc_conn_rcv_from_sock(con))
  366. break;
  367. /* Don't flood Rx machine */
  368. if (++count >= MAX_RECV_MSG_COUNT) {
  369. cond_resched();
  370. count = 0;
  371. }
  372. }
  373. conn_put(con);
  374. }
  375. /* tipc_conn_data_ready - interrupt callback indicating the socket has data
  376. * The queued work is launched into tipc_recv_work()->tipc_conn_rcv_from_sock()
  377. */
  378. static void tipc_conn_data_ready(struct sock *sk)
  379. {
  380. struct tipc_conn *con;
  381. read_lock_bh(&sk->sk_callback_lock);
  382. con = sk->sk_user_data;
  383. if (connected(con)) {
  384. conn_get(con);
  385. if (!queue_work(con->server->rcv_wq, &con->rwork))
  386. conn_put(con);
  387. }
  388. read_unlock_bh(&sk->sk_callback_lock);
  389. }
  390. static void tipc_topsrv_accept(struct work_struct *work)
  391. {
  392. struct tipc_topsrv *srv = container_of(work, struct tipc_topsrv, awork);
  393. struct socket *lsock = srv->listener;
  394. struct socket *newsock;
  395. struct tipc_conn *con;
  396. struct sock *newsk;
  397. int ret;
  398. while (1) {
  399. ret = kernel_accept(lsock, &newsock, O_NONBLOCK);
  400. if (ret < 0)
  401. return;
  402. con = tipc_conn_alloc(srv);
  403. if (IS_ERR(con)) {
  404. ret = PTR_ERR(con);
  405. sock_release(newsock);
  406. return;
  407. }
  408. /* Register callbacks */
  409. newsk = newsock->sk;
  410. write_lock_bh(&newsk->sk_callback_lock);
  411. newsk->sk_data_ready = tipc_conn_data_ready;
  412. newsk->sk_write_space = tipc_conn_write_space;
  413. newsk->sk_user_data = con;
  414. con->sock = newsock;
  415. write_unlock_bh(&newsk->sk_callback_lock);
  416. /* Wake up receive process in case of 'SYN+' message */
  417. newsk->sk_data_ready(newsk);
  418. }
  419. }
  420. /* tipc_toprsv_listener_data_ready - interrupt callback with connection request
  421. * The queued job is launched into tipc_topsrv_accept()
  422. */
  423. static void tipc_topsrv_listener_data_ready(struct sock *sk)
  424. {
  425. struct tipc_topsrv *srv;
  426. read_lock_bh(&sk->sk_callback_lock);
  427. srv = sk->sk_user_data;
  428. if (srv->listener)
  429. queue_work(srv->rcv_wq, &srv->awork);
  430. read_unlock_bh(&sk->sk_callback_lock);
  431. }
  432. static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)
  433. {
  434. int imp = TIPC_CRITICAL_IMPORTANCE;
  435. struct socket *lsock = NULL;
  436. struct sockaddr_tipc saddr;
  437. struct sock *sk;
  438. int rc;
  439. rc = sock_create_kern(srv->net, AF_TIPC, SOCK_SEQPACKET, 0, &lsock);
  440. if (rc < 0)
  441. return rc;
  442. srv->listener = lsock;
  443. sk = lsock->sk;
  444. write_lock_bh(&sk->sk_callback_lock);
  445. sk->sk_data_ready = tipc_topsrv_listener_data_ready;
  446. sk->sk_user_data = srv;
  447. write_unlock_bh(&sk->sk_callback_lock);
  448. rc = kernel_setsockopt(lsock, SOL_TIPC, TIPC_IMPORTANCE,
  449. (char *)&imp, sizeof(imp));
  450. if (rc < 0)
  451. goto err;
  452. saddr.family = AF_TIPC;
  453. saddr.addrtype = TIPC_ADDR_NAMESEQ;
  454. saddr.addr.nameseq.type = TIPC_TOP_SRV;
  455. saddr.addr.nameseq.lower = TIPC_TOP_SRV;
  456. saddr.addr.nameseq.upper = TIPC_TOP_SRV;
  457. saddr.scope = TIPC_NODE_SCOPE;
  458. rc = kernel_bind(lsock, (struct sockaddr *)&saddr, sizeof(saddr));
  459. if (rc < 0)
  460. goto err;
  461. rc = kernel_listen(lsock, 0);
  462. if (rc < 0)
  463. goto err;
  464. /* As server's listening socket owner and creator is the same module,
  465. * we have to decrease TIPC module reference count to guarantee that
  466. * it remains zero after the server socket is created, otherwise,
  467. * executing "rmmod" command is unable to make TIPC module deleted
  468. * after TIPC module is inserted successfully.
  469. *
  470. * However, the reference count is ever increased twice in
  471. * sock_create_kern(): one is to increase the reference count of owner
  472. * of TIPC socket's proto_ops struct; another is to increment the
  473. * reference count of owner of TIPC proto struct. Therefore, we must
  474. * decrement the module reference count twice to ensure that it keeps
  475. * zero after server's listening socket is created. Of course, we
  476. * must bump the module reference count twice as well before the socket
  477. * is closed.
  478. */
  479. module_put(lsock->ops->owner);
  480. module_put(sk->sk_prot_creator->owner);
  481. return 0;
  482. err:
  483. sock_release(lsock);
  484. return -EINVAL;
  485. }
  486. bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
  487. u32 upper, u32 filter, int *conid)
  488. {
  489. struct tipc_subscr sub;
  490. struct tipc_conn *con;
  491. int rc;
  492. sub.seq.type = type;
  493. sub.seq.lower = lower;
  494. sub.seq.upper = upper;
  495. sub.timeout = TIPC_WAIT_FOREVER;
  496. sub.filter = filter;
  497. *(u32 *)&sub.usr_handle = port;
  498. con = tipc_conn_alloc(tipc_topsrv(net));
  499. if (IS_ERR(con))
  500. return false;
  501. *conid = con->conid;
  502. con->sock = NULL;
  503. rc = tipc_conn_rcv_sub(tipc_topsrv(net), con, &sub);
  504. if (rc >= 0)
  505. return true;
  506. conn_put(con);
  507. return false;
  508. }
  509. void tipc_topsrv_kern_unsubscr(struct net *net, int conid)
  510. {
  511. struct tipc_conn *con;
  512. con = tipc_conn_lookup(tipc_topsrv(net), conid);
  513. if (!con)
  514. return;
  515. test_and_clear_bit(CF_CONNECTED, &con->flags);
  516. tipc_conn_delete_sub(con, NULL);
  517. conn_put(con);
  518. conn_put(con);
  519. }
  520. static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt)
  521. {
  522. u32 port = *(u32 *)&evt->s.usr_handle;
  523. u32 self = tipc_own_addr(net);
  524. struct sk_buff_head evtq;
  525. struct sk_buff *skb;
  526. skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt),
  527. self, self, port, port, 0);
  528. if (!skb)
  529. return;
  530. msg_set_dest_droppable(buf_msg(skb), true);
  531. memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt));
  532. skb_queue_head_init(&evtq);
  533. __skb_queue_tail(&evtq, skb);
  534. tipc_sk_rcv(net, &evtq);
  535. }
  536. static int tipc_topsrv_work_start(struct tipc_topsrv *s)
  537. {
  538. s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
  539. if (!s->rcv_wq) {
  540. pr_err("can't start tipc receive workqueue\n");
  541. return -ENOMEM;
  542. }
  543. s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
  544. if (!s->send_wq) {
  545. pr_err("can't start tipc send workqueue\n");
  546. destroy_workqueue(s->rcv_wq);
  547. return -ENOMEM;
  548. }
  549. return 0;
  550. }
  551. static void tipc_topsrv_work_stop(struct tipc_topsrv *s)
  552. {
  553. destroy_workqueue(s->rcv_wq);
  554. destroy_workqueue(s->send_wq);
  555. }
  556. int tipc_topsrv_start(struct net *net)
  557. {
  558. struct tipc_net *tn = tipc_net(net);
  559. const char name[] = "topology_server";
  560. struct tipc_topsrv *srv;
  561. int ret;
  562. srv = kzalloc(sizeof(*srv), GFP_ATOMIC);
  563. if (!srv)
  564. return -ENOMEM;
  565. srv->net = net;
  566. srv->max_rcvbuf_size = sizeof(struct tipc_subscr);
  567. INIT_WORK(&srv->awork, tipc_topsrv_accept);
  568. strncpy(srv->name, name, strlen(name) + 1);
  569. tn->topsrv = srv;
  570. atomic_set(&tn->subscription_count, 0);
  571. spin_lock_init(&srv->idr_lock);
  572. idr_init(&srv->conn_idr);
  573. srv->idr_in_use = 0;
  574. ret = tipc_topsrv_work_start(srv);
  575. if (ret < 0)
  576. return ret;
  577. ret = tipc_topsrv_create_listener(srv);
  578. if (ret < 0)
  579. tipc_topsrv_work_stop(srv);
  580. return ret;
  581. }
  582. void tipc_topsrv_stop(struct net *net)
  583. {
  584. struct tipc_topsrv *srv = tipc_topsrv(net);
  585. struct socket *lsock = srv->listener;
  586. struct tipc_conn *con;
  587. int id;
  588. spin_lock_bh(&srv->idr_lock);
  589. for (id = 0; srv->idr_in_use; id++) {
  590. con = idr_find(&srv->conn_idr, id);
  591. if (con) {
  592. spin_unlock_bh(&srv->idr_lock);
  593. tipc_conn_close(con);
  594. spin_lock_bh(&srv->idr_lock);
  595. }
  596. }
  597. __module_get(lsock->ops->owner);
  598. __module_get(lsock->sk->sk_prot_creator->owner);
  599. srv->listener = NULL;
  600. spin_unlock_bh(&srv->idr_lock);
  601. sock_release(lsock);
  602. tipc_topsrv_work_stop(srv);
  603. idr_destroy(&srv->conn_idr);
  604. kfree(srv);
  605. }