server.c 18 KB

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