topsrv.c 19 KB

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