topsrv.c 19 KB

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